blob: ab006b113c38ed6b652a07cc407f7b555beed890 [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 Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (*key == NUL)
2787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (!quiet)
2789 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
2793 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002794 lp->ll_list = NULL;
2795 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002796 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002797
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002798 /* When assigning to a scope dictionary check that a function and
2799 * variable name is valid (only variable name unless it is l: or
2800 * g: dictionary). Disallow overwriting a builtin function. */
2801 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002802 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002803 int prevval;
2804 int wrong;
2805
2806 if (len != -1)
2807 {
2808 prevval = key[len];
2809 key[len] = NUL;
2810 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002811 else
2812 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002813 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2814 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002815 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002816 || !valid_varname(key);
2817 if (len != -1)
2818 key[len] = prevval;
2819 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 return NULL;
2821 }
2822
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002824 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002825 /* Can't add "v:" variable. */
2826 if (lp->ll_dict == &vimvardict)
2827 {
2828 EMSG2(_(e_illvar), name);
2829 return NULL;
2830 }
2831
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002832 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002836 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 if (len == -1)
2838 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 }
2841 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 if (len == -1)
2846 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 p = NULL;
2849 break;
2850 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002851 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002852 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002853 return NULL;
2854
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 if (len == -1)
2856 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 }
2859 else
2860 {
2861 /*
2862 * Get the number and item for the only or first index of the List.
2863 */
2864 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 else
2867 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002868 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 clear_tv(&var1);
2870 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 lp->ll_list = lp->ll_tv->vval.v_list;
2873 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2874 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002876 if (lp->ll_n1 < 0)
2877 {
2878 lp->ll_n1 = 0;
2879 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2880 }
2881 }
2882 if (lp->ll_li == NULL)
2883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002886 if (!quiet)
2887 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 }
2890
2891 /*
2892 * May need to find the item or absolute index for the second
2893 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 * When no index given: "lp->ll_empty2" is TRUE.
2895 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002898 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002899 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002900 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002904 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002905 {
2906 if (!quiet)
2907 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002909 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2914 if (lp->ll_n1 < 0)
2915 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2916 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002917 {
2918 if (!quiet)
2919 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002921 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002922 }
2923
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002925 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002926 }
2927
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 return p;
2929}
2930
2931/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002932 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 */
2934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936{
2937 vim_free(lp->ll_exp_name);
2938 vim_free(lp->ll_newkey);
2939}
2940
2941/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002944 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 */
2946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002947set_var_lval(
2948 lval_T *lp,
2949 char_u *endp,
2950 typval_T *rettv,
2951 int copy,
2952 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953{
2954 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002955 listitem_T *ri;
2956 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957
2958 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002961 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 cc = *endp;
2963 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 if (op != NULL && *op != '=')
2965 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002966 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002967
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002968 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002969 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002970 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002971 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002972 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002973 if ((di == NULL
2974 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2975 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2976 FALSE)))
2977 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 set_var(lp->ll_name, &tv, FALSE);
2979 clear_tv(&tv);
2980 }
2981 }
2982 else
2983 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002985 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002987 else if (tv_check_lock(lp->ll_newkey == NULL
2988 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002989 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002990 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002991 else if (lp->ll_range)
2992 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002993 listitem_T *ll_li = lp->ll_li;
2994 int ll_n1 = lp->ll_n1;
2995
2996 /*
2997 * Check whether any of the list items is locked
2998 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002999 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003000 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003001 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003002 return;
3003 ri = ri->li_next;
3004 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3005 break;
3006 ll_li = ll_li->li_next;
3007 ++ll_n1;
3008 }
3009
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 /*
3011 * Assign the List values to the list items.
3012 */
3013 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 if (op != NULL && *op != '=')
3016 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3017 else
3018 {
3019 clear_tv(&lp->ll_li->li_tv);
3020 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3021 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 ri = ri->li_next;
3023 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3024 break;
3025 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003026 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003028 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 ri = NULL;
3031 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003032 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003033 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 lp->ll_li = lp->ll_li->li_next;
3035 ++lp->ll_n1;
3036 }
3037 if (ri != NULL)
3038 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 else if (lp->ll_empty2
3040 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 : lp->ll_n1 != lp->ll_n2)
3042 EMSG(_("E711: List value has not enough items"));
3043 }
3044 else
3045 {
3046 /*
3047 * Assign to a List or Dictionary item.
3048 */
3049 if (lp->ll_newkey != NULL)
3050 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003051 if (op != NULL && *op != '=')
3052 {
3053 EMSG2(_(e_letwrong), op);
3054 return;
3055 }
3056
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003058 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 if (di == NULL)
3060 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003061 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3062 {
3063 vim_free(di);
3064 return;
3065 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003068 else if (op != NULL && *op != '=')
3069 {
3070 tv_op(lp->ll_tv, rettv, op);
3071 return;
3072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003073 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003075
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003076 /*
3077 * Assign the value to the variable or list item.
3078 */
3079 if (copy)
3080 copy_tv(rettv, lp->ll_tv);
3081 else
3082 {
3083 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003084 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003085 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003086 }
3087 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003088}
3089
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003090/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003091 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3092 * Returns OK or FAIL.
3093 */
3094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003095tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096{
3097 long n;
3098 char_u numbuf[NUMBUFLEN];
3099 char_u *s;
3100
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003101 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3102 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3103 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003104 {
3105 switch (tv1->v_type)
3106 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003107 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003108 case VAR_DICT:
3109 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003110 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003111 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003112 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003113 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 break;
3115
3116 case VAR_LIST:
3117 if (*op != '+' || tv2->v_type != VAR_LIST)
3118 break;
3119 /* List += List */
3120 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3121 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3122 return OK;
3123
3124 case VAR_NUMBER:
3125 case VAR_STRING:
3126 if (tv2->v_type == VAR_LIST)
3127 break;
3128 if (*op == '+' || *op == '-')
3129 {
3130 /* nr += nr or nr -= nr*/
3131 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003132#ifdef FEAT_FLOAT
3133 if (tv2->v_type == VAR_FLOAT)
3134 {
3135 float_T f = n;
3136
3137 if (*op == '+')
3138 f += tv2->vval.v_float;
3139 else
3140 f -= tv2->vval.v_float;
3141 clear_tv(tv1);
3142 tv1->v_type = VAR_FLOAT;
3143 tv1->vval.v_float = f;
3144 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003145 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003146#endif
3147 {
3148 if (*op == '+')
3149 n += get_tv_number(tv2);
3150 else
3151 n -= get_tv_number(tv2);
3152 clear_tv(tv1);
3153 tv1->v_type = VAR_NUMBER;
3154 tv1->vval.v_number = n;
3155 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003156 }
3157 else
3158 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 if (tv2->v_type == VAR_FLOAT)
3160 break;
3161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003162 /* str .= str */
3163 s = get_tv_string(tv1);
3164 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3165 clear_tv(tv1);
3166 tv1->v_type = VAR_STRING;
3167 tv1->vval.v_string = s;
3168 }
3169 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003170
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003172#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173 {
3174 float_T f;
3175
3176 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3177 && tv2->v_type != VAR_NUMBER
3178 && tv2->v_type != VAR_STRING))
3179 break;
3180 if (tv2->v_type == VAR_FLOAT)
3181 f = tv2->vval.v_float;
3182 else
3183 f = get_tv_number(tv2);
3184 if (*op == '+')
3185 tv1->vval.v_float += f;
3186 else
3187 tv1->vval.v_float -= f;
3188 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003190 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003191 }
3192 }
3193
3194 EMSG2(_(e_letwrong), op);
3195 return FAIL;
3196}
3197
3198/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003199 * Add a watcher to a list.
3200 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003203{
3204 lw->lw_next = l->lv_watch;
3205 l->lv_watch = lw;
3206}
3207
3208/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003209 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003210 * No warning when it isn't found...
3211 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003213list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214{
Bram Moolenaar33570922005-01-25 22:26:29 +00003215 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216
3217 lwp = &l->lv_watch;
3218 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3219 {
3220 if (lw == lwrem)
3221 {
3222 *lwp = lw->lw_next;
3223 break;
3224 }
3225 lwp = &lw->lw_next;
3226 }
3227}
3228
3229/*
3230 * Just before removing an item from a list: advance watchers to the next
3231 * item.
3232 */
3233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003234list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235{
Bram Moolenaar33570922005-01-25 22:26:29 +00003236 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237
3238 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3239 if (lw->lw_item == item)
3240 lw->lw_item = item->li_next;
3241}
3242
3243/*
3244 * Evaluate the expression used in a ":for var in expr" command.
3245 * "arg" points to "var".
3246 * Set "*errp" to TRUE for an error, FALSE otherwise;
3247 * Return a pointer that holds the info. Null when there is an error.
3248 */
3249 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003250eval_for_line(
3251 char_u *arg,
3252 int *errp,
3253 char_u **nextcmdp,
3254 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255{
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003257 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003258 typval_T tv;
3259 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260
3261 *errp = TRUE; /* default: there is an error */
3262
Bram Moolenaar33570922005-01-25 22:26:29 +00003263 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 if (fi == NULL)
3265 return NULL;
3266
3267 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3268 if (expr == NULL)
3269 return fi;
3270
3271 expr = skipwhite(expr);
3272 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3273 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003274 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 return fi;
3276 }
3277
3278 if (skip)
3279 ++emsg_skip;
3280 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3281 {
3282 *errp = FALSE;
3283 if (!skip)
3284 {
3285 l = tv.vval.v_list;
3286 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003289 clear_tv(&tv);
3290 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 else
3292 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003293 /* No need to increment the refcount, it's already set for the
3294 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 fi->fi_list = l;
3296 list_add_watch(l, &fi->fi_lw);
3297 fi->fi_lw.lw_item = l->lv_first;
3298 }
3299 }
3300 }
3301 if (skip)
3302 --emsg_skip;
3303
3304 return fi;
3305}
3306
3307/*
3308 * Use the first item in a ":for" list. Advance to the next.
3309 * Assign the values to the variable (list). "arg" points to the first one.
3310 * Return TRUE when a valid item was found, FALSE when at end of list or
3311 * something wrong.
3312 */
3313 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003314next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003316 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003318 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319
3320 item = fi->fi_lw.lw_item;
3321 if (item == NULL)
3322 result = FALSE;
3323 else
3324 {
3325 fi->fi_lw.lw_item = item->li_next;
3326 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3327 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3328 }
3329 return result;
3330}
3331
3332/*
3333 * Free the structure used to store info used by ":for".
3334 */
3335 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003336free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337{
Bram Moolenaar33570922005-01-25 22:26:29 +00003338 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003340 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003341 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003343 list_unref(fi->fi_list);
3344 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 vim_free(fi);
3346}
3347
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3349
3350 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003351set_context_for_expression(
3352 expand_T *xp,
3353 char_u *arg,
3354 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
3356 int got_eq = FALSE;
3357 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360 if (cmdidx == CMD_let)
3361 {
3362 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003363 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003364 {
3365 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003366 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003367 {
3368 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003369 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003370 if (vim_iswhite(*p))
3371 break;
3372 }
3373 return;
3374 }
3375 }
3376 else
3377 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3378 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 while ((xp->xp_pattern = vim_strpbrk(arg,
3380 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3381 {
3382 c = *xp->xp_pattern;
3383 if (c == '&')
3384 {
3385 c = xp->xp_pattern[1];
3386 if (c == '&')
3387 {
3388 ++xp->xp_pattern;
3389 xp->xp_context = cmdidx != CMD_let || got_eq
3390 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3391 }
3392 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003393 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003395 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3396 xp->xp_pattern += 2;
3397
3398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 }
3400 else if (c == '$')
3401 {
3402 /* environment variable */
3403 xp->xp_context = EXPAND_ENV_VARS;
3404 }
3405 else if (c == '=')
3406 {
3407 got_eq = TRUE;
3408 xp->xp_context = EXPAND_EXPRESSION;
3409 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003410 else if (c == '#'
3411 && xp->xp_context == EXPAND_EXPRESSION)
3412 {
3413 /* Autoload function/variable contains '#'. */
3414 break;
3415 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003416 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 && xp->xp_context == EXPAND_FUNCTIONS
3418 && vim_strchr(xp->xp_pattern, '(') == NULL)
3419 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003420 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 break;
3422 }
3423 else if (cmdidx != CMD_let || got_eq)
3424 {
3425 if (c == '"') /* string */
3426 {
3427 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3428 if (c == '\\' && xp->xp_pattern[1] != NUL)
3429 ++xp->xp_pattern;
3430 xp->xp_context = EXPAND_NOTHING;
3431 }
3432 else if (c == '\'') /* literal string */
3433 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003434 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3436 /* skip */ ;
3437 xp->xp_context = EXPAND_NOTHING;
3438 }
3439 else if (c == '|')
3440 {
3441 if (xp->xp_pattern[1] == '|')
3442 {
3443 ++xp->xp_pattern;
3444 xp->xp_context = EXPAND_EXPRESSION;
3445 }
3446 else
3447 xp->xp_context = EXPAND_COMMANDS;
3448 }
3449 else
3450 xp->xp_context = EXPAND_EXPRESSION;
3451 }
3452 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003453 /* Doesn't look like something valid, expand as an expression
3454 * anyway. */
3455 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 arg = xp->xp_pattern;
3457 if (*arg != NUL)
3458 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3459 /* skip */ ;
3460 }
3461 xp->xp_pattern = arg;
3462}
3463
3464#endif /* FEAT_CMDL_COMPL */
3465
3466/*
3467 * ":1,25call func(arg1, arg2)" function call.
3468 */
3469 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003470ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
3472 char_u *arg = eap->arg;
3473 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003475 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003477 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 linenr_T lnum;
3479 int doesrange;
3480 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003481 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003482 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003484 if (eap->skip)
3485 {
3486 /* trans_function_name() doesn't work well when skipping, use eval0()
3487 * instead to skip to any following command, e.g. for:
3488 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003489 ++emsg_skip;
3490 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3491 clear_tv(&rettv);
3492 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003493 return;
3494 }
3495
Bram Moolenaar65639032016-03-16 21:40:30 +01003496 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003497 if (fudi.fd_newkey != NULL)
3498 {
3499 /* Still need to give an error message for missing key. */
3500 EMSG2(_(e_dictkey), fudi.fd_newkey);
3501 vim_free(fudi.fd_newkey);
3502 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003503 if (tofree == NULL)
3504 return;
3505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003506 /* Increase refcount on dictionary, it could get deleted when evaluating
3507 * the arguments. */
3508 if (fudi.fd_dict != NULL)
3509 ++fudi.fd_dict->dv_refcount;
3510
Bram Moolenaar65639032016-03-16 21:40:30 +01003511 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3512 * contents. For VAR_PARTIAL get its partial, unless we already have one
3513 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003514 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003515 name = deref_func_name(tofree, &len,
3516 partial != NULL ? NULL : &partial, FALSE);
3517
Bram Moolenaar532c7802005-01-27 14:44:31 +00003518 /* Skip white space to allow ":call func ()". Not good, but required for
3519 * backward compatibility. */
3520 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003521 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
3523 if (*startarg != '(')
3524 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003525 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 goto end;
3527 }
3528
3529 /*
3530 * When skipping, evaluate the function once, to find the end of the
3531 * arguments.
3532 * When the function takes a range, this is discovered after the first
3533 * call, and the loop is broken.
3534 */
3535 if (eap->skip)
3536 {
3537 ++emsg_skip;
3538 lnum = eap->line2; /* do it once, also with an invalid range */
3539 }
3540 else
3541 lnum = eap->line1;
3542 for ( ; lnum <= eap->line2; ++lnum)
3543 {
3544 if (!eap->skip && eap->addr_count > 0)
3545 {
3546 curwin->w_cursor.lnum = lnum;
3547 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003548#ifdef FEAT_VIRTUALEDIT
3549 curwin->w_cursor.coladd = 0;
3550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 }
3552 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003553 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003554 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003555 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
3557 failed = TRUE;
3558 break;
3559 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003560
3561 /* Handle a function returning a Funcref, Dictionary or List. */
3562 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3563 {
3564 failed = TRUE;
3565 break;
3566 }
3567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003568 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (doesrange || eap->skip)
3570 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003571
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003573 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003574 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003575 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 if (aborting())
3577 break;
3578 }
3579 if (eap->skip)
3580 --emsg_skip;
3581
3582 if (!failed)
3583 {
3584 /* Check for trailing illegal characters and a following command. */
3585 if (!ends_excmd(*arg))
3586 {
3587 emsg_severe = TRUE;
3588 EMSG(_(e_trailing));
3589 }
3590 else
3591 eap->nextcmd = check_nextcmd(arg);
3592 }
3593
3594end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003595 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003596 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597}
3598
3599/*
3600 * ":unlet[!] var1 ... " command.
3601 */
3602 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003603ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605 ex_unletlock(eap, eap->arg, 0);
3606}
3607
3608/*
3609 * ":lockvar" and ":unlockvar" commands
3610 */
3611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 int deep = 2;
3616
3617 if (eap->forceit)
3618 deep = -1;
3619 else if (vim_isdigit(*arg))
3620 {
3621 deep = getdigits(&arg);
3622 arg = skipwhite(arg);
3623 }
3624
3625 ex_unletlock(eap, arg, deep);
3626}
3627
3628/*
3629 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3630 */
3631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632ex_unletlock(
3633 exarg_T *eap,
3634 char_u *argstart,
3635 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636{
3637 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003640 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 do
3643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003645 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003646 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003647 if (lv.ll_name == NULL)
3648 error = TRUE; /* error but continue parsing */
3649 if (name_end == NULL || (!vim_iswhite(*name_end)
3650 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003652 if (name_end != NULL)
3653 {
3654 emsg_severe = TRUE;
3655 EMSG(_(e_trailing));
3656 }
3657 if (!(eap->skip || error))
3658 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 break;
3660 }
3661
3662 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 {
3664 if (eap->cmdidx == CMD_unlet)
3665 {
3666 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3667 error = TRUE;
3668 }
3669 else
3670 {
3671 if (do_lock_var(&lv, name_end, deep,
3672 eap->cmdidx == CMD_lockvar) == FAIL)
3673 error = TRUE;
3674 }
3675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 if (!eap->skip)
3678 clear_lval(&lv);
3679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 arg = skipwhite(name_end);
3681 } while (!ends_excmd(*arg));
3682
3683 eap->nextcmd = check_nextcmd(arg);
3684}
3685
Bram Moolenaar8c711452005-01-14 21:53:12 +00003686 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003687do_unlet_var(
3688 lval_T *lp,
3689 char_u *name_end,
3690 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 int ret = OK;
3693 int cc;
3694
3695 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003696 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 cc = *name_end;
3698 *name_end = NUL;
3699
3700 /* Normal name or expanded name. */
3701 if (check_changedtick(lp->ll_name))
3702 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003703 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003705 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003706 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003707 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003708 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003709 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003710 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003711 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 else if (lp->ll_range)
3713 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003714 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003715 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003716 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003717
3718 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3719 {
3720 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003721 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003722 return FAIL;
3723 ll_li = li;
3724 ++ll_n1;
3725 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726
3727 /* Delete a range of List items. */
3728 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3729 {
3730 li = lp->ll_li->li_next;
3731 listitem_remove(lp->ll_list, lp->ll_li);
3732 lp->ll_li = li;
3733 ++lp->ll_n1;
3734 }
3735 }
3736 else
3737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003738 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003739 /* unlet a List item. */
3740 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003741 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003742 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003743 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003744 }
3745
3746 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003747}
3748
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749/*
3750 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003751 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 */
3753 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003754do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755{
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 hashtab_T *ht;
3757 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003758 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003759 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003760 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
Bram Moolenaar33570922005-01-25 22:26:29 +00003762 ht = find_var_ht(name, &varname);
3763 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003765 if (ht == &globvarht)
3766 d = &globvardict;
3767 else if (current_funccal != NULL
3768 && ht == &current_funccal->l_vars.dv_hashtab)
3769 d = &current_funccal->l_vars;
3770 else if (ht == &compat_hashtab)
3771 d = &vimvardict;
3772 else
3773 {
3774 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3775 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3776 }
3777 if (d == NULL)
3778 {
3779 EMSG2(_(e_intern2), "do_unlet()");
3780 return FAIL;
3781 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003782 hi = hash_find(ht, varname);
3783 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003784 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003785 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003786 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003787 || var_check_ro(di->di_flags, name, FALSE)
3788 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003789 return FAIL;
3790
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003791 delete_var(ht, hi);
3792 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003795 if (forceit)
3796 return OK;
3797 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 return FAIL;
3799}
3800
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003801/*
3802 * Lock or unlock variable indicated by "lp".
3803 * "deep" is the levels to go (-1 for unlimited);
3804 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3805 */
3806 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003807do_lock_var(
3808 lval_T *lp,
3809 char_u *name_end,
3810 int deep,
3811 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003812{
3813 int ret = OK;
3814 int cc;
3815 dictitem_T *di;
3816
3817 if (deep == 0) /* nothing to do */
3818 return OK;
3819
3820 if (lp->ll_tv == NULL)
3821 {
3822 cc = *name_end;
3823 *name_end = NUL;
3824
3825 /* Normal name or expanded name. */
3826 if (check_changedtick(lp->ll_name))
3827 ret = FAIL;
3828 else
3829 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003830 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003831 if (di == NULL)
3832 ret = FAIL;
3833 else
3834 {
3835 if (lock)
3836 di->di_flags |= DI_FLAGS_LOCK;
3837 else
3838 di->di_flags &= ~DI_FLAGS_LOCK;
3839 item_lock(&di->di_tv, deep, lock);
3840 }
3841 }
3842 *name_end = cc;
3843 }
3844 else if (lp->ll_range)
3845 {
3846 listitem_T *li = lp->ll_li;
3847
3848 /* (un)lock a range of List items. */
3849 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3850 {
3851 item_lock(&li->li_tv, deep, lock);
3852 li = li->li_next;
3853 ++lp->ll_n1;
3854 }
3855 }
3856 else if (lp->ll_list != NULL)
3857 /* (un)lock a List item. */
3858 item_lock(&lp->ll_li->li_tv, deep, lock);
3859 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003860 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003861 item_lock(&lp->ll_di->di_tv, deep, lock);
3862
3863 return ret;
3864}
3865
3866/*
3867 * Lock or unlock an item. "deep" is nr of levels to go.
3868 */
3869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003870item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003871{
3872 static int recurse = 0;
3873 list_T *l;
3874 listitem_T *li;
3875 dict_T *d;
3876 hashitem_T *hi;
3877 int todo;
3878
3879 if (recurse >= DICT_MAXNEST)
3880 {
3881 EMSG(_("E743: variable nested too deep for (un)lock"));
3882 return;
3883 }
3884 if (deep == 0)
3885 return;
3886 ++recurse;
3887
3888 /* lock/unlock the item itself */
3889 if (lock)
3890 tv->v_lock |= VAR_LOCKED;
3891 else
3892 tv->v_lock &= ~VAR_LOCKED;
3893
3894 switch (tv->v_type)
3895 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003896 case VAR_UNKNOWN:
3897 case VAR_NUMBER:
3898 case VAR_STRING:
3899 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003900 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003901 case VAR_FLOAT:
3902 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003903 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003904 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003905 break;
3906
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003907 case VAR_LIST:
3908 if ((l = tv->vval.v_list) != NULL)
3909 {
3910 if (lock)
3911 l->lv_lock |= VAR_LOCKED;
3912 else
3913 l->lv_lock &= ~VAR_LOCKED;
3914 if (deep < 0 || deep > 1)
3915 /* recursive: lock/unlock the items the List contains */
3916 for (li = l->lv_first; li != NULL; li = li->li_next)
3917 item_lock(&li->li_tv, deep - 1, lock);
3918 }
3919 break;
3920 case VAR_DICT:
3921 if ((d = tv->vval.v_dict) != NULL)
3922 {
3923 if (lock)
3924 d->dv_lock |= VAR_LOCKED;
3925 else
3926 d->dv_lock &= ~VAR_LOCKED;
3927 if (deep < 0 || deep > 1)
3928 {
3929 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003930 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003931 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3932 {
3933 if (!HASHITEM_EMPTY(hi))
3934 {
3935 --todo;
3936 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3937 }
3938 }
3939 }
3940 }
3941 }
3942 --recurse;
3943}
3944
Bram Moolenaara40058a2005-07-11 22:42:07 +00003945/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003946 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3947 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003948 */
3949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003950tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003951{
3952 return (tv->v_lock & VAR_LOCKED)
3953 || (tv->v_type == VAR_LIST
3954 && tv->vval.v_list != NULL
3955 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3956 || (tv->v_type == VAR_DICT
3957 && tv->vval.v_dict != NULL
3958 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3959}
3960
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3962/*
3963 * Delete all "menutrans_" variables.
3964 */
3965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003966del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967{
Bram Moolenaar33570922005-01-25 22:26:29 +00003968 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003969 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003972 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003973 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003974 {
3975 if (!HASHITEM_EMPTY(hi))
3976 {
3977 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003978 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3979 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003980 }
3981 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983}
3984#endif
3985
3986#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3987
3988/*
3989 * Local string buffer for the next two functions to store a variable name
3990 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3991 * get_user_var_name().
3992 */
3993
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003994static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996static char_u *varnamebuf = NULL;
3997static int varnamebuflen = 0;
3998
3999/*
4000 * Function to concatenate a prefix and a variable name.
4001 */
4002 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004003cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 int len;
4006
4007 len = (int)STRLEN(name) + 3;
4008 if (len > varnamebuflen)
4009 {
4010 vim_free(varnamebuf);
4011 len += 10; /* some additional space */
4012 varnamebuf = alloc(len);
4013 if (varnamebuf == NULL)
4014 {
4015 varnamebuflen = 0;
4016 return NULL;
4017 }
4018 varnamebuflen = len;
4019 }
4020 *varnamebuf = prefix;
4021 varnamebuf[1] = ':';
4022 STRCPY(varnamebuf + 2, name);
4023 return varnamebuf;
4024}
4025
4026/*
4027 * Function given to ExpandGeneric() to obtain the list of user defined
4028 * (global/buffer/window/built-in) variable names.
4029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004031get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004033 static long_u gdone;
4034 static long_u bdone;
4035 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004036#ifdef FEAT_WINDOWS
4037 static long_u tdone;
4038#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004039 static int vidx;
4040 static hashitem_T *hi;
4041 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042
4043 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004044 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004045 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004046#ifdef FEAT_WINDOWS
4047 tdone = 0;
4048#endif
4049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050
4051 /* Global variables */
4052 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 else
4057 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004058 while (HASHITEM_EMPTY(hi))
4059 ++hi;
4060 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4061 return cat_prefix_varname('g', hi->hi_key);
4062 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004064
4065 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004066 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004067 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004069 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004071 else
4072 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004073 while (HASHITEM_EMPTY(hi))
4074 ++hi;
4075 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004077 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004079 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 return (char_u *)"b:changedtick";
4081 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004082
4083 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004084 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004087 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004088 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004089 else
4090 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004091 while (HASHITEM_EMPTY(hi))
4092 ++hi;
4093 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004095
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004096#ifdef FEAT_WINDOWS
4097 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004098 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004099 if (tdone < ht->ht_used)
4100 {
4101 if (tdone++ == 0)
4102 hi = ht->ht_array;
4103 else
4104 ++hi;
4105 while (HASHITEM_EMPTY(hi))
4106 ++hi;
4107 return cat_prefix_varname('t', hi->hi_key);
4108 }
4109#endif
4110
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 /* v: variables */
4112 if (vidx < VV_LEN)
4113 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 vim_free(varnamebuf);
4116 varnamebuf = NULL;
4117 varnamebuflen = 0;
4118 return NULL;
4119}
4120
4121#endif /* FEAT_CMDL_COMPL */
4122
4123/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004124 * Return TRUE if "pat" matches "text".
4125 * Does not use 'cpo' and always uses 'magic'.
4126 */
4127 static int
4128pattern_match(char_u *pat, char_u *text, int ic)
4129{
4130 int matches = FALSE;
4131 char_u *save_cpo;
4132 regmatch_T regmatch;
4133
4134 /* avoid 'l' flag in 'cpoptions' */
4135 save_cpo = p_cpo;
4136 p_cpo = (char_u *)"";
4137 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4138 if (regmatch.regprog != NULL)
4139 {
4140 regmatch.rm_ic = ic;
4141 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4142 vim_regfree(regmatch.regprog);
4143 }
4144 p_cpo = save_cpo;
4145 return matches;
4146}
4147
4148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 * types for expressions.
4150 */
4151typedef enum
4152{
4153 TYPE_UNKNOWN = 0
4154 , TYPE_EQUAL /* == */
4155 , TYPE_NEQUAL /* != */
4156 , TYPE_GREATER /* > */
4157 , TYPE_GEQUAL /* >= */
4158 , TYPE_SMALLER /* < */
4159 , TYPE_SEQUAL /* <= */
4160 , TYPE_MATCH /* =~ */
4161 , TYPE_NOMATCH /* !~ */
4162} exptype_T;
4163
4164/*
4165 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004166 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4168 */
4169
4170/*
4171 * Handle zero level expression.
4172 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004173 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004174 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 * Return OK or FAIL.
4176 */
4177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004178eval0(
4179 char_u *arg,
4180 typval_T *rettv,
4181 char_u **nextcmd,
4182 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
4184 int ret;
4185 char_u *p;
4186
4187 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 if (ret == FAIL || !ends_excmd(*p))
4190 {
4191 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 /*
4194 * Report the invalid expression unless the expression evaluation has
4195 * been cancelled due to an aborting error, an interrupt, or an
4196 * exception.
4197 */
4198 if (!aborting())
4199 EMSG2(_(e_invexpr2), arg);
4200 ret = FAIL;
4201 }
4202 if (nextcmd != NULL)
4203 *nextcmd = check_nextcmd(p);
4204
4205 return ret;
4206}
4207
4208/*
4209 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004210 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 *
4212 * "arg" must point to the first non-white of the expression.
4213 * "arg" is advanced to the next non-white after the recognized expression.
4214 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004215 * Note: "rettv.v_lock" is not set.
4216 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 * Return OK or FAIL.
4218 */
4219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004220eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
4222 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004223 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225 /*
4226 * Get the first variable.
4227 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 return FAIL;
4230
4231 if ((*arg)[0] == '?')
4232 {
4233 result = FALSE;
4234 if (evaluate)
4235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004236 int error = FALSE;
4237
4238 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (error)
4242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
4244
4245 /*
4246 * Get the second variable.
4247 */
4248 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 return FAIL;
4251
4252 /*
4253 * Check for the ":".
4254 */
4255 if ((*arg)[0] != ':')
4256 {
4257 EMSG(_("E109: Missing ':' after '?'"));
4258 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004259 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 return FAIL;
4261 }
4262
4263 /*
4264 * Get the third variable.
4265 */
4266 *arg = skipwhite(*arg + 1);
4267 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4268 {
4269 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 return FAIL;
4272 }
4273 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276
4277 return OK;
4278}
4279
4280/*
4281 * Handle first level expression:
4282 * expr2 || expr2 || expr2 logical OR
4283 *
4284 * "arg" must point to the first non-white of the expression.
4285 * "arg" is advanced to the next non-white after the recognized expression.
4286 *
4287 * Return OK or FAIL.
4288 */
4289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004290eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291{
Bram Moolenaar33570922005-01-25 22:26:29 +00004292 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 long result;
4294 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296
4297 /*
4298 * Get the first variable.
4299 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 return FAIL;
4302
4303 /*
4304 * Repeat until there is no following "||".
4305 */
4306 first = TRUE;
4307 result = FALSE;
4308 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4309 {
4310 if (evaluate && first)
4311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004312 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 if (error)
4316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 first = FALSE;
4318 }
4319
4320 /*
4321 * Get the second variable.
4322 */
4323 *arg = skipwhite(*arg + 2);
4324 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4325 return FAIL;
4326
4327 /*
4328 * Compute the result.
4329 */
4330 if (evaluate && !result)
4331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004332 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004334 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (error)
4336 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338 if (evaluate)
4339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004340 rettv->v_type = VAR_NUMBER;
4341 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 }
4343 }
4344
4345 return OK;
4346}
4347
4348/*
4349 * Handle second level expression:
4350 * expr3 && expr3 && expr3 logical AND
4351 *
4352 * "arg" must point to the first non-white of the expression.
4353 * "arg" is advanced to the next non-white after the recognized expression.
4354 *
4355 * Return OK or FAIL.
4356 */
4357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004358eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359{
Bram Moolenaar33570922005-01-25 22:26:29 +00004360 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 long result;
4362 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
4365 /*
4366 * Get the first variable.
4367 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 return FAIL;
4370
4371 /*
4372 * Repeat until there is no following "&&".
4373 */
4374 first = TRUE;
4375 result = TRUE;
4376 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4377 {
4378 if (evaluate && first)
4379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004380 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004383 if (error)
4384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 first = FALSE;
4386 }
4387
4388 /*
4389 * Get the second variable.
4390 */
4391 *arg = skipwhite(*arg + 2);
4392 if (eval4(arg, &var2, evaluate && result) == FAIL)
4393 return FAIL;
4394
4395 /*
4396 * Compute the result.
4397 */
4398 if (evaluate && result)
4399 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004400 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 if (error)
4404 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 }
4406 if (evaluate)
4407 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408 rettv->v_type = VAR_NUMBER;
4409 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 }
4412
4413 return OK;
4414}
4415
4416/*
4417 * Handle third level expression:
4418 * var1 == var2
4419 * var1 =~ var2
4420 * var1 != var2
4421 * var1 !~ var2
4422 * var1 > var2
4423 * var1 >= var2
4424 * var1 < var2
4425 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 * var1 is var2
4427 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 *
4429 * "arg" must point to the first non-white of the expression.
4430 * "arg" is advanced to the next non-white after the recognized expression.
4431 *
4432 * Return OK or FAIL.
4433 */
4434 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004435eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436{
Bram Moolenaar33570922005-01-25 22:26:29 +00004437 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 char_u *p;
4439 int i;
4440 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 int len = 2;
4443 long n1, n2;
4444 char_u *s1, *s2;
4445 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 /*
4449 * Get the first variable.
4450 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004451 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 return FAIL;
4453
4454 p = *arg;
4455 switch (p[0])
4456 {
4457 case '=': if (p[1] == '=')
4458 type = TYPE_EQUAL;
4459 else if (p[1] == '~')
4460 type = TYPE_MATCH;
4461 break;
4462 case '!': if (p[1] == '=')
4463 type = TYPE_NEQUAL;
4464 else if (p[1] == '~')
4465 type = TYPE_NOMATCH;
4466 break;
4467 case '>': if (p[1] != '=')
4468 {
4469 type = TYPE_GREATER;
4470 len = 1;
4471 }
4472 else
4473 type = TYPE_GEQUAL;
4474 break;
4475 case '<': if (p[1] != '=')
4476 {
4477 type = TYPE_SMALLER;
4478 len = 1;
4479 }
4480 else
4481 type = TYPE_SEQUAL;
4482 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004483 case 'i': if (p[1] == 's')
4484 {
4485 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4486 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004487 i = p[len];
4488 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004489 {
4490 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4491 type_is = TRUE;
4492 }
4493 }
4494 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496
4497 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004498 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 */
4500 if (type != TYPE_UNKNOWN)
4501 {
4502 /* extra question mark appended: ignore case */
4503 if (p[len] == '?')
4504 {
4505 ic = TRUE;
4506 ++len;
4507 }
4508 /* extra '#' appended: match case */
4509 else if (p[len] == '#')
4510 {
4511 ic = FALSE;
4512 ++len;
4513 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004514 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 else
4516 ic = p_ic;
4517
4518 /*
4519 * Get the second variable.
4520 */
4521 *arg = skipwhite(p + len);
4522 if (eval5(arg, &var2, evaluate) == FAIL)
4523 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004524 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 return FAIL;
4526 }
4527
4528 if (evaluate)
4529 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004530 if (type_is && rettv->v_type != var2.v_type)
4531 {
4532 /* For "is" a different type always means FALSE, for "notis"
4533 * it means TRUE. */
4534 n1 = (type == TYPE_NEQUAL);
4535 }
4536 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4537 {
4538 if (type_is)
4539 {
4540 n1 = (rettv->v_type == var2.v_type
4541 && rettv->vval.v_list == var2.vval.v_list);
4542 if (type == TYPE_NEQUAL)
4543 n1 = !n1;
4544 }
4545 else if (rettv->v_type != var2.v_type
4546 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4547 {
4548 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004549 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004551 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 clear_tv(rettv);
4553 clear_tv(&var2);
4554 return FAIL;
4555 }
4556 else
4557 {
4558 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004559 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4560 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 if (type == TYPE_NEQUAL)
4562 n1 = !n1;
4563 }
4564 }
4565
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004566 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4567 {
4568 if (type_is)
4569 {
4570 n1 = (rettv->v_type == var2.v_type
4571 && rettv->vval.v_dict == var2.vval.v_dict);
4572 if (type == TYPE_NEQUAL)
4573 n1 = !n1;
4574 }
4575 else if (rettv->v_type != var2.v_type
4576 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4577 {
4578 if (rettv->v_type != var2.v_type)
4579 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4580 else
4581 EMSG(_("E736: Invalid operation for Dictionary"));
4582 clear_tv(rettv);
4583 clear_tv(&var2);
4584 return FAIL;
4585 }
4586 else
4587 {
4588 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004589 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4590 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004591 if (type == TYPE_NEQUAL)
4592 n1 = !n1;
4593 }
4594 }
4595
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004596 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4597 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004598 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004599 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004600 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004601 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004602 clear_tv(rettv);
4603 clear_tv(&var2);
4604 return FAIL;
4605 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004606 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004607 if (type == TYPE_NEQUAL)
4608 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004609 }
4610
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611#ifdef FEAT_FLOAT
4612 /*
4613 * If one of the two variables is a float, compare as a float.
4614 * When using "=~" or "!~", always compare as string.
4615 */
4616 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4617 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4618 {
4619 float_T f1, f2;
4620
4621 if (rettv->v_type == VAR_FLOAT)
4622 f1 = rettv->vval.v_float;
4623 else
4624 f1 = get_tv_number(rettv);
4625 if (var2.v_type == VAR_FLOAT)
4626 f2 = var2.vval.v_float;
4627 else
4628 f2 = get_tv_number(&var2);
4629 n1 = FALSE;
4630 switch (type)
4631 {
4632 case TYPE_EQUAL: n1 = (f1 == f2); break;
4633 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4634 case TYPE_GREATER: n1 = (f1 > f2); break;
4635 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4636 case TYPE_SMALLER: n1 = (f1 < f2); break;
4637 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4638 case TYPE_UNKNOWN:
4639 case TYPE_MATCH:
4640 case TYPE_NOMATCH: break; /* avoid gcc warning */
4641 }
4642 }
4643#endif
4644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 /*
4646 * If one of the two variables is a number, compare as a number.
4647 * When using "=~" or "!~", always compare as string.
4648 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004649 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004652 n1 = get_tv_number(rettv);
4653 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 switch (type)
4655 {
4656 case TYPE_EQUAL: n1 = (n1 == n2); break;
4657 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4658 case TYPE_GREATER: n1 = (n1 > n2); break;
4659 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4660 case TYPE_SMALLER: n1 = (n1 < n2); break;
4661 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4662 case TYPE_UNKNOWN:
4663 case TYPE_MATCH:
4664 case TYPE_NOMATCH: break; /* avoid gcc warning */
4665 }
4666 }
4667 else
4668 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004669 s1 = get_tv_string_buf(rettv, buf1);
4670 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4672 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4673 else
4674 i = 0;
4675 n1 = FALSE;
4676 switch (type)
4677 {
4678 case TYPE_EQUAL: n1 = (i == 0); break;
4679 case TYPE_NEQUAL: n1 = (i != 0); break;
4680 case TYPE_GREATER: n1 = (i > 0); break;
4681 case TYPE_GEQUAL: n1 = (i >= 0); break;
4682 case TYPE_SMALLER: n1 = (i < 0); break;
4683 case TYPE_SEQUAL: n1 = (i <= 0); break;
4684
4685 case TYPE_MATCH:
4686 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004687 n1 = pattern_match(s2, s1, ic);
4688 if (type == TYPE_NOMATCH)
4689 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 break;
4691
4692 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4693 }
4694 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 clear_tv(rettv);
4696 clear_tv(&var2);
4697 rettv->v_type = VAR_NUMBER;
4698 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700 }
4701
4702 return OK;
4703}
4704
4705/*
4706 * Handle fourth level expression:
4707 * + number addition
4708 * - number subtraction
4709 * . string concatenation
4710 *
4711 * "arg" must point to the first non-white of the expression.
4712 * "arg" is advanced to the next non-white after the recognized expression.
4713 *
4714 * Return OK or FAIL.
4715 */
4716 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004717eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718{
Bram Moolenaar33570922005-01-25 22:26:29 +00004719 typval_T var2;
4720 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 int op;
4722 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004723#ifdef FEAT_FLOAT
4724 float_T f1 = 0, f2 = 0;
4725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 char_u *s1, *s2;
4727 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4728 char_u *p;
4729
4730 /*
4731 * Get the first variable.
4732 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004733 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 return FAIL;
4735
4736 /*
4737 * Repeat computing, until no '+', '-' or '.' is following.
4738 */
4739 for (;;)
4740 {
4741 op = **arg;
4742 if (op != '+' && op != '-' && op != '.')
4743 break;
4744
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004745 if ((op != '+' || rettv->v_type != VAR_LIST)
4746#ifdef FEAT_FLOAT
4747 && (op == '.' || rettv->v_type != VAR_FLOAT)
4748#endif
4749 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004750 {
4751 /* For "list + ...", an illegal use of the first operand as
4752 * a number cannot be determined before evaluating the 2nd
4753 * operand: if this is also a list, all is ok.
4754 * For "something . ...", "something - ..." or "non-list + ...",
4755 * we know that the first operand needs to be a string or number
4756 * without evaluating the 2nd operand. So check before to avoid
4757 * side effects after an error. */
4758 if (evaluate && get_tv_string_chk(rettv) == NULL)
4759 {
4760 clear_tv(rettv);
4761 return FAIL;
4762 }
4763 }
4764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 /*
4766 * Get the second variable.
4767 */
4768 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004769 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 return FAIL;
4773 }
4774
4775 if (evaluate)
4776 {
4777 /*
4778 * Compute the result.
4779 */
4780 if (op == '.')
4781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004782 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4783 s2 = get_tv_string_buf_chk(&var2, buf2);
4784 if (s2 == NULL) /* type error ? */
4785 {
4786 clear_tv(rettv);
4787 clear_tv(&var2);
4788 return FAIL;
4789 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004790 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004791 clear_tv(rettv);
4792 rettv->v_type = VAR_STRING;
4793 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004795 else if (op == '+' && rettv->v_type == VAR_LIST
4796 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004797 {
4798 /* concatenate Lists */
4799 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4800 &var3) == FAIL)
4801 {
4802 clear_tv(rettv);
4803 clear_tv(&var2);
4804 return FAIL;
4805 }
4806 clear_tv(rettv);
4807 *rettv = var3;
4808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 else
4810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004811 int error = FALSE;
4812
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004813#ifdef FEAT_FLOAT
4814 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004815 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004816 f1 = rettv->vval.v_float;
4817 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004818 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819 else
4820#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822 n1 = get_tv_number_chk(rettv, &error);
4823 if (error)
4824 {
4825 /* This can only happen for "list + non-list". For
4826 * "non-list + ..." or "something - ...", we returned
4827 * before evaluating the 2nd operand. */
4828 clear_tv(rettv);
4829 return FAIL;
4830 }
4831#ifdef FEAT_FLOAT
4832 if (var2.v_type == VAR_FLOAT)
4833 f1 = n1;
4834#endif
4835 }
4836#ifdef FEAT_FLOAT
4837 if (var2.v_type == VAR_FLOAT)
4838 {
4839 f2 = var2.vval.v_float;
4840 n2 = 0;
4841 }
4842 else
4843#endif
4844 {
4845 n2 = get_tv_number_chk(&var2, &error);
4846 if (error)
4847 {
4848 clear_tv(rettv);
4849 clear_tv(&var2);
4850 return FAIL;
4851 }
4852#ifdef FEAT_FLOAT
4853 if (rettv->v_type == VAR_FLOAT)
4854 f2 = n2;
4855#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004857 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004858
4859#ifdef FEAT_FLOAT
4860 /* If there is a float on either side the result is a float. */
4861 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4862 {
4863 if (op == '+')
4864 f1 = f1 + f2;
4865 else
4866 f1 = f1 - f2;
4867 rettv->v_type = VAR_FLOAT;
4868 rettv->vval.v_float = f1;
4869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871#endif
4872 {
4873 if (op == '+')
4874 n1 = n1 + n2;
4875 else
4876 n1 = n1 - n2;
4877 rettv->v_type = VAR_NUMBER;
4878 rettv->vval.v_number = n1;
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 }
4884 return OK;
4885}
4886
4887/*
4888 * Handle fifth level expression:
4889 * * number multiplication
4890 * / number division
4891 * % number modulo
4892 *
4893 * "arg" must point to the first non-white of the expression.
4894 * "arg" is advanced to the next non-white after the recognized expression.
4895 *
4896 * Return OK or FAIL.
4897 */
4898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004899eval6(
4900 char_u **arg,
4901 typval_T *rettv,
4902 int evaluate,
4903 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
Bram Moolenaar33570922005-01-25 22:26:29 +00004905 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 int op;
4907 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004908#ifdef FEAT_FLOAT
4909 int use_float = FALSE;
4910 float_T f1 = 0, f2;
4911#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004912 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914 /*
4915 * Get the first variable.
4916 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004917 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 return FAIL;
4919
4920 /*
4921 * Repeat computing, until no '*', '/' or '%' is following.
4922 */
4923 for (;;)
4924 {
4925 op = **arg;
4926 if (op != '*' && op != '/' && op != '%')
4927 break;
4928
4929 if (evaluate)
4930 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#ifdef FEAT_FLOAT
4932 if (rettv->v_type == VAR_FLOAT)
4933 {
4934 f1 = rettv->vval.v_float;
4935 use_float = TRUE;
4936 n1 = 0;
4937 }
4938 else
4939#endif
4940 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004941 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004942 if (error)
4943 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
4945 else
4946 n1 = 0;
4947
4948 /*
4949 * Get the second variable.
4950 */
4951 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004952 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 return FAIL;
4954
4955 if (evaluate)
4956 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004957#ifdef FEAT_FLOAT
4958 if (var2.v_type == VAR_FLOAT)
4959 {
4960 if (!use_float)
4961 {
4962 f1 = n1;
4963 use_float = TRUE;
4964 }
4965 f2 = var2.vval.v_float;
4966 n2 = 0;
4967 }
4968 else
4969#endif
4970 {
4971 n2 = get_tv_number_chk(&var2, &error);
4972 clear_tv(&var2);
4973 if (error)
4974 return FAIL;
4975#ifdef FEAT_FLOAT
4976 if (use_float)
4977 f2 = n2;
4978#endif
4979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
4981 /*
4982 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985#ifdef FEAT_FLOAT
4986 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988 if (op == '*')
4989 f1 = f1 * f2;
4990 else if (op == '/')
4991 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004992# ifdef VMS
4993 /* VMS crashes on divide by zero, work around it */
4994 if (f2 == 0.0)
4995 {
4996 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004997 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004999 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005000 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005001 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005002 }
5003 else
5004 f1 = f1 / f2;
5005# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 /* We rely on the floating point library to handle divide
5007 * by zero to result in "inf" and not a crash. */
5008 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005009# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005013 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014 return FAIL;
5015 }
5016 rettv->v_type = VAR_FLOAT;
5017 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005022 if (op == '*')
5023 n1 = n1 * n2;
5024 else if (op == '/')
5025 {
5026 if (n2 == 0) /* give an error message? */
5027 {
5028 if (n1 == 0)
5029 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5030 else if (n1 < 0)
5031 n1 = -0x7fffffffL;
5032 else
5033 n1 = 0x7fffffffL;
5034 }
5035 else
5036 n1 = n1 / n2;
5037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005039 {
5040 if (n2 == 0) /* give an error message? */
5041 n1 = 0;
5042 else
5043 n1 = n1 % n2;
5044 }
5045 rettv->v_type = VAR_NUMBER;
5046 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 }
5049 }
5050
5051 return OK;
5052}
5053
5054/*
5055 * Handle sixth level expression:
5056 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005057 * "string" string constant
5058 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 * &option-name option value
5060 * @r register contents
5061 * identifier variable value
5062 * function() function call
5063 * $VAR environment variable
5064 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005065 * [expr, expr] List
5066 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 *
5068 * Also handle:
5069 * ! in front logical NOT
5070 * - in front unary minus
5071 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 * trailing [] subscript in String or List
5073 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 *
5075 * "arg" must point to the first non-white of the expression.
5076 * "arg" is advanced to the next non-white after the recognized expression.
5077 *
5078 * Return OK or FAIL.
5079 */
5080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005081eval7(
5082 char_u **arg,
5083 typval_T *rettv,
5084 int evaluate,
5085 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 long n;
5088 int len;
5089 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 char_u *start_leader, *end_leader;
5091 int ret = OK;
5092 char_u *alias;
5093
5094 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005095 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005096 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
5100 /*
5101 * Skip '!' and '-' characters. They are handled later.
5102 */
5103 start_leader = *arg;
5104 while (**arg == '!' || **arg == '-' || **arg == '+')
5105 *arg = skipwhite(*arg + 1);
5106 end_leader = *arg;
5107
5108 switch (**arg)
5109 {
5110 /*
5111 * Number constant.
5112 */
5113 case '0':
5114 case '1':
5115 case '2':
5116 case '3':
5117 case '4':
5118 case '5':
5119 case '6':
5120 case '7':
5121 case '8':
5122 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005123 {
5124#ifdef FEAT_FLOAT
5125 char_u *p = skipdigits(*arg + 1);
5126 int get_float = FALSE;
5127
5128 /* We accept a float when the format matches
5129 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005130 * strict to avoid backwards compatibility problems.
5131 * Don't look for a float after the "." operator, so that
5132 * ":let vers = 1.2.3" doesn't fail. */
5133 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005135 get_float = TRUE;
5136 p = skipdigits(p + 2);
5137 if (*p == 'e' || *p == 'E')
5138 {
5139 ++p;
5140 if (*p == '-' || *p == '+')
5141 ++p;
5142 if (!vim_isdigit(*p))
5143 get_float = FALSE;
5144 else
5145 p = skipdigits(p + 1);
5146 }
5147 if (ASCII_ISALPHA(*p) || *p == '.')
5148 get_float = FALSE;
5149 }
5150 if (get_float)
5151 {
5152 float_T f;
5153
5154 *arg += string2float(*arg, &f);
5155 if (evaluate)
5156 {
5157 rettv->v_type = VAR_FLOAT;
5158 rettv->vval.v_float = f;
5159 }
5160 }
5161 else
5162#endif
5163 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005164 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165 *arg += len;
5166 if (evaluate)
5167 {
5168 rettv->v_type = VAR_NUMBER;
5169 rettv->vval.v_number = n;
5170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
5175 /*
5176 * String constant: "string".
5177 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 break;
5180
5181 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005184 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 break;
5186
5187 /*
5188 * List: [expr, expr]
5189 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 break;
5192
5193 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 * Dictionary: {key: val, key: val}
5195 */
5196 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5197 break;
5198
5199 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005200 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005202 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 break;
5204
5205 /*
5206 * Environment variable: $VAR.
5207 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 break;
5210
5211 /*
5212 * Register contents: @r.
5213 */
5214 case '@': ++*arg;
5215 if (evaluate)
5216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005217 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005218 rettv->vval.v_string = get_reg_contents(**arg,
5219 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221 if (**arg != NUL)
5222 ++*arg;
5223 break;
5224
5225 /*
5226 * nested expression: (expression).
5227 */
5228 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005229 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 if (**arg == ')')
5231 ++*arg;
5232 else if (ret == OK)
5233 {
5234 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005235 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 ret = FAIL;
5237 }
5238 break;
5239
Bram Moolenaar8c711452005-01-14 21:53:12 +00005240 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 break;
5242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243
5244 if (ret == NOTDONE)
5245 {
5246 /*
5247 * Must be a variable or function name.
5248 * Can also be a curly-braces kind of name: {expr}.
5249 */
5250 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005251 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 if (alias != NULL)
5253 s = alias;
5254
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005255 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005256 ret = FAIL;
5257 else
5258 {
5259 if (**arg == '(') /* recursive! */
5260 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005261 partial_T *partial;
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 /* If "s" is the name of a variable of type VAR_FUNC
5264 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005265 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266
5267 /* Invoke the function. */
5268 ret = get_func_tv(s, len, rettv, arg,
5269 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005270 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005271
5272 /* If evaluate is FALSE rettv->v_type was not set in
5273 * get_func_tv, but it's needed in handle_subscript() to parse
5274 * what follows. So set it here. */
5275 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5276 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005277 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005278 rettv->v_type = VAR_FUNC;
5279 }
5280
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 /* Stop the expression evaluation when immediately
5282 * aborting on error, or when an interrupt occurred or
5283 * an exception was thrown but not caught. */
5284 if (aborting())
5285 {
5286 if (ret == OK)
5287 clear_tv(rettv);
5288 ret = FAIL;
5289 }
5290 }
5291 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005292 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005293 else
5294 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005296 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005297 }
5298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 *arg = skipwhite(*arg);
5300
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005301 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5302 * expr(expr). */
5303 if (ret == OK)
5304 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306 /*
5307 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5308 */
5309 if (ret == OK && evaluate && end_leader > start_leader)
5310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 int val = 0;
5313#ifdef FEAT_FLOAT
5314 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005315
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005316 if (rettv->v_type == VAR_FLOAT)
5317 f = rettv->vval.v_float;
5318 else
5319#endif
5320 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005323 clear_tv(rettv);
5324 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 else
5327 {
5328 while (end_leader > start_leader)
5329 {
5330 --end_leader;
5331 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005332 {
5333#ifdef FEAT_FLOAT
5334 if (rettv->v_type == VAR_FLOAT)
5335 f = !f;
5336 else
5337#endif
5338 val = !val;
5339 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005340 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005341 {
5342#ifdef FEAT_FLOAT
5343 if (rettv->v_type == VAR_FLOAT)
5344 f = -f;
5345 else
5346#endif
5347 val = -val;
5348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005350#ifdef FEAT_FLOAT
5351 if (rettv->v_type == VAR_FLOAT)
5352 {
5353 clear_tv(rettv);
5354 rettv->vval.v_float = f;
5355 }
5356 else
5357#endif
5358 {
5359 clear_tv(rettv);
5360 rettv->v_type = VAR_NUMBER;
5361 rettv->vval.v_number = val;
5362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 }
5365
5366 return ret;
5367}
5368
5369/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005370 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5371 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5373 */
5374 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005375eval_index(
5376 char_u **arg,
5377 typval_T *rettv,
5378 int evaluate,
5379 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380{
5381 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005382 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005384 long len = -1;
5385 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388
Bram Moolenaara03f2332016-02-06 18:09:59 +01005389 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005391 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005392 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005393 if (verbose)
5394 EMSG(_("E695: Cannot index a Funcref"));
5395 return FAIL;
5396 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005397#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005398 if (verbose)
5399 EMSG(_(e_float_as_string));
5400 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005401#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005402 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005403 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005404 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005405 if (verbose)
5406 EMSG(_("E909: Cannot index a special variable"));
5407 return FAIL;
5408 case VAR_UNKNOWN:
5409 if (evaluate)
5410 return FAIL;
5411 /* FALLTHROUGH */
5412
5413 case VAR_STRING:
5414 case VAR_NUMBER:
5415 case VAR_LIST:
5416 case VAR_DICT:
5417 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005418 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005420 init_tv(&var1);
5421 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 /*
5425 * dict.name
5426 */
5427 key = *arg + 1;
5428 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5429 ;
5430 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005432 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
5434 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436 /*
5437 * something[idx]
5438 *
5439 * Get the (first) variable from inside the [].
5440 */
5441 *arg = skipwhite(*arg + 1);
5442 if (**arg == ':')
5443 empty1 = TRUE;
5444 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5445 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005446 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5447 {
5448 /* not a number or string */
5449 clear_tv(&var1);
5450 return FAIL;
5451 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452
5453 /*
5454 * Get the second variable from inside the [:].
5455 */
5456 if (**arg == ':')
5457 {
5458 range = TRUE;
5459 *arg = skipwhite(*arg + 1);
5460 if (**arg == ']')
5461 empty2 = TRUE;
5462 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5463 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005464 if (!empty1)
5465 clear_tv(&var1);
5466 return FAIL;
5467 }
5468 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5469 {
5470 /* not a number or string */
5471 if (!empty1)
5472 clear_tv(&var1);
5473 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005474 return FAIL;
5475 }
5476 }
5477
5478 /* Check for the ']'. */
5479 if (**arg != ']')
5480 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005481 if (verbose)
5482 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005483 clear_tv(&var1);
5484 if (range)
5485 clear_tv(&var2);
5486 return FAIL;
5487 }
5488 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490
5491 if (evaluate)
5492 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493 n1 = 0;
5494 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005495 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005496 n1 = get_tv_number(&var1);
5497 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 }
5499 if (range)
5500 {
5501 if (empty2)
5502 n2 = -1;
5503 else
5504 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 n2 = get_tv_number(&var2);
5506 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 }
5508 }
5509
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005512 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005513 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005514 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005515 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005516 case VAR_SPECIAL:
5517 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005518 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005519 break; /* not evaluating, skipping over subscript */
5520
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 case VAR_NUMBER:
5522 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 len = (long)STRLEN(s);
5525 if (range)
5526 {
5527 /* The resulting variable is a substring. If the indexes
5528 * are out of range the result is empty. */
5529 if (n1 < 0)
5530 {
5531 n1 = len + n1;
5532 if (n1 < 0)
5533 n1 = 0;
5534 }
5535 if (n2 < 0)
5536 n2 = len + n2;
5537 else if (n2 >= len)
5538 n2 = len;
5539 if (n1 >= len || n2 < 0 || n1 > n2)
5540 s = NULL;
5541 else
5542 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5543 }
5544 else
5545 {
5546 /* The resulting variable is a string of a single
5547 * character. If the index is too big or negative the
5548 * result is empty. */
5549 if (n1 >= len || n1 < 0)
5550 s = NULL;
5551 else
5552 s = vim_strnsave(s + n1, 1);
5553 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005554 clear_tv(rettv);
5555 rettv->v_type = VAR_STRING;
5556 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 break;
5558
5559 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 if (n1 < 0)
5562 n1 = len + n1;
5563 if (!empty1 && (n1 < 0 || n1 >= len))
5564 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005565 /* For a range we allow invalid values and return an empty
5566 * list. A list index out of range is an error. */
5567 if (!range)
5568 {
5569 if (verbose)
5570 EMSGN(_(e_listidx), n1);
5571 return FAIL;
5572 }
5573 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574 }
5575 if (range)
5576 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005577 list_T *l;
5578 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005579
5580 if (n2 < 0)
5581 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005582 else if (n2 >= len)
5583 n2 = len - 1;
5584 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005585 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 l = list_alloc();
5587 if (l == NULL)
5588 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 n1 <= n2; ++n1)
5591 {
5592 if (list_append_tv(l, &item->li_tv) == FAIL)
5593 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005594 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 return FAIL;
5596 }
5597 item = item->li_next;
5598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 clear_tv(rettv);
5600 rettv->v_type = VAR_LIST;
5601 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005602 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603 }
5604 else
5605 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005606 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005607 clear_tv(rettv);
5608 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 }
5610 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005611
5612 case VAR_DICT:
5613 if (range)
5614 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005615 if (verbose)
5616 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005617 if (len == -1)
5618 clear_tv(&var1);
5619 return FAIL;
5620 }
5621 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623
5624 if (len == -1)
5625 {
5626 key = get_tv_string(&var1);
5627 if (*key == NUL)
5628 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005629 if (verbose)
5630 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005631 clear_tv(&var1);
5632 return FAIL;
5633 }
5634 }
5635
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005636 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005638 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005639 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 if (len == -1)
5641 clear_tv(&var1);
5642 if (item == NULL)
5643 return FAIL;
5644
5645 copy_tv(&item->di_tv, &var1);
5646 clear_tv(rettv);
5647 *rettv = var1;
5648 }
5649 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005650 }
5651 }
5652
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005653 return OK;
5654}
5655
5656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 * Get an option value.
5658 * "arg" points to the '&' or '+' before the option name.
5659 * "arg" is advanced to character after the option name.
5660 * Return OK or FAIL.
5661 */
5662 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005663get_option_tv(
5664 char_u **arg,
5665 typval_T *rettv, /* when NULL, only check if option exists */
5666 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 char_u *option_end;
5669 long numval;
5670 char_u *stringval;
5671 int opt_type;
5672 int c;
5673 int working = (**arg == '+'); /* has("+option") */
5674 int ret = OK;
5675 int opt_flags;
5676
5677 /*
5678 * Isolate the option name and find its value.
5679 */
5680 option_end = find_option_end(arg, &opt_flags);
5681 if (option_end == NULL)
5682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 EMSG2(_("E112: Option name missing: %s"), *arg);
5685 return FAIL;
5686 }
5687
5688 if (!evaluate)
5689 {
5690 *arg = option_end;
5691 return OK;
5692 }
5693
5694 c = *option_end;
5695 *option_end = NUL;
5696 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
5699 if (opt_type == -3) /* invalid name */
5700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 EMSG2(_("E113: Unknown option: %s"), *arg);
5703 ret = FAIL;
5704 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005705 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 {
5707 if (opt_type == -2) /* hidden string option */
5708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709 rettv->v_type = VAR_STRING;
5710 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
5712 else if (opt_type == -1) /* hidden number option */
5713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005714 rettv->v_type = VAR_NUMBER;
5715 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 else if (opt_type == 1) /* number option */
5718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 rettv->v_type = VAR_NUMBER;
5720 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 else /* string option */
5723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005724 rettv->v_type = VAR_STRING;
5725 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
5727 }
5728 else if (working && (opt_type == -2 || opt_type == -1))
5729 ret = FAIL;
5730
5731 *option_end = c; /* put back for error messages */
5732 *arg = option_end;
5733
5734 return ret;
5735}
5736
5737/*
5738 * Allocate a variable for a string constant.
5739 * Return OK or FAIL.
5740 */
5741 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005742get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
5744 char_u *p;
5745 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 int extra = 0;
5747
5748 /*
5749 * Find the end of the string, skipping backslashed characters.
5750 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 {
5753 if (*p == '\\' && p[1] != NUL)
5754 {
5755 ++p;
5756 /* A "\<x>" form occupies at least 4 characters, and produces up
5757 * to 6 characters: reserve space for 2 extra */
5758 if (*p == '<')
5759 extra += 2;
5760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762
5763 if (*p != '"')
5764 {
5765 EMSG2(_("E114: Missing quote: %s"), *arg);
5766 return FAIL;
5767 }
5768
5769 /* If only parsing, set *arg and return here */
5770 if (!evaluate)
5771 {
5772 *arg = p + 1;
5773 return OK;
5774 }
5775
5776 /*
5777 * Copy the string into allocated memory, handling backslashed
5778 * characters.
5779 */
5780 name = alloc((unsigned)(p - *arg + extra));
5781 if (name == NULL)
5782 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 rettv->v_type = VAR_STRING;
5784 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 if (*p == '\\')
5789 {
5790 switch (*++p)
5791 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 case 'b': *name++ = BS; ++p; break;
5793 case 'e': *name++ = ESC; ++p; break;
5794 case 'f': *name++ = FF; ++p; break;
5795 case 'n': *name++ = NL; ++p; break;
5796 case 'r': *name++ = CAR; ++p; break;
5797 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
5799 case 'X': /* hex: "\x1", "\x12" */
5800 case 'x':
5801 case 'u': /* Unicode: "\u0023" */
5802 case 'U':
5803 if (vim_isxdigit(p[1]))
5804 {
5805 int n, nr;
5806 int c = toupper(*p);
5807
5808 if (c == 'X')
5809 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005810 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005812 else
5813 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 nr = 0;
5815 while (--n >= 0 && vim_isxdigit(p[1]))
5816 {
5817 ++p;
5818 nr = (nr << 4) + hex2nr(*p);
5819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821#ifdef FEAT_MBYTE
5822 /* For "\u" store the number according to
5823 * 'encoding'. */
5824 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005825 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 else
5827#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831
5832 /* octal: "\1", "\12", "\123" */
5833 case '0':
5834 case '1':
5835 case '2':
5836 case '3':
5837 case '4':
5838 case '5':
5839 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 case '7': *name = *p++ - '0';
5841 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 *name = (*name << 3) + *p++ - '0';
5844 if (*p >= '0' && *p <= '7')
5845 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 break;
5849
5850 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 if (extra != 0)
5853 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 break;
5856 }
5857 /* FALLTHROUGH */
5858
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 break;
5861 }
5862 }
5863 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 *arg = p + 1;
5869
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 return OK;
5871}
5872
5873/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 * Return OK or FAIL.
5876 */
5877 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005878get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879{
5880 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 int reduce = 0;
5883
5884 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 */
5887 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 break;
5893 ++reduce;
5894 ++p;
5895 }
5896 }
5897
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005901 return FAIL;
5902 }
5903
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 if (!evaluate)
5906 {
5907 *arg = p + 1;
5908 return OK;
5909 }
5910
5911 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 */
5914 str = alloc((unsigned)((p - *arg) - reduce));
5915 if (str == NULL)
5916 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005917 rettv->v_type = VAR_STRING;
5918 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 break;
5926 ++p;
5927 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005930 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931 *arg = p + 1;
5932
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005933 return OK;
5934}
5935
5936/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937 * Allocate a variable for a List and fill it from "*arg".
5938 * Return OK or FAIL.
5939 */
5940 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005941get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942{
Bram Moolenaar33570922005-01-25 22:26:29 +00005943 list_T *l = NULL;
5944 typval_T tv;
5945 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946
5947 if (evaluate)
5948 {
5949 l = list_alloc();
5950 if (l == NULL)
5951 return FAIL;
5952 }
5953
5954 *arg = skipwhite(*arg + 1);
5955 while (**arg != ']' && **arg != NUL)
5956 {
5957 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5958 goto failret;
5959 if (evaluate)
5960 {
5961 item = listitem_alloc();
5962 if (item != NULL)
5963 {
5964 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005965 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 list_append(l, item);
5967 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005968 else
5969 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970 }
5971
5972 if (**arg == ']')
5973 break;
5974 if (**arg != ',')
5975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005976 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 goto failret;
5978 }
5979 *arg = skipwhite(*arg + 1);
5980 }
5981
5982 if (**arg != ']')
5983 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985failret:
5986 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005987 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 return FAIL;
5989 }
5990
5991 *arg = skipwhite(*arg + 1);
5992 if (evaluate)
5993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005994 rettv->v_type = VAR_LIST;
5995 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 ++l->lv_refcount;
5997 }
5998
5999 return OK;
6000}
6001
6002/*
6003 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006004 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006006 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006007list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006009 list_T *l;
6010
6011 l = (list_T *)alloc_clear(sizeof(list_T));
6012 if (l != NULL)
6013 {
6014 /* Prepend the list to the list of lists for garbage collection. */
6015 if (first_list != NULL)
6016 first_list->lv_used_prev = l;
6017 l->lv_used_prev = NULL;
6018 l->lv_used_next = first_list;
6019 first_list = l;
6020 }
6021 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022}
6023
6024/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006025 * Allocate an empty list for a return value.
6026 * Returns OK or FAIL.
6027 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006028 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006029rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006030{
6031 list_T *l = list_alloc();
6032
6033 if (l == NULL)
6034 return FAIL;
6035
6036 rettv->vval.v_list = l;
6037 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006038 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006039 ++l->lv_refcount;
6040 return OK;
6041}
6042
6043/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 * Unreference a list: decrement the reference count and free it when it
6045 * becomes zero.
6046 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006048list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006050 if (l != NULL && --l->lv_refcount <= 0)
6051 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006052}
6053
6054/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006055 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 * Ignores the reference count.
6057 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006059list_free(
6060 list_T *l,
6061 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062{
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006065 /* Remove the list from the list of lists for garbage collection. */
6066 if (l->lv_used_prev == NULL)
6067 first_list = l->lv_used_next;
6068 else
6069 l->lv_used_prev->lv_used_next = l->lv_used_next;
6070 if (l->lv_used_next != NULL)
6071 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6072
Bram Moolenaard9fba312005-06-26 22:34:35 +00006073 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006075 /* Remove the item before deleting it. */
6076 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006077 if (recurse || (item->li_tv.v_type != VAR_LIST
6078 && item->li_tv.v_type != VAR_DICT))
6079 clear_tv(&item->li_tv);
6080 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 }
6082 vim_free(l);
6083}
6084
6085/*
6086 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006087 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006089 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006090listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091{
Bram Moolenaar33570922005-01-25 22:26:29 +00006092 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093}
6094
6095/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006096 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006098 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006099listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006101 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 vim_free(item);
6103}
6104
6105/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006106 * Remove a list item from a List and free it. Also clears the value.
6107 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006109listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006110{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006111 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006112 listitem_free(item);
6113}
6114
6115/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116 * Get the number of items in a list.
6117 */
6118 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006121 if (l == NULL)
6122 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006123 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006124}
6125
6126/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127 * Return TRUE when two lists have exactly the same values.
6128 */
6129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006130list_equal(
6131 list_T *l1,
6132 list_T *l2,
6133 int ic, /* ignore case for strings */
6134 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135{
Bram Moolenaar33570922005-01-25 22:26:29 +00006136 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006137
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006138 if (l1 == NULL || l2 == NULL)
6139 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006140 if (l1 == l2)
6141 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006142 if (list_len(l1) != list_len(l2))
6143 return FALSE;
6144
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006145 for (item1 = l1->lv_first, item2 = l2->lv_first;
6146 item1 != NULL && item2 != NULL;
6147 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006148 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006149 return FALSE;
6150 return item1 == NULL && item2 == NULL;
6151}
6152
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006153/*
6154 * Return the dictitem that an entry in a hashtable points to.
6155 */
6156 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006158{
6159 return HI2DI(hi);
6160}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006161
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006162/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163 * Return TRUE when two dictionaries have exactly the same key/values.
6164 */
6165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006166dict_equal(
6167 dict_T *d1,
6168 dict_T *d2,
6169 int ic, /* ignore case for strings */
6170 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171{
Bram Moolenaar33570922005-01-25 22:26:29 +00006172 hashitem_T *hi;
6173 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006174 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006176 if (d1 == NULL || d2 == NULL)
6177 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006178 if (d1 == d2)
6179 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006180 if (dict_len(d1) != dict_len(d2))
6181 return FALSE;
6182
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006183 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006185 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006186 if (!HASHITEM_EMPTY(hi))
6187 {
6188 item2 = dict_find(d2, hi->hi_key, -1);
6189 if (item2 == NULL)
6190 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006192 return FALSE;
6193 --todo;
6194 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006195 }
6196 return TRUE;
6197}
6198
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006199static int tv_equal_recurse_limit;
6200
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006201/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006203 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006204 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205 */
6206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006207tv_equal(
6208 typval_T *tv1,
6209 typval_T *tv2,
6210 int ic, /* ignore case */
6211 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006212{
6213 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006214 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006215 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006216 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006217
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006218 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6219 if ((tv1->v_type == VAR_FUNC
6220 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6221 && (tv2->v_type == VAR_FUNC
6222 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6223 {
6224 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6225 : tv1->vval.v_partial->pt_name;
6226 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6227 : tv2->vval.v_partial->pt_name;
6228 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6229 }
6230
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006231 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006232 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006233
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006234 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006235 * recursiveness to a limit. We guess they are equal then.
6236 * A fixed limit has the problem of still taking an awful long time.
6237 * Reduce the limit every time running into it. That should work fine for
6238 * deeply linked structures that are not recursively linked and catch
6239 * recursiveness quickly. */
6240 if (!recursive)
6241 tv_equal_recurse_limit = 1000;
6242 if (recursive_cnt >= tv_equal_recurse_limit)
6243 {
6244 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006245 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006246 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006247
6248 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006250 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006251 ++recursive_cnt;
6252 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6253 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006254 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006255
6256 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006257 ++recursive_cnt;
6258 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6259 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006260 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006261
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006262 case VAR_NUMBER:
6263 return tv1->vval.v_number == tv2->vval.v_number;
6264
6265 case VAR_STRING:
6266 s1 = get_tv_string_buf(tv1, buf1);
6267 s2 = get_tv_string_buf(tv2, buf2);
6268 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006269
6270 case VAR_SPECIAL:
6271 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006272
6273 case VAR_FLOAT:
6274#ifdef FEAT_FLOAT
6275 return tv1->vval.v_float == tv2->vval.v_float;
6276#endif
6277 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006278#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006279 return tv1->vval.v_job == tv2->vval.v_job;
6280#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006281 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006282#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006283 return tv1->vval.v_channel == tv2->vval.v_channel;
6284#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006285 case VAR_FUNC:
6286 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006287 case VAR_UNKNOWN:
6288 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006289 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006290
Bram Moolenaara03f2332016-02-06 18:09:59 +01006291 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6292 * does not equal anything, not even itself. */
6293 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006294}
6295
6296/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 * Locate item with index "n" in list "l" and return it.
6298 * A negative index is counted from the end; -1 is the last item.
6299 * Returns NULL when "n" is out of range.
6300 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006301 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006302list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303{
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 long idx;
6306
6307 if (l == NULL)
6308 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006309
6310 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006312 n = l->lv_len + n;
6313
6314 /* Check for index out of range. */
6315 if (n < 0 || n >= l->lv_len)
6316 return NULL;
6317
6318 /* When there is a cached index may start search from there. */
6319 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006321 if (n < l->lv_idx / 2)
6322 {
6323 /* closest to the start of the list */
6324 item = l->lv_first;
6325 idx = 0;
6326 }
6327 else if (n > (l->lv_idx + l->lv_len) / 2)
6328 {
6329 /* closest to the end of the list */
6330 item = l->lv_last;
6331 idx = l->lv_len - 1;
6332 }
6333 else
6334 {
6335 /* closest to the cached index */
6336 item = l->lv_idx_item;
6337 idx = l->lv_idx;
6338 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 }
6340 else
6341 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006342 if (n < l->lv_len / 2)
6343 {
6344 /* closest to the start of the list */
6345 item = l->lv_first;
6346 idx = 0;
6347 }
6348 else
6349 {
6350 /* closest to the end of the list */
6351 item = l->lv_last;
6352 idx = l->lv_len - 1;
6353 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006355
6356 while (n > idx)
6357 {
6358 /* search forward */
6359 item = item->li_next;
6360 ++idx;
6361 }
6362 while (n < idx)
6363 {
6364 /* search backward */
6365 item = item->li_prev;
6366 --idx;
6367 }
6368
6369 /* cache the used index */
6370 l->lv_idx = idx;
6371 l->lv_idx_item = item;
6372
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373 return item;
6374}
6375
6376/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006377 * Get list item "l[idx]" as a number.
6378 */
6379 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006380list_find_nr(
6381 list_T *l,
6382 long idx,
6383 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006384{
6385 listitem_T *li;
6386
6387 li = list_find(l, idx);
6388 if (li == NULL)
6389 {
6390 if (errorp != NULL)
6391 *errorp = TRUE;
6392 return -1L;
6393 }
6394 return get_tv_number_chk(&li->li_tv, errorp);
6395}
6396
6397/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006398 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6399 */
6400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006401list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006402{
6403 listitem_T *li;
6404
6405 li = list_find(l, idx - 1);
6406 if (li == NULL)
6407 {
6408 EMSGN(_(e_listidx), idx);
6409 return NULL;
6410 }
6411 return get_tv_string(&li->li_tv);
6412}
6413
6414/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006415 * Locate "item" list "l" and return its index.
6416 * Returns -1 when "item" is not in the list.
6417 */
6418 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006419list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006420{
6421 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006423
6424 if (l == NULL)
6425 return -1;
6426 idx = 0;
6427 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6428 ++idx;
6429 if (li == NULL)
6430 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006431 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006432}
6433
6434/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435 * Append item "item" to the end of list "l".
6436 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006438list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439{
6440 if (l->lv_last == NULL)
6441 {
6442 /* empty list */
6443 l->lv_first = item;
6444 l->lv_last = item;
6445 item->li_prev = NULL;
6446 }
6447 else
6448 {
6449 l->lv_last->li_next = item;
6450 item->li_prev = l->lv_last;
6451 l->lv_last = item;
6452 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006453 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454 item->li_next = NULL;
6455}
6456
6457/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006461 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006462list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006464 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465
Bram Moolenaar05159a02005-02-26 23:04:13 +00006466 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006468 copy_tv(tv, &li->li_tv);
6469 list_append(l, li);
6470 return OK;
6471}
6472
6473/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006474 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006475 * Return FAIL when out of memory.
6476 */
6477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006479{
6480 listitem_T *li = listitem_alloc();
6481
6482 if (li == NULL)
6483 return FAIL;
6484 li->li_tv.v_type = VAR_DICT;
6485 li->li_tv.v_lock = 0;
6486 li->li_tv.vval.v_dict = dict;
6487 list_append(list, li);
6488 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489 return OK;
6490}
6491
6492/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006493 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006494 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006495 * Returns FAIL when out of memory.
6496 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006498list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006499{
6500 listitem_T *li = listitem_alloc();
6501
6502 if (li == NULL)
6503 return FAIL;
6504 list_append(l, li);
6505 li->li_tv.v_type = VAR_STRING;
6506 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006507 if (str == NULL)
6508 li->li_tv.vval.v_string = NULL;
6509 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006510 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006511 return FAIL;
6512 return OK;
6513}
6514
6515/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006516 * Append "n" to list "l".
6517 * Returns FAIL when out of memory.
6518 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006519 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006520list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006521{
6522 listitem_T *li;
6523
6524 li = listitem_alloc();
6525 if (li == NULL)
6526 return FAIL;
6527 li->li_tv.v_type = VAR_NUMBER;
6528 li->li_tv.v_lock = 0;
6529 li->li_tv.vval.v_number = n;
6530 list_append(l, li);
6531 return OK;
6532}
6533
6534/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006535 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 * If "item" is NULL append at the end.
6537 * Return FAIL when out of memory.
6538 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006539 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006540list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541{
Bram Moolenaar33570922005-01-25 22:26:29 +00006542 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543
6544 if (ni == NULL)
6545 return FAIL;
6546 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006547 list_insert(l, ni, item);
6548 return OK;
6549}
6550
6551 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006552list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006553{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006554 if (item == NULL)
6555 /* Append new item at end of list. */
6556 list_append(l, ni);
6557 else
6558 {
6559 /* Insert new item before existing item. */
6560 ni->li_prev = item->li_prev;
6561 ni->li_next = item;
6562 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006563 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006564 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006565 ++l->lv_idx;
6566 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006568 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006570 l->lv_idx_item = NULL;
6571 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006573 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006575}
6576
6577/*
6578 * Extend "l1" with "l2".
6579 * If "bef" is NULL append at the end, otherwise insert before this item.
6580 * Returns FAIL when out of memory.
6581 */
6582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006583list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584{
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006586 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006588 /* We also quit the loop when we have inserted the original item count of
6589 * the list, avoid a hang when we extend a list with itself. */
6590 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6592 return FAIL;
6593 return OK;
6594}
6595
6596/*
6597 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6598 * Return FAIL when out of memory.
6599 */
6600 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006601list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602{
Bram Moolenaar33570922005-01-25 22:26:29 +00006603 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006604
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006605 if (l1 == NULL || l2 == NULL)
6606 return FAIL;
6607
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006608 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006609 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006610 if (l == NULL)
6611 return FAIL;
6612 tv->v_type = VAR_LIST;
6613 tv->vval.v_list = l;
6614
6615 /* append all items from the second list */
6616 return list_extend(l, l2, NULL);
6617}
6618
6619/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006620 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006622 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 * Returns NULL when out of memory.
6624 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006626list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627{
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 list_T *copy;
6629 listitem_T *item;
6630 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631
6632 if (orig == NULL)
6633 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634
6635 copy = list_alloc();
6636 if (copy != NULL)
6637 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006638 if (copyID != 0)
6639 {
6640 /* Do this before adding the items, because one of the items may
6641 * refer back to this list. */
6642 orig->lv_copyID = copyID;
6643 orig->lv_copylist = copy;
6644 }
6645 for (item = orig->lv_first; item != NULL && !got_int;
6646 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 {
6648 ni = listitem_alloc();
6649 if (ni == NULL)
6650 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006651 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 {
6653 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6654 {
6655 vim_free(ni);
6656 break;
6657 }
6658 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006660 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661 list_append(copy, ni);
6662 }
6663 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006664 if (item != NULL)
6665 {
6666 list_unref(copy);
6667 copy = NULL;
6668 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669 }
6670
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 return copy;
6672}
6673
6674/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006675 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006676 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006677 * This used to be called list_remove, but that conflicts with a Sun header
6678 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006681vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006682{
Bram Moolenaar33570922005-01-25 22:26:29 +00006683 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006685 /* notify watchers */
6686 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006688 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006689 list_fix_watch(l, ip);
6690 if (ip == item2)
6691 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006692 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006693
6694 if (item2->li_next == NULL)
6695 l->lv_last = item->li_prev;
6696 else
6697 item2->li_next->li_prev = item->li_prev;
6698 if (item->li_prev == NULL)
6699 l->lv_first = item2->li_next;
6700 else
6701 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006702 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006703}
6704
6705/*
6706 * Return an allocated string with the string representation of a list.
6707 * May return NULL.
6708 */
6709 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006710list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006711{
6712 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006713
6714 if (tv->vval.v_list == NULL)
6715 return NULL;
6716 ga_init2(&ga, (int)sizeof(char), 80);
6717 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006718 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006719 {
6720 vim_free(ga.ga_data);
6721 return NULL;
6722 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006723 ga_append(&ga, ']');
6724 ga_append(&ga, NUL);
6725 return (char_u *)ga.ga_data;
6726}
6727
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006728typedef struct join_S {
6729 char_u *s;
6730 char_u *tofree;
6731} join_T;
6732
6733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006734list_join_inner(
6735 garray_T *gap, /* to store the result in */
6736 list_T *l,
6737 char_u *sep,
6738 int echo_style,
6739 int copyID,
6740 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006741{
6742 int i;
6743 join_T *p;
6744 int len;
6745 int sumlen = 0;
6746 int first = TRUE;
6747 char_u *tofree;
6748 char_u numbuf[NUMBUFLEN];
6749 listitem_T *item;
6750 char_u *s;
6751
6752 /* Stringify each item in the list. */
6753 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6754 {
6755 if (echo_style)
6756 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6757 else
6758 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6759 if (s == NULL)
6760 return FAIL;
6761
6762 len = (int)STRLEN(s);
6763 sumlen += len;
6764
Bram Moolenaarcde88542015-08-11 19:14:00 +02006765 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006766 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6767 if (tofree != NULL || s != numbuf)
6768 {
6769 p->s = s;
6770 p->tofree = tofree;
6771 }
6772 else
6773 {
6774 p->s = vim_strnsave(s, len);
6775 p->tofree = p->s;
6776 }
6777
6778 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006779 if (did_echo_string_emsg) /* recursion error, bail out */
6780 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006781 }
6782
6783 /* Allocate result buffer with its total size, avoid re-allocation and
6784 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6785 if (join_gap->ga_len >= 2)
6786 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6787 if (ga_grow(gap, sumlen + 2) == FAIL)
6788 return FAIL;
6789
6790 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6791 {
6792 if (first)
6793 first = FALSE;
6794 else
6795 ga_concat(gap, sep);
6796 p = ((join_T *)join_gap->ga_data) + i;
6797
6798 if (p->s != NULL)
6799 ga_concat(gap, p->s);
6800 line_breakcheck();
6801 }
6802
6803 return OK;
6804}
6805
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006806/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006807 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006808 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006809 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006810 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006812list_join(
6813 garray_T *gap,
6814 list_T *l,
6815 char_u *sep,
6816 int echo_style,
6817 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006818{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006819 garray_T join_ga;
6820 int retval;
6821 join_T *p;
6822 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823
Bram Moolenaard39a7512015-04-16 22:51:22 +02006824 if (l->lv_len < 1)
6825 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006826 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6827 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6828
6829 /* Dispose each item in join_ga. */
6830 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006831 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006832 p = (join_T *)join_ga.ga_data;
6833 for (i = 0; i < join_ga.ga_len; ++i)
6834 {
6835 vim_free(p->tofree);
6836 ++p;
6837 }
6838 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006839 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006840
6841 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006842}
6843
6844/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006845 * Return the next (unique) copy ID.
6846 * Used for serializing nested structures.
6847 */
6848 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006849get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006850{
6851 current_copyID += COPYID_INC;
6852 return current_copyID;
6853}
6854
6855/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 * Garbage collection for lists and dictionaries.
6857 *
6858 * We use reference counts to be able to free most items right away when they
6859 * are no longer used. But for composite items it's possible that it becomes
6860 * unused while the reference count is > 0: When there is a recursive
6861 * reference. Example:
6862 * :let l = [1, 2, 3]
6863 * :let d = {9: l}
6864 * :let l[1] = d
6865 *
6866 * Since this is quite unusual we handle this with garbage collection: every
6867 * once in a while find out which lists and dicts are not referenced from any
6868 * variable.
6869 *
6870 * Here is a good reference text about garbage collection (refers to Python
6871 * but it applies to all reference-counting mechanisms):
6872 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006873 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006874
6875/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876 * Do garbage collection for lists and dicts.
6877 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006878 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006880garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006881{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006882 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884 buf_T *buf;
6885 win_T *wp;
6886 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006887 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006888 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006890#ifdef FEAT_WINDOWS
6891 tabpage_T *tp;
6892#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006894 /* Only do this once. */
6895 want_garbage_collect = FALSE;
6896 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006897 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006898
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 /* We advance by two because we add one for items referenced through
6900 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006901 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006902
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903 /*
6904 * 1. Go through all accessible variables and mark all lists and dicts
6905 * with copyID.
6906 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006907
6908 /* Don't free variables in the previous_funccal list unless they are only
6909 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006910 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006911 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6912 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6914 NULL);
6915 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6916 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006917 }
6918
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 /* script-local variables */
6920 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922
6923 /* buffer-local variables */
6924 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6926 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927
6928 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006929 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6931 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006932#ifdef FEAT_AUTOCMD
6933 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6935 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006936#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006938#ifdef FEAT_WINDOWS
6939 /* tabpage-local variables */
6940 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006941 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6942 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006943#endif
6944
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006947
6948 /* function-local variables */
6949 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6950 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006951 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6952 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 }
6954
Bram Moolenaard812df62008-11-09 12:46:09 +00006955 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006956 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006957
Bram Moolenaar1dced572012-04-05 16:54:08 +02006958#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006960#endif
6961
Bram Moolenaardb913952012-06-29 12:54:53 +02006962#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006964#endif
6965
6966#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006967 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006968#endif
6969
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006970#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006971 abort = abort || set_ref_in_channel(copyID);
6972#endif
6973
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006974 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006976 /*
6977 * 2. Free lists and dictionaries that are not referenced.
6978 */
6979 did_free = free_unref_items(copyID);
6980
6981 /*
6982 * 3. Check if any funccal can be freed now.
6983 */
6984 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006986 if (can_free_funccal(*pfc, copyID))
6987 {
6988 fc = *pfc;
6989 *pfc = fc->caller;
6990 free_funccal(fc, TRUE);
6991 did_free = TRUE;
6992 did_free_funccal = TRUE;
6993 }
6994 else
6995 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006997 if (did_free_funccal)
6998 /* When a funccal was freed some more items might be garbage
6999 * collected, so run again. */
7000 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007001 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007002 else if (p_verbose > 0)
7003 {
7004 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7005 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006
7007 return did_free;
7008}
7009
7010/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01007011 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007012 */
7013 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007014free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007015{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007016 dict_T *dd, *dd_next;
7017 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 int did_free = FALSE;
7019
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007021 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007022 */
7023 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007024 {
7025 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007026 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007028 /* Free the Dictionary and ordinary items it contains, but don't
7029 * recurse into Lists and Dictionaries, they will be in the list
7030 * of dicts or list of lists. */
7031 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007034 dd = dd_next;
7035 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007036
7037 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007038 * Go through the list of lists and free items without the copyID.
7039 * But don't free a list that has a watcher (used in a for loop), these
7040 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 */
7042 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007043 {
7044 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007045 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7046 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007048 /* Free the List and ordinary items it contains, but don't recurse
7049 * into Lists and Dictionaries, they will be in the list of dicts
7050 * or list of lists. */
7051 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007052 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007054 ll = ll_next;
7055 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007056
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057 return did_free;
7058}
7059
7060/*
7061 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 * "list_stack" is used to add lists to be marked. Can be NULL.
7063 *
7064 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007065 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007067set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068{
7069 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 hashtab_T *cur_ht;
7073 ht_stack_T *ht_stack = NULL;
7074 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007075
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007076 cur_ht = ht;
7077 for (;;)
7078 {
7079 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 /* Mark each item in the hashtab. If the item contains a hashtab
7082 * it is added to ht_stack, if it contains a list it is added to
7083 * list_stack. */
7084 todo = (int)cur_ht->ht_used;
7085 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7086 if (!HASHITEM_EMPTY(hi))
7087 {
7088 --todo;
7089 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7090 &ht_stack, list_stack);
7091 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007093
7094 if (ht_stack == NULL)
7095 break;
7096
7097 /* take an item from the stack */
7098 cur_ht = ht_stack->ht;
7099 tempitem = ht_stack;
7100 ht_stack = ht_stack->prev;
7101 free(tempitem);
7102 }
7103
7104 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105}
7106
7107/*
7108 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007109 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7110 *
7111 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007114set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 listitem_T *li;
7117 int abort = FALSE;
7118 list_T *cur_l;
7119 list_stack_T *list_stack = NULL;
7120 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007121
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007122 cur_l = l;
7123 for (;;)
7124 {
7125 if (!abort)
7126 /* Mark each item in the list. If the item contains a hashtab
7127 * it is added to ht_stack, if it contains a list it is added to
7128 * list_stack. */
7129 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7130 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7131 ht_stack, &list_stack);
7132 if (list_stack == NULL)
7133 break;
7134
7135 /* take an item from the stack */
7136 cur_l = list_stack->list;
7137 tempitem = list_stack;
7138 list_stack = list_stack->prev;
7139 free(tempitem);
7140 }
7141
7142 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007143}
7144
7145/*
7146 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007147 * "list_stack" is used to add lists to be marked. Can be NULL.
7148 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7149 *
7150 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007151 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007152 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007153set_ref_in_item(
7154 typval_T *tv,
7155 int copyID,
7156 ht_stack_T **ht_stack,
7157 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007158{
7159 dict_T *dd;
7160 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007163 if (tv->v_type == VAR_DICT || tv->v_type == VAR_PARTIAL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007164 {
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007165 if (tv->v_type == VAR_DICT)
7166 dd = tv->vval.v_dict;
7167 else if (tv->vval.v_partial != NULL)
7168 dd = tv->vval.v_partial->pt_dict;
7169 else
7170 dd = NULL;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007171 if (dd != NULL && dd->dv_copyID != copyID)
7172 {
7173 /* Didn't see this dict yet. */
7174 dd->dv_copyID = copyID;
7175 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007176 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007177 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7178 }
7179 else
7180 {
7181 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7182 if (newitem == NULL)
7183 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007184 else
7185 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007186 newitem->ht = &dd->dv_hashtab;
7187 newitem->prev = *ht_stack;
7188 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007189 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007190 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007191 }
7192 }
7193 else if (tv->v_type == VAR_LIST)
7194 {
7195 ll = tv->vval.v_list;
7196 if (ll != NULL && ll->lv_copyID != copyID)
7197 {
7198 /* Didn't see this list yet. */
7199 ll->lv_copyID = copyID;
7200 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007201 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007202 abort = set_ref_in_list(ll, copyID, ht_stack);
7203 }
7204 else
7205 {
7206 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007207 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007208 if (newitem == NULL)
7209 abort = TRUE;
7210 else
7211 {
7212 newitem->list = ll;
7213 newitem->prev = *list_stack;
7214 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007215 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007216 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007217 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007218 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007219 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007220}
7221
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007222 static void
7223partial_free(partial_T *pt, int free_dict)
7224{
7225 int i;
7226
7227 for (i = 0; i < pt->pt_argc; ++i)
7228 clear_tv(&pt->pt_argv[i]);
7229 vim_free(pt->pt_argv);
7230 if (free_dict)
7231 dict_unref(pt->pt_dict);
7232 func_unref(pt->pt_name);
7233 vim_free(pt->pt_name);
7234 vim_free(pt);
7235}
7236
7237/*
7238 * Unreference a closure: decrement the reference count and free it when it
7239 * becomes zero.
7240 */
7241 void
7242partial_unref(partial_T *pt)
7243{
7244 if (pt != NULL && --pt->pt_refcount <= 0)
7245 partial_free(pt, TRUE);
7246}
7247
Bram Moolenaard9fba312005-06-26 22:34:35 +00007248/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249 * Allocate an empty header for a dictionary.
7250 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007251 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007252dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007253{
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007257 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007258 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007259 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007260 if (first_dict != NULL)
7261 first_dict->dv_used_prev = d;
7262 d->dv_used_next = first_dict;
7263 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007264 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007265
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007267 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007268 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007269 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007270 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007271 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273}
7274
7275/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007276 * Allocate an empty dict for a return value.
7277 * Returns OK or FAIL.
7278 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007279 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007280rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007281{
7282 dict_T *d = dict_alloc();
7283
7284 if (d == NULL)
7285 return FAIL;
7286
7287 rettv->vval.v_dict = d;
7288 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007289 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007290 ++d->dv_refcount;
7291 return OK;
7292}
7293
7294
7295/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296 * Unreference a Dictionary: decrement the reference count and free it when it
7297 * becomes zero.
7298 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007300dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007302 if (d != NULL && --d->dv_refcount <= 0)
7303 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304}
7305
7306/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007307 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 * Ignores the reference count.
7309 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007310 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007311dict_free(
7312 dict_T *d,
7313 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007316 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007317 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007318
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007319 /* Remove the dict from the list of dicts for garbage collection. */
7320 if (d->dv_used_prev == NULL)
7321 first_dict = d->dv_used_next;
7322 else
7323 d->dv_used_prev->dv_used_next = d->dv_used_next;
7324 if (d->dv_used_next != NULL)
7325 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7326
7327 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007328 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007329 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007330 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 if (!HASHITEM_EMPTY(hi))
7333 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007334 /* Remove the item before deleting it, just in case there is
7335 * something recursive causing trouble. */
7336 di = HI2DI(hi);
7337 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007338 if (recurse || (di->di_tv.v_type != VAR_LIST
7339 && di->di_tv.v_type != VAR_DICT))
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007340 {
7341 if (!recurse && di->di_tv.v_type == VAR_PARTIAL)
7342 {
7343 partial_T *pt = di->di_tv.vval.v_partial;
7344
7345 /* We unref the partial but not the dict it refers to. */
7346 if (pt != NULL && --pt->pt_refcount == 0)
7347 partial_free(pt, FALSE);
7348 }
7349 else
7350 clear_tv(&di->di_tv);
7351 }
Bram Moolenaar685295c2006-10-15 20:37:38 +00007352 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 --todo;
7354 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357 vim_free(d);
7358}
7359
7360/*
7361 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 * The "key" is copied to the new item.
7363 * Note that the value of the item "di_tv" still needs to be initialized!
7364 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007366 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007367dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368{
Bram Moolenaar33570922005-01-25 22:26:29 +00007369 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007371 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007374 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007375 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007376 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007378}
7379
7380/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007381 * Make a copy of a Dictionary item.
7382 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007384dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385{
Bram Moolenaar33570922005-01-25 22:26:29 +00007386 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007387
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007388 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7389 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007390 if (di != NULL)
7391 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007393 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 copy_tv(&org->di_tv, &di->di_tv);
7395 }
7396 return di;
7397}
7398
7399/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 * Remove item "item" from Dictionary "dict" and free it.
7401 */
7402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007403dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404{
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007406
Bram Moolenaar33570922005-01-25 22:26:29 +00007407 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 if (HASHITEM_EMPTY(hi))
7409 EMSG2(_(e_intern2), "dictitem_remove()");
7410 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007412 dictitem_free(item);
7413}
7414
7415/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416 * Free a dict item. Also clears the value.
7417 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007418 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007419dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007422 if (item->di_flags & DI_FLAGS_ALLOC)
7423 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424}
7425
7426/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007427 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7428 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007429 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007430 * Returns NULL when out of memory.
7431 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007432 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007433dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007434{
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 dict_T *copy;
7436 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007437 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007439
7440 if (orig == NULL)
7441 return NULL;
7442
7443 copy = dict_alloc();
7444 if (copy != NULL)
7445 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007446 if (copyID != 0)
7447 {
7448 orig->dv_copyID = copyID;
7449 orig->dv_copydict = copy;
7450 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007451 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007452 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007454 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007455 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007456 --todo;
7457
7458 di = dictitem_alloc(hi->hi_key);
7459 if (di == NULL)
7460 break;
7461 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007462 {
7463 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7464 copyID) == FAIL)
7465 {
7466 vim_free(di);
7467 break;
7468 }
7469 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007470 else
7471 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7472 if (dict_add(copy, di) == FAIL)
7473 {
7474 dictitem_free(di);
7475 break;
7476 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479
Bram Moolenaare9a41262005-01-15 22:18:47 +00007480 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007481 if (todo > 0)
7482 {
7483 dict_unref(copy);
7484 copy = NULL;
7485 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007486 }
7487
7488 return copy;
7489}
7490
7491/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007493 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007496dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497{
Bram Moolenaar33570922005-01-25 22:26:29 +00007498 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499}
7500
Bram Moolenaar8c711452005-01-14 21:53:12 +00007501/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007502 * Add a number or string entry to dictionary "d".
7503 * When "str" is NULL use number "nr", otherwise use "str".
7504 * Returns FAIL when out of memory and when key already exists.
7505 */
7506 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007507dict_add_nr_str(
7508 dict_T *d,
7509 char *key,
7510 long nr,
7511 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007512{
7513 dictitem_T *item;
7514
7515 item = dictitem_alloc((char_u *)key);
7516 if (item == NULL)
7517 return FAIL;
7518 item->di_tv.v_lock = 0;
7519 if (str == NULL)
7520 {
7521 item->di_tv.v_type = VAR_NUMBER;
7522 item->di_tv.vval.v_number = nr;
7523 }
7524 else
7525 {
7526 item->di_tv.v_type = VAR_STRING;
7527 item->di_tv.vval.v_string = vim_strsave(str);
7528 }
7529 if (dict_add(d, item) == FAIL)
7530 {
7531 dictitem_free(item);
7532 return FAIL;
7533 }
7534 return OK;
7535}
7536
7537/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007538 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007539 * Returns FAIL when out of memory and when key already exists.
7540 */
7541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007542dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007543{
7544 dictitem_T *item;
7545
7546 item = dictitem_alloc((char_u *)key);
7547 if (item == NULL)
7548 return FAIL;
7549 item->di_tv.v_lock = 0;
7550 item->di_tv.v_type = VAR_LIST;
7551 item->di_tv.vval.v_list = list;
7552 if (dict_add(d, item) == FAIL)
7553 {
7554 dictitem_free(item);
7555 return FAIL;
7556 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007557 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007558 return OK;
7559}
7560
7561/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007562 * Get the number of items in a Dictionary.
7563 */
7564 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007565dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007566{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567 if (d == NULL)
7568 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007569 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007570}
7571
7572/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573 * Find item "key[len]" in Dictionary "d".
7574 * If "len" is negative use strlen(key).
7575 * Returns NULL when not found.
7576 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007577 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007578dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580#define AKEYLEN 200
7581 char_u buf[AKEYLEN];
7582 char_u *akey;
7583 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007584 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007585
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 if (len < 0)
7587 akey = key;
7588 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 tofree = akey = vim_strnsave(key, len);
7591 if (akey == NULL)
7592 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007593 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 else
7595 {
7596 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007597 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 akey = buf;
7599 }
7600
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007602 vim_free(tofree);
7603 if (HASHITEM_EMPTY(hi))
7604 return NULL;
7605 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606}
7607
7608/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007609 * Get a string item from a dictionary.
7610 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007611 * Returns NULL if the entry doesn't exist or out of memory.
7612 */
7613 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007614get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007615{
7616 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007617 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007618
7619 di = dict_find(d, key, -1);
7620 if (di == NULL)
7621 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007622 s = get_tv_string(&di->di_tv);
7623 if (save && s != NULL)
7624 s = vim_strsave(s);
7625 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007626}
7627
7628/*
7629 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007630 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007631 */
7632 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007633get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007634{
7635 dictitem_T *di;
7636
7637 di = dict_find(d, key, -1);
7638 if (di == NULL)
7639 return 0;
7640 return get_tv_number(&di->di_tv);
7641}
7642
7643/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007644 * Return an allocated string with the string representation of a Dictionary.
7645 * May return NULL.
7646 */
7647 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007648dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007649{
7650 garray_T ga;
7651 int first = TRUE;
7652 char_u *tofree;
7653 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007654 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 return NULL;
7661 ga_init2(&ga, (int)sizeof(char), 80);
7662 ga_append(&ga, '{');
7663
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007664 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007665 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007669 --todo;
7670
7671 if (first)
7672 first = FALSE;
7673 else
7674 ga_concat(&ga, (char_u *)", ");
7675
7676 tofree = string_quote(hi->hi_key, FALSE);
7677 if (tofree != NULL)
7678 {
7679 ga_concat(&ga, tofree);
7680 vim_free(tofree);
7681 }
7682 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007683 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007684 if (s != NULL)
7685 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007687 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007688 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007689 line_breakcheck();
7690
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007693 if (todo > 0)
7694 {
7695 vim_free(ga.ga_data);
7696 return NULL;
7697 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698
7699 ga_append(&ga, '}');
7700 ga_append(&ga, NUL);
7701 return (char_u *)ga.ga_data;
7702}
7703
7704/*
7705 * Allocate a variable for a Dictionary and fill it from "*arg".
7706 * Return OK or FAIL. Returns NOTDONE for {expr}.
7707 */
7708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007709get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710{
Bram Moolenaar33570922005-01-25 22:26:29 +00007711 dict_T *d = NULL;
7712 typval_T tvkey;
7713 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007714 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007717 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718
7719 /*
7720 * First check if it's not a curly-braces thing: {expr}.
7721 * Must do this without evaluating, otherwise a function may be called
7722 * twice. Unfortunately this means we need to call eval1() twice for the
7723 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007724 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007726 if (*start != '}')
7727 {
7728 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7729 return FAIL;
7730 if (*start == '}')
7731 return NOTDONE;
7732 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733
7734 if (evaluate)
7735 {
7736 d = dict_alloc();
7737 if (d == NULL)
7738 return FAIL;
7739 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007740 tvkey.v_type = VAR_UNKNOWN;
7741 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742
7743 *arg = skipwhite(*arg + 1);
7744 while (**arg != '}' && **arg != NUL)
7745 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007746 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747 goto failret;
7748 if (**arg != ':')
7749 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007750 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 goto failret;
7753 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007754 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007756 key = get_tv_string_buf_chk(&tvkey, buf);
7757 if (key == NULL || *key == NUL)
7758 {
7759 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7760 if (key != NULL)
7761 EMSG(_(e_emptykey));
7762 clear_tv(&tvkey);
7763 goto failret;
7764 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007766
7767 *arg = skipwhite(*arg + 1);
7768 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7769 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007770 if (evaluate)
7771 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007772 goto failret;
7773 }
7774 if (evaluate)
7775 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007776 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 if (item != NULL)
7778 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007779 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007780 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007781 clear_tv(&tv);
7782 goto failret;
7783 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007784 item = dictitem_alloc(key);
7785 clear_tv(&tvkey);
7786 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007787 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007789 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007790 if (dict_add(d, item) == FAIL)
7791 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007792 }
7793 }
7794
7795 if (**arg == '}')
7796 break;
7797 if (**arg != ',')
7798 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007799 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007800 goto failret;
7801 }
7802 *arg = skipwhite(*arg + 1);
7803 }
7804
7805 if (**arg != '}')
7806 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007807 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007808failret:
7809 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007810 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007811 return FAIL;
7812 }
7813
7814 *arg = skipwhite(*arg + 1);
7815 if (evaluate)
7816 {
7817 rettv->v_type = VAR_DICT;
7818 rettv->vval.v_dict = d;
7819 ++d->dv_refcount;
7820 }
7821
7822 return OK;
7823}
7824
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007825#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007826#endif
7827
Bram Moolenaar17a13432016-01-24 14:22:10 +01007828 static char *
7829get_var_special_name(int nr)
7830{
7831 switch (nr)
7832 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007833 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007834 case VVAL_TRUE: return "v:true";
7835 case VVAL_NONE: return "v:none";
7836 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007837 }
7838 EMSG2(_(e_intern2), "get_var_special_name()");
7839 return "42";
7840}
7841
Bram Moolenaar8c711452005-01-14 21:53:12 +00007842/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007843 * Return a string with the string representation of a variable.
7844 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007845 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007846 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007847 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007848 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007849 */
7850 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007851echo_string(
7852 typval_T *tv,
7853 char_u **tofree,
7854 char_u *numbuf,
7855 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007856{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857 static int recurse = 0;
7858 char_u *r = NULL;
7859
Bram Moolenaar33570922005-01-25 22:26:29 +00007860 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007861 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007862 if (!did_echo_string_emsg)
7863 {
7864 /* Only give this message once for a recursive call to avoid
7865 * flooding the user with errors. And stop iterating over lists
7866 * and dicts. */
7867 did_echo_string_emsg = TRUE;
7868 EMSG(_("E724: variable nested too deep for displaying"));
7869 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007870 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007871 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007872 }
7873 ++recurse;
7874
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007875 switch (tv->v_type)
7876 {
7877 case VAR_FUNC:
7878 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007879 r = tv->vval.v_string;
7880 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007881
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007882 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007883 {
7884 partial_T *pt = tv->vval.v_partial;
7885 char_u *fname = string_quote(pt == NULL ? NULL
7886 : pt->pt_name, FALSE);
7887 garray_T ga;
7888 int i;
7889 char_u *tf;
7890
7891 ga_init2(&ga, 1, 100);
7892 ga_concat(&ga, (char_u *)"function(");
7893 if (fname != NULL)
7894 {
7895 ga_concat(&ga, fname);
7896 vim_free(fname);
7897 }
7898 if (pt != NULL && pt->pt_argc > 0)
7899 {
7900 ga_concat(&ga, (char_u *)", [");
7901 for (i = 0; i < pt->pt_argc; ++i)
7902 {
7903 if (i > 0)
7904 ga_concat(&ga, (char_u *)", ");
7905 ga_concat(&ga,
7906 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7907 vim_free(tf);
7908 }
7909 ga_concat(&ga, (char_u *)"]");
7910 }
7911 if (pt != NULL && pt->pt_dict != NULL)
7912 {
7913 typval_T dtv;
7914
7915 ga_concat(&ga, (char_u *)", ");
7916 dtv.v_type = VAR_DICT;
7917 dtv.vval.v_dict = pt->pt_dict;
7918 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7919 vim_free(tf);
7920 }
7921 ga_concat(&ga, (char_u *)")");
7922
7923 *tofree = ga.ga_data;
7924 r = *tofree;
7925 break;
7926 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007927
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007929 if (tv->vval.v_list == NULL)
7930 {
7931 *tofree = NULL;
7932 r = NULL;
7933 }
7934 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7935 {
7936 *tofree = NULL;
7937 r = (char_u *)"[...]";
7938 }
7939 else
7940 {
7941 tv->vval.v_list->lv_copyID = copyID;
7942 *tofree = list2string(tv, copyID);
7943 r = *tofree;
7944 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007945 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007946
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007948 if (tv->vval.v_dict == NULL)
7949 {
7950 *tofree = NULL;
7951 r = NULL;
7952 }
7953 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7954 {
7955 *tofree = NULL;
7956 r = (char_u *)"{...}";
7957 }
7958 else
7959 {
7960 tv->vval.v_dict->dv_copyID = copyID;
7961 *tofree = dict2string(tv, copyID);
7962 r = *tofree;
7963 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007964 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007966 case VAR_STRING:
7967 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007968 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007969 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007970 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007971 *tofree = NULL;
7972 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007973 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007974
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007975 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007976#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007977 *tofree = NULL;
7978 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7979 r = numbuf;
7980 break;
7981#endif
7982
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007983 case VAR_SPECIAL:
7984 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007985 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007986 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007987 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007988
Bram Moolenaar8502c702014-06-17 12:51:16 +02007989 if (--recurse == 0)
7990 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007991 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007992}
7993
7994/*
7995 * Return a string with the string representation of a variable.
7996 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7997 * "numbuf" is used for a number.
7998 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007999 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008000 */
8001 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008002tv2string(
8003 typval_T *tv,
8004 char_u **tofree,
8005 char_u *numbuf,
8006 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008007{
8008 switch (tv->v_type)
8009 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008010 case VAR_FUNC:
8011 *tofree = string_quote(tv->vval.v_string, TRUE);
8012 return *tofree;
8013 case VAR_STRING:
8014 *tofree = string_quote(tv->vval.v_string, FALSE);
8015 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008016 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008017#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008018 *tofree = NULL;
8019 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8020 return numbuf;
8021#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008022 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008023 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008024 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008025 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008026 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008027 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008028 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008029 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008030 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008032 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008033}
8034
8035/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 * Return string "str" in ' quotes, doubling ' characters.
8037 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008038 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008039 */
8040 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008041string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008042{
Bram Moolenaar33570922005-01-25 22:26:29 +00008043 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008044 char_u *p, *r, *s;
8045
Bram Moolenaar33570922005-01-25 22:26:29 +00008046 len = (function ? 13 : 3);
8047 if (str != NULL)
8048 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008049 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008050 for (p = str; *p != NUL; mb_ptr_adv(p))
8051 if (*p == '\'')
8052 ++len;
8053 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008054 s = r = alloc(len);
8055 if (r != NULL)
8056 {
8057 if (function)
8058 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008059 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008060 r += 10;
8061 }
8062 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008063 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008064 if (str != NULL)
8065 for (p = str; *p != NUL; )
8066 {
8067 if (*p == '\'')
8068 *r++ = '\'';
8069 MB_COPY_CHAR(p, r);
8070 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008071 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008072 if (function)
8073 *r++ = ')';
8074 *r++ = NUL;
8075 }
8076 return s;
8077}
8078
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008079#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080/*
8081 * Convert the string "text" to a floating point number.
8082 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8083 * this always uses a decimal point.
8084 * Returns the length of the text that was consumed.
8085 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008086 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008087string2float(
8088 char_u *text,
8089 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008090{
8091 char *s = (char *)text;
8092 float_T f;
8093
8094 f = strtod(s, &s);
8095 *value = f;
8096 return (int)((char_u *)s - text);
8097}
8098#endif
8099
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 * Get the value of an environment variable.
8102 * "arg" is pointing to the '$'. It is advanced to after the name.
8103 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008104 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 */
8106 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008107get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108{
8109 char_u *string = NULL;
8110 int len;
8111 int cc;
8112 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008113 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114
8115 ++*arg;
8116 name = *arg;
8117 len = get_env_len(arg);
8118 if (evaluate)
8119 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008120 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008121 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008122
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008123 cc = name[len];
8124 name[len] = NUL;
8125 /* first try vim_getenv(), fast for normal environment vars */
8126 string = vim_getenv(name, &mustfree);
8127 if (string != NULL && *string != NUL)
8128 {
8129 if (!mustfree)
8130 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008132 else
8133 {
8134 if (mustfree)
8135 vim_free(string);
8136
8137 /* next try expanding things like $VIM and ${HOME} */
8138 string = expand_env_save(name - 1);
8139 if (string != NULL && *string == '$')
8140 {
8141 vim_free(string);
8142 string = NULL;
8143 }
8144 }
8145 name[len] = cc;
8146
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008147 rettv->v_type = VAR_STRING;
8148 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 }
8150
8151 return OK;
8152}
8153
8154/*
8155 * Array with names and number of arguments of all internal functions
8156 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8157 */
8158static struct fst
8159{
8160 char *f_name; /* function name */
8161 char f_min_argc; /* minimal number of arguments */
8162 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008163 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008164 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165} functions[] =
8166{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008167#ifdef FEAT_FLOAT
8168 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008169 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008170#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008171 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008172 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008173 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {"append", 2, 2, f_append},
8175 {"argc", 0, 0, f_argc},
8176 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008177 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008178 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008179#ifdef FEAT_FLOAT
8180 {"asin", 1, 1, f_asin}, /* WJMc */
8181#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008182 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008183 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008184 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008185 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008186 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008187 {"assert_notequal", 2, 3, f_assert_notequal},
8188 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008189 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008190#ifdef FEAT_FLOAT
8191 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008192 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008195 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"bufexists", 1, 1, f_bufexists},
8197 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8198 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8199 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8200 {"buflisted", 1, 1, f_buflisted},
8201 {"bufloaded", 1, 1, f_bufloaded},
8202 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008203 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"bufwinnr", 1, 1, f_bufwinnr},
8205 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008206 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008207 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008208 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008209#ifdef FEAT_FLOAT
8210 {"ceil", 1, 1, f_ceil},
8211#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008212#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008213 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008214 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8215 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008216 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008217 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008218 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008219 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008220 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008221 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008222 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008223 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008224 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8225 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008226 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008227 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008228#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008229 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008230 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008232 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008234#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008235 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008236 {"complete_add", 1, 1, f_complete_add},
8237 {"complete_check", 0, 0, f_complete_check},
8238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008240 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008241#ifdef FEAT_FLOAT
8242 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008243 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008244#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008245 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008247 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008248 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008249 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008251 {"diff_filler", 1, 1, f_diff_filler},
8252 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008253 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008254 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008256 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {"eventhandler", 0, 0, f_eventhandler},
8258 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008259 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008261#ifdef FEAT_FLOAT
8262 {"exp", 1, 1, f_exp},
8263#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008264 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008265 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008266 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8268 {"filereadable", 1, 1, f_filereadable},
8269 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008270 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008271 {"finddir", 1, 3, f_finddir},
8272 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#ifdef FEAT_FLOAT
8274 {"float2nr", 1, 1, f_float2nr},
8275 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008276 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008277#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008278 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"fnamemodify", 2, 2, f_fnamemodify},
8280 {"foldclosed", 1, 1, f_foldclosed},
8281 {"foldclosedend", 1, 1, f_foldclosedend},
8282 {"foldlevel", 1, 1, f_foldlevel},
8283 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008284 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008286 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008287 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008288 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008289 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008290 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 {"getchar", 0, 1, f_getchar},
8292 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008293 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"getcmdline", 0, 0, f_getcmdline},
8295 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008296 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008297 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008298 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008299 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008300 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008301 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"getfsize", 1, 1, f_getfsize},
8303 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008304 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008305 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008306 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008307 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008308 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008309 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008310 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008311 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008313 {"gettabvar", 2, 3, f_gettabvar},
8314 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"getwinposx", 0, 0, f_getwinposx},
8316 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008317 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008318 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008319 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008320 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008322 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008323 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008324 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8326 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8327 {"histadd", 2, 2, f_histadd},
8328 {"histdel", 1, 2, f_histdel},
8329 {"histget", 1, 2, f_histget},
8330 {"histnr", 1, 1, f_histnr},
8331 {"hlID", 1, 1, f_hlID},
8332 {"hlexists", 1, 1, f_hlexists},
8333 {"hostname", 0, 0, f_hostname},
8334 {"iconv", 3, 3, f_iconv},
8335 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008336 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008337 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008339 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"inputrestore", 0, 0, f_inputrestore},
8341 {"inputsave", 0, 0, f_inputsave},
8342 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008343 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008344 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008346 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008347#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8348 {"isnan", 1, 1, f_isnan},
8349#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008350 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008351#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008352 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008353 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008354 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008355 {"job_start", 1, 2, f_job_start},
8356 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008357 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008358#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008359 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008360 {"js_decode", 1, 1, f_js_decode},
8361 {"js_encode", 1, 1, f_js_encode},
8362 {"json_decode", 1, 1, f_json_decode},
8363 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008364 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008366 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 {"libcall", 3, 3, f_libcall},
8368 {"libcallnr", 3, 3, f_libcallnr},
8369 {"line", 1, 1, f_line},
8370 {"line2byte", 1, 1, f_line2byte},
8371 {"lispindent", 1, 1, f_lispindent},
8372 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008373#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008374 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008375 {"log10", 1, 1, f_log10},
8376#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008377#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008378 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008379#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008380 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008381 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008382 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008383 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008384 {"matchadd", 2, 5, f_matchadd},
8385 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008386 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008387 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008388 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008389 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008390 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008391 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008392 {"max", 1, 1, f_max},
8393 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008394#ifdef vim_mkdir
8395 {"mkdir", 1, 3, f_mkdir},
8396#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008397 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008398#ifdef FEAT_MZSCHEME
8399 {"mzeval", 1, 1, f_mzeval},
8400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008402 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008403 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008404 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008405#ifdef FEAT_PERL
8406 {"perleval", 1, 1, f_perleval},
8407#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008408#ifdef FEAT_FLOAT
8409 {"pow", 2, 2, f_pow},
8410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008412 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008413 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008414#ifdef FEAT_PYTHON3
8415 {"py3eval", 1, 1, f_py3eval},
8416#endif
8417#ifdef FEAT_PYTHON
8418 {"pyeval", 1, 1, f_pyeval},
8419#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008420 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008421 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008422 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008423#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008424 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008425#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008426 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 {"remote_expr", 2, 3, f_remote_expr},
8428 {"remote_foreground", 1, 1, f_remote_foreground},
8429 {"remote_peek", 1, 2, f_remote_peek},
8430 {"remote_read", 1, 1, f_remote_read},
8431 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008432 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008434 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008436 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008437#ifdef FEAT_FLOAT
8438 {"round", 1, 1, f_round},
8439#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008440 {"screenattr", 2, 2, f_screenattr},
8441 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008442 {"screencol", 0, 0, f_screencol},
8443 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008444 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008445 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008446 {"searchpair", 3, 7, f_searchpair},
8447 {"searchpairpos", 3, 7, f_searchpairpos},
8448 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"server2client", 2, 2, f_server2client},
8450 {"serverlist", 0, 0, f_serverlist},
8451 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008452 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008454 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008456 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008457 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008458 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008459 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008461 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008462 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008464#ifdef FEAT_CRYPT
8465 {"sha256", 1, 1, f_sha256},
8466#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008467 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008468 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008470#ifdef FEAT_FLOAT
8471 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008472 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008473#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008474 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008475 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008476 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008477 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008478 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008479#ifdef FEAT_FLOAT
8480 {"sqrt", 1, 1, f_sqrt},
8481 {"str2float", 1, 1, f_str2float},
8482#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008483 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008484 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008485 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486#ifdef HAVE_STRFTIME
8487 {"strftime", 1, 2, f_strftime},
8488#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008489 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008490 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {"strlen", 1, 1, f_strlen},
8492 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008493 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008495 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008496 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 {"substitute", 4, 4, f_substitute},
8498 {"synID", 3, 3, f_synID},
8499 {"synIDattr", 2, 3, f_synIDattr},
8500 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008501 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008502 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008503 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008504 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008505 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008506 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008507 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008508 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008509 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008510#ifdef FEAT_FLOAT
8511 {"tan", 1, 1, f_tan},
8512 {"tanh", 1, 1, f_tanh},
8513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008515 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008516#ifdef FEAT_TIMERS
8517 {"timer_start", 2, 3, f_timer_start},
8518 {"timer_stop", 1, 1, f_timer_stop},
8519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"tolower", 1, 1, f_tolower},
8521 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008522 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008523#ifdef FEAT_FLOAT
8524 {"trunc", 1, 1, f_trunc},
8525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008527 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008528 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008529 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008530 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {"virtcol", 1, 1, f_virtcol},
8532 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008533 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008534 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008535 {"win_getid", 0, 2, f_win_getid},
8536 {"win_gotoid", 1, 1, f_win_gotoid},
8537 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8538 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 {"winbufnr", 1, 1, f_winbufnr},
8540 {"wincol", 0, 0, f_wincol},
8541 {"winheight", 1, 1, f_winheight},
8542 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008543 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008545 {"winrestview", 1, 1, f_winrestview},
8546 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008548 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008549 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008550 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551};
8552
8553#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8554
8555/*
8556 * Function given to ExpandGeneric() to obtain the list of internal
8557 * or user defined function names.
8558 */
8559 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008560get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562 static int intidx = -1;
8563 char_u *name;
8564
8565 if (idx == 0)
8566 intidx = -1;
8567 if (intidx < 0)
8568 {
8569 name = get_user_func_name(xp, idx);
8570 if (name != NULL)
8571 return name;
8572 }
8573 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8574 {
8575 STRCPY(IObuff, functions[intidx].f_name);
8576 STRCAT(IObuff, "(");
8577 if (functions[intidx].f_max_argc == 0)
8578 STRCAT(IObuff, ")");
8579 return IObuff;
8580 }
8581
8582 return NULL;
8583}
8584
8585/*
8586 * Function given to ExpandGeneric() to obtain the list of internal or
8587 * user defined variable or function names.
8588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008590get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
8592 static int intidx = -1;
8593 char_u *name;
8594
8595 if (idx == 0)
8596 intidx = -1;
8597 if (intidx < 0)
8598 {
8599 name = get_function_name(xp, idx);
8600 if (name != NULL)
8601 return name;
8602 }
8603 return get_user_var_name(xp, ++intidx);
8604}
8605
8606#endif /* FEAT_CMDL_COMPL */
8607
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008608#if defined(EBCDIC) || defined(PROTO)
8609/*
8610 * Compare struct fst by function name.
8611 */
8612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008613compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008614{
8615 struct fst *p1 = (struct fst *)s1;
8616 struct fst *p2 = (struct fst *)s2;
8617
8618 return STRCMP(p1->f_name, p2->f_name);
8619}
8620
8621/*
8622 * Sort the function table by function name.
8623 * The sorting of the table above is ASCII dependant.
8624 * On machines using EBCDIC we have to sort it.
8625 */
8626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008627sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008628{
8629 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8630
8631 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8632}
8633#endif
8634
8635
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636/*
8637 * Find internal function in table above.
8638 * Return index, or -1 if not found
8639 */
8640 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008641find_internal_func(
8642 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
8644 int first = 0;
8645 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8646 int cmp;
8647 int x;
8648
8649 /*
8650 * Find the function name in the table. Binary search.
8651 */
8652 while (first <= last)
8653 {
8654 x = first + ((unsigned)(last - first) >> 1);
8655 cmp = STRCMP(name, functions[x].f_name);
8656 if (cmp < 0)
8657 last = x - 1;
8658 else if (cmp > 0)
8659 first = x + 1;
8660 else
8661 return x;
8662 }
8663 return -1;
8664}
8665
8666/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008667 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8668 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008669 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8670 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008671 */
8672 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008673deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008674{
Bram Moolenaar33570922005-01-25 22:26:29 +00008675 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008676 int cc;
8677
Bram Moolenaar65639032016-03-16 21:40:30 +01008678 if (partialp != NULL)
8679 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008680
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008681 cc = name[*lenp];
8682 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008683 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008684 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008685 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008686 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008687 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008688 {
8689 *lenp = 0;
8690 return (char_u *)""; /* just in case */
8691 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008692 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008693 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008694 }
8695
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008696 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8697 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008698 partial_T *pt = v->di_tv.vval.v_partial;
8699
8700 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008701 {
8702 *lenp = 0;
8703 return (char_u *)""; /* just in case */
8704 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008705 if (partialp != NULL)
8706 *partialp = pt;
8707 *lenp = (int)STRLEN(pt->pt_name);
8708 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008709 }
8710
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008711 return name;
8712}
8713
8714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 * Allocate a variable for the result of a function.
8716 * Return OK or FAIL.
8717 */
8718 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008719get_func_tv(
8720 char_u *name, /* name of the function */
8721 int len, /* length of "name" */
8722 typval_T *rettv,
8723 char_u **arg, /* argument, pointing to the '(' */
8724 linenr_T firstline, /* first line of range */
8725 linenr_T lastline, /* last line of range */
8726 int *doesrange, /* return: function handled range */
8727 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008728 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008729 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 char_u *argp;
8732 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008733 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 int argcount = 0; /* number of arguments found */
8735
8736 /*
8737 * Get the arguments.
8738 */
8739 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008740 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 {
8742 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8743 if (*argp == ')' || *argp == ',' || *argp == NUL)
8744 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8746 {
8747 ret = FAIL;
8748 break;
8749 }
8750 ++argcount;
8751 if (*argp != ',')
8752 break;
8753 }
8754 if (*argp == ')')
8755 ++argp;
8756 else
8757 ret = FAIL;
8758
8759 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008761 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008763 {
8764 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008765 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008766 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008767 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
8770 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008771 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
8773 *arg = skipwhite(argp);
8774 return ret;
8775}
8776
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008777#define ERROR_UNKNOWN 0
8778#define ERROR_TOOMANY 1
8779#define ERROR_TOOFEW 2
8780#define ERROR_SCRIPT 3
8781#define ERROR_DICT 4
8782#define ERROR_NONE 5
8783#define ERROR_OTHER 6
8784#define FLEN_FIXED 40
8785
8786/*
8787 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8788 * Change <SNR>123_name() to K_SNR 123_name().
8789 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8790 * (slow).
8791 */
8792 static char_u *
8793fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8794{
8795 int llen;
8796 char_u *fname;
8797 int i;
8798
8799 llen = eval_fname_script(name);
8800 if (llen > 0)
8801 {
8802 fname_buf[0] = K_SPECIAL;
8803 fname_buf[1] = KS_EXTRA;
8804 fname_buf[2] = (int)KE_SNR;
8805 i = 3;
8806 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8807 {
8808 if (current_SID <= 0)
8809 *error = ERROR_SCRIPT;
8810 else
8811 {
8812 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8813 i = (int)STRLEN(fname_buf);
8814 }
8815 }
8816 if (i + STRLEN(name + llen) < FLEN_FIXED)
8817 {
8818 STRCPY(fname_buf + i, name + llen);
8819 fname = fname_buf;
8820 }
8821 else
8822 {
8823 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8824 if (fname == NULL)
8825 *error = ERROR_OTHER;
8826 else
8827 {
8828 *tofree = fname;
8829 mch_memmove(fname, fname_buf, (size_t)i);
8830 STRCPY(fname + i, name + llen);
8831 }
8832 }
8833 }
8834 else
8835 fname = name;
8836 return fname;
8837}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
8839/*
8840 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008841 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008842 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008844 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008845call_func(
8846 char_u *funcname, /* name of the function */
8847 int len, /* length of "name" */
8848 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008849 int argcount_in, /* number of "argvars" */
8850 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008851 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008852 linenr_T firstline, /* first line of range */
8853 linenr_T lastline, /* last line of range */
8854 int *doesrange, /* return: function handled range */
8855 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008856 partial_T *partial, /* optional, can be NULL */
8857 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858{
8859 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 int error = ERROR_NONE;
8861 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008864 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008866 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008867 int argcount = argcount_in;
8868 typval_T *argvars = argvars_in;
8869 dict_T *selfdict = selfdict_in;
8870 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8871 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008872
8873 /* Make a copy of the name, if it comes from a funcref variable it could
8874 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008875 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008876 if (name == NULL)
8877 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008879 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
8881 *doesrange = FALSE;
8882
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008883 if (partial != NULL)
8884 {
8885 if (partial->pt_dict != NULL)
8886 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008887 /* When the function has a partial with a dict and there is a dict
8888 * argument, use the dict argument. That is backwards compatible.
8889 */
8890 if (selfdict_in == NULL)
8891 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008892 }
8893 if (error == ERROR_NONE && partial->pt_argc > 0)
8894 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008895 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8896 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8897 for (i = 0; i < argcount_in; ++i)
8898 argv[i + argv_clear] = argvars_in[i];
8899 argvars = argv;
8900 argcount = partial->pt_argc + argcount_in;
8901 }
8902 }
8903
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904
8905 /* execute the function if no errors detected and executing */
8906 if (evaluate && error == ERROR_NONE)
8907 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008908 char_u *rfname = fname;
8909
8910 /* Ignore "g:" before a function name. */
8911 if (fname[0] == 'g' && fname[1] == ':')
8912 rfname = fname + 2;
8913
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008914 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8915 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 error = ERROR_UNKNOWN;
8917
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008918 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 {
8920 /*
8921 * User defined function.
8922 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008923 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008924
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008926 /* Trigger FuncUndefined event, may load the function. */
8927 if (fp == NULL
8928 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008929 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008930 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008932 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008933 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 }
8935#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008936 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008937 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008938 {
8939 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008940 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008941 }
8942
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 if (fp != NULL)
8944 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008945 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008947 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008949 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008951 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008952 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 else
8954 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008955 int did_save_redo = FALSE;
8956
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 /*
8958 * Call the user function.
8959 * Save and restore search patterns, script variables and
8960 * redo buffer.
8961 */
8962 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008963#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008964 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008965#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008966 {
8967 saveRedobuff();
8968 did_save_redo = TRUE;
8969 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008970 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008972 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008973 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8974 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8975 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008976 /* Function was unreferenced while being used, free it
8977 * now. */
8978 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008979 if (did_save_redo)
8980 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 restore_search_patterns();
8982 error = ERROR_NONE;
8983 }
8984 }
8985 }
8986 else
8987 {
8988 /*
8989 * Find the function name in the table, call its implementation.
8990 */
8991 i = find_internal_func(fname);
8992 if (i >= 0)
8993 {
8994 if (argcount < functions[i].f_min_argc)
8995 error = ERROR_TOOFEW;
8996 else if (argcount > functions[i].f_max_argc)
8997 error = ERROR_TOOMANY;
8998 else
8999 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009000 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009001 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 error = ERROR_NONE;
9003 }
9004 }
9005 }
9006 /*
9007 * The function call (or "FuncUndefined" autocommand sequence) might
9008 * have been aborted by an error, an interrupt, or an explicitly thrown
9009 * exception that has not been caught so far. This situation can be
9010 * tested for by calling aborting(). For an error in an internal
9011 * function or for the "E132" error in call_user_func(), however, the
9012 * throw point at which the "force_abort" flag (temporarily reset by
9013 * emsg()) is normally updated has not been reached yet. We need to
9014 * update that flag first to make aborting() reliable.
9015 */
9016 update_force_abort();
9017 }
9018 if (error == ERROR_NONE)
9019 ret = OK;
9020
9021 /*
9022 * Report an error unless the argument evaluation or function call has been
9023 * cancelled due to an aborting error, an interrupt, or an exception.
9024 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009025 if (!aborting())
9026 {
9027 switch (error)
9028 {
9029 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009030 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009031 break;
9032 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009033 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009034 break;
9035 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009036 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009037 name);
9038 break;
9039 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009040 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009041 name);
9042 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009043 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009044 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 name);
9046 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009047 }
9048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009050 while (argv_clear > 0)
9051 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009052 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009053 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054
9055 return ret;
9056}
9057
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009058/*
9059 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009060 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009061 */
9062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009063emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009064{
9065 char_u *p;
9066
9067 if (*name == K_SPECIAL)
9068 p = concat_str((char_u *)"<SNR>", name + 3);
9069 else
9070 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009071 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009072 if (p != name)
9073 vim_free(p);
9074}
9075
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009076/*
9077 * Return TRUE for a non-zero Number and a non-empty String.
9078 */
9079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009080non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009081{
9082 return ((argvars[0].v_type == VAR_NUMBER
9083 && argvars[0].vval.v_number != 0)
9084 || (argvars[0].v_type == VAR_STRING
9085 && argvars[0].vval.v_string != NULL
9086 && *argvars[0].vval.v_string != NUL));
9087}
9088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089/*********************************************
9090 * Implementation of the built-in functions
9091 */
9092
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009093#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009094static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009095
9096/*
9097 * Get the float value of "argvars[0]" into "f".
9098 * Returns FAIL when the argument is not a Number or Float.
9099 */
9100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009101get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009102{
9103 if (argvars[0].v_type == VAR_FLOAT)
9104 {
9105 *f = argvars[0].vval.v_float;
9106 return OK;
9107 }
9108 if (argvars[0].v_type == VAR_NUMBER)
9109 {
9110 *f = (float_T)argvars[0].vval.v_number;
9111 return OK;
9112 }
9113 EMSG(_("E808: Number or Float required"));
9114 return FAIL;
9115}
9116
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009117/*
9118 * "abs(expr)" function
9119 */
9120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009121f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009122{
9123 if (argvars[0].v_type == VAR_FLOAT)
9124 {
9125 rettv->v_type = VAR_FLOAT;
9126 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9127 }
9128 else
9129 {
9130 varnumber_T n;
9131 int error = FALSE;
9132
9133 n = get_tv_number_chk(&argvars[0], &error);
9134 if (error)
9135 rettv->vval.v_number = -1;
9136 else if (n > 0)
9137 rettv->vval.v_number = n;
9138 else
9139 rettv->vval.v_number = -n;
9140 }
9141}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009142
9143/*
9144 * "acos()" function
9145 */
9146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009147f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009148{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009149 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009150
9151 rettv->v_type = VAR_FLOAT;
9152 if (get_float_arg(argvars, &f) == OK)
9153 rettv->vval.v_float = acos(f);
9154 else
9155 rettv->vval.v_float = 0.0;
9156}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009157#endif
9158
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009160 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 */
9162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009163f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009168 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009170 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009171 && !tv_check_lock(l->lv_lock,
9172 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009173 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009175 }
9176 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009177 EMSG(_(e_listreq));
9178}
9179
9180/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009181 * "alloc_fail(id, countdown, repeat)" function
9182 */
9183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009184f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009185{
9186 if (argvars[0].v_type != VAR_NUMBER
9187 || argvars[0].vval.v_number <= 0
9188 || argvars[1].v_type != VAR_NUMBER
9189 || argvars[1].vval.v_number < 0
9190 || argvars[2].v_type != VAR_NUMBER)
9191 EMSG(_(e_invarg));
9192 else
9193 {
9194 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009195 if (alloc_fail_id >= aid_last)
9196 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009197 alloc_fail_countdown = argvars[1].vval.v_number;
9198 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009199 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009200 }
9201}
9202
9203/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009204 * "and(expr, expr)" function
9205 */
9206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009207f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009208{
9209 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9210 & get_tv_number_chk(&argvars[1], NULL);
9211}
9212
9213/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009214 * "append(lnum, string/list)" function
9215 */
9216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009217f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009218{
9219 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009220 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009221 list_T *l = NULL;
9222 listitem_T *li = NULL;
9223 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009224 long added = 0;
9225
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009226 /* When coming here from Insert mode, sync undo, so that this can be
9227 * undone separately from what was previously inserted. */
9228 if (u_sync_once == 2)
9229 {
9230 u_sync_once = 1; /* notify that u_sync() was called */
9231 u_sync(TRUE);
9232 }
9233
Bram Moolenaar0d660222005-01-07 21:51:51 +00009234 lnum = get_tv_lnum(argvars);
9235 if (lnum >= 0
9236 && lnum <= curbuf->b_ml.ml_line_count
9237 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009238 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009239 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009240 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009241 l = argvars[1].vval.v_list;
9242 if (l == NULL)
9243 return;
9244 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009245 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009246 for (;;)
9247 {
9248 if (l == NULL)
9249 tv = &argvars[1]; /* append a string */
9250 else if (li == NULL)
9251 break; /* end of list */
9252 else
9253 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009254 line = get_tv_string_chk(tv);
9255 if (line == NULL) /* type error */
9256 {
9257 rettv->vval.v_number = 1; /* Failed */
9258 break;
9259 }
9260 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009261 ++added;
9262 if (l == NULL)
9263 break;
9264 li = li->li_next;
9265 }
9266
9267 appended_lines_mark(lnum, added);
9268 if (curwin->w_cursor.lnum > lnum)
9269 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 else
9272 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273}
9274
9275/*
9276 * "argc()" function
9277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009279f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282}
9283
9284/*
9285 * "argidx()" function
9286 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009288f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009290 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291}
9292
9293/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009294 * "arglistid()" function
9295 */
9296 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009297f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009298{
9299 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009300
9301 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009302 wp = find_tabwin(&argvars[0], &argvars[1]);
9303 if (wp != NULL)
9304 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009305}
9306
9307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 * "argv(nr)" function
9309 */
9310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009311f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312{
9313 int idx;
9314
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009315 if (argvars[0].v_type != VAR_UNKNOWN)
9316 {
9317 idx = get_tv_number_chk(&argvars[0], NULL);
9318 if (idx >= 0 && idx < ARGCOUNT)
9319 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9320 else
9321 rettv->vval.v_string = NULL;
9322 rettv->v_type = VAR_STRING;
9323 }
9324 else if (rettv_list_alloc(rettv) == OK)
9325 for (idx = 0; idx < ARGCOUNT; ++idx)
9326 list_append_string(rettv->vval.v_list,
9327 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328}
9329
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009330typedef enum
9331{
9332 ASSERT_EQUAL,
9333 ASSERT_NOTEQUAL,
9334 ASSERT_MATCH,
9335 ASSERT_NOTMATCH,
9336 ASSERT_OTHER,
9337} assert_type_T;
9338
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009339static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009340static 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 +01009341static void assert_error(garray_T *gap);
9342static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009343
9344/*
9345 * Prepare "gap" for an assert error and add the sourcing position.
9346 */
9347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009348prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009349{
9350 char buf[NUMBUFLEN];
9351
9352 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009353 if (sourcing_name != NULL)
9354 {
9355 ga_concat(gap, sourcing_name);
9356 if (sourcing_lnum > 0)
9357 ga_concat(gap, (char_u *)" ");
9358 }
9359 if (sourcing_lnum > 0)
9360 {
9361 sprintf(buf, "line %ld", (long)sourcing_lnum);
9362 ga_concat(gap, (char_u *)buf);
9363 }
9364 if (sourcing_name != NULL || sourcing_lnum > 0)
9365 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009366}
9367
9368/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009369 * Append "str" to "gap", escaping unprintable characters.
9370 * Changes NL to \n, CR to \r, etc.
9371 */
9372 static void
9373ga_concat_esc(garray_T *gap, char_u *str)
9374{
9375 char_u *p;
9376 char_u buf[NUMBUFLEN];
9377
Bram Moolenaarf1551962016-03-15 12:55:58 +01009378 if (str == NULL)
9379 {
9380 ga_concat(gap, (char_u *)"NULL");
9381 return;
9382 }
9383
Bram Moolenaar23689172016-02-15 22:37:37 +01009384 for (p = str; *p != NUL; ++p)
9385 switch (*p)
9386 {
9387 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9388 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9389 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9390 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9391 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9392 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9393 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9394 default:
9395 if (*p < ' ')
9396 {
9397 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9398 ga_concat(gap, buf);
9399 }
9400 else
9401 ga_append(gap, *p);
9402 break;
9403 }
9404}
9405
9406/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009407 * Fill "gap" with information about an assert error.
9408 */
9409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009410fill_assert_error(
9411 garray_T *gap,
9412 typval_T *opt_msg_tv,
9413 char_u *exp_str,
9414 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009415 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009416 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009417{
9418 char_u numbuf[NUMBUFLEN];
9419 char_u *tofree;
9420
9421 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9422 {
9423 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9424 vim_free(tofree);
9425 }
9426 else
9427 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009428 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009429 ga_concat(gap, (char_u *)"Pattern ");
9430 else
9431 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009432 if (exp_str == NULL)
9433 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009434 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009435 vim_free(tofree);
9436 }
9437 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009438 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009439 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009440 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009441 else if (atype == ASSERT_NOTMATCH)
9442 ga_concat(gap, (char_u *)" does match ");
9443 else if (atype == ASSERT_NOTEQUAL)
9444 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009445 else
9446 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009447 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009448 vim_free(tofree);
9449 }
9450}
Bram Moolenaar43345542015-11-29 17:35:35 +01009451
9452/*
9453 * Add an assert error to v:errors.
9454 */
9455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009456assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009457{
9458 struct vimvar *vp = &vimvars[VV_ERRORS];
9459
9460 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9461 /* Make sure v:errors is a list. */
9462 set_vim_var_list(VV_ERRORS, list_alloc());
9463 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9464}
9465
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009466 static void
9467assert_equal_common(typval_T *argvars, assert_type_T atype)
9468{
9469 garray_T ga;
9470
9471 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9472 != (atype == ASSERT_EQUAL))
9473 {
9474 prepare_assert_error(&ga);
9475 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9476 atype);
9477 assert_error(&ga);
9478 ga_clear(&ga);
9479 }
9480}
9481
Bram Moolenaar43345542015-11-29 17:35:35 +01009482/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009483 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009484 */
9485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009486f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009487{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009488 assert_equal_common(argvars, ASSERT_EQUAL);
9489}
Bram Moolenaar43345542015-11-29 17:35:35 +01009490
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009491/*
9492 * "assert_notequal(expected, actual[, msg])" function
9493 */
9494 static void
9495f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9496{
9497 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009498}
9499
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009500/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009501 * "assert_exception(string[, msg])" function
9502 */
9503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009504f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009505{
9506 garray_T ga;
9507 char *error;
9508
9509 error = (char *)get_tv_string_chk(&argvars[0]);
9510 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9511 {
9512 prepare_assert_error(&ga);
9513 ga_concat(&ga, (char_u *)"v:exception is not set");
9514 assert_error(&ga);
9515 ga_clear(&ga);
9516 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009517 else if (error != NULL
9518 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009519 {
9520 prepare_assert_error(&ga);
9521 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009522 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009523 assert_error(&ga);
9524 ga_clear(&ga);
9525 }
9526}
9527
9528/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009529 * "assert_fails(cmd [, error])" function
9530 */
9531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009532f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009533{
9534 char_u *cmd = get_tv_string_chk(&argvars[0]);
9535 garray_T ga;
9536
9537 called_emsg = FALSE;
9538 suppress_errthrow = TRUE;
9539 emsg_silent = TRUE;
9540 do_cmdline_cmd(cmd);
9541 if (!called_emsg)
9542 {
9543 prepare_assert_error(&ga);
9544 ga_concat(&ga, (char_u *)"command did not fail: ");
9545 ga_concat(&ga, cmd);
9546 assert_error(&ga);
9547 ga_clear(&ga);
9548 }
9549 else if (argvars[1].v_type != VAR_UNKNOWN)
9550 {
9551 char_u buf[NUMBUFLEN];
9552 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9553
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009554 if (error == NULL
9555 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009556 {
9557 prepare_assert_error(&ga);
9558 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009559 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009560 assert_error(&ga);
9561 ga_clear(&ga);
9562 }
9563 }
9564
9565 called_emsg = FALSE;
9566 suppress_errthrow = FALSE;
9567 emsg_silent = FALSE;
9568 emsg_on_display = FALSE;
9569 set_vim_var_string(VV_ERRMSG, NULL, 0);
9570}
9571
9572/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009573 * Common for assert_true() and assert_false().
9574 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009576assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009577{
9578 int error = FALSE;
9579 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009580
Bram Moolenaar37127922016-02-06 20:29:28 +01009581 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009582 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009583 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009584 if (argvars[0].v_type != VAR_NUMBER
9585 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9586 || error)
9587 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009588 prepare_assert_error(&ga);
9589 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009590 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009591 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009592 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009593 ga_clear(&ga);
9594 }
9595}
9596
9597/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009598 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009599 */
9600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009601f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009602{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009603 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009604}
9605
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009606 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009607assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009608{
9609 garray_T ga;
9610 char_u buf1[NUMBUFLEN];
9611 char_u buf2[NUMBUFLEN];
9612 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9613 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9614
Bram Moolenaar72188e92016-03-28 22:48:29 +02009615 if (pat == NULL || text == NULL)
9616 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009617 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009618 {
9619 prepare_assert_error(&ga);
9620 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009621 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009622 assert_error(&ga);
9623 ga_clear(&ga);
9624 }
9625}
9626
9627/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009628 * "assert_match(pattern, actual[, msg])" function
9629 */
9630 static void
9631f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9632{
9633 assert_match_common(argvars, ASSERT_MATCH);
9634}
9635
9636/*
9637 * "assert_notmatch(pattern, actual[, msg])" function
9638 */
9639 static void
9640f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9641{
9642 assert_match_common(argvars, ASSERT_NOTMATCH);
9643}
9644
9645/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009646 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009647 */
9648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009649f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009650{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009651 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009652}
9653
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009654#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009655/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009656 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009657 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009659f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009660{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009661 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009662
9663 rettv->v_type = VAR_FLOAT;
9664 if (get_float_arg(argvars, &f) == OK)
9665 rettv->vval.v_float = asin(f);
9666 else
9667 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009668}
9669
9670/*
9671 * "atan()" function
9672 */
9673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009674f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009675{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009676 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009677
9678 rettv->v_type = VAR_FLOAT;
9679 if (get_float_arg(argvars, &f) == OK)
9680 rettv->vval.v_float = atan(f);
9681 else
9682 rettv->vval.v_float = 0.0;
9683}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009684
9685/*
9686 * "atan2()" function
9687 */
9688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009689f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009690{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009691 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009692
9693 rettv->v_type = VAR_FLOAT;
9694 if (get_float_arg(argvars, &fx) == OK
9695 && get_float_arg(&argvars[1], &fy) == OK)
9696 rettv->vval.v_float = atan2(fx, fy);
9697 else
9698 rettv->vval.v_float = 0.0;
9699}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009700#endif
9701
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702/*
9703 * "browse(save, title, initdir, default)" function
9704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009706f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708#ifdef FEAT_BROWSE
9709 int save;
9710 char_u *title;
9711 char_u *initdir;
9712 char_u *defname;
9713 char_u buf[NUMBUFLEN];
9714 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009715 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009717 save = get_tv_number_chk(&argvars[0], &error);
9718 title = get_tv_string_chk(&argvars[1]);
9719 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9720 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009722 if (error || title == NULL || initdir == NULL || defname == NULL)
9723 rettv->vval.v_string = NULL;
9724 else
9725 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009726 do_browse(save ? BROWSE_SAVE : 0,
9727 title, defname, NULL, initdir, NULL, curbuf);
9728#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009730#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009732}
9733
9734/*
9735 * "browsedir(title, initdir)" function
9736 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009738f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009739{
9740#ifdef FEAT_BROWSE
9741 char_u *title;
9742 char_u *initdir;
9743 char_u buf[NUMBUFLEN];
9744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009745 title = get_tv_string_chk(&argvars[0]);
9746 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009747
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009748 if (title == NULL || initdir == NULL)
9749 rettv->vval.v_string = NULL;
9750 else
9751 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009752 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009757}
9758
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009759static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009760
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761/*
9762 * Find a buffer by number or exact name.
9763 */
9764 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009765find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766{
9767 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009769 if (avar->v_type == VAR_NUMBER)
9770 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009771 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009773 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009774 if (buf == NULL)
9775 {
9776 /* No full path name match, try a match with a URL or a "nofile"
9777 * buffer, these don't use the full path. */
9778 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9779 if (buf->b_fname != NULL
9780 && (path_with_url(buf->b_fname)
9781#ifdef FEAT_QUICKFIX
9782 || bt_nofile(buf)
9783#endif
9784 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009785 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009786 break;
9787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 }
9789 return buf;
9790}
9791
9792/*
9793 * "bufexists(expr)" function
9794 */
9795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009796f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799}
9800
9801/*
9802 * "buflisted(expr)" function
9803 */
9804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009805f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 buf_T *buf;
9808
9809 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811}
9812
9813/*
9814 * "bufloaded(expr)" function
9815 */
9816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009817f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818{
9819 buf_T *buf;
9820
9821 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823}
9824
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009825 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009826buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 int save_magic;
9829 char_u *save_cpo;
9830 buf_T *buf;
9831
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9833 save_magic = p_magic;
9834 p_magic = TRUE;
9835 save_cpo = p_cpo;
9836 p_cpo = (char_u *)"";
9837
9838 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009839 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
9841 p_magic = save_magic;
9842 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009843 return buf;
9844}
9845
9846/*
9847 * Get buffer by number or pattern.
9848 */
9849 static buf_T *
9850get_buf_tv(typval_T *tv, int curtab_only)
9851{
9852 char_u *name = tv->vval.v_string;
9853 buf_T *buf;
9854
9855 if (tv->v_type == VAR_NUMBER)
9856 return buflist_findnr((int)tv->vval.v_number);
9857 if (tv->v_type != VAR_STRING)
9858 return NULL;
9859 if (name == NULL || *name == NUL)
9860 return curbuf;
9861 if (name[0] == '$' && name[1] == NUL)
9862 return lastbuf;
9863
9864 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865
9866 /* If not found, try expanding the name, like done for bufexists(). */
9867 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869
9870 return buf;
9871}
9872
9873/*
9874 * "bufname(expr)" function
9875 */
9876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009877f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878{
9879 buf_T *buf;
9880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009883 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009884 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 --emsg_off;
9890}
9891
9892/*
9893 * "bufnr(expr)" function
9894 */
9895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009896f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897{
9898 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009899 int error = FALSE;
9900 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009902 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009904 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009905 --emsg_off;
9906
9907 /* If the buffer isn't found and the second argument is not zero create a
9908 * new buffer. */
9909 if (buf == NULL
9910 && argvars[1].v_type != VAR_UNKNOWN
9911 && get_tv_number_chk(&argvars[1], &error) != 0
9912 && !error
9913 && (name = get_tv_string_chk(&argvars[0])) != NULL
9914 && !error)
9915 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9916
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009920 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921}
9922
9923/*
9924 * "bufwinnr(nr)" function
9925 */
9926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009927f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928{
9929#ifdef FEAT_WINDOWS
9930 win_T *wp;
9931 int winnr = 0;
9932#endif
9933 buf_T *buf;
9934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009937 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938#ifdef FEAT_WINDOWS
9939 for (wp = firstwin; wp; wp = wp->w_next)
9940 {
9941 ++winnr;
9942 if (wp->w_buffer == buf)
9943 break;
9944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009947 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948#endif
9949 --emsg_off;
9950}
9951
9952/*
9953 * "byte2line(byte)" function
9954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009956f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957{
9958#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960#else
9961 long boff = 0;
9962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009963 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 (linenr_T)0, &boff);
9969#endif
9970}
9971
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009973byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009974{
9975#ifdef FEAT_MBYTE
9976 char_u *t;
9977#endif
9978 char_u *str;
9979 long idx;
9980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009981 str = get_tv_string_chk(&argvars[0]);
9982 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009984 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009985 return;
9986
9987#ifdef FEAT_MBYTE
9988 t = str;
9989 for ( ; idx > 0; idx--)
9990 {
9991 if (*t == NUL) /* EOL reached */
9992 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009993 if (enc_utf8 && comp)
9994 t += utf_ptr2len(t);
9995 else
9996 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009997 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009998 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009999#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010000 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010002#endif
10003}
10004
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010005/*
10006 * "byteidx()" function
10007 */
10008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010009f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010010{
10011 byteidx(argvars, rettv, FALSE);
10012}
10013
10014/*
10015 * "byteidxcomp()" function
10016 */
10017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010018f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010019{
10020 byteidx(argvars, rettv, TRUE);
10021}
10022
Bram Moolenaardb913952012-06-29 12:54:53 +020010023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010024func_call(
10025 char_u *name,
10026 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010027 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010028 dict_T *selfdict,
10029 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010030{
10031 listitem_T *item;
10032 typval_T argv[MAX_FUNC_ARGS + 1];
10033 int argc = 0;
10034 int dummy;
10035 int r = 0;
10036
10037 for (item = args->vval.v_list->lv_first; item != NULL;
10038 item = item->li_next)
10039 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010040 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010041 {
10042 EMSG(_("E699: Too many arguments"));
10043 break;
10044 }
10045 /* Make a copy of each argument. This is needed to be able to set
10046 * v_lock to VAR_FIXED in the copy without changing the original list.
10047 */
10048 copy_tv(&item->li_tv, &argv[argc++]);
10049 }
10050
10051 if (item == NULL)
10052 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10053 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010054 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010055
10056 /* Free the arguments. */
10057 while (argc > 0)
10058 clear_tv(&argv[--argc]);
10059
10060 return r;
10061}
10062
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010063/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010064 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010065 */
10066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010067f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010068{
10069 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010070 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010071 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010072
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010073 if (argvars[1].v_type != VAR_LIST)
10074 {
10075 EMSG(_(e_listreq));
10076 return;
10077 }
10078 if (argvars[1].vval.v_list == NULL)
10079 return;
10080
10081 if (argvars[0].v_type == VAR_FUNC)
10082 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010083 else if (argvars[0].v_type == VAR_PARTIAL)
10084 {
10085 partial = argvars[0].vval.v_partial;
10086 func = partial->pt_name;
10087 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010088 else
10089 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010090 if (*func == NUL)
10091 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010092
Bram Moolenaare9a41262005-01-15 22:18:47 +000010093 if (argvars[2].v_type != VAR_UNKNOWN)
10094 {
10095 if (argvars[2].v_type != VAR_DICT)
10096 {
10097 EMSG(_(e_dictreq));
10098 return;
10099 }
10100 selfdict = argvars[2].vval.v_dict;
10101 }
10102
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010103 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010104}
10105
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010106#ifdef FEAT_FLOAT
10107/*
10108 * "ceil({float})" function
10109 */
10110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010111f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010112{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010113 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010114
10115 rettv->v_type = VAR_FLOAT;
10116 if (get_float_arg(argvars, &f) == OK)
10117 rettv->vval.v_float = ceil(f);
10118 else
10119 rettv->vval.v_float = 0.0;
10120}
10121#endif
10122
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010123#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010124/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010125 * "ch_close()" function
10126 */
10127 static void
10128f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10129{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010130 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010131
10132 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010133 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010134 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010135 channel_clear(channel);
10136 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010137}
10138
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010139/*
10140 * "ch_getbufnr()" function
10141 */
10142 static void
10143f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10144{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010145 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010146
10147 rettv->vval.v_number = -1;
10148 if (channel != NULL)
10149 {
10150 char_u *what = get_tv_string(&argvars[1]);
10151 int part;
10152
10153 if (STRCMP(what, "err") == 0)
10154 part = PART_ERR;
10155 else if (STRCMP(what, "out") == 0)
10156 part = PART_OUT;
10157 else if (STRCMP(what, "in") == 0)
10158 part = PART_IN;
10159 else
10160 part = PART_SOCK;
10161 if (channel->ch_part[part].ch_buffer != NULL)
10162 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10163 }
10164}
10165
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010166/*
10167 * "ch_getjob()" function
10168 */
10169 static void
10170f_ch_getjob(typval_T *argvars, typval_T *rettv)
10171{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010172 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010173
10174 if (channel != NULL)
10175 {
10176 rettv->v_type = VAR_JOB;
10177 rettv->vval.v_job = channel->ch_job;
10178 if (channel->ch_job != NULL)
10179 ++channel->ch_job->jv_refcount;
10180 }
10181}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010182
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010183/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010184 * "ch_info()" function
10185 */
10186 static void
10187f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10188{
10189 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10190
10191 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10192 channel_info(channel, rettv->vval.v_dict);
10193}
10194
10195/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010196 * "ch_log()" function
10197 */
10198 static void
10199f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10200{
10201 char_u *msg = get_tv_string(&argvars[0]);
10202 channel_T *channel = NULL;
10203
10204 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010205 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010206
10207 ch_log(channel, (char *)msg);
10208}
10209
10210/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010211 * "ch_logfile()" function
10212 */
10213 static void
10214f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10215{
10216 char_u *fname;
10217 char_u *opt = (char_u *)"";
10218 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010219
10220 fname = get_tv_string(&argvars[0]);
10221 if (argvars[1].v_type == VAR_STRING)
10222 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010223 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010224}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010225
10226/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010227 * "ch_open()" function
10228 */
10229 static void
10230f_ch_open(typval_T *argvars, typval_T *rettv)
10231{
Bram Moolenaar77073442016-02-13 23:23:53 +010010232 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010233 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010234}
10235
10236/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010237 * "ch_read()" function
10238 */
10239 static void
10240f_ch_read(typval_T *argvars, typval_T *rettv)
10241{
10242 common_channel_read(argvars, rettv, FALSE);
10243}
10244
10245/*
10246 * "ch_readraw()" function
10247 */
10248 static void
10249f_ch_readraw(typval_T *argvars, typval_T *rettv)
10250{
10251 common_channel_read(argvars, rettv, TRUE);
10252}
10253
10254/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010255 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010256 */
10257 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010258f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10259{
10260 ch_expr_common(argvars, rettv, TRUE);
10261}
10262
10263/*
10264 * "ch_sendexpr()" function
10265 */
10266 static void
10267f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10268{
10269 ch_expr_common(argvars, rettv, FALSE);
10270}
10271
10272/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010273 * "ch_evalraw()" function
10274 */
10275 static void
10276f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10277{
10278 ch_raw_common(argvars, rettv, TRUE);
10279}
10280
10281/*
10282 * "ch_sendraw()" function
10283 */
10284 static void
10285f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10286{
10287 ch_raw_common(argvars, rettv, FALSE);
10288}
10289
10290/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010291 * "ch_setoptions()" function
10292 */
10293 static void
10294f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10295{
10296 channel_T *channel;
10297 jobopt_T opt;
10298
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010299 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010300 if (channel == NULL)
10301 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010302 clear_job_options(&opt);
10303 if (get_job_options(&argvars[1], &opt,
10304 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010305 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010306 channel_set_options(channel, &opt);
10307}
10308
10309/*
10310 * "ch_status()" function
10311 */
10312 static void
10313f_ch_status(typval_T *argvars, typval_T *rettv)
10314{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010315 channel_T *channel;
10316
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010317 /* return an empty string by default */
10318 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010319 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010320
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010321 channel = get_channel_arg(&argvars[0], FALSE);
10322 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010323}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010324#endif
10325
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010326/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010327 * "changenr()" function
10328 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010330f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010331{
10332 rettv->vval.v_number = curbuf->b_u_seq_cur;
10333}
10334
10335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 * "char2nr(string)" function
10337 */
10338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010339f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340{
10341#ifdef FEAT_MBYTE
10342 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010343 {
10344 int utf8 = 0;
10345
10346 if (argvars[1].v_type != VAR_UNKNOWN)
10347 utf8 = get_tv_number_chk(&argvars[1], NULL);
10348
10349 if (utf8)
10350 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10351 else
10352 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 else
10355#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010356 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357}
10358
10359/*
10360 * "cindent(lnum)" function
10361 */
10362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010363f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364{
10365#ifdef FEAT_CINDENT
10366 pos_T pos;
10367 linenr_T lnum;
10368
10369 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010370 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10372 {
10373 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010374 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010375 curwin->w_cursor = pos;
10376 }
10377 else
10378#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380}
10381
10382/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010383 * "clearmatches()" function
10384 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010386f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010387{
10388#ifdef FEAT_SEARCH_EXTRA
10389 clear_matches(curwin);
10390#endif
10391}
10392
10393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 * "col(string)" function
10395 */
10396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010397f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398{
10399 colnr_T col = 0;
10400 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010401 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010403 fp = var2fpos(&argvars[0], FALSE, &fnum);
10404 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 {
10406 if (fp->col == MAXCOL)
10407 {
10408 /* '> can be MAXCOL, get the length of the line then */
10409 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010410 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411 else
10412 col = MAXCOL;
10413 }
10414 else
10415 {
10416 col = fp->col + 1;
10417#ifdef FEAT_VIRTUALEDIT
10418 /* col(".") when the cursor is on the NUL at the end of the line
10419 * because of "coladd" can be seen as an extra column. */
10420 if (virtual_active() && fp == &curwin->w_cursor)
10421 {
10422 char_u *p = ml_get_cursor();
10423
10424 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10425 curwin->w_virtcol - curwin->w_cursor.coladd))
10426 {
10427# ifdef FEAT_MBYTE
10428 int l;
10429
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010430 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 col += l;
10432# else
10433 if (*p != NUL && p[1] == NUL)
10434 ++col;
10435# endif
10436 }
10437 }
10438#endif
10439 }
10440 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010441 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442}
10443
Bram Moolenaar572cb562005-08-05 21:35:02 +000010444#if defined(FEAT_INS_EXPAND)
10445/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010446 * "complete()" function
10447 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010448 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010449f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010450{
10451 int startcol;
10452
10453 if ((State & INSERT) == 0)
10454 {
10455 EMSG(_("E785: complete() can only be used in Insert mode"));
10456 return;
10457 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010458
10459 /* Check for undo allowed here, because if something was already inserted
10460 * the line was already saved for undo and this check isn't done. */
10461 if (!undo_allowed())
10462 return;
10463
Bram Moolenaarade00832006-03-10 21:46:58 +000010464 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10465 {
10466 EMSG(_(e_invarg));
10467 return;
10468 }
10469
10470 startcol = get_tv_number_chk(&argvars[0], NULL);
10471 if (startcol <= 0)
10472 return;
10473
10474 set_completion(startcol - 1, argvars[1].vval.v_list);
10475}
10476
10477/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010478 * "complete_add()" function
10479 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010481f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010482{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010483 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010484}
10485
10486/*
10487 * "complete_check()" function
10488 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010490f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010491{
10492 int saved = RedrawingDisabled;
10493
10494 RedrawingDisabled = 0;
10495 ins_compl_check_keys(0);
10496 rettv->vval.v_number = compl_interrupted;
10497 RedrawingDisabled = saved;
10498}
10499#endif
10500
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501/*
10502 * "confirm(message, buttons[, default [, type]])" function
10503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010505f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506{
10507#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10508 char_u *message;
10509 char_u *buttons = NULL;
10510 char_u buf[NUMBUFLEN];
10511 char_u buf2[NUMBUFLEN];
10512 int def = 1;
10513 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010514 char_u *typestr;
10515 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 message = get_tv_string_chk(&argvars[0]);
10518 if (message == NULL)
10519 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010520 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010522 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10523 if (buttons == NULL)
10524 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010525 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010528 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010530 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10531 if (typestr == NULL)
10532 error = TRUE;
10533 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 switch (TOUPPER_ASC(*typestr))
10536 {
10537 case 'E': type = VIM_ERROR; break;
10538 case 'Q': type = VIM_QUESTION; break;
10539 case 'I': type = VIM_INFO; break;
10540 case 'W': type = VIM_WARNING; break;
10541 case 'G': type = VIM_GENERIC; break;
10542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 }
10544 }
10545 }
10546 }
10547
10548 if (buttons == NULL || *buttons == NUL)
10549 buttons = (char_u *)_("&Ok");
10550
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010551 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010553 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554#endif
10555}
10556
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010557/*
10558 * "copy()" function
10559 */
10560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010561f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010562{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010563 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010564}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010566#ifdef FEAT_FLOAT
10567/*
10568 * "cos()" function
10569 */
10570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010571f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010572{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010573 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010574
10575 rettv->v_type = VAR_FLOAT;
10576 if (get_float_arg(argvars, &f) == OK)
10577 rettv->vval.v_float = cos(f);
10578 else
10579 rettv->vval.v_float = 0.0;
10580}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010581
10582/*
10583 * "cosh()" function
10584 */
10585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010586f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010587{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010588 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010589
10590 rettv->v_type = VAR_FLOAT;
10591 if (get_float_arg(argvars, &f) == OK)
10592 rettv->vval.v_float = cosh(f);
10593 else
10594 rettv->vval.v_float = 0.0;
10595}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010596#endif
10597
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010599 * "count()" function
10600 */
10601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010602f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010603{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010604 long n = 0;
10605 int ic = FALSE;
10606
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010608 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010609 listitem_T *li;
10610 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010612
Bram Moolenaare9a41262005-01-15 22:18:47 +000010613 if ((l = argvars[0].vval.v_list) != NULL)
10614 {
10615 li = l->lv_first;
10616 if (argvars[2].v_type != VAR_UNKNOWN)
10617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 int error = FALSE;
10619
10620 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010621 if (argvars[3].v_type != VAR_UNKNOWN)
10622 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 idx = get_tv_number_chk(&argvars[3], &error);
10624 if (!error)
10625 {
10626 li = list_find(l, idx);
10627 if (li == NULL)
10628 EMSGN(_(e_listidx), idx);
10629 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010631 if (error)
10632 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010633 }
10634
10635 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010636 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010637 ++n;
10638 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010639 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010640 else if (argvars[0].v_type == VAR_DICT)
10641 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 int todo;
10643 dict_T *d;
10644 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010645
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010646 if ((d = argvars[0].vval.v_dict) != NULL)
10647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010648 int error = FALSE;
10649
Bram Moolenaare9a41262005-01-15 22:18:47 +000010650 if (argvars[2].v_type != VAR_UNKNOWN)
10651 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010652 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010653 if (argvars[3].v_type != VAR_UNKNOWN)
10654 EMSG(_(e_invarg));
10655 }
10656
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010657 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010658 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010659 {
10660 if (!HASHITEM_EMPTY(hi))
10661 {
10662 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010663 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010664 ++n;
10665 }
10666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010667 }
10668 }
10669 else
10670 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010671 rettv->vval.v_number = n;
10672}
10673
10674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10676 *
10677 * Checks the existence of a cscope connection.
10678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010680f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681{
10682#ifdef FEAT_CSCOPE
10683 int num = 0;
10684 char_u *dbpath = NULL;
10685 char_u *prepend = NULL;
10686 char_u buf[NUMBUFLEN];
10687
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010688 if (argvars[0].v_type != VAR_UNKNOWN
10689 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010691 num = (int)get_tv_number(&argvars[0]);
10692 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010693 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010694 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 }
10696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698#endif
10699}
10700
10701/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010702 * "cursor(lnum, col)" function, or
10703 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010705 * Moves the cursor to the specified line and column.
10706 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010709f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710{
10711 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010712#ifdef FEAT_VIRTUALEDIT
10713 long coladd = 0;
10714#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010715 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010717 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010718 if (argvars[1].v_type == VAR_UNKNOWN)
10719 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010720 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010721 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010722
Bram Moolenaar493c1782014-05-28 14:34:46 +020010723 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010724 {
10725 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010726 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010727 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010728 line = pos.lnum;
10729 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010730#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010731 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010732#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010733 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010734 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010735 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010736 set_curswant = FALSE;
10737 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010738 }
10739 else
10740 {
10741 line = get_tv_lnum(argvars);
10742 col = get_tv_number_chk(&argvars[1], NULL);
10743#ifdef FEAT_VIRTUALEDIT
10744 if (argvars[2].v_type != VAR_UNKNOWN)
10745 coladd = get_tv_number_chk(&argvars[2], NULL);
10746#endif
10747 }
10748 if (line < 0 || col < 0
10749#ifdef FEAT_VIRTUALEDIT
10750 || coladd < 0
10751#endif
10752 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010753 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754 if (line > 0)
10755 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 if (col > 0)
10757 curwin->w_cursor.col = col - 1;
10758#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010759 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760#endif
10761
10762 /* Make sure the cursor is in a valid position. */
10763 check_cursor();
10764#ifdef FEAT_MBYTE
10765 /* Correct cursor for multi-byte character. */
10766 if (has_mbyte)
10767 mb_adjust_cursor();
10768#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010769
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010770 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010771 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772}
10773
10774/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010775 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 */
10777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010778f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010780 int noref = 0;
10781
10782 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010783 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010784 if (noref < 0 || noref > 1)
10785 EMSG(_(e_invarg));
10786 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010787 {
10788 current_copyID += COPYID_INC;
10789 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791}
10792
10793/*
10794 * "delete()" function
10795 */
10796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010797f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010799 char_u nbuf[NUMBUFLEN];
10800 char_u *name;
10801 char_u *flags;
10802
10803 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010805 return;
10806
10807 name = get_tv_string(&argvars[0]);
10808 if (name == NULL || *name == NUL)
10809 {
10810 EMSG(_(e_invarg));
10811 return;
10812 }
10813
10814 if (argvars[1].v_type != VAR_UNKNOWN)
10815 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010817 flags = (char_u *)"";
10818
10819 if (*flags == NUL)
10820 /* delete a file */
10821 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10822 else if (STRCMP(flags, "d") == 0)
10823 /* delete an empty directory */
10824 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10825 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010826 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010827 rettv->vval.v_number = delete_recursive(name);
10828 else
10829 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830}
10831
10832/*
10833 * "did_filetype()" function
10834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010836f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837{
10838#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840#endif
10841}
10842
10843/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010844 * "diff_filler()" function
10845 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010847f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010848{
10849#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010851#endif
10852}
10853
10854/*
10855 * "diff_hlID()" function
10856 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010858f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010859{
10860#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010861 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010862 static linenr_T prev_lnum = 0;
10863 static int changedtick = 0;
10864 static int fnum = 0;
10865 static int change_start = 0;
10866 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010867 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010868 int filler_lines;
10869 int col;
10870
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 if (lnum < 0) /* ignore type error in {lnum} arg */
10872 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010873 if (lnum != prev_lnum
10874 || changedtick != curbuf->b_changedtick
10875 || fnum != curbuf->b_fnum)
10876 {
10877 /* New line, buffer, change: need to get the values. */
10878 filler_lines = diff_check(curwin, lnum);
10879 if (filler_lines < 0)
10880 {
10881 if (filler_lines == -1)
10882 {
10883 change_start = MAXCOL;
10884 change_end = -1;
10885 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10886 hlID = HLF_ADD; /* added line */
10887 else
10888 hlID = HLF_CHD; /* changed line */
10889 }
10890 else
10891 hlID = HLF_ADD; /* added line */
10892 }
10893 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010894 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010895 prev_lnum = lnum;
10896 changedtick = curbuf->b_changedtick;
10897 fnum = curbuf->b_fnum;
10898 }
10899
10900 if (hlID == HLF_CHD || hlID == HLF_TXD)
10901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010902 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010903 if (col >= change_start && col <= change_end)
10904 hlID = HLF_TXD; /* changed text */
10905 else
10906 hlID = HLF_CHD; /* changed line */
10907 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010908 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010909#endif
10910}
10911
10912/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010913 * "disable_char_avail_for_testing({expr})" function
10914 */
10915 static void
10916f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10917{
10918 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10919}
10920
10921/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010922 * "empty({expr})" function
10923 */
10924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010925f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010926{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010927 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010928
10929 switch (argvars[0].v_type)
10930 {
10931 case VAR_STRING:
10932 case VAR_FUNC:
10933 n = argvars[0].vval.v_string == NULL
10934 || *argvars[0].vval.v_string == NUL;
10935 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010936 case VAR_PARTIAL:
10937 n = FALSE;
10938 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010939 case VAR_NUMBER:
10940 n = argvars[0].vval.v_number == 0;
10941 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010942 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010943#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010944 n = argvars[0].vval.v_float == 0.0;
10945 break;
10946#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010947 case VAR_LIST:
10948 n = argvars[0].vval.v_list == NULL
10949 || argvars[0].vval.v_list->lv_first == NULL;
10950 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951 case VAR_DICT:
10952 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010953 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010954 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010955 case VAR_SPECIAL:
10956 n = argvars[0].vval.v_number != VVAL_TRUE;
10957 break;
10958
Bram Moolenaar835dc632016-02-07 14:27:38 +010010959 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010960#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010961 n = argvars[0].vval.v_job == NULL
10962 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10963 break;
10964#endif
10965 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010966#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010967 n = argvars[0].vval.v_channel == NULL
10968 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010969 break;
10970#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010971 case VAR_UNKNOWN:
10972 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10973 n = TRUE;
10974 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010975 }
10976
10977 rettv->vval.v_number = n;
10978}
10979
10980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 * "escape({string}, {chars})" function
10982 */
10983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010984f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985{
10986 char_u buf[NUMBUFLEN];
10987
Bram Moolenaar758711c2005-02-02 23:11:38 +000010988 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10989 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010990 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991}
10992
10993/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010994 * "eval()" function
10995 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010997f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010998{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010999 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011001 s = get_tv_string_chk(&argvars[0]);
11002 if (s != NULL)
11003 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011004
Bram Moolenaar615b9972015-01-14 17:15:05 +010011005 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011006 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11007 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011008 if (p != NULL && !aborting())
11009 EMSG2(_(e_invexpr2), p);
11010 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011012 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011013 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011014 else if (*s != NUL)
11015 EMSG(_(e_trailing));
11016}
11017
11018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 * "eventhandler()" function
11020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011022f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011025}
11026
11027/*
11028 * "executable()" function
11029 */
11030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011031f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011033 char_u *name = get_tv_string(&argvars[0]);
11034
11035 /* Check in $PATH and also check directly if there is a directory name. */
11036 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11037 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011038}
11039
11040/*
11041 * "exepath()" function
11042 */
11043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011044f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011045{
11046 char_u *p = NULL;
11047
Bram Moolenaarb5971142015-03-21 17:32:19 +010011048 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011049 rettv->v_type = VAR_STRING;
11050 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051}
11052
11053/*
11054 * "exists()" function
11055 */
11056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011057f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058{
11059 char_u *p;
11060 char_u *name;
11061 int n = FALSE;
11062 int len = 0;
11063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 if (*p == '$') /* environment variable */
11066 {
11067 /* first try "normal" environment variables (fast) */
11068 if (mch_getenv(p + 1) != NULL)
11069 n = TRUE;
11070 else
11071 {
11072 /* try expanding things like $VIM and ${HOME} */
11073 p = expand_env_save(p);
11074 if (p != NULL && *p != '$')
11075 n = TRUE;
11076 vim_free(p);
11077 }
11078 }
11079 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011082 if (*skipwhite(p) != NUL)
11083 n = FALSE; /* trailing garbage */
11084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085 else if (*p == '*') /* internal or user defined function */
11086 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011087 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 }
11089 else if (*p == ':')
11090 {
11091 n = cmd_exists(p + 1);
11092 }
11093 else if (*p == '#')
11094 {
11095#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011096 if (p[1] == '#')
11097 n = autocmd_supported(p + 2);
11098 else
11099 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100#endif
11101 }
11102 else /* internal variable */
11103 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011104 char_u *tofree;
11105 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011107 /* get_name_len() takes care of expanding curly braces */
11108 name = p;
11109 len = get_name_len(&p, &tofree, TRUE, FALSE);
11110 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011112 if (tofree != NULL)
11113 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011114 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011115 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011117 /* handle d.key, l[idx], f(expr) */
11118 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11119 if (n)
11120 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 }
11122 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011123 if (*p != NUL)
11124 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011126 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 }
11128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130}
11131
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011132#ifdef FEAT_FLOAT
11133/*
11134 * "exp()" function
11135 */
11136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011137f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011138{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011139 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011140
11141 rettv->v_type = VAR_FLOAT;
11142 if (get_float_arg(argvars, &f) == OK)
11143 rettv->vval.v_float = exp(f);
11144 else
11145 rettv->vval.v_float = 0.0;
11146}
11147#endif
11148
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149/*
11150 * "expand()" function
11151 */
11152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011153f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154{
11155 char_u *s;
11156 int len;
11157 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011158 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011160 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011161 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011164 if (argvars[1].v_type != VAR_UNKNOWN
11165 && argvars[2].v_type != VAR_UNKNOWN
11166 && get_tv_number_chk(&argvars[2], &error)
11167 && !error)
11168 {
11169 rettv->v_type = VAR_LIST;
11170 rettv->vval.v_list = NULL;
11171 }
11172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 if (*s == '%' || *s == '#' || *s == '<')
11175 {
11176 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011177 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011179 if (rettv->v_type == VAR_LIST)
11180 {
11181 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11182 list_append_string(rettv->vval.v_list, result, -1);
11183 else
11184 vim_free(result);
11185 }
11186 else
11187 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011188 }
11189 else
11190 {
11191 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011192 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011193 if (argvars[1].v_type != VAR_UNKNOWN
11194 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011195 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 if (!error)
11197 {
11198 ExpandInit(&xpc);
11199 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011200 if (p_wic)
11201 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011202 if (rettv->v_type == VAR_STRING)
11203 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11204 options, WILD_ALL);
11205 else if (rettv_list_alloc(rettv) != FAIL)
11206 {
11207 int i;
11208
11209 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11210 for (i = 0; i < xpc.xp_numfiles; i++)
11211 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11212 ExpandCleanup(&xpc);
11213 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011214 }
11215 else
11216 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011217 }
11218}
11219
11220/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011221 * Go over all entries in "d2" and add them to "d1".
11222 * When "action" is "error" then a duplicate key is an error.
11223 * When "action" is "force" then a duplicate key is overwritten.
11224 * Otherwise duplicate keys are ignored ("action" is "keep").
11225 */
11226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011227dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011228{
11229 dictitem_T *di1;
11230 hashitem_T *hi2;
11231 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011232 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011233
11234 todo = (int)d2->dv_hashtab.ht_used;
11235 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11236 {
11237 if (!HASHITEM_EMPTY(hi2))
11238 {
11239 --todo;
11240 di1 = dict_find(d1, hi2->hi_key, -1);
11241 if (d1->dv_scope != 0)
11242 {
11243 /* Disallow replacing a builtin function in l: and g:.
11244 * Check the key to be valid when adding to any
11245 * scope. */
11246 if (d1->dv_scope == VAR_DEF_SCOPE
11247 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11248 && var_check_func_name(hi2->hi_key,
11249 di1 == NULL))
11250 break;
11251 if (!valid_varname(hi2->hi_key))
11252 break;
11253 }
11254 if (di1 == NULL)
11255 {
11256 di1 = dictitem_copy(HI2DI(hi2));
11257 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11258 dictitem_free(di1);
11259 }
11260 else if (*action == 'e')
11261 {
11262 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11263 break;
11264 }
11265 else if (*action == 'f' && HI2DI(hi2) != di1)
11266 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011267 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11268 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011269 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011270 clear_tv(&di1->di_tv);
11271 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11272 }
11273 }
11274 }
11275}
11276
11277/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011278 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011279 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011280 */
11281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011282f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011283{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011284 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011285
Bram Moolenaare9a41262005-01-15 22:18:47 +000011286 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011287 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 list_T *l1, *l2;
11289 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011290 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011291 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011292
Bram Moolenaare9a41262005-01-15 22:18:47 +000011293 l1 = argvars[0].vval.v_list;
11294 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011295 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011296 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011297 {
11298 if (argvars[2].v_type != VAR_UNKNOWN)
11299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011300 before = get_tv_number_chk(&argvars[2], &error);
11301 if (error)
11302 return; /* type error; errmsg already given */
11303
Bram Moolenaar758711c2005-02-02 23:11:38 +000011304 if (before == l1->lv_len)
11305 item = NULL;
11306 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011307 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011308 item = list_find(l1, before);
11309 if (item == NULL)
11310 {
11311 EMSGN(_(e_listidx), before);
11312 return;
11313 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011314 }
11315 }
11316 else
11317 item = NULL;
11318 list_extend(l1, l2, item);
11319
Bram Moolenaare9a41262005-01-15 22:18:47 +000011320 copy_tv(&argvars[0], rettv);
11321 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011322 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011323 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11324 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011325 dict_T *d1, *d2;
11326 char_u *action;
11327 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011328
11329 d1 = argvars[0].vval.v_dict;
11330 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011331 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011332 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011333 {
11334 /* Check the third argument. */
11335 if (argvars[2].v_type != VAR_UNKNOWN)
11336 {
11337 static char *(av[]) = {"keep", "force", "error"};
11338
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011339 action = get_tv_string_chk(&argvars[2]);
11340 if (action == NULL)
11341 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011342 for (i = 0; i < 3; ++i)
11343 if (STRCMP(action, av[i]) == 0)
11344 break;
11345 if (i == 3)
11346 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011347 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011348 return;
11349 }
11350 }
11351 else
11352 action = (char_u *)"force";
11353
Bram Moolenaara9922d62013-05-30 13:01:18 +020011354 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011355
Bram Moolenaare9a41262005-01-15 22:18:47 +000011356 copy_tv(&argvars[0], rettv);
11357 }
11358 }
11359 else
11360 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011361}
11362
11363/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011364 * "feedkeys()" function
11365 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011367f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011368{
11369 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011370 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011371 char_u *keys, *flags;
11372 char_u nbuf[NUMBUFLEN];
11373 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011374 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011375 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011376
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011377 /* This is not allowed in the sandbox. If the commands would still be
11378 * executed in the sandbox it would be OK, but it probably happens later,
11379 * when "sandbox" is no longer set. */
11380 if (check_secure())
11381 return;
11382
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011383 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011384
11385 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011386 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011387 flags = get_tv_string_buf(&argvars[1], nbuf);
11388 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011389 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011390 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011391 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011392 case 'n': remap = FALSE; break;
11393 case 'm': remap = TRUE; break;
11394 case 't': typed = TRUE; break;
11395 case 'i': insert = TRUE; break;
11396 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011397 }
11398 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011399 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011400
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011401 if (*keys != NUL || execute)
11402 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011403 /* Need to escape K_SPECIAL and CSI before putting the string in the
11404 * typeahead buffer. */
11405 keys_esc = vim_strsave_escape_csi(keys);
11406 if (keys_esc != NULL)
11407 {
11408 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011409 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011410 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011411 if (vgetc_busy)
11412 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011413 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011414 {
11415 int save_msg_scroll = msg_scroll;
11416
11417 /* Avoid a 1 second delay when the keys start Insert mode. */
11418 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011419
11420 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011421 exec_normal(TRUE);
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011422 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011423 msg_scroll |= save_msg_scroll;
11424 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011425 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011426 }
11427}
11428
11429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 * "filereadable()" function
11431 */
11432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011433f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011435 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 char_u *p;
11437 int n;
11438
Bram Moolenaarc236c162008-07-13 17:41:49 +000011439#ifndef O_NONBLOCK
11440# define O_NONBLOCK 0
11441#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011443 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11444 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 {
11446 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011447 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448 }
11449 else
11450 n = FALSE;
11451
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011453}
11454
11455/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011456 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 * rights to write into.
11458 */
11459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011460f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011462 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463}
11464
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011465 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011466findfilendir(
11467 typval_T *argvars UNUSED,
11468 typval_T *rettv,
11469 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011470{
11471#ifdef FEAT_SEARCHPATH
11472 char_u *fname;
11473 char_u *fresult = NULL;
11474 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11475 char_u *p;
11476 char_u pathbuf[NUMBUFLEN];
11477 int count = 1;
11478 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011479 int error = FALSE;
11480#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011481
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011482 rettv->vval.v_string = NULL;
11483 rettv->v_type = VAR_STRING;
11484
11485#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011487
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011488 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11491 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011492 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011493 else
11494 {
11495 if (*p != NUL)
11496 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011499 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011501 }
11502
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011503 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11504 error = TRUE;
11505
11506 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011508 do
11509 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011510 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011511 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011512 fresult = find_file_in_path_option(first ? fname : NULL,
11513 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011514 0, first, path,
11515 find_what,
11516 curbuf->b_ffname,
11517 find_what == FINDFILE_DIR
11518 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011519 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011520
11521 if (fresult != NULL && rettv->v_type == VAR_LIST)
11522 list_append_string(rettv->vval.v_list, fresult, -1);
11523
11524 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011525 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011526
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011527 if (rettv->v_type == VAR_STRING)
11528 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011529#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011530}
11531
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011532static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11533static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011534
11535/*
11536 * Implementation of map() and filter().
11537 */
11538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011539filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011540{
11541 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011542 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011543 listitem_T *li, *nli;
11544 list_T *l = NULL;
11545 dictitem_T *di;
11546 hashtab_T *ht;
11547 hashitem_T *hi;
11548 dict_T *d = NULL;
11549 typval_T save_val;
11550 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011551 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011552 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011553 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011554 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011555 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011556 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011557 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011558
Bram Moolenaare9a41262005-01-15 22:18:47 +000011559 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011560 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011561 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011562 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 return;
11564 }
11565 else if (argvars[0].v_type == VAR_DICT)
11566 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011567 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011568 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011569 return;
11570 }
11571 else
11572 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011573 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011574 return;
11575 }
11576
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011577 expr = get_tv_string_buf_chk(&argvars[1], buf);
11578 /* On type errors, the preceding call has already displayed an error
11579 * message. Avoid a misleading error message for an empty string that
11580 * was not passed as argument. */
11581 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011583 prepare_vimvar(VV_VAL, &save_val);
11584 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011585
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011586 /* We reset "did_emsg" to be able to detect whether an error
11587 * occurred during evaluation of the expression. */
11588 save_did_emsg = did_emsg;
11589 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011590
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011591 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011592 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011594 vimvars[VV_KEY].vv_type = VAR_STRING;
11595
11596 ht = &d->dv_hashtab;
11597 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011598 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011601 if (!HASHITEM_EMPTY(hi))
11602 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011603 int r;
11604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011605 --todo;
11606 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011607 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011608 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11609 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011610 break;
11611 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011612 r = filter_map_one(&di->di_tv, expr, map, &rem);
11613 clear_tv(&vimvars[VV_KEY].vv_tv);
11614 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011615 break;
11616 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011617 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011618 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11619 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011620 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011621 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011622 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011623 }
11624 }
11625 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011626 }
11627 else
11628 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011629 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11630
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011631 for (li = l->lv_first; li != NULL; li = nli)
11632 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011633 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011634 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011635 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011636 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011637 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011638 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011639 break;
11640 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011641 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011642 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011643 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011644 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011645
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011646 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011648
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011649 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011650 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011651
11652 copy_tv(&argvars[0], rettv);
11653}
11654
11655 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011656filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011657{
Bram Moolenaar33570922005-01-25 22:26:29 +000011658 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011659 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011660 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011661
Bram Moolenaar33570922005-01-25 22:26:29 +000011662 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011663 s = expr;
11664 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011665 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011666 if (*s != NUL) /* check for trailing chars after expr */
11667 {
11668 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011669 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011670 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011671 }
11672 if (map)
11673 {
11674 /* map(): replace the list item value */
11675 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011676 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011677 *tv = rettv;
11678 }
11679 else
11680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011681 int error = FALSE;
11682
Bram Moolenaare9a41262005-01-15 22:18:47 +000011683 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011684 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011685 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 /* On type error, nothing has been removed; return FAIL to stop the
11687 * loop. The error message was given by get_tv_number_chk(). */
11688 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011689 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011690 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011691 retval = OK;
11692theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011693 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011694 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011695}
11696
11697/*
11698 * "filter()" function
11699 */
11700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011701f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011702{
11703 filter_map(argvars, rettv, FALSE);
11704}
11705
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011707 * "finddir({fname}[, {path}[, {count}]])" function
11708 */
11709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011710f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011711{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011712 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011713}
11714
11715/*
11716 * "findfile({fname}[, {path}[, {count}]])" function
11717 */
11718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011719f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011720{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011721 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011722}
11723
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011724#ifdef FEAT_FLOAT
11725/*
11726 * "float2nr({float})" function
11727 */
11728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011729f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011730{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011731 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011732
11733 if (get_float_arg(argvars, &f) == OK)
11734 {
11735 if (f < -0x7fffffff)
11736 rettv->vval.v_number = -0x7fffffff;
11737 else if (f > 0x7fffffff)
11738 rettv->vval.v_number = 0x7fffffff;
11739 else
11740 rettv->vval.v_number = (varnumber_T)f;
11741 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011742}
11743
11744/*
11745 * "floor({float})" function
11746 */
11747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011748f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011749{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011750 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011751
11752 rettv->v_type = VAR_FLOAT;
11753 if (get_float_arg(argvars, &f) == OK)
11754 rettv->vval.v_float = floor(f);
11755 else
11756 rettv->vval.v_float = 0.0;
11757}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011758
11759/*
11760 * "fmod()" function
11761 */
11762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011763f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011764{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011765 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011766
11767 rettv->v_type = VAR_FLOAT;
11768 if (get_float_arg(argvars, &fx) == OK
11769 && get_float_arg(&argvars[1], &fy) == OK)
11770 rettv->vval.v_float = fmod(fx, fy);
11771 else
11772 rettv->vval.v_float = 0.0;
11773}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011774#endif
11775
Bram Moolenaar0d660222005-01-07 21:51:51 +000011776/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011777 * "fnameescape({string})" function
11778 */
11779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011780f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011781{
11782 rettv->vval.v_string = vim_strsave_fnameescape(
11783 get_tv_string(&argvars[0]), FALSE);
11784 rettv->v_type = VAR_STRING;
11785}
11786
11787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 * "fnamemodify({fname}, {mods})" function
11789 */
11790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011791f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011792{
11793 char_u *fname;
11794 char_u *mods;
11795 int usedlen = 0;
11796 int len;
11797 char_u *fbuf = NULL;
11798 char_u buf[NUMBUFLEN];
11799
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011800 fname = get_tv_string_chk(&argvars[0]);
11801 mods = get_tv_string_buf_chk(&argvars[1], buf);
11802 if (fname == NULL || mods == NULL)
11803 fname = NULL;
11804 else
11805 {
11806 len = (int)STRLEN(fname);
11807 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011810 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011811 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011814 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 vim_free(fbuf);
11816}
11817
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011818static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819
11820/*
11821 * "foldclosed()" function
11822 */
11823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011824foldclosed_both(
11825 typval_T *argvars UNUSED,
11826 typval_T *rettv,
11827 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828{
11829#ifdef FEAT_FOLDING
11830 linenr_T lnum;
11831 linenr_T first, last;
11832
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011833 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11835 {
11836 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11837 {
11838 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011841 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 return;
11843 }
11844 }
11845#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847}
11848
11849/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850 * "foldclosed()" function
11851 */
11852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011853f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011854{
11855 foldclosed_both(argvars, rettv, FALSE);
11856}
11857
11858/*
11859 * "foldclosedend()" function
11860 */
11861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011862f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011863{
11864 foldclosed_both(argvars, rettv, TRUE);
11865}
11866
11867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 * "foldlevel()" function
11869 */
11870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011871f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872{
11873#ifdef FEAT_FOLDING
11874 linenr_T lnum;
11875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880}
11881
11882/*
11883 * "foldtext()" function
11884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011886f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887{
11888#ifdef FEAT_FOLDING
11889 linenr_T lnum;
11890 char_u *s;
11891 char_u *r;
11892 int len;
11893 char *txt;
11894#endif
11895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011896 rettv->v_type = VAR_STRING;
11897 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011899 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11900 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11901 <= curbuf->b_ml.ml_line_count
11902 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 {
11904 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011905 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11906 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011907 {
11908 if (!linewhite(lnum))
11909 break;
11910 ++lnum;
11911 }
11912
11913 /* Find interesting text in this line. */
11914 s = skipwhite(ml_get(lnum));
11915 /* skip C comment-start */
11916 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011919 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011920 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011921 {
11922 s = skipwhite(ml_get(lnum + 1));
11923 if (*s == '*')
11924 s = skipwhite(s + 1);
11925 }
11926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 txt = _("+-%s%3ld lines: ");
11928 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011929 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 + 20 /* for %3ld */
11931 + STRLEN(s))); /* concatenated */
11932 if (r != NULL)
11933 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011934 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11935 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11936 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011937 len = (int)STRLEN(r);
11938 STRCAT(r, s);
11939 /* remove 'foldmarker' and 'commentstring' */
11940 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011941 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 }
11943 }
11944#endif
11945}
11946
11947/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011948 * "foldtextresult(lnum)" function
11949 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011951f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011952{
11953#ifdef FEAT_FOLDING
11954 linenr_T lnum;
11955 char_u *text;
11956 char_u buf[51];
11957 foldinfo_T foldinfo;
11958 int fold_count;
11959#endif
11960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011961 rettv->v_type = VAR_STRING;
11962 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011963#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011964 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011965 /* treat illegal types and illegal string values for {lnum} the same */
11966 if (lnum < 0)
11967 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011968 fold_count = foldedCount(curwin, lnum, &foldinfo);
11969 if (fold_count > 0)
11970 {
11971 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11972 &foldinfo, buf);
11973 if (text == buf)
11974 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011975 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011976 }
11977#endif
11978}
11979
11980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 * "foreground()" function
11982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011984f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986#ifdef FEAT_GUI
11987 if (gui.in_use)
11988 gui_mch_set_foreground();
11989#else
11990# ifdef WIN32
11991 win32_set_foreground();
11992# endif
11993#endif
11994}
11995
11996/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011997 * "function()" function
11998 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012000f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012001{
12002 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012003 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012004 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012005 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012006
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012007 if (argvars[0].v_type == VAR_FUNC)
12008 {
12009 /* function(MyFunc, [arg], dict) */
12010 s = argvars[0].vval.v_string;
12011 }
12012 else if (argvars[0].v_type == VAR_PARTIAL
12013 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012014 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012015 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012016 arg_pt = argvars[0].vval.v_partial;
12017 s = arg_pt->pt_name;
12018 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012019 else
12020 {
12021 /* function('MyFunc', [arg], dict) */
12022 s = get_tv_string(&argvars[0]);
12023 use_string = TRUE;
12024 }
12025
12026 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012027 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012028 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012029 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12030 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012031 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012032 else
12033 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012034 int dict_idx = 0;
12035 int arg_idx = 0;
12036 list_T *list = NULL;
12037
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012038 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012039 {
12040 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012041 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012042
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012043 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12044 * also be called from another script. Using trans_function_name()
12045 * would also work, but some plugins depend on the name being
12046 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012047 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012048 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12049 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012050 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012051 STRCPY(name, sid_buf);
12052 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012053 }
12054 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012055 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012056 name = vim_strsave(s);
12057
12058 if (argvars[1].v_type != VAR_UNKNOWN)
12059 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012060 if (argvars[2].v_type != VAR_UNKNOWN)
12061 {
12062 /* function(name, [args], dict) */
12063 arg_idx = 1;
12064 dict_idx = 2;
12065 }
12066 else if (argvars[1].v_type == VAR_DICT)
12067 /* function(name, dict) */
12068 dict_idx = 1;
12069 else
12070 /* function(name, [args]) */
12071 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012072 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012073 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012074 if (argvars[dict_idx].v_type != VAR_DICT)
12075 {
12076 EMSG(_("E922: expected a dict"));
12077 vim_free(name);
12078 return;
12079 }
12080 if (argvars[dict_idx].vval.v_dict == NULL)
12081 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012082 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012083 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012084 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012085 if (argvars[arg_idx].v_type != VAR_LIST)
12086 {
12087 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12088 vim_free(name);
12089 return;
12090 }
12091 list = argvars[arg_idx].vval.v_list;
12092 if (list == NULL || list->lv_len == 0)
12093 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012094 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012095 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012096 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012097 {
12098 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012099
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012100 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012101 if (pt == NULL)
12102 vim_free(name);
12103 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012104 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012105 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012106 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012107 listitem_T *li;
12108 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012109 int arg_len = 0;
12110 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012111
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012112 if (arg_pt != NULL)
12113 arg_len = arg_pt->pt_argc;
12114 if (list != NULL)
12115 lv_len = list->lv_len;
12116 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012117 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012118 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012119 if (pt->pt_argv == NULL)
12120 {
12121 vim_free(pt);
12122 vim_free(name);
12123 return;
12124 }
12125 else
12126 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012127 for (i = 0; i < arg_len; i++)
12128 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12129 if (lv_len > 0)
12130 for (li = list->lv_first; li != NULL;
12131 li = li->li_next)
12132 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012133 }
12134 }
12135
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012136 /* For "function(dict.func, [], dict)" and "func" is a partial
12137 * use "dict". That is backwards compatible. */
12138 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012139 {
12140 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12141 ++pt->pt_dict->dv_refcount;
12142 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012143 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012144 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012145 pt->pt_dict = arg_pt->pt_dict;
12146 if (pt->pt_dict != NULL)
12147 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012148 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012149
12150 pt->pt_refcount = 1;
12151 pt->pt_name = name;
12152 func_ref(pt->pt_name);
12153 }
12154 rettv->v_type = VAR_PARTIAL;
12155 rettv->vval.v_partial = pt;
12156 }
12157 else
12158 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012159 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012160 rettv->v_type = VAR_FUNC;
12161 rettv->vval.v_string = name;
12162 func_ref(name);
12163 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012164 }
12165}
12166
12167/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012168 * "garbagecollect()" function
12169 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012171f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012172{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012173 /* This is postponed until we are back at the toplevel, because we may be
12174 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12175 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012176
12177 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12178 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012179}
12180
12181/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012182 * "get()" function
12183 */
12184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012185f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012186{
Bram Moolenaar33570922005-01-25 22:26:29 +000012187 listitem_T *li;
12188 list_T *l;
12189 dictitem_T *di;
12190 dict_T *d;
12191 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012192
Bram Moolenaare9a41262005-01-15 22:18:47 +000012193 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012194 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012195 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012197 int error = FALSE;
12198
12199 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12200 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012201 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012202 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012204 else if (argvars[0].v_type == VAR_DICT)
12205 {
12206 if ((d = argvars[0].vval.v_dict) != NULL)
12207 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012208 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012209 if (di != NULL)
12210 tv = &di->di_tv;
12211 }
12212 }
12213 else
12214 EMSG2(_(e_listdictarg), "get()");
12215
12216 if (tv == NULL)
12217 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012218 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012219 copy_tv(&argvars[2], rettv);
12220 }
12221 else
12222 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012223}
12224
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012225static 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 +000012226
12227/*
12228 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012229 * Return a range (from start to end) of lines in rettv from the specified
12230 * buffer.
12231 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012232 */
12233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012234get_buffer_lines(
12235 buf_T *buf,
12236 linenr_T start,
12237 linenr_T end,
12238 int retlist,
12239 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012240{
12241 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012242
Bram Moolenaar959a1432013-12-14 12:17:38 +010012243 rettv->v_type = VAR_STRING;
12244 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012245 if (retlist && rettv_list_alloc(rettv) == FAIL)
12246 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012247
12248 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12249 return;
12250
12251 if (!retlist)
12252 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012253 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12254 p = ml_get_buf(buf, start, FALSE);
12255 else
12256 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012257 rettv->vval.v_string = vim_strsave(p);
12258 }
12259 else
12260 {
12261 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012262 return;
12263
12264 if (start < 1)
12265 start = 1;
12266 if (end > buf->b_ml.ml_line_count)
12267 end = buf->b_ml.ml_line_count;
12268 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012269 if (list_append_string(rettv->vval.v_list,
12270 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012271 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012272 }
12273}
12274
12275/*
12276 * "getbufline()" function
12277 */
12278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012279f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012280{
12281 linenr_T lnum;
12282 linenr_T end;
12283 buf_T *buf;
12284
12285 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12286 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012287 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012288 --emsg_off;
12289
Bram Moolenaar661b1822005-07-28 22:36:45 +000012290 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012291 if (argvars[2].v_type == VAR_UNKNOWN)
12292 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012293 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012294 end = get_tv_lnum_buf(&argvars[2], buf);
12295
Bram Moolenaar342337a2005-07-21 21:11:17 +000012296 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012297}
12298
Bram Moolenaar0d660222005-01-07 21:51:51 +000012299/*
12300 * "getbufvar()" function
12301 */
12302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012303f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012304{
12305 buf_T *buf;
12306 buf_T *save_curbuf;
12307 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012308 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012309 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012310
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012311 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12312 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012313 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012314 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012315
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012316 rettv->v_type = VAR_STRING;
12317 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012318
12319 if (buf != NULL && varname != NULL)
12320 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012321 /* set curbuf to be our buf, temporarily */
12322 save_curbuf = curbuf;
12323 curbuf = buf;
12324
Bram Moolenaar0d660222005-01-07 21:51:51 +000012325 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012326 {
12327 if (get_option_tv(&varname, rettv, TRUE) == OK)
12328 done = TRUE;
12329 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012330 else if (STRCMP(varname, "changedtick") == 0)
12331 {
12332 rettv->v_type = VAR_NUMBER;
12333 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012334 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012335 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012336 else
12337 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012338 /* Look up the variable. */
12339 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12340 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12341 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012342 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012343 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012344 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012345 done = TRUE;
12346 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012347 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012348
12349 /* restore previous notion of curbuf */
12350 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012351 }
12352
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012353 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12354 /* use the default value */
12355 copy_tv(&argvars[2], rettv);
12356
Bram Moolenaar0d660222005-01-07 21:51:51 +000012357 --emsg_off;
12358}
12359
12360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 * "getchar()" function
12362 */
12363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012364f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365{
12366 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012367 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012369 /* Position the cursor. Needed after a message that ends in a space. */
12370 windgoto(msg_row, msg_col);
12371
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 ++no_mapping;
12373 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012374 for (;;)
12375 {
12376 if (argvars[0].v_type == VAR_UNKNOWN)
12377 /* getchar(): blocking wait. */
12378 n = safe_vgetc();
12379 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12380 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012381 n = vpeekc_any();
12382 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012383 /* illegal argument or getchar(0) and no char avail: return zero */
12384 n = 0;
12385 else
12386 /* getchar(0) and char avail: return char */
12387 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012388
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012389 if (n == K_IGNORE)
12390 continue;
12391 break;
12392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393 --no_mapping;
12394 --allow_keys;
12395
Bram Moolenaar219b8702006-11-01 14:32:36 +000012396 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12397 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12398 vimvars[VV_MOUSE_COL].vv_nr = 0;
12399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012400 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 if (IS_SPECIAL(n) || mod_mask != 0)
12402 {
12403 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12404 int i = 0;
12405
12406 /* Turn a special key into three bytes, plus modifier. */
12407 if (mod_mask != 0)
12408 {
12409 temp[i++] = K_SPECIAL;
12410 temp[i++] = KS_MODIFIER;
12411 temp[i++] = mod_mask;
12412 }
12413 if (IS_SPECIAL(n))
12414 {
12415 temp[i++] = K_SPECIAL;
12416 temp[i++] = K_SECOND(n);
12417 temp[i++] = K_THIRD(n);
12418 }
12419#ifdef FEAT_MBYTE
12420 else if (has_mbyte)
12421 i += (*mb_char2bytes)(n, temp + i);
12422#endif
12423 else
12424 temp[i++] = n;
12425 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012426 rettv->v_type = VAR_STRING;
12427 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012428
12429#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012430 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012431 {
12432 int row = mouse_row;
12433 int col = mouse_col;
12434 win_T *win;
12435 linenr_T lnum;
12436# ifdef FEAT_WINDOWS
12437 win_T *wp;
12438# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012439 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012440
12441 if (row >= 0 && col >= 0)
12442 {
12443 /* Find the window at the mouse coordinates and compute the
12444 * text position. */
12445 win = mouse_find_win(&row, &col);
12446 (void)mouse_comp_pos(win, &row, &col, &lnum);
12447# ifdef FEAT_WINDOWS
12448 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012449 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012450# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012451 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012452 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12453 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12454 }
12455 }
12456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 }
12458}
12459
12460/*
12461 * "getcharmod()" function
12462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012464f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467}
12468
12469/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012470 * "getcharsearch()" function
12471 */
12472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012473f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012474{
12475 if (rettv_dict_alloc(rettv) != FAIL)
12476 {
12477 dict_T *dict = rettv->vval.v_dict;
12478
12479 dict_add_nr_str(dict, "char", 0L, last_csearch());
12480 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12481 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12482 }
12483}
12484
12485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 * "getcmdline()" function
12487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012489f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491 rettv->v_type = VAR_STRING;
12492 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493}
12494
12495/*
12496 * "getcmdpos()" function
12497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012499f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502}
12503
12504/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012505 * "getcmdtype()" function
12506 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012508f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012509{
12510 rettv->v_type = VAR_STRING;
12511 rettv->vval.v_string = alloc(2);
12512 if (rettv->vval.v_string != NULL)
12513 {
12514 rettv->vval.v_string[0] = get_cmdline_type();
12515 rettv->vval.v_string[1] = NUL;
12516 }
12517}
12518
12519/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012520 * "getcmdwintype()" function
12521 */
12522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012523f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012524{
12525 rettv->v_type = VAR_STRING;
12526 rettv->vval.v_string = NULL;
12527#ifdef FEAT_CMDWIN
12528 rettv->vval.v_string = alloc(2);
12529 if (rettv->vval.v_string != NULL)
12530 {
12531 rettv->vval.v_string[0] = cmdwin_type;
12532 rettv->vval.v_string[1] = NUL;
12533 }
12534#endif
12535}
12536
12537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538 * "getcwd()" function
12539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012541f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012542{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012543 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012544 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012546 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012547 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012548
12549 wp = find_tabwin(&argvars[0], &argvars[1]);
12550 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012552 if (wp->w_localdir != NULL)
12553 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12554 else if(globaldir != NULL)
12555 rettv->vval.v_string = vim_strsave(globaldir);
12556 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012557 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012558 cwd = alloc(MAXPATHL);
12559 if (cwd != NULL)
12560 {
12561 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12562 rettv->vval.v_string = vim_strsave(cwd);
12563 vim_free(cwd);
12564 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012565 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012566#ifdef BACKSLASH_IN_FILENAME
12567 if (rettv->vval.v_string != NULL)
12568 slash_adjust(rettv->vval.v_string);
12569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 }
12571}
12572
12573/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012574 * "getfontname()" function
12575 */
12576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012577f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012578{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012579 rettv->v_type = VAR_STRING;
12580 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012581#ifdef FEAT_GUI
12582 if (gui.in_use)
12583 {
12584 GuiFont font;
12585 char_u *name = NULL;
12586
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012587 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012588 {
12589 /* Get the "Normal" font. Either the name saved by
12590 * hl_set_font_name() or from the font ID. */
12591 font = gui.norm_font;
12592 name = hl_get_font_name();
12593 }
12594 else
12595 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012597 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12598 return;
12599 font = gui_mch_get_font(name, FALSE);
12600 if (font == NOFONT)
12601 return; /* Invalid font name, return empty string. */
12602 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012603 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012604 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012605 gui_mch_free_font(font);
12606 }
12607#endif
12608}
12609
12610/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012611 * "getfperm({fname})" function
12612 */
12613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012614f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012615{
12616 char_u *fname;
12617 struct stat st;
12618 char_u *perm = NULL;
12619 char_u flags[] = "rwx";
12620 int i;
12621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012622 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012624 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012625 if (mch_stat((char *)fname, &st) >= 0)
12626 {
12627 perm = vim_strsave((char_u *)"---------");
12628 if (perm != NULL)
12629 {
12630 for (i = 0; i < 9; i++)
12631 {
12632 if (st.st_mode & (1 << (8 - i)))
12633 perm[i] = flags[i % 3];
12634 }
12635 }
12636 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012638}
12639
12640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641 * "getfsize({fname})" function
12642 */
12643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012644f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645{
12646 char_u *fname;
12647 struct stat st;
12648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652
12653 if (mch_stat((char *)fname, &st) >= 0)
12654 {
12655 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012656 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012660
12661 /* non-perfect check for overflow */
12662 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12663 rettv->vval.v_number = -2;
12664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012665 }
12666 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668}
12669
12670/*
12671 * "getftime({fname})" function
12672 */
12673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012674f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675{
12676 char_u *fname;
12677 struct stat st;
12678
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680
12681 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685}
12686
12687/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012688 * "getftype({fname})" function
12689 */
12690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012691f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012692{
12693 char_u *fname;
12694 struct stat st;
12695 char_u *type = NULL;
12696 char *t;
12697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012698 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012701 if (mch_lstat((char *)fname, &st) >= 0)
12702 {
12703#ifdef S_ISREG
12704 if (S_ISREG(st.st_mode))
12705 t = "file";
12706 else if (S_ISDIR(st.st_mode))
12707 t = "dir";
12708# ifdef S_ISLNK
12709 else if (S_ISLNK(st.st_mode))
12710 t = "link";
12711# endif
12712# ifdef S_ISBLK
12713 else if (S_ISBLK(st.st_mode))
12714 t = "bdev";
12715# endif
12716# ifdef S_ISCHR
12717 else if (S_ISCHR(st.st_mode))
12718 t = "cdev";
12719# endif
12720# ifdef S_ISFIFO
12721 else if (S_ISFIFO(st.st_mode))
12722 t = "fifo";
12723# endif
12724# ifdef S_ISSOCK
12725 else if (S_ISSOCK(st.st_mode))
12726 t = "fifo";
12727# endif
12728 else
12729 t = "other";
12730#else
12731# ifdef S_IFMT
12732 switch (st.st_mode & S_IFMT)
12733 {
12734 case S_IFREG: t = "file"; break;
12735 case S_IFDIR: t = "dir"; break;
12736# ifdef S_IFLNK
12737 case S_IFLNK: t = "link"; break;
12738# endif
12739# ifdef S_IFBLK
12740 case S_IFBLK: t = "bdev"; break;
12741# endif
12742# ifdef S_IFCHR
12743 case S_IFCHR: t = "cdev"; break;
12744# endif
12745# ifdef S_IFIFO
12746 case S_IFIFO: t = "fifo"; break;
12747# endif
12748# ifdef S_IFSOCK
12749 case S_IFSOCK: t = "socket"; break;
12750# endif
12751 default: t = "other";
12752 }
12753# else
12754 if (mch_isdir(fname))
12755 t = "dir";
12756 else
12757 t = "file";
12758# endif
12759#endif
12760 type = vim_strsave((char_u *)t);
12761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012763}
12764
12765/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012766 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012767 */
12768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012769f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012770{
12771 linenr_T lnum;
12772 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012773 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012774
12775 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012776 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012777 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012778 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012779 retlist = FALSE;
12780 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012781 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012782 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012783 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012784 retlist = TRUE;
12785 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012786
Bram Moolenaar342337a2005-07-21 21:11:17 +000012787 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012788}
12789
12790/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012791 * "getmatches()" function
12792 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012794f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012795{
12796#ifdef FEAT_SEARCH_EXTRA
12797 dict_T *dict;
12798 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012799 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012800
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012801 if (rettv_list_alloc(rettv) == OK)
12802 {
12803 while (cur != NULL)
12804 {
12805 dict = dict_alloc();
12806 if (dict == NULL)
12807 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012808 if (cur->match.regprog == NULL)
12809 {
12810 /* match added with matchaddpos() */
12811 for (i = 0; i < MAXPOSMATCH; ++i)
12812 {
12813 llpos_T *llpos;
12814 char buf[6];
12815 list_T *l;
12816
12817 llpos = &cur->pos.pos[i];
12818 if (llpos->lnum == 0)
12819 break;
12820 l = list_alloc();
12821 if (l == NULL)
12822 break;
12823 list_append_number(l, (varnumber_T)llpos->lnum);
12824 if (llpos->col > 0)
12825 {
12826 list_append_number(l, (varnumber_T)llpos->col);
12827 list_append_number(l, (varnumber_T)llpos->len);
12828 }
12829 sprintf(buf, "pos%d", i + 1);
12830 dict_add_list(dict, buf, l);
12831 }
12832 }
12833 else
12834 {
12835 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12836 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012837 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012838 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12839 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020012840# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020012841 if (cur->conceal_char)
12842 {
12843 char_u buf[MB_MAXBYTES + 1];
12844
12845 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12846 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12847 }
12848# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012849 list_append_dict(rettv->vval.v_list, dict);
12850 cur = cur->next;
12851 }
12852 }
12853#endif
12854}
12855
12856/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012857 * "getpid()" function
12858 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012860f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012861{
12862 rettv->vval.v_number = mch_get_pid();
12863}
12864
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012865static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012866
12867/*
12868 * "getcurpos()" function
12869 */
12870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012871f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012872{
12873 getpos_both(argvars, rettv, TRUE);
12874}
12875
Bram Moolenaar18081e32008-02-20 19:11:07 +000012876/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012877 * "getpos(string)" function
12878 */
12879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012880f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012881{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012882 getpos_both(argvars, rettv, FALSE);
12883}
12884
12885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012886getpos_both(
12887 typval_T *argvars,
12888 typval_T *rettv,
12889 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012890{
Bram Moolenaara5525202006-03-02 22:52:09 +000012891 pos_T *fp;
12892 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012893 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012894
12895 if (rettv_list_alloc(rettv) == OK)
12896 {
12897 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012898 if (getcurpos)
12899 fp = &curwin->w_cursor;
12900 else
12901 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012902 if (fnum != -1)
12903 list_append_number(l, (varnumber_T)fnum);
12904 else
12905 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012906 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12907 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012908 list_append_number(l, (fp != NULL)
12909 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012910 : (varnumber_T)0);
12911 list_append_number(l,
12912#ifdef FEAT_VIRTUALEDIT
12913 (fp != NULL) ? (varnumber_T)fp->coladd :
12914#endif
12915 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012916 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012917 {
12918 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012919 list_append_number(l, curwin->w_curswant == MAXCOL ?
12920 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012921 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012922 }
12923 else
12924 rettv->vval.v_number = FALSE;
12925}
12926
12927/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012928 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012929 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012931f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012932{
12933#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012934 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012935#endif
12936
Bram Moolenaar2641f772005-03-25 21:58:17 +000012937#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012938 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012939 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012940 wp = NULL;
12941 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12942 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012943 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012944 if (wp == NULL)
12945 return;
12946 }
12947
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012948 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012949 }
12950#endif
12951}
12952
12953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012954 * "getreg()" function
12955 */
12956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012957f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958{
12959 char_u *strregname;
12960 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012961 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012962 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012963 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012965 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012966 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012967 strregname = get_tv_string_chk(&argvars[0]);
12968 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012969 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012970 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012971 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012972 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12973 return_list = get_tv_number_chk(&argvars[2], &error);
12974 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012977 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012978
12979 if (error)
12980 return;
12981
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 regname = (strregname == NULL ? '"' : *strregname);
12983 if (regname == 0)
12984 regname = '"';
12985
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012986 if (return_list)
12987 {
12988 rettv->v_type = VAR_LIST;
12989 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12990 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012991 if (rettv->vval.v_list != NULL)
12992 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012993 }
12994 else
12995 {
12996 rettv->v_type = VAR_STRING;
12997 rettv->vval.v_string = get_reg_contents(regname,
12998 arg2 ? GREG_EXPR_SRC : 0);
12999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000}
13001
13002/*
13003 * "getregtype()" function
13004 */
13005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013006f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007{
13008 char_u *strregname;
13009 int regname;
13010 char_u buf[NUMBUFLEN + 2];
13011 long reglen = 0;
13012
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013013 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013014 {
13015 strregname = get_tv_string_chk(&argvars[0]);
13016 if (strregname == NULL) /* type error; errmsg already given */
13017 {
13018 rettv->v_type = VAR_STRING;
13019 rettv->vval.v_string = NULL;
13020 return;
13021 }
13022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 else
13024 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013025 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026
13027 regname = (strregname == NULL ? '"' : *strregname);
13028 if (regname == 0)
13029 regname = '"';
13030
13031 buf[0] = NUL;
13032 buf[1] = NUL;
13033 switch (get_reg_type(regname, &reglen))
13034 {
13035 case MLINE: buf[0] = 'V'; break;
13036 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 case MBLOCK:
13038 buf[0] = Ctrl_V;
13039 sprintf((char *)buf + 1, "%ld", reglen + 1);
13040 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013042 rettv->v_type = VAR_STRING;
13043 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044}
13045
13046/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013047 * "gettabvar()" function
13048 */
13049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013050f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013051{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013052 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013053 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013054 dictitem_T *v;
13055 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013056 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013057
13058 rettv->v_type = VAR_STRING;
13059 rettv->vval.v_string = NULL;
13060
13061 varname = get_tv_string_chk(&argvars[1]);
13062 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13063 if (tp != NULL && varname != NULL)
13064 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013065 /* Set tp to be our tabpage, temporarily. Also set the window to the
13066 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013067 if (switch_win(&oldcurwin, &oldtabpage,
13068 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013069 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013070 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013071 /* look up the variable */
13072 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13073 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13074 if (v != NULL)
13075 {
13076 copy_tv(&v->di_tv, rettv);
13077 done = TRUE;
13078 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013079 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013080
13081 /* restore previous notion of curwin */
13082 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013083 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013084
13085 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013086 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013087}
13088
13089/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013090 * "gettabwinvar()" function
13091 */
13092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013093f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013094{
13095 getwinvar(argvars, rettv, 1);
13096}
13097
13098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 * "getwinposx()" function
13100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013102f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105#ifdef FEAT_GUI
13106 if (gui.in_use)
13107 {
13108 int x, y;
13109
13110 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112 }
13113#endif
13114}
13115
13116/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013117 * "win_findbuf()" function
13118 */
13119 static void
13120f_win_findbuf(typval_T *argvars, typval_T *rettv)
13121{
13122 if (rettv_list_alloc(rettv) != FAIL)
13123 win_findbuf(argvars, rettv->vval.v_list);
13124}
13125
13126/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013127 * "win_getid()" function
13128 */
13129 static void
13130f_win_getid(typval_T *argvars, typval_T *rettv)
13131{
13132 rettv->vval.v_number = win_getid(argvars);
13133}
13134
13135/*
13136 * "win_gotoid()" function
13137 */
13138 static void
13139f_win_gotoid(typval_T *argvars, typval_T *rettv)
13140{
13141 rettv->vval.v_number = win_gotoid(argvars);
13142}
13143
13144/*
13145 * "win_id2tabwin()" function
13146 */
13147 static void
13148f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13149{
13150 if (rettv_list_alloc(rettv) != FAIL)
13151 win_id2tabwin(argvars, rettv->vval.v_list);
13152}
13153
13154/*
13155 * "win_id2win()" function
13156 */
13157 static void
13158f_win_id2win(typval_T *argvars, typval_T *rettv)
13159{
13160 rettv->vval.v_number = win_id2win(argvars);
13161}
13162
13163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 * "getwinposy()" function
13165 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013167f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170#ifdef FEAT_GUI
13171 if (gui.in_use)
13172 {
13173 int x, y;
13174
13175 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177 }
13178#endif
13179}
13180
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013181/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013182 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013183 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013184 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013185find_win_by_nr(
13186 typval_T *vp,
13187 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013188{
13189#ifdef FEAT_WINDOWS
13190 win_T *wp;
13191#endif
13192 int nr;
13193
13194 nr = get_tv_number_chk(vp, NULL);
13195
13196#ifdef FEAT_WINDOWS
13197 if (nr < 0)
13198 return NULL;
13199 if (nr == 0)
13200 return curwin;
13201
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013202 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13203 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013204 if (--nr <= 0)
13205 break;
13206 return wp;
13207#else
13208 if (nr == 0 || nr == 1)
13209 return curwin;
13210 return NULL;
13211#endif
13212}
13213
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013215 * Find window specified by "wvp" in tabpage "tvp".
13216 */
13217 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013218find_tabwin(
13219 typval_T *wvp, /* VAR_UNKNOWN for current window */
13220 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013221{
13222 win_T *wp = NULL;
13223 tabpage_T *tp = NULL;
13224 long n;
13225
13226 if (wvp->v_type != VAR_UNKNOWN)
13227 {
13228 if (tvp->v_type != VAR_UNKNOWN)
13229 {
13230 n = get_tv_number(tvp);
13231 if (n >= 0)
13232 tp = find_tabpage(n);
13233 }
13234 else
13235 tp = curtab;
13236
13237 if (tp != NULL)
13238 wp = find_win_by_nr(wvp, tp);
13239 }
13240 else
13241 wp = curwin;
13242
13243 return wp;
13244}
13245
13246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247 * "getwinvar()" function
13248 */
13249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013250f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013252 getwinvar(argvars, rettv, 0);
13253}
13254
13255/*
13256 * getwinvar() and gettabwinvar()
13257 */
13258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013259getwinvar(
13260 typval_T *argvars,
13261 typval_T *rettv,
13262 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013263{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013264 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013266 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013267 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013268 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013269#ifdef FEAT_WINDOWS
13270 win_T *oldcurwin;
13271 tabpage_T *oldtabpage;
13272 int need_switch_win;
13273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013275#ifdef FEAT_WINDOWS
13276 if (off == 1)
13277 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13278 else
13279 tp = curtab;
13280#endif
13281 win = find_win_by_nr(&argvars[off], tp);
13282 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013283 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013285 rettv->v_type = VAR_STRING;
13286 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287
13288 if (win != NULL && varname != NULL)
13289 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013290#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013291 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013292 * otherwise the window is not valid. Only do this when needed,
13293 * autocommands get blocked. */
13294 need_switch_win = !(tp == curtab && win == curwin);
13295 if (!need_switch_win
13296 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13297#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013298 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013299 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013300 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013301 if (get_option_tv(&varname, rettv, 1) == OK)
13302 done = TRUE;
13303 }
13304 else
13305 {
13306 /* Look up the variable. */
13307 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13308 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13309 varname, FALSE);
13310 if (v != NULL)
13311 {
13312 copy_tv(&v->di_tv, rettv);
13313 done = TRUE;
13314 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013317
Bram Moolenaarba117c22015-09-29 16:53:22 +020013318#ifdef FEAT_WINDOWS
13319 if (need_switch_win)
13320 /* restore previous notion of curwin */
13321 restore_win(oldcurwin, oldtabpage, TRUE);
13322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 }
13324
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013325 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13326 /* use the default return value */
13327 copy_tv(&argvars[off + 2], rettv);
13328
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 --emsg_off;
13330}
13331
13332/*
13333 * "glob()" function
13334 */
13335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013336f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013338 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013340 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013342 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013343 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013345 if (argvars[1].v_type != VAR_UNKNOWN)
13346 {
13347 if (get_tv_number_chk(&argvars[1], &error))
13348 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013349 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013350 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013351 if (get_tv_number_chk(&argvars[2], &error))
13352 {
13353 rettv->v_type = VAR_LIST;
13354 rettv->vval.v_list = NULL;
13355 }
13356 if (argvars[3].v_type != VAR_UNKNOWN
13357 && get_tv_number_chk(&argvars[3], &error))
13358 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013359 }
13360 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013361 if (!error)
13362 {
13363 ExpandInit(&xpc);
13364 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013365 if (p_wic)
13366 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013367 if (rettv->v_type == VAR_STRING)
13368 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013369 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013370 else if (rettv_list_alloc(rettv) != FAIL)
13371 {
13372 int i;
13373
13374 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13375 NULL, options, WILD_ALL_KEEP);
13376 for (i = 0; i < xpc.xp_numfiles; i++)
13377 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13378
13379 ExpandCleanup(&xpc);
13380 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013381 }
13382 else
13383 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384}
13385
13386/*
13387 * "globpath()" function
13388 */
13389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013390f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013392 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013394 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013395 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013396 garray_T ga;
13397 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013399 /* When the optional second argument is non-zero, don't remove matches
13400 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013401 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013402 if (argvars[2].v_type != VAR_UNKNOWN)
13403 {
13404 if (get_tv_number_chk(&argvars[2], &error))
13405 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013406 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013407 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013408 if (get_tv_number_chk(&argvars[3], &error))
13409 {
13410 rettv->v_type = VAR_LIST;
13411 rettv->vval.v_list = NULL;
13412 }
13413 if (argvars[4].v_type != VAR_UNKNOWN
13414 && get_tv_number_chk(&argvars[4], &error))
13415 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013416 }
13417 }
13418 if (file != NULL && !error)
13419 {
13420 ga_init2(&ga, (int)sizeof(char_u *), 10);
13421 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13422 if (rettv->v_type == VAR_STRING)
13423 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13424 else if (rettv_list_alloc(rettv) != FAIL)
13425 for (i = 0; i < ga.ga_len; ++i)
13426 list_append_string(rettv->vval.v_list,
13427 ((char_u **)(ga.ga_data))[i], -1);
13428 ga_clear_strings(&ga);
13429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013430 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432}
13433
13434/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013435 * "glob2regpat()" function
13436 */
13437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013438f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013439{
13440 char_u *pat = get_tv_string_chk(&argvars[0]);
13441
13442 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013443 rettv->vval.v_string = (pat == NULL)
13444 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013445}
13446
13447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 * "has()" function
13449 */
13450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013451f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452{
13453 int i;
13454 char_u *name;
13455 int n = FALSE;
13456 static char *(has_list[]) =
13457 {
13458#ifdef AMIGA
13459 "amiga",
13460# ifdef FEAT_ARP
13461 "arp",
13462# endif
13463#endif
13464#ifdef __BEOS__
13465 "beos",
13466#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013467#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 "mac",
13469#endif
13470#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013471 "macunix", /* built with 'darwin' enabled */
13472#endif
13473#if defined(__APPLE__) && __APPLE__ == 1
13474 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476#ifdef __QNX__
13477 "qnx",
13478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479#ifdef UNIX
13480 "unix",
13481#endif
13482#ifdef VMS
13483 "vms",
13484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485#ifdef WIN32
13486 "win32",
13487#endif
13488#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13489 "win32unix",
13490#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013491#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 "win64",
13493#endif
13494#ifdef EBCDIC
13495 "ebcdic",
13496#endif
13497#ifndef CASE_INSENSITIVE_FILENAME
13498 "fname_case",
13499#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013500#ifdef HAVE_ACL
13501 "acl",
13502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503#ifdef FEAT_ARABIC
13504 "arabic",
13505#endif
13506#ifdef FEAT_AUTOCMD
13507 "autocmd",
13508#endif
13509#ifdef FEAT_BEVAL
13510 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013511# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13512 "balloon_multiline",
13513# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514#endif
13515#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13516 "builtin_terms",
13517# ifdef ALL_BUILTIN_TCAPS
13518 "all_builtin_terms",
13519# endif
13520#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013521#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13522 || defined(FEAT_GUI_W32) \
13523 || defined(FEAT_GUI_MOTIF))
13524 "browsefilter",
13525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526#ifdef FEAT_BYTEOFF
13527 "byte_offset",
13528#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013529#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013530 "channel",
13531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532#ifdef FEAT_CINDENT
13533 "cindent",
13534#endif
13535#ifdef FEAT_CLIENTSERVER
13536 "clientserver",
13537#endif
13538#ifdef FEAT_CLIPBOARD
13539 "clipboard",
13540#endif
13541#ifdef FEAT_CMDL_COMPL
13542 "cmdline_compl",
13543#endif
13544#ifdef FEAT_CMDHIST
13545 "cmdline_hist",
13546#endif
13547#ifdef FEAT_COMMENTS
13548 "comments",
13549#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013550#ifdef FEAT_CONCEAL
13551 "conceal",
13552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553#ifdef FEAT_CRYPT
13554 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013555 "crypt-blowfish",
13556 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557#endif
13558#ifdef FEAT_CSCOPE
13559 "cscope",
13560#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013561#ifdef FEAT_CURSORBIND
13562 "cursorbind",
13563#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013564#ifdef CURSOR_SHAPE
13565 "cursorshape",
13566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567#ifdef DEBUG
13568 "debug",
13569#endif
13570#ifdef FEAT_CON_DIALOG
13571 "dialog_con",
13572#endif
13573#ifdef FEAT_GUI_DIALOG
13574 "dialog_gui",
13575#endif
13576#ifdef FEAT_DIFF
13577 "diff",
13578#endif
13579#ifdef FEAT_DIGRAPHS
13580 "digraphs",
13581#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013582#ifdef FEAT_DIRECTX
13583 "directx",
13584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585#ifdef FEAT_DND
13586 "dnd",
13587#endif
13588#ifdef FEAT_EMACS_TAGS
13589 "emacs_tags",
13590#endif
13591 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013592 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593#ifdef FEAT_SEARCH_EXTRA
13594 "extra_search",
13595#endif
13596#ifdef FEAT_FKMAP
13597 "farsi",
13598#endif
13599#ifdef FEAT_SEARCHPATH
13600 "file_in_path",
13601#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013602#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013603 "filterpipe",
13604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605#ifdef FEAT_FIND_ID
13606 "find_in_path",
13607#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013608#ifdef FEAT_FLOAT
13609 "float",
13610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611#ifdef FEAT_FOLDING
13612 "folding",
13613#endif
13614#ifdef FEAT_FOOTER
13615 "footer",
13616#endif
13617#if !defined(USE_SYSTEM) && defined(UNIX)
13618 "fork",
13619#endif
13620#ifdef FEAT_GETTEXT
13621 "gettext",
13622#endif
13623#ifdef FEAT_GUI
13624 "gui",
13625#endif
13626#ifdef FEAT_GUI_ATHENA
13627# ifdef FEAT_GUI_NEXTAW
13628 "gui_neXtaw",
13629# else
13630 "gui_athena",
13631# endif
13632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633#ifdef FEAT_GUI_GTK
13634 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013635# ifdef USE_GTK3
13636 "gui_gtk3",
13637# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013641#ifdef FEAT_GUI_GNOME
13642 "gui_gnome",
13643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644#ifdef FEAT_GUI_MAC
13645 "gui_mac",
13646#endif
13647#ifdef FEAT_GUI_MOTIF
13648 "gui_motif",
13649#endif
13650#ifdef FEAT_GUI_PHOTON
13651 "gui_photon",
13652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653#ifdef FEAT_GUI_W32
13654 "gui_win32",
13655#endif
13656#ifdef FEAT_HANGULIN
13657 "hangul_input",
13658#endif
13659#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13660 "iconv",
13661#endif
13662#ifdef FEAT_INS_EXPAND
13663 "insert_expand",
13664#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013665#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013666 "job",
13667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668#ifdef FEAT_JUMPLIST
13669 "jumplist",
13670#endif
13671#ifdef FEAT_KEYMAP
13672 "keymap",
13673#endif
13674#ifdef FEAT_LANGMAP
13675 "langmap",
13676#endif
13677#ifdef FEAT_LIBCALL
13678 "libcall",
13679#endif
13680#ifdef FEAT_LINEBREAK
13681 "linebreak",
13682#endif
13683#ifdef FEAT_LISP
13684 "lispindent",
13685#endif
13686#ifdef FEAT_LISTCMDS
13687 "listcmds",
13688#endif
13689#ifdef FEAT_LOCALMAP
13690 "localmap",
13691#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013692#ifdef FEAT_LUA
13693# ifndef DYNAMIC_LUA
13694 "lua",
13695# endif
13696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697#ifdef FEAT_MENU
13698 "menu",
13699#endif
13700#ifdef FEAT_SESSION
13701 "mksession",
13702#endif
13703#ifdef FEAT_MODIFY_FNAME
13704 "modify_fname",
13705#endif
13706#ifdef FEAT_MOUSE
13707 "mouse",
13708#endif
13709#ifdef FEAT_MOUSESHAPE
13710 "mouseshape",
13711#endif
13712#if defined(UNIX) || defined(VMS)
13713# ifdef FEAT_MOUSE_DEC
13714 "mouse_dec",
13715# endif
13716# ifdef FEAT_MOUSE_GPM
13717 "mouse_gpm",
13718# endif
13719# ifdef FEAT_MOUSE_JSB
13720 "mouse_jsbterm",
13721# endif
13722# ifdef FEAT_MOUSE_NET
13723 "mouse_netterm",
13724# endif
13725# ifdef FEAT_MOUSE_PTERM
13726 "mouse_pterm",
13727# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013728# ifdef FEAT_MOUSE_SGR
13729 "mouse_sgr",
13730# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013731# ifdef FEAT_SYSMOUSE
13732 "mouse_sysmouse",
13733# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013734# ifdef FEAT_MOUSE_URXVT
13735 "mouse_urxvt",
13736# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737# ifdef FEAT_MOUSE_XTERM
13738 "mouse_xterm",
13739# endif
13740#endif
13741#ifdef FEAT_MBYTE
13742 "multi_byte",
13743#endif
13744#ifdef FEAT_MBYTE_IME
13745 "multi_byte_ime",
13746#endif
13747#ifdef FEAT_MULTI_LANG
13748 "multi_lang",
13749#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013750#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013751#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013752 "mzscheme",
13753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#ifdef FEAT_OLE
13756 "ole",
13757#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013758 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759#ifdef FEAT_PATH_EXTRA
13760 "path_extra",
13761#endif
13762#ifdef FEAT_PERL
13763#ifndef DYNAMIC_PERL
13764 "perl",
13765#endif
13766#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013767#ifdef FEAT_PERSISTENT_UNDO
13768 "persistent_undo",
13769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770#ifdef FEAT_PYTHON
13771#ifndef DYNAMIC_PYTHON
13772 "python",
13773#endif
13774#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013775#ifdef FEAT_PYTHON3
13776#ifndef DYNAMIC_PYTHON3
13777 "python3",
13778#endif
13779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780#ifdef FEAT_POSTSCRIPT
13781 "postscript",
13782#endif
13783#ifdef FEAT_PRINTER
13784 "printer",
13785#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013786#ifdef FEAT_PROFILE
13787 "profile",
13788#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013789#ifdef FEAT_RELTIME
13790 "reltime",
13791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792#ifdef FEAT_QUICKFIX
13793 "quickfix",
13794#endif
13795#ifdef FEAT_RIGHTLEFT
13796 "rightleft",
13797#endif
13798#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13799 "ruby",
13800#endif
13801#ifdef FEAT_SCROLLBIND
13802 "scrollbind",
13803#endif
13804#ifdef FEAT_CMDL_INFO
13805 "showcmd",
13806 "cmdline_info",
13807#endif
13808#ifdef FEAT_SIGNS
13809 "signs",
13810#endif
13811#ifdef FEAT_SMARTINDENT
13812 "smartindent",
13813#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013814#ifdef STARTUPTIME
13815 "startuptime",
13816#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817#ifdef FEAT_STL_OPT
13818 "statusline",
13819#endif
13820#ifdef FEAT_SUN_WORKSHOP
13821 "sun_workshop",
13822#endif
13823#ifdef FEAT_NETBEANS_INTG
13824 "netbeans_intg",
13825#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013826#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013827 "spell",
13828#endif
13829#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 "syntax",
13831#endif
13832#if defined(USE_SYSTEM) || !defined(UNIX)
13833 "system",
13834#endif
13835#ifdef FEAT_TAG_BINS
13836 "tag_binary",
13837#endif
13838#ifdef FEAT_TAG_OLDSTATIC
13839 "tag_old_static",
13840#endif
13841#ifdef FEAT_TAG_ANYWHITE
13842 "tag_any_white",
13843#endif
13844#ifdef FEAT_TCL
13845# ifndef DYNAMIC_TCL
13846 "tcl",
13847# endif
13848#endif
13849#ifdef TERMINFO
13850 "terminfo",
13851#endif
13852#ifdef FEAT_TERMRESPONSE
13853 "termresponse",
13854#endif
13855#ifdef FEAT_TEXTOBJ
13856 "textobjects",
13857#endif
13858#ifdef HAVE_TGETENT
13859 "tgetent",
13860#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013861#ifdef FEAT_TIMERS
13862 "timers",
13863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864#ifdef FEAT_TITLE
13865 "title",
13866#endif
13867#ifdef FEAT_TOOLBAR
13868 "toolbar",
13869#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013870#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13871 "unnamedplus",
13872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873#ifdef FEAT_USR_CMDS
13874 "user-commands", /* was accidentally included in 5.4 */
13875 "user_commands",
13876#endif
13877#ifdef FEAT_VIMINFO
13878 "viminfo",
13879#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013880#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 "vertsplit",
13882#endif
13883#ifdef FEAT_VIRTUALEDIT
13884 "virtualedit",
13885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887#ifdef FEAT_VISUALEXTRA
13888 "visualextra",
13889#endif
13890#ifdef FEAT_VREPLACE
13891 "vreplace",
13892#endif
13893#ifdef FEAT_WILDIGN
13894 "wildignore",
13895#endif
13896#ifdef FEAT_WILDMENU
13897 "wildmenu",
13898#endif
13899#ifdef FEAT_WINDOWS
13900 "windows",
13901#endif
13902#ifdef FEAT_WAK
13903 "winaltkeys",
13904#endif
13905#ifdef FEAT_WRITEBACKUP
13906 "writebackup",
13907#endif
13908#ifdef FEAT_XIM
13909 "xim",
13910#endif
13911#ifdef FEAT_XFONTSET
13912 "xfontset",
13913#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013914#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013915 "xpm",
13916 "xpm_w32", /* for backward compatibility */
13917#else
13918# if defined(HAVE_XPM)
13919 "xpm",
13920# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922#ifdef USE_XSMP
13923 "xsmp",
13924#endif
13925#ifdef USE_XSMP_INTERACT
13926 "xsmp_interact",
13927#endif
13928#ifdef FEAT_XCLIPBOARD
13929 "xterm_clipboard",
13930#endif
13931#ifdef FEAT_XTERM_SAVE
13932 "xterm_save",
13933#endif
13934#if defined(UNIX) && defined(FEAT_X11)
13935 "X11",
13936#endif
13937 NULL
13938 };
13939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 for (i = 0; has_list[i] != NULL; ++i)
13942 if (STRICMP(name, has_list[i]) == 0)
13943 {
13944 n = TRUE;
13945 break;
13946 }
13947
13948 if (n == FALSE)
13949 {
13950 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013951 {
13952 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010013953 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013954 && vim_isdigit(name[6])
13955 && vim_isdigit(name[8])
13956 && vim_isdigit(name[10]))
13957 {
13958 int major = atoi((char *)name + 6);
13959 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013960
13961 /* Expect "patch-9.9.01234". */
13962 n = (major < VIM_VERSION_MAJOR
13963 || (major == VIM_VERSION_MAJOR
13964 && (minor < VIM_VERSION_MINOR
13965 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013966 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013967 }
13968 else
13969 n = has_patch(atoi((char *)name + 5));
13970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 else if (STRICMP(name, "vim_starting") == 0)
13972 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013973#ifdef FEAT_MBYTE
13974 else if (STRICMP(name, "multi_byte_encoding") == 0)
13975 n = has_mbyte;
13976#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013977#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13978 else if (STRICMP(name, "balloon_multiline") == 0)
13979 n = multiline_balloon_available();
13980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981#ifdef DYNAMIC_TCL
13982 else if (STRICMP(name, "tcl") == 0)
13983 n = tcl_enabled(FALSE);
13984#endif
13985#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13986 else if (STRICMP(name, "iconv") == 0)
13987 n = iconv_enabled(FALSE);
13988#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013989#ifdef DYNAMIC_LUA
13990 else if (STRICMP(name, "lua") == 0)
13991 n = lua_enabled(FALSE);
13992#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013993#ifdef DYNAMIC_MZSCHEME
13994 else if (STRICMP(name, "mzscheme") == 0)
13995 n = mzscheme_enabled(FALSE);
13996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997#ifdef DYNAMIC_RUBY
13998 else if (STRICMP(name, "ruby") == 0)
13999 n = ruby_enabled(FALSE);
14000#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014001#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002#ifdef DYNAMIC_PYTHON
14003 else if (STRICMP(name, "python") == 0)
14004 n = python_enabled(FALSE);
14005#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014006#endif
14007#ifdef FEAT_PYTHON3
14008#ifdef DYNAMIC_PYTHON3
14009 else if (STRICMP(name, "python3") == 0)
14010 n = python3_enabled(FALSE);
14011#endif
14012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013#ifdef DYNAMIC_PERL
14014 else if (STRICMP(name, "perl") == 0)
14015 n = perl_enabled(FALSE);
14016#endif
14017#ifdef FEAT_GUI
14018 else if (STRICMP(name, "gui_running") == 0)
14019 n = (gui.in_use || gui.starting);
14020# ifdef FEAT_GUI_W32
14021 else if (STRICMP(name, "gui_win32s") == 0)
14022 n = gui_is_win32s();
14023# endif
14024# ifdef FEAT_BROWSE
14025 else if (STRICMP(name, "browse") == 0)
14026 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14027# endif
14028#endif
14029#ifdef FEAT_SYN_HL
14030 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014031 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032#endif
14033#if defined(WIN3264)
14034 else if (STRICMP(name, "win95") == 0)
14035 n = mch_windows95();
14036#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014037#ifdef FEAT_NETBEANS_INTG
14038 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014039 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 }
14042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044}
14045
14046/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014047 * "has_key()" function
14048 */
14049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014050f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014051{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014052 if (argvars[0].v_type != VAR_DICT)
14053 {
14054 EMSG(_(e_dictreq));
14055 return;
14056 }
14057 if (argvars[0].vval.v_dict == NULL)
14058 return;
14059
14060 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014061 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014062}
14063
14064/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014065 * "haslocaldir()" function
14066 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014068f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014069{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014070 win_T *wp = NULL;
14071
14072 wp = find_tabwin(&argvars[0], &argvars[1]);
14073 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014074}
14075
14076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077 * "hasmapto()" function
14078 */
14079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014080f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081{
14082 char_u *name;
14083 char_u *mode;
14084 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014085 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014086
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014088 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 mode = (char_u *)"nvo";
14090 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014093 if (argvars[2].v_type != VAR_UNKNOWN)
14094 abbr = get_tv_number(&argvars[2]);
14095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096
Bram Moolenaar2c932302006-03-18 21:42:09 +000014097 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014098 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014100 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101}
14102
14103/*
14104 * "histadd()" function
14105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014107f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014108{
14109#ifdef FEAT_CMDHIST
14110 int histype;
14111 char_u *str;
14112 char_u buf[NUMBUFLEN];
14113#endif
14114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014115 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 if (check_restricted() || check_secure())
14117 return;
14118#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014119 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14120 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 if (histype >= 0)
14122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 if (*str != NUL)
14125 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014126 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014128 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129 return;
14130 }
14131 }
14132#endif
14133}
14134
14135/*
14136 * "histdel()" function
14137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014139f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014140{
14141#ifdef FEAT_CMDHIST
14142 int n;
14143 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014146 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14147 if (str == NULL)
14148 n = 0;
14149 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014152 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014155 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 else
14157 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014158 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159 get_tv_string_buf(&argvars[1], buf));
14160 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161#endif
14162}
14163
14164/*
14165 * "histget()" function
14166 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014168f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169{
14170#ifdef FEAT_CMDHIST
14171 int type;
14172 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014175 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14176 if (str == NULL)
14177 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014179 {
14180 type = get_histtype(str);
14181 if (argvars[1].v_type == VAR_UNKNOWN)
14182 idx = get_history_idx(type);
14183 else
14184 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14185 /* -1 on type error */
14186 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014189 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192}
14193
14194/*
14195 * "histnr()" function
14196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014198f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199{
14200 int i;
14201
14202#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014203 char_u *history = get_tv_string_chk(&argvars[0]);
14204
14205 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014206 if (i >= HIST_CMD && i < HIST_COUNT)
14207 i = get_history_idx(i);
14208 else
14209#endif
14210 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014211 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212}
14213
14214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 * "highlightID(name)" function
14216 */
14217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014218f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014220 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221}
14222
14223/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014224 * "highlight_exists()" function
14225 */
14226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014227f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014228{
14229 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14230}
14231
14232/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233 * "hostname()" function
14234 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014236f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237{
14238 char_u hostname[256];
14239
14240 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 rettv->v_type = VAR_STRING;
14242 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243}
14244
14245/*
14246 * iconv() function
14247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014249f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014250{
14251#ifdef FEAT_MBYTE
14252 char_u buf1[NUMBUFLEN];
14253 char_u buf2[NUMBUFLEN];
14254 char_u *from, *to, *str;
14255 vimconv_T vimconv;
14256#endif
14257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014258 rettv->v_type = VAR_STRING;
14259 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260
14261#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014262 str = get_tv_string(&argvars[0]);
14263 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14264 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014265 vimconv.vc_type = CONV_NONE;
14266 convert_setup(&vimconv, from, to);
14267
14268 /* If the encodings are equal, no conversion needed. */
14269 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273
14274 convert_setup(&vimconv, NULL, NULL);
14275 vim_free(from);
14276 vim_free(to);
14277#endif
14278}
14279
14280/*
14281 * "indent()" function
14282 */
14283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014284f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285{
14286 linenr_T lnum;
14287
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014292 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293}
14294
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014295/*
14296 * "index()" function
14297 */
14298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014299f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014300{
Bram Moolenaar33570922005-01-25 22:26:29 +000014301 list_T *l;
14302 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014303 long idx = 0;
14304 int ic = FALSE;
14305
14306 rettv->vval.v_number = -1;
14307 if (argvars[0].v_type != VAR_LIST)
14308 {
14309 EMSG(_(e_listreq));
14310 return;
14311 }
14312 l = argvars[0].vval.v_list;
14313 if (l != NULL)
14314 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014315 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014316 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014318 int error = FALSE;
14319
Bram Moolenaar758711c2005-02-02 23:11:38 +000014320 /* Start at specified item. Use the cached index that list_find()
14321 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014322 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014323 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014324 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014325 ic = get_tv_number_chk(&argvars[3], &error);
14326 if (error)
14327 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014328 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014329
Bram Moolenaar758711c2005-02-02 23:11:38 +000014330 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014331 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014332 {
14333 rettv->vval.v_number = idx;
14334 break;
14335 }
14336 }
14337}
14338
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339static int inputsecret_flag = 0;
14340
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014341static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014342
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014344 * This function is used by f_input() and f_inputdialog() functions. The third
14345 * argument to f_input() specifies the type of completion to use at the
14346 * prompt. The third argument to f_inputdialog() specifies the value to return
14347 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 */
14349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014350get_user_input(
14351 typval_T *argvars,
14352 typval_T *rettv,
14353 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356 char_u *p = NULL;
14357 int c;
14358 char_u buf[NUMBUFLEN];
14359 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014361 int xp_type = EXPAND_NOTHING;
14362 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014365 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366
14367#ifdef NO_CONSOLE_INPUT
14368 /* While starting up, there is no place to enter text. */
14369 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371#endif
14372
14373 cmd_silent = FALSE; /* Want to see the prompt. */
14374 if (prompt != NULL)
14375 {
14376 /* Only the part of the message after the last NL is considered as
14377 * prompt for the command line */
14378 p = vim_strrchr(prompt, '\n');
14379 if (p == NULL)
14380 p = prompt;
14381 else
14382 {
14383 ++p;
14384 c = *p;
14385 *p = NUL;
14386 msg_start();
14387 msg_clr_eos();
14388 msg_puts_attr(prompt, echo_attr);
14389 msg_didout = FALSE;
14390 msg_starthere();
14391 *p = c;
14392 }
14393 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014395 if (argvars[1].v_type != VAR_UNKNOWN)
14396 {
14397 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14398 if (defstr != NULL)
14399 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014401 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014402 {
14403 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014404 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014405 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014406
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014407 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014408 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014409
Bram Moolenaar4463f292005-09-25 22:20:24 +000014410 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14411 if (xp_name == NULL)
14412 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014413
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014414 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014415
Bram Moolenaar4463f292005-09-25 22:20:24 +000014416 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14417 &xp_arg) == FAIL)
14418 return;
14419 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014420 }
14421
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014423 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014424 int save_ex_normal_busy = ex_normal_busy;
14425 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014426 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014427 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14428 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014429 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014430 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014431 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014432 && argvars[1].v_type != VAR_UNKNOWN
14433 && argvars[2].v_type != VAR_UNKNOWN)
14434 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14435 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014436
14437 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 /* since the user typed this, no need to wait for return */
14440 need_wait_return = FALSE;
14441 msg_didout = FALSE;
14442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 cmd_silent = cmd_silent_save;
14444}
14445
14446/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014447 * "input()" function
14448 * Also handles inputsecret() when inputsecret is set.
14449 */
14450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014451f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014452{
14453 get_user_input(argvars, rettv, FALSE);
14454}
14455
14456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 * "inputdialog()" function
14458 */
14459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014460f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461{
14462#if defined(FEAT_GUI_TEXTDIALOG)
14463 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14464 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14465 {
14466 char_u *message;
14467 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014468 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014470 message = get_tv_string_chk(&argvars[0]);
14471 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014472 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014473 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014474 else
14475 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014476 if (message != NULL && defstr != NULL
14477 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014478 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480 else
14481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014482 if (message != NULL && defstr != NULL
14483 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014484 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014485 rettv->vval.v_string = vim_strsave(
14486 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014488 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 }
14492 else
14493#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014494 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495}
14496
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014497/*
14498 * "inputlist()" function
14499 */
14500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014501f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014502{
14503 listitem_T *li;
14504 int selected;
14505 int mouse_used;
14506
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014507#ifdef NO_CONSOLE_INPUT
14508 /* While starting up, there is no place to enter text. */
14509 if (no_console_input())
14510 return;
14511#endif
14512 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14513 {
14514 EMSG2(_(e_listarg), "inputlist()");
14515 return;
14516 }
14517
14518 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014519 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014520 lines_left = Rows; /* avoid more prompt */
14521 msg_scroll = TRUE;
14522 msg_clr_eos();
14523
14524 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14525 {
14526 msg_puts(get_tv_string(&li->li_tv));
14527 msg_putchar('\n');
14528 }
14529
14530 /* Ask for choice. */
14531 selected = prompt_for_number(&mouse_used);
14532 if (mouse_used)
14533 selected -= lines_left;
14534
14535 rettv->vval.v_number = selected;
14536}
14537
14538
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14540
14541/*
14542 * "inputrestore()" function
14543 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014545f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546{
14547 if (ga_userinput.ga_len > 0)
14548 {
14549 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14551 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014552 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 }
14554 else if (p_verbose > 1)
14555 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014556 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 }
14559}
14560
14561/*
14562 * "inputsave()" function
14563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014565f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014567 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 if (ga_grow(&ga_userinput, 1) == OK)
14569 {
14570 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14571 + ga_userinput.ga_len);
14572 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014573 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574 }
14575 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014576 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577}
14578
14579/*
14580 * "inputsecret()" function
14581 */
14582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014583f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584{
14585 ++cmdline_star;
14586 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014587 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588 --cmdline_star;
14589 --inputsecret_flag;
14590}
14591
14592/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014593 * "insert()" function
14594 */
14595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014596f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014597{
14598 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014599 listitem_T *item;
14600 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014602
14603 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014605 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014606 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014607 {
14608 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014609 before = get_tv_number_chk(&argvars[2], &error);
14610 if (error)
14611 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014612
Bram Moolenaar758711c2005-02-02 23:11:38 +000014613 if (before == l->lv_len)
14614 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014615 else
14616 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014617 item = list_find(l, before);
14618 if (item == NULL)
14619 {
14620 EMSGN(_(e_listidx), before);
14621 l = NULL;
14622 }
14623 }
14624 if (l != NULL)
14625 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014626 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014627 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014628 }
14629 }
14630}
14631
14632/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014633 * "invert(expr)" function
14634 */
14635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014636f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014637{
14638 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14639}
14640
14641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014642 * "isdirectory()" function
14643 */
14644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014645f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014647 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014648}
14649
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014650/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014651 * "islocked()" function
14652 */
14653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014654f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014655{
14656 lval_T lv;
14657 char_u *end;
14658 dictitem_T *di;
14659
14660 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014661 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14662 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014663 if (end != NULL && lv.ll_name != NULL)
14664 {
14665 if (*end != NUL)
14666 EMSG(_(e_trailing));
14667 else
14668 {
14669 if (lv.ll_tv == NULL)
14670 {
14671 if (check_changedtick(lv.ll_name))
14672 rettv->vval.v_number = 1; /* always locked */
14673 else
14674 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014675 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014676 if (di != NULL)
14677 {
14678 /* Consider a variable locked when:
14679 * 1. the variable itself is locked
14680 * 2. the value of the variable is locked.
14681 * 3. the List or Dict value is locked.
14682 */
14683 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14684 || tv_islocked(&di->di_tv));
14685 }
14686 }
14687 }
14688 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014689 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014690 else if (lv.ll_newkey != NULL)
14691 EMSG2(_(e_dictkey), lv.ll_newkey);
14692 else if (lv.ll_list != NULL)
14693 /* List item. */
14694 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14695 else
14696 /* Dictionary item. */
14697 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14698 }
14699 }
14700
14701 clear_lval(&lv);
14702}
14703
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014704#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14705/*
14706 * "isnan()" function
14707 */
14708 static void
14709f_isnan(typval_T *argvars, typval_T *rettv)
14710{
14711 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14712 && isnan(argvars[0].vval.v_float);
14713}
14714#endif
14715
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014716static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014717
14718/*
14719 * Turn a dict into a list:
14720 * "what" == 0: list of keys
14721 * "what" == 1: list of values
14722 * "what" == 2: list of items
14723 */
14724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014725dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014726{
Bram Moolenaar33570922005-01-25 22:26:29 +000014727 list_T *l2;
14728 dictitem_T *di;
14729 hashitem_T *hi;
14730 listitem_T *li;
14731 listitem_T *li2;
14732 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014733 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014734
Bram Moolenaar8c711452005-01-14 21:53:12 +000014735 if (argvars[0].v_type != VAR_DICT)
14736 {
14737 EMSG(_(e_dictreq));
14738 return;
14739 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014740 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014741 return;
14742
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014743 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014744 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014745
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014746 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014747 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014748 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014749 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014750 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014751 --todo;
14752 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014753
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014754 li = listitem_alloc();
14755 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014756 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014757 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014758
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014759 if (what == 0)
14760 {
14761 /* keys() */
14762 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014763 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014764 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14765 }
14766 else if (what == 1)
14767 {
14768 /* values() */
14769 copy_tv(&di->di_tv, &li->li_tv);
14770 }
14771 else
14772 {
14773 /* items() */
14774 l2 = list_alloc();
14775 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014776 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014777 li->li_tv.vval.v_list = l2;
14778 if (l2 == NULL)
14779 break;
14780 ++l2->lv_refcount;
14781
14782 li2 = listitem_alloc();
14783 if (li2 == NULL)
14784 break;
14785 list_append(l2, li2);
14786 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014787 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014788 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14789
14790 li2 = listitem_alloc();
14791 if (li2 == NULL)
14792 break;
14793 list_append(l2, li2);
14794 copy_tv(&di->di_tv, &li2->li_tv);
14795 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014796 }
14797 }
14798}
14799
14800/*
14801 * "items(dict)" function
14802 */
14803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014804f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014805{
14806 dict_list(argvars, rettv, 2);
14807}
14808
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014809#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014810/*
14811 * Get the job from the argument.
14812 * Returns NULL if the job is invalid.
14813 */
14814 static job_T *
14815get_job_arg(typval_T *tv)
14816{
14817 job_T *job;
14818
14819 if (tv->v_type != VAR_JOB)
14820 {
14821 EMSG2(_(e_invarg2), get_tv_string(tv));
14822 return NULL;
14823 }
14824 job = tv->vval.v_job;
14825
14826 if (job == NULL)
14827 EMSG(_("E916: not a valid job"));
14828 return job;
14829}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014830
Bram Moolenaar835dc632016-02-07 14:27:38 +010014831/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014832 * "job_getchannel()" function
14833 */
14834 static void
14835f_job_getchannel(typval_T *argvars, typval_T *rettv)
14836{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014837 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014838
Bram Moolenaar65edff82016-02-21 16:40:11 +010014839 if (job != NULL)
14840 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014841 rettv->v_type = VAR_CHANNEL;
14842 rettv->vval.v_channel = job->jv_channel;
14843 if (job->jv_channel != NULL)
14844 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014845 }
14846}
14847
14848/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014849 * "job_info()" function
14850 */
14851 static void
14852f_job_info(typval_T *argvars, typval_T *rettv)
14853{
14854 job_T *job = get_job_arg(&argvars[0]);
14855
14856 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14857 job_info(job, rettv->vval.v_dict);
14858}
14859
14860/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014861 * "job_setoptions()" function
14862 */
14863 static void
14864f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14865{
14866 job_T *job = get_job_arg(&argvars[0]);
14867 jobopt_T opt;
14868
14869 if (job == NULL)
14870 return;
14871 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014872 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014873 return;
14874 job_set_options(job, &opt);
14875}
14876
14877/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014878 * "job_start()" function
14879 */
14880 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014881f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014882{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014883 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014884 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014885}
14886
14887/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014888 * "job_status()" function
14889 */
14890 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014891f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014892{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014893 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014894
Bram Moolenaar65edff82016-02-21 16:40:11 +010014895 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014896 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014897 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014898 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014899 }
14900}
14901
14902/*
14903 * "job_stop()" function
14904 */
14905 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014906f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014907{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014908 job_T *job = get_job_arg(&argvars[0]);
14909
14910 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014911 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014912}
14913#endif
14914
Bram Moolenaar071d4272004-06-13 20:20:40 +000014915/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014916 * "join()" function
14917 */
14918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014919f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014920{
14921 garray_T ga;
14922 char_u *sep;
14923
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014924 if (argvars[0].v_type != VAR_LIST)
14925 {
14926 EMSG(_(e_listreq));
14927 return;
14928 }
14929 if (argvars[0].vval.v_list == NULL)
14930 return;
14931 if (argvars[1].v_type == VAR_UNKNOWN)
14932 sep = (char_u *)" ";
14933 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014934 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014935
14936 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014937
14938 if (sep != NULL)
14939 {
14940 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014941 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014942 ga_append(&ga, NUL);
14943 rettv->vval.v_string = (char_u *)ga.ga_data;
14944 }
14945 else
14946 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014947}
14948
14949/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014950 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014951 */
14952 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014953f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014954{
14955 js_read_T reader;
14956
14957 reader.js_buf = get_tv_string(&argvars[0]);
14958 reader.js_fill = NULL;
14959 reader.js_used = 0;
14960 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14961 EMSG(_(e_invarg));
14962}
14963
14964/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014965 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014966 */
14967 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014968f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014969{
14970 rettv->v_type = VAR_STRING;
14971 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14972}
14973
14974/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014975 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014976 */
14977 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014978f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014979{
14980 js_read_T reader;
14981
14982 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014983 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014984 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014985 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014986 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014987}
14988
14989/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014990 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014991 */
14992 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014993f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014994{
14995 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014996 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014997}
14998
14999/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015000 * "keys()" function
15001 */
15002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015003f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015004{
15005 dict_list(argvars, rettv, 0);
15006}
15007
15008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015009 * "last_buffer_nr()" function.
15010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015012f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013{
15014 int n = 0;
15015 buf_T *buf;
15016
15017 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15018 if (n < buf->b_fnum)
15019 n = buf->b_fnum;
15020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015021 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015022}
15023
15024/*
15025 * "len()" function
15026 */
15027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015028f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015029{
15030 switch (argvars[0].v_type)
15031 {
15032 case VAR_STRING:
15033 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034 rettv->vval.v_number = (varnumber_T)STRLEN(
15035 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015036 break;
15037 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015038 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015039 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015040 case VAR_DICT:
15041 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15042 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015043 case VAR_UNKNOWN:
15044 case VAR_SPECIAL:
15045 case VAR_FLOAT:
15046 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015047 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015048 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015049 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015050 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015051 break;
15052 }
15053}
15054
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015055static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015056
15057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015058libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015059{
15060#ifdef FEAT_LIBCALL
15061 char_u *string_in;
15062 char_u **string_result;
15063 int nr_result;
15064#endif
15065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015066 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015067 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015069
15070 if (check_restricted() || check_secure())
15071 return;
15072
15073#ifdef FEAT_LIBCALL
15074 /* The first two args must be strings, otherwise its meaningless */
15075 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15076 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015077 string_in = NULL;
15078 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015079 string_in = argvars[2].vval.v_string;
15080 if (type == VAR_NUMBER)
15081 string_result = NULL;
15082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015084 if (mch_libcall(argvars[0].vval.v_string,
15085 argvars[1].vval.v_string,
15086 string_in,
15087 argvars[2].vval.v_number,
15088 string_result,
15089 &nr_result) == OK
15090 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015091 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015092 }
15093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094}
15095
15096/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015097 * "libcall()" function
15098 */
15099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015100f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015101{
15102 libcall_common(argvars, rettv, VAR_STRING);
15103}
15104
15105/*
15106 * "libcallnr()" function
15107 */
15108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015109f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015110{
15111 libcall_common(argvars, rettv, VAR_NUMBER);
15112}
15113
15114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115 * "line(string)" function
15116 */
15117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015118f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119{
15120 linenr_T lnum = 0;
15121 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015122 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015123
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015124 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125 if (fp != NULL)
15126 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015127 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128}
15129
15130/*
15131 * "line2byte(lnum)" function
15132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015134f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015135{
15136#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138#else
15139 linenr_T lnum;
15140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015145 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15146 if (rettv->vval.v_number >= 0)
15147 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148#endif
15149}
15150
15151/*
15152 * "lispindent(lnum)" function
15153 */
15154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015155f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015156{
15157#ifdef FEAT_LISP
15158 pos_T pos;
15159 linenr_T lnum;
15160
15161 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015162 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15164 {
15165 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015166 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167 curwin->w_cursor = pos;
15168 }
15169 else
15170#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015171 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172}
15173
15174/*
15175 * "localtime()" function
15176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015178f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015180 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181}
15182
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015183static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015184
15185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015186get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187{
15188 char_u *keys;
15189 char_u *which;
15190 char_u buf[NUMBUFLEN];
15191 char_u *keys_buf = NULL;
15192 char_u *rhs;
15193 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015194 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015195 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015196 mapblock_T *mp;
15197 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015198
15199 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015200 rettv->v_type = VAR_STRING;
15201 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015203 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204 if (*keys == NUL)
15205 return;
15206
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015207 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015209 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015210 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015211 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015212 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015213 if (argvars[3].v_type != VAR_UNKNOWN)
15214 get_dict = get_tv_number(&argvars[3]);
15215 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217 else
15218 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015219 if (which == NULL)
15220 return;
15221
Bram Moolenaar071d4272004-06-13 20:20:40 +000015222 mode = get_map_mode(&which, 0);
15223
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015224 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015225 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015227
15228 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015229 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015230 /* Return a string. */
15231 if (rhs != NULL)
15232 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233
Bram Moolenaarbd743252010-10-20 21:23:33 +020015234 }
15235 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15236 {
15237 /* Return a dictionary. */
15238 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15239 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15240 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241
Bram Moolenaarbd743252010-10-20 21:23:33 +020015242 dict_add_nr_str(dict, "lhs", 0L, lhs);
15243 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15244 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15245 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15246 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15247 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15248 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015249 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015250 dict_add_nr_str(dict, "mode", 0L, mapmode);
15251
15252 vim_free(lhs);
15253 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254 }
15255}
15256
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015257#ifdef FEAT_FLOAT
15258/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015259 * "log()" function
15260 */
15261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015262f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015263{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015264 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015265
15266 rettv->v_type = VAR_FLOAT;
15267 if (get_float_arg(argvars, &f) == OK)
15268 rettv->vval.v_float = log(f);
15269 else
15270 rettv->vval.v_float = 0.0;
15271}
15272
15273/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015274 * "log10()" function
15275 */
15276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015277f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015278{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015279 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015280
15281 rettv->v_type = VAR_FLOAT;
15282 if (get_float_arg(argvars, &f) == OK)
15283 rettv->vval.v_float = log10(f);
15284 else
15285 rettv->vval.v_float = 0.0;
15286}
15287#endif
15288
Bram Moolenaar1dced572012-04-05 16:54:08 +020015289#ifdef FEAT_LUA
15290/*
15291 * "luaeval()" function
15292 */
15293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015294f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015295{
15296 char_u *str;
15297 char_u buf[NUMBUFLEN];
15298
15299 str = get_tv_string_buf(&argvars[0], buf);
15300 do_luaeval(str, argvars + 1, rettv);
15301}
15302#endif
15303
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015305 * "map()" function
15306 */
15307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015308f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015309{
15310 filter_map(argvars, rettv, TRUE);
15311}
15312
15313/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015314 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 */
15316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015317f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015319 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320}
15321
15322/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015323 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324 */
15325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015326f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015328 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329}
15330
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015331static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332
15333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015334find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015336 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015337 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015338 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 char_u *pat;
15340 regmatch_T regmatch;
15341 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015342 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 char_u *save_cpo;
15344 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015345 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015346 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015347 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015348 list_T *l = NULL;
15349 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015350 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015351 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352
15353 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15354 save_cpo = p_cpo;
15355 p_cpo = (char_u *)"";
15356
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015357 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015358 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015359 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015360 /* type 3: return empty list when there are no matches.
15361 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015362 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015363 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015364 if (type == 4
15365 && (list_append_string(rettv->vval.v_list,
15366 (char_u *)"", 0) == FAIL
15367 || list_append_number(rettv->vval.v_list,
15368 (varnumber_T)-1) == FAIL
15369 || list_append_number(rettv->vval.v_list,
15370 (varnumber_T)-1) == FAIL
15371 || list_append_number(rettv->vval.v_list,
15372 (varnumber_T)-1) == FAIL))
15373 {
15374 list_free(rettv->vval.v_list, TRUE);
15375 rettv->vval.v_list = NULL;
15376 goto theend;
15377 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015378 }
15379 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 rettv->v_type = VAR_STRING;
15382 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015385 if (argvars[0].v_type == VAR_LIST)
15386 {
15387 if ((l = argvars[0].vval.v_list) == NULL)
15388 goto theend;
15389 li = l->lv_first;
15390 }
15391 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015392 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015393 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015394 len = (long)STRLEN(str);
15395 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015396
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015397 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15398 if (pat == NULL)
15399 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015400
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015401 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015403 int error = FALSE;
15404
15405 start = get_tv_number_chk(&argvars[2], &error);
15406 if (error)
15407 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015408 if (l != NULL)
15409 {
15410 li = list_find(l, start);
15411 if (li == NULL)
15412 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015413 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015414 }
15415 else
15416 {
15417 if (start < 0)
15418 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015419 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015420 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015421 /* When "count" argument is there ignore matches before "start",
15422 * otherwise skip part of the string. Differs when pattern is "^"
15423 * or "\<". */
15424 if (argvars[3].v_type != VAR_UNKNOWN)
15425 startcol = start;
15426 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015427 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015428 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015429 len -= start;
15430 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015431 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015432
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015433 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015434 nth = get_tv_number_chk(&argvars[3], &error);
15435 if (error)
15436 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 }
15438
15439 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15440 if (regmatch.regprog != NULL)
15441 {
15442 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015443
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015444 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015445 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015446 if (l != NULL)
15447 {
15448 if (li == NULL)
15449 {
15450 match = FALSE;
15451 break;
15452 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015453 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015454 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015455 if (str == NULL)
15456 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015457 }
15458
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015459 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015460
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015461 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015462 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015463 if (l == NULL && !match)
15464 break;
15465
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015466 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015467 if (l != NULL)
15468 {
15469 li = li->li_next;
15470 ++idx;
15471 }
15472 else
15473 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015474#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015475 startcol = (colnr_T)(regmatch.startp[0]
15476 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015477#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015478 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015479#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015480 if (startcol > (colnr_T)len
15481 || str + startcol <= regmatch.startp[0])
15482 {
15483 match = FALSE;
15484 break;
15485 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015486 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015487 }
15488
15489 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015491 if (type == 4)
15492 {
15493 listitem_T *li1 = rettv->vval.v_list->lv_first;
15494 listitem_T *li2 = li1->li_next;
15495 listitem_T *li3 = li2->li_next;
15496 listitem_T *li4 = li3->li_next;
15497
15498 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15499 (int)(regmatch.endp[0] - regmatch.startp[0]));
15500 li3->li_tv.vval.v_number =
15501 (varnumber_T)(regmatch.startp[0] - expr);
15502 li4->li_tv.vval.v_number =
15503 (varnumber_T)(regmatch.endp[0] - expr);
15504 if (l != NULL)
15505 li2->li_tv.vval.v_number = (varnumber_T)idx;
15506 }
15507 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015508 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015509 int i;
15510
15511 /* return list with matched string and submatches */
15512 for (i = 0; i < NSUBEXP; ++i)
15513 {
15514 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015515 {
15516 if (list_append_string(rettv->vval.v_list,
15517 (char_u *)"", 0) == FAIL)
15518 break;
15519 }
15520 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015521 regmatch.startp[i],
15522 (int)(regmatch.endp[i] - regmatch.startp[i]))
15523 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015524 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015525 }
15526 }
15527 else if (type == 2)
15528 {
15529 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015530 if (l != NULL)
15531 copy_tv(&li->li_tv, rettv);
15532 else
15533 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015535 }
15536 else if (l != NULL)
15537 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538 else
15539 {
15540 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015541 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 (varnumber_T)(regmatch.startp[0] - str);
15543 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015544 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015546 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 }
15548 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015549 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 }
15551
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015552 if (type == 4 && l == NULL)
15553 /* matchstrpos() without a list: drop the second item. */
15554 listitem_remove(rettv->vval.v_list,
15555 rettv->vval.v_list->lv_first->li_next);
15556
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015558 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 p_cpo = save_cpo;
15560}
15561
15562/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015563 * "match()" function
15564 */
15565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015566f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015567{
15568 find_some_match(argvars, rettv, 1);
15569}
15570
15571/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015572 * "matchadd()" function
15573 */
15574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015575f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015576{
15577#ifdef FEAT_SEARCH_EXTRA
15578 char_u buf[NUMBUFLEN];
15579 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15580 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15581 int prio = 10; /* default priority */
15582 int id = -1;
15583 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015584 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015585
15586 rettv->vval.v_number = -1;
15587
15588 if (grp == NULL || pat == NULL)
15589 return;
15590 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015591 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015592 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015593 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015594 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015595 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015596 if (argvars[4].v_type != VAR_UNKNOWN)
15597 {
15598 if (argvars[4].v_type != VAR_DICT)
15599 {
15600 EMSG(_(e_dictreq));
15601 return;
15602 }
15603 if (dict_find(argvars[4].vval.v_dict,
15604 (char_u *)"conceal", -1) != NULL)
15605 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15606 (char_u *)"conceal", FALSE);
15607 }
15608 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015609 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015610 if (error == TRUE)
15611 return;
15612 if (id >= 1 && id <= 3)
15613 {
15614 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15615 return;
15616 }
15617
Bram Moolenaar6561d522015-07-21 15:48:27 +020015618 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15619 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015620#endif
15621}
15622
15623/*
15624 * "matchaddpos()" function
15625 */
15626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015627f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015628{
15629#ifdef FEAT_SEARCH_EXTRA
15630 char_u buf[NUMBUFLEN];
15631 char_u *group;
15632 int prio = 10;
15633 int id = -1;
15634 int error = FALSE;
15635 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015636 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015637
15638 rettv->vval.v_number = -1;
15639
15640 group = get_tv_string_buf_chk(&argvars[0], buf);
15641 if (group == NULL)
15642 return;
15643
15644 if (argvars[1].v_type != VAR_LIST)
15645 {
15646 EMSG2(_(e_listarg), "matchaddpos()");
15647 return;
15648 }
15649 l = argvars[1].vval.v_list;
15650 if (l == NULL)
15651 return;
15652
15653 if (argvars[2].v_type != VAR_UNKNOWN)
15654 {
15655 prio = get_tv_number_chk(&argvars[2], &error);
15656 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015657 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015658 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015659 if (argvars[4].v_type != VAR_UNKNOWN)
15660 {
15661 if (argvars[4].v_type != VAR_DICT)
15662 {
15663 EMSG(_(e_dictreq));
15664 return;
15665 }
15666 if (dict_find(argvars[4].vval.v_dict,
15667 (char_u *)"conceal", -1) != NULL)
15668 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15669 (char_u *)"conceal", FALSE);
15670 }
15671 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015672 }
15673 if (error == TRUE)
15674 return;
15675
15676 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15677 if (id == 1 || id == 2)
15678 {
15679 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15680 return;
15681 }
15682
Bram Moolenaar6561d522015-07-21 15:48:27 +020015683 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15684 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015685#endif
15686}
15687
15688/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015689 * "matcharg()" function
15690 */
15691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015692f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015693{
15694 if (rettv_list_alloc(rettv) == OK)
15695 {
15696#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015697 int id = get_tv_number(&argvars[0]);
15698 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015699
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015700 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015701 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015702 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15703 {
15704 list_append_string(rettv->vval.v_list,
15705 syn_id2name(m->hlg_id), -1);
15706 list_append_string(rettv->vval.v_list, m->pattern, -1);
15707 }
15708 else
15709 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015710 list_append_string(rettv->vval.v_list, NULL, -1);
15711 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015712 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015713 }
15714#endif
15715 }
15716}
15717
15718/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015719 * "matchdelete()" function
15720 */
15721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015722f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015723{
15724#ifdef FEAT_SEARCH_EXTRA
15725 rettv->vval.v_number = match_delete(curwin,
15726 (int)get_tv_number(&argvars[0]), TRUE);
15727#endif
15728}
15729
15730/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015731 * "matchend()" function
15732 */
15733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015734f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015735{
15736 find_some_match(argvars, rettv, 0);
15737}
15738
15739/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015740 * "matchlist()" function
15741 */
15742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015743f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015744{
15745 find_some_match(argvars, rettv, 3);
15746}
15747
15748/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015749 * "matchstr()" function
15750 */
15751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015752f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015753{
15754 find_some_match(argvars, rettv, 2);
15755}
15756
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015757/*
15758 * "matchstrpos()" function
15759 */
15760 static void
15761f_matchstrpos(typval_T *argvars, typval_T *rettv)
15762{
15763 find_some_match(argvars, rettv, 4);
15764}
15765
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015766static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015767
15768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015769max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015770{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015771 long n = 0;
15772 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015773 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015774
15775 if (argvars[0].v_type == VAR_LIST)
15776 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 list_T *l;
15778 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015779
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015780 l = argvars[0].vval.v_list;
15781 if (l != NULL)
15782 {
15783 li = l->lv_first;
15784 if (li != NULL)
15785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015786 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015787 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015788 {
15789 li = li->li_next;
15790 if (li == NULL)
15791 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015792 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015793 if (domax ? i > n : i < n)
15794 n = i;
15795 }
15796 }
15797 }
15798 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015799 else if (argvars[0].v_type == VAR_DICT)
15800 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015801 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015802 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015803 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015804 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015805
15806 d = argvars[0].vval.v_dict;
15807 if (d != NULL)
15808 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015809 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015810 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015811 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015812 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015813 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015814 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015815 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015816 if (first)
15817 {
15818 n = i;
15819 first = FALSE;
15820 }
15821 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015822 n = i;
15823 }
15824 }
15825 }
15826 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015827 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015828 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015829 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015830}
15831
15832/*
15833 * "max()" function
15834 */
15835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015836f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015837{
15838 max_min(argvars, rettv, TRUE);
15839}
15840
15841/*
15842 * "min()" function
15843 */
15844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015845f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015846{
15847 max_min(argvars, rettv, FALSE);
15848}
15849
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015850static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015851
15852/*
15853 * Create the directory in which "dir" is located, and higher levels when
15854 * needed.
15855 */
15856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015857mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015858{
15859 char_u *p;
15860 char_u *updir;
15861 int r = FAIL;
15862
15863 /* Get end of directory name in "dir".
15864 * We're done when it's "/" or "c:/". */
15865 p = gettail_sep(dir);
15866 if (p <= get_past_head(dir))
15867 return OK;
15868
15869 /* If the directory exists we're done. Otherwise: create it.*/
15870 updir = vim_strnsave(dir, (int)(p - dir));
15871 if (updir == NULL)
15872 return FAIL;
15873 if (mch_isdir(updir))
15874 r = OK;
15875 else if (mkdir_recurse(updir, prot) == OK)
15876 r = vim_mkdir_emsg(updir, prot);
15877 vim_free(updir);
15878 return r;
15879}
15880
15881#ifdef vim_mkdir
15882/*
15883 * "mkdir()" function
15884 */
15885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015886f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015887{
15888 char_u *dir;
15889 char_u buf[NUMBUFLEN];
15890 int prot = 0755;
15891
15892 rettv->vval.v_number = FAIL;
15893 if (check_restricted() || check_secure())
15894 return;
15895
15896 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015897 if (*dir == NUL)
15898 rettv->vval.v_number = FAIL;
15899 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015900 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015901 if (*gettail(dir) == NUL)
15902 /* remove trailing slashes */
15903 *gettail_sep(dir) = NUL;
15904
15905 if (argvars[1].v_type != VAR_UNKNOWN)
15906 {
15907 if (argvars[2].v_type != VAR_UNKNOWN)
15908 prot = get_tv_number_chk(&argvars[2], NULL);
15909 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15910 mkdir_recurse(dir, prot);
15911 }
15912 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015913 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015914}
15915#endif
15916
Bram Moolenaar0d660222005-01-07 21:51:51 +000015917/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918 * "mode()" function
15919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015921f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015922{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015923 char_u buf[3];
15924
15925 buf[1] = NUL;
15926 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 if (VIsual_active)
15929 {
15930 if (VIsual_select)
15931 buf[0] = VIsual_mode + 's' - 'v';
15932 else
15933 buf[0] = VIsual_mode;
15934 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015935 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015936 || State == CONFIRM)
15937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015938 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015939 if (State == ASKMORE)
15940 buf[1] = 'm';
15941 else if (State == CONFIRM)
15942 buf[1] = '?';
15943 }
15944 else if (State == EXTERNCMD)
15945 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946 else if (State & INSERT)
15947 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015948#ifdef FEAT_VREPLACE
15949 if (State & VREPLACE_FLAG)
15950 {
15951 buf[0] = 'R';
15952 buf[1] = 'v';
15953 }
15954 else
15955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956 if (State & REPLACE_FLAG)
15957 buf[0] = 'R';
15958 else
15959 buf[0] = 'i';
15960 }
15961 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015963 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015964 if (exmode_active)
15965 buf[1] = 'v';
15966 }
15967 else if (exmode_active)
15968 {
15969 buf[0] = 'c';
15970 buf[1] = 'e';
15971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015975 if (finish_op)
15976 buf[1] = 'o';
15977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015979 /* Clear out the minor mode when the argument is not a non-zero number or
15980 * non-empty string. */
15981 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015982 buf[1] = NUL;
15983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015984 rettv->vval.v_string = vim_strsave(buf);
15985 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986}
15987
Bram Moolenaar429fa852013-04-15 12:27:36 +020015988#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015989/*
15990 * "mzeval()" function
15991 */
15992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015993f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015994{
15995 char_u *str;
15996 char_u buf[NUMBUFLEN];
15997
15998 str = get_tv_string_buf(&argvars[0], buf);
15999 do_mzeval(str, rettv);
16000}
Bram Moolenaar75676462013-01-30 14:55:42 +010016001
16002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016003mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016004{
16005 typval_T argvars[3];
16006
16007 argvars[0].v_type = VAR_STRING;
16008 argvars[0].vval.v_string = name;
16009 copy_tv(args, &argvars[1]);
16010 argvars[2].v_type = VAR_UNKNOWN;
16011 f_call(argvars, rettv);
16012 clear_tv(&argvars[1]);
16013}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016014#endif
16015
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016017 * "nextnonblank()" function
16018 */
16019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016020f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016021{
16022 linenr_T lnum;
16023
16024 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016026 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016027 {
16028 lnum = 0;
16029 break;
16030 }
16031 if (*skipwhite(ml_get(lnum)) != NUL)
16032 break;
16033 }
16034 rettv->vval.v_number = lnum;
16035}
16036
16037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016038 * "nr2char()" function
16039 */
16040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016041f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042{
16043 char_u buf[NUMBUFLEN];
16044
16045#ifdef FEAT_MBYTE
16046 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016047 {
16048 int utf8 = 0;
16049
16050 if (argvars[1].v_type != VAR_UNKNOWN)
16051 utf8 = get_tv_number_chk(&argvars[1], NULL);
16052 if (utf8)
16053 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16054 else
16055 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 else
16058#endif
16059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016060 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061 buf[1] = NUL;
16062 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016063 rettv->v_type = VAR_STRING;
16064 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016065}
16066
16067/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016068 * "or(expr, expr)" function
16069 */
16070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016071f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016072{
16073 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16074 | get_tv_number_chk(&argvars[1], NULL);
16075}
16076
16077/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016078 * "pathshorten()" function
16079 */
16080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016081f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016082{
16083 char_u *p;
16084
16085 rettv->v_type = VAR_STRING;
16086 p = get_tv_string_chk(&argvars[0]);
16087 if (p == NULL)
16088 rettv->vval.v_string = NULL;
16089 else
16090 {
16091 p = vim_strsave(p);
16092 rettv->vval.v_string = p;
16093 if (p != NULL)
16094 shorten_dir(p);
16095 }
16096}
16097
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016098#ifdef FEAT_PERL
16099/*
16100 * "perleval()" function
16101 */
16102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016103f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016104{
16105 char_u *str;
16106 char_u buf[NUMBUFLEN];
16107
16108 str = get_tv_string_buf(&argvars[0], buf);
16109 do_perleval(str, rettv);
16110}
16111#endif
16112
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016113#ifdef FEAT_FLOAT
16114/*
16115 * "pow()" function
16116 */
16117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016118f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016119{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016120 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016121
16122 rettv->v_type = VAR_FLOAT;
16123 if (get_float_arg(argvars, &fx) == OK
16124 && get_float_arg(&argvars[1], &fy) == OK)
16125 rettv->vval.v_float = pow(fx, fy);
16126 else
16127 rettv->vval.v_float = 0.0;
16128}
16129#endif
16130
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016131/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016132 * "prevnonblank()" function
16133 */
16134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016135f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136{
16137 linenr_T lnum;
16138
16139 lnum = get_tv_lnum(argvars);
16140 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16141 lnum = 0;
16142 else
16143 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16144 --lnum;
16145 rettv->vval.v_number = lnum;
16146}
16147
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016148/* This dummy va_list is here because:
16149 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16150 * - locally in the function results in a "used before set" warning
16151 * - using va_start() to initialize it gives "function with fixed args" error */
16152static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016153
Bram Moolenaar8c711452005-01-14 21:53:12 +000016154/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016155 * "printf()" function
16156 */
16157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016158f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016159{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016160 char_u buf[NUMBUFLEN];
16161 int len;
16162 char_u *s;
16163 int saved_did_emsg = did_emsg;
16164 char *fmt;
16165
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016166 rettv->v_type = VAR_STRING;
16167 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016168
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016169 /* Get the required length, allocate the buffer and do it for real. */
16170 did_emsg = FALSE;
16171 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16172 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16173 if (!did_emsg)
16174 {
16175 s = alloc(len + 1);
16176 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016177 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016178 rettv->vval.v_string = s;
16179 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016180 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016181 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016182 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016183}
16184
16185/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016186 * "pumvisible()" function
16187 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016189f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016190{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016191#ifdef FEAT_INS_EXPAND
16192 if (pum_visible())
16193 rettv->vval.v_number = 1;
16194#endif
16195}
16196
Bram Moolenaardb913952012-06-29 12:54:53 +020016197#ifdef FEAT_PYTHON3
16198/*
16199 * "py3eval()" function
16200 */
16201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016202f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016203{
16204 char_u *str;
16205 char_u buf[NUMBUFLEN];
16206
16207 str = get_tv_string_buf(&argvars[0], buf);
16208 do_py3eval(str, rettv);
16209}
16210#endif
16211
16212#ifdef FEAT_PYTHON
16213/*
16214 * "pyeval()" function
16215 */
16216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016217f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016218{
16219 char_u *str;
16220 char_u buf[NUMBUFLEN];
16221
16222 str = get_tv_string_buf(&argvars[0], buf);
16223 do_pyeval(str, rettv);
16224}
16225#endif
16226
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016227/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016228 * "range()" function
16229 */
16230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016231f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016232{
16233 long start;
16234 long end;
16235 long stride = 1;
16236 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016237 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016238
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016239 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016240 if (argvars[1].v_type == VAR_UNKNOWN)
16241 {
16242 end = start - 1;
16243 start = 0;
16244 }
16245 else
16246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016247 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016248 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016249 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016250 }
16251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016252 if (error)
16253 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016254 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016255 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016256 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016257 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016258 else
16259 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016260 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016261 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016262 if (list_append_number(rettv->vval.v_list,
16263 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016264 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016265 }
16266}
16267
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016268/*
16269 * "readfile()" function
16270 */
16271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016272f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016273{
16274 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016275 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016276 char_u *fname;
16277 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016278 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16279 int io_size = sizeof(buf);
16280 int readlen; /* size of last fread() */
16281 char_u *prev = NULL; /* previously read bytes, if any */
16282 long prevlen = 0; /* length of data in prev */
16283 long prevsize = 0; /* size of prev buffer */
16284 long maxline = MAXLNUM;
16285 long cnt = 0;
16286 char_u *p; /* position in buf */
16287 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016288
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016289 if (argvars[1].v_type != VAR_UNKNOWN)
16290 {
16291 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16292 binary = TRUE;
16293 if (argvars[2].v_type != VAR_UNKNOWN)
16294 maxline = get_tv_number(&argvars[2]);
16295 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016296
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016297 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016298 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016299
16300 /* Always open the file in binary mode, library functions have a mind of
16301 * their own about CR-LF conversion. */
16302 fname = get_tv_string(&argvars[0]);
16303 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16304 {
16305 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16306 return;
16307 }
16308
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016309 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016310 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016311 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016312
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016313 /* This for loop processes what was read, but is also entered at end
16314 * of file so that either:
16315 * - an incomplete line gets written
16316 * - a "binary" file gets an empty line at the end if it ends in a
16317 * newline. */
16318 for (p = buf, start = buf;
16319 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16320 ++p)
16321 {
16322 if (*p == '\n' || readlen <= 0)
16323 {
16324 listitem_T *li;
16325 char_u *s = NULL;
16326 long_u len = p - start;
16327
16328 /* Finished a line. Remove CRs before NL. */
16329 if (readlen > 0 && !binary)
16330 {
16331 while (len > 0 && start[len - 1] == '\r')
16332 --len;
16333 /* removal may cross back to the "prev" string */
16334 if (len == 0)
16335 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16336 --prevlen;
16337 }
16338 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016339 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016340 else
16341 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016342 /* Change "prev" buffer to be the right size. This way
16343 * the bytes are only copied once, and very long lines are
16344 * allocated only once. */
16345 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016346 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016347 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016348 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016349 prev = NULL; /* the list will own the string */
16350 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016351 }
16352 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016353 if (s == NULL)
16354 {
16355 do_outofmem_msg((long_u) prevlen + len + 1);
16356 failed = TRUE;
16357 break;
16358 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016359
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016360 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016361 {
16362 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016363 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016364 break;
16365 }
16366 li->li_tv.v_type = VAR_STRING;
16367 li->li_tv.v_lock = 0;
16368 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016369 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016370
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016371 start = p + 1; /* step over newline */
16372 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016373 break;
16374 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016375 else if (*p == NUL)
16376 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016377#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016378 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16379 * when finding the BF and check the previous two bytes. */
16380 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016381 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016382 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16383 * + 1, these may be in the "prev" string. */
16384 char_u back1 = p >= buf + 1 ? p[-1]
16385 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16386 char_u back2 = p >= buf + 2 ? p[-2]
16387 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16388 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016389
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016390 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016391 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016392 char_u *dest = p - 2;
16393
16394 /* Usually a BOM is at the beginning of a file, and so at
16395 * the beginning of a line; then we can just step over it.
16396 */
16397 if (start == dest)
16398 start = p + 1;
16399 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016400 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016401 /* have to shuffle buf to close gap */
16402 int adjust_prevlen = 0;
16403
16404 if (dest < buf)
16405 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016406 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016407 dest = buf;
16408 }
16409 if (readlen > p - buf + 1)
16410 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16411 readlen -= 3 - adjust_prevlen;
16412 prevlen -= adjust_prevlen;
16413 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016414 }
16415 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016416 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016417#endif
16418 } /* for */
16419
16420 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16421 break;
16422 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016423 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016424 /* There's part of a line in buf, store it in "prev". */
16425 if (p - start + prevlen >= prevsize)
16426 {
16427 /* need bigger "prev" buffer */
16428 char_u *newprev;
16429
16430 /* A common use case is ordinary text files and "prev" gets a
16431 * fragment of a line, so the first allocation is made
16432 * small, to avoid repeatedly 'allocing' large and
16433 * 'reallocing' small. */
16434 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016435 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016436 else
16437 {
16438 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016439 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016440 prevsize = grow50pc > growmin ? grow50pc : growmin;
16441 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016442 newprev = prev == NULL ? alloc(prevsize)
16443 : vim_realloc(prev, prevsize);
16444 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016445 {
16446 do_outofmem_msg((long_u)prevsize);
16447 failed = TRUE;
16448 break;
16449 }
16450 prev = newprev;
16451 }
16452 /* Add the line part to end of "prev". */
16453 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016454 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016455 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016456 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016457
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016458 /*
16459 * For a negative line count use only the lines at the end of the file,
16460 * free the rest.
16461 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016462 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016463 while (cnt > -maxline)
16464 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016465 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016466 --cnt;
16467 }
16468
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016469 if (failed)
16470 {
16471 list_free(rettv->vval.v_list, TRUE);
16472 /* readfile doc says an empty list is returned on error */
16473 rettv->vval.v_list = list_alloc();
16474 }
16475
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016476 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016477 fclose(fd);
16478}
16479
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016480#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016481static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016482
16483/*
16484 * Convert a List to proftime_T.
16485 * Return FAIL when there is something wrong.
16486 */
16487 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016488list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016489{
16490 long n1, n2;
16491 int error = FALSE;
16492
16493 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16494 || arg->vval.v_list->lv_len != 2)
16495 return FAIL;
16496 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16497 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16498# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016499 tm->HighPart = n1;
16500 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016501# else
16502 tm->tv_sec = n1;
16503 tm->tv_usec = n2;
16504# endif
16505 return error ? FAIL : OK;
16506}
16507#endif /* FEAT_RELTIME */
16508
16509/*
16510 * "reltime()" function
16511 */
16512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016513f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016514{
16515#ifdef FEAT_RELTIME
16516 proftime_T res;
16517 proftime_T start;
16518
16519 if (argvars[0].v_type == VAR_UNKNOWN)
16520 {
16521 /* No arguments: get current time. */
16522 profile_start(&res);
16523 }
16524 else if (argvars[1].v_type == VAR_UNKNOWN)
16525 {
16526 if (list2proftime(&argvars[0], &res) == FAIL)
16527 return;
16528 profile_end(&res);
16529 }
16530 else
16531 {
16532 /* Two arguments: compute the difference. */
16533 if (list2proftime(&argvars[0], &start) == FAIL
16534 || list2proftime(&argvars[1], &res) == FAIL)
16535 return;
16536 profile_sub(&res, &start);
16537 }
16538
16539 if (rettv_list_alloc(rettv) == OK)
16540 {
16541 long n1, n2;
16542
16543# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016544 n1 = res.HighPart;
16545 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016546# else
16547 n1 = res.tv_sec;
16548 n2 = res.tv_usec;
16549# endif
16550 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16551 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16552 }
16553#endif
16554}
16555
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016556#ifdef FEAT_FLOAT
16557/*
16558 * "reltimefloat()" function
16559 */
16560 static void
16561f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16562{
16563# ifdef FEAT_RELTIME
16564 proftime_T tm;
16565# endif
16566
16567 rettv->v_type = VAR_FLOAT;
16568 rettv->vval.v_float = 0;
16569# ifdef FEAT_RELTIME
16570 if (list2proftime(&argvars[0], &tm) == OK)
16571 rettv->vval.v_float = profile_float(&tm);
16572# endif
16573}
16574#endif
16575
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016576/*
16577 * "reltimestr()" function
16578 */
16579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016580f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016581{
16582#ifdef FEAT_RELTIME
16583 proftime_T tm;
16584#endif
16585
16586 rettv->v_type = VAR_STRING;
16587 rettv->vval.v_string = NULL;
16588#ifdef FEAT_RELTIME
16589 if (list2proftime(&argvars[0], &tm) == OK)
16590 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16591#endif
16592}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016593
Bram Moolenaar0d660222005-01-07 21:51:51 +000016594#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016595static void make_connection(void);
16596static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016597
16598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016599make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016600{
16601 if (X_DISPLAY == NULL
16602# ifdef FEAT_GUI
16603 && !gui.in_use
16604# endif
16605 )
16606 {
16607 x_force_connect = TRUE;
16608 setup_term_clip();
16609 x_force_connect = FALSE;
16610 }
16611}
16612
16613 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016614check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016615{
16616 make_connection();
16617 if (X_DISPLAY == NULL)
16618 {
16619 EMSG(_("E240: No connection to Vim server"));
16620 return FAIL;
16621 }
16622 return OK;
16623}
16624#endif
16625
16626#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016627static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016628
16629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016630remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016631{
16632 char_u *server_name;
16633 char_u *keys;
16634 char_u *r = NULL;
16635 char_u buf[NUMBUFLEN];
16636# ifdef WIN32
16637 HWND w;
16638# else
16639 Window w;
16640# endif
16641
16642 if (check_restricted() || check_secure())
16643 return;
16644
16645# ifdef FEAT_X11
16646 if (check_connection() == FAIL)
16647 return;
16648# endif
16649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016650 server_name = get_tv_string_chk(&argvars[0]);
16651 if (server_name == NULL)
16652 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016653 keys = get_tv_string_buf(&argvars[1], buf);
16654# ifdef WIN32
16655 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16656# else
16657 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16658 < 0)
16659# endif
16660 {
16661 if (r != NULL)
16662 EMSG(r); /* sending worked but evaluation failed */
16663 else
16664 EMSG2(_("E241: Unable to send to %s"), server_name);
16665 return;
16666 }
16667
16668 rettv->vval.v_string = r;
16669
16670 if (argvars[2].v_type != VAR_UNKNOWN)
16671 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016672 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016673 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016674 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016675
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016676 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016677 v.di_tv.v_type = VAR_STRING;
16678 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016679 idvar = get_tv_string_chk(&argvars[2]);
16680 if (idvar != NULL)
16681 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016682 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016683 }
16684}
16685#endif
16686
16687/*
16688 * "remote_expr()" function
16689 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016691f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016692{
16693 rettv->v_type = VAR_STRING;
16694 rettv->vval.v_string = NULL;
16695#ifdef FEAT_CLIENTSERVER
16696 remote_common(argvars, rettv, TRUE);
16697#endif
16698}
16699
16700/*
16701 * "remote_foreground()" function
16702 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016704f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016705{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016706#ifdef FEAT_CLIENTSERVER
16707# ifdef WIN32
16708 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016709 {
16710 char_u *server_name = get_tv_string_chk(&argvars[0]);
16711
16712 if (server_name != NULL)
16713 serverForeground(server_name);
16714 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016715# else
16716 /* Send a foreground() expression to the server. */
16717 argvars[1].v_type = VAR_STRING;
16718 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16719 argvars[2].v_type = VAR_UNKNOWN;
16720 remote_common(argvars, rettv, TRUE);
16721 vim_free(argvars[1].vval.v_string);
16722# endif
16723#endif
16724}
16725
Bram Moolenaar0d660222005-01-07 21:51:51 +000016726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016727f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016728{
16729#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016731 char_u *s = NULL;
16732# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016733 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016734# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016735 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016736
16737 if (check_restricted() || check_secure())
16738 {
16739 rettv->vval.v_number = -1;
16740 return;
16741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 serverid = get_tv_string_chk(&argvars[0]);
16743 if (serverid == NULL)
16744 {
16745 rettv->vval.v_number = -1;
16746 return; /* type error; errmsg already given */
16747 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016748# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016749 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016750 if (n == 0)
16751 rettv->vval.v_number = -1;
16752 else
16753 {
16754 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16755 rettv->vval.v_number = (s != NULL);
16756 }
16757# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016758 if (check_connection() == FAIL)
16759 return;
16760
16761 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016762 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016763# endif
16764
16765 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016767 char_u *retvar;
16768
Bram Moolenaar33570922005-01-25 22:26:29 +000016769 v.di_tv.v_type = VAR_STRING;
16770 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016771 retvar = get_tv_string_chk(&argvars[1]);
16772 if (retvar != NULL)
16773 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016775 }
16776#else
16777 rettv->vval.v_number = -1;
16778#endif
16779}
16780
Bram Moolenaar0d660222005-01-07 21:51:51 +000016781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016782f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016783{
16784 char_u *r = NULL;
16785
16786#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016787 char_u *serverid = get_tv_string_chk(&argvars[0]);
16788
16789 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016790 {
16791# ifdef WIN32
16792 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016793 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016794
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016795 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016796 if (n != 0)
16797 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16798 if (r == NULL)
16799# else
16800 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016801 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016802# endif
16803 EMSG(_("E277: Unable to read a server reply"));
16804 }
16805#endif
16806 rettv->v_type = VAR_STRING;
16807 rettv->vval.v_string = r;
16808}
16809
16810/*
16811 * "remote_send()" function
16812 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016814f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016815{
16816 rettv->v_type = VAR_STRING;
16817 rettv->vval.v_string = NULL;
16818#ifdef FEAT_CLIENTSERVER
16819 remote_common(argvars, rettv, FALSE);
16820#endif
16821}
16822
16823/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016824 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016825 */
16826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016827f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016828{
Bram Moolenaar33570922005-01-25 22:26:29 +000016829 list_T *l;
16830 listitem_T *item, *item2;
16831 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016832 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016833 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016834 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016835 dict_T *d;
16836 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016837 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016838
Bram Moolenaar8c711452005-01-14 21:53:12 +000016839 if (argvars[0].v_type == VAR_DICT)
16840 {
16841 if (argvars[2].v_type != VAR_UNKNOWN)
16842 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016843 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016844 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016846 key = get_tv_string_chk(&argvars[1]);
16847 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016848 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016849 di = dict_find(d, key, -1);
16850 if (di == NULL)
16851 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016852 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16853 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016854 {
16855 *rettv = di->di_tv;
16856 init_tv(&di->di_tv);
16857 dictitem_remove(d, di);
16858 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016859 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016860 }
16861 }
16862 else if (argvars[0].v_type != VAR_LIST)
16863 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016864 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016865 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016866 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 int error = FALSE;
16868
16869 idx = get_tv_number_chk(&argvars[1], &error);
16870 if (error)
16871 ; /* type error: do nothing, errmsg already given */
16872 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016873 EMSGN(_(e_listidx), idx);
16874 else
16875 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016876 if (argvars[2].v_type == VAR_UNKNOWN)
16877 {
16878 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016879 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016880 *rettv = item->li_tv;
16881 vim_free(item);
16882 }
16883 else
16884 {
16885 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016886 end = get_tv_number_chk(&argvars[2], &error);
16887 if (error)
16888 ; /* type error: do nothing */
16889 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016890 EMSGN(_(e_listidx), end);
16891 else
16892 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016893 int cnt = 0;
16894
16895 for (li = item; li != NULL; li = li->li_next)
16896 {
16897 ++cnt;
16898 if (li == item2)
16899 break;
16900 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016901 if (li == NULL) /* didn't find "item2" after "item" */
16902 EMSG(_(e_invrange));
16903 else
16904 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016905 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016906 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016907 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016908 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016909 l->lv_first = item;
16910 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016911 item->li_prev = NULL;
16912 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016913 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016914 }
16915 }
16916 }
16917 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016918 }
16919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920}
16921
16922/*
16923 * "rename({from}, {to})" function
16924 */
16925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016926f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927{
16928 char_u buf[NUMBUFLEN];
16929
16930 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016931 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016933 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16934 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935}
16936
16937/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016938 * "repeat()" function
16939 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016941f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016942{
16943 char_u *p;
16944 int n;
16945 int slen;
16946 int len;
16947 char_u *r;
16948 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016949
16950 n = get_tv_number(&argvars[1]);
16951 if (argvars[0].v_type == VAR_LIST)
16952 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016953 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016954 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016955 if (list_extend(rettv->vval.v_list,
16956 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016957 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016958 }
16959 else
16960 {
16961 p = get_tv_string(&argvars[0]);
16962 rettv->v_type = VAR_STRING;
16963 rettv->vval.v_string = NULL;
16964
16965 slen = (int)STRLEN(p);
16966 len = slen * n;
16967 if (len <= 0)
16968 return;
16969
16970 r = alloc(len + 1);
16971 if (r != NULL)
16972 {
16973 for (i = 0; i < n; i++)
16974 mch_memmove(r + i * slen, p, (size_t)slen);
16975 r[len] = NUL;
16976 }
16977
16978 rettv->vval.v_string = r;
16979 }
16980}
16981
16982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 * "resolve()" function
16984 */
16985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016986f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016987{
16988 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016989#ifdef HAVE_READLINK
16990 char_u *buf = NULL;
16991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016993 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994#ifdef FEAT_SHORTCUT
16995 {
16996 char_u *v = NULL;
16997
16998 v = mch_resolve_shortcut(p);
16999 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017000 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017001 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017002 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 }
17004#else
17005# ifdef HAVE_READLINK
17006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 char_u *cpy;
17008 int len;
17009 char_u *remain = NULL;
17010 char_u *q;
17011 int is_relative_to_current = FALSE;
17012 int has_trailing_pathsep = FALSE;
17013 int limit = 100;
17014
17015 p = vim_strsave(p);
17016
17017 if (p[0] == '.' && (vim_ispathsep(p[1])
17018 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17019 is_relative_to_current = TRUE;
17020
17021 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017022 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017025 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027
17028 q = getnextcomp(p);
17029 if (*q != NUL)
17030 {
17031 /* Separate the first path component in "p", and keep the
17032 * remainder (beginning with the path separator). */
17033 remain = vim_strsave(q - 1);
17034 q[-1] = NUL;
17035 }
17036
Bram Moolenaard9462e32011-04-11 21:35:11 +020017037 buf = alloc(MAXPATHL + 1);
17038 if (buf == NULL)
17039 goto fail;
17040
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 for (;;)
17042 {
17043 for (;;)
17044 {
17045 len = readlink((char *)p, (char *)buf, MAXPATHL);
17046 if (len <= 0)
17047 break;
17048 buf[len] = NUL;
17049
17050 if (limit-- == 0)
17051 {
17052 vim_free(p);
17053 vim_free(remain);
17054 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017055 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 goto fail;
17057 }
17058
17059 /* Ensure that the result will have a trailing path separator
17060 * if the argument has one. */
17061 if (remain == NULL && has_trailing_pathsep)
17062 add_pathsep(buf);
17063
17064 /* Separate the first path component in the link value and
17065 * concatenate the remainders. */
17066 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17067 if (*q != NUL)
17068 {
17069 if (remain == NULL)
17070 remain = vim_strsave(q - 1);
17071 else
17072 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017073 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 if (cpy != NULL)
17075 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 vim_free(remain);
17077 remain = cpy;
17078 }
17079 }
17080 q[-1] = NUL;
17081 }
17082
17083 q = gettail(p);
17084 if (q > p && *q == NUL)
17085 {
17086 /* Ignore trailing path separator. */
17087 q[-1] = NUL;
17088 q = gettail(p);
17089 }
17090 if (q > p && !mch_isFullName(buf))
17091 {
17092 /* symlink is relative to directory of argument */
17093 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17094 if (cpy != NULL)
17095 {
17096 STRCPY(cpy, p);
17097 STRCPY(gettail(cpy), buf);
17098 vim_free(p);
17099 p = cpy;
17100 }
17101 }
17102 else
17103 {
17104 vim_free(p);
17105 p = vim_strsave(buf);
17106 }
17107 }
17108
17109 if (remain == NULL)
17110 break;
17111
17112 /* Append the first path component of "remain" to "p". */
17113 q = getnextcomp(remain + 1);
17114 len = q - remain - (*q != NUL);
17115 cpy = vim_strnsave(p, STRLEN(p) + len);
17116 if (cpy != NULL)
17117 {
17118 STRNCAT(cpy, remain, len);
17119 vim_free(p);
17120 p = cpy;
17121 }
17122 /* Shorten "remain". */
17123 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017124 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 else
17126 {
17127 vim_free(remain);
17128 remain = NULL;
17129 }
17130 }
17131
17132 /* If the result is a relative path name, make it explicitly relative to
17133 * the current directory if and only if the argument had this form. */
17134 if (!vim_ispathsep(*p))
17135 {
17136 if (is_relative_to_current
17137 && *p != NUL
17138 && !(p[0] == '.'
17139 && (p[1] == NUL
17140 || vim_ispathsep(p[1])
17141 || (p[1] == '.'
17142 && (p[2] == NUL
17143 || vim_ispathsep(p[2]))))))
17144 {
17145 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017146 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 if (cpy != NULL)
17148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 vim_free(p);
17150 p = cpy;
17151 }
17152 }
17153 else if (!is_relative_to_current)
17154 {
17155 /* Strip leading "./". */
17156 q = p;
17157 while (q[0] == '.' && vim_ispathsep(q[1]))
17158 q += 2;
17159 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017160 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017161 }
17162 }
17163
17164 /* Ensure that the result will have no trailing path separator
17165 * if the argument had none. But keep "/" or "//". */
17166 if (!has_trailing_pathsep)
17167 {
17168 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017169 if (after_pathsep(p, q))
17170 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 }
17172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017173 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 }
17175# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017176 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177# endif
17178#endif
17179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017180 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017181
17182#ifdef HAVE_READLINK
17183fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017184 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017186 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187}
17188
17189/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017190 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 */
17192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017193f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194{
Bram Moolenaar33570922005-01-25 22:26:29 +000017195 list_T *l;
17196 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198 if (argvars[0].v_type != VAR_LIST)
17199 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017200 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017201 && !tv_check_lock(l->lv_lock,
17202 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017203 {
17204 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017205 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017206 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017207 while (li != NULL)
17208 {
17209 ni = li->li_prev;
17210 list_append(l, li);
17211 li = ni;
17212 }
17213 rettv->vval.v_list = l;
17214 rettv->v_type = VAR_LIST;
17215 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017216 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218}
17219
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017220#define SP_NOMOVE 0x01 /* don't move cursor */
17221#define SP_REPEAT 0x02 /* repeat to find outer pair */
17222#define SP_RETCOUNT 0x04 /* return matchcount */
17223#define SP_SETPCMARK 0x08 /* set previous context mark */
17224#define SP_START 0x10 /* accept match at start position */
17225#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17226#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017227#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017228
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017229static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017230
17231/*
17232 * Get flags for a search function.
17233 * Possibly sets "p_ws".
17234 * Returns BACKWARD, FORWARD or zero (for an error).
17235 */
17236 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017237get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017238{
17239 int dir = FORWARD;
17240 char_u *flags;
17241 char_u nbuf[NUMBUFLEN];
17242 int mask;
17243
17244 if (varp->v_type != VAR_UNKNOWN)
17245 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017246 flags = get_tv_string_buf_chk(varp, nbuf);
17247 if (flags == NULL)
17248 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249 while (*flags != NUL)
17250 {
17251 switch (*flags)
17252 {
17253 case 'b': dir = BACKWARD; break;
17254 case 'w': p_ws = TRUE; break;
17255 case 'W': p_ws = FALSE; break;
17256 default: mask = 0;
17257 if (flagsp != NULL)
17258 switch (*flags)
17259 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017260 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017261 case 'e': mask = SP_END; break;
17262 case 'm': mask = SP_RETCOUNT; break;
17263 case 'n': mask = SP_NOMOVE; break;
17264 case 'p': mask = SP_SUBPAT; break;
17265 case 'r': mask = SP_REPEAT; break;
17266 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017267 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017268 }
17269 if (mask == 0)
17270 {
17271 EMSG2(_(e_invarg2), flags);
17272 dir = 0;
17273 }
17274 else
17275 *flagsp |= mask;
17276 }
17277 if (dir == 0)
17278 break;
17279 ++flags;
17280 }
17281 }
17282 return dir;
17283}
17284
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017286 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017288 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017289search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017291 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 char_u *pat;
17293 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017294 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295 int save_p_ws = p_ws;
17296 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017297 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017298 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017299 proftime_T tm;
17300#ifdef FEAT_RELTIME
17301 long time_limit = 0;
17302#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017303 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017304 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017306 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017307 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017308 if (dir == 0)
17309 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017310 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017311 if (flags & SP_START)
17312 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017313 if (flags & SP_END)
17314 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017315 if (flags & SP_COLUMN)
17316 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017317
Bram Moolenaar76929292008-01-06 19:07:36 +000017318 /* Optional arguments: line number to stop searching and timeout. */
17319 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017320 {
17321 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17322 if (lnum_stop < 0)
17323 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017324#ifdef FEAT_RELTIME
17325 if (argvars[3].v_type != VAR_UNKNOWN)
17326 {
17327 time_limit = get_tv_number_chk(&argvars[3], NULL);
17328 if (time_limit < 0)
17329 goto theend;
17330 }
17331#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017332 }
17333
Bram Moolenaar76929292008-01-06 19:07:36 +000017334#ifdef FEAT_RELTIME
17335 /* Set the time limit, if there is one. */
17336 profile_setlimit(time_limit, &tm);
17337#endif
17338
Bram Moolenaar231334e2005-07-25 20:46:57 +000017339 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017340 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017341 * Check to make sure only those flags are set.
17342 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17343 * flags cannot be set. Check for that condition also.
17344 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017345 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017346 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017348 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017349 goto theend;
17350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017352 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017353 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017354 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017355 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017357 if (flags & SP_SUBPAT)
17358 retval = subpatnum;
17359 else
17360 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017361 if (flags & SP_SETPCMARK)
17362 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017364 if (match_pos != NULL)
17365 {
17366 /* Store the match cursor position */
17367 match_pos->lnum = pos.lnum;
17368 match_pos->col = pos.col + 1;
17369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370 /* "/$" will put the cursor after the end of the line, may need to
17371 * correct that here */
17372 check_cursor();
17373 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017374
17375 /* If 'n' flag is used: restore cursor position. */
17376 if (flags & SP_NOMOVE)
17377 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017378 else
17379 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017380theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017382
17383 return retval;
17384}
17385
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017386#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017387
17388/*
17389 * round() is not in C90, use ceil() or floor() instead.
17390 */
17391 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017392vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017393{
17394 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17395}
17396
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017397/*
17398 * "round({float})" function
17399 */
17400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017401f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017402{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017403 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017404
17405 rettv->v_type = VAR_FLOAT;
17406 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017407 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017408 else
17409 rettv->vval.v_float = 0.0;
17410}
17411#endif
17412
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017413/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017414 * "screenattr()" function
17415 */
17416 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017417f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017418{
17419 int row;
17420 int col;
17421 int c;
17422
17423 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17424 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17425 if (row < 0 || row >= screen_Rows
17426 || col < 0 || col >= screen_Columns)
17427 c = -1;
17428 else
17429 c = ScreenAttrs[LineOffset[row] + col];
17430 rettv->vval.v_number = c;
17431}
17432
17433/*
17434 * "screenchar()" function
17435 */
17436 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017437f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017438{
17439 int row;
17440 int col;
17441 int off;
17442 int c;
17443
17444 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17445 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17446 if (row < 0 || row >= screen_Rows
17447 || col < 0 || col >= screen_Columns)
17448 c = -1;
17449 else
17450 {
17451 off = LineOffset[row] + col;
17452#ifdef FEAT_MBYTE
17453 if (enc_utf8 && ScreenLinesUC[off] != 0)
17454 c = ScreenLinesUC[off];
17455 else
17456#endif
17457 c = ScreenLines[off];
17458 }
17459 rettv->vval.v_number = c;
17460}
17461
17462/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017463 * "screencol()" function
17464 *
17465 * First column is 1 to be consistent with virtcol().
17466 */
17467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017468f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017469{
17470 rettv->vval.v_number = screen_screencol() + 1;
17471}
17472
17473/*
17474 * "screenrow()" function
17475 */
17476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017477f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017478{
17479 rettv->vval.v_number = screen_screenrow() + 1;
17480}
17481
17482/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017483 * "search()" function
17484 */
17485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017486f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017487{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017488 int flags = 0;
17489
17490 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491}
17492
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017494 * "searchdecl()" function
17495 */
17496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017497f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017498{
17499 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017500 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017501 int error = FALSE;
17502 char_u *name;
17503
17504 rettv->vval.v_number = 1; /* default: FAIL */
17505
17506 name = get_tv_string_chk(&argvars[0]);
17507 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017508 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017509 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017510 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17511 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17512 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017513 if (!error && name != NULL)
17514 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017515 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017516}
17517
17518/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017519 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017521 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017522searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523{
17524 char_u *spat, *mpat, *epat;
17525 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 int dir;
17528 int flags = 0;
17529 char_u nbuf1[NUMBUFLEN];
17530 char_u nbuf2[NUMBUFLEN];
17531 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017532 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017533 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017534 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017537 spat = get_tv_string_chk(&argvars[0]);
17538 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17539 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17540 if (spat == NULL || mpat == NULL || epat == NULL)
17541 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542
Bram Moolenaar071d4272004-06-13 20:20:40 +000017543 /* Handle the optional fourth argument: flags */
17544 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017545 if (dir == 0)
17546 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017547
17548 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017549 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17550 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017551 if ((flags & (SP_END | SP_SUBPAT)) != 0
17552 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017553 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017554 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017555 goto theend;
17556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017558 /* Using 'r' implies 'W', otherwise it doesn't work. */
17559 if (flags & SP_REPEAT)
17560 p_ws = FALSE;
17561
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017562 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017563 if (argvars[3].v_type == VAR_UNKNOWN
17564 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565 skip = (char_u *)"";
17566 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017567 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017568 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017569 if (argvars[5].v_type != VAR_UNKNOWN)
17570 {
17571 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17572 if (lnum_stop < 0)
17573 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017574#ifdef FEAT_RELTIME
17575 if (argvars[6].v_type != VAR_UNKNOWN)
17576 {
17577 time_limit = get_tv_number_chk(&argvars[6], NULL);
17578 if (time_limit < 0)
17579 goto theend;
17580 }
17581#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017582 }
17583 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017584 if (skip == NULL)
17585 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017587 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017588 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017589
17590theend:
17591 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017592
17593 return retval;
17594}
17595
17596/*
17597 * "searchpair()" function
17598 */
17599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017600f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017601{
17602 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17603}
17604
17605/*
17606 * "searchpairpos()" function
17607 */
17608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017609f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017610{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017611 pos_T match_pos;
17612 int lnum = 0;
17613 int col = 0;
17614
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017615 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017616 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017617
17618 if (searchpair_cmn(argvars, &match_pos) > 0)
17619 {
17620 lnum = match_pos.lnum;
17621 col = match_pos.col;
17622 }
17623
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017624 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17625 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017626}
17627
17628/*
17629 * Search for a start/middle/end thing.
17630 * Used by searchpair(), see its documentation for the details.
17631 * Returns 0 or -1 for no match,
17632 */
17633 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017634do_searchpair(
17635 char_u *spat, /* start pattern */
17636 char_u *mpat, /* middle pattern */
17637 char_u *epat, /* end pattern */
17638 int dir, /* BACKWARD or FORWARD */
17639 char_u *skip, /* skip expression */
17640 int flags, /* SP_SETPCMARK and other SP_ values */
17641 pos_T *match_pos,
17642 linenr_T lnum_stop, /* stop at this line if not zero */
17643 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017644{
17645 char_u *save_cpo;
17646 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17647 long retval = 0;
17648 pos_T pos;
17649 pos_T firstpos;
17650 pos_T foundpos;
17651 pos_T save_cursor;
17652 pos_T save_pos;
17653 int n;
17654 int r;
17655 int nest = 1;
17656 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017657 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017658 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017659
17660 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17661 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017662 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017663
Bram Moolenaar76929292008-01-06 19:07:36 +000017664#ifdef FEAT_RELTIME
17665 /* Set the time limit, if there is one. */
17666 profile_setlimit(time_limit, &tm);
17667#endif
17668
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017669 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17670 * start/middle/end (pat3, for the top pair). */
17671 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17672 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17673 if (pat2 == NULL || pat3 == NULL)
17674 goto theend;
17675 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17676 if (*mpat == NUL)
17677 STRCPY(pat3, pat2);
17678 else
17679 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17680 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017681 if (flags & SP_START)
17682 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017683
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 save_cursor = curwin->w_cursor;
17685 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017686 clearpos(&firstpos);
17687 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688 pat = pat3;
17689 for (;;)
17690 {
17691 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017692 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17694 /* didn't find it or found the first match again: FAIL */
17695 break;
17696
17697 if (firstpos.lnum == 0)
17698 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017699 if (equalpos(pos, foundpos))
17700 {
17701 /* Found the same position again. Can happen with a pattern that
17702 * has "\zs" at the end and searching backwards. Advance one
17703 * character and try again. */
17704 if (dir == BACKWARD)
17705 decl(&pos);
17706 else
17707 incl(&pos);
17708 }
17709 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017711 /* clear the start flag to avoid getting stuck here */
17712 options &= ~SEARCH_START;
17713
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 /* If the skip pattern matches, ignore this match. */
17715 if (*skip != NUL)
17716 {
17717 save_pos = curwin->w_cursor;
17718 curwin->w_cursor = pos;
17719 r = eval_to_bool(skip, &err, NULL, FALSE);
17720 curwin->w_cursor = save_pos;
17721 if (err)
17722 {
17723 /* Evaluating {skip} caused an error, break here. */
17724 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017725 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 break;
17727 }
17728 if (r)
17729 continue;
17730 }
17731
17732 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17733 {
17734 /* Found end when searching backwards or start when searching
17735 * forward: nested pair. */
17736 ++nest;
17737 pat = pat2; /* nested, don't search for middle */
17738 }
17739 else
17740 {
17741 /* Found end when searching forward or start when searching
17742 * backward: end of (nested) pair; or found middle in outer pair. */
17743 if (--nest == 1)
17744 pat = pat3; /* outer level, search for middle */
17745 }
17746
17747 if (nest == 0)
17748 {
17749 /* Found the match: return matchcount or line number. */
17750 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017751 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017753 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017754 if (flags & SP_SETPCMARK)
17755 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 curwin->w_cursor = pos;
17757 if (!(flags & SP_REPEAT))
17758 break;
17759 nest = 1; /* search for next unmatched */
17760 }
17761 }
17762
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017763 if (match_pos != NULL)
17764 {
17765 /* Store the match cursor position */
17766 match_pos->lnum = curwin->w_cursor.lnum;
17767 match_pos->col = curwin->w_cursor.col + 1;
17768 }
17769
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017771 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772 curwin->w_cursor = save_cursor;
17773
17774theend:
17775 vim_free(pat2);
17776 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017777 if (p_cpo == empty_option)
17778 p_cpo = save_cpo;
17779 else
17780 /* Darn, evaluating the {skip} expression changed the value. */
17781 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017782
17783 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784}
17785
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017786/*
17787 * "searchpos()" function
17788 */
17789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017790f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017791{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017792 pos_T match_pos;
17793 int lnum = 0;
17794 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017795 int n;
17796 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017797
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017798 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017799 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017800
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017801 n = search_cmn(argvars, &match_pos, &flags);
17802 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017803 {
17804 lnum = match_pos.lnum;
17805 col = match_pos.col;
17806 }
17807
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017808 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17809 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017810 if (flags & SP_SUBPAT)
17811 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017812}
17813
Bram Moolenaar0d660222005-01-07 21:51:51 +000017814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017815f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017817#ifdef FEAT_CLIENTSERVER
17818 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017819 char_u *server = get_tv_string_chk(&argvars[0]);
17820 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017821
Bram Moolenaar0d660222005-01-07 21:51:51 +000017822 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017823 if (server == NULL || reply == NULL)
17824 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017825 if (check_restricted() || check_secure())
17826 return;
17827# ifdef FEAT_X11
17828 if (check_connection() == FAIL)
17829 return;
17830# endif
17831
17832 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017834 EMSG(_("E258: Unable to send to client"));
17835 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017836 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017837 rettv->vval.v_number = 0;
17838#else
17839 rettv->vval.v_number = -1;
17840#endif
17841}
17842
Bram Moolenaar0d660222005-01-07 21:51:51 +000017843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017844f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017845{
17846 char_u *r = NULL;
17847
17848#ifdef FEAT_CLIENTSERVER
17849# ifdef WIN32
17850 r = serverGetVimNames();
17851# else
17852 make_connection();
17853 if (X_DISPLAY != NULL)
17854 r = serverGetVimNames(X_DISPLAY);
17855# endif
17856#endif
17857 rettv->v_type = VAR_STRING;
17858 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859}
17860
17861/*
17862 * "setbufvar()" function
17863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017865f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866{
17867 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017870 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 char_u nbuf[NUMBUFLEN];
17872
17873 if (check_restricted() || check_secure())
17874 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017875 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17876 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017877 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878 varp = &argvars[2];
17879
17880 if (buf != NULL && varname != NULL && varp != NULL)
17881 {
17882 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884
17885 if (*varname == '&')
17886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017887 long numval;
17888 char_u *strval;
17889 int error = FALSE;
17890
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017892 numval = get_tv_number_chk(varp, &error);
17893 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017894 if (!error && strval != NULL)
17895 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 }
17897 else
17898 {
17899 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17900 if (bufvarname != NULL)
17901 {
17902 STRCPY(bufvarname, "b:");
17903 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017904 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 vim_free(bufvarname);
17906 }
17907 }
17908
17909 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912}
17913
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017915f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017916{
17917 dict_T *d;
17918 dictitem_T *di;
17919 char_u *csearch;
17920
17921 if (argvars[0].v_type != VAR_DICT)
17922 {
17923 EMSG(_(e_dictreq));
17924 return;
17925 }
17926
17927 if ((d = argvars[0].vval.v_dict) != NULL)
17928 {
17929 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17930 if (csearch != NULL)
17931 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017932#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017933 if (enc_utf8)
17934 {
17935 int pcc[MAX_MCO];
17936 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017937
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017938 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17939 }
17940 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017941#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017942 set_last_csearch(PTR2CHAR(csearch),
17943 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017944 }
17945
17946 di = dict_find(d, (char_u *)"forward", -1);
17947 if (di != NULL)
17948 set_csearch_direction(get_tv_number(&di->di_tv)
17949 ? FORWARD : BACKWARD);
17950
17951 di = dict_find(d, (char_u *)"until", -1);
17952 if (di != NULL)
17953 set_csearch_until(!!get_tv_number(&di->di_tv));
17954 }
17955}
17956
Bram Moolenaar071d4272004-06-13 20:20:40 +000017957/*
17958 * "setcmdpos()" function
17959 */
17960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017961f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017962{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017963 int pos = (int)get_tv_number(&argvars[0]) - 1;
17964
17965 if (pos >= 0)
17966 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967}
17968
17969/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017970 * "setfperm({fname}, {mode})" function
17971 */
17972 static void
17973f_setfperm(typval_T *argvars, typval_T *rettv)
17974{
17975 char_u *fname;
17976 char_u modebuf[NUMBUFLEN];
17977 char_u *mode_str;
17978 int i;
17979 int mask;
17980 int mode = 0;
17981
17982 rettv->vval.v_number = 0;
17983 fname = get_tv_string_chk(&argvars[0]);
17984 if (fname == NULL)
17985 return;
17986 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17987 if (mode_str == NULL)
17988 return;
17989 if (STRLEN(mode_str) != 9)
17990 {
17991 EMSG2(_(e_invarg2), mode_str);
17992 return;
17993 }
17994
17995 mask = 1;
17996 for (i = 8; i >= 0; --i)
17997 {
17998 if (mode_str[i] != '-')
17999 mode |= mask;
18000 mask = mask << 1;
18001 }
18002 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18003}
18004
18005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 * "setline()" function
18007 */
18008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018009f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010{
18011 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018012 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018013 list_T *l = NULL;
18014 listitem_T *li = NULL;
18015 long added = 0;
18016 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018018 lnum = get_tv_lnum(&argvars[0]);
18019 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018021 l = argvars[1].vval.v_list;
18022 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018024 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018025 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018026
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018027 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018028 for (;;)
18029 {
18030 if (l != NULL)
18031 {
18032 /* list argument, get next string */
18033 if (li == NULL)
18034 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018035 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018036 li = li->li_next;
18037 }
18038
18039 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018040 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018041 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018042
18043 /* When coming here from Insert mode, sync undo, so that this can be
18044 * undone separately from what was previously inserted. */
18045 if (u_sync_once == 2)
18046 {
18047 u_sync_once = 1; /* notify that u_sync() was called */
18048 u_sync(TRUE);
18049 }
18050
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018051 if (lnum <= curbuf->b_ml.ml_line_count)
18052 {
18053 /* existing line, replace it */
18054 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18055 {
18056 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018057 if (lnum == curwin->w_cursor.lnum)
18058 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018059 rettv->vval.v_number = 0; /* OK */
18060 }
18061 }
18062 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18063 {
18064 /* lnum is one past the last line, append the line */
18065 ++added;
18066 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18067 rettv->vval.v_number = 0; /* OK */
18068 }
18069
18070 if (l == NULL) /* only one string argument */
18071 break;
18072 ++lnum;
18073 }
18074
18075 if (added > 0)
18076 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077}
18078
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018079static 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 +000018080
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018082 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018083 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018085set_qf_ll_list(
18086 win_T *wp UNUSED,
18087 typval_T *list_arg UNUSED,
18088 typval_T *action_arg UNUSED,
18089 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018090{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018091#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018092 char_u *act;
18093 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018094#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018095
Bram Moolenaar2641f772005-03-25 21:58:17 +000018096 rettv->vval.v_number = -1;
18097
18098#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018099 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018100 EMSG(_(e_listreq));
18101 else
18102 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018103 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018104
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018105 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018106 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018107 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018108 if (act == NULL)
18109 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018110 if (*act == 'a' || *act == 'r')
18111 action = *act;
18112 }
18113
Bram Moolenaar81484f42012-12-05 15:16:47 +010018114 if (l != NULL && set_errorlist(wp, l, action,
18115 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018116 rettv->vval.v_number = 0;
18117 }
18118#endif
18119}
18120
18121/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018122 * "setloclist()" function
18123 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018125f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018126{
18127 win_T *win;
18128
18129 rettv->vval.v_number = -1;
18130
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018131 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018132 if (win != NULL)
18133 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18134}
18135
18136/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018137 * "setmatches()" function
18138 */
18139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018140f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018141{
18142#ifdef FEAT_SEARCH_EXTRA
18143 list_T *l;
18144 listitem_T *li;
18145 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018146 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018147
18148 rettv->vval.v_number = -1;
18149 if (argvars[0].v_type != VAR_LIST)
18150 {
18151 EMSG(_(e_listreq));
18152 return;
18153 }
18154 if ((l = argvars[0].vval.v_list) != NULL)
18155 {
18156
18157 /* To some extent make sure that we are dealing with a list from
18158 * "getmatches()". */
18159 li = l->lv_first;
18160 while (li != NULL)
18161 {
18162 if (li->li_tv.v_type != VAR_DICT
18163 || (d = li->li_tv.vval.v_dict) == NULL)
18164 {
18165 EMSG(_(e_invarg));
18166 return;
18167 }
18168 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018169 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18170 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018171 && dict_find(d, (char_u *)"priority", -1) != NULL
18172 && dict_find(d, (char_u *)"id", -1) != NULL))
18173 {
18174 EMSG(_(e_invarg));
18175 return;
18176 }
18177 li = li->li_next;
18178 }
18179
18180 clear_matches(curwin);
18181 li = l->lv_first;
18182 while (li != NULL)
18183 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018184 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018185 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018186 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018187 char_u *group;
18188 int priority;
18189 int id;
18190 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018191
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018192 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018193 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18194 {
18195 if (s == NULL)
18196 {
18197 s = list_alloc();
18198 if (s == NULL)
18199 return;
18200 }
18201
18202 /* match from matchaddpos() */
18203 for (i = 1; i < 9; i++)
18204 {
18205 sprintf((char *)buf, (char *)"pos%d", i);
18206 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18207 {
18208 if (di->di_tv.v_type != VAR_LIST)
18209 return;
18210
18211 list_append_tv(s, &di->di_tv);
18212 s->lv_refcount++;
18213 }
18214 else
18215 break;
18216 }
18217 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018218
18219 group = get_dict_string(d, (char_u *)"group", FALSE);
18220 priority = (int)get_dict_number(d, (char_u *)"priority");
18221 id = (int)get_dict_number(d, (char_u *)"id");
18222 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18223 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18224 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018225 if (i == 0)
18226 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018227 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018228 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018229 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018230 }
18231 else
18232 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018233 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018234 list_unref(s);
18235 s = NULL;
18236 }
18237
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018238 li = li->li_next;
18239 }
18240 rettv->vval.v_number = 0;
18241 }
18242#endif
18243}
18244
18245/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018246 * "setpos()" function
18247 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018249f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018250{
18251 pos_T pos;
18252 int fnum;
18253 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018254 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018255
Bram Moolenaar08250432008-02-13 11:42:46 +000018256 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018257 name = get_tv_string_chk(argvars);
18258 if (name != NULL)
18259 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018260 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018261 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018262 if (--pos.col < 0)
18263 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018264 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018265 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018266 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018267 if (fnum == curbuf->b_fnum)
18268 {
18269 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018270 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018271 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018272 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018273 curwin->w_set_curswant = FALSE;
18274 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018275 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018276 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018277 }
18278 else
18279 EMSG(_(e_invarg));
18280 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018281 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18282 {
18283 /* set mark */
18284 if (setmark_pos(name[1], &pos, fnum) == OK)
18285 rettv->vval.v_number = 0;
18286 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018287 else
18288 EMSG(_(e_invarg));
18289 }
18290 }
18291}
18292
18293/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018294 * "setqflist()" function
18295 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018297f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018298{
18299 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18300}
18301
18302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 * "setreg()" function
18304 */
18305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018306f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307{
18308 int regname;
18309 char_u *strregname;
18310 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018311 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312 int append;
18313 char_u yank_type;
18314 long block_len;
18315
18316 block_len = -1;
18317 yank_type = MAUTO;
18318 append = FALSE;
18319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018320 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018321 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018323 if (strregname == NULL)
18324 return; /* type error; errmsg already given */
18325 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326 if (regname == 0 || regname == '@')
18327 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018329 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018331 stropt = get_tv_string_chk(&argvars[2]);
18332 if (stropt == NULL)
18333 return; /* type error */
18334 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 switch (*stropt)
18336 {
18337 case 'a': case 'A': /* append */
18338 append = TRUE;
18339 break;
18340 case 'v': case 'c': /* character-wise selection */
18341 yank_type = MCHAR;
18342 break;
18343 case 'V': case 'l': /* line-wise selection */
18344 yank_type = MLINE;
18345 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346 case 'b': case Ctrl_V: /* block-wise selection */
18347 yank_type = MBLOCK;
18348 if (VIM_ISDIGIT(stropt[1]))
18349 {
18350 ++stropt;
18351 block_len = getdigits(&stropt) - 1;
18352 --stropt;
18353 }
18354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355 }
18356 }
18357
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018358 if (argvars[1].v_type == VAR_LIST)
18359 {
18360 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018361 char_u **allocval;
18362 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018363 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018364 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018365 int len = argvars[1].vval.v_list->lv_len;
18366 listitem_T *li;
18367
Bram Moolenaar7d647822014-04-05 21:28:56 +020018368 /* First half: use for pointers to result lines; second half: use for
18369 * pointers to allocated copies. */
18370 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018371 if (lstval == NULL)
18372 return;
18373 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018374 allocval = lstval + len + 2;
18375 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018376
18377 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18378 li = li->li_next)
18379 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018380 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018381 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018382 goto free_lstval;
18383 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018384 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018385 /* Need to make a copy, next get_tv_string_buf_chk() will
18386 * overwrite the string. */
18387 strval = vim_strsave(buf);
18388 if (strval == NULL)
18389 goto free_lstval;
18390 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018391 }
18392 *curval++ = strval;
18393 }
18394 *curval++ = NULL;
18395
18396 write_reg_contents_lst(regname, lstval, -1,
18397 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018398free_lstval:
18399 while (curallocval > allocval)
18400 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018401 vim_free(lstval);
18402 }
18403 else
18404 {
18405 strval = get_tv_string_chk(&argvars[1]);
18406 if (strval == NULL)
18407 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018408 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018410 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018411 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412}
18413
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018414/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018415 * "settabvar()" function
18416 */
18417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018418f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018419{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018420#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018421 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018422 tabpage_T *tp;
18423#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018424 char_u *varname, *tabvarname;
18425 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018426
18427 rettv->vval.v_number = 0;
18428
18429 if (check_restricted() || check_secure())
18430 return;
18431
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018432#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018433 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018434#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018435 varname = get_tv_string_chk(&argvars[1]);
18436 varp = &argvars[2];
18437
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018438 if (varname != NULL && varp != NULL
18439#ifdef FEAT_WINDOWS
18440 && tp != NULL
18441#endif
18442 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018443 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018444#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018445 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018446 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018447#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018448
18449 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18450 if (tabvarname != NULL)
18451 {
18452 STRCPY(tabvarname, "t:");
18453 STRCPY(tabvarname + 2, varname);
18454 set_var(tabvarname, varp, TRUE);
18455 vim_free(tabvarname);
18456 }
18457
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018458#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018459 /* Restore current tabpage */
18460 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018461 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018462#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018463 }
18464}
18465
18466/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018467 * "settabwinvar()" function
18468 */
18469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018470f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018471{
18472 setwinvar(argvars, rettv, 1);
18473}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474
18475/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018476 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018479f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018481 setwinvar(argvars, rettv, 0);
18482}
18483
18484/*
18485 * "setwinvar()" and "settabwinvar()" functions
18486 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018487
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018489setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018490{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 win_T *win;
18492#ifdef FEAT_WINDOWS
18493 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018494 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018495 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496#endif
18497 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018498 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018500 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501
18502 if (check_restricted() || check_secure())
18503 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018504
18505#ifdef FEAT_WINDOWS
18506 if (off == 1)
18507 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18508 else
18509 tp = curtab;
18510#endif
18511 win = find_win_by_nr(&argvars[off], tp);
18512 varname = get_tv_string_chk(&argvars[off + 1]);
18513 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018514
18515 if (win != NULL && varname != NULL && varp != NULL)
18516 {
18517#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018518 need_switch_win = !(tp == curtab && win == curwin);
18519 if (!need_switch_win
18520 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018523 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018525 long numval;
18526 char_u *strval;
18527 int error = FALSE;
18528
18529 ++varname;
18530 numval = get_tv_number_chk(varp, &error);
18531 strval = get_tv_string_buf_chk(varp, nbuf);
18532 if (!error && strval != NULL)
18533 set_option_value(varname, numval, strval, OPT_LOCAL);
18534 }
18535 else
18536 {
18537 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18538 if (winvarname != NULL)
18539 {
18540 STRCPY(winvarname, "w:");
18541 STRCPY(winvarname + 2, varname);
18542 set_var(winvarname, varp, TRUE);
18543 vim_free(winvarname);
18544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 }
18546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018548 if (need_switch_win)
18549 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550#endif
18551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552}
18553
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018554#ifdef FEAT_CRYPT
18555/*
18556 * "sha256({string})" function
18557 */
18558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018559f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018560{
18561 char_u *p;
18562
18563 p = get_tv_string(&argvars[0]);
18564 rettv->vval.v_string = vim_strsave(
18565 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18566 rettv->v_type = VAR_STRING;
18567}
18568#endif /* FEAT_CRYPT */
18569
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018571 * "shellescape({string})" function
18572 */
18573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018574f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018575{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018576 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018577 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018578 rettv->v_type = VAR_STRING;
18579}
18580
18581/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018582 * shiftwidth() function
18583 */
18584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018585f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018586{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018587 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018588}
18589
18590/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018591 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018592 */
18593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018594f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018596 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597
Bram Moolenaar0d660222005-01-07 21:51:51 +000018598 p = get_tv_string(&argvars[0]);
18599 rettv->vval.v_string = vim_strsave(p);
18600 simplify_filename(rettv->vval.v_string); /* simplify in place */
18601 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602}
18603
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018604#ifdef FEAT_FLOAT
18605/*
18606 * "sin()" function
18607 */
18608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018609f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018610{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018611 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018612
18613 rettv->v_type = VAR_FLOAT;
18614 if (get_float_arg(argvars, &f) == OK)
18615 rettv->vval.v_float = sin(f);
18616 else
18617 rettv->vval.v_float = 0.0;
18618}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018619
18620/*
18621 * "sinh()" function
18622 */
18623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018624f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018625{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018626 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018627
18628 rettv->v_type = VAR_FLOAT;
18629 if (get_float_arg(argvars, &f) == OK)
18630 rettv->vval.v_float = sinh(f);
18631 else
18632 rettv->vval.v_float = 0.0;
18633}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018634#endif
18635
Bram Moolenaar0d660222005-01-07 21:51:51 +000018636static int
18637#ifdef __BORLANDC__
18638 _RTLENTRYF
18639#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018640 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018641static int
18642#ifdef __BORLANDC__
18643 _RTLENTRYF
18644#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018645 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018646
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018647/* struct used in the array that's given to qsort() */
18648typedef struct
18649{
18650 listitem_T *item;
18651 int idx;
18652} sortItem_T;
18653
Bram Moolenaar0b962472016-02-22 22:51:33 +010018654/* struct storing information about current sort */
18655typedef struct
18656{
18657 int item_compare_ic;
18658 int item_compare_numeric;
18659 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018660#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018661 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018662#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018663 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018664 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018665 dict_T *item_compare_selfdict;
18666 int item_compare_func_err;
18667 int item_compare_keep_zero;
18668} sortinfo_T;
18669static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018670static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018671#define ITEM_COMPARE_FAIL 999
18672
Bram Moolenaar071d4272004-06-13 20:20:40 +000018673/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018674 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018676 static int
18677#ifdef __BORLANDC__
18678_RTLENTRYF
18679#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018680item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018682 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018683 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018684 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018685 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018686 int res;
18687 char_u numbuf1[NUMBUFLEN];
18688 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018690 si1 = (sortItem_T *)s1;
18691 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018692 tv1 = &si1->item->li_tv;
18693 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018694
Bram Moolenaar0b962472016-02-22 22:51:33 +010018695 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018696 {
18697 long v1 = get_tv_number(tv1);
18698 long v2 = get_tv_number(tv2);
18699
18700 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18701 }
18702
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018703#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018704 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018705 {
18706 float_T v1 = get_tv_float(tv1);
18707 float_T v2 = get_tv_float(tv2);
18708
18709 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18710 }
18711#endif
18712
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018713 /* tv2string() puts quotes around a string and allocates memory. Don't do
18714 * that for string variables. Use a single quote when comparing with a
18715 * non-string to do what the docs promise. */
18716 if (tv1->v_type == VAR_STRING)
18717 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018718 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018719 p1 = (char_u *)"'";
18720 else
18721 p1 = tv1->vval.v_string;
18722 }
18723 else
18724 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18725 if (tv2->v_type == VAR_STRING)
18726 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018727 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018728 p2 = (char_u *)"'";
18729 else
18730 p2 = tv2->vval.v_string;
18731 }
18732 else
18733 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018734 if (p1 == NULL)
18735 p1 = (char_u *)"";
18736 if (p2 == NULL)
18737 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018738 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018739 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018740 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018741 res = STRICMP(p1, p2);
18742 else
18743 res = STRCMP(p1, p2);
18744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018746 {
18747 double n1, n2;
18748 n1 = strtod((char *)p1, (char **)&p1);
18749 n2 = strtod((char *)p2, (char **)&p2);
18750 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18751 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018752
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018753 /* When the result would be zero, compare the item indexes. Makes the
18754 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018755 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018756 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018757
Bram Moolenaar0d660222005-01-07 21:51:51 +000018758 vim_free(tofree1);
18759 vim_free(tofree2);
18760 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761}
18762
18763 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018764#ifdef __BORLANDC__
18765_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018767item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018768{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018769 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018770 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018772 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018773 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018774 char_u *func_name;
18775 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018777 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018778 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018779 return 0;
18780
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018781 si1 = (sortItem_T *)s1;
18782 si2 = (sortItem_T *)s2;
18783
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018784 if (partial == NULL)
18785 func_name = sortinfo->item_compare_func;
18786 else
18787 func_name = partial->pt_name;
18788
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018789 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018790 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018791 copy_tv(&si1->item->li_tv, &argv[0]);
18792 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018793
18794 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018795 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018796 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018797 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018798 clear_tv(&argv[0]);
18799 clear_tv(&argv[1]);
18800
18801 if (res == FAIL)
18802 res = ITEM_COMPARE_FAIL;
18803 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018804 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18805 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018806 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018807 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018808
18809 /* When the result would be zero, compare the pointers themselves. Makes
18810 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018811 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018812 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018813
Bram Moolenaar0d660222005-01-07 21:51:51 +000018814 return res;
18815}
18816
18817/*
18818 * "sort({list})" function
18819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018821do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822{
Bram Moolenaar33570922005-01-25 22:26:29 +000018823 list_T *l;
18824 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018825 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018826 sortinfo_T *old_sortinfo;
18827 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018828 long len;
18829 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830
Bram Moolenaar0b962472016-02-22 22:51:33 +010018831 /* Pointer to current info struct used in compare function. Save and
18832 * restore the current one for nested calls. */
18833 old_sortinfo = sortinfo;
18834 sortinfo = &info;
18835
Bram Moolenaar0d660222005-01-07 21:51:51 +000018836 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018837 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 else
18839 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018840 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018841 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018842 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18843 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018844 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018845 rettv->vval.v_list = l;
18846 rettv->v_type = VAR_LIST;
18847 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018848
Bram Moolenaar0d660222005-01-07 21:51:51 +000018849 len = list_len(l);
18850 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018851 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852
Bram Moolenaar0b962472016-02-22 22:51:33 +010018853 info.item_compare_ic = FALSE;
18854 info.item_compare_numeric = FALSE;
18855 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018856#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018857 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018858#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018859 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018860 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018861 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018862 if (argvars[1].v_type != VAR_UNKNOWN)
18863 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018864 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018865 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018866 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018867 else if (argvars[1].v_type == VAR_PARTIAL)
18868 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018869 else
18870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018871 int error = FALSE;
18872
18873 i = get_tv_number_chk(&argvars[1], &error);
18874 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018875 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018876 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018877 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018878 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018879 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018880 else if (i != 0)
18881 {
18882 EMSG(_(e_invarg));
18883 goto theend;
18884 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018885 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018886 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018887 if (*info.item_compare_func == NUL)
18888 {
18889 /* empty string means default sort */
18890 info.item_compare_func = NULL;
18891 }
18892 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018893 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018894 info.item_compare_func = NULL;
18895 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018896 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018897 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018898 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018899 info.item_compare_func = NULL;
18900 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018901 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018902#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018903 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018904 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018905 info.item_compare_func = NULL;
18906 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018907 }
18908#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018909 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018910 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018911 info.item_compare_func = NULL;
18912 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018913 }
18914 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018915 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018916
18917 if (argvars[2].v_type != VAR_UNKNOWN)
18918 {
18919 /* optional third argument: {dict} */
18920 if (argvars[2].v_type != VAR_DICT)
18921 {
18922 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018923 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018924 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018925 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018926 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018930 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018931 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018932 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933
Bram Moolenaar327aa022014-03-25 18:24:23 +010018934 i = 0;
18935 if (sort)
18936 {
18937 /* sort(): ptrs will be the list to sort */
18938 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018939 {
18940 ptrs[i].item = li;
18941 ptrs[i].idx = i;
18942 ++i;
18943 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018944
Bram Moolenaar0b962472016-02-22 22:51:33 +010018945 info.item_compare_func_err = FALSE;
18946 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018947 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018948 if ((info.item_compare_func != NULL
18949 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018950 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018951 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018952 EMSG(_("E702: Sort compare function failed"));
18953 else
18954 {
18955 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018956 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018957 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018958 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018959 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018960
Bram Moolenaar0b962472016-02-22 22:51:33 +010018961 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018962 {
18963 /* Clear the List and append the items in sorted order. */
18964 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18965 l->lv_len = 0;
18966 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018967 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018968 }
18969 }
18970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018971 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018973 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018974
18975 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018976 info.item_compare_func_err = FALSE;
18977 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018978 item_compare_func_ptr = info.item_compare_func != NULL
18979 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018980 ? item_compare2 : item_compare;
18981
18982 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18983 li = li->li_next)
18984 {
18985 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18986 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018987 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018988 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018989 {
18990 EMSG(_("E882: Uniq compare function failed"));
18991 break;
18992 }
18993 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018994
Bram Moolenaar0b962472016-02-22 22:51:33 +010018995 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018996 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018997 while (--i >= 0)
18998 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018999 li = ptrs[i].item->li_next;
19000 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019001 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019002 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019003 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019004 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019005 list_fix_watch(l, li);
19006 listitem_free(li);
19007 l->lv_len--;
19008 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019009 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019010 }
19011
19012 vim_free(ptrs);
19013 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019014theend:
19015 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019016}
19017
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019018/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019019 * "sort({list})" function
19020 */
19021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019022f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019023{
19024 do_sort_uniq(argvars, rettv, TRUE);
19025}
19026
19027/*
19028 * "uniq({list})" function
19029 */
19030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019031f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019032{
19033 do_sort_uniq(argvars, rettv, FALSE);
19034}
19035
19036/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019037 * "soundfold({word})" function
19038 */
19039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019040f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019041{
19042 char_u *s;
19043
19044 rettv->v_type = VAR_STRING;
19045 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019046#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019047 rettv->vval.v_string = eval_soundfold(s);
19048#else
19049 rettv->vval.v_string = vim_strsave(s);
19050#endif
19051}
19052
19053/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019054 * "spellbadword()" function
19055 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019056 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019057f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019058{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019059 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019060 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019061 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019062
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019063 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019064 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019065
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019066#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019067 if (argvars[0].v_type == VAR_UNKNOWN)
19068 {
19069 /* Find the start and length of the badly spelled word. */
19070 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19071 if (len != 0)
19072 word = ml_get_cursor();
19073 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019074 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019075 {
19076 char_u *str = get_tv_string_chk(&argvars[0]);
19077 int capcol = -1;
19078
19079 if (str != NULL)
19080 {
19081 /* Check the argument for spelling. */
19082 while (*str != NUL)
19083 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019084 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019085 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019086 {
19087 word = str;
19088 break;
19089 }
19090 str += len;
19091 }
19092 }
19093 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019094#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019095
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019096 list_append_string(rettv->vval.v_list, word, len);
19097 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019098 attr == HLF_SPB ? "bad" :
19099 attr == HLF_SPR ? "rare" :
19100 attr == HLF_SPL ? "local" :
19101 attr == HLF_SPC ? "caps" :
19102 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019103}
19104
19105/*
19106 * "spellsuggest()" function
19107 */
19108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019109f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019110{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019111#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019112 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019113 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019114 int maxcount;
19115 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019116 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019117 listitem_T *li;
19118 int need_capital = FALSE;
19119#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019120
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019121 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019122 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019123
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019124#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019125 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019126 {
19127 str = get_tv_string(&argvars[0]);
19128 if (argvars[1].v_type != VAR_UNKNOWN)
19129 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019130 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019131 if (maxcount <= 0)
19132 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019133 if (argvars[2].v_type != VAR_UNKNOWN)
19134 {
19135 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19136 if (typeerr)
19137 return;
19138 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019139 }
19140 else
19141 maxcount = 25;
19142
Bram Moolenaar4770d092006-01-12 23:22:24 +000019143 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019144
19145 for (i = 0; i < ga.ga_len; ++i)
19146 {
19147 str = ((char_u **)ga.ga_data)[i];
19148
19149 li = listitem_alloc();
19150 if (li == NULL)
19151 vim_free(str);
19152 else
19153 {
19154 li->li_tv.v_type = VAR_STRING;
19155 li->li_tv.v_lock = 0;
19156 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019157 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019158 }
19159 }
19160 ga_clear(&ga);
19161 }
19162#endif
19163}
19164
Bram Moolenaar0d660222005-01-07 21:51:51 +000019165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019166f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019167{
19168 char_u *str;
19169 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019170 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019171 regmatch_T regmatch;
19172 char_u patbuf[NUMBUFLEN];
19173 char_u *save_cpo;
19174 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019175 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019176 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019177 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019178
19179 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19180 save_cpo = p_cpo;
19181 p_cpo = (char_u *)"";
19182
19183 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019184 if (argvars[1].v_type != VAR_UNKNOWN)
19185 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019186 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19187 if (pat == NULL)
19188 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019189 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019190 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019191 }
19192 if (pat == NULL || *pat == NUL)
19193 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019195 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019197 if (typeerr)
19198 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019199
Bram Moolenaar0d660222005-01-07 21:51:51 +000019200 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19201 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019202 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019203 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019204 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019205 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019206 if (*str == NUL)
19207 match = FALSE; /* empty item at the end */
19208 else
19209 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019210 if (match)
19211 end = regmatch.startp[0];
19212 else
19213 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019214 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19215 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019216 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019217 if (list_append_string(rettv->vval.v_list, str,
19218 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019219 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019220 }
19221 if (!match)
19222 break;
19223 /* Advance to just after the match. */
19224 if (regmatch.endp[0] > str)
19225 col = 0;
19226 else
19227 {
19228 /* Don't get stuck at the same match. */
19229#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019230 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019231#else
19232 col = 1;
19233#endif
19234 }
19235 str = regmatch.endp[0];
19236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237
Bram Moolenaar473de612013-06-08 18:19:48 +020019238 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240
Bram Moolenaar0d660222005-01-07 21:51:51 +000019241 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242}
19243
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019244#ifdef FEAT_FLOAT
19245/*
19246 * "sqrt()" function
19247 */
19248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019249f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019250{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019251 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019252
19253 rettv->v_type = VAR_FLOAT;
19254 if (get_float_arg(argvars, &f) == OK)
19255 rettv->vval.v_float = sqrt(f);
19256 else
19257 rettv->vval.v_float = 0.0;
19258}
19259
19260/*
19261 * "str2float()" function
19262 */
19263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019264f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019265{
19266 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19267
19268 if (*p == '+')
19269 p = skipwhite(p + 1);
19270 (void)string2float(p, &rettv->vval.v_float);
19271 rettv->v_type = VAR_FLOAT;
19272}
19273#endif
19274
Bram Moolenaar2c932302006-03-18 21:42:09 +000019275/*
19276 * "str2nr()" function
19277 */
19278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019279f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019280{
19281 int base = 10;
19282 char_u *p;
19283 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019284 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019285
19286 if (argvars[1].v_type != VAR_UNKNOWN)
19287 {
19288 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019289 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019290 {
19291 EMSG(_(e_invarg));
19292 return;
19293 }
19294 }
19295
19296 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019297 if (*p == '+')
19298 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019299 switch (base)
19300 {
19301 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19302 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19303 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19304 default: what = 0;
19305 }
19306 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019307 rettv->vval.v_number = n;
19308}
19309
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310#ifdef HAVE_STRFTIME
19311/*
19312 * "strftime({format}[, {time}])" function
19313 */
19314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019315f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316{
19317 char_u result_buf[256];
19318 struct tm *curtime;
19319 time_t seconds;
19320 char_u *p;
19321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019324 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019325 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 seconds = time(NULL);
19327 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019328 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 curtime = localtime(&seconds);
19330 /* MSVC returns NULL for an invalid value of seconds. */
19331 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019332 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333 else
19334 {
19335# ifdef FEAT_MBYTE
19336 vimconv_T conv;
19337 char_u *enc;
19338
19339 conv.vc_type = CONV_NONE;
19340 enc = enc_locale();
19341 convert_setup(&conv, p_enc, enc);
19342 if (conv.vc_type != CONV_NONE)
19343 p = string_convert(&conv, p, NULL);
19344# endif
19345 if (p != NULL)
19346 (void)strftime((char *)result_buf, sizeof(result_buf),
19347 (char *)p, curtime);
19348 else
19349 result_buf[0] = NUL;
19350
19351# ifdef FEAT_MBYTE
19352 if (conv.vc_type != CONV_NONE)
19353 vim_free(p);
19354 convert_setup(&conv, enc, p_enc);
19355 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019356 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019357 else
19358# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019359 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019360
19361# ifdef FEAT_MBYTE
19362 /* Release conversion descriptors */
19363 convert_setup(&conv, NULL, NULL);
19364 vim_free(enc);
19365# endif
19366 }
19367}
19368#endif
19369
19370/*
19371 * "stridx()" function
19372 */
19373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019374f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019375{
19376 char_u buf[NUMBUFLEN];
19377 char_u *needle;
19378 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019379 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019381 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019383 needle = get_tv_string_chk(&argvars[1]);
19384 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019385 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019386 if (needle == NULL || haystack == NULL)
19387 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388
Bram Moolenaar33570922005-01-25 22:26:29 +000019389 if (argvars[2].v_type != VAR_UNKNOWN)
19390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019391 int error = FALSE;
19392
19393 start_idx = get_tv_number_chk(&argvars[2], &error);
19394 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019395 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019396 if (start_idx >= 0)
19397 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019398 }
19399
19400 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19401 if (pos != NULL)
19402 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403}
19404
19405/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019406 * "string()" function
19407 */
19408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019409f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019410{
19411 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019412 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019414 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019415 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19416 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019417 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019418 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019419 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420}
19421
19422/*
19423 * "strlen()" function
19424 */
19425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019426f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019427{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019428 rettv->vval.v_number = (varnumber_T)(STRLEN(
19429 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019430}
19431
19432/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019433 * "strchars()" function
19434 */
19435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019436f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019437{
19438 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019439 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019440#ifdef FEAT_MBYTE
19441 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019442 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019443#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019444
19445 if (argvars[1].v_type != VAR_UNKNOWN)
19446 skipcc = get_tv_number_chk(&argvars[1], NULL);
19447 if (skipcc < 0 || skipcc > 1)
19448 EMSG(_(e_invarg));
19449 else
19450 {
19451#ifdef FEAT_MBYTE
19452 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19453 while (*s != NUL)
19454 {
19455 func_mb_ptr2char_adv(&s);
19456 ++len;
19457 }
19458 rettv->vval.v_number = len;
19459#else
19460 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19461#endif
19462 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019463}
19464
19465/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019466 * "strdisplaywidth()" function
19467 */
19468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019469f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019470{
19471 char_u *s = get_tv_string(&argvars[0]);
19472 int col = 0;
19473
19474 if (argvars[1].v_type != VAR_UNKNOWN)
19475 col = get_tv_number(&argvars[1]);
19476
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019477 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019478}
19479
19480/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019481 * "strwidth()" function
19482 */
19483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019484f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019485{
19486 char_u *s = get_tv_string(&argvars[0]);
19487
19488 rettv->vval.v_number = (varnumber_T)(
19489#ifdef FEAT_MBYTE
19490 mb_string2cells(s, -1)
19491#else
19492 STRLEN(s)
19493#endif
19494 );
19495}
19496
19497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019498 * "strpart()" function
19499 */
19500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019501f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502{
19503 char_u *p;
19504 int n;
19505 int len;
19506 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019507 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 slen = (int)STRLEN(p);
19511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019512 n = get_tv_number_chk(&argvars[1], &error);
19513 if (error)
19514 len = 0;
19515 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019516 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517 else
19518 len = slen - n; /* default len: all bytes that are available. */
19519
19520 /*
19521 * Only return the overlap between the specified part and the actual
19522 * string.
19523 */
19524 if (n < 0)
19525 {
19526 len += n;
19527 n = 0;
19528 }
19529 else if (n > slen)
19530 n = slen;
19531 if (len < 0)
19532 len = 0;
19533 else if (n + len > slen)
19534 len = slen - n;
19535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019536 rettv->v_type = VAR_STRING;
19537 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538}
19539
19540/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019541 * "strridx()" function
19542 */
19543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019544f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019545{
19546 char_u buf[NUMBUFLEN];
19547 char_u *needle;
19548 char_u *haystack;
19549 char_u *rest;
19550 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019551 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019553 needle = get_tv_string_chk(&argvars[1]);
19554 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019555
19556 rettv->vval.v_number = -1;
19557 if (needle == NULL || haystack == NULL)
19558 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019559
19560 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019561 if (argvars[2].v_type != VAR_UNKNOWN)
19562 {
19563 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019564 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019565 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019566 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019567 }
19568 else
19569 end_idx = haystack_len;
19570
Bram Moolenaar0d660222005-01-07 21:51:51 +000019571 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019572 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019573 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019574 lastmatch = haystack + end_idx;
19575 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019576 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019577 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019578 for (rest = haystack; *rest != '\0'; ++rest)
19579 {
19580 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019581 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019582 break;
19583 lastmatch = rest;
19584 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019585 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019586
19587 if (lastmatch == NULL)
19588 rettv->vval.v_number = -1;
19589 else
19590 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19591}
19592
19593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019594 * "strtrans()" function
19595 */
19596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019597f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019599 rettv->v_type = VAR_STRING;
19600 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601}
19602
19603/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019604 * "submatch()" function
19605 */
19606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019607f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019608{
Bram Moolenaar41571762014-04-02 19:00:58 +020019609 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019610 int no;
19611 int retList = 0;
19612
19613 no = (int)get_tv_number_chk(&argvars[0], &error);
19614 if (error)
19615 return;
19616 error = FALSE;
19617 if (argvars[1].v_type != VAR_UNKNOWN)
19618 retList = get_tv_number_chk(&argvars[1], &error);
19619 if (error)
19620 return;
19621
19622 if (retList == 0)
19623 {
19624 rettv->v_type = VAR_STRING;
19625 rettv->vval.v_string = reg_submatch(no);
19626 }
19627 else
19628 {
19629 rettv->v_type = VAR_LIST;
19630 rettv->vval.v_list = reg_submatch_list(no);
19631 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019632}
19633
19634/*
19635 * "substitute()" function
19636 */
19637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019638f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019639{
19640 char_u patbuf[NUMBUFLEN];
19641 char_u subbuf[NUMBUFLEN];
19642 char_u flagsbuf[NUMBUFLEN];
19643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019644 char_u *str = get_tv_string_chk(&argvars[0]);
19645 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19646 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19647 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19648
Bram Moolenaar0d660222005-01-07 21:51:51 +000019649 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019650 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19651 rettv->vval.v_string = NULL;
19652 else
19653 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019654}
19655
19656/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019657 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019660f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661{
19662 int id = 0;
19663#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019664 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665 long col;
19666 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019667 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019669 lnum = get_tv_lnum(argvars); /* -1 on type error */
19670 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19671 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019673 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019674 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019675 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676#endif
19677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019678 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679}
19680
19681/*
19682 * "synIDattr(id, what [, mode])" function
19683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019685f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686{
19687 char_u *p = NULL;
19688#ifdef FEAT_SYN_HL
19689 int id;
19690 char_u *what;
19691 char_u *mode;
19692 char_u modebuf[NUMBUFLEN];
19693 int modec;
19694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019695 id = get_tv_number(&argvars[0]);
19696 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019697 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019699 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019701 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702 modec = 0; /* replace invalid with current */
19703 }
19704 else
19705 {
19706#ifdef FEAT_GUI
19707 if (gui.in_use)
19708 modec = 'g';
19709 else
19710#endif
19711 if (t_colors > 1)
19712 modec = 'c';
19713 else
19714 modec = 't';
19715 }
19716
19717
19718 switch (TOLOWER_ASC(what[0]))
19719 {
19720 case 'b':
19721 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19722 p = highlight_color(id, what, modec);
19723 else /* bold */
19724 p = highlight_has_attr(id, HL_BOLD, modec);
19725 break;
19726
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019727 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 p = highlight_color(id, what, modec);
19729 break;
19730
19731 case 'i':
19732 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19733 p = highlight_has_attr(id, HL_INVERSE, modec);
19734 else /* italic */
19735 p = highlight_has_attr(id, HL_ITALIC, modec);
19736 break;
19737
19738 case 'n': /* name */
19739 p = get_highlight_name(NULL, id - 1);
19740 break;
19741
19742 case 'r': /* reverse */
19743 p = highlight_has_attr(id, HL_INVERSE, modec);
19744 break;
19745
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019746 case 's':
19747 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19748 p = highlight_color(id, what, modec);
19749 else /* standout */
19750 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 break;
19752
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019753 case 'u':
19754 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19755 /* underline */
19756 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19757 else
19758 /* undercurl */
19759 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760 break;
19761 }
19762
19763 if (p != NULL)
19764 p = vim_strsave(p);
19765#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019766 rettv->v_type = VAR_STRING;
19767 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019768}
19769
19770/*
19771 * "synIDtrans(id)" function
19772 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019774f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775{
19776 int id;
19777
19778#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019779 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780
19781 if (id > 0)
19782 id = syn_get_final_id(id);
19783 else
19784#endif
19785 id = 0;
19786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019787 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788}
19789
19790/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019791 * "synconcealed(lnum, col)" function
19792 */
19793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019794f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019795{
19796#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19797 long lnum;
19798 long col;
19799 int syntax_flags = 0;
19800 int cchar;
19801 int matchid = 0;
19802 char_u str[NUMBUFLEN];
19803#endif
19804
19805 rettv->v_type = VAR_LIST;
19806 rettv->vval.v_list = NULL;
19807
19808#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19809 lnum = get_tv_lnum(argvars); /* -1 on type error */
19810 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19811
19812 vim_memset(str, NUL, sizeof(str));
19813
19814 if (rettv_list_alloc(rettv) != FAIL)
19815 {
19816 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19817 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19818 && curwin->w_p_cole > 0)
19819 {
19820 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19821 syntax_flags = get_syntax_info(&matchid);
19822
19823 /* get the conceal character */
19824 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19825 {
19826 cchar = syn_get_sub_char();
19827 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19828 cchar = lcs_conceal;
19829 if (cchar != NUL)
19830 {
19831# ifdef FEAT_MBYTE
19832 if (has_mbyte)
19833 (*mb_char2bytes)(cchar, str);
19834 else
19835# endif
19836 str[0] = cchar;
19837 }
19838 }
19839 }
19840
19841 list_append_number(rettv->vval.v_list,
19842 (syntax_flags & HL_CONCEAL) != 0);
19843 /* -1 to auto-determine strlen */
19844 list_append_string(rettv->vval.v_list, str, -1);
19845 list_append_number(rettv->vval.v_list, matchid);
19846 }
19847#endif
19848}
19849
19850/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019851 * "synstack(lnum, col)" function
19852 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019854f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019855{
19856#ifdef FEAT_SYN_HL
19857 long lnum;
19858 long col;
19859 int i;
19860 int id;
19861#endif
19862
19863 rettv->v_type = VAR_LIST;
19864 rettv->vval.v_list = NULL;
19865
19866#ifdef FEAT_SYN_HL
19867 lnum = get_tv_lnum(argvars); /* -1 on type error */
19868 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19869
19870 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019871 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019872 && rettv_list_alloc(rettv) != FAIL)
19873 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019874 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019875 for (i = 0; ; ++i)
19876 {
19877 id = syn_get_stack_item(i);
19878 if (id < 0)
19879 break;
19880 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19881 break;
19882 }
19883 }
19884#endif
19885}
19886
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019888get_cmd_output_as_rettv(
19889 typval_T *argvars,
19890 typval_T *rettv,
19891 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019893 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019895 char_u *infile = NULL;
19896 char_u buf[NUMBUFLEN];
19897 int err = FALSE;
19898 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019899 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019900 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019902 rettv->v_type = VAR_STRING;
19903 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019904 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019905 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019906
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019907 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019908 {
19909 /*
19910 * Write the string to a temp file, to be used for input of the shell
19911 * command.
19912 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019913 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019914 {
19915 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019916 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019917 }
19918
19919 fd = mch_fopen((char *)infile, WRITEBIN);
19920 if (fd == NULL)
19921 {
19922 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019923 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019924 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019925 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019926 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019927 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19928 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019929 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019930 else
19931 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019932 size_t len;
19933
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019934 p = get_tv_string_buf_chk(&argvars[1], buf);
19935 if (p == NULL)
19936 {
19937 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019938 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019939 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019940 len = STRLEN(p);
19941 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019942 err = TRUE;
19943 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019944 if (fclose(fd) != 0)
19945 err = TRUE;
19946 if (err)
19947 {
19948 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019949 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019950 }
19951 }
19952
Bram Moolenaar52a72462014-08-29 15:53:52 +020019953 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19954 * echoes typeahead, that messes up the display. */
19955 if (!msg_silent)
19956 flags += SHELL_COOKED;
19957
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019958 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019959 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019960 int len;
19961 listitem_T *li;
19962 char_u *s = NULL;
19963 char_u *start;
19964 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019965 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966
Bram Moolenaar52a72462014-08-29 15:53:52 +020019967 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019968 if (res == NULL)
19969 goto errret;
19970
19971 list = list_alloc();
19972 if (list == NULL)
19973 goto errret;
19974
19975 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019977 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019978 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019979 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019980 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019981
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019982 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019983 if (s == NULL)
19984 goto errret;
19985
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019986 for (p = s; start < end; ++p, ++start)
19987 *p = *start == NUL ? NL : *start;
19988 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019989
19990 li = listitem_alloc();
19991 if (li == NULL)
19992 {
19993 vim_free(s);
19994 goto errret;
19995 }
19996 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019997 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019998 li->li_tv.vval.v_string = s;
19999 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020001
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020002 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020003 rettv->v_type = VAR_LIST;
20004 rettv->vval.v_list = list;
20005 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020007 else
20008 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020009 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020010#ifdef USE_CR
20011 /* translate <CR> into <NL> */
20012 if (res != NULL)
20013 {
20014 char_u *s;
20015
20016 for (s = res; *s; ++s)
20017 {
20018 if (*s == CAR)
20019 *s = NL;
20020 }
20021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022#else
20023# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020024 /* translate <CR><NL> into <NL> */
20025 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020027 char_u *s, *d;
20028
20029 d = res;
20030 for (s = res; *s; ++s)
20031 {
20032 if (s[0] == CAR && s[1] == NL)
20033 ++s;
20034 *d++ = *s;
20035 }
20036 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038# endif
20039#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020040 rettv->vval.v_string = res;
20041 res = NULL;
20042 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020043
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020044errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020045 if (infile != NULL)
20046 {
20047 mch_remove(infile);
20048 vim_free(infile);
20049 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020050 if (res != NULL)
20051 vim_free(res);
20052 if (list != NULL)
20053 list_free(list, TRUE);
20054}
20055
20056/*
20057 * "system()" function
20058 */
20059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020060f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020061{
20062 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20063}
20064
20065/*
20066 * "systemlist()" function
20067 */
20068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020069f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020070{
20071 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072}
20073
20074/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020075 * "tabpagebuflist()" function
20076 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020078f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020079{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020080#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020081 tabpage_T *tp;
20082 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020083
20084 if (argvars[0].v_type == VAR_UNKNOWN)
20085 wp = firstwin;
20086 else
20087 {
20088 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20089 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020090 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020091 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020092 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020093 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020094 for (; wp != NULL; wp = wp->w_next)
20095 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020096 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020097 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020098 }
20099#endif
20100}
20101
20102
20103/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020104 * "tabpagenr()" function
20105 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020107f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020108{
20109 int nr = 1;
20110#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020111 char_u *arg;
20112
20113 if (argvars[0].v_type != VAR_UNKNOWN)
20114 {
20115 arg = get_tv_string_chk(&argvars[0]);
20116 nr = 0;
20117 if (arg != NULL)
20118 {
20119 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020120 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020121 else
20122 EMSG2(_(e_invexpr2), arg);
20123 }
20124 }
20125 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020126 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020127#endif
20128 rettv->vval.v_number = nr;
20129}
20130
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020131
20132#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020133static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020134
20135/*
20136 * Common code for tabpagewinnr() and winnr().
20137 */
20138 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020139get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020140{
20141 win_T *twin;
20142 int nr = 1;
20143 win_T *wp;
20144 char_u *arg;
20145
20146 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20147 if (argvar->v_type != VAR_UNKNOWN)
20148 {
20149 arg = get_tv_string_chk(argvar);
20150 if (arg == NULL)
20151 nr = 0; /* type error; errmsg already given */
20152 else if (STRCMP(arg, "$") == 0)
20153 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20154 else if (STRCMP(arg, "#") == 0)
20155 {
20156 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20157 if (twin == NULL)
20158 nr = 0;
20159 }
20160 else
20161 {
20162 EMSG2(_(e_invexpr2), arg);
20163 nr = 0;
20164 }
20165 }
20166
20167 if (nr > 0)
20168 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20169 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020170 {
20171 if (wp == NULL)
20172 {
20173 /* didn't find it in this tabpage */
20174 nr = 0;
20175 break;
20176 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020177 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020178 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020179 return nr;
20180}
20181#endif
20182
20183/*
20184 * "tabpagewinnr()" function
20185 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020187f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020188{
20189 int nr = 1;
20190#ifdef FEAT_WINDOWS
20191 tabpage_T *tp;
20192
20193 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20194 if (tp == NULL)
20195 nr = 0;
20196 else
20197 nr = get_winnr(tp, &argvars[1]);
20198#endif
20199 rettv->vval.v_number = nr;
20200}
20201
20202
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020203/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020204 * "tagfiles()" function
20205 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020207f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020208{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020209 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020210 tagname_T tn;
20211 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020212
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020213 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020214 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020215 fname = alloc(MAXPATHL);
20216 if (fname == NULL)
20217 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020218
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020219 for (first = TRUE; ; first = FALSE)
20220 if (get_tagfname(&tn, first, fname) == FAIL
20221 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020222 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020223 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020224 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020225}
20226
20227/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020228 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020229 */
20230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020231f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020232{
20233 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020234
20235 tag_pattern = get_tv_string(&argvars[0]);
20236
20237 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020238 if (*tag_pattern == NUL)
20239 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020240
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020241 if (rettv_list_alloc(rettv) == OK)
20242 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020243}
20244
20245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 * "tempname()" function
20247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020249f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250{
20251 static int x = 'A';
20252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020253 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020254 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020255
20256 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20257 * names. Skip 'I' and 'O', they are used for shell redirection. */
20258 do
20259 {
20260 if (x == 'Z')
20261 x = '0';
20262 else if (x == '9')
20263 x = 'A';
20264 else
20265 {
20266#ifdef EBCDIC
20267 if (x == 'I')
20268 x = 'J';
20269 else if (x == 'R')
20270 x = 'S';
20271 else
20272#endif
20273 ++x;
20274 }
20275 } while (x == 'I' || x == 'O');
20276}
20277
20278/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020279 * "test(list)" function: Just checking the walls...
20280 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020282f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020283{
20284 /* Used for unit testing. Change the code below to your liking. */
20285#if 0
20286 listitem_T *li;
20287 list_T *l;
20288 char_u *bad, *good;
20289
20290 if (argvars[0].v_type != VAR_LIST)
20291 return;
20292 l = argvars[0].vval.v_list;
20293 if (l == NULL)
20294 return;
20295 li = l->lv_first;
20296 if (li == NULL)
20297 return;
20298 bad = get_tv_string(&li->li_tv);
20299 li = li->li_next;
20300 if (li == NULL)
20301 return;
20302 good = get_tv_string(&li->li_tv);
20303 rettv->vval.v_number = test_edit_score(bad, good);
20304#endif
20305}
20306
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020307#ifdef FEAT_FLOAT
20308/*
20309 * "tan()" function
20310 */
20311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020312f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020313{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020314 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020315
20316 rettv->v_type = VAR_FLOAT;
20317 if (get_float_arg(argvars, &f) == OK)
20318 rettv->vval.v_float = tan(f);
20319 else
20320 rettv->vval.v_float = 0.0;
20321}
20322
20323/*
20324 * "tanh()" function
20325 */
20326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020327f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020328{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020329 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020330
20331 rettv->v_type = VAR_FLOAT;
20332 if (get_float_arg(argvars, &f) == OK)
20333 rettv->vval.v_float = tanh(f);
20334 else
20335 rettv->vval.v_float = 0.0;
20336}
20337#endif
20338
Bram Moolenaar975b5272016-03-15 23:10:59 +010020339#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20340/*
20341 * Get a callback from "arg". It can be a Funcref or a function name.
20342 * When "arg" is zero return an empty string.
20343 * Return NULL for an invalid argument.
20344 */
20345 char_u *
20346get_callback(typval_T *arg, partial_T **pp)
20347{
20348 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20349 {
20350 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020351 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020352 return (*pp)->pt_name;
20353 }
20354 *pp = NULL;
20355 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20356 return arg->vval.v_string;
20357 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20358 return (char_u *)"";
20359 EMSG(_("E921: Invalid callback argument"));
20360 return NULL;
20361}
20362#endif
20363
20364#ifdef FEAT_TIMERS
20365/*
20366 * "timer_start(time, callback [, options])" function
20367 */
20368 static void
20369f_timer_start(typval_T *argvars, typval_T *rettv)
20370{
20371 long msec = get_tv_number(&argvars[0]);
20372 timer_T *timer;
20373 int repeat = 0;
20374 char_u *callback;
20375 dict_T *dict;
20376
20377 if (argvars[2].v_type != VAR_UNKNOWN)
20378 {
20379 if (argvars[2].v_type != VAR_DICT
20380 || (dict = argvars[2].vval.v_dict) == NULL)
20381 {
20382 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20383 return;
20384 }
20385 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20386 repeat = get_dict_number(dict, (char_u *)"repeat");
20387 }
20388
20389 timer = create_timer(msec, repeat);
20390 callback = get_callback(&argvars[1], &timer->tr_partial);
20391 if (callback == NULL)
20392 {
20393 stop_timer(timer);
20394 rettv->vval.v_number = -1;
20395 }
20396 else
20397 {
20398 timer->tr_callback = vim_strsave(callback);
20399 rettv->vval.v_number = timer->tr_id;
20400 }
20401}
20402
20403/*
20404 * "timer_stop(timer)" function
20405 */
20406 static void
20407f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20408{
20409 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20410
20411 if (timer != NULL)
20412 stop_timer(timer);
20413}
20414#endif
20415
Bram Moolenaard52d9742005-08-21 22:20:28 +000020416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020417 * "tolower(string)" function
20418 */
20419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020420f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421{
20422 char_u *p;
20423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020424 p = vim_strsave(get_tv_string(&argvars[0]));
20425 rettv->v_type = VAR_STRING;
20426 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427
20428 if (p != NULL)
20429 while (*p != NUL)
20430 {
20431#ifdef FEAT_MBYTE
20432 int l;
20433
20434 if (enc_utf8)
20435 {
20436 int c, lc;
20437
20438 c = utf_ptr2char(p);
20439 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020440 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 /* TODO: reallocate string when byte count changes. */
20442 if (utf_char2len(lc) == l)
20443 utf_char2bytes(lc, p);
20444 p += l;
20445 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020446 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 p += l; /* skip multi-byte character */
20448 else
20449#endif
20450 {
20451 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20452 ++p;
20453 }
20454 }
20455}
20456
20457/*
20458 * "toupper(string)" function
20459 */
20460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020461f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020463 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020464 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465}
20466
20467/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020468 * "tr(string, fromstr, tostr)" function
20469 */
20470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020471f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020472{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020473 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020474 char_u *fromstr;
20475 char_u *tostr;
20476 char_u *p;
20477#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020478 int inlen;
20479 int fromlen;
20480 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020481 int idx;
20482 char_u *cpstr;
20483 int cplen;
20484 int first = TRUE;
20485#endif
20486 char_u buf[NUMBUFLEN];
20487 char_u buf2[NUMBUFLEN];
20488 garray_T ga;
20489
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020490 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020491 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20492 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020493
20494 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495 rettv->v_type = VAR_STRING;
20496 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020497 if (fromstr == NULL || tostr == NULL)
20498 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020499 ga_init2(&ga, (int)sizeof(char), 80);
20500
20501#ifdef FEAT_MBYTE
20502 if (!has_mbyte)
20503#endif
20504 /* not multi-byte: fromstr and tostr must be the same length */
20505 if (STRLEN(fromstr) != STRLEN(tostr))
20506 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020507#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020508error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020509#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020510 EMSG2(_(e_invarg2), fromstr);
20511 ga_clear(&ga);
20512 return;
20513 }
20514
20515 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020516 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020517 {
20518#ifdef FEAT_MBYTE
20519 if (has_mbyte)
20520 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020521 inlen = (*mb_ptr2len)(in_str);
20522 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020523 cplen = inlen;
20524 idx = 0;
20525 for (p = fromstr; *p != NUL; p += fromlen)
20526 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020527 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020528 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020529 {
20530 for (p = tostr; *p != NUL; p += tolen)
20531 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020532 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020533 if (idx-- == 0)
20534 {
20535 cplen = tolen;
20536 cpstr = p;
20537 break;
20538 }
20539 }
20540 if (*p == NUL) /* tostr is shorter than fromstr */
20541 goto error;
20542 break;
20543 }
20544 ++idx;
20545 }
20546
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020547 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020548 {
20549 /* Check that fromstr and tostr have the same number of
20550 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020551 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020552 first = FALSE;
20553 for (p = tostr; *p != NUL; p += tolen)
20554 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020555 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020556 --idx;
20557 }
20558 if (idx != 0)
20559 goto error;
20560 }
20561
Bram Moolenaarcde88542015-08-11 19:14:00 +020020562 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020563 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020564 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020565
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020566 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020567 }
20568 else
20569#endif
20570 {
20571 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020572 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020573 if (p != NULL)
20574 ga_append(&ga, tostr[p - fromstr]);
20575 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020576 ga_append(&ga, *in_str);
20577 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020578 }
20579 }
20580
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020581 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020582 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020583 ga_append(&ga, NUL);
20584
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020585 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020586}
20587
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020588#ifdef FEAT_FLOAT
20589/*
20590 * "trunc({float})" function
20591 */
20592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020593f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020594{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020595 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020596
20597 rettv->v_type = VAR_FLOAT;
20598 if (get_float_arg(argvars, &f) == OK)
20599 /* trunc() is not in C90, use floor() or ceil() instead. */
20600 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20601 else
20602 rettv->vval.v_float = 0.0;
20603}
20604#endif
20605
Bram Moolenaar8299df92004-07-10 09:47:34 +000020606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 * "type(expr)" function
20608 */
20609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020610f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020612 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020613
20614 switch (argvars[0].v_type)
20615 {
20616 case VAR_NUMBER: n = 0; break;
20617 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020618 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020619 case VAR_FUNC: n = 2; break;
20620 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020621 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020622 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020623 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020624 if (argvars[0].vval.v_number == VVAL_FALSE
20625 || argvars[0].vval.v_number == VVAL_TRUE)
20626 n = 6;
20627 else
20628 n = 7;
20629 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020630 case VAR_JOB: n = 8; break;
20631 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020632 case VAR_UNKNOWN:
20633 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20634 n = -1;
20635 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020636 }
20637 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638}
20639
20640/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020641 * "undofile(name)" function
20642 */
20643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020644f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020645{
20646 rettv->v_type = VAR_STRING;
20647#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020648 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020649 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020650
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020651 if (*fname == NUL)
20652 {
20653 /* If there is no file name there will be no undo file. */
20654 rettv->vval.v_string = NULL;
20655 }
20656 else
20657 {
20658 char_u *ffname = FullName_save(fname, FALSE);
20659
20660 if (ffname != NULL)
20661 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20662 vim_free(ffname);
20663 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020664 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020665#else
20666 rettv->vval.v_string = NULL;
20667#endif
20668}
20669
20670/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020671 * "undotree()" function
20672 */
20673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020674f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020675{
20676 if (rettv_dict_alloc(rettv) == OK)
20677 {
20678 dict_T *dict = rettv->vval.v_dict;
20679 list_T *list;
20680
Bram Moolenaar730cde92010-06-27 05:18:54 +020020681 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020682 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020683 dict_add_nr_str(dict, "save_last",
20684 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020685 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20686 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020687 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020688
20689 list = list_alloc();
20690 if (list != NULL)
20691 {
20692 u_eval_tree(curbuf->b_u_oldhead, list);
20693 dict_add_list(dict, "entries", list);
20694 }
20695 }
20696}
20697
20698/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020699 * "values(dict)" function
20700 */
20701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020702f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020703{
20704 dict_list(argvars, rettv, 1);
20705}
20706
20707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 * "virtcol(string)" function
20709 */
20710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020711f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712{
20713 colnr_T vcol = 0;
20714 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020715 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020717 fp = var2fpos(&argvars[0], FALSE, &fnum);
20718 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20719 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720 {
20721 getvvcol(curwin, fp, NULL, NULL, &vcol);
20722 ++vcol;
20723 }
20724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020725 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726}
20727
20728/*
20729 * "visualmode()" function
20730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020732f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 char_u str[2];
20735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020736 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 str[0] = curbuf->b_visual_mode_eval;
20738 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020739 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740
20741 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020742 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744}
20745
20746/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020747 * "wildmenumode()" function
20748 */
20749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020750f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020751{
20752#ifdef FEAT_WILDMENU
20753 if (wild_menu_showing)
20754 rettv->vval.v_number = 1;
20755#endif
20756}
20757
20758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020759 * "winbufnr(nr)" function
20760 */
20761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020762f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020763{
20764 win_T *wp;
20765
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020766 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020770 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771}
20772
20773/*
20774 * "wincol()" function
20775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020777f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778{
20779 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020780 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781}
20782
20783/*
20784 * "winheight(nr)" function
20785 */
20786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020787f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788{
20789 win_T *wp;
20790
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020791 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020795 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796}
20797
20798/*
20799 * "winline()" function
20800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020802f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020803{
20804 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020805 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806}
20807
20808/*
20809 * "winnr()" function
20810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020812f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813{
20814 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020815
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020817 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020818#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020819 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020820}
20821
20822/*
20823 * "winrestcmd()" function
20824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020826f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827{
20828#ifdef FEAT_WINDOWS
20829 win_T *wp;
20830 int winnr = 1;
20831 garray_T ga;
20832 char_u buf[50];
20833
20834 ga_init2(&ga, (int)sizeof(char), 70);
20835 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20836 {
20837 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20838 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20840 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 ++winnr;
20842 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020843 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020845 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020847 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020849 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850}
20851
20852/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020853 * "winrestview()" function
20854 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020856f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020857{
20858 dict_T *dict;
20859
20860 if (argvars[0].v_type != VAR_DICT
20861 || (dict = argvars[0].vval.v_dict) == NULL)
20862 EMSG(_(e_invarg));
20863 else
20864 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020865 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20866 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20867 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20868 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020869#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020870 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20871 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020872#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020873 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20874 {
20875 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20876 curwin->w_set_curswant = FALSE;
20877 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020878
Bram Moolenaar82c25852014-05-28 16:47:16 +020020879 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20880 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020881#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020882 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20883 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020884#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020885 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20886 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20887 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20888 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020889
20890 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020891 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020892# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020893 win_new_width(curwin, W_WIDTH(curwin));
20894# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020895 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020896
Bram Moolenaarb851a962014-10-31 15:45:52 +010020897 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020898 curwin->w_topline = 1;
20899 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20900 curwin->w_topline = curbuf->b_ml.ml_line_count;
20901#ifdef FEAT_DIFF
20902 check_topfill(curwin, TRUE);
20903#endif
20904 }
20905}
20906
20907/*
20908 * "winsaveview()" function
20909 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020911f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020912{
20913 dict_T *dict;
20914
Bram Moolenaara800b422010-06-27 01:15:55 +020020915 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020916 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020917 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020918
20919 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20920 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20921#ifdef FEAT_VIRTUALEDIT
20922 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20923#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020924 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020925 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20926
20927 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20928#ifdef FEAT_DIFF
20929 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20930#endif
20931 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20932 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20933}
20934
20935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 * "winwidth(nr)" function
20937 */
20938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020939f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940{
20941 win_T *wp;
20942
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020943 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020945 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020947#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020948 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020950 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951#endif
20952}
20953
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020955 * "wordcount()" function
20956 */
20957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020958f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020959{
20960 if (rettv_dict_alloc(rettv) == FAIL)
20961 return;
20962 cursor_pos_info(rettv->vval.v_dict);
20963}
20964
20965/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020966 * Write list of strings to file
20967 */
20968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020969write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020970{
20971 listitem_T *li;
20972 int c;
20973 int ret = OK;
20974 char_u *s;
20975
20976 for (li = list->lv_first; li != NULL; li = li->li_next)
20977 {
20978 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20979 {
20980 if (*s == '\n')
20981 c = putc(NUL, fd);
20982 else
20983 c = putc(*s, fd);
20984 if (c == EOF)
20985 {
20986 ret = FAIL;
20987 break;
20988 }
20989 }
20990 if (!binary || li->li_next != NULL)
20991 if (putc('\n', fd) == EOF)
20992 {
20993 ret = FAIL;
20994 break;
20995 }
20996 if (ret == FAIL)
20997 {
20998 EMSG(_(e_write));
20999 break;
21000 }
21001 }
21002 return ret;
21003}
21004
21005/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021006 * "writefile()" function
21007 */
21008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021009f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021010{
21011 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021012 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021013 char_u *fname;
21014 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021015 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021016
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021017 if (check_restricted() || check_secure())
21018 return;
21019
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021020 if (argvars[0].v_type != VAR_LIST)
21021 {
21022 EMSG2(_(e_listarg), "writefile()");
21023 return;
21024 }
21025 if (argvars[0].vval.v_list == NULL)
21026 return;
21027
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021028 if (argvars[2].v_type != VAR_UNKNOWN)
21029 {
21030 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21031 binary = TRUE;
21032 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21033 append = TRUE;
21034 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021035
21036 /* Always open the file in binary mode, library functions have a mind of
21037 * their own about CR-LF conversion. */
21038 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021039 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21040 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021041 {
21042 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21043 ret = -1;
21044 }
21045 else
21046 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021047 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21048 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021049 fclose(fd);
21050 }
21051
21052 rettv->vval.v_number = ret;
21053}
21054
21055/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021056 * "xor(expr, expr)" function
21057 */
21058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021059f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021060{
21061 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21062 ^ get_tv_number_chk(&argvars[1], NULL);
21063}
21064
21065
21066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021068 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021069 */
21070 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021071var2fpos(
21072 typval_T *varp,
21073 int dollar_lnum, /* TRUE when $ is last line */
21074 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021076 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021078 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079
Bram Moolenaara5525202006-03-02 22:52:09 +000021080 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021081 if (varp->v_type == VAR_LIST)
21082 {
21083 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021084 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021085 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021086 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021087
21088 l = varp->vval.v_list;
21089 if (l == NULL)
21090 return NULL;
21091
21092 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021093 pos.lnum = list_find_nr(l, 0L, &error);
21094 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021095 return NULL; /* invalid line number */
21096
21097 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021098 pos.col = list_find_nr(l, 1L, &error);
21099 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021100 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021101 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021102
21103 /* We accept "$" for the column number: last column. */
21104 li = list_find(l, 1L);
21105 if (li != NULL && li->li_tv.v_type == VAR_STRING
21106 && li->li_tv.vval.v_string != NULL
21107 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21108 pos.col = len + 1;
21109
Bram Moolenaara5525202006-03-02 22:52:09 +000021110 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021111 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021112 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021113 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021114
Bram Moolenaara5525202006-03-02 22:52:09 +000021115#ifdef FEAT_VIRTUALEDIT
21116 /* Get the virtual offset. Defaults to zero. */
21117 pos.coladd = list_find_nr(l, 2L, &error);
21118 if (error)
21119 pos.coladd = 0;
21120#endif
21121
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021122 return &pos;
21123 }
21124
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021125 name = get_tv_string_chk(varp);
21126 if (name == NULL)
21127 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021128 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021130 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21131 {
21132 if (VIsual_active)
21133 return &VIsual;
21134 return &curwin->w_cursor;
21135 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021136 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021138 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21140 return NULL;
21141 return pp;
21142 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021143
21144#ifdef FEAT_VIRTUALEDIT
21145 pos.coladd = 0;
21146#endif
21147
Bram Moolenaar477933c2007-07-17 14:32:23 +000021148 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021149 {
21150 pos.col = 0;
21151 if (name[1] == '0') /* "w0": first visible line */
21152 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021153 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021154 pos.lnum = curwin->w_topline;
21155 return &pos;
21156 }
21157 else if (name[1] == '$') /* "w$": last visible line */
21158 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021159 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021160 pos.lnum = curwin->w_botline - 1;
21161 return &pos;
21162 }
21163 }
21164 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021166 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021167 {
21168 pos.lnum = curbuf->b_ml.ml_line_count;
21169 pos.col = 0;
21170 }
21171 else
21172 {
21173 pos.lnum = curwin->w_cursor.lnum;
21174 pos.col = (colnr_T)STRLEN(ml_get_curline());
21175 }
21176 return &pos;
21177 }
21178 return NULL;
21179}
21180
21181/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021182 * Convert list in "arg" into a position and optional file number.
21183 * When "fnump" is NULL there is no file number, only 3 items.
21184 * Note that the column is passed on as-is, the caller may want to decrement
21185 * it to use 1 for the first column.
21186 * Return FAIL when conversion is not possible, doesn't check the position for
21187 * validity.
21188 */
21189 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021190list2fpos(
21191 typval_T *arg,
21192 pos_T *posp,
21193 int *fnump,
21194 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021195{
21196 list_T *l = arg->vval.v_list;
21197 long i = 0;
21198 long n;
21199
Bram Moolenaar493c1782014-05-28 14:34:46 +020021200 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21201 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021202 if (arg->v_type != VAR_LIST
21203 || l == NULL
21204 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021205 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021206 return FAIL;
21207
21208 if (fnump != NULL)
21209 {
21210 n = list_find_nr(l, i++, NULL); /* fnum */
21211 if (n < 0)
21212 return FAIL;
21213 if (n == 0)
21214 n = curbuf->b_fnum; /* current buffer */
21215 *fnump = n;
21216 }
21217
21218 n = list_find_nr(l, i++, NULL); /* lnum */
21219 if (n < 0)
21220 return FAIL;
21221 posp->lnum = n;
21222
21223 n = list_find_nr(l, i++, NULL); /* col */
21224 if (n < 0)
21225 return FAIL;
21226 posp->col = n;
21227
21228#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021229 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021230 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021231 posp->coladd = 0;
21232 else
21233 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021234#endif
21235
Bram Moolenaar493c1782014-05-28 14:34:46 +020021236 if (curswantp != NULL)
21237 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21238
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021239 return OK;
21240}
21241
21242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 * Get the length of an environment variable name.
21244 * Advance "arg" to the first character after the name.
21245 * Return 0 for error.
21246 */
21247 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021248get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249{
21250 char_u *p;
21251 int len;
21252
21253 for (p = *arg; vim_isIDc(*p); ++p)
21254 ;
21255 if (p == *arg) /* no name found */
21256 return 0;
21257
21258 len = (int)(p - *arg);
21259 *arg = p;
21260 return len;
21261}
21262
21263/*
21264 * Get the length of the name of a function or internal variable.
21265 * "arg" is advanced to the first non-white character after the name.
21266 * Return 0 if something is wrong.
21267 */
21268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021269get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270{
21271 char_u *p;
21272 int len;
21273
21274 /* Find the end of the name. */
21275 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021276 {
21277 if (*p == ':')
21278 {
21279 /* "s:" is start of "s:var", but "n:" is not and can be used in
21280 * slice "[n:]". Also "xx:" is not a namespace. */
21281 len = (int)(p - *arg);
21282 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21283 || len > 1)
21284 break;
21285 }
21286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 if (p == *arg) /* no name found */
21288 return 0;
21289
21290 len = (int)(p - *arg);
21291 *arg = skipwhite(p);
21292
21293 return len;
21294}
21295
21296/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021297 * Get the length of the name of a variable or function.
21298 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021300 * Return -1 if curly braces expansion failed.
21301 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021302 * If the name contains 'magic' {}'s, expand them and return the
21303 * expanded name in an allocated string via 'alias' - caller must free.
21304 */
21305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021306get_name_len(
21307 char_u **arg,
21308 char_u **alias,
21309 int evaluate,
21310 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311{
21312 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 char_u *p;
21314 char_u *expr_start;
21315 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316
21317 *alias = NULL; /* default to no alias */
21318
21319 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21320 && (*arg)[2] == (int)KE_SNR)
21321 {
21322 /* hard coded <SNR>, already translated */
21323 *arg += 3;
21324 return get_id_len(arg) + 3;
21325 }
21326 len = eval_fname_script(*arg);
21327 if (len > 0)
21328 {
21329 /* literal "<SID>", "s:" or "<SNR>" */
21330 *arg += len;
21331 }
21332
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021334 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021335 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021336 p = find_name_end(*arg, &expr_start, &expr_end,
21337 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 if (expr_start != NULL)
21339 {
21340 char_u *temp_string;
21341
21342 if (!evaluate)
21343 {
21344 len += (int)(p - *arg);
21345 *arg = skipwhite(p);
21346 return len;
21347 }
21348
21349 /*
21350 * Include any <SID> etc in the expanded string:
21351 * Thus the -len here.
21352 */
21353 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21354 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021355 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 *alias = temp_string;
21357 *arg = skipwhite(p);
21358 return (int)STRLEN(temp_string);
21359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360
21361 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021362 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 EMSG2(_(e_invexpr2), *arg);
21364
21365 return len;
21366}
21367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021368/*
21369 * Find the end of a variable or function name, taking care of magic braces.
21370 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21371 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021372 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021373 * Return a pointer to just after the name. Equal to "arg" if there is no
21374 * valid name.
21375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021377find_name_end(
21378 char_u *arg,
21379 char_u **expr_start,
21380 char_u **expr_end,
21381 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021383 int mb_nest = 0;
21384 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021386 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021388 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021390 *expr_start = NULL;
21391 *expr_end = NULL;
21392 }
21393
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021394 /* Quick check for valid starting character. */
21395 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21396 return arg;
21397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021398 for (p = arg; *p != NUL
21399 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021400 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021401 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021402 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021403 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021404 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021405 if (*p == '\'')
21406 {
21407 /* skip over 'string' to avoid counting [ and ] inside it. */
21408 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21409 ;
21410 if (*p == NUL)
21411 break;
21412 }
21413 else if (*p == '"')
21414 {
21415 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21416 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21417 if (*p == '\\' && p[1] != NUL)
21418 ++p;
21419 if (*p == NUL)
21420 break;
21421 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021422 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21423 {
21424 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021425 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021426 len = (int)(p - arg);
21427 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021428 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021429 break;
21430 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021432 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021434 if (*p == '[')
21435 ++br_nest;
21436 else if (*p == ']')
21437 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021440 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021442 if (*p == '{')
21443 {
21444 mb_nest++;
21445 if (expr_start != NULL && *expr_start == NULL)
21446 *expr_start = p;
21447 }
21448 else if (*p == '}')
21449 {
21450 mb_nest--;
21451 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21452 *expr_end = p;
21453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455 }
21456
21457 return p;
21458}
21459
21460/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021461 * Expands out the 'magic' {}'s in a variable/function name.
21462 * Note that this can call itself recursively, to deal with
21463 * constructs like foo{bar}{baz}{bam}
21464 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21465 * "in_start" ^
21466 * "expr_start" ^
21467 * "expr_end" ^
21468 * "in_end" ^
21469 *
21470 * Returns a new allocated string, which the caller must free.
21471 * Returns NULL for failure.
21472 */
21473 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021474make_expanded_name(
21475 char_u *in_start,
21476 char_u *expr_start,
21477 char_u *expr_end,
21478 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021479{
21480 char_u c1;
21481 char_u *retval = NULL;
21482 char_u *temp_result;
21483 char_u *nextcmd = NULL;
21484
21485 if (expr_end == NULL || in_end == NULL)
21486 return NULL;
21487 *expr_start = NUL;
21488 *expr_end = NUL;
21489 c1 = *in_end;
21490 *in_end = NUL;
21491
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021492 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021493 if (temp_result != NULL && nextcmd == NULL)
21494 {
21495 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21496 + (in_end - expr_end) + 1));
21497 if (retval != NULL)
21498 {
21499 STRCPY(retval, in_start);
21500 STRCAT(retval, temp_result);
21501 STRCAT(retval, expr_end + 1);
21502 }
21503 }
21504 vim_free(temp_result);
21505
21506 *in_end = c1; /* put char back for error messages */
21507 *expr_start = '{';
21508 *expr_end = '}';
21509
21510 if (retval != NULL)
21511 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021512 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021513 if (expr_start != NULL)
21514 {
21515 /* Further expansion! */
21516 temp_result = make_expanded_name(retval, expr_start,
21517 expr_end, temp_result);
21518 vim_free(retval);
21519 retval = temp_result;
21520 }
21521 }
21522
21523 return retval;
21524}
21525
21526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021528 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 */
21530 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021531eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021533 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21534}
21535
21536/*
21537 * Return TRUE if character "c" can be used as the first character in a
21538 * variable or function name (excluding '{' and '}').
21539 */
21540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021541eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021542{
21543 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021544}
21545
21546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 * Set number v: variable to "val".
21548 */
21549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021550set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021552 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553}
21554
21555/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021556 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021557 */
21558 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021559get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021561 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562}
21563
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021564/*
21565 * Get string v: variable value. Uses a static buffer, can only be used once.
21566 */
21567 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021568get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021569{
21570 return get_tv_string(&vimvars[idx].vv_tv);
21571}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021572
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021574 * Get List v: variable value. Caller must take care of reference count when
21575 * needed.
21576 */
21577 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021578get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021579{
21580 return vimvars[idx].vv_list;
21581}
21582
21583/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021584 * Set v:char to character "c".
21585 */
21586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021587set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021588{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021589 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021590
21591#ifdef FEAT_MBYTE
21592 if (has_mbyte)
21593 buf[(*mb_char2bytes)(c, buf)] = NUL;
21594 else
21595#endif
21596 {
21597 buf[0] = c;
21598 buf[1] = NUL;
21599 }
21600 set_vim_var_string(VV_CHAR, buf, -1);
21601}
21602
21603/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021604 * Set v:count to "count" and v:count1 to "count1".
21605 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 */
21607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021608set_vcount(
21609 long count,
21610 long count1,
21611 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021613 if (set_prevcount)
21614 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021615 vimvars[VV_COUNT].vv_nr = count;
21616 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617}
21618
21619/*
21620 * Set string v: variable to a copy of "val".
21621 */
21622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021623set_vim_var_string(
21624 int idx,
21625 char_u *val,
21626 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021627{
Bram Moolenaara542c682016-01-31 16:28:04 +010021628 clear_tv(&vimvars[idx].vv_di.di_tv);
21629 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021631 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021633 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021635 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636}
21637
21638/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021639 * Set List v: variable to "val".
21640 */
21641 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021642set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021643{
Bram Moolenaara542c682016-01-31 16:28:04 +010021644 clear_tv(&vimvars[idx].vv_di.di_tv);
21645 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021646 vimvars[idx].vv_list = val;
21647 if (val != NULL)
21648 ++val->lv_refcount;
21649}
21650
21651/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021652 * Set Dictionary v: variable to "val".
21653 */
21654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021655set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021656{
21657 int todo;
21658 hashitem_T *hi;
21659
Bram Moolenaara542c682016-01-31 16:28:04 +010021660 clear_tv(&vimvars[idx].vv_di.di_tv);
21661 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021662 vimvars[idx].vv_dict = val;
21663 if (val != NULL)
21664 {
21665 ++val->dv_refcount;
21666
21667 /* Set readonly */
21668 todo = (int)val->dv_hashtab.ht_used;
21669 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21670 {
21671 if (HASHITEM_EMPTY(hi))
21672 continue;
21673 --todo;
21674 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21675 }
21676 }
21677}
21678
21679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 * Set v:register if needed.
21681 */
21682 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021683set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684{
21685 char_u regname;
21686
21687 if (c == 0 || c == ' ')
21688 regname = '"';
21689 else
21690 regname = c;
21691 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021692 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 set_vim_var_string(VV_REG, &regname, 1);
21694}
21695
21696/*
21697 * Get or set v:exception. If "oldval" == NULL, return the current value.
21698 * Otherwise, restore the value to "oldval" and return NULL.
21699 * Must always be called in pairs to save and restore v:exception! Does not
21700 * take care of memory allocations.
21701 */
21702 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021703v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704{
21705 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021706 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707
Bram Moolenaare9a41262005-01-15 22:18:47 +000021708 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709 return NULL;
21710}
21711
21712/*
21713 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21714 * Otherwise, restore the value to "oldval" and return NULL.
21715 * Must always be called in pairs to save and restore v:throwpoint! Does not
21716 * take care of memory allocations.
21717 */
21718 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021719v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021720{
21721 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021722 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021723
Bram Moolenaare9a41262005-01-15 22:18:47 +000021724 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725 return NULL;
21726}
21727
21728#if defined(FEAT_AUTOCMD) || defined(PROTO)
21729/*
21730 * Set v:cmdarg.
21731 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21732 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21733 * Must always be called in pairs!
21734 */
21735 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021736set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737{
21738 char_u *oldval;
21739 char_u *newval;
21740 unsigned len;
21741
Bram Moolenaare9a41262005-01-15 22:18:47 +000021742 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021743 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021745 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021746 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021747 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 }
21749
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021750 if (eap->force_bin == FORCE_BIN)
21751 len = 6;
21752 else if (eap->force_bin == FORCE_NOBIN)
21753 len = 8;
21754 else
21755 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021756
21757 if (eap->read_edit)
21758 len += 7;
21759
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021760 if (eap->force_ff != 0)
21761 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21762# ifdef FEAT_MBYTE
21763 if (eap->force_enc != 0)
21764 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021765 if (eap->bad_char != 0)
21766 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021767# endif
21768
21769 newval = alloc(len + 1);
21770 if (newval == NULL)
21771 return NULL;
21772
21773 if (eap->force_bin == FORCE_BIN)
21774 sprintf((char *)newval, " ++bin");
21775 else if (eap->force_bin == FORCE_NOBIN)
21776 sprintf((char *)newval, " ++nobin");
21777 else
21778 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021779
21780 if (eap->read_edit)
21781 STRCAT(newval, " ++edit");
21782
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021783 if (eap->force_ff != 0)
21784 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21785 eap->cmd + eap->force_ff);
21786# ifdef FEAT_MBYTE
21787 if (eap->force_enc != 0)
21788 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21789 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021790 if (eap->bad_char == BAD_KEEP)
21791 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21792 else if (eap->bad_char == BAD_DROP)
21793 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21794 else if (eap->bad_char != 0)
21795 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021796# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021797 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021798 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799}
21800#endif
21801
21802/*
21803 * Get the value of internal variable "name".
21804 * Return OK or FAIL.
21805 */
21806 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021807get_var_tv(
21808 char_u *name,
21809 int len, /* length of "name" */
21810 typval_T *rettv, /* NULL when only checking existence */
21811 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21812 int verbose, /* may give error message */
21813 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814{
21815 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021816 typval_T *tv = NULL;
21817 typval_T atv;
21818 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820
21821 /* truncate the name, so that we can use strcmp() */
21822 cc = name[len];
21823 name[len] = NUL;
21824
21825 /*
21826 * Check for "b:changedtick".
21827 */
21828 if (STRCMP(name, "b:changedtick") == 0)
21829 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021830 atv.v_type = VAR_NUMBER;
21831 atv.vval.v_number = curbuf->b_changedtick;
21832 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021833 }
21834
21835 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 * Check for user-defined variables.
21837 */
21838 else
21839 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021840 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021842 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021844 if (dip != NULL)
21845 *dip = v;
21846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847 }
21848
Bram Moolenaare9a41262005-01-15 22:18:47 +000021849 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021851 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021852 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 ret = FAIL;
21854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021855 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021856 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857
21858 name[len] = cc;
21859
21860 return ret;
21861}
21862
21863/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021864 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21865 * Also handle function call with Funcref variable: func(expr)
21866 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21867 */
21868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021869handle_subscript(
21870 char_u **arg,
21871 typval_T *rettv,
21872 int evaluate, /* do more than finding the end */
21873 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021874{
21875 int ret = OK;
21876 dict_T *selfdict = NULL;
21877 char_u *s;
21878 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021879 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021880
21881 while (ret == OK
21882 && (**arg == '['
21883 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021884 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21885 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021886 && !vim_iswhite(*(*arg - 1)))
21887 {
21888 if (**arg == '(')
21889 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021890 partial_T *pt = NULL;
21891
Bram Moolenaard9fba312005-06-26 22:34:35 +000021892 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021893 if (evaluate)
21894 {
21895 functv = *rettv;
21896 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021897
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021898 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021899 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021900 {
21901 pt = functv.vval.v_partial;
21902 s = pt->pt_name;
21903 }
21904 else
21905 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021906 }
21907 else
21908 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021909 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021910 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021911 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021912
21913 /* Clear the funcref afterwards, so that deleting it while
21914 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021915 if (evaluate)
21916 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021917
21918 /* Stop the expression evaluation when immediately aborting on
21919 * error, or when an interrupt occurred or an exception was thrown
21920 * but not caught. */
21921 if (aborting())
21922 {
21923 if (ret == OK)
21924 clear_tv(rettv);
21925 ret = FAIL;
21926 }
21927 dict_unref(selfdict);
21928 selfdict = NULL;
21929 }
21930 else /* **arg == '[' || **arg == '.' */
21931 {
21932 dict_unref(selfdict);
21933 if (rettv->v_type == VAR_DICT)
21934 {
21935 selfdict = rettv->vval.v_dict;
21936 if (selfdict != NULL)
21937 ++selfdict->dv_refcount;
21938 }
21939 else
21940 selfdict = NULL;
21941 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21942 {
21943 clear_tv(rettv);
21944 ret = FAIL;
21945 }
21946 }
21947 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021948
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021949 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21950 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021951 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021952 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21953 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021954 char_u *tofree = NULL;
21955 ufunc_T *fp;
21956 char_u fname_buf[FLEN_FIXED + 1];
21957 int error;
21958
21959 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021960 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021961 fp = find_func(fname);
21962 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021963
21964 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021965 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021966 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021967 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21968
21969 if (pt != NULL)
21970 {
21971 pt->pt_refcount = 1;
21972 pt->pt_dict = selfdict;
21973 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021974 if (rettv->v_type == VAR_FUNC)
21975 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021976 /* Just a function: Take over the function name and use
21977 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021978 pt->pt_name = rettv->vval.v_string;
21979 }
21980 else
21981 {
21982 partial_T *ret_pt = rettv->vval.v_partial;
21983 int i;
21984
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021985 /* Partial: copy the function name, use selfdict and copy
21986 * args. Can't take over name or args, the partial might
21987 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021988 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021989 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021990 if (ret_pt->pt_argc > 0)
21991 {
21992 pt->pt_argv = (typval_T *)alloc(
21993 sizeof(typval_T) * ret_pt->pt_argc);
21994 if (pt->pt_argv == NULL)
21995 /* out of memory: drop the arguments */
21996 pt->pt_argc = 0;
21997 else
21998 {
21999 pt->pt_argc = ret_pt->pt_argc;
22000 for (i = 0; i < pt->pt_argc; i++)
22001 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22002 }
22003 }
22004 partial_unref(ret_pt);
22005 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022006 rettv->v_type = VAR_PARTIAL;
22007 rettv->vval.v_partial = pt;
22008 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022009 }
22010 }
22011
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022012 dict_unref(selfdict);
22013 return ret;
22014}
22015
22016/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022017 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022018 * value).
22019 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022020 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022021alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022022{
Bram Moolenaar33570922005-01-25 22:26:29 +000022023 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022024}
22025
22026/*
22027 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 * The string "s" must have been allocated, it is consumed.
22029 * Return NULL for out of memory, the variable otherwise.
22030 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022031 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022032alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033{
Bram Moolenaar33570922005-01-25 22:26:29 +000022034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022036 rettv = alloc_tv();
22037 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022039 rettv->v_type = VAR_STRING;
22040 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 }
22042 else
22043 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022044 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022045}
22046
22047/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022048 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022051free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052{
22053 if (varp != NULL)
22054 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022055 switch (varp->v_type)
22056 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022057 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022058 func_unref(varp->vval.v_string);
22059 /*FALLTHROUGH*/
22060 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022061 vim_free(varp->vval.v_string);
22062 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022063 case VAR_PARTIAL:
22064 partial_unref(varp->vval.v_partial);
22065 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022066 case VAR_LIST:
22067 list_unref(varp->vval.v_list);
22068 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022069 case VAR_DICT:
22070 dict_unref(varp->vval.v_dict);
22071 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022072 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022073#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022074 job_unref(varp->vval.v_job);
22075 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022076#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022077 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022078#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022079 channel_unref(varp->vval.v_channel);
22080 break;
22081#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022082 case VAR_NUMBER:
22083 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022084 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022085 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022086 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 vim_free(varp);
22089 }
22090}
22091
22092/*
22093 * Free the memory for a variable value and set the value to NULL or 0.
22094 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022096clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022097{
22098 if (varp != NULL)
22099 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022100 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022101 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022102 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022103 func_unref(varp->vval.v_string);
22104 /*FALLTHROUGH*/
22105 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022106 vim_free(varp->vval.v_string);
22107 varp->vval.v_string = NULL;
22108 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022109 case VAR_PARTIAL:
22110 partial_unref(varp->vval.v_partial);
22111 varp->vval.v_partial = NULL;
22112 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022113 case VAR_LIST:
22114 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022115 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022116 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022117 case VAR_DICT:
22118 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022119 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022120 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022121 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022122 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022123 varp->vval.v_number = 0;
22124 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022125 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022126#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022127 varp->vval.v_float = 0.0;
22128 break;
22129#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022130 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022131#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022132 job_unref(varp->vval.v_job);
22133 varp->vval.v_job = NULL;
22134#endif
22135 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022136 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022137#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022138 channel_unref(varp->vval.v_channel);
22139 varp->vval.v_channel = NULL;
22140#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022141 case VAR_UNKNOWN:
22142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022143 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022144 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 }
22146}
22147
22148/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022149 * Set the value of a variable to NULL without freeing items.
22150 */
22151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022152init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022153{
22154 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022155 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022156}
22157
22158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 * Get the number value of a variable.
22160 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022161 * For incompatible types, return 0.
22162 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22163 * caller of incompatible types: it sets *denote to TRUE if "denote"
22164 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022166 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022167get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022169 int error = FALSE;
22170
22171 return get_tv_number_chk(varp, &error); /* return 0L on error */
22172}
22173
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022174 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022175get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022176{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022177 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022179 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022181 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022182 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022183 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022184#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022185 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022186 break;
22187#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022188 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022189 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022190 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022191 break;
22192 case VAR_STRING:
22193 if (varp->vval.v_string != NULL)
22194 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022195 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022196 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022197 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022198 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022199 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022200 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022201 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022202 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022203 case VAR_SPECIAL:
22204 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22205 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022206 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022207#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022208 EMSG(_("E910: Using a Job as a Number"));
22209 break;
22210#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022211 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022212#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022213 EMSG(_("E913: Using a Channel as a Number"));
22214 break;
22215#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022216 case VAR_UNKNOWN:
22217 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022218 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022219 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022220 if (denote == NULL) /* useful for values that must be unsigned */
22221 n = -1;
22222 else
22223 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022224 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225}
22226
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022227#ifdef FEAT_FLOAT
22228 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022229get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022230{
22231 switch (varp->v_type)
22232 {
22233 case VAR_NUMBER:
22234 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022235 case VAR_FLOAT:
22236 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022237 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022238 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022239 EMSG(_("E891: Using a Funcref as a Float"));
22240 break;
22241 case VAR_STRING:
22242 EMSG(_("E892: Using a String as a Float"));
22243 break;
22244 case VAR_LIST:
22245 EMSG(_("E893: Using a List as a Float"));
22246 break;
22247 case VAR_DICT:
22248 EMSG(_("E894: Using a Dictionary as a Float"));
22249 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022250 case VAR_SPECIAL:
22251 EMSG(_("E907: Using a special value as a Float"));
22252 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022253 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022254# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022255 EMSG(_("E911: Using a Job as a Float"));
22256 break;
22257# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022258 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022259# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022260 EMSG(_("E914: Using a Channel as a Float"));
22261 break;
22262# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022263 case VAR_UNKNOWN:
22264 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022265 break;
22266 }
22267 return 0;
22268}
22269#endif
22270
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022272 * Get the lnum from the first argument.
22273 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022274 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 */
22276 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022277get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278{
Bram Moolenaar33570922005-01-25 22:26:29 +000022279 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 linenr_T lnum;
22281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022282 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283 if (lnum == 0) /* no valid number, try using line() */
22284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022285 rettv.v_type = VAR_NUMBER;
22286 f_line(argvars, &rettv);
22287 lnum = rettv.vval.v_number;
22288 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 }
22290 return lnum;
22291}
22292
22293/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022294 * Get the lnum from the first argument.
22295 * Also accepts "$", then "buf" is used.
22296 * Returns 0 on error.
22297 */
22298 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022299get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022300{
22301 if (argvars[0].v_type == VAR_STRING
22302 && argvars[0].vval.v_string != NULL
22303 && argvars[0].vval.v_string[0] == '$'
22304 && buf != NULL)
22305 return buf->b_ml.ml_line_count;
22306 return get_tv_number_chk(&argvars[0], NULL);
22307}
22308
22309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022310 * Get the string value of a variable.
22311 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022312 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22313 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 * If the String variable has never been set, return an empty string.
22315 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022316 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22317 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022319 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022320get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022321{
22322 static char_u mybuf[NUMBUFLEN];
22323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022324 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325}
22326
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022327 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022328get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022330 char_u *res = get_tv_string_buf_chk(varp, buf);
22331
22332 return res != NULL ? res : (char_u *)"";
22333}
22334
Bram Moolenaar7d647822014-04-05 21:28:56 +020022335/*
22336 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22337 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022338 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022339get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022340{
22341 static char_u mybuf[NUMBUFLEN];
22342
22343 return get_tv_string_buf_chk(varp, mybuf);
22344}
22345
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022346 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022347get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022348{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022349 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022351 case VAR_NUMBER:
22352 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22353 return buf;
22354 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022355 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022356 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022357 break;
22358 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022359 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022360 break;
22361 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022362 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022363 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022364 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022365#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022366 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022367 break;
22368#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022369 case VAR_STRING:
22370 if (varp->vval.v_string != NULL)
22371 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022372 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022373 case VAR_SPECIAL:
22374 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22375 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022376 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022377#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022378 {
22379 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022380 char *status;
22381
22382 if (job == NULL)
22383 return (char_u *)"no process";
22384 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022385 : job->jv_status == JOB_ENDED ? "dead"
22386 : "run";
22387# ifdef UNIX
22388 vim_snprintf((char *)buf, NUMBUFLEN,
22389 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022390# elif defined(WIN32)
22391 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022392 "process %ld %s",
22393 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022394 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022395# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022396 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022397 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22398# endif
22399 return buf;
22400 }
22401#endif
22402 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022403 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022404#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022405 {
22406 channel_T *channel = varp->vval.v_channel;
22407 char *status = channel_status(channel);
22408
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022409 if (channel == NULL)
22410 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22411 else
22412 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022413 "channel %d %s", channel->ch_id, status);
22414 return buf;
22415 }
22416#endif
22417 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022418 case VAR_UNKNOWN:
22419 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022420 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022422 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423}
22424
22425/*
22426 * Find variable "name" in the list of variables.
22427 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022428 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022429 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022430 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022431 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022432 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022433find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022435 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022436 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022437
Bram Moolenaara7043832005-01-21 11:56:39 +000022438 ht = find_var_ht(name, &varname);
22439 if (htp != NULL)
22440 *htp = ht;
22441 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022443 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444}
22445
22446/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022447 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022448 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022450 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022451find_var_in_ht(
22452 hashtab_T *ht,
22453 int htname,
22454 char_u *varname,
22455 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022456{
Bram Moolenaar33570922005-01-25 22:26:29 +000022457 hashitem_T *hi;
22458
22459 if (*varname == NUL)
22460 {
22461 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022462 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022463 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022464 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022465 case 'g': return &globvars_var;
22466 case 'v': return &vimvars_var;
22467 case 'b': return &curbuf->b_bufvar;
22468 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022469#ifdef FEAT_WINDOWS
22470 case 't': return &curtab->tp_winvar;
22471#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022472 case 'l': return current_funccal == NULL
22473 ? NULL : &current_funccal->l_vars_var;
22474 case 'a': return current_funccal == NULL
22475 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022476 }
22477 return NULL;
22478 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022479
22480 hi = hash_find(ht, varname);
22481 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022482 {
22483 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022484 * worked find the variable again. Don't auto-load a script if it was
22485 * loaded already, otherwise it would be loaded every time when
22486 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022487 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022488 {
22489 /* Note: script_autoload() may make "hi" invalid. It must either
22490 * be obtained again or not used. */
22491 if (!script_autoload(varname, FALSE) || aborting())
22492 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022493 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022494 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022495 if (HASHITEM_EMPTY(hi))
22496 return NULL;
22497 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022498 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022499}
22500
22501/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022502 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022503 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022504 * Set "varname" to the start of name without ':'.
22505 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022506 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022507find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022509 hashitem_T *hi;
22510
Bram Moolenaar73627d02015-08-11 15:46:09 +020022511 if (name[0] == NUL)
22512 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 if (name[1] != ':')
22514 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022515 /* The name must not start with a colon or #. */
22516 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 return NULL;
22518 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022519
22520 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022521 hi = hash_find(&compat_hashtab, name);
22522 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022523 return &compat_hashtab;
22524
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022526 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022527 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528 }
22529 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022530 if (*name == 'g') /* global variable */
22531 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022532 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22533 */
22534 if (vim_strchr(name + 2, ':') != NULL
22535 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022536 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022537 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022538 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022540 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022541#ifdef FEAT_WINDOWS
22542 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022543 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022544#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022545 if (*name == 'v') /* v: variable */
22546 return &vimvarht;
22547 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022548 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022549 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022550 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 if (*name == 's' /* script variable */
22552 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22553 return &SCRIPT_VARS(current_SID);
22554 return NULL;
22555}
22556
22557/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022558 * Get function call environment based on bactrace debug level
22559 */
22560 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022561get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022562{
22563 int i;
22564 funccall_T *funccal;
22565 funccall_T *temp_funccal;
22566
22567 funccal = current_funccal;
22568 if (debug_backtrace_level > 0)
22569 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022570 for (i = 0; i < debug_backtrace_level; i++)
22571 {
22572 temp_funccal = funccal->caller;
22573 if (temp_funccal)
22574 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022575 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022576 /* backtrace level overflow. reset to max */
22577 debug_backtrace_level = i;
22578 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022579 }
22580 return funccal;
22581}
22582
22583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022585 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 * Returns NULL when it doesn't exist.
22587 */
22588 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022589get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590{
Bram Moolenaar33570922005-01-25 22:26:29 +000022591 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022593 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 if (v == NULL)
22595 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022596 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597}
22598
22599/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022600 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 * sourcing this script and when executing functions defined in the script.
22602 */
22603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022604new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605{
Bram Moolenaara7043832005-01-21 11:56:39 +000022606 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022607 hashtab_T *ht;
22608 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022609
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22611 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022612 /* Re-allocating ga_data means that an ht_array pointing to
22613 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022614 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022615 for (i = 1; i <= ga_scripts.ga_len; ++i)
22616 {
22617 ht = &SCRIPT_VARS(i);
22618 if (ht->ht_mask == HT_INIT_SIZE - 1)
22619 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022620 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022621 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022622 }
22623
Bram Moolenaar071d4272004-06-13 20:20:40 +000022624 while (ga_scripts.ga_len < id)
22625 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022626 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022627 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022628 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 }
22631 }
22632}
22633
22634/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022635 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22636 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 */
22638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022639init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640{
Bram Moolenaar33570922005-01-25 22:26:29 +000022641 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022642 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022643 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022644 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022645 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022646 dict_var->di_tv.vval.v_dict = dict;
22647 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022648 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022649 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22650 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651}
22652
22653/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022654 * Unreference a dictionary initialized by init_var_dict().
22655 */
22656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022657unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022658{
22659 /* Now the dict needs to be freed if no one else is using it, go back to
22660 * normal reference counting. */
22661 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22662 dict_unref(dict);
22663}
22664
22665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022667 * Frees all allocated variables and the value they contain.
22668 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669 */
22670 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022671vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022672{
22673 vars_clear_ext(ht, TRUE);
22674}
22675
22676/*
22677 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22678 */
22679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022680vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681{
Bram Moolenaara7043832005-01-21 11:56:39 +000022682 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022683 hashitem_T *hi;
22684 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685
Bram Moolenaar33570922005-01-25 22:26:29 +000022686 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022687 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022688 for (hi = ht->ht_array; todo > 0; ++hi)
22689 {
22690 if (!HASHITEM_EMPTY(hi))
22691 {
22692 --todo;
22693
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022695 * ht_array might change then. hash_clear() takes care of it
22696 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022697 v = HI2DI(hi);
22698 if (free_val)
22699 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022700 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022701 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022702 }
22703 }
22704 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022705 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706}
22707
Bram Moolenaara7043832005-01-21 11:56:39 +000022708/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022709 * Delete a variable from hashtab "ht" at item "hi".
22710 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022713delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714{
Bram Moolenaar33570922005-01-25 22:26:29 +000022715 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022716
22717 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022718 clear_tv(&di->di_tv);
22719 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720}
22721
22722/*
22723 * List the value of one internal variable.
22724 */
22725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022726list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022727{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022728 char_u *tofree;
22729 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022730 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022731
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022732 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022733 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022734 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022735 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736}
22737
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022739list_one_var_a(
22740 char_u *prefix,
22741 char_u *name,
22742 int type,
22743 char_u *string,
22744 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745{
Bram Moolenaar31859182007-08-14 20:41:13 +000022746 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22747 msg_start();
22748 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 if (name != NULL) /* "a:" vars don't have a name stored */
22750 msg_puts(name);
22751 msg_putchar(' ');
22752 msg_advance(22);
22753 if (type == VAR_NUMBER)
22754 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022755 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022756 msg_putchar('*');
22757 else if (type == VAR_LIST)
22758 {
22759 msg_putchar('[');
22760 if (*string == '[')
22761 ++string;
22762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022763 else if (type == VAR_DICT)
22764 {
22765 msg_putchar('{');
22766 if (*string == '{')
22767 ++string;
22768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 else
22770 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022771
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022773
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022774 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022775 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022776 if (*first)
22777 {
22778 msg_clr_eos();
22779 *first = FALSE;
22780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781}
22782
22783/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022784 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785 * If the variable already exists, the value is updated.
22786 * Otherwise the variable is created.
22787 */
22788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022789set_var(
22790 char_u *name,
22791 typval_T *tv,
22792 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793{
Bram Moolenaar33570922005-01-25 22:26:29 +000022794 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022796 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022798 ht = find_var_ht(name, &varname);
22799 if (ht == NULL || *varname == NUL)
22800 {
22801 EMSG2(_(e_illvar), name);
22802 return;
22803 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022804 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022805
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022806 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22807 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022808 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022809
Bram Moolenaar33570922005-01-25 22:26:29 +000022810 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022812 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022813 if (var_check_ro(v->di_flags, name, FALSE)
22814 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022815 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022816
22817 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022818 * Handle setting internal v: variables separately where needed to
22819 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022820 */
22821 if (ht == &vimvarht)
22822 {
22823 if (v->di_tv.v_type == VAR_STRING)
22824 {
22825 vim_free(v->di_tv.vval.v_string);
22826 if (copy || tv->v_type != VAR_STRING)
22827 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22828 else
22829 {
22830 /* Take over the string to avoid an extra alloc/free. */
22831 v->di_tv.vval.v_string = tv->vval.v_string;
22832 tv->vval.v_string = NULL;
22833 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022834 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022835 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022836 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022837 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022838 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022839 if (STRCMP(varname, "searchforward") == 0)
22840 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022841#ifdef FEAT_SEARCH_EXTRA
22842 else if (STRCMP(varname, "hlsearch") == 0)
22843 {
22844 no_hlsearch = !v->di_tv.vval.v_number;
22845 redraw_all_later(SOME_VALID);
22846 }
22847#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022848 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022849 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022850 else if (v->di_tv.v_type != tv->v_type)
22851 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022852 }
22853
22854 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 }
22856 else /* add a new variable */
22857 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022858 /* Can't add "v:" variable. */
22859 if (ht == &vimvarht)
22860 {
22861 EMSG2(_(e_illvar), name);
22862 return;
22863 }
22864
Bram Moolenaar92124a32005-06-17 22:03:40 +000022865 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022866 if (!valid_varname(varname))
22867 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022868
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022869 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22870 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022871 if (v == NULL)
22872 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022873 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022874 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022876 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 return;
22878 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022879 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022881
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022882 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022883 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022884 else
22885 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022886 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022887 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022888 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890}
22891
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022892/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022893 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022894 * Also give an error message.
22895 */
22896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022897var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022898{
22899 if (flags & DI_FLAGS_RO)
22900 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022901 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022902 return TRUE;
22903 }
22904 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22905 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022906 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022907 return TRUE;
22908 }
22909 return FALSE;
22910}
22911
22912/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022913 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22914 * Also give an error message.
22915 */
22916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022917var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022918{
22919 if (flags & DI_FLAGS_FIX)
22920 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022921 EMSG2(_("E795: Cannot delete variable %s"),
22922 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022923 return TRUE;
22924 }
22925 return FALSE;
22926}
22927
22928/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022929 * Check if a funcref is assigned to a valid variable name.
22930 * Return TRUE and give an error if not.
22931 */
22932 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022933var_check_func_name(
22934 char_u *name, /* points to start of variable name */
22935 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022936{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022937 /* Allow for w: b: s: and t:. */
22938 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022939 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22940 ? name[2] : name[0]))
22941 {
22942 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22943 name);
22944 return TRUE;
22945 }
22946 /* Don't allow hiding a function. When "v" is not NULL we might be
22947 * assigning another function to the same var, the type is checked
22948 * below. */
22949 if (new_var && function_exists(name))
22950 {
22951 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22952 name);
22953 return TRUE;
22954 }
22955 return FALSE;
22956}
22957
22958/*
22959 * Check if a variable name is valid.
22960 * Return FALSE and give an error if not.
22961 */
22962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022963valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022964{
22965 char_u *p;
22966
22967 for (p = varname; *p != NUL; ++p)
22968 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22969 && *p != AUTOLOAD_CHAR)
22970 {
22971 EMSG2(_(e_illvar), varname);
22972 return FALSE;
22973 }
22974 return TRUE;
22975}
22976
22977/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022978 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022979 * Also give an error message, using "name" or _("name") when use_gettext is
22980 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022981 */
22982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022983tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022984{
22985 if (lock & VAR_LOCKED)
22986 {
22987 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022988 name == NULL ? (char_u *)_("Unknown")
22989 : use_gettext ? (char_u *)_(name)
22990 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022991 return TRUE;
22992 }
22993 if (lock & VAR_FIXED)
22994 {
22995 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022996 name == NULL ? (char_u *)_("Unknown")
22997 : use_gettext ? (char_u *)_(name)
22998 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022999 return TRUE;
23000 }
23001 return FALSE;
23002}
23003
23004/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023005 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023006 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023007 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023008 * It is OK for "from" and "to" to point to the same item. This is used to
23009 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023010 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023012copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023014 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023015 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023016 switch (from->v_type)
23017 {
23018 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023019 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023020 to->vval.v_number = from->vval.v_number;
23021 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023022 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023023#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023024 to->vval.v_float = from->vval.v_float;
23025 break;
23026#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023027 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023028#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023029 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023030 if (to->vval.v_job != NULL)
23031 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023032 break;
23033#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023034 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023035#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023036 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023037 if (to->vval.v_channel != NULL)
23038 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023039 break;
23040#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023041 case VAR_STRING:
23042 case VAR_FUNC:
23043 if (from->vval.v_string == NULL)
23044 to->vval.v_string = NULL;
23045 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023047 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023048 if (from->v_type == VAR_FUNC)
23049 func_ref(to->vval.v_string);
23050 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023051 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023052 case VAR_PARTIAL:
23053 if (from->vval.v_partial == NULL)
23054 to->vval.v_partial = NULL;
23055 else
23056 {
23057 to->vval.v_partial = from->vval.v_partial;
23058 ++to->vval.v_partial->pt_refcount;
23059 }
23060 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023061 case VAR_LIST:
23062 if (from->vval.v_list == NULL)
23063 to->vval.v_list = NULL;
23064 else
23065 {
23066 to->vval.v_list = from->vval.v_list;
23067 ++to->vval.v_list->lv_refcount;
23068 }
23069 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023070 case VAR_DICT:
23071 if (from->vval.v_dict == NULL)
23072 to->vval.v_dict = NULL;
23073 else
23074 {
23075 to->vval.v_dict = from->vval.v_dict;
23076 ++to->vval.v_dict->dv_refcount;
23077 }
23078 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023079 case VAR_UNKNOWN:
23080 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023081 break;
23082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083}
23084
23085/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023086 * Make a copy of an item.
23087 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023088 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23089 * reference to an already copied list/dict can be used.
23090 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023091 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023093item_copy(
23094 typval_T *from,
23095 typval_T *to,
23096 int deep,
23097 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023098{
23099 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023100 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023101
Bram Moolenaar33570922005-01-25 22:26:29 +000023102 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023103 {
23104 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023105 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023106 }
23107 ++recurse;
23108
23109 switch (from->v_type)
23110 {
23111 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023112 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023113 case VAR_STRING:
23114 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023115 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023116 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023117 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023118 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023119 copy_tv(from, to);
23120 break;
23121 case VAR_LIST:
23122 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023123 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023124 if (from->vval.v_list == NULL)
23125 to->vval.v_list = NULL;
23126 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23127 {
23128 /* use the copy made earlier */
23129 to->vval.v_list = from->vval.v_list->lv_copylist;
23130 ++to->vval.v_list->lv_refcount;
23131 }
23132 else
23133 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23134 if (to->vval.v_list == NULL)
23135 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023136 break;
23137 case VAR_DICT:
23138 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023139 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023140 if (from->vval.v_dict == NULL)
23141 to->vval.v_dict = NULL;
23142 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23143 {
23144 /* use the copy made earlier */
23145 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23146 ++to->vval.v_dict->dv_refcount;
23147 }
23148 else
23149 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23150 if (to->vval.v_dict == NULL)
23151 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023152 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023153 case VAR_UNKNOWN:
23154 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023155 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023156 }
23157 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023158 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023159}
23160
23161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 * ":echo expr1 ..." print each argument separated with a space, add a
23163 * newline at the end.
23164 * ":echon expr1 ..." print each argument plain.
23165 */
23166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023167ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168{
23169 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023171 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 char_u *p;
23173 int needclr = TRUE;
23174 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023175 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176
23177 if (eap->skip)
23178 ++emsg_skip;
23179 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23180 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023181 /* If eval1() causes an error message the text from the command may
23182 * still need to be cleared. E.g., "echo 22,44". */
23183 need_clr_eos = needclr;
23184
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023186 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 {
23188 /*
23189 * Report the invalid expression unless the expression evaluation
23190 * has been cancelled due to an aborting error, an interrupt, or an
23191 * exception.
23192 */
23193 if (!aborting())
23194 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023195 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023196 break;
23197 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023198 need_clr_eos = FALSE;
23199
Bram Moolenaar071d4272004-06-13 20:20:40 +000023200 if (!eap->skip)
23201 {
23202 if (atstart)
23203 {
23204 atstart = FALSE;
23205 /* Call msg_start() after eval1(), evaluating the expression
23206 * may cause a message to appear. */
23207 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023208 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023209 /* Mark the saved text as finishing the line, so that what
23210 * follows is displayed on a new line when scrolling back
23211 * at the more prompt. */
23212 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 }
23216 else if (eap->cmdidx == CMD_echo)
23217 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023218 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023219 if (p != NULL)
23220 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023222 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023224 if (*p != TAB && needclr)
23225 {
23226 /* remove any text still there from the command */
23227 msg_clr_eos();
23228 needclr = FALSE;
23229 }
23230 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 }
23232 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023233 {
23234#ifdef FEAT_MBYTE
23235 if (has_mbyte)
23236 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023237 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023238
23239 (void)msg_outtrans_len_attr(p, i, echo_attr);
23240 p += i - 1;
23241 }
23242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023244 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023247 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023249 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 arg = skipwhite(arg);
23251 }
23252 eap->nextcmd = check_nextcmd(arg);
23253
23254 if (eap->skip)
23255 --emsg_skip;
23256 else
23257 {
23258 /* remove text that may still be there from the command */
23259 if (needclr)
23260 msg_clr_eos();
23261 if (eap->cmdidx == CMD_echo)
23262 msg_end();
23263 }
23264}
23265
23266/*
23267 * ":echohl {name}".
23268 */
23269 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023270ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271{
23272 int id;
23273
23274 id = syn_name2id(eap->arg);
23275 if (id == 0)
23276 echo_attr = 0;
23277 else
23278 echo_attr = syn_id2attr(id);
23279}
23280
23281/*
23282 * ":execute expr1 ..." execute the result of an expression.
23283 * ":echomsg expr1 ..." Print a message
23284 * ":echoerr expr1 ..." Print an error
23285 * Each gets spaces around each argument and a newline at the end for
23286 * echo commands
23287 */
23288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023289ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290{
23291 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023292 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 int ret = OK;
23294 char_u *p;
23295 garray_T ga;
23296 int len;
23297 int save_did_emsg;
23298
23299 ga_init2(&ga, 1, 80);
23300
23301 if (eap->skip)
23302 ++emsg_skip;
23303 while (*arg != NUL && *arg != '|' && *arg != '\n')
23304 {
23305 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023306 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 {
23308 /*
23309 * Report the invalid expression unless the expression evaluation
23310 * has been cancelled due to an aborting error, an interrupt, or an
23311 * exception.
23312 */
23313 if (!aborting())
23314 EMSG2(_(e_invexpr2), p);
23315 ret = FAIL;
23316 break;
23317 }
23318
23319 if (!eap->skip)
23320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023321 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322 len = (int)STRLEN(p);
23323 if (ga_grow(&ga, len + 2) == FAIL)
23324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023325 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 ret = FAIL;
23327 break;
23328 }
23329 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332 ga.ga_len += len;
23333 }
23334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023335 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336 arg = skipwhite(arg);
23337 }
23338
23339 if (ret != FAIL && ga.ga_data != NULL)
23340 {
23341 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023343 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023344 out_flush();
23345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023346 else if (eap->cmdidx == CMD_echoerr)
23347 {
23348 /* We don't want to abort following commands, restore did_emsg. */
23349 save_did_emsg = did_emsg;
23350 EMSG((char_u *)ga.ga_data);
23351 if (!force_abort)
23352 did_emsg = save_did_emsg;
23353 }
23354 else if (eap->cmdidx == CMD_execute)
23355 do_cmdline((char_u *)ga.ga_data,
23356 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23357 }
23358
23359 ga_clear(&ga);
23360
23361 if (eap->skip)
23362 --emsg_skip;
23363
23364 eap->nextcmd = check_nextcmd(arg);
23365}
23366
23367/*
23368 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23369 * "arg" points to the "&" or '+' when called, to "option" when returning.
23370 * Returns NULL when no option name found. Otherwise pointer to the char
23371 * after the option name.
23372 */
23373 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023374find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375{
23376 char_u *p = *arg;
23377
23378 ++p;
23379 if (*p == 'g' && p[1] == ':')
23380 {
23381 *opt_flags = OPT_GLOBAL;
23382 p += 2;
23383 }
23384 else if (*p == 'l' && p[1] == ':')
23385 {
23386 *opt_flags = OPT_LOCAL;
23387 p += 2;
23388 }
23389 else
23390 *opt_flags = 0;
23391
23392 if (!ASCII_ISALPHA(*p))
23393 return NULL;
23394 *arg = p;
23395
23396 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23397 p += 4; /* termcap option */
23398 else
23399 while (ASCII_ISALPHA(*p))
23400 ++p;
23401 return p;
23402}
23403
23404/*
23405 * ":function"
23406 */
23407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023408ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409{
23410 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023411 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412 int j;
23413 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023414 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023415 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023416 char_u *name = NULL;
23417 char_u *p;
23418 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023419 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023420 garray_T newargs;
23421 garray_T newlines;
23422 int varargs = FALSE;
23423 int mustend = FALSE;
23424 int flags = 0;
23425 ufunc_T *fp;
23426 int indent;
23427 int nesting;
23428 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023429 dictitem_T *v;
23430 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023431 static int func_nr = 0; /* number for nameless function */
23432 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023433 hashtab_T *ht;
23434 int todo;
23435 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023436 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437
23438 /*
23439 * ":function" without argument: list functions.
23440 */
23441 if (ends_excmd(*eap->arg))
23442 {
23443 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023444 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023445 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023446 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023447 {
23448 if (!HASHITEM_EMPTY(hi))
23449 {
23450 --todo;
23451 fp = HI2UF(hi);
23452 if (!isdigit(*fp->uf_name))
23453 list_func_head(fp, FALSE);
23454 }
23455 }
23456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457 eap->nextcmd = check_nextcmd(eap->arg);
23458 return;
23459 }
23460
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023461 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023462 * ":function /pat": list functions matching pattern.
23463 */
23464 if (*eap->arg == '/')
23465 {
23466 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23467 if (!eap->skip)
23468 {
23469 regmatch_T regmatch;
23470
23471 c = *p;
23472 *p = NUL;
23473 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23474 *p = c;
23475 if (regmatch.regprog != NULL)
23476 {
23477 regmatch.rm_ic = p_ic;
23478
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023479 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023480 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23481 {
23482 if (!HASHITEM_EMPTY(hi))
23483 {
23484 --todo;
23485 fp = HI2UF(hi);
23486 if (!isdigit(*fp->uf_name)
23487 && vim_regexec(&regmatch, fp->uf_name, 0))
23488 list_func_head(fp, FALSE);
23489 }
23490 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023491 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023492 }
23493 }
23494 if (*p == '/')
23495 ++p;
23496 eap->nextcmd = check_nextcmd(p);
23497 return;
23498 }
23499
23500 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023501 * Get the function name. There are these situations:
23502 * func normal function name
23503 * "name" == func, "fudi.fd_dict" == NULL
23504 * dict.func new dictionary entry
23505 * "name" == NULL, "fudi.fd_dict" set,
23506 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23507 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023508 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023509 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23510 * dict.func existing dict entry that's not a Funcref
23511 * "name" == NULL, "fudi.fd_dict" set,
23512 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023513 * s:func script-local function name
23514 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023517 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023518 paren = (vim_strchr(p, '(') != NULL);
23519 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023520 {
23521 /*
23522 * Return on an invalid expression in braces, unless the expression
23523 * evaluation has been cancelled due to an aborting error, an
23524 * interrupt, or an exception.
23525 */
23526 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023527 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023528 if (!eap->skip && fudi.fd_newkey != NULL)
23529 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023530 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023533 else
23534 eap->skip = TRUE;
23535 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023536
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 /* An error in a function call during evaluation of an expression in magic
23538 * braces should not cause the function not to be defined. */
23539 saved_did_emsg = did_emsg;
23540 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541
23542 /*
23543 * ":function func" with only function name: list function.
23544 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023545 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 {
23547 if (!ends_excmd(*skipwhite(p)))
23548 {
23549 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023550 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 }
23552 eap->nextcmd = check_nextcmd(p);
23553 if (eap->nextcmd != NULL)
23554 *p = NUL;
23555 if (!eap->skip && !got_int)
23556 {
23557 fp = find_func(name);
23558 if (fp != NULL)
23559 {
23560 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023561 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023563 if (FUNCLINE(fp, j) == NULL)
23564 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 msg_putchar('\n');
23566 msg_outnum((long)(j + 1));
23567 if (j < 9)
23568 msg_putchar(' ');
23569 if (j < 99)
23570 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023571 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 out_flush(); /* show a line at a time */
23573 ui_breakcheck();
23574 }
23575 if (!got_int)
23576 {
23577 msg_putchar('\n');
23578 msg_puts((char_u *)" endfunction");
23579 }
23580 }
23581 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023582 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023584 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585 }
23586
23587 /*
23588 * ":function name(arg1, arg2)" Define function.
23589 */
23590 p = skipwhite(p);
23591 if (*p != '(')
23592 {
23593 if (!eap->skip)
23594 {
23595 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023596 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 }
23598 /* attempt to continue by skipping some text */
23599 if (vim_strchr(p, '(') != NULL)
23600 p = vim_strchr(p, '(');
23601 }
23602 p = skipwhite(p + 1);
23603
23604 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23605 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23606
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023607 if (!eap->skip)
23608 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023609 /* Check the name of the function. Unless it's a dictionary function
23610 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023611 if (name != NULL)
23612 arg = name;
23613 else
23614 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023615 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023616 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23617 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023618 {
23619 if (*arg == K_SPECIAL)
23620 j = 3;
23621 else
23622 j = 0;
23623 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23624 : eval_isnamec(arg[j])))
23625 ++j;
23626 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023627 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023628 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023629 /* Disallow using the g: dict. */
23630 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23631 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023632 }
23633
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 /*
23635 * Isolate the arguments: "arg1, arg2, ...)"
23636 */
23637 while (*p != ')')
23638 {
23639 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23640 {
23641 varargs = TRUE;
23642 p += 3;
23643 mustend = TRUE;
23644 }
23645 else
23646 {
23647 arg = p;
23648 while (ASCII_ISALNUM(*p) || *p == '_')
23649 ++p;
23650 if (arg == p || isdigit(*arg)
23651 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23652 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23653 {
23654 if (!eap->skip)
23655 EMSG2(_("E125: Illegal argument: %s"), arg);
23656 break;
23657 }
23658 if (ga_grow(&newargs, 1) == FAIL)
23659 goto erret;
23660 c = *p;
23661 *p = NUL;
23662 arg = vim_strsave(arg);
23663 if (arg == NULL)
23664 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023665
23666 /* Check for duplicate argument name. */
23667 for (i = 0; i < newargs.ga_len; ++i)
23668 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23669 {
23670 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023671 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023672 goto erret;
23673 }
23674
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23676 *p = c;
23677 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678 if (*p == ',')
23679 ++p;
23680 else
23681 mustend = TRUE;
23682 }
23683 p = skipwhite(p);
23684 if (mustend && *p != ')')
23685 {
23686 if (!eap->skip)
23687 EMSG2(_(e_invarg2), eap->arg);
23688 break;
23689 }
23690 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023691 if (*p != ')')
23692 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 ++p; /* skip the ')' */
23694
Bram Moolenaare9a41262005-01-15 22:18:47 +000023695 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696 for (;;)
23697 {
23698 p = skipwhite(p);
23699 if (STRNCMP(p, "range", 5) == 0)
23700 {
23701 flags |= FC_RANGE;
23702 p += 5;
23703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023704 else if (STRNCMP(p, "dict", 4) == 0)
23705 {
23706 flags |= FC_DICT;
23707 p += 4;
23708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709 else if (STRNCMP(p, "abort", 5) == 0)
23710 {
23711 flags |= FC_ABORT;
23712 p += 5;
23713 }
23714 else
23715 break;
23716 }
23717
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023718 /* When there is a line break use what follows for the function body.
23719 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23720 if (*p == '\n')
23721 line_arg = p + 1;
23722 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 EMSG(_(e_trailing));
23724
23725 /*
23726 * Read the body of the function, until ":endfunction" is found.
23727 */
23728 if (KeyTyped)
23729 {
23730 /* Check if the function already exists, don't let the user type the
23731 * whole function before telling him it doesn't work! For a script we
23732 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023733 if (!eap->skip && !eap->forceit)
23734 {
23735 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23736 EMSG(_(e_funcdict));
23737 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023738 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023741 if (!eap->skip && did_emsg)
23742 goto erret;
23743
Bram Moolenaar071d4272004-06-13 20:20:40 +000023744 msg_putchar('\n'); /* don't overwrite the function name */
23745 cmdline_row = msg_row;
23746 }
23747
23748 indent = 2;
23749 nesting = 0;
23750 for (;;)
23751 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023752 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023753 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023754 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023755 saved_wait_return = FALSE;
23756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023758 sourcing_lnum_off = sourcing_lnum;
23759
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023760 if (line_arg != NULL)
23761 {
23762 /* Use eap->arg, split up in parts by line breaks. */
23763 theline = line_arg;
23764 p = vim_strchr(theline, '\n');
23765 if (p == NULL)
23766 line_arg += STRLEN(line_arg);
23767 else
23768 {
23769 *p = NUL;
23770 line_arg = p + 1;
23771 }
23772 }
23773 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 theline = getcmdline(':', 0L, indent);
23775 else
23776 theline = eap->getline(':', eap->cookie, indent);
23777 if (KeyTyped)
23778 lines_left = Rows - 1;
23779 if (theline == NULL)
23780 {
23781 EMSG(_("E126: Missing :endfunction"));
23782 goto erret;
23783 }
23784
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023785 /* Detect line continuation: sourcing_lnum increased more than one. */
23786 if (sourcing_lnum > sourcing_lnum_off + 1)
23787 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23788 else
23789 sourcing_lnum_off = 0;
23790
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 if (skip_until != NULL)
23792 {
23793 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23794 * don't check for ":endfunc". */
23795 if (STRCMP(theline, skip_until) == 0)
23796 {
23797 vim_free(skip_until);
23798 skip_until = NULL;
23799 }
23800 }
23801 else
23802 {
23803 /* skip ':' and blanks*/
23804 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23805 ;
23806
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023807 /* Check for "endfunction". */
23808 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023810 if (line_arg == NULL)
23811 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812 break;
23813 }
23814
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023815 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023816 * at "end". */
23817 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23818 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023819 else if (STRNCMP(p, "if", 2) == 0
23820 || STRNCMP(p, "wh", 2) == 0
23821 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 || STRNCMP(p, "try", 3) == 0)
23823 indent += 2;
23824
23825 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023826 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023828 if (*p == '!')
23829 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023831 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023832 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023833 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023834 ++nesting;
23835 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836 }
23837 }
23838
23839 /* Check for ":append" or ":insert". */
23840 p = skip_range(p, NULL);
23841 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23842 || (p[0] == 'i'
23843 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23844 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23845 skip_until = vim_strsave((char_u *)".");
23846
23847 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23848 arg = skipwhite(skiptowhite(p));
23849 if (arg[0] == '<' && arg[1] =='<'
23850 && ((p[0] == 'p' && p[1] == 'y'
23851 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23852 || (p[0] == 'p' && p[1] == 'e'
23853 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23854 || (p[0] == 't' && p[1] == 'c'
23855 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023856 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23857 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23859 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023860 || (p[0] == 'm' && p[1] == 'z'
23861 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 ))
23863 {
23864 /* ":python <<" continues until a dot, like ":append" */
23865 p = skipwhite(arg + 2);
23866 if (*p == NUL)
23867 skip_until = vim_strsave((char_u *)".");
23868 else
23869 skip_until = vim_strsave(p);
23870 }
23871 }
23872
23873 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023874 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023875 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023876 if (line_arg == NULL)
23877 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023879 }
23880
23881 /* Copy the line to newly allocated memory. get_one_sourceline()
23882 * allocates 250 bytes per line, this saves 80% on average. The cost
23883 * is an extra alloc/free. */
23884 p = vim_strsave(theline);
23885 if (p != NULL)
23886 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023887 if (line_arg == NULL)
23888 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023889 theline = p;
23890 }
23891
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023892 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23893
23894 /* Add NULL lines for continuation lines, so that the line count is
23895 * equal to the index in the growarray. */
23896 while (sourcing_lnum_off-- > 0)
23897 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023898
23899 /* Check for end of eap->arg. */
23900 if (line_arg != NULL && *line_arg == NUL)
23901 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 }
23903
23904 /* Don't define the function when skipping commands or when an error was
23905 * detected. */
23906 if (eap->skip || did_emsg)
23907 goto erret;
23908
23909 /*
23910 * If there are no errors, add the function
23911 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023912 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023913 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023914 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023915 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023916 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023917 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023918 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023919 goto erret;
23920 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023921
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023922 fp = find_func(name);
23923 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023924 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023925 if (!eap->forceit)
23926 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023927 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023928 goto erret;
23929 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023930 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023931 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023932 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023933 name);
23934 goto erret;
23935 }
23936 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023937 ga_clear_strings(&(fp->uf_args));
23938 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023939 vim_free(name);
23940 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 }
23943 else
23944 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023945 char numbuf[20];
23946
23947 fp = NULL;
23948 if (fudi.fd_newkey == NULL && !eap->forceit)
23949 {
23950 EMSG(_(e_funcdict));
23951 goto erret;
23952 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023953 if (fudi.fd_di == NULL)
23954 {
23955 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023956 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023957 goto erret;
23958 }
23959 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023960 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023961 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023962
23963 /* Give the function a sequential number. Can only be used with a
23964 * Funcref! */
23965 vim_free(name);
23966 sprintf(numbuf, "%d", ++func_nr);
23967 name = vim_strsave((char_u *)numbuf);
23968 if (name == NULL)
23969 goto erret;
23970 }
23971
23972 if (fp == NULL)
23973 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023974 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023975 {
23976 int slen, plen;
23977 char_u *scriptname;
23978
23979 /* Check that the autoload name matches the script name. */
23980 j = FAIL;
23981 if (sourcing_name != NULL)
23982 {
23983 scriptname = autoload_name(name);
23984 if (scriptname != NULL)
23985 {
23986 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023987 plen = (int)STRLEN(p);
23988 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023989 if (slen > plen && fnamecmp(p,
23990 sourcing_name + slen - plen) == 0)
23991 j = OK;
23992 vim_free(scriptname);
23993 }
23994 }
23995 if (j == FAIL)
23996 {
23997 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23998 goto erret;
23999 }
24000 }
24001
24002 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 if (fp == NULL)
24004 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024005
24006 if (fudi.fd_dict != NULL)
24007 {
24008 if (fudi.fd_di == NULL)
24009 {
24010 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024011 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012 if (fudi.fd_di == NULL)
24013 {
24014 vim_free(fp);
24015 goto erret;
24016 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024017 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24018 {
24019 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024020 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024021 goto erret;
24022 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024023 }
24024 else
24025 /* overwrite existing dict entry */
24026 clear_tv(&fudi.fd_di->di_tv);
24027 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024028 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024029 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024030 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024031
24032 /* behave like "dict" was used */
24033 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024034 }
24035
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024037 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024038 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24039 {
24040 vim_free(fp);
24041 goto erret;
24042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024043 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024044 fp->uf_args = newargs;
24045 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024046#ifdef FEAT_PROFILE
24047 fp->uf_tml_count = NULL;
24048 fp->uf_tml_total = NULL;
24049 fp->uf_tml_self = NULL;
24050 fp->uf_profiling = FALSE;
24051 if (prof_def_func())
24052 func_do_profile(fp);
24053#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024054 fp->uf_varargs = varargs;
24055 fp->uf_flags = flags;
24056 fp->uf_calls = 0;
24057 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024058 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059
24060erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024061 ga_clear_strings(&newargs);
24062 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024063ret_free:
24064 vim_free(skip_until);
24065 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024066 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024068 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069}
24070
24071/*
24072 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024073 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024074 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024075 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024076 * TFN_INT: internal function name OK
24077 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024078 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 * Advances "pp" to just after the function name (if no error).
24080 */
24081 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024082trans_function_name(
24083 char_u **pp,
24084 int skip, /* only find the end, don't evaluate */
24085 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024086 funcdict_T *fdp, /* return: info about dictionary used */
24087 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024089 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 char_u *start;
24091 char_u *end;
24092 int lead;
24093 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024094 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024095 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024096
24097 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024098 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024100
24101 /* Check for hard coded <SNR>: already translated function ID (from a user
24102 * command). */
24103 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24104 && (*pp)[2] == (int)KE_SNR)
24105 {
24106 *pp += 3;
24107 len = get_id_len(pp) + 3;
24108 return vim_strnsave(start, len);
24109 }
24110
24111 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24112 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024114 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024115 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024116
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024117 /* Note that TFN_ flags use the same values as GLV_ flags. */
24118 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024119 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024120 if (end == start)
24121 {
24122 if (!skip)
24123 EMSG(_("E129: Function name required"));
24124 goto theend;
24125 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024126 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024127 {
24128 /*
24129 * Report an invalid expression in braces, unless the expression
24130 * evaluation has been cancelled due to an aborting error, an
24131 * interrupt, or an exception.
24132 */
24133 if (!aborting())
24134 {
24135 if (end != NULL)
24136 EMSG2(_(e_invarg2), start);
24137 }
24138 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024139 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024140 goto theend;
24141 }
24142
24143 if (lv.ll_tv != NULL)
24144 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024145 if (fdp != NULL)
24146 {
24147 fdp->fd_dict = lv.ll_dict;
24148 fdp->fd_newkey = lv.ll_newkey;
24149 lv.ll_newkey = NULL;
24150 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024151 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024152 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24153 {
24154 name = vim_strsave(lv.ll_tv->vval.v_string);
24155 *pp = end;
24156 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024157 else if (lv.ll_tv->v_type == VAR_PARTIAL
24158 && lv.ll_tv->vval.v_partial != NULL)
24159 {
24160 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24161 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024162 if (partial != NULL)
24163 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024164 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024165 else
24166 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024167 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24168 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024169 EMSG(_(e_funcref));
24170 else
24171 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024172 name = NULL;
24173 }
24174 goto theend;
24175 }
24176
24177 if (lv.ll_name == NULL)
24178 {
24179 /* Error found, but continue after the function name. */
24180 *pp = end;
24181 goto theend;
24182 }
24183
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024184 /* Check if the name is a Funcref. If so, use the value. */
24185 if (lv.ll_exp_name != NULL)
24186 {
24187 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024188 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024189 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024190 if (name == lv.ll_exp_name)
24191 name = NULL;
24192 }
24193 else
24194 {
24195 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024196 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024197 if (name == *pp)
24198 name = NULL;
24199 }
24200 if (name != NULL)
24201 {
24202 name = vim_strsave(name);
24203 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024204 if (STRNCMP(name, "<SNR>", 5) == 0)
24205 {
24206 /* Change "<SNR>" to the byte sequence. */
24207 name[0] = K_SPECIAL;
24208 name[1] = KS_EXTRA;
24209 name[2] = (int)KE_SNR;
24210 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24211 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024212 goto theend;
24213 }
24214
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024215 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024216 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024217 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024218 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24219 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24220 {
24221 /* When there was "s:" already or the name expanded to get a
24222 * leading "s:" then remove it. */
24223 lv.ll_name += 2;
24224 len -= 2;
24225 lead = 2;
24226 }
24227 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024228 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024229 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024230 /* skip over "s:" and "g:" */
24231 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024232 lv.ll_name += 2;
24233 len = (int)(end - lv.ll_name);
24234 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024235
24236 /*
24237 * Copy the function name to allocated memory.
24238 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24239 * Accept <SNR>123_name() outside a script.
24240 */
24241 if (skip)
24242 lead = 0; /* do nothing */
24243 else if (lead > 0)
24244 {
24245 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024246 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24247 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024248 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024249 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024250 if (current_SID <= 0)
24251 {
24252 EMSG(_(e_usingsid));
24253 goto theend;
24254 }
24255 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24256 lead += (int)STRLEN(sid_buf);
24257 }
24258 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024259 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024260 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024261 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024262 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024263 goto theend;
24264 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024265 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024266 {
24267 char_u *cp = vim_strchr(lv.ll_name, ':');
24268
24269 if (cp != NULL && cp < end)
24270 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024271 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024272 goto theend;
24273 }
24274 }
24275
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024276 name = alloc((unsigned)(len + lead + 1));
24277 if (name != NULL)
24278 {
24279 if (lead > 0)
24280 {
24281 name[0] = K_SPECIAL;
24282 name[1] = KS_EXTRA;
24283 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024284 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024285 STRCPY(name + 3, sid_buf);
24286 }
24287 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024288 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024289 }
24290 *pp = end;
24291
24292theend:
24293 clear_lval(&lv);
24294 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295}
24296
24297/*
24298 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24299 * Return 2 if "p" starts with "s:".
24300 * Return 0 otherwise.
24301 */
24302 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024303eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024304{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024305 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24306 * the standard library function. */
24307 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24308 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309 return 5;
24310 if (p[0] == 's' && p[1] == ':')
24311 return 2;
24312 return 0;
24313}
24314
24315/*
24316 * Return TRUE if "p" starts with "<SID>" or "s:".
24317 * Only works if eval_fname_script() returned non-zero for "p"!
24318 */
24319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024320eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024321{
24322 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24323}
24324
24325/*
24326 * List the head of the function: "name(arg1, arg2)".
24327 */
24328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024329list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330{
24331 int j;
24332
24333 msg_start();
24334 if (indent)
24335 MSG_PUTS(" ");
24336 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024337 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024338 {
24339 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024340 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024341 }
24342 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024343 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024345 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346 {
24347 if (j)
24348 MSG_PUTS(", ");
24349 msg_puts(FUNCARG(fp, j));
24350 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024351 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 {
24353 if (j)
24354 MSG_PUTS(", ");
24355 MSG_PUTS("...");
24356 }
24357 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024358 if (fp->uf_flags & FC_ABORT)
24359 MSG_PUTS(" abort");
24360 if (fp->uf_flags & FC_RANGE)
24361 MSG_PUTS(" range");
24362 if (fp->uf_flags & FC_DICT)
24363 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024364 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024365 if (p_verbose > 0)
24366 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367}
24368
24369/*
24370 * Find a function by name, return pointer to it in ufuncs.
24371 * Return NULL for unknown function.
24372 */
24373 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024374find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024376 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024377
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024378 hi = hash_find(&func_hashtab, name);
24379 if (!HASHITEM_EMPTY(hi))
24380 return HI2UF(hi);
24381 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024382}
24383
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024384#if defined(EXITFREE) || defined(PROTO)
24385 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024386free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024387{
24388 hashitem_T *hi;
24389
24390 /* Need to start all over every time, because func_free() may change the
24391 * hash table. */
24392 while (func_hashtab.ht_used > 0)
24393 for (hi = func_hashtab.ht_array; ; ++hi)
24394 if (!HASHITEM_EMPTY(hi))
24395 {
24396 func_free(HI2UF(hi));
24397 break;
24398 }
24399}
24400#endif
24401
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024402 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024403translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024404{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024405 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024406 return find_internal_func(name) >= 0;
24407 return find_func(name) != NULL;
24408}
24409
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024410/*
24411 * Return TRUE if a function "name" exists.
24412 */
24413 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024414function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024415{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024416 char_u *nm = name;
24417 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024418 int n = FALSE;
24419
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024420 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024421 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024422 nm = skipwhite(nm);
24423
24424 /* Only accept "funcname", "funcname ", "funcname (..." and
24425 * "funcname(...", not "funcname!...". */
24426 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024427 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024428 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024429 return n;
24430}
24431
Bram Moolenaara1544c02013-05-30 12:35:52 +020024432 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024433get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024434{
24435 char_u *nm = name;
24436 char_u *p;
24437
Bram Moolenaar65639032016-03-16 21:40:30 +010024438 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024439
24440 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024441 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024442 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024443
Bram Moolenaara1544c02013-05-30 12:35:52 +020024444 vim_free(p);
24445 return NULL;
24446}
24447
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024448/*
24449 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024450 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24451 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024452 */
24453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024454builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024455{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024456 char_u *p;
24457
24458 if (!ASCII_ISLOWER(name[0]))
24459 return FALSE;
24460 p = vim_strchr(name, AUTOLOAD_CHAR);
24461 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024462}
24463
Bram Moolenaar05159a02005-02-26 23:04:13 +000024464#if defined(FEAT_PROFILE) || defined(PROTO)
24465/*
24466 * Start profiling function "fp".
24467 */
24468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024469func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024470{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024471 int len = fp->uf_lines.ga_len;
24472
24473 if (len == 0)
24474 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024475 fp->uf_tm_count = 0;
24476 profile_zero(&fp->uf_tm_self);
24477 profile_zero(&fp->uf_tm_total);
24478 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024479 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024480 if (fp->uf_tml_total == NULL)
24481 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024482 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024483 if (fp->uf_tml_self == NULL)
24484 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024485 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024486 fp->uf_tml_idx = -1;
24487 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24488 || fp->uf_tml_self == NULL)
24489 return; /* out of memory */
24490
24491 fp->uf_profiling = TRUE;
24492}
24493
24494/*
24495 * Dump the profiling results for all functions in file "fd".
24496 */
24497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024498func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024499{
24500 hashitem_T *hi;
24501 int todo;
24502 ufunc_T *fp;
24503 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024504 ufunc_T **sorttab;
24505 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024506
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024507 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024508 if (todo == 0)
24509 return; /* nothing to dump */
24510
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024511 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024512
Bram Moolenaar05159a02005-02-26 23:04:13 +000024513 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24514 {
24515 if (!HASHITEM_EMPTY(hi))
24516 {
24517 --todo;
24518 fp = HI2UF(hi);
24519 if (fp->uf_profiling)
24520 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024521 if (sorttab != NULL)
24522 sorttab[st_len++] = fp;
24523
Bram Moolenaar05159a02005-02-26 23:04:13 +000024524 if (fp->uf_name[0] == K_SPECIAL)
24525 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24526 else
24527 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24528 if (fp->uf_tm_count == 1)
24529 fprintf(fd, "Called 1 time\n");
24530 else
24531 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24532 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24533 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24534 fprintf(fd, "\n");
24535 fprintf(fd, "count total (s) self (s)\n");
24536
24537 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24538 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024539 if (FUNCLINE(fp, i) == NULL)
24540 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024541 prof_func_line(fd, fp->uf_tml_count[i],
24542 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024543 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24544 }
24545 fprintf(fd, "\n");
24546 }
24547 }
24548 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024549
24550 if (sorttab != NULL && st_len > 0)
24551 {
24552 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24553 prof_total_cmp);
24554 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24555 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24556 prof_self_cmp);
24557 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24558 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024559
24560 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024561}
Bram Moolenaar73830342005-02-28 22:48:19 +000024562
24563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024564prof_sort_list(
24565 FILE *fd,
24566 ufunc_T **sorttab,
24567 int st_len,
24568 char *title,
24569 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024570{
24571 int i;
24572 ufunc_T *fp;
24573
24574 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24575 fprintf(fd, "count total (s) self (s) function\n");
24576 for (i = 0; i < 20 && i < st_len; ++i)
24577 {
24578 fp = sorttab[i];
24579 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24580 prefer_self);
24581 if (fp->uf_name[0] == K_SPECIAL)
24582 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24583 else
24584 fprintf(fd, " %s()\n", fp->uf_name);
24585 }
24586 fprintf(fd, "\n");
24587}
24588
24589/*
24590 * Print the count and times for one function or function line.
24591 */
24592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024593prof_func_line(
24594 FILE *fd,
24595 int count,
24596 proftime_T *total,
24597 proftime_T *self,
24598 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024599{
24600 if (count > 0)
24601 {
24602 fprintf(fd, "%5d ", count);
24603 if (prefer_self && profile_equal(total, self))
24604 fprintf(fd, " ");
24605 else
24606 fprintf(fd, "%s ", profile_msg(total));
24607 if (!prefer_self && profile_equal(total, self))
24608 fprintf(fd, " ");
24609 else
24610 fprintf(fd, "%s ", profile_msg(self));
24611 }
24612 else
24613 fprintf(fd, " ");
24614}
24615
24616/*
24617 * Compare function for total time sorting.
24618 */
24619 static int
24620#ifdef __BORLANDC__
24621_RTLENTRYF
24622#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024623prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024624{
24625 ufunc_T *p1, *p2;
24626
24627 p1 = *(ufunc_T **)s1;
24628 p2 = *(ufunc_T **)s2;
24629 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24630}
24631
24632/*
24633 * Compare function for self time sorting.
24634 */
24635 static int
24636#ifdef __BORLANDC__
24637_RTLENTRYF
24638#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024639prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024640{
24641 ufunc_T *p1, *p2;
24642
24643 p1 = *(ufunc_T **)s1;
24644 p2 = *(ufunc_T **)s2;
24645 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24646}
24647
Bram Moolenaar05159a02005-02-26 23:04:13 +000024648#endif
24649
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024650/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024651 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024652 * Return TRUE if a package was loaded.
24653 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024655script_autoload(
24656 char_u *name,
24657 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024658{
24659 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024660 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024661 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024662 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024663
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024664 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024665 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024666 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024667 return FALSE;
24668
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024669 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024670
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024671 /* Find the name in the list of previously loaded package names. Skip
24672 * "autoload/", it's always the same. */
24673 for (i = 0; i < ga_loaded.ga_len; ++i)
24674 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24675 break;
24676 if (!reload && i < ga_loaded.ga_len)
24677 ret = FALSE; /* was loaded already */
24678 else
24679 {
24680 /* Remember the name if it wasn't loaded already. */
24681 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24682 {
24683 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24684 tofree = NULL;
24685 }
24686
24687 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024688 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024689 ret = TRUE;
24690 }
24691
24692 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024693 return ret;
24694}
24695
24696/*
24697 * Return the autoload script name for a function or variable name.
24698 * Returns NULL when out of memory.
24699 */
24700 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024701autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024702{
24703 char_u *p;
24704 char_u *scriptname;
24705
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024706 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024707 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24708 if (scriptname == NULL)
24709 return FALSE;
24710 STRCPY(scriptname, "autoload/");
24711 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024712 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024713 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024714 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024715 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024716 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024717}
24718
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24720
24721/*
24722 * Function given to ExpandGeneric() to obtain the list of user defined
24723 * function names.
24724 */
24725 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024726get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024728 static long_u done;
24729 static hashitem_T *hi;
24730 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024731
24732 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024733 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024734 done = 0;
24735 hi = func_hashtab.ht_array;
24736 }
24737 if (done < func_hashtab.ht_used)
24738 {
24739 if (done++ > 0)
24740 ++hi;
24741 while (HASHITEM_EMPTY(hi))
24742 ++hi;
24743 fp = HI2UF(hi);
24744
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024745 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024746 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024747
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024748 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24749 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024750
24751 cat_func_name(IObuff, fp);
24752 if (xp->xp_context != EXPAND_USER_FUNC)
24753 {
24754 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024755 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024756 STRCAT(IObuff, ")");
24757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758 return IObuff;
24759 }
24760 return NULL;
24761}
24762
24763#endif /* FEAT_CMDL_COMPL */
24764
24765/*
24766 * Copy the function name of "fp" to buffer "buf".
24767 * "buf" must be able to hold the function name plus three bytes.
24768 * Takes care of script-local function names.
24769 */
24770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024771cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024772{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024773 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024774 {
24775 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024776 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 }
24778 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024779 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024780}
24781
24782/*
24783 * ":delfunction {name}"
24784 */
24785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024786ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024788 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 char_u *p;
24790 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024791 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792
24793 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024794 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024795 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024796 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024797 {
24798 if (fudi.fd_dict != NULL && !eap->skip)
24799 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802 if (!ends_excmd(*skipwhite(p)))
24803 {
24804 vim_free(name);
24805 EMSG(_(e_trailing));
24806 return;
24807 }
24808 eap->nextcmd = check_nextcmd(p);
24809 if (eap->nextcmd != NULL)
24810 *p = NUL;
24811
24812 if (!eap->skip)
24813 fp = find_func(name);
24814 vim_free(name);
24815
24816 if (!eap->skip)
24817 {
24818 if (fp == NULL)
24819 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024820 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024821 return;
24822 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024823 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824 {
24825 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24826 return;
24827 }
24828
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024829 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024831 /* Delete the dict item that refers to the function, it will
24832 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024833 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024835 else
24836 func_free(fp);
24837 }
24838}
24839
24840/*
24841 * Free a function and remove it from the list of functions.
24842 */
24843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024844func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024845{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024846 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024847
24848 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024849 ga_clear_strings(&(fp->uf_args));
24850 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024851#ifdef FEAT_PROFILE
24852 vim_free(fp->uf_tml_count);
24853 vim_free(fp->uf_tml_total);
24854 vim_free(fp->uf_tml_self);
24855#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024856
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024857 /* remove the function from the function hashtable */
24858 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24859 if (HASHITEM_EMPTY(hi))
24860 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024861 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024862 hash_remove(&func_hashtab, hi);
24863
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024864 vim_free(fp);
24865}
24866
24867/*
24868 * Unreference a Function: decrement the reference count and free it when it
24869 * becomes zero. Only for numbered functions.
24870 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024871 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024872func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024873{
24874 ufunc_T *fp;
24875
24876 if (name != NULL && isdigit(*name))
24877 {
24878 fp = find_func(name);
24879 if (fp == NULL)
24880 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024881 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024882 {
24883 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024884 * when "uf_calls" becomes zero. */
24885 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024886 func_free(fp);
24887 }
24888 }
24889}
24890
24891/*
24892 * Count a reference to a Function.
24893 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024895func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024896{
24897 ufunc_T *fp;
24898
24899 if (name != NULL && isdigit(*name))
24900 {
24901 fp = find_func(name);
24902 if (fp == NULL)
24903 EMSG2(_(e_intern2), "func_ref()");
24904 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024905 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024906 }
24907}
24908
24909/*
24910 * Call a user function.
24911 */
24912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024913call_user_func(
24914 ufunc_T *fp, /* pointer to function */
24915 int argcount, /* nr of args */
24916 typval_T *argvars, /* arguments */
24917 typval_T *rettv, /* return value */
24918 linenr_T firstline, /* first line of range */
24919 linenr_T lastline, /* last line of range */
24920 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921{
Bram Moolenaar33570922005-01-25 22:26:29 +000024922 char_u *save_sourcing_name;
24923 linenr_T save_sourcing_lnum;
24924 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024925 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024926 int save_did_emsg;
24927 static int depth = 0;
24928 dictitem_T *v;
24929 int fixvar_idx = 0; /* index in fixvar[] */
24930 int i;
24931 int ai;
24932 char_u numbuf[NUMBUFLEN];
24933 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024934 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024935#ifdef FEAT_PROFILE
24936 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024937 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939
24940 /* If depth of calling is getting too high, don't execute the function */
24941 if (depth >= p_mfd)
24942 {
24943 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024944 rettv->v_type = VAR_NUMBER;
24945 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024946 return;
24947 }
24948 ++depth;
24949
24950 line_breakcheck(); /* check for CTRL-C hit */
24951
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024952 fc = (funccall_T *)alloc(sizeof(funccall_T));
24953 fc->caller = current_funccal;
24954 current_funccal = fc;
24955 fc->func = fp;
24956 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024957 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024958 fc->linenr = 0;
24959 fc->returned = FALSE;
24960 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024961 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024962 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24963 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024964
Bram Moolenaar33570922005-01-25 22:26:29 +000024965 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024966 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024967 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24968 * each argument variable and saves a lot of time.
24969 */
24970 /*
24971 * Init l: variables.
24972 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024973 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024974 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024975 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024976 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24977 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024978 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024979 name = v->di_key;
24980 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024981 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024982 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024983 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024984 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024985 v->di_tv.vval.v_dict = selfdict;
24986 ++selfdict->dv_refcount;
24987 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024988
Bram Moolenaar33570922005-01-25 22:26:29 +000024989 /*
24990 * Init a: variables.
24991 * Set a:0 to "argcount".
24992 * Set a:000 to a list with room for the "..." arguments.
24993 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024994 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024995 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024996 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024997 /* Use "name" to avoid a warning from some compiler that checks the
24998 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024999 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025000 name = v->di_key;
25001 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025002 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025003 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025004 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025005 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025006 v->di_tv.vval.v_list = &fc->l_varlist;
25007 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25008 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25009 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025010
25011 /*
25012 * Set a:firstline to "firstline" and a:lastline to "lastline".
25013 * Set a:name to named arguments.
25014 * Set a:N to the "..." arguments.
25015 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025016 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025017 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025018 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025019 (varnumber_T)lastline);
25020 for (i = 0; i < argcount; ++i)
25021 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025022 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025023 if (ai < 0)
25024 /* named argument a:name */
25025 name = FUNCARG(fp, i);
25026 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025027 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025028 /* "..." argument a:1, a:2, etc. */
25029 sprintf((char *)numbuf, "%d", ai + 1);
25030 name = numbuf;
25031 }
25032 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25033 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025034 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025035 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25036 }
25037 else
25038 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025039 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25040 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025041 if (v == NULL)
25042 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025043 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025044 }
25045 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025046 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025047
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025048 /* Note: the values are copied directly to avoid alloc/free.
25049 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025050 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025051 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025052
25053 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25054 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025055 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25056 fc->l_listitems[ai].li_tv = argvars[i];
25057 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025058 }
25059 }
25060
Bram Moolenaar071d4272004-06-13 20:20:40 +000025061 /* Don't redraw while executing the function. */
25062 ++RedrawingDisabled;
25063 save_sourcing_name = sourcing_name;
25064 save_sourcing_lnum = sourcing_lnum;
25065 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025066 /* need space for function name + ("function " + 3) or "[number]" */
25067 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25068 + STRLEN(fp->uf_name) + 20;
25069 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070 if (sourcing_name != NULL)
25071 {
25072 if (save_sourcing_name != NULL
25073 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025074 sprintf((char *)sourcing_name, "%s[%d]..",
25075 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 else
25077 STRCPY(sourcing_name, "function ");
25078 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25079
25080 if (p_verbose >= 12)
25081 {
25082 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025083 verbose_enter_scroll();
25084
Bram Moolenaar555b2802005-05-19 21:08:39 +000025085 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025086 if (p_verbose >= 14)
25087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025088 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025089 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025090 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025091 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025092
25093 msg_puts((char_u *)"(");
25094 for (i = 0; i < argcount; ++i)
25095 {
25096 if (i > 0)
25097 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025098 if (argvars[i].v_type == VAR_NUMBER)
25099 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100 else
25101 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025102 /* Do not want errors such as E724 here. */
25103 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025104 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025105 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025106 if (s != NULL)
25107 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025108 if (vim_strsize(s) > MSG_BUF_CLEN)
25109 {
25110 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25111 s = buf;
25112 }
25113 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025114 vim_free(tofree);
25115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116 }
25117 }
25118 msg_puts((char_u *)")");
25119 }
25120 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025121
25122 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025123 --no_wait_return;
25124 }
25125 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025126#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025127 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025128 {
25129 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25130 func_do_profile(fp);
25131 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025132 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025133 {
25134 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025135 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025136 profile_zero(&fp->uf_tm_children);
25137 }
25138 script_prof_save(&wait_start);
25139 }
25140#endif
25141
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025143 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144 save_did_emsg = did_emsg;
25145 did_emsg = FALSE;
25146
25147 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025148 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025149 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25150
25151 --RedrawingDisabled;
25152
25153 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025154 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025156 clear_tv(rettv);
25157 rettv->v_type = VAR_NUMBER;
25158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159 }
25160
Bram Moolenaar05159a02005-02-26 23:04:13 +000025161#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025162 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025163 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025164 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025165 profile_end(&call_start);
25166 profile_sub_wait(&wait_start, &call_start);
25167 profile_add(&fp->uf_tm_total, &call_start);
25168 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025169 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025170 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025171 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25172 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025173 }
25174 }
25175#endif
25176
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177 /* when being verbose, mention the return value */
25178 if (p_verbose >= 12)
25179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025181 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025184 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025185 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025186 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025187 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025188 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025189 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025190 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025191 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025192 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025193 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025194
Bram Moolenaar555b2802005-05-19 21:08:39 +000025195 /* The value may be very long. Skip the middle part, so that we
25196 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025197 * truncate it at the end. Don't want errors such as E724 here. */
25198 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025199 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025200 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025201 if (s != NULL)
25202 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025203 if (vim_strsize(s) > MSG_BUF_CLEN)
25204 {
25205 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25206 s = buf;
25207 }
25208 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025209 vim_free(tofree);
25210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211 }
25212 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025213
25214 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 --no_wait_return;
25216 }
25217
25218 vim_free(sourcing_name);
25219 sourcing_name = save_sourcing_name;
25220 sourcing_lnum = save_sourcing_lnum;
25221 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025222#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025223 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025224 script_prof_restore(&wait_start);
25225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025226
25227 if (p_verbose >= 12 && sourcing_name != NULL)
25228 {
25229 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025230 verbose_enter_scroll();
25231
Bram Moolenaar555b2802005-05-19 21:08:39 +000025232 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025234
25235 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 --no_wait_return;
25237 }
25238
25239 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025240 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025241 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025242
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025243 /* If the a:000 list and the l: and a: dicts are not referenced we can
25244 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025245 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25246 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25247 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25248 {
25249 free_funccal(fc, FALSE);
25250 }
25251 else
25252 {
25253 hashitem_T *hi;
25254 listitem_T *li;
25255 int todo;
25256
25257 /* "fc" is still in use. This can happen when returning "a:000" or
25258 * assigning "l:" to a global variable.
25259 * Link "fc" in the list for garbage collection later. */
25260 fc->caller = previous_funccal;
25261 previous_funccal = fc;
25262
25263 /* Make a copy of the a: variables, since we didn't do that above. */
25264 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25265 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25266 {
25267 if (!HASHITEM_EMPTY(hi))
25268 {
25269 --todo;
25270 v = HI2DI(hi);
25271 copy_tv(&v->di_tv, &v->di_tv);
25272 }
25273 }
25274
25275 /* Make a copy of the a:000 items, since we didn't do that above. */
25276 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25277 copy_tv(&li->li_tv, &li->li_tv);
25278 }
25279}
25280
25281/*
25282 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025283 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025284 */
25285 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025286can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025287{
25288 return (fc->l_varlist.lv_copyID != copyID
25289 && fc->l_vars.dv_copyID != copyID
25290 && fc->l_avars.dv_copyID != copyID);
25291}
25292
25293/*
25294 * Free "fc" and what it contains.
25295 */
25296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025297free_funccal(
25298 funccall_T *fc,
25299 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025300{
25301 listitem_T *li;
25302
25303 /* The a: variables typevals may not have been allocated, only free the
25304 * allocated variables. */
25305 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25306
25307 /* free all l: variables */
25308 vars_clear(&fc->l_vars.dv_hashtab);
25309
25310 /* Free the a:000 variables if they were allocated. */
25311 if (free_val)
25312 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25313 clear_tv(&li->li_tv);
25314
25315 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316}
25317
25318/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025319 * Add a number variable "name" to dict "dp" with value "nr".
25320 */
25321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025322add_nr_var(
25323 dict_T *dp,
25324 dictitem_T *v,
25325 char *name,
25326 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025327{
25328 STRCPY(v->di_key, name);
25329 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25330 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25331 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025332 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025333 v->di_tv.vval.v_number = nr;
25334}
25335
25336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337 * ":return [expr]"
25338 */
25339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025340ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025341{
25342 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025343 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344 int returning = FALSE;
25345
25346 if (current_funccal == NULL)
25347 {
25348 EMSG(_("E133: :return not inside a function"));
25349 return;
25350 }
25351
25352 if (eap->skip)
25353 ++emsg_skip;
25354
25355 eap->nextcmd = NULL;
25356 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025357 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025358 {
25359 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025360 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025361 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025362 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 }
25364 /* It's safer to return also on error. */
25365 else if (!eap->skip)
25366 {
25367 /*
25368 * Return unless the expression evaluation has been cancelled due to an
25369 * aborting error, an interrupt, or an exception.
25370 */
25371 if (!aborting())
25372 returning = do_return(eap, FALSE, TRUE, NULL);
25373 }
25374
25375 /* When skipping or the return gets pending, advance to the next command
25376 * in this line (!returning). Otherwise, ignore the rest of the line.
25377 * Following lines will be ignored by get_func_line(). */
25378 if (returning)
25379 eap->nextcmd = NULL;
25380 else if (eap->nextcmd == NULL) /* no argument */
25381 eap->nextcmd = check_nextcmd(arg);
25382
25383 if (eap->skip)
25384 --emsg_skip;
25385}
25386
25387/*
25388 * Return from a function. Possibly makes the return pending. Also called
25389 * for a pending return at the ":endtry" or after returning from an extra
25390 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025391 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025392 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025393 * FALSE when the return gets pending.
25394 */
25395 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025396do_return(
25397 exarg_T *eap,
25398 int reanimate,
25399 int is_cmd,
25400 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401{
25402 int idx;
25403 struct condstack *cstack = eap->cstack;
25404
25405 if (reanimate)
25406 /* Undo the return. */
25407 current_funccal->returned = FALSE;
25408
25409 /*
25410 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25411 * not in its finally clause (which then is to be executed next) is found.
25412 * In this case, make the ":return" pending for execution at the ":endtry".
25413 * Otherwise, return normally.
25414 */
25415 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25416 if (idx >= 0)
25417 {
25418 cstack->cs_pending[idx] = CSTP_RETURN;
25419
25420 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025421 /* A pending return again gets pending. "rettv" points to an
25422 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025424 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425 else
25426 {
25427 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025428 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025430 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025432 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433 {
25434 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025435 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025436 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437 else
25438 EMSG(_(e_outofmem));
25439 }
25440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025441 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025442
25443 if (reanimate)
25444 {
25445 /* The pending return value could be overwritten by a ":return"
25446 * without argument in a finally clause; reset the default
25447 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025448 current_funccal->rettv->v_type = VAR_NUMBER;
25449 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450 }
25451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025452 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 }
25454 else
25455 {
25456 current_funccal->returned = TRUE;
25457
25458 /* If the return is carried out now, store the return value. For
25459 * a return immediately after reanimation, the value is already
25460 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025461 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025463 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025464 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025466 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025467 }
25468 }
25469
25470 return idx < 0;
25471}
25472
25473/*
25474 * Free the variable with a pending return value.
25475 */
25476 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025477discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478{
Bram Moolenaar33570922005-01-25 22:26:29 +000025479 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480}
25481
25482/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025483 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484 * is an allocated string. Used by report_pending() for verbose messages.
25485 */
25486 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025487get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025489 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025490 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025491 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025493 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025494 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025495 if (s == NULL)
25496 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025497
25498 STRCPY(IObuff, ":return ");
25499 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25500 if (STRLEN(s) + 8 >= IOSIZE)
25501 STRCPY(IObuff + IOSIZE - 4, "...");
25502 vim_free(tofree);
25503 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504}
25505
25506/*
25507 * Get next function line.
25508 * Called by do_cmdline() to get the next line.
25509 * Returns allocated string, or NULL for end of function.
25510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025512get_func_line(
25513 int c UNUSED,
25514 void *cookie,
25515 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516{
Bram Moolenaar33570922005-01-25 22:26:29 +000025517 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025518 ufunc_T *fp = fcp->func;
25519 char_u *retval;
25520 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521
25522 /* If breakpoints have been added/deleted need to check for it. */
25523 if (fcp->dbg_tick != debug_tick)
25524 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025525 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025526 sourcing_lnum);
25527 fcp->dbg_tick = debug_tick;
25528 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025529#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025530 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025531 func_line_end(cookie);
25532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533
Bram Moolenaar05159a02005-02-26 23:04:13 +000025534 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025535 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25536 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025537 retval = NULL;
25538 else
25539 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025540 /* Skip NULL lines (continuation lines). */
25541 while (fcp->linenr < gap->ga_len
25542 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25543 ++fcp->linenr;
25544 if (fcp->linenr >= gap->ga_len)
25545 retval = NULL;
25546 else
25547 {
25548 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25549 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025550#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025551 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025552 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025553#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555 }
25556
25557 /* Did we encounter a breakpoint? */
25558 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25559 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025560 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025562 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 sourcing_lnum);
25564 fcp->dbg_tick = debug_tick;
25565 }
25566
25567 return retval;
25568}
25569
Bram Moolenaar05159a02005-02-26 23:04:13 +000025570#if defined(FEAT_PROFILE) || defined(PROTO)
25571/*
25572 * Called when starting to read a function line.
25573 * "sourcing_lnum" must be correct!
25574 * When skipping lines it may not actually be executed, but we won't find out
25575 * until later and we need to store the time now.
25576 */
25577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025578func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025579{
25580 funccall_T *fcp = (funccall_T *)cookie;
25581 ufunc_T *fp = fcp->func;
25582
25583 if (fp->uf_profiling && sourcing_lnum >= 1
25584 && sourcing_lnum <= fp->uf_lines.ga_len)
25585 {
25586 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025587 /* Skip continuation lines. */
25588 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25589 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025590 fp->uf_tml_execed = FALSE;
25591 profile_start(&fp->uf_tml_start);
25592 profile_zero(&fp->uf_tml_children);
25593 profile_get_wait(&fp->uf_tml_wait);
25594 }
25595}
25596
25597/*
25598 * Called when actually executing a function line.
25599 */
25600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025601func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025602{
25603 funccall_T *fcp = (funccall_T *)cookie;
25604 ufunc_T *fp = fcp->func;
25605
25606 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25607 fp->uf_tml_execed = TRUE;
25608}
25609
25610/*
25611 * Called when done with a function line.
25612 */
25613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025614func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025615{
25616 funccall_T *fcp = (funccall_T *)cookie;
25617 ufunc_T *fp = fcp->func;
25618
25619 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25620 {
25621 if (fp->uf_tml_execed)
25622 {
25623 ++fp->uf_tml_count[fp->uf_tml_idx];
25624 profile_end(&fp->uf_tml_start);
25625 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025626 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025627 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25628 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025629 }
25630 fp->uf_tml_idx = -1;
25631 }
25632}
25633#endif
25634
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635/*
25636 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025637 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025638 */
25639 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025640func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025641{
Bram Moolenaar33570922005-01-25 22:26:29 +000025642 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025643
25644 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25645 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025646 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025647 || fcp->returned);
25648}
25649
25650/*
25651 * return TRUE if cookie indicates a function which "abort"s on errors.
25652 */
25653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025654func_has_abort(
25655 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025656{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025657 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658}
25659
25660#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25661typedef enum
25662{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025663 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25664 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25665 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666} var_flavour_T;
25667
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025668static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669
25670 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025671var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025672{
25673 char_u *p = varname;
25674
25675 if (ASCII_ISUPPER(*p))
25676 {
25677 while (*(++p))
25678 if (ASCII_ISLOWER(*p))
25679 return VAR_FLAVOUR_SESSION;
25680 return VAR_FLAVOUR_VIMINFO;
25681 }
25682 else
25683 return VAR_FLAVOUR_DEFAULT;
25684}
25685#endif
25686
25687#if defined(FEAT_VIMINFO) || defined(PROTO)
25688/*
25689 * Restore global vars that start with a capital from the viminfo file
25690 */
25691 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025692read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025693{
25694 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025695 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025696 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025697 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025698
25699 if (!writing && (find_viminfo_parameter('!') != NULL))
25700 {
25701 tab = vim_strchr(virp->vir_line + 1, '\t');
25702 if (tab != NULL)
25703 {
25704 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025705 switch (*tab)
25706 {
25707 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025708#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025709 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025710#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025711 case 'D': type = VAR_DICT; break;
25712 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025713 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025715
25716 tab = vim_strchr(tab, '\t');
25717 if (tab != NULL)
25718 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025719 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025720 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025721 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025722 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025723#ifdef FEAT_FLOAT
25724 else if (type == VAR_FLOAT)
25725 (void)string2float(tab + 1, &tv.vval.v_float);
25726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025727 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025728 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025729 if (type == VAR_DICT || type == VAR_LIST)
25730 {
25731 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25732
25733 if (etv == NULL)
25734 /* Failed to parse back the dict or list, use it as a
25735 * string. */
25736 tv.v_type = VAR_STRING;
25737 else
25738 {
25739 vim_free(tv.vval.v_string);
25740 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025741 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025742 }
25743 }
25744
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025745 /* when in a function use global variables */
25746 save_funccal = current_funccal;
25747 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025748 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025749 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025750
25751 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025752 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025753 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25754 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755 }
25756 }
25757 }
25758
25759 return viminfo_readline(virp);
25760}
25761
25762/*
25763 * Write global vars that start with a capital to the viminfo file
25764 */
25765 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025766write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025767{
Bram Moolenaar33570922005-01-25 22:26:29 +000025768 hashitem_T *hi;
25769 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025770 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025771 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025772 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025773 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025774 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025775
25776 if (find_viminfo_parameter('!') == NULL)
25777 return;
25778
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025779 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025780
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025781 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025782 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025784 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025785 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025786 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025787 this_var = HI2DI(hi);
25788 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025789 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025790 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025791 {
25792 case VAR_STRING: s = "STR"; break;
25793 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025794 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025795 case VAR_DICT: s = "DIC"; break;
25796 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025797 case VAR_SPECIAL: s = "XPL"; break;
25798
25799 case VAR_UNKNOWN:
25800 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025801 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025802 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025803 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025804 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025805 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025806 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025807 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025808 if (p != NULL)
25809 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025810 vim_free(tofree);
25811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025812 }
25813 }
25814}
25815#endif
25816
25817#if defined(FEAT_SESSION) || defined(PROTO)
25818 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025819store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820{
Bram Moolenaar33570922005-01-25 22:26:29 +000025821 hashitem_T *hi;
25822 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025823 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025824 char_u *p, *t;
25825
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025826 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025827 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025828 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025829 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025831 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025832 this_var = HI2DI(hi);
25833 if ((this_var->di_tv.v_type == VAR_NUMBER
25834 || this_var->di_tv.v_type == VAR_STRING)
25835 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025836 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025837 /* Escape special characters with a backslash. Turn a LF and
25838 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025839 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025840 (char_u *)"\\\"\n\r");
25841 if (p == NULL) /* out of memory */
25842 break;
25843 for (t = p; *t != NUL; ++t)
25844 if (*t == '\n')
25845 *t = 'n';
25846 else if (*t == '\r')
25847 *t = 'r';
25848 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025849 this_var->di_key,
25850 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25851 : ' ',
25852 p,
25853 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25854 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025855 || put_eol(fd) == FAIL)
25856 {
25857 vim_free(p);
25858 return FAIL;
25859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860 vim_free(p);
25861 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025862#ifdef FEAT_FLOAT
25863 else if (this_var->di_tv.v_type == VAR_FLOAT
25864 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25865 {
25866 float_T f = this_var->di_tv.vval.v_float;
25867 int sign = ' ';
25868
25869 if (f < 0)
25870 {
25871 f = -f;
25872 sign = '-';
25873 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025874 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025875 this_var->di_key, sign, f) < 0)
25876 || put_eol(fd) == FAIL)
25877 return FAIL;
25878 }
25879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025880 }
25881 }
25882 return OK;
25883}
25884#endif
25885
Bram Moolenaar661b1822005-07-28 22:36:45 +000025886/*
25887 * Display script name where an item was last set.
25888 * Should only be invoked when 'verbose' is non-zero.
25889 */
25890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025891last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025892{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025893 char_u *p;
25894
Bram Moolenaar661b1822005-07-28 22:36:45 +000025895 if (scriptID != 0)
25896 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025897 p = home_replace_save(NULL, get_scriptname(scriptID));
25898 if (p != NULL)
25899 {
25900 verbose_enter();
25901 MSG_PUTS(_("\n\tLast set from "));
25902 MSG_PUTS(p);
25903 vim_free(p);
25904 verbose_leave();
25905 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025906 }
25907}
25908
Bram Moolenaard812df62008-11-09 12:46:09 +000025909/*
25910 * List v:oldfiles in a nice way.
25911 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025913ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025914{
25915 list_T *l = vimvars[VV_OLDFILES].vv_list;
25916 listitem_T *li;
25917 int nr = 0;
25918
25919 if (l == NULL)
25920 msg((char_u *)_("No old files"));
25921 else
25922 {
25923 msg_start();
25924 msg_scroll = TRUE;
25925 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25926 {
25927 msg_outnum((long)++nr);
25928 MSG_PUTS(": ");
25929 msg_outtrans(get_tv_string(&li->li_tv));
25930 msg_putchar('\n');
25931 out_flush(); /* output one line at a time */
25932 ui_breakcheck();
25933 }
25934 /* Assume "got_int" was set to truncate the listing. */
25935 got_int = FALSE;
25936
25937#ifdef FEAT_BROWSE_CMD
25938 if (cmdmod.browse)
25939 {
25940 quit_more = FALSE;
25941 nr = prompt_for_number(FALSE);
25942 msg_starthere();
25943 if (nr > 0)
25944 {
25945 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25946 (long)nr);
25947
25948 if (p != NULL)
25949 {
25950 p = expand_env_save(p);
25951 eap->arg = p;
25952 eap->cmdidx = CMD_edit;
25953 cmdmod.browse = FALSE;
25954 do_exedit(eap, NULL);
25955 vim_free(p);
25956 }
25957 }
25958 }
25959#endif
25960 }
25961}
25962
Bram Moolenaar53744302015-07-17 17:38:22 +020025963/* reset v:option_new, v:option_old and v:option_type */
25964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025965reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025966{
25967 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25968 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25969 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25970}
25971
25972
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973#endif /* FEAT_EVAL */
25974
Bram Moolenaar071d4272004-06-13 20:20:40 +000025975
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025976#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025977
25978#ifdef WIN3264
25979/*
25980 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25981 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025982static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25983static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25984static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025985
25986/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025987 * Get the short path (8.3) for the filename in "fnamep".
25988 * Only works for a valid file name.
25989 * When the path gets longer "fnamep" is changed and the allocated buffer
25990 * is put in "bufp".
25991 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25992 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993 */
25994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025995get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025997 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998 char_u *newbuf;
25999
26000 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026001 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026002 if (l > len - 1)
26003 {
26004 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026005 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006 newbuf = vim_strnsave(*fnamep, l);
26007 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026008 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009
26010 vim_free(*bufp);
26011 *fnamep = *bufp = newbuf;
26012
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026013 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026014 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015 }
26016
26017 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026018 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019}
26020
26021/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026022 * Get the short path (8.3) for the filename in "fname". The converted
26023 * path is returned in "bufp".
26024 *
26025 * Some of the directories specified in "fname" may not exist. This function
26026 * will shorten the existing directories at the beginning of the path and then
26027 * append the remaining non-existing path.
26028 *
26029 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026030 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026031 * bufp - Pointer to an allocated buffer for the filename.
26032 * fnamelen - Length of the filename pointed to by fname
26033 *
26034 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035 */
26036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026037shortpath_for_invalid_fname(
26038 char_u **fname,
26039 char_u **bufp,
26040 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026041{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026042 char_u *short_fname, *save_fname, *pbuf_unused;
26043 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026044 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026045 int old_len, len;
26046 int new_len, sfx_len;
26047 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048
26049 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026050 old_len = *fnamelen;
26051 save_fname = vim_strnsave(*fname, old_len);
26052 pbuf_unused = NULL;
26053 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026054
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026055 endp = save_fname + old_len - 1; /* Find the end of the copy */
26056 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026057
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026058 /*
26059 * Try shortening the supplied path till it succeeds by removing one
26060 * directory at a time from the tail of the path.
26061 */
26062 len = 0;
26063 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026064 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026065 /* go back one path-separator */
26066 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26067 --endp;
26068 if (endp <= save_fname)
26069 break; /* processed the complete path */
26070
26071 /*
26072 * Replace the path separator with a NUL and try to shorten the
26073 * resulting path.
26074 */
26075 ch = *endp;
26076 *endp = 0;
26077 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026078 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026079 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26080 {
26081 retval = FAIL;
26082 goto theend;
26083 }
26084 *endp = ch; /* preserve the string */
26085
26086 if (len > 0)
26087 break; /* successfully shortened the path */
26088
26089 /* failed to shorten the path. Skip the path separator */
26090 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091 }
26092
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026093 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026095 /*
26096 * Succeeded in shortening the path. Now concatenate the shortened
26097 * path with the remaining path at the tail.
26098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026099
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026100 /* Compute the length of the new path. */
26101 sfx_len = (int)(save_endp - endp) + 1;
26102 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026104 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026105 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026106 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026107 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026108 /* There is not enough space in the currently allocated string,
26109 * copy it to a buffer big enough. */
26110 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026111 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026112 {
26113 retval = FAIL;
26114 goto theend;
26115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026116 }
26117 else
26118 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026119 /* Transfer short_fname to the main buffer (it's big enough),
26120 * unless get_short_pathname() did its work in-place. */
26121 *fname = *bufp = save_fname;
26122 if (short_fname != save_fname)
26123 vim_strncpy(save_fname, short_fname, len);
26124 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026125 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026126
26127 /* concat the not-shortened part of the path */
26128 vim_strncpy(*fname + len, endp, sfx_len);
26129 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026130 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026131
26132theend:
26133 vim_free(pbuf_unused);
26134 vim_free(save_fname);
26135
26136 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137}
26138
26139/*
26140 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026141 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026142 */
26143 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026144shortpath_for_partial(
26145 char_u **fnamep,
26146 char_u **bufp,
26147 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026148{
26149 int sepcount, len, tflen;
26150 char_u *p;
26151 char_u *pbuf, *tfname;
26152 int hasTilde;
26153
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026154 /* Count up the path separators from the RHS.. so we know which part
26155 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026156 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026157 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026158 if (vim_ispathsep(*p))
26159 ++sepcount;
26160
26161 /* Need full path first (use expand_env() to remove a "~/") */
26162 hasTilde = (**fnamep == '~');
26163 if (hasTilde)
26164 pbuf = tfname = expand_env_save(*fnamep);
26165 else
26166 pbuf = tfname = FullName_save(*fnamep, FALSE);
26167
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026168 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026170 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026172
26173 if (len == 0)
26174 {
26175 /* Don't have a valid filename, so shorten the rest of the
26176 * path if we can. This CAN give us invalid 8.3 filenames, but
26177 * there's not a lot of point in guessing what it might be.
26178 */
26179 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026180 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26181 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182 }
26183
26184 /* Count the paths backward to find the beginning of the desired string. */
26185 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026186 {
26187#ifdef FEAT_MBYTE
26188 if (has_mbyte)
26189 p -= mb_head_off(tfname, p);
26190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026191 if (vim_ispathsep(*p))
26192 {
26193 if (sepcount == 0 || (hasTilde && sepcount == 1))
26194 break;
26195 else
26196 sepcount --;
26197 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026199 if (hasTilde)
26200 {
26201 --p;
26202 if (p >= tfname)
26203 *p = '~';
26204 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026205 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206 }
26207 else
26208 ++p;
26209
26210 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26211 vim_free(*bufp);
26212 *fnamelen = (int)STRLEN(p);
26213 *bufp = pbuf;
26214 *fnamep = p;
26215
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026216 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026217}
26218#endif /* WIN3264 */
26219
26220/*
26221 * Adjust a filename, according to a string of modifiers.
26222 * *fnamep must be NUL terminated when called. When returning, the length is
26223 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026224 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225 * When there is an error, *fnamep is set to NULL.
26226 */
26227 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026228modify_fname(
26229 char_u *src, /* string with modifiers */
26230 int *usedlen, /* characters after src that are used */
26231 char_u **fnamep, /* file name so far */
26232 char_u **bufp, /* buffer for allocated file name or NULL */
26233 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026234{
26235 int valid = 0;
26236 char_u *tail;
26237 char_u *s, *p, *pbuf;
26238 char_u dirname[MAXPATHL];
26239 int c;
26240 int has_fullname = 0;
26241#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026242 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026243 int has_shortname = 0;
26244#endif
26245
26246repeat:
26247 /* ":p" - full path/file_name */
26248 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26249 {
26250 has_fullname = 1;
26251
26252 valid |= VALID_PATH;
26253 *usedlen += 2;
26254
26255 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26256 if ((*fnamep)[0] == '~'
26257#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26258 && ((*fnamep)[1] == '/'
26259# ifdef BACKSLASH_IN_FILENAME
26260 || (*fnamep)[1] == '\\'
26261# endif
26262 || (*fnamep)[1] == NUL)
26263
26264#endif
26265 )
26266 {
26267 *fnamep = expand_env_save(*fnamep);
26268 vim_free(*bufp); /* free any allocated file name */
26269 *bufp = *fnamep;
26270 if (*fnamep == NULL)
26271 return -1;
26272 }
26273
26274 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026275 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026276 {
26277 if (vim_ispathsep(*p)
26278 && p[1] == '.'
26279 && (p[2] == NUL
26280 || vim_ispathsep(p[2])
26281 || (p[2] == '.'
26282 && (p[3] == NUL || vim_ispathsep(p[3])))))
26283 break;
26284 }
26285
26286 /* FullName_save() is slow, don't use it when not needed. */
26287 if (*p != NUL || !vim_isAbsName(*fnamep))
26288 {
26289 *fnamep = FullName_save(*fnamep, *p != NUL);
26290 vim_free(*bufp); /* free any allocated file name */
26291 *bufp = *fnamep;
26292 if (*fnamep == NULL)
26293 return -1;
26294 }
26295
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026296#ifdef WIN3264
26297# if _WIN32_WINNT >= 0x0500
26298 if (vim_strchr(*fnamep, '~') != NULL)
26299 {
26300 /* Expand 8.3 filename to full path. Needed to make sure the same
26301 * file does not have two different names.
26302 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26303 p = alloc(_MAX_PATH + 1);
26304 if (p != NULL)
26305 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026306 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026307 {
26308 vim_free(*bufp);
26309 *bufp = *fnamep = p;
26310 }
26311 else
26312 vim_free(p);
26313 }
26314 }
26315# endif
26316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 /* Append a path separator to a directory. */
26318 if (mch_isdir(*fnamep))
26319 {
26320 /* Make room for one or two extra characters. */
26321 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26322 vim_free(*bufp); /* free any allocated file name */
26323 *bufp = *fnamep;
26324 if (*fnamep == NULL)
26325 return -1;
26326 add_pathsep(*fnamep);
26327 }
26328 }
26329
26330 /* ":." - path relative to the current directory */
26331 /* ":~" - path relative to the home directory */
26332 /* ":8" - shortname path - postponed till after */
26333 while (src[*usedlen] == ':'
26334 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26335 {
26336 *usedlen += 2;
26337 if (c == '8')
26338 {
26339#ifdef WIN3264
26340 has_shortname = 1; /* Postpone this. */
26341#endif
26342 continue;
26343 }
26344 pbuf = NULL;
26345 /* Need full path first (use expand_env() to remove a "~/") */
26346 if (!has_fullname)
26347 {
26348 if (c == '.' && **fnamep == '~')
26349 p = pbuf = expand_env_save(*fnamep);
26350 else
26351 p = pbuf = FullName_save(*fnamep, FALSE);
26352 }
26353 else
26354 p = *fnamep;
26355
26356 has_fullname = 0;
26357
26358 if (p != NULL)
26359 {
26360 if (c == '.')
26361 {
26362 mch_dirname(dirname, MAXPATHL);
26363 s = shorten_fname(p, dirname);
26364 if (s != NULL)
26365 {
26366 *fnamep = s;
26367 if (pbuf != NULL)
26368 {
26369 vim_free(*bufp); /* free any allocated file name */
26370 *bufp = pbuf;
26371 pbuf = NULL;
26372 }
26373 }
26374 }
26375 else
26376 {
26377 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26378 /* Only replace it when it starts with '~' */
26379 if (*dirname == '~')
26380 {
26381 s = vim_strsave(dirname);
26382 if (s != NULL)
26383 {
26384 *fnamep = s;
26385 vim_free(*bufp);
26386 *bufp = s;
26387 }
26388 }
26389 }
26390 vim_free(pbuf);
26391 }
26392 }
26393
26394 tail = gettail(*fnamep);
26395 *fnamelen = (int)STRLEN(*fnamep);
26396
26397 /* ":h" - head, remove "/file_name", can be repeated */
26398 /* Don't remove the first "/" or "c:\" */
26399 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26400 {
26401 valid |= VALID_HEAD;
26402 *usedlen += 2;
26403 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026404 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026405 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026406 *fnamelen = (int)(tail - *fnamep);
26407#ifdef VMS
26408 if (*fnamelen > 0)
26409 *fnamelen += 1; /* the path separator is part of the path */
26410#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026411 if (*fnamelen == 0)
26412 {
26413 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26414 p = vim_strsave((char_u *)".");
26415 if (p == NULL)
26416 return -1;
26417 vim_free(*bufp);
26418 *bufp = *fnamep = tail = p;
26419 *fnamelen = 1;
26420 }
26421 else
26422 {
26423 while (tail > s && !after_pathsep(s, tail))
26424 mb_ptr_back(*fnamep, tail);
26425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426 }
26427
26428 /* ":8" - shortname */
26429 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26430 {
26431 *usedlen += 2;
26432#ifdef WIN3264
26433 has_shortname = 1;
26434#endif
26435 }
26436
26437#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026438 /*
26439 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026440 */
26441 if (has_shortname)
26442 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026443 /* Copy the string if it is shortened by :h and when it wasn't copied
26444 * yet, because we are going to change it in place. Avoids changing
26445 * the buffer name for "%:8". */
26446 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026447 {
26448 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026449 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026450 return -1;
26451 vim_free(*bufp);
26452 *bufp = *fnamep = p;
26453 }
26454
26455 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026456 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026457 if (!has_fullname && !vim_isAbsName(*fnamep))
26458 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026459 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460 return -1;
26461 }
26462 else
26463 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026464 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026465
Bram Moolenaardc935552011-08-17 15:23:23 +020026466 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026467 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026468 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469 return -1;
26470
26471 if (l == 0)
26472 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026473 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026474 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026475 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026476 return -1;
26477 }
26478 *fnamelen = l;
26479 }
26480 }
26481#endif /* WIN3264 */
26482
26483 /* ":t" - tail, just the basename */
26484 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26485 {
26486 *usedlen += 2;
26487 *fnamelen -= (int)(tail - *fnamep);
26488 *fnamep = tail;
26489 }
26490
26491 /* ":e" - extension, can be repeated */
26492 /* ":r" - root, without extension, can be repeated */
26493 while (src[*usedlen] == ':'
26494 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26495 {
26496 /* find a '.' in the tail:
26497 * - for second :e: before the current fname
26498 * - otherwise: The last '.'
26499 */
26500 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26501 s = *fnamep - 2;
26502 else
26503 s = *fnamep + *fnamelen - 1;
26504 for ( ; s > tail; --s)
26505 if (s[0] == '.')
26506 break;
26507 if (src[*usedlen + 1] == 'e') /* :e */
26508 {
26509 if (s > tail)
26510 {
26511 *fnamelen += (int)(*fnamep - (s + 1));
26512 *fnamep = s + 1;
26513#ifdef VMS
26514 /* cut version from the extension */
26515 s = *fnamep + *fnamelen - 1;
26516 for ( ; s > *fnamep; --s)
26517 if (s[0] == ';')
26518 break;
26519 if (s > *fnamep)
26520 *fnamelen = s - *fnamep;
26521#endif
26522 }
26523 else if (*fnamep <= tail)
26524 *fnamelen = 0;
26525 }
26526 else /* :r */
26527 {
26528 if (s > tail) /* remove one extension */
26529 *fnamelen = (int)(s - *fnamep);
26530 }
26531 *usedlen += 2;
26532 }
26533
26534 /* ":s?pat?foo?" - substitute */
26535 /* ":gs?pat?foo?" - global substitute */
26536 if (src[*usedlen] == ':'
26537 && (src[*usedlen + 1] == 's'
26538 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26539 {
26540 char_u *str;
26541 char_u *pat;
26542 char_u *sub;
26543 int sep;
26544 char_u *flags;
26545 int didit = FALSE;
26546
26547 flags = (char_u *)"";
26548 s = src + *usedlen + 2;
26549 if (src[*usedlen + 1] == 'g')
26550 {
26551 flags = (char_u *)"g";
26552 ++s;
26553 }
26554
26555 sep = *s++;
26556 if (sep)
26557 {
26558 /* find end of pattern */
26559 p = vim_strchr(s, sep);
26560 if (p != NULL)
26561 {
26562 pat = vim_strnsave(s, (int)(p - s));
26563 if (pat != NULL)
26564 {
26565 s = p + 1;
26566 /* find end of substitution */
26567 p = vim_strchr(s, sep);
26568 if (p != NULL)
26569 {
26570 sub = vim_strnsave(s, (int)(p - s));
26571 str = vim_strnsave(*fnamep, *fnamelen);
26572 if (sub != NULL && str != NULL)
26573 {
26574 *usedlen = (int)(p + 1 - src);
26575 s = do_string_sub(str, pat, sub, flags);
26576 if (s != NULL)
26577 {
26578 *fnamep = s;
26579 *fnamelen = (int)STRLEN(s);
26580 vim_free(*bufp);
26581 *bufp = s;
26582 didit = TRUE;
26583 }
26584 }
26585 vim_free(sub);
26586 vim_free(str);
26587 }
26588 vim_free(pat);
26589 }
26590 }
26591 /* after using ":s", repeat all the modifiers */
26592 if (didit)
26593 goto repeat;
26594 }
26595 }
26596
Bram Moolenaar26df0922014-02-23 23:39:13 +010026597 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26598 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026599 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026600 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026601 if (c != NUL)
26602 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026603 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026604 if (c != NUL)
26605 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026606 if (p == NULL)
26607 return -1;
26608 vim_free(*bufp);
26609 *bufp = *fnamep = p;
26610 *fnamelen = (int)STRLEN(p);
26611 *usedlen += 2;
26612 }
26613
Bram Moolenaar071d4272004-06-13 20:20:40 +000026614 return valid;
26615}
26616
26617/*
26618 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26619 * "flags" can be "g" to do a global substitute.
26620 * Returns an allocated string, NULL for error.
26621 */
26622 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026623do_string_sub(
26624 char_u *str,
26625 char_u *pat,
26626 char_u *sub,
26627 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026628{
26629 int sublen;
26630 regmatch_T regmatch;
26631 int i;
26632 int do_all;
26633 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026634 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026635 garray_T ga;
26636 char_u *ret;
26637 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026638 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026639
26640 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26641 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026642 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026643
26644 ga_init2(&ga, 1, 200);
26645
26646 do_all = (flags[0] == 'g');
26647
26648 regmatch.rm_ic = p_ic;
26649 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26650 if (regmatch.regprog != NULL)
26651 {
26652 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026653 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026654 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26655 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026656 /* Skip empty match except for first match. */
26657 if (regmatch.startp[0] == regmatch.endp[0])
26658 {
26659 if (zero_width == regmatch.startp[0])
26660 {
26661 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026662 i = MB_PTR2LEN(tail);
26663 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26664 (size_t)i);
26665 ga.ga_len += i;
26666 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026667 continue;
26668 }
26669 zero_width = regmatch.startp[0];
26670 }
26671
Bram Moolenaar071d4272004-06-13 20:20:40 +000026672 /*
26673 * Get some space for a temporary buffer to do the substitution
26674 * into. It will contain:
26675 * - The text up to where the match is.
26676 * - The substituted text.
26677 * - The text after the match.
26678 */
26679 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026680 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026681 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26682 {
26683 ga_clear(&ga);
26684 break;
26685 }
26686
26687 /* copy the text up to where the match is */
26688 i = (int)(regmatch.startp[0] - tail);
26689 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26690 /* add the substituted text */
26691 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26692 + ga.ga_len + i, TRUE, TRUE, FALSE);
26693 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026694 tail = regmatch.endp[0];
26695 if (*tail == NUL)
26696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026697 if (!do_all)
26698 break;
26699 }
26700
26701 if (ga.ga_data != NULL)
26702 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26703
Bram Moolenaar473de612013-06-08 18:19:48 +020026704 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026705 }
26706
26707 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26708 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026709 if (p_cpo == empty_option)
26710 p_cpo = save_cpo;
26711 else
26712 /* Darn, evaluating {sub} expression changed the value. */
26713 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026714
26715 return ret;
26716}
26717
26718#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */