blob: 0c270b928d4c68476939be063488e9a8d071a3f4 [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);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200433static void list_free_contents(list_T *l);
434static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100435static long list_len(list_T *l);
436static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
437static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
438static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
439static long list_find_nr(list_T *l, long idx, int *errorp);
440static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100441static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
442static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
443static list_T *list_copy(list_T *orig, int deep, int copyID);
444static char_u *list2string(typval_T *tv, int copyID);
445static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
446static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
447static int free_unref_items(int copyID);
448static dictitem_T *dictitem_copy(dictitem_T *org);
449static void dictitem_remove(dict_T *dict, dictitem_T *item);
450static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
451static long dict_len(dict_T *d);
452static char_u *dict2string(typval_T *tv, int copyID);
453static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
454static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
455static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
456static char_u *string_quote(char_u *str, int function);
457static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
458static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100459static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
460static 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 +0100461static void emsg_funcname(char *ermsg, char_u *name);
462static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000463
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200464static void dict_free_contents(dict_T *d);
465static void dict_free_dict(dict_T *d);
466
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100468static void f_abs(typval_T *argvars, typval_T *rettv);
469static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_add(typval_T *argvars, typval_T *rettv);
472static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
473static void f_and(typval_T *argvars, typval_T *rettv);
474static void f_append(typval_T *argvars, typval_T *rettv);
475static void f_argc(typval_T *argvars, typval_T *rettv);
476static void f_argidx(typval_T *argvars, typval_T *rettv);
477static void f_arglistid(typval_T *argvars, typval_T *rettv);
478static void f_argv(typval_T *argvars, typval_T *rettv);
479static void f_assert_equal(typval_T *argvars, typval_T *rettv);
480static void f_assert_exception(typval_T *argvars, typval_T *rettv);
481static void f_assert_fails(typval_T *argvars, typval_T *rettv);
482static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200483static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200484static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
485static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100486static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100488static void f_asin(typval_T *argvars, typval_T *rettv);
489static void f_atan(typval_T *argvars, typval_T *rettv);
490static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100492static void f_browse(typval_T *argvars, typval_T *rettv);
493static void f_browsedir(typval_T *argvars, typval_T *rettv);
494static void f_bufexists(typval_T *argvars, typval_T *rettv);
495static void f_buflisted(typval_T *argvars, typval_T *rettv);
496static void f_bufloaded(typval_T *argvars, typval_T *rettv);
497static void f_bufname(typval_T *argvars, typval_T *rettv);
498static void f_bufnr(typval_T *argvars, typval_T *rettv);
499static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
500static void f_byte2line(typval_T *argvars, typval_T *rettv);
501static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
502static void f_byteidx(typval_T *argvars, typval_T *rettv);
503static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
504static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100506static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100508#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100509static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100510static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100512static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100513static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100514static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100515static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100516static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
517static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100518static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100520static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
521static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100522static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100523static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100524#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100525static void f_changenr(typval_T *argvars, typval_T *rettv);
526static void f_char2nr(typval_T *argvars, typval_T *rettv);
527static void f_cindent(typval_T *argvars, typval_T *rettv);
528static void f_clearmatches(typval_T *argvars, typval_T *rettv);
529static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000530#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_complete(typval_T *argvars, typval_T *rettv);
532static void f_complete_add(typval_T *argvars, typval_T *rettv);
533static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000534#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100535static void f_confirm(typval_T *argvars, typval_T *rettv);
536static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_cos(typval_T *argvars, typval_T *rettv);
539static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_count(typval_T *argvars, typval_T *rettv);
542static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
543static void f_cursor(typval_T *argsvars, typval_T *rettv);
544static void f_deepcopy(typval_T *argvars, typval_T *rettv);
545static void f_delete(typval_T *argvars, typval_T *rettv);
546static void f_did_filetype(typval_T *argvars, typval_T *rettv);
547static void f_diff_filler(typval_T *argvars, typval_T *rettv);
548static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100549static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_empty(typval_T *argvars, typval_T *rettv);
551static void f_escape(typval_T *argvars, typval_T *rettv);
552static void f_eval(typval_T *argvars, typval_T *rettv);
553static void f_eventhandler(typval_T *argvars, typval_T *rettv);
554static void f_executable(typval_T *argvars, typval_T *rettv);
555static void f_exepath(typval_T *argvars, typval_T *rettv);
556static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200559#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_expand(typval_T *argvars, typval_T *rettv);
561static void f_extend(typval_T *argvars, typval_T *rettv);
562static void f_feedkeys(typval_T *argvars, typval_T *rettv);
563static void f_filereadable(typval_T *argvars, typval_T *rettv);
564static void f_filewritable(typval_T *argvars, typval_T *rettv);
565static void f_filter(typval_T *argvars, typval_T *rettv);
566static void f_finddir(typval_T *argvars, typval_T *rettv);
567static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000568#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100569static void f_float2nr(typval_T *argvars, typval_T *rettv);
570static void f_floor(typval_T *argvars, typval_T *rettv);
571static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000572#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100573static void f_fnameescape(typval_T *argvars, typval_T *rettv);
574static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
575static void f_foldclosed(typval_T *argvars, typval_T *rettv);
576static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
577static void f_foldlevel(typval_T *argvars, typval_T *rettv);
578static void f_foldtext(typval_T *argvars, typval_T *rettv);
579static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
580static void f_foreground(typval_T *argvars, typval_T *rettv);
581static void f_function(typval_T *argvars, typval_T *rettv);
582static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
583static void f_get(typval_T *argvars, typval_T *rettv);
584static void f_getbufline(typval_T *argvars, typval_T *rettv);
585static void f_getbufvar(typval_T *argvars, typval_T *rettv);
586static void f_getchar(typval_T *argvars, typval_T *rettv);
587static void f_getcharmod(typval_T *argvars, typval_T *rettv);
588static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
589static void f_getcmdline(typval_T *argvars, typval_T *rettv);
590static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
591static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
592static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
593static void f_getcwd(typval_T *argvars, typval_T *rettv);
594static void f_getfontname(typval_T *argvars, typval_T *rettv);
595static void f_getfperm(typval_T *argvars, typval_T *rettv);
596static void f_getfsize(typval_T *argvars, typval_T *rettv);
597static void f_getftime(typval_T *argvars, typval_T *rettv);
598static void f_getftype(typval_T *argvars, typval_T *rettv);
599static void f_getline(typval_T *argvars, typval_T *rettv);
600static void f_getmatches(typval_T *argvars, typval_T *rettv);
601static void f_getpid(typval_T *argvars, typval_T *rettv);
602static void f_getcurpos(typval_T *argvars, typval_T *rettv);
603static void f_getpos(typval_T *argvars, typval_T *rettv);
604static void f_getqflist(typval_T *argvars, typval_T *rettv);
605static void f_getreg(typval_T *argvars, typval_T *rettv);
606static void f_getregtype(typval_T *argvars, typval_T *rettv);
607static void f_gettabvar(typval_T *argvars, typval_T *rettv);
608static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
609static void f_getwinposx(typval_T *argvars, typval_T *rettv);
610static void f_getwinposy(typval_T *argvars, typval_T *rettv);
611static void f_getwinvar(typval_T *argvars, typval_T *rettv);
612static void f_glob(typval_T *argvars, typval_T *rettv);
613static void f_globpath(typval_T *argvars, typval_T *rettv);
614static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
615static void f_has(typval_T *argvars, typval_T *rettv);
616static void f_has_key(typval_T *argvars, typval_T *rettv);
617static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
618static void f_hasmapto(typval_T *argvars, typval_T *rettv);
619static void f_histadd(typval_T *argvars, typval_T *rettv);
620static void f_histdel(typval_T *argvars, typval_T *rettv);
621static void f_histget(typval_T *argvars, typval_T *rettv);
622static void f_histnr(typval_T *argvars, typval_T *rettv);
623static void f_hlID(typval_T *argvars, typval_T *rettv);
624static void f_hlexists(typval_T *argvars, typval_T *rettv);
625static void f_hostname(typval_T *argvars, typval_T *rettv);
626static void f_iconv(typval_T *argvars, typval_T *rettv);
627static void f_indent(typval_T *argvars, typval_T *rettv);
628static void f_index(typval_T *argvars, typval_T *rettv);
629static void f_input(typval_T *argvars, typval_T *rettv);
630static void f_inputdialog(typval_T *argvars, typval_T *rettv);
631static void f_inputlist(typval_T *argvars, typval_T *rettv);
632static void f_inputrestore(typval_T *argvars, typval_T *rettv);
633static void f_inputsave(typval_T *argvars, typval_T *rettv);
634static void f_inputsecret(typval_T *argvars, typval_T *rettv);
635static void f_insert(typval_T *argvars, typval_T *rettv);
636static void f_invert(typval_T *argvars, typval_T *rettv);
637static void f_isdirectory(typval_T *argvars, typval_T *rettv);
638static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100639#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
640static void f_isnan(typval_T *argvars, typval_T *rettv);
641#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100642static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100643#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100644static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100645static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100646static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100647static void f_job_start(typval_T *argvars, typval_T *rettv);
648static void f_job_stop(typval_T *argvars, typval_T *rettv);
649static void f_job_status(typval_T *argvars, typval_T *rettv);
650#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100651static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100652static void f_js_decode(typval_T *argvars, typval_T *rettv);
653static void f_js_encode(typval_T *argvars, typval_T *rettv);
654static void f_json_decode(typval_T *argvars, typval_T *rettv);
655static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100656static void f_keys(typval_T *argvars, typval_T *rettv);
657static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
658static void f_len(typval_T *argvars, typval_T *rettv);
659static void f_libcall(typval_T *argvars, typval_T *rettv);
660static void f_libcallnr(typval_T *argvars, typval_T *rettv);
661static void f_line(typval_T *argvars, typval_T *rettv);
662static void f_line2byte(typval_T *argvars, typval_T *rettv);
663static void f_lispindent(typval_T *argvars, typval_T *rettv);
664static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000665#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100666static void f_log(typval_T *argvars, typval_T *rettv);
667static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200669#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100670static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200671#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_map(typval_T *argvars, typval_T *rettv);
673static void f_maparg(typval_T *argvars, typval_T *rettv);
674static void f_mapcheck(typval_T *argvars, typval_T *rettv);
675static void f_match(typval_T *argvars, typval_T *rettv);
676static void f_matchadd(typval_T *argvars, typval_T *rettv);
677static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
678static void f_matcharg(typval_T *argvars, typval_T *rettv);
679static void f_matchdelete(typval_T *argvars, typval_T *rettv);
680static void f_matchend(typval_T *argvars, typval_T *rettv);
681static void f_matchlist(typval_T *argvars, typval_T *rettv);
682static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200683static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100684static void f_max(typval_T *argvars, typval_T *rettv);
685static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000686#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000688#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100690#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100692#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
694static void f_nr2char(typval_T *argvars, typval_T *rettv);
695static void f_or(typval_T *argvars, typval_T *rettv);
696static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100697#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100699#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
704static void f_printf(typval_T *argvars, typval_T *rettv);
705static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200706#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200708#endif
709#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_range(typval_T *argvars, typval_T *rettv);
713static void f_readfile(typval_T *argvars, typval_T *rettv);
714static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100715#ifdef FEAT_FLOAT
716static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
717#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_reltimestr(typval_T *argvars, typval_T *rettv);
719static void f_remote_expr(typval_T *argvars, typval_T *rettv);
720static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
721static void f_remote_peek(typval_T *argvars, typval_T *rettv);
722static void f_remote_read(typval_T *argvars, typval_T *rettv);
723static void f_remote_send(typval_T *argvars, typval_T *rettv);
724static void f_remove(typval_T *argvars, typval_T *rettv);
725static void f_rename(typval_T *argvars, typval_T *rettv);
726static void f_repeat(typval_T *argvars, typval_T *rettv);
727static void f_resolve(typval_T *argvars, typval_T *rettv);
728static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100730static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100732static void f_screenattr(typval_T *argvars, typval_T *rettv);
733static void f_screenchar(typval_T *argvars, typval_T *rettv);
734static void f_screencol(typval_T *argvars, typval_T *rettv);
735static void f_screenrow(typval_T *argvars, typval_T *rettv);
736static void f_search(typval_T *argvars, typval_T *rettv);
737static void f_searchdecl(typval_T *argvars, typval_T *rettv);
738static void f_searchpair(typval_T *argvars, typval_T *rettv);
739static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
740static void f_searchpos(typval_T *argvars, typval_T *rettv);
741static void f_server2client(typval_T *argvars, typval_T *rettv);
742static void f_serverlist(typval_T *argvars, typval_T *rettv);
743static void f_setbufvar(typval_T *argvars, typval_T *rettv);
744static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
745static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100746static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_setline(typval_T *argvars, typval_T *rettv);
748static void f_setloclist(typval_T *argvars, typval_T *rettv);
749static void f_setmatches(typval_T *argvars, typval_T *rettv);
750static void f_setpos(typval_T *argvars, typval_T *rettv);
751static void f_setqflist(typval_T *argvars, typval_T *rettv);
752static void f_setreg(typval_T *argvars, typval_T *rettv);
753static void f_settabvar(typval_T *argvars, typval_T *rettv);
754static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
755static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100756#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100758#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_shellescape(typval_T *argvars, typval_T *rettv);
760static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
761static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sin(typval_T *argvars, typval_T *rettv);
764static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_sort(typval_T *argvars, typval_T *rettv);
767static void f_soundfold(typval_T *argvars, typval_T *rettv);
768static void f_spellbadword(typval_T *argvars, typval_T *rettv);
769static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
770static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_sqrt(typval_T *argvars, typval_T *rettv);
773static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000774#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_str2nr(typval_T *argvars, typval_T *rettv);
776static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000777#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000779#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_stridx(typval_T *argvars, typval_T *rettv);
781static void f_string(typval_T *argvars, typval_T *rettv);
782static void f_strlen(typval_T *argvars, typval_T *rettv);
783static void f_strpart(typval_T *argvars, typval_T *rettv);
784static void f_strridx(typval_T *argvars, typval_T *rettv);
785static void f_strtrans(typval_T *argvars, typval_T *rettv);
786static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
787static void f_strwidth(typval_T *argvars, typval_T *rettv);
788static void f_submatch(typval_T *argvars, typval_T *rettv);
789static void f_substitute(typval_T *argvars, typval_T *rettv);
790static void f_synID(typval_T *argvars, typval_T *rettv);
791static void f_synIDattr(typval_T *argvars, typval_T *rettv);
792static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
793static void f_synstack(typval_T *argvars, typval_T *rettv);
794static void f_synconcealed(typval_T *argvars, typval_T *rettv);
795static void f_system(typval_T *argvars, typval_T *rettv);
796static void f_systemlist(typval_T *argvars, typval_T *rettv);
797static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
798static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
799static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
800static void f_taglist(typval_T *argvars, typval_T *rettv);
801static void f_tagfiles(typval_T *argvars, typval_T *rettv);
802static void f_tempname(typval_T *argvars, typval_T *rettv);
803static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200804#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_tan(typval_T *argvars, typval_T *rettv);
806static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200807#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100808#ifdef FEAT_TIMERS
809static void f_timer_start(typval_T *argvars, typval_T *rettv);
810static void f_timer_stop(typval_T *argvars, typval_T *rettv);
811#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100812static void f_tolower(typval_T *argvars, typval_T *rettv);
813static void f_toupper(typval_T *argvars, typval_T *rettv);
814static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000815#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100816static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000817#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100818static void f_type(typval_T *argvars, typval_T *rettv);
819static void f_undofile(typval_T *argvars, typval_T *rettv);
820static void f_undotree(typval_T *argvars, typval_T *rettv);
821static void f_uniq(typval_T *argvars, typval_T *rettv);
822static void f_values(typval_T *argvars, typval_T *rettv);
823static void f_virtcol(typval_T *argvars, typval_T *rettv);
824static void f_visualmode(typval_T *argvars, typval_T *rettv);
825static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100826static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100827static void f_win_getid(typval_T *argvars, typval_T *rettv);
828static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
829static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
830static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100831static void f_winbufnr(typval_T *argvars, typval_T *rettv);
832static void f_wincol(typval_T *argvars, typval_T *rettv);
833static void f_winheight(typval_T *argvars, typval_T *rettv);
834static void f_winline(typval_T *argvars, typval_T *rettv);
835static void f_winnr(typval_T *argvars, typval_T *rettv);
836static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
837static void f_winrestview(typval_T *argvars, typval_T *rettv);
838static void f_winsaveview(typval_T *argvars, typval_T *rettv);
839static void f_winwidth(typval_T *argvars, typval_T *rettv);
840static void f_writefile(typval_T *argvars, typval_T *rettv);
841static void f_wordcount(typval_T *argvars, typval_T *rettv);
842static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000843
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100844static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
845static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
846static int get_env_len(char_u **arg);
847static int get_id_len(char_u **arg);
848static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
849static 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 +0000850#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
851#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
852 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100853static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
854static int eval_isnamec(int c);
855static int eval_isnamec1(int c);
856static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
857static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100858static typval_T *alloc_string_tv(char_u *string);
859static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100860#ifdef FEAT_FLOAT
861static float_T get_tv_float(typval_T *varp);
862#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100863static linenr_T get_tv_lnum(typval_T *argvars);
864static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100865static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
866static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
867static hashtab_T *find_var_ht(char_u *name, char_u **varname);
868static funccall_T *get_funccal(void);
869static void vars_clear_ext(hashtab_T *ht, int free_val);
870static void delete_var(hashtab_T *ht, hashitem_T *hi);
871static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
872static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
873static void set_var(char_u *name, typval_T *varp, int copy);
874static int var_check_ro(int flags, char_u *name, int use_gettext);
875static int var_check_fixed(int flags, char_u *name, int use_gettext);
876static int var_check_func_name(char_u *name, int new_var);
877static int valid_varname(char_u *varname);
878static int tv_check_lock(int lock, char_u *name, int use_gettext);
879static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
880static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100881static 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 +0100882static int eval_fname_script(char_u *p);
883static int eval_fname_sid(char_u *p);
884static void list_func_head(ufunc_T *fp, int indent);
885static ufunc_T *find_func(char_u *name);
886static int function_exists(char_u *name);
887static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000888#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889static void func_do_profile(ufunc_T *fp);
890static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
891static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
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_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000897static int
898# ifdef __BORLANDC__
899 _RTLENTRYF
900# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100901 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000902#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100903static int script_autoload(char_u *name, int reload);
904static char_u *autoload_name(char_u *name);
905static void cat_func_name(char_u *buf, ufunc_T *fp);
906static void func_free(ufunc_T *fp);
907static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
908static int can_free_funccal(funccall_T *fc, int copyID) ;
909static void free_funccal(funccall_T *fc, int free_val);
910static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
911static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
912static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
913static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
914static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
915static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
916static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
917static int write_list(FILE *fd, list_T *list, int binary);
918static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000919
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200920
921#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100922static int compare_func_name(const void *s1, const void *s2);
923static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200924#endif
925
Bram Moolenaar33570922005-01-25 22:26:29 +0000926/*
927 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000928 */
929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100930eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000931{
Bram Moolenaar33570922005-01-25 22:26:29 +0000932 int i;
933 struct vimvar *p;
934
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200935 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
936 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200937 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000938 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000939 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940
941 for (i = 0; i < VV_LEN; ++i)
942 {
943 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200944 if (STRLEN(p->vv_name) > 16)
945 {
946 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
947 getout(1);
948 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000949 STRCPY(p->vv_di.di_key, p->vv_name);
950 if (p->vv_flags & VV_RO)
951 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
952 else if (p->vv_flags & VV_RO_SBX)
953 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
954 else
955 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000956
957 /* add to v: scope dict, unless the value is not always available */
958 if (p->vv_type != VAR_UNKNOWN)
959 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000960 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000961 /* add to compat scope dict */
962 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000963 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100964 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
965
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000966 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100967 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200968 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100969 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100970
971 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
972 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
973 set_vim_var_nr(VV_NONE, VVAL_NONE);
974 set_vim_var_nr(VV_NULL, VVAL_NULL);
975
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200976 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200977
978#ifdef EBCDIC
979 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100980 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200981 */
982 sortFunctions();
983#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000984}
985
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986#if defined(EXITFREE) || defined(PROTO)
987 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100988eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989{
990 int i;
991 struct vimvar *p;
992
993 for (i = 0; i < VV_LEN; ++i)
994 {
995 p = &vimvars[i];
996 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000997 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000998 vim_free(p->vv_str);
999 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001000 }
1001 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1002 {
1003 list_unref(p->vv_list);
1004 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001005 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001006 }
1007 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001008 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001009 hash_clear(&compat_hashtab);
1010
Bram Moolenaard9fba312005-06-26 22:34:35 +00001011 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001012# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001013 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001014# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001015
1016 /* global variables */
1017 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001018
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001019 /* autoloaded script names */
1020 ga_clear_strings(&ga_loaded);
1021
Bram Moolenaarcca74132013-09-25 21:00:28 +02001022 /* Script-local variables. First clear all the variables and in a second
1023 * loop free the scriptvar_T, because a variable in one script might hold
1024 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001025 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001026 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001027 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001028 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001029 ga_clear(&ga_scripts);
1030
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001031 /* unreferenced lists and dicts */
1032 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001033
1034 /* functions */
1035 free_all_functions();
1036 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001037}
1038#endif
1039
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 * Return the name of the executed function.
1042 */
1043 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001044func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047}
1048
1049/*
1050 * Return the address holding the next breakpoint line for a funccall cookie.
1051 */
1052 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001053func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054{
Bram Moolenaar33570922005-01-25 22:26:29 +00001055 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056}
1057
1058/*
1059 * Return the address holding the debug tick for a funccall cookie.
1060 */
1061 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001062func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063{
Bram Moolenaar33570922005-01-25 22:26:29 +00001064 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065}
1066
1067/*
1068 * Return the nesting level for a funccall cookie.
1069 */
1070 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001071func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072{
Bram Moolenaar33570922005-01-25 22:26:29 +00001073 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074}
1075
1076/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001077funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001079/* pointer to list of previously used funccal, still around because some
1080 * item in it is still being used. */
1081funccall_T *previous_funccal = NULL;
1082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083/*
1084 * Return TRUE when a function was ended by a ":return" command.
1085 */
1086 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001087current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
1089 return current_funccal->returned;
1090}
1091
1092
1093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 * Set an internal variable to a string value. Creates the variable if it does
1095 * not already exist.
1096 */
1097 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001098set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001100 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001101 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102
1103 val = vim_strsave(value);
1104 if (val != NULL)
1105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001106 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001107 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001109 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001110 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
1112 }
1113}
1114
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117static char_u *redir_endp = NULL;
1118static char_u *redir_varname = NULL;
1119
1120/*
1121 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001122 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 * Returns OK if successfully completed the setup. FAIL otherwise.
1124 */
1125 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001126var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127{
1128 int save_emsg;
1129 int err;
1130 typval_T tv;
1131
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001132 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001133 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 {
1135 EMSG(_(e_invarg));
1136 return FAIL;
1137 }
1138
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 redir_varname = vim_strsave(name);
1141 if (redir_varname == NULL)
1142 return FAIL;
1143
1144 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1145 if (redir_lval == NULL)
1146 {
1147 var_redir_stop();
1148 return FAIL;
1149 }
1150
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001151 /* The output is stored in growarray "redir_ga" until redirection ends. */
1152 ga_init2(&redir_ga, (int)sizeof(char), 500);
1153
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001155 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001156 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1158 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001159 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 if (redir_endp != NULL && *redir_endp != NUL)
1161 /* Trailing characters are present after the variable name */
1162 EMSG(_(e_trailing));
1163 else
1164 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 var_redir_stop();
1167 return FAIL;
1168 }
1169
1170 /* check if we can write to the variable: set it to or append an empty
1171 * string */
1172 save_emsg = did_emsg;
1173 did_emsg = FALSE;
1174 tv.v_type = VAR_STRING;
1175 tv.vval.v_string = (char_u *)"";
1176 if (append)
1177 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1178 else
1179 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001180 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001182 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001183 if (err)
1184 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001185 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186 var_redir_stop();
1187 return FAIL;
1188 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189
1190 return OK;
1191}
1192
1193/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001194 * Append "value[value_len]" to the variable set by var_redir_start().
1195 * The actual appending is postponed until redirection ends, because the value
1196 * appended may in fact be the string we write to, changing it may cause freed
1197 * memory to be used:
1198 * :redir => foo
1199 * :let foo
1200 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 */
1202 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001203var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001205 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001206
1207 if (redir_lval == NULL)
1208 return;
1209
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001210 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001211 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001213 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001215 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216 {
1217 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001218 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001219 }
1220 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001222}
1223
1224/*
1225 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001226 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 */
1228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001229var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001231 typval_T tv;
1232
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001233 if (redir_lval != NULL)
1234 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001235 /* If there was no error: assign the text to the variable. */
1236 if (redir_endp != NULL)
1237 {
1238 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1239 tv.v_type = VAR_STRING;
1240 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001241 /* Call get_lval() again, if it's inside a Dict or List it may
1242 * have changed. */
1243 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001244 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001245 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1246 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1247 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001248 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001249
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001250 /* free the collected output */
1251 vim_free(redir_ga.ga_data);
1252 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001253
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001254 vim_free(redir_lval);
1255 redir_lval = NULL;
1256 }
1257 vim_free(redir_varname);
1258 redir_varname = NULL;
1259}
1260
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261# if defined(FEAT_MBYTE) || defined(PROTO)
1262 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001263eval_charconvert(
1264 char_u *enc_from,
1265 char_u *enc_to,
1266 char_u *fname_from,
1267 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268{
1269 int err = FALSE;
1270
1271 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1272 set_vim_var_string(VV_CC_TO, enc_to, -1);
1273 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1274 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1275 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1276 err = TRUE;
1277 set_vim_var_string(VV_CC_FROM, NULL, -1);
1278 set_vim_var_string(VV_CC_TO, NULL, -1);
1279 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1280 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1281
1282 if (err)
1283 return FAIL;
1284 return OK;
1285}
1286# endif
1287
1288# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001290eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291{
1292 int err = FALSE;
1293
1294 set_vim_var_string(VV_FNAME_IN, fname, -1);
1295 set_vim_var_string(VV_CMDARG, args, -1);
1296 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1297 err = TRUE;
1298 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1299 set_vim_var_string(VV_CMDARG, NULL, -1);
1300
1301 if (err)
1302 {
1303 mch_remove(fname);
1304 return FAIL;
1305 }
1306 return OK;
1307}
1308# endif
1309
1310# if defined(FEAT_DIFF) || defined(PROTO)
1311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001312eval_diff(
1313 char_u *origfile,
1314 char_u *newfile,
1315 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 int err = FALSE;
1318
1319 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1320 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1321 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1322 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1323 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1324 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1325 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1326}
1327
1328 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001329eval_patch(
1330 char_u *origfile,
1331 char_u *difffile,
1332 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333{
1334 int err;
1335
1336 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1337 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1338 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1339 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1340 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1341 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1342 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1343}
1344# endif
1345
1346/*
1347 * Top level evaluation function, returning a boolean.
1348 * Sets "error" to TRUE if there was an error.
1349 * Return TRUE or FALSE.
1350 */
1351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001352eval_to_bool(
1353 char_u *arg,
1354 int *error,
1355 char_u **nextcmd,
1356 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
Bram Moolenaar33570922005-01-25 22:26:29 +00001358 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 int retval = FALSE;
1360
1361 if (skip)
1362 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001363 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 else
1366 {
1367 *error = FALSE;
1368 if (!skip)
1369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001370 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373 }
1374 if (skip)
1375 --emsg_skip;
1376
1377 return retval;
1378}
1379
1380/*
1381 * Top level evaluation function, returning a string. If "skip" is TRUE,
1382 * only parsing to "nextcmd" is done, without reporting errors. Return
1383 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1384 */
1385 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001386eval_to_string_skip(
1387 char_u *arg,
1388 char_u **nextcmd,
1389 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
Bram Moolenaar33570922005-01-25 22:26:29 +00001391 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *retval;
1393
1394 if (skip)
1395 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 retval = NULL;
1398 else
1399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001400 retval = vim_strsave(get_tv_string(&tv));
1401 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403 if (skip)
1404 --emsg_skip;
1405
1406 return retval;
1407}
1408
1409/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001410 * Skip over an expression at "*pp".
1411 * Return FAIL for an error, OK otherwise.
1412 */
1413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001414skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001415{
Bram Moolenaar33570922005-01-25 22:26:29 +00001416 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001417
1418 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001420}
1421
1422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001424 * When "convert" is TRUE convert a List into a sequence of lines and convert
1425 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 * Return pointer to allocated memory, or NULL for failure.
1427 */
1428 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001429eval_to_string(
1430 char_u *arg,
1431 char_u **nextcmd,
1432 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433{
Bram Moolenaar33570922005-01-25 22:26:29 +00001434 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001436 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001437#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001438 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001441 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 retval = NULL;
1443 else
1444 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001445 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001446 {
1447 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001448 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001449 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001450 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001451 if (tv.vval.v_list->lv_len > 0)
1452 ga_append(&ga, NL);
1453 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001454 ga_append(&ga, NUL);
1455 retval = (char_u *)ga.ga_data;
1456 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001457#ifdef FEAT_FLOAT
1458 else if (convert && tv.v_type == VAR_FLOAT)
1459 {
1460 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1461 retval = vim_strsave(numbuf);
1462 }
1463#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001464 else
1465 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001466 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 }
1468
1469 return retval;
1470}
1471
1472/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001473 * Call eval_to_string() without using current local variables and using
1474 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 */
1476 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001477eval_to_string_safe(
1478 char_u *arg,
1479 char_u **nextcmd,
1480 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481{
1482 char_u *retval;
1483 void *save_funccalp;
1484
1485 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001486 if (use_sandbox)
1487 ++sandbox;
1488 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001489 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001490 if (use_sandbox)
1491 --sandbox;
1492 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 restore_funccal(save_funccalp);
1494 return retval;
1495}
1496
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497/*
1498 * Top level evaluation function, returning a number.
1499 * Evaluates "expr" silently.
1500 * Returns -1 for an error.
1501 */
1502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001503eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504{
Bram Moolenaar33570922005-01-25 22:26:29 +00001505 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001507 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
1509 ++emsg_off;
1510
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 retval = -1;
1513 else
1514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001515 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001516 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 }
1518 --emsg_off;
1519
1520 return retval;
1521}
1522
Bram Moolenaara40058a2005-07-11 22:42:07 +00001523/*
1524 * Prepare v: variable "idx" to be used.
1525 * Save the current typeval in "save_tv".
1526 * When not used yet add the variable to the v: hashtable.
1527 */
1528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001529prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001530{
1531 *save_tv = vimvars[idx].vv_tv;
1532 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1533 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1534}
1535
1536/*
1537 * Restore v: variable "idx" to typeval "save_tv".
1538 * When no longer defined, remove the variable from the v: hashtable.
1539 */
1540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001541restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001542{
1543 hashitem_T *hi;
1544
Bram Moolenaara40058a2005-07-11 22:42:07 +00001545 vimvars[idx].vv_tv = *save_tv;
1546 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1547 {
1548 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1549 if (HASHITEM_EMPTY(hi))
1550 EMSG2(_(e_intern2), "restore_vimvar()");
1551 else
1552 hash_remove(&vimvarht, hi);
1553 }
1554}
1555
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001556#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001557/*
1558 * Evaluate an expression to a list with suggestions.
1559 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001560 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001561 */
1562 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001563eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001564{
1565 typval_T save_val;
1566 typval_T rettv;
1567 list_T *list = NULL;
1568 char_u *p = skipwhite(expr);
1569
1570 /* Set "v:val" to the bad word. */
1571 prepare_vimvar(VV_VAL, &save_val);
1572 vimvars[VV_VAL].vv_type = VAR_STRING;
1573 vimvars[VV_VAL].vv_str = badword;
1574 if (p_verbose == 0)
1575 ++emsg_off;
1576
1577 if (eval1(&p, &rettv, TRUE) == OK)
1578 {
1579 if (rettv.v_type != VAR_LIST)
1580 clear_tv(&rettv);
1581 else
1582 list = rettv.vval.v_list;
1583 }
1584
1585 if (p_verbose == 0)
1586 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001587 restore_vimvar(VV_VAL, &save_val);
1588
1589 return list;
1590}
1591
1592/*
1593 * "list" is supposed to contain two items: a word and a number. Return the
1594 * word in "pp" and the number as the return value.
1595 * Return -1 if anything isn't right.
1596 * Used to get the good word and score from the eval_spell_expr() result.
1597 */
1598 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001599get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001600{
1601 listitem_T *li;
1602
1603 li = list->lv_first;
1604 if (li == NULL)
1605 return -1;
1606 *pp = get_tv_string(&li->li_tv);
1607
1608 li = li->li_next;
1609 if (li == NULL)
1610 return -1;
1611 return get_tv_number(&li->li_tv);
1612}
1613#endif
1614
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001615/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001616 * Top level evaluation function.
1617 * Returns an allocated typval_T with the result.
1618 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001619 */
1620 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001621eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001622{
1623 typval_T *tv;
1624
1625 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001626 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001627 {
1628 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001629 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001630 }
1631
1632 return tv;
1633}
1634
1635
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638 * Uses argv[argc] for the function arguments. Only Number and String
1639 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001643call_vim_function(
1644 char_u *func,
1645 int argc,
1646 char_u **argv,
1647 int safe, /* use the sandbox */
1648 int str_arg_only, /* all arguments are strings */
1649 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650{
Bram Moolenaar33570922005-01-25 22:26:29 +00001651 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 long n;
1653 int len;
1654 int i;
1655 int doesrange;
1656 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001657 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001659 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001661 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663 for (i = 0; i < argc; i++)
1664 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001665 /* Pass a NULL or empty argument as an empty string */
1666 if (argv[i] == NULL || *argv[i] == NUL)
1667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001668 argvars[i].v_type = VAR_STRING;
1669 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001670 continue;
1671 }
1672
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001673 if (str_arg_only)
1674 len = 0;
1675 else
1676 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001677 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (len != 0 && len == (int)STRLEN(argv[i]))
1679 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001680 argvars[i].v_type = VAR_NUMBER;
1681 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 }
1683 else
1684 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001685 argvars[i].v_type = VAR_STRING;
1686 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 }
1688 }
1689
1690 if (safe)
1691 {
1692 save_funccalp = save_funccal();
1693 ++sandbox;
1694 }
1695
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1697 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001699 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 if (safe)
1701 {
1702 --sandbox;
1703 restore_funccal(save_funccalp);
1704 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001705 vim_free(argvars);
1706
1707 if (ret == FAIL)
1708 clear_tv(rettv);
1709
1710 return ret;
1711}
1712
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001713/*
1714 * Call vimL function "func" and return the result as a number.
1715 * Returns -1 when calling the function fails.
1716 * Uses argv[argc] for the function arguments.
1717 */
1718 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001719call_func_retnr(
1720 char_u *func,
1721 int argc,
1722 char_u **argv,
1723 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001724{
1725 typval_T rettv;
1726 long retval;
1727
1728 /* All arguments are passed as strings, no conversion to number. */
1729 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1730 return -1;
1731
1732 retval = get_tv_number_chk(&rettv, NULL);
1733 clear_tv(&rettv);
1734 return retval;
1735}
1736
1737#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1738 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1739
Bram Moolenaar4f688582007-07-24 12:34:30 +00001740# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001742 * Call vimL function "func" and return the result as a string.
1743 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 * Uses argv[argc] for the function arguments.
1745 */
1746 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001747call_func_retstr(
1748 char_u *func,
1749 int argc,
1750 char_u **argv,
1751 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001752{
1753 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001754 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001756 /* All arguments are passed as strings, no conversion to number. */
1757 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758 return NULL;
1759
1760 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 return retval;
1763}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001764# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001765
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001766/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001767 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001768 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001769 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770 */
1771 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001772call_func_retlist(
1773 char_u *func,
1774 int argc,
1775 char_u **argv,
1776 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001777{
1778 typval_T rettv;
1779
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001780 /* All arguments are passed as strings, no conversion to number. */
1781 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001782 return NULL;
1783
1784 if (rettv.v_type != VAR_LIST)
1785 {
1786 clear_tv(&rettv);
1787 return NULL;
1788 }
1789
1790 return rettv.vval.v_list;
1791}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792#endif
1793
1794/*
1795 * Save the current function call pointer, and set it to NULL.
1796 * Used when executing autocommands and for ":source".
1797 */
1798 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001799save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001801 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 current_funccal = NULL;
1804 return (void *)fc;
1805}
1806
1807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001810 funccall_T *fc = (funccall_T *)vfc;
1811
1812 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813}
1814
Bram Moolenaar05159a02005-02-26 23:04:13 +00001815#if defined(FEAT_PROFILE) || defined(PROTO)
1816/*
1817 * Prepare profiling for entering a child or something else that is not
1818 * counted for the script/function itself.
1819 * Should always be called in pair with prof_child_exit().
1820 */
1821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822prof_child_enter(
1823 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001824{
1825 funccall_T *fc = current_funccal;
1826
1827 if (fc != NULL && fc->func->uf_profiling)
1828 profile_start(&fc->prof_child);
1829 script_prof_save(tm);
1830}
1831
1832/*
1833 * Take care of time spent in a child.
1834 * Should always be called after prof_child_enter().
1835 */
1836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837prof_child_exit(
1838 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001839{
1840 funccall_T *fc = current_funccal;
1841
1842 if (fc != NULL && fc->func->uf_profiling)
1843 {
1844 profile_end(&fc->prof_child);
1845 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1846 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1847 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1848 }
1849 script_prof_restore(tm);
1850}
1851#endif
1852
1853
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#ifdef FEAT_FOLDING
1855/*
1856 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1857 * it in "*cp". Doesn't give error messages.
1858 */
1859 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001860eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
Bram Moolenaar33570922005-01-25 22:26:29 +00001862 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 int retval;
1864 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001865 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1866 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001869 if (use_sandbox)
1870 ++sandbox;
1871 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 retval = 0;
1875 else
1876 {
1877 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 if (tv.v_type == VAR_NUMBER)
1879 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001880 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 retval = 0;
1882 else
1883 {
1884 /* If the result is a string, check if there is a non-digit before
1885 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (!VIM_ISDIGIT(*s) && *s != '-')
1888 *cp = *s++;
1889 retval = atol((char *)s);
1890 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
1893 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001894 if (use_sandbox)
1895 --sandbox;
1896 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897
1898 return retval;
1899}
1900#endif
1901
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 * ":let" list all variable values
1904 * ":let var1 var2" list variable values
1905 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 * ":let var += expr" assignment command.
1907 * ":let var -= expr" assignment command.
1908 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 */
1911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001912ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913{
1914 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001916 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 int var_count = 0;
1919 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001920 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001921 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001922 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
Bram Moolenaardb552d602006-03-23 22:59:57 +00001924 argend = skip_var_list(arg, &var_count, &semicolon);
1925 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001927 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1928 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001929 expr = skipwhite(argend);
1930 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1931 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001933 /*
1934 * ":let" without "=": list variables
1935 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 if (*arg == '[')
1937 EMSG(_(e_invarg));
1938 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001940 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001941 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001942 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001944 list_glob_vars(&first);
1945 list_buf_vars(&first);
1946 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001947#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001948 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001949#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001950 list_script_vars(&first);
1951 list_func_vars(&first);
1952 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 eap->nextcmd = check_nextcmd(arg);
1955 }
1956 else
1957 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 op[0] = '=';
1959 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001960 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001962 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1963 op[0] = *expr; /* +=, -= or .= */
1964 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001966 else
1967 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (eap->skip)
1970 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001971 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (eap->skip)
1973 {
1974 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 --emsg_skip;
1977 }
1978 else if (i != FAIL)
1979 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001982 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 }
1984 }
1985}
1986
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987/*
1988 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1989 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001990 * When "nextchars" is not NULL it points to a string with characters that
1991 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1992 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 * Returns OK or FAIL;
1994 */
1995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001996ex_let_vars(
1997 char_u *arg_start,
1998 typval_T *tv,
1999 int copy, /* copy values from "tv", don't move */
2000 int semicolon, /* from skip_var_list() */
2001 int var_count, /* from skip_var_list() */
2002 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003{
2004 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002005 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002007 listitem_T *item;
2008 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009
2010 if (*arg != '[')
2011 {
2012 /*
2013 * ":let var = expr" or ":for var in list"
2014 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002015 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 return FAIL;
2017 return OK;
2018 }
2019
2020 /*
2021 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2022 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002023 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 {
2025 EMSG(_(e_listreq));
2026 return FAIL;
2027 }
2028
2029 i = list_len(l);
2030 if (semicolon == 0 && var_count < i)
2031 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002032 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002033 return FAIL;
2034 }
2035 if (var_count - semicolon > i)
2036 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002037 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002038 return FAIL;
2039 }
2040
2041 item = l->lv_first;
2042 while (*arg != ']')
2043 {
2044 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002045 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 item = item->li_next;
2047 if (arg == NULL)
2048 return FAIL;
2049
2050 arg = skipwhite(arg);
2051 if (*arg == ';')
2052 {
2053 /* Put the rest of the list (may be empty) in the var after ';'.
2054 * Create a new list for this. */
2055 l = list_alloc();
2056 if (l == NULL)
2057 return FAIL;
2058 while (item != NULL)
2059 {
2060 list_append_tv(l, &item->li_tv);
2061 item = item->li_next;
2062 }
2063
2064 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002065 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 ltv.vval.v_list = l;
2067 l->lv_refcount = 1;
2068
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002069 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2070 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071 clear_tv(&ltv);
2072 if (arg == NULL)
2073 return FAIL;
2074 break;
2075 }
2076 else if (*arg != ',' && *arg != ']')
2077 {
2078 EMSG2(_(e_intern2), "ex_let_vars()");
2079 return FAIL;
2080 }
2081 }
2082
2083 return OK;
2084}
2085
2086/*
2087 * Skip over assignable variable "var" or list of variables "[var, var]".
2088 * Used for ":let varvar = expr" and ":for varvar in expr".
2089 * For "[var, var]" increment "*var_count" for each variable.
2090 * for "[var, var; var]" set "semicolon".
2091 * Return NULL for an error.
2092 */
2093 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002094skip_var_list(
2095 char_u *arg,
2096 int *var_count,
2097 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002098{
2099 char_u *p, *s;
2100
2101 if (*arg == '[')
2102 {
2103 /* "[var, var]": find the matching ']'. */
2104 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002105 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002106 {
2107 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2108 s = skip_var_one(p);
2109 if (s == p)
2110 {
2111 EMSG2(_(e_invarg2), p);
2112 return NULL;
2113 }
2114 ++*var_count;
2115
2116 p = skipwhite(s);
2117 if (*p == ']')
2118 break;
2119 else if (*p == ';')
2120 {
2121 if (*semicolon == 1)
2122 {
2123 EMSG(_("Double ; in list of variables"));
2124 return NULL;
2125 }
2126 *semicolon = 1;
2127 }
2128 else if (*p != ',')
2129 {
2130 EMSG2(_(e_invarg2), p);
2131 return NULL;
2132 }
2133 }
2134 return p + 1;
2135 }
2136 else
2137 return skip_var_one(arg);
2138}
2139
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002140/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002141 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002142 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002143 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002144 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002145skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002146{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002147 if (*arg == '@' && arg[1] != NUL)
2148 return arg + 2;
2149 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2150 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002151}
2152
Bram Moolenaara7043832005-01-21 11:56:39 +00002153/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002154 * List variables for hashtab "ht" with prefix "prefix".
2155 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002158list_hashtable_vars(
2159 hashtab_T *ht,
2160 char_u *prefix,
2161 int empty,
2162 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002163{
Bram Moolenaar33570922005-01-25 22:26:29 +00002164 hashitem_T *hi;
2165 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002166 int todo;
2167
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002168 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002169 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2170 {
2171 if (!HASHITEM_EMPTY(hi))
2172 {
2173 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002174 di = HI2DI(hi);
2175 if (empty || di->di_tv.v_type != VAR_STRING
2176 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002178 }
2179 }
2180}
2181
2182/*
2183 * List global variables.
2184 */
2185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002186list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002187{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002189}
2190
2191/*
2192 * List buffer variables.
2193 */
2194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002195list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002196{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 char_u numbuf[NUMBUFLEN];
2198
Bram Moolenaar429fa852013-04-15 12:27:36 +02002199 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002201
2202 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2204 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002205}
2206
2207/*
2208 * List window variables.
2209 */
2210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002211list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002212{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002213 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002215}
2216
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002217#ifdef FEAT_WINDOWS
2218/*
2219 * List tab page variables.
2220 */
2221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002223{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002224 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002226}
2227#endif
2228
Bram Moolenaara7043832005-01-21 11:56:39 +00002229/*
2230 * List Vim variables.
2231 */
2232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002235 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236}
2237
2238/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002239 * List script-local variables, if there is a script.
2240 */
2241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002242list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002243{
2244 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2246 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002247}
2248
2249/*
2250 * List function variables, if there is a function.
2251 */
2252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002253list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002254{
2255 if (current_funccal != NULL)
2256 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002258}
2259
2260/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 * List variables in "arg".
2262 */
2263 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002264list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265{
2266 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 char_u *name_start;
2270 char_u *arg_subsc;
2271 char_u *tofree;
2272 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273
2274 while (!ends_excmd(*arg) && !got_int)
2275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002278 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2280 {
2281 emsg_severe = TRUE;
2282 EMSG(_(e_trailing));
2283 break;
2284 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 /* get_name_len() takes care of expanding curly braces */
2289 name_start = name = arg;
2290 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2291 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293 /* This is mainly to keep test 49 working: when expanding
2294 * curly braces fails overrule the exception error message. */
2295 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 emsg_severe = TRUE;
2298 EMSG2(_(e_invarg2), arg);
2299 break;
2300 }
2301 error = TRUE;
2302 }
2303 else
2304 {
2305 if (tofree != NULL)
2306 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002307 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309 else
2310 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002311 /* handle d.key, l[idx], f(expr) */
2312 arg_subsc = arg;
2313 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002314 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002319 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002321 case 'g': list_glob_vars(first); break;
2322 case 'b': list_buf_vars(first); break;
2323 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002324#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002325 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002326#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002327 case 'v': list_vim_vars(first); break;
2328 case 's': list_script_vars(first); break;
2329 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330 default:
2331 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002332 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 }
2334 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 {
2336 char_u numbuf[NUMBUFLEN];
2337 char_u *tf;
2338 int c;
2339 char_u *s;
2340
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002341 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342 c = *arg;
2343 *arg = NUL;
2344 list_one_var_a((char_u *)"",
2345 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002346 tv.v_type,
2347 s == NULL ? (char_u *)"" : s,
2348 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002349 *arg = c;
2350 vim_free(tf);
2351 }
2352 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002353 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 }
2355 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002356
2357 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002359
2360 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361 }
2362
2363 return arg;
2364}
2365
2366/*
2367 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2368 * Returns a pointer to the char just after the var name.
2369 * Returns NULL if there is an error.
2370 */
2371 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002372ex_let_one(
2373 char_u *arg, /* points to variable name */
2374 typval_T *tv, /* value to assign to variable */
2375 int copy, /* copy value from "tv" */
2376 char_u *endchars, /* valid chars after variable name or NULL */
2377 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378{
2379 int c1;
2380 char_u *name;
2381 char_u *p;
2382 char_u *arg_end = NULL;
2383 int len;
2384 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386
2387 /*
2388 * ":let $VAR = expr": Set environment variable.
2389 */
2390 if (*arg == '$')
2391 {
2392 /* Find the end of the name. */
2393 ++arg;
2394 name = arg;
2395 len = get_env_len(&arg);
2396 if (len == 0)
2397 EMSG2(_(e_invarg2), name - 1);
2398 else
2399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 if (op != NULL && (*op == '+' || *op == '-'))
2401 EMSG2(_(e_letwrong), op);
2402 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002403 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002405 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002406 {
2407 c1 = name[len];
2408 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002409 p = get_tv_string_chk(tv);
2410 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002411 {
2412 int mustfree = FALSE;
2413 char_u *s = vim_getenv(name, &mustfree);
2414
2415 if (s != NULL)
2416 {
2417 p = tofree = concat_str(s, p);
2418 if (mustfree)
2419 vim_free(s);
2420 }
2421 }
2422 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002423 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002424 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 if (STRICMP(name, "HOME") == 0)
2426 init_homedir();
2427 else if (didset_vim && STRICMP(name, "VIM") == 0)
2428 didset_vim = FALSE;
2429 else if (didset_vimruntime
2430 && STRICMP(name, "VIMRUNTIME") == 0)
2431 didset_vimruntime = FALSE;
2432 arg_end = arg;
2433 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002436 }
2437 }
2438 }
2439
2440 /*
2441 * ":let &option = expr": Set option value.
2442 * ":let &l:option = expr": Set local option value.
2443 * ":let &g:option = expr": Set global option value.
2444 */
2445 else if (*arg == '&')
2446 {
2447 /* Find the end of the name. */
2448 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002449 if (p == NULL || (endchars != NULL
2450 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 EMSG(_(e_letunexp));
2452 else
2453 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 long n;
2455 int opt_type;
2456 long numval;
2457 char_u *stringval = NULL;
2458 char_u *s;
2459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 c1 = *p;
2461 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002462
2463 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002464 s = get_tv_string_chk(tv); /* != NULL if number or string */
2465 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 {
2467 opt_type = get_option_value(arg, &numval,
2468 &stringval, opt_flags);
2469 if ((opt_type == 1 && *op == '.')
2470 || (opt_type == 0 && *op != '.'))
2471 EMSG2(_(e_letwrong), op);
2472 else
2473 {
2474 if (opt_type == 1) /* number */
2475 {
2476 if (*op == '+')
2477 n = numval + n;
2478 else
2479 n = numval - n;
2480 }
2481 else if (opt_type == 0 && stringval != NULL) /* string */
2482 {
2483 s = concat_str(stringval, s);
2484 vim_free(stringval);
2485 stringval = s;
2486 }
2487 }
2488 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002489 if (s != NULL)
2490 {
2491 set_option_value(arg, n, s, opt_flags);
2492 arg_end = p;
2493 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002495 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 }
2497 }
2498
2499 /*
2500 * ":let @r = expr": Set register contents.
2501 */
2502 else if (*arg == '@')
2503 {
2504 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 if (op != NULL && (*op == '+' || *op == '-'))
2506 EMSG2(_(e_letwrong), op);
2507 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002508 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 EMSG(_(e_letunexp));
2510 else
2511 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002512 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 char_u *s;
2514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002515 p = get_tv_string_chk(tv);
2516 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002517 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002518 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 if (s != NULL)
2520 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002521 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002522 vim_free(s);
2523 }
2524 }
2525 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002526 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002527 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002528 arg_end = arg + 1;
2529 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002530 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 }
2532 }
2533
2534 /*
2535 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002538 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002540 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002542 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2546 EMSG(_(e_letunexp));
2547 else
2548 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550 arg_end = p;
2551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 }
2555
2556 else
2557 EMSG2(_(e_invarg2), arg);
2558
2559 return arg_end;
2560}
2561
2562/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002563 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2564 */
2565 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002566check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567{
2568 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2569 {
2570 EMSG2(_(e_readonlyvar), arg);
2571 return TRUE;
2572 }
2573 return FALSE;
2574}
2575
2576/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 * Get an lval: variable, Dict item or List item that can be assigned a value
2578 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2579 * "name.key", "name.key[expr]" etc.
2580 * Indexing only works if "name" is an existing List or Dictionary.
2581 * "name" points to the start of the name.
2582 * If "rettv" is not NULL it points to the value to be assigned.
2583 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2584 * wrong; must end in space or cmd separator.
2585 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002586 * flags:
2587 * GLV_QUIET: do not give error messages
2588 * GLV_NO_AUTOLOAD: do not use script autoloading
2589 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002591 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 */
2594 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002595get_lval(
2596 char_u *name,
2597 typval_T *rettv,
2598 lval_T *lp,
2599 int unlet,
2600 int skip,
2601 int flags, /* GLV_ values */
2602 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 char_u *expr_start, *expr_end;
2606 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002607 dictitem_T *v;
2608 typval_T var1;
2609 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002611 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002612 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002614 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002615 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002616
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002618 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619
2620 if (skip)
2621 {
2622 /* When skipping just find the end of the name. */
2623 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002624 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 }
2626
2627 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002628 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002629 if (expr_start != NULL)
2630 {
2631 /* Don't expand the name when we already know there is an error. */
2632 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2633 && *p != '[' && *p != '.')
2634 {
2635 EMSG(_(e_trailing));
2636 return NULL;
2637 }
2638
2639 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2640 if (lp->ll_exp_name == NULL)
2641 {
2642 /* Report an invalid expression in braces, unless the
2643 * expression evaluation has been cancelled due to an
2644 * aborting error, an interrupt, or an exception. */
2645 if (!aborting() && !quiet)
2646 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002647 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 EMSG2(_(e_invarg2), name);
2649 return NULL;
2650 }
2651 }
2652 lp->ll_name = lp->ll_exp_name;
2653 }
2654 else
2655 lp->ll_name = name;
2656
2657 /* Without [idx] or .key we are done. */
2658 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2659 return p;
2660
2661 cc = *p;
2662 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002663 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (v == NULL && !quiet)
2665 EMSG2(_(e_undefvar), lp->ll_name);
2666 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 if (v == NULL)
2668 return NULL;
2669
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 /*
2671 * Loop until no more [idx] or .key is following.
2672 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002673 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2677 && !(lp->ll_tv->v_type == VAR_DICT
2678 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002679 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (!quiet)
2681 EMSG(_("E689: Can only index a List or Dictionary"));
2682 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002683 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
2687 EMSG(_("E708: [:] must come last"));
2688 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002689 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 len = -1;
2692 if (*p == '.')
2693 {
2694 key = p + 1;
2695 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2696 ;
2697 if (len == 0)
2698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
2700 EMSG(_(e_emptykey));
2701 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703 p = key + len;
2704 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705 else
2706 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002708 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 if (*p == ':')
2710 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 else
2712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 empty1 = FALSE;
2714 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 if (get_tv_string_chk(&var1) == NULL)
2717 {
2718 /* not a number or string */
2719 clear_tv(&var1);
2720 return NULL;
2721 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 }
2723
2724 /* Optionally get the second index [ :expr]. */
2725 if (*p == ':')
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002730 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002731 if (!empty1)
2732 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 if (rettv != NULL && (rettv->v_type != VAR_LIST
2736 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (!quiet)
2739 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (!empty1)
2741 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 }
2744 p = skipwhite(p + 1);
2745 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002746 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 else
2748 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 if (!empty1)
2753 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 if (get_tv_string_chk(&var2) == NULL)
2757 {
2758 /* not a number or string */
2759 if (!empty1)
2760 clear_tv(&var1);
2761 clear_tv(&var2);
2762 return NULL;
2763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002766 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002769
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (!quiet)
2773 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 if (!empty1)
2775 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 }
2780
2781 /* Skip to past ']'. */
2782 ++p;
2783 }
2784
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 {
2787 if (len == -1)
2788 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002790 key = get_tv_string_chk(&var1); /* is number or string */
2791 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 }
2796 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002797 lp->ll_list = NULL;
2798 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002799 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 /* When assigning to a scope dictionary check that a function and
2802 * variable name is valid (only variable name unless it is l: or
2803 * g: dictionary). Disallow overwriting a builtin function. */
2804 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002806 int prevval;
2807 int wrong;
2808
2809 if (len != -1)
2810 {
2811 prevval = key[len];
2812 key[len] = NUL;
2813 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002814 else
2815 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002816 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2817 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002819 || !valid_varname(key);
2820 if (len != -1)
2821 key[len] = prevval;
2822 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002823 return NULL;
2824 }
2825
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002828 /* Can't add "v:" variable. */
2829 if (lp->ll_dict == &vimvardict)
2830 {
2831 EMSG2(_(e_illvar), name);
2832 return NULL;
2833 }
2834
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002835 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002839 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 if (len == -1)
2841 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 }
2844 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 if (len == -1)
2849 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 p = NULL;
2852 break;
2853 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002854 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002855 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002856 return NULL;
2857
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 if (len == -1)
2859 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 }
2862 else
2863 {
2864 /*
2865 * Get the number and item for the only or first index of the List.
2866 */
2867 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002869 else
2870 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002871 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002872 clear_tv(&var1);
2873 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002874 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_list = lp->ll_tv->vval.v_list;
2876 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2877 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002879 if (lp->ll_n1 < 0)
2880 {
2881 lp->ll_n1 = 0;
2882 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2883 }
2884 }
2885 if (lp->ll_li == NULL)
2886 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002889 if (!quiet)
2890 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 }
2893
2894 /*
2895 * May need to find the item or absolute index for the second
2896 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 * When no index given: "lp->ll_empty2" is TRUE.
2898 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002899 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002902 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002908 {
2909 if (!quiet)
2910 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002914 }
2915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2917 if (lp->ll_n1 < 0)
2918 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2919 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002920 {
2921 if (!quiet)
2922 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002924 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002925 }
2926
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 }
2930
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 return p;
2932}
2933
2934/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002935 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 */
2937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939{
2940 vim_free(lp->ll_exp_name);
2941 vim_free(lp->ll_newkey);
2942}
2943
2944/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002945 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 */
2949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002950set_var_lval(
2951 lval_T *lp,
2952 char_u *endp,
2953 typval_T *rettv,
2954 int copy,
2955 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956{
2957 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002958 listitem_T *ri;
2959 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960
2961 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002962 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 cc = *endp;
2966 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002967 if (op != NULL && *op != '=')
2968 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002969 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002971 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002972 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002973 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002974 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002975 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002976 if ((di == NULL
2977 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2978 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2979 FALSE)))
2980 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 set_var(lp->ll_name, &tv, FALSE);
2982 clear_tv(&tv);
2983 }
2984 }
2985 else
2986 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002990 else if (tv_check_lock(lp->ll_newkey == NULL
2991 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002992 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002993 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002994 else if (lp->ll_range)
2995 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002996 listitem_T *ll_li = lp->ll_li;
2997 int ll_n1 = lp->ll_n1;
2998
2999 /*
3000 * Check whether any of the list items is locked
3001 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003002 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003003 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003004 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003005 return;
3006 ri = ri->li_next;
3007 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3008 break;
3009 ll_li = ll_li->li_next;
3010 ++ll_n1;
3011 }
3012
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 /*
3014 * Assign the List values to the list items.
3015 */
3016 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003018 if (op != NULL && *op != '=')
3019 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3020 else
3021 {
3022 clear_tv(&lp->ll_li->li_tv);
3023 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3024 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 ri = ri->li_next;
3026 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3027 break;
3028 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003030 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003031 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003032 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 ri = NULL;
3034 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003036 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003037 lp->ll_li = lp->ll_li->li_next;
3038 ++lp->ll_n1;
3039 }
3040 if (ri != NULL)
3041 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 else if (lp->ll_empty2
3043 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 : lp->ll_n1 != lp->ll_n2)
3045 EMSG(_("E711: List value has not enough items"));
3046 }
3047 else
3048 {
3049 /*
3050 * Assign to a List or Dictionary item.
3051 */
3052 if (lp->ll_newkey != NULL)
3053 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003054 if (op != NULL && *op != '=')
3055 {
3056 EMSG2(_(e_letwrong), op);
3057 return;
3058 }
3059
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003061 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 if (di == NULL)
3063 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003064 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3065 {
3066 vim_free(di);
3067 return;
3068 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003069 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003070 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 else if (op != NULL && *op != '=')
3072 {
3073 tv_op(lp->ll_tv, rettv, op);
3074 return;
3075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003078
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003079 /*
3080 * Assign the value to the variable or list item.
3081 */
3082 if (copy)
3083 copy_tv(rettv, lp->ll_tv);
3084 else
3085 {
3086 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003087 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003088 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003089 }
3090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091}
3092
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003093/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3095 * Returns OK or FAIL.
3096 */
3097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003098tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099{
3100 long n;
3101 char_u numbuf[NUMBUFLEN];
3102 char_u *s;
3103
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003104 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3105 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3106 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003107 {
3108 switch (tv1->v_type)
3109 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003110 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003111 case VAR_DICT:
3112 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003113 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003114 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003115 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003116 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003117 break;
3118
3119 case VAR_LIST:
3120 if (*op != '+' || tv2->v_type != VAR_LIST)
3121 break;
3122 /* List += List */
3123 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3124 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3125 return OK;
3126
3127 case VAR_NUMBER:
3128 case VAR_STRING:
3129 if (tv2->v_type == VAR_LIST)
3130 break;
3131 if (*op == '+' || *op == '-')
3132 {
3133 /* nr += nr or nr -= nr*/
3134 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003135#ifdef FEAT_FLOAT
3136 if (tv2->v_type == VAR_FLOAT)
3137 {
3138 float_T f = n;
3139
3140 if (*op == '+')
3141 f += tv2->vval.v_float;
3142 else
3143 f -= tv2->vval.v_float;
3144 clear_tv(tv1);
3145 tv1->v_type = VAR_FLOAT;
3146 tv1->vval.v_float = f;
3147 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003148 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149#endif
3150 {
3151 if (*op == '+')
3152 n += get_tv_number(tv2);
3153 else
3154 n -= get_tv_number(tv2);
3155 clear_tv(tv1);
3156 tv1->v_type = VAR_NUMBER;
3157 tv1->vval.v_number = n;
3158 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003159 }
3160 else
3161 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162 if (tv2->v_type == VAR_FLOAT)
3163 break;
3164
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003165 /* str .= str */
3166 s = get_tv_string(tv1);
3167 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3168 clear_tv(tv1);
3169 tv1->v_type = VAR_STRING;
3170 tv1->vval.v_string = s;
3171 }
3172 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003173
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003174 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003175#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003176 {
3177 float_T f;
3178
3179 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3180 && tv2->v_type != VAR_NUMBER
3181 && tv2->v_type != VAR_STRING))
3182 break;
3183 if (tv2->v_type == VAR_FLOAT)
3184 f = tv2->vval.v_float;
3185 else
3186 f = get_tv_number(tv2);
3187 if (*op == '+')
3188 tv1->vval.v_float += f;
3189 else
3190 tv1->vval.v_float -= f;
3191 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003193 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003194 }
3195 }
3196
3197 EMSG2(_(e_letwrong), op);
3198 return FAIL;
3199}
3200
3201/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 * Add a watcher to a list.
3203 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003205list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206{
3207 lw->lw_next = l->lv_watch;
3208 l->lv_watch = lw;
3209}
3210
3211/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003212 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 * No warning when it isn't found...
3214 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003215 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003216list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217{
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219
3220 lwp = &l->lv_watch;
3221 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3222 {
3223 if (lw == lwrem)
3224 {
3225 *lwp = lw->lw_next;
3226 break;
3227 }
3228 lwp = &lw->lw_next;
3229 }
3230}
3231
3232/*
3233 * Just before removing an item from a list: advance watchers to the next
3234 * item.
3235 */
3236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003237list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003238{
Bram Moolenaar33570922005-01-25 22:26:29 +00003239 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003240
3241 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3242 if (lw->lw_item == item)
3243 lw->lw_item = item->li_next;
3244}
3245
3246/*
3247 * Evaluate the expression used in a ":for var in expr" command.
3248 * "arg" points to "var".
3249 * Set "*errp" to TRUE for an error, FALSE otherwise;
3250 * Return a pointer that holds the info. Null when there is an error.
3251 */
3252 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253eval_for_line(
3254 char_u *arg,
3255 int *errp,
3256 char_u **nextcmdp,
3257 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258{
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003260 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 typval_T tv;
3262 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263
3264 *errp = TRUE; /* default: there is an error */
3265
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 if (fi == NULL)
3268 return NULL;
3269
3270 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3271 if (expr == NULL)
3272 return fi;
3273
3274 expr = skipwhite(expr);
3275 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3276 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003277 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 return fi;
3279 }
3280
3281 if (skip)
3282 ++emsg_skip;
3283 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3284 {
3285 *errp = FALSE;
3286 if (!skip)
3287 {
3288 l = tv.vval.v_list;
3289 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003290 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003291 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003292 clear_tv(&tv);
3293 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 else
3295 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003296 /* No need to increment the refcount, it's already set for the
3297 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 fi->fi_list = l;
3299 list_add_watch(l, &fi->fi_lw);
3300 fi->fi_lw.lw_item = l->lv_first;
3301 }
3302 }
3303 }
3304 if (skip)
3305 --emsg_skip;
3306
3307 return fi;
3308}
3309
3310/*
3311 * Use the first item in a ":for" list. Advance to the next.
3312 * Assign the values to the variable (list). "arg" points to the first one.
3313 * Return TRUE when a valid item was found, FALSE when at end of list or
3314 * something wrong.
3315 */
3316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003319 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003320 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003321 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003322
3323 item = fi->fi_lw.lw_item;
3324 if (item == NULL)
3325 result = FALSE;
3326 else
3327 {
3328 fi->fi_lw.lw_item = item->li_next;
3329 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3330 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3331 }
3332 return result;
3333}
3334
3335/*
3336 * Free the structure used to store info used by ":for".
3337 */
3338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340{
Bram Moolenaar33570922005-01-25 22:26:29 +00003341 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003343 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003344 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003345 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003346 list_unref(fi->fi_list);
3347 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 vim_free(fi);
3349}
3350
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3352
3353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003354set_context_for_expression(
3355 expand_T *xp,
3356 char_u *arg,
3357 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 int got_eq = FALSE;
3360 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363 if (cmdidx == CMD_let)
3364 {
3365 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003366 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003367 {
3368 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003369 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003370 {
3371 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003372 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003373 if (vim_iswhite(*p))
3374 break;
3375 }
3376 return;
3377 }
3378 }
3379 else
3380 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3381 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 while ((xp->xp_pattern = vim_strpbrk(arg,
3383 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3384 {
3385 c = *xp->xp_pattern;
3386 if (c == '&')
3387 {
3388 c = xp->xp_pattern[1];
3389 if (c == '&')
3390 {
3391 ++xp->xp_pattern;
3392 xp->xp_context = cmdidx != CMD_let || got_eq
3393 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3394 }
3395 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003398 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3399 xp->xp_pattern += 2;
3400
3401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 }
3403 else if (c == '$')
3404 {
3405 /* environment variable */
3406 xp->xp_context = EXPAND_ENV_VARS;
3407 }
3408 else if (c == '=')
3409 {
3410 got_eq = TRUE;
3411 xp->xp_context = EXPAND_EXPRESSION;
3412 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003413 else if (c == '#'
3414 && xp->xp_context == EXPAND_EXPRESSION)
3415 {
3416 /* Autoload function/variable contains '#'. */
3417 break;
3418 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003419 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 && xp->xp_context == EXPAND_FUNCTIONS
3421 && vim_strchr(xp->xp_pattern, '(') == NULL)
3422 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003423 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 break;
3425 }
3426 else if (cmdidx != CMD_let || got_eq)
3427 {
3428 if (c == '"') /* string */
3429 {
3430 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3431 if (c == '\\' && xp->xp_pattern[1] != NUL)
3432 ++xp->xp_pattern;
3433 xp->xp_context = EXPAND_NOTHING;
3434 }
3435 else if (c == '\'') /* literal string */
3436 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003437 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3439 /* skip */ ;
3440 xp->xp_context = EXPAND_NOTHING;
3441 }
3442 else if (c == '|')
3443 {
3444 if (xp->xp_pattern[1] == '|')
3445 {
3446 ++xp->xp_pattern;
3447 xp->xp_context = EXPAND_EXPRESSION;
3448 }
3449 else
3450 xp->xp_context = EXPAND_COMMANDS;
3451 }
3452 else
3453 xp->xp_context = EXPAND_EXPRESSION;
3454 }
3455 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003456 /* Doesn't look like something valid, expand as an expression
3457 * anyway. */
3458 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 arg = xp->xp_pattern;
3460 if (*arg != NUL)
3461 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3462 /* skip */ ;
3463 }
3464 xp->xp_pattern = arg;
3465}
3466
3467#endif /* FEAT_CMDL_COMPL */
3468
3469/*
3470 * ":1,25call func(arg1, arg2)" function call.
3471 */
3472 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003473ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
3475 char_u *arg = eap->arg;
3476 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003478 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003480 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 linenr_T lnum;
3482 int doesrange;
3483 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003484 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003485 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003487 if (eap->skip)
3488 {
3489 /* trans_function_name() doesn't work well when skipping, use eval0()
3490 * instead to skip to any following command, e.g. for:
3491 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003492 ++emsg_skip;
3493 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3494 clear_tv(&rettv);
3495 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003496 return;
3497 }
3498
Bram Moolenaar65639032016-03-16 21:40:30 +01003499 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003500 if (fudi.fd_newkey != NULL)
3501 {
3502 /* Still need to give an error message for missing key. */
3503 EMSG2(_(e_dictkey), fudi.fd_newkey);
3504 vim_free(fudi.fd_newkey);
3505 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003506 if (tofree == NULL)
3507 return;
3508
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003509 /* Increase refcount on dictionary, it could get deleted when evaluating
3510 * the arguments. */
3511 if (fudi.fd_dict != NULL)
3512 ++fudi.fd_dict->dv_refcount;
3513
Bram Moolenaar65639032016-03-16 21:40:30 +01003514 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3515 * contents. For VAR_PARTIAL get its partial, unless we already have one
3516 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003517 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003518 name = deref_func_name(tofree, &len,
3519 partial != NULL ? NULL : &partial, FALSE);
3520
Bram Moolenaar532c7802005-01-27 14:44:31 +00003521 /* Skip white space to allow ":call func ()". Not good, but required for
3522 * backward compatibility. */
3523 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003524 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 if (*startarg != '(')
3527 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003528 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 goto end;
3530 }
3531
3532 /*
3533 * When skipping, evaluate the function once, to find the end of the
3534 * arguments.
3535 * When the function takes a range, this is discovered after the first
3536 * call, and the loop is broken.
3537 */
3538 if (eap->skip)
3539 {
3540 ++emsg_skip;
3541 lnum = eap->line2; /* do it once, also with an invalid range */
3542 }
3543 else
3544 lnum = eap->line1;
3545 for ( ; lnum <= eap->line2; ++lnum)
3546 {
3547 if (!eap->skip && eap->addr_count > 0)
3548 {
3549 curwin->w_cursor.lnum = lnum;
3550 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003551#ifdef FEAT_VIRTUALEDIT
3552 curwin->w_cursor.coladd = 0;
3553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003556 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003557 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003558 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
3560 failed = TRUE;
3561 break;
3562 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003563
3564 /* Handle a function returning a Funcref, Dictionary or List. */
3565 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3566 {
3567 failed = TRUE;
3568 break;
3569 }
3570
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003571 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (doesrange || eap->skip)
3573 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003576 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003577 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003578 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (aborting())
3580 break;
3581 }
3582 if (eap->skip)
3583 --emsg_skip;
3584
3585 if (!failed)
3586 {
3587 /* Check for trailing illegal characters and a following command. */
3588 if (!ends_excmd(*arg))
3589 {
3590 emsg_severe = TRUE;
3591 EMSG(_(e_trailing));
3592 }
3593 else
3594 eap->nextcmd = check_nextcmd(arg);
3595 }
3596
3597end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003598 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003599 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600}
3601
3602/*
3603 * ":unlet[!] var1 ... " command.
3604 */
3605 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003606ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003608 ex_unletlock(eap, eap->arg, 0);
3609}
3610
3611/*
3612 * ":lockvar" and ":unlockvar" commands
3613 */
3614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003615ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003618 int deep = 2;
3619
3620 if (eap->forceit)
3621 deep = -1;
3622 else if (vim_isdigit(*arg))
3623 {
3624 deep = getdigits(&arg);
3625 arg = skipwhite(arg);
3626 }
3627
3628 ex_unletlock(eap, arg, deep);
3629}
3630
3631/*
3632 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3633 */
3634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635ex_unletlock(
3636 exarg_T *eap,
3637 char_u *argstart,
3638 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639{
3640 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
3645 do
3646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003647 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003648 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003649 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 if (lv.ll_name == NULL)
3651 error = TRUE; /* error but continue parsing */
3652 if (name_end == NULL || (!vim_iswhite(*name_end)
3653 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003655 if (name_end != NULL)
3656 {
3657 emsg_severe = TRUE;
3658 EMSG(_(e_trailing));
3659 }
3660 if (!(eap->skip || error))
3661 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 break;
3663 }
3664
3665 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003666 {
3667 if (eap->cmdidx == CMD_unlet)
3668 {
3669 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3670 error = TRUE;
3671 }
3672 else
3673 {
3674 if (do_lock_var(&lv, name_end, deep,
3675 eap->cmdidx == CMD_lockvar) == FAIL)
3676 error = TRUE;
3677 }
3678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 if (!eap->skip)
3681 clear_lval(&lv);
3682
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 arg = skipwhite(name_end);
3684 } while (!ends_excmd(*arg));
3685
3686 eap->nextcmd = check_nextcmd(arg);
3687}
3688
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003690do_unlet_var(
3691 lval_T *lp,
3692 char_u *name_end,
3693 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 int ret = OK;
3696 int cc;
3697
3698 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003699 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 cc = *name_end;
3701 *name_end = NUL;
3702
3703 /* Normal name or expanded name. */
3704 if (check_changedtick(lp->ll_name))
3705 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003706 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003707 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003710 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003711 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003712 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003713 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003714 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 else if (lp->ll_range)
3716 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003718 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003719 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003720
3721 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3722 {
3723 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003724 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003725 return FAIL;
3726 ll_li = li;
3727 ++ll_n1;
3728 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729
3730 /* Delete a range of List items. */
3731 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3732 {
3733 li = lp->ll_li->li_next;
3734 listitem_remove(lp->ll_list, lp->ll_li);
3735 lp->ll_li = li;
3736 ++lp->ll_n1;
3737 }
3738 }
3739 else
3740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003741 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003742 /* unlet a List item. */
3743 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003744 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003745 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003746 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003747 }
3748
3749 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003750}
3751
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752/*
3753 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003754 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 */
3756 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003757do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
Bram Moolenaar33570922005-01-25 22:26:29 +00003759 hashtab_T *ht;
3760 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003761 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003762 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003763 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 ht = find_var_ht(name, &varname);
3766 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003768 if (ht == &globvarht)
3769 d = &globvardict;
3770 else if (current_funccal != NULL
3771 && ht == &current_funccal->l_vars.dv_hashtab)
3772 d = &current_funccal->l_vars;
3773 else if (ht == &compat_hashtab)
3774 d = &vimvardict;
3775 else
3776 {
3777 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3778 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3779 }
3780 if (d == NULL)
3781 {
3782 EMSG2(_(e_intern2), "do_unlet()");
3783 return FAIL;
3784 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 hi = hash_find(ht, varname);
3786 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003787 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003788 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003789 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003790 || var_check_ro(di->di_flags, name, FALSE)
3791 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003792 return FAIL;
3793
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003794 delete_var(ht, hi);
3795 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003798 if (forceit)
3799 return OK;
3800 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802}
3803
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003804/*
3805 * Lock or unlock variable indicated by "lp".
3806 * "deep" is the levels to go (-1 for unlimited);
3807 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3808 */
3809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003810do_lock_var(
3811 lval_T *lp,
3812 char_u *name_end,
3813 int deep,
3814 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815{
3816 int ret = OK;
3817 int cc;
3818 dictitem_T *di;
3819
3820 if (deep == 0) /* nothing to do */
3821 return OK;
3822
3823 if (lp->ll_tv == NULL)
3824 {
3825 cc = *name_end;
3826 *name_end = NUL;
3827
3828 /* Normal name or expanded name. */
3829 if (check_changedtick(lp->ll_name))
3830 ret = FAIL;
3831 else
3832 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003833 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003834 if (di == NULL)
3835 ret = FAIL;
3836 else
3837 {
3838 if (lock)
3839 di->di_flags |= DI_FLAGS_LOCK;
3840 else
3841 di->di_flags &= ~DI_FLAGS_LOCK;
3842 item_lock(&di->di_tv, deep, lock);
3843 }
3844 }
3845 *name_end = cc;
3846 }
3847 else if (lp->ll_range)
3848 {
3849 listitem_T *li = lp->ll_li;
3850
3851 /* (un)lock a range of List items. */
3852 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3853 {
3854 item_lock(&li->li_tv, deep, lock);
3855 li = li->li_next;
3856 ++lp->ll_n1;
3857 }
3858 }
3859 else if (lp->ll_list != NULL)
3860 /* (un)lock a List item. */
3861 item_lock(&lp->ll_li->li_tv, deep, lock);
3862 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003863 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003864 item_lock(&lp->ll_di->di_tv, deep, lock);
3865
3866 return ret;
3867}
3868
3869/*
3870 * Lock or unlock an item. "deep" is nr of levels to go.
3871 */
3872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003873item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003874{
3875 static int recurse = 0;
3876 list_T *l;
3877 listitem_T *li;
3878 dict_T *d;
3879 hashitem_T *hi;
3880 int todo;
3881
3882 if (recurse >= DICT_MAXNEST)
3883 {
3884 EMSG(_("E743: variable nested too deep for (un)lock"));
3885 return;
3886 }
3887 if (deep == 0)
3888 return;
3889 ++recurse;
3890
3891 /* lock/unlock the item itself */
3892 if (lock)
3893 tv->v_lock |= VAR_LOCKED;
3894 else
3895 tv->v_lock &= ~VAR_LOCKED;
3896
3897 switch (tv->v_type)
3898 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003899 case VAR_UNKNOWN:
3900 case VAR_NUMBER:
3901 case VAR_STRING:
3902 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003903 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003904 case VAR_FLOAT:
3905 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003906 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003907 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003908 break;
3909
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003910 case VAR_LIST:
3911 if ((l = tv->vval.v_list) != NULL)
3912 {
3913 if (lock)
3914 l->lv_lock |= VAR_LOCKED;
3915 else
3916 l->lv_lock &= ~VAR_LOCKED;
3917 if (deep < 0 || deep > 1)
3918 /* recursive: lock/unlock the items the List contains */
3919 for (li = l->lv_first; li != NULL; li = li->li_next)
3920 item_lock(&li->li_tv, deep - 1, lock);
3921 }
3922 break;
3923 case VAR_DICT:
3924 if ((d = tv->vval.v_dict) != NULL)
3925 {
3926 if (lock)
3927 d->dv_lock |= VAR_LOCKED;
3928 else
3929 d->dv_lock &= ~VAR_LOCKED;
3930 if (deep < 0 || deep > 1)
3931 {
3932 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003933 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003934 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3935 {
3936 if (!HASHITEM_EMPTY(hi))
3937 {
3938 --todo;
3939 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3940 }
3941 }
3942 }
3943 }
3944 }
3945 --recurse;
3946}
3947
Bram Moolenaara40058a2005-07-11 22:42:07 +00003948/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003949 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3950 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003951 */
3952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003953tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003954{
3955 return (tv->v_lock & VAR_LOCKED)
3956 || (tv->v_type == VAR_LIST
3957 && tv->vval.v_list != NULL
3958 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3959 || (tv->v_type == VAR_DICT
3960 && tv->vval.v_dict != NULL
3961 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3962}
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3965/*
3966 * Delete all "menutrans_" variables.
3967 */
3968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003969del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970{
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003972 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003975 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003976 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003977 {
3978 if (!HASHITEM_EMPTY(hi))
3979 {
3980 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003981 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3982 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 }
3984 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986}
3987#endif
3988
3989#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3990
3991/*
3992 * Local string buffer for the next two functions to store a variable name
3993 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3994 * get_user_var_name().
3995 */
3996
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003997static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998
3999static char_u *varnamebuf = NULL;
4000static int varnamebuflen = 0;
4001
4002/*
4003 * Function to concatenate a prefix and a variable name.
4004 */
4005 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004006cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007{
4008 int len;
4009
4010 len = (int)STRLEN(name) + 3;
4011 if (len > varnamebuflen)
4012 {
4013 vim_free(varnamebuf);
4014 len += 10; /* some additional space */
4015 varnamebuf = alloc(len);
4016 if (varnamebuf == NULL)
4017 {
4018 varnamebuflen = 0;
4019 return NULL;
4020 }
4021 varnamebuflen = len;
4022 }
4023 *varnamebuf = prefix;
4024 varnamebuf[1] = ':';
4025 STRCPY(varnamebuf + 2, name);
4026 return varnamebuf;
4027}
4028
4029/*
4030 * Function given to ExpandGeneric() to obtain the list of user defined
4031 * (global/buffer/window/built-in) variable names.
4032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004034get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004036 static long_u gdone;
4037 static long_u bdone;
4038 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004039#ifdef FEAT_WINDOWS
4040 static long_u tdone;
4041#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004042 static int vidx;
4043 static hashitem_T *hi;
4044 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
4046 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004047 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004048 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004049#ifdef FEAT_WINDOWS
4050 tdone = 0;
4051#endif
4052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004053
4054 /* Global variables */
4055 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004057 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004059 else
4060 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 while (HASHITEM_EMPTY(hi))
4062 ++hi;
4063 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4064 return cat_prefix_varname('g', hi->hi_key);
4065 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004067
4068 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004069 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004072 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004074 else
4075 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004076 while (HASHITEM_EMPTY(hi))
4077 ++hi;
4078 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004080 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004082 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 return (char_u *)"b:changedtick";
4084 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004085
4086 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004087 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004088 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004090 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004092 else
4093 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004094 while (HASHITEM_EMPTY(hi))
4095 ++hi;
4096 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004098
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004099#ifdef FEAT_WINDOWS
4100 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004101 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004102 if (tdone < ht->ht_used)
4103 {
4104 if (tdone++ == 0)
4105 hi = ht->ht_array;
4106 else
4107 ++hi;
4108 while (HASHITEM_EMPTY(hi))
4109 ++hi;
4110 return cat_prefix_varname('t', hi->hi_key);
4111 }
4112#endif
4113
Bram Moolenaar33570922005-01-25 22:26:29 +00004114 /* v: variables */
4115 if (vidx < VV_LEN)
4116 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
4118 vim_free(varnamebuf);
4119 varnamebuf = NULL;
4120 varnamebuflen = 0;
4121 return NULL;
4122}
4123
4124#endif /* FEAT_CMDL_COMPL */
4125
4126/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004127 * Return TRUE if "pat" matches "text".
4128 * Does not use 'cpo' and always uses 'magic'.
4129 */
4130 static int
4131pattern_match(char_u *pat, char_u *text, int ic)
4132{
4133 int matches = FALSE;
4134 char_u *save_cpo;
4135 regmatch_T regmatch;
4136
4137 /* avoid 'l' flag in 'cpoptions' */
4138 save_cpo = p_cpo;
4139 p_cpo = (char_u *)"";
4140 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4141 if (regmatch.regprog != NULL)
4142 {
4143 regmatch.rm_ic = ic;
4144 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4145 vim_regfree(regmatch.regprog);
4146 }
4147 p_cpo = save_cpo;
4148 return matches;
4149}
4150
4151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 * types for expressions.
4153 */
4154typedef enum
4155{
4156 TYPE_UNKNOWN = 0
4157 , TYPE_EQUAL /* == */
4158 , TYPE_NEQUAL /* != */
4159 , TYPE_GREATER /* > */
4160 , TYPE_GEQUAL /* >= */
4161 , TYPE_SMALLER /* < */
4162 , TYPE_SEQUAL /* <= */
4163 , TYPE_MATCH /* =~ */
4164 , TYPE_NOMATCH /* !~ */
4165} exptype_T;
4166
4167/*
4168 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4171 */
4172
4173/*
4174 * Handle zero level expression.
4175 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004177 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 * Return OK or FAIL.
4179 */
4180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004181eval0(
4182 char_u *arg,
4183 typval_T *rettv,
4184 char_u **nextcmd,
4185 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186{
4187 int ret;
4188 char_u *p;
4189
4190 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 if (ret == FAIL || !ends_excmd(*p))
4193 {
4194 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 /*
4197 * Report the invalid expression unless the expression evaluation has
4198 * been cancelled due to an aborting error, an interrupt, or an
4199 * exception.
4200 */
4201 if (!aborting())
4202 EMSG2(_(e_invexpr2), arg);
4203 ret = FAIL;
4204 }
4205 if (nextcmd != NULL)
4206 *nextcmd = check_nextcmd(p);
4207
4208 return ret;
4209}
4210
4211/*
4212 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004213 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 *
4215 * "arg" must point to the first non-white of the expression.
4216 * "arg" is advanced to the next non-white after the recognized expression.
4217 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004218 * Note: "rettv.v_lock" is not set.
4219 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 * Return OK or FAIL.
4221 */
4222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004223eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
4225 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 /*
4229 * Get the first variable.
4230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return FAIL;
4233
4234 if ((*arg)[0] == '?')
4235 {
4236 result = FALSE;
4237 if (evaluate)
4238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 int error = FALSE;
4240
4241 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (error)
4245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247
4248 /*
4249 * Get the second variable.
4250 */
4251 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 /*
4256 * Check for the ":".
4257 */
4258 if ((*arg)[0] != ':')
4259 {
4260 EMSG(_("E109: Missing ':' after '?'"));
4261 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 return FAIL;
4264 }
4265
4266 /*
4267 * Get the third variable.
4268 */
4269 *arg = skipwhite(*arg + 1);
4270 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4271 {
4272 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 return FAIL;
4275 }
4276 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279
4280 return OK;
4281}
4282
4283/*
4284 * Handle first level expression:
4285 * expr2 || expr2 || expr2 logical OR
4286 *
4287 * "arg" must point to the first non-white of the expression.
4288 * "arg" is advanced to the next non-white after the recognized expression.
4289 *
4290 * Return OK or FAIL.
4291 */
4292 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004293eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294{
Bram Moolenaar33570922005-01-25 22:26:29 +00004295 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 long result;
4297 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299
4300 /*
4301 * Get the first variable.
4302 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 return FAIL;
4305
4306 /*
4307 * Repeat until there is no following "||".
4308 */
4309 first = TRUE;
4310 result = FALSE;
4311 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4312 {
4313 if (evaluate && first)
4314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004315 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004317 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 if (error)
4319 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 first = FALSE;
4321 }
4322
4323 /*
4324 * Get the second variable.
4325 */
4326 *arg = skipwhite(*arg + 2);
4327 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4328 return FAIL;
4329
4330 /*
4331 * Compute the result.
4332 */
4333 if (evaluate && !result)
4334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (error)
4339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 if (evaluate)
4342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 rettv->v_type = VAR_NUMBER;
4344 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346 }
4347
4348 return OK;
4349}
4350
4351/*
4352 * Handle second level expression:
4353 * expr3 && expr3 && expr3 logical AND
4354 *
4355 * "arg" must point to the first non-white of the expression.
4356 * "arg" is advanced to the next non-white after the recognized expression.
4357 *
4358 * Return OK or FAIL.
4359 */
4360 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004361eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362{
Bram Moolenaar33570922005-01-25 22:26:29 +00004363 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 long result;
4365 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004366 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367
4368 /*
4369 * Get the first variable.
4370 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 return FAIL;
4373
4374 /*
4375 * Repeat until there is no following "&&".
4376 */
4377 first = TRUE;
4378 result = TRUE;
4379 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4380 {
4381 if (evaluate && first)
4382 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004383 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004385 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 if (error)
4387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 first = FALSE;
4389 }
4390
4391 /*
4392 * Get the second variable.
4393 */
4394 *arg = skipwhite(*arg + 2);
4395 if (eval4(arg, &var2, evaluate && result) == FAIL)
4396 return FAIL;
4397
4398 /*
4399 * Compute the result.
4400 */
4401 if (evaluate && result)
4402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (error)
4407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409 if (evaluate)
4410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411 rettv->v_type = VAR_NUMBER;
4412 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 }
4415
4416 return OK;
4417}
4418
4419/*
4420 * Handle third level expression:
4421 * var1 == var2
4422 * var1 =~ var2
4423 * var1 != var2
4424 * var1 !~ var2
4425 * var1 > var2
4426 * var1 >= var2
4427 * var1 < var2
4428 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004429 * var1 is var2
4430 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 *
4432 * "arg" must point to the first non-white of the expression.
4433 * "arg" is advanced to the next non-white after the recognized expression.
4434 *
4435 * Return OK or FAIL.
4436 */
4437 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004438eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439{
Bram Moolenaar33570922005-01-25 22:26:29 +00004440 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 char_u *p;
4442 int i;
4443 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004444 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 int len = 2;
4446 long n1, n2;
4447 char_u *s1, *s2;
4448 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450
4451 /*
4452 * Get the first variable.
4453 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004454 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 return FAIL;
4456
4457 p = *arg;
4458 switch (p[0])
4459 {
4460 case '=': if (p[1] == '=')
4461 type = TYPE_EQUAL;
4462 else if (p[1] == '~')
4463 type = TYPE_MATCH;
4464 break;
4465 case '!': if (p[1] == '=')
4466 type = TYPE_NEQUAL;
4467 else if (p[1] == '~')
4468 type = TYPE_NOMATCH;
4469 break;
4470 case '>': if (p[1] != '=')
4471 {
4472 type = TYPE_GREATER;
4473 len = 1;
4474 }
4475 else
4476 type = TYPE_GEQUAL;
4477 break;
4478 case '<': if (p[1] != '=')
4479 {
4480 type = TYPE_SMALLER;
4481 len = 1;
4482 }
4483 else
4484 type = TYPE_SEQUAL;
4485 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 case 'i': if (p[1] == 's')
4487 {
4488 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4489 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004490 i = p[len];
4491 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004492 {
4493 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4494 type_is = TRUE;
4495 }
4496 }
4497 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499
4500 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004501 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 */
4503 if (type != TYPE_UNKNOWN)
4504 {
4505 /* extra question mark appended: ignore case */
4506 if (p[len] == '?')
4507 {
4508 ic = TRUE;
4509 ++len;
4510 }
4511 /* extra '#' appended: match case */
4512 else if (p[len] == '#')
4513 {
4514 ic = FALSE;
4515 ++len;
4516 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004517 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 else
4519 ic = p_ic;
4520
4521 /*
4522 * Get the second variable.
4523 */
4524 *arg = skipwhite(p + len);
4525 if (eval5(arg, &var2, evaluate) == FAIL)
4526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004527 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 return FAIL;
4529 }
4530
4531 if (evaluate)
4532 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004533 if (type_is && rettv->v_type != var2.v_type)
4534 {
4535 /* For "is" a different type always means FALSE, for "notis"
4536 * it means TRUE. */
4537 n1 = (type == TYPE_NEQUAL);
4538 }
4539 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4540 {
4541 if (type_is)
4542 {
4543 n1 = (rettv->v_type == var2.v_type
4544 && rettv->vval.v_list == var2.vval.v_list);
4545 if (type == TYPE_NEQUAL)
4546 n1 = !n1;
4547 }
4548 else if (rettv->v_type != var2.v_type
4549 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4550 {
4551 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004552 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004554 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 clear_tv(rettv);
4556 clear_tv(&var2);
4557 return FAIL;
4558 }
4559 else
4560 {
4561 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004562 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4563 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004564 if (type == TYPE_NEQUAL)
4565 n1 = !n1;
4566 }
4567 }
4568
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004569 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4570 {
4571 if (type_is)
4572 {
4573 n1 = (rettv->v_type == var2.v_type
4574 && rettv->vval.v_dict == var2.vval.v_dict);
4575 if (type == TYPE_NEQUAL)
4576 n1 = !n1;
4577 }
4578 else if (rettv->v_type != var2.v_type
4579 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4580 {
4581 if (rettv->v_type != var2.v_type)
4582 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4583 else
4584 EMSG(_("E736: Invalid operation for Dictionary"));
4585 clear_tv(rettv);
4586 clear_tv(&var2);
4587 return FAIL;
4588 }
4589 else
4590 {
4591 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004592 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4593 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004594 if (type == TYPE_NEQUAL)
4595 n1 = !n1;
4596 }
4597 }
4598
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004599 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4600 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004601 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004602 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004603 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004604 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004605 clear_tv(rettv);
4606 clear_tv(&var2);
4607 return FAIL;
4608 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004609 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004610 if (type == TYPE_NEQUAL)
4611 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004612 }
4613
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004614#ifdef FEAT_FLOAT
4615 /*
4616 * If one of the two variables is a float, compare as a float.
4617 * When using "=~" or "!~", always compare as string.
4618 */
4619 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4620 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4621 {
4622 float_T f1, f2;
4623
4624 if (rettv->v_type == VAR_FLOAT)
4625 f1 = rettv->vval.v_float;
4626 else
4627 f1 = get_tv_number(rettv);
4628 if (var2.v_type == VAR_FLOAT)
4629 f2 = var2.vval.v_float;
4630 else
4631 f2 = get_tv_number(&var2);
4632 n1 = FALSE;
4633 switch (type)
4634 {
4635 case TYPE_EQUAL: n1 = (f1 == f2); break;
4636 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4637 case TYPE_GREATER: n1 = (f1 > f2); break;
4638 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4639 case TYPE_SMALLER: n1 = (f1 < f2); break;
4640 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4641 case TYPE_UNKNOWN:
4642 case TYPE_MATCH:
4643 case TYPE_NOMATCH: break; /* avoid gcc warning */
4644 }
4645 }
4646#endif
4647
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 /*
4649 * If one of the two variables is a number, compare as a number.
4650 * When using "=~" or "!~", always compare as string.
4651 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004652 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4654 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004655 n1 = get_tv_number(rettv);
4656 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 switch (type)
4658 {
4659 case TYPE_EQUAL: n1 = (n1 == n2); break;
4660 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4661 case TYPE_GREATER: n1 = (n1 > n2); break;
4662 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4663 case TYPE_SMALLER: n1 = (n1 < n2); break;
4664 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4665 case TYPE_UNKNOWN:
4666 case TYPE_MATCH:
4667 case TYPE_NOMATCH: break; /* avoid gcc warning */
4668 }
4669 }
4670 else
4671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004672 s1 = get_tv_string_buf(rettv, buf1);
4673 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4675 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4676 else
4677 i = 0;
4678 n1 = FALSE;
4679 switch (type)
4680 {
4681 case TYPE_EQUAL: n1 = (i == 0); break;
4682 case TYPE_NEQUAL: n1 = (i != 0); break;
4683 case TYPE_GREATER: n1 = (i > 0); break;
4684 case TYPE_GEQUAL: n1 = (i >= 0); break;
4685 case TYPE_SMALLER: n1 = (i < 0); break;
4686 case TYPE_SEQUAL: n1 = (i <= 0); break;
4687
4688 case TYPE_MATCH:
4689 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004690 n1 = pattern_match(s2, s1, ic);
4691 if (type == TYPE_NOMATCH)
4692 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 break;
4694
4695 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4696 }
4697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 clear_tv(rettv);
4699 clear_tv(&var2);
4700 rettv->v_type = VAR_NUMBER;
4701 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 }
4704
4705 return OK;
4706}
4707
4708/*
4709 * Handle fourth level expression:
4710 * + number addition
4711 * - number subtraction
4712 * . string concatenation
4713 *
4714 * "arg" must point to the first non-white of the expression.
4715 * "arg" is advanced to the next non-white after the recognized expression.
4716 *
4717 * Return OK or FAIL.
4718 */
4719 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004720eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
Bram Moolenaar33570922005-01-25 22:26:29 +00004722 typval_T var2;
4723 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 int op;
4725 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004726#ifdef FEAT_FLOAT
4727 float_T f1 = 0, f2 = 0;
4728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 char_u *s1, *s2;
4730 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4731 char_u *p;
4732
4733 /*
4734 * Get the first variable.
4735 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004736 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 return FAIL;
4738
4739 /*
4740 * Repeat computing, until no '+', '-' or '.' is following.
4741 */
4742 for (;;)
4743 {
4744 op = **arg;
4745 if (op != '+' && op != '-' && op != '.')
4746 break;
4747
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004748 if ((op != '+' || rettv->v_type != VAR_LIST)
4749#ifdef FEAT_FLOAT
4750 && (op == '.' || rettv->v_type != VAR_FLOAT)
4751#endif
4752 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004753 {
4754 /* For "list + ...", an illegal use of the first operand as
4755 * a number cannot be determined before evaluating the 2nd
4756 * operand: if this is also a list, all is ok.
4757 * For "something . ...", "something - ..." or "non-list + ...",
4758 * we know that the first operand needs to be a string or number
4759 * without evaluating the 2nd operand. So check before to avoid
4760 * side effects after an error. */
4761 if (evaluate && get_tv_string_chk(rettv) == NULL)
4762 {
4763 clear_tv(rettv);
4764 return FAIL;
4765 }
4766 }
4767
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 /*
4769 * Get the second variable.
4770 */
4771 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004772 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004774 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 return FAIL;
4776 }
4777
4778 if (evaluate)
4779 {
4780 /*
4781 * Compute the result.
4782 */
4783 if (op == '.')
4784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004785 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4786 s2 = get_tv_string_buf_chk(&var2, buf2);
4787 if (s2 == NULL) /* type error ? */
4788 {
4789 clear_tv(rettv);
4790 clear_tv(&var2);
4791 return FAIL;
4792 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004793 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 clear_tv(rettv);
4795 rettv->v_type = VAR_STRING;
4796 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004798 else if (op == '+' && rettv->v_type == VAR_LIST
4799 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004800 {
4801 /* concatenate Lists */
4802 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4803 &var3) == FAIL)
4804 {
4805 clear_tv(rettv);
4806 clear_tv(&var2);
4807 return FAIL;
4808 }
4809 clear_tv(rettv);
4810 *rettv = var3;
4811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 else
4813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004814 int error = FALSE;
4815
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004816#ifdef FEAT_FLOAT
4817 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819 f1 = rettv->vval.v_float;
4820 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004821 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822 else
4823#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004824 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004825 n1 = get_tv_number_chk(rettv, &error);
4826 if (error)
4827 {
4828 /* This can only happen for "list + non-list". For
4829 * "non-list + ..." or "something - ...", we returned
4830 * before evaluating the 2nd operand. */
4831 clear_tv(rettv);
4832 return FAIL;
4833 }
4834#ifdef FEAT_FLOAT
4835 if (var2.v_type == VAR_FLOAT)
4836 f1 = n1;
4837#endif
4838 }
4839#ifdef FEAT_FLOAT
4840 if (var2.v_type == VAR_FLOAT)
4841 {
4842 f2 = var2.vval.v_float;
4843 n2 = 0;
4844 }
4845 else
4846#endif
4847 {
4848 n2 = get_tv_number_chk(&var2, &error);
4849 if (error)
4850 {
4851 clear_tv(rettv);
4852 clear_tv(&var2);
4853 return FAIL;
4854 }
4855#ifdef FEAT_FLOAT
4856 if (rettv->v_type == VAR_FLOAT)
4857 f2 = n2;
4858#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004861
4862#ifdef FEAT_FLOAT
4863 /* If there is a float on either side the result is a float. */
4864 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4865 {
4866 if (op == '+')
4867 f1 = f1 + f2;
4868 else
4869 f1 = f1 - f2;
4870 rettv->v_type = VAR_FLOAT;
4871 rettv->vval.v_float = f1;
4872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#endif
4875 {
4876 if (op == '+')
4877 n1 = n1 + n2;
4878 else
4879 n1 = n1 - n2;
4880 rettv->v_type = VAR_NUMBER;
4881 rettv->vval.v_number = n1;
4882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004884 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886 }
4887 return OK;
4888}
4889
4890/*
4891 * Handle fifth level expression:
4892 * * number multiplication
4893 * / number division
4894 * % number modulo
4895 *
4896 * "arg" must point to the first non-white of the expression.
4897 * "arg" is advanced to the next non-white after the recognized expression.
4898 *
4899 * Return OK or FAIL.
4900 */
4901 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004902eval6(
4903 char_u **arg,
4904 typval_T *rettv,
4905 int evaluate,
4906 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907{
Bram Moolenaar33570922005-01-25 22:26:29 +00004908 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 int op;
4910 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911#ifdef FEAT_FLOAT
4912 int use_float = FALSE;
4913 float_T f1 = 0, f2;
4914#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 /*
4918 * Get the first variable.
4919 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004920 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 return FAIL;
4922
4923 /*
4924 * Repeat computing, until no '*', '/' or '%' is following.
4925 */
4926 for (;;)
4927 {
4928 op = **arg;
4929 if (op != '*' && op != '/' && op != '%')
4930 break;
4931
4932 if (evaluate)
4933 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004934#ifdef FEAT_FLOAT
4935 if (rettv->v_type == VAR_FLOAT)
4936 {
4937 f1 = rettv->vval.v_float;
4938 use_float = TRUE;
4939 n1 = 0;
4940 }
4941 else
4942#endif
4943 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004944 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004945 if (error)
4946 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
4948 else
4949 n1 = 0;
4950
4951 /*
4952 * Get the second variable.
4953 */
4954 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004955 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 return FAIL;
4957
4958 if (evaluate)
4959 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004960#ifdef FEAT_FLOAT
4961 if (var2.v_type == VAR_FLOAT)
4962 {
4963 if (!use_float)
4964 {
4965 f1 = n1;
4966 use_float = TRUE;
4967 }
4968 f2 = var2.vval.v_float;
4969 n2 = 0;
4970 }
4971 else
4972#endif
4973 {
4974 n2 = get_tv_number_chk(&var2, &error);
4975 clear_tv(&var2);
4976 if (error)
4977 return FAIL;
4978#ifdef FEAT_FLOAT
4979 if (use_float)
4980 f2 = n2;
4981#endif
4982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
4984 /*
4985 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004988#ifdef FEAT_FLOAT
4989 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 if (op == '*')
4992 f1 = f1 * f2;
4993 else if (op == '/')
4994 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004995# ifdef VMS
4996 /* VMS crashes on divide by zero, work around it */
4997 if (f2 == 0.0)
4998 {
4999 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005000 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005001 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005002 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005003 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005004 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005005 }
5006 else
5007 f1 = f1 / f2;
5008# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 /* We rely on the floating point library to handle divide
5010 * by zero to result in "inf" and not a crash. */
5011 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005012# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005015 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005016 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017 return FAIL;
5018 }
5019 rettv->v_type = VAR_FLOAT;
5020 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005025 if (op == '*')
5026 n1 = n1 * n2;
5027 else if (op == '/')
5028 {
5029 if (n2 == 0) /* give an error message? */
5030 {
5031 if (n1 == 0)
5032 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5033 else if (n1 < 0)
5034 n1 = -0x7fffffffL;
5035 else
5036 n1 = 0x7fffffffL;
5037 }
5038 else
5039 n1 = n1 / n2;
5040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005042 {
5043 if (n2 == 0) /* give an error message? */
5044 n1 = 0;
5045 else
5046 n1 = n1 % n2;
5047 }
5048 rettv->v_type = VAR_NUMBER;
5049 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052 }
5053
5054 return OK;
5055}
5056
5057/*
5058 * Handle sixth level expression:
5059 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005060 * "string" string constant
5061 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 * &option-name option value
5063 * @r register contents
5064 * identifier variable value
5065 * function() function call
5066 * $VAR environment variable
5067 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005068 * [expr, expr] List
5069 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 *
5071 * Also handle:
5072 * ! in front logical NOT
5073 * - in front unary minus
5074 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 * trailing [] subscript in String or List
5076 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 *
5078 * "arg" must point to the first non-white of the expression.
5079 * "arg" is advanced to the next non-white after the recognized expression.
5080 *
5081 * Return OK or FAIL.
5082 */
5083 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005084eval7(
5085 char_u **arg,
5086 typval_T *rettv,
5087 int evaluate,
5088 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 long n;
5091 int len;
5092 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 char_u *start_leader, *end_leader;
5094 int ret = OK;
5095 char_u *alias;
5096
5097 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102
5103 /*
5104 * Skip '!' and '-' characters. They are handled later.
5105 */
5106 start_leader = *arg;
5107 while (**arg == '!' || **arg == '-' || **arg == '+')
5108 *arg = skipwhite(*arg + 1);
5109 end_leader = *arg;
5110
5111 switch (**arg)
5112 {
5113 /*
5114 * Number constant.
5115 */
5116 case '0':
5117 case '1':
5118 case '2':
5119 case '3':
5120 case '4':
5121 case '5':
5122 case '6':
5123 case '7':
5124 case '8':
5125 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005126 {
5127#ifdef FEAT_FLOAT
5128 char_u *p = skipdigits(*arg + 1);
5129 int get_float = FALSE;
5130
5131 /* We accept a float when the format matches
5132 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005133 * strict to avoid backwards compatibility problems.
5134 * Don't look for a float after the "." operator, so that
5135 * ":let vers = 1.2.3" doesn't fail. */
5136 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 get_float = TRUE;
5139 p = skipdigits(p + 2);
5140 if (*p == 'e' || *p == 'E')
5141 {
5142 ++p;
5143 if (*p == '-' || *p == '+')
5144 ++p;
5145 if (!vim_isdigit(*p))
5146 get_float = FALSE;
5147 else
5148 p = skipdigits(p + 1);
5149 }
5150 if (ASCII_ISALPHA(*p) || *p == '.')
5151 get_float = FALSE;
5152 }
5153 if (get_float)
5154 {
5155 float_T f;
5156
5157 *arg += string2float(*arg, &f);
5158 if (evaluate)
5159 {
5160 rettv->v_type = VAR_FLOAT;
5161 rettv->vval.v_float = f;
5162 }
5163 }
5164 else
5165#endif
5166 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005167 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005168 *arg += len;
5169 if (evaluate)
5170 {
5171 rettv->v_type = VAR_NUMBER;
5172 rettv->vval.v_number = n;
5173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177
5178 /*
5179 * String constant: "string".
5180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005188 break;
5189
5190 /*
5191 * List: [expr, expr]
5192 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 break;
5195
5196 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005197 * Dictionary: {key: val, key: val}
5198 */
5199 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5200 break;
5201
5202 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005203 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005205 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 break;
5207
5208 /*
5209 * Environment variable: $VAR.
5210 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005211 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 break;
5213
5214 /*
5215 * Register contents: @r.
5216 */
5217 case '@': ++*arg;
5218 if (evaluate)
5219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005220 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005221 rettv->vval.v_string = get_reg_contents(**arg,
5222 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 }
5224 if (**arg != NUL)
5225 ++*arg;
5226 break;
5227
5228 /*
5229 * nested expression: (expression).
5230 */
5231 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005232 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 if (**arg == ')')
5234 ++*arg;
5235 else if (ret == OK)
5236 {
5237 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 ret = FAIL;
5240 }
5241 break;
5242
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 break;
5245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246
5247 if (ret == NOTDONE)
5248 {
5249 /*
5250 * Must be a variable or function name.
5251 * Can also be a curly-braces kind of name: {expr}.
5252 */
5253 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005254 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 if (alias != NULL)
5256 s = alias;
5257
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005258 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 ret = FAIL;
5260 else
5261 {
5262 if (**arg == '(') /* recursive! */
5263 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005264 partial_T *partial;
5265
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 /* If "s" is the name of a variable of type VAR_FUNC
5267 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005268 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269
5270 /* Invoke the function. */
5271 ret = get_func_tv(s, len, rettv, arg,
5272 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005273 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005274
5275 /* If evaluate is FALSE rettv->v_type was not set in
5276 * get_func_tv, but it's needed in handle_subscript() to parse
5277 * what follows. So set it here. */
5278 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5279 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005280 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005281 rettv->v_type = VAR_FUNC;
5282 }
5283
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 /* Stop the expression evaluation when immediately
5285 * aborting on error, or when an interrupt occurred or
5286 * an exception was thrown but not caught. */
5287 if (aborting())
5288 {
5289 if (ret == OK)
5290 clear_tv(rettv);
5291 ret = FAIL;
5292 }
5293 }
5294 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005295 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005296 else
5297 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005298 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005299 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 }
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 *arg = skipwhite(*arg);
5303
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005304 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5305 * expr(expr). */
5306 if (ret == OK)
5307 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
5309 /*
5310 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5311 */
5312 if (ret == OK && evaluate && end_leader > start_leader)
5313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005314 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005315 int val = 0;
5316#ifdef FEAT_FLOAT
5317 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005319 if (rettv->v_type == VAR_FLOAT)
5320 f = rettv->vval.v_float;
5321 else
5322#endif
5323 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 clear_tv(rettv);
5327 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329 else
5330 {
5331 while (end_leader > start_leader)
5332 {
5333 --end_leader;
5334 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005335 {
5336#ifdef FEAT_FLOAT
5337 if (rettv->v_type == VAR_FLOAT)
5338 f = !f;
5339 else
5340#endif
5341 val = !val;
5342 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005343 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005344 {
5345#ifdef FEAT_FLOAT
5346 if (rettv->v_type == VAR_FLOAT)
5347 f = -f;
5348 else
5349#endif
5350 val = -val;
5351 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005352 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005353#ifdef FEAT_FLOAT
5354 if (rettv->v_type == VAR_FLOAT)
5355 {
5356 clear_tv(rettv);
5357 rettv->vval.v_float = f;
5358 }
5359 else
5360#endif
5361 {
5362 clear_tv(rettv);
5363 rettv->v_type = VAR_NUMBER;
5364 rettv->vval.v_number = val;
5365 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 }
5368
5369 return ret;
5370}
5371
5372/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005373 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5374 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5376 */
5377 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005378eval_index(
5379 char_u **arg,
5380 typval_T *rettv,
5381 int evaluate,
5382 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383{
5384 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387 long len = -1;
5388 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391
Bram Moolenaara03f2332016-02-06 18:09:59 +01005392 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005394 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005395 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005396 if (verbose)
5397 EMSG(_("E695: Cannot index a Funcref"));
5398 return FAIL;
5399 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005400#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005401 if (verbose)
5402 EMSG(_(e_float_as_string));
5403 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005404#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005405 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005406 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005407 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005408 if (verbose)
5409 EMSG(_("E909: Cannot index a special variable"));
5410 return FAIL;
5411 case VAR_UNKNOWN:
5412 if (evaluate)
5413 return FAIL;
5414 /* FALLTHROUGH */
5415
5416 case VAR_STRING:
5417 case VAR_NUMBER:
5418 case VAR_LIST:
5419 case VAR_DICT:
5420 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005421 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005423 init_tv(&var1);
5424 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005425 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 /*
5428 * dict.name
5429 */
5430 key = *arg + 1;
5431 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5432 ;
5433 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005435 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 }
5437 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005439 /*
5440 * something[idx]
5441 *
5442 * Get the (first) variable from inside the [].
5443 */
5444 *arg = skipwhite(*arg + 1);
5445 if (**arg == ':')
5446 empty1 = TRUE;
5447 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5448 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005449 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5450 {
5451 /* not a number or string */
5452 clear_tv(&var1);
5453 return FAIL;
5454 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455
5456 /*
5457 * Get the second variable from inside the [:].
5458 */
5459 if (**arg == ':')
5460 {
5461 range = TRUE;
5462 *arg = skipwhite(*arg + 1);
5463 if (**arg == ']')
5464 empty2 = TRUE;
5465 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005467 if (!empty1)
5468 clear_tv(&var1);
5469 return FAIL;
5470 }
5471 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5472 {
5473 /* not a number or string */
5474 if (!empty1)
5475 clear_tv(&var1);
5476 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005477 return FAIL;
5478 }
5479 }
5480
5481 /* Check for the ']'. */
5482 if (**arg != ']')
5483 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005484 if (verbose)
5485 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005486 clear_tv(&var1);
5487 if (range)
5488 clear_tv(&var2);
5489 return FAIL;
5490 }
5491 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005492 }
5493
5494 if (evaluate)
5495 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496 n1 = 0;
5497 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 n1 = get_tv_number(&var1);
5500 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 }
5502 if (range)
5503 {
5504 if (empty2)
5505 n2 = -1;
5506 else
5507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 n2 = get_tv_number(&var2);
5509 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 }
5511 }
5512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005515 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005516 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005517 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005518 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005519 case VAR_SPECIAL:
5520 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005521 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005522 break; /* not evaluating, skipping over subscript */
5523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 case VAR_NUMBER:
5525 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005526 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527 len = (long)STRLEN(s);
5528 if (range)
5529 {
5530 /* The resulting variable is a substring. If the indexes
5531 * are out of range the result is empty. */
5532 if (n1 < 0)
5533 {
5534 n1 = len + n1;
5535 if (n1 < 0)
5536 n1 = 0;
5537 }
5538 if (n2 < 0)
5539 n2 = len + n2;
5540 else if (n2 >= len)
5541 n2 = len;
5542 if (n1 >= len || n2 < 0 || n1 > n2)
5543 s = NULL;
5544 else
5545 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5546 }
5547 else
5548 {
5549 /* The resulting variable is a string of a single
5550 * character. If the index is too big or negative the
5551 * result is empty. */
5552 if (n1 >= len || n1 < 0)
5553 s = NULL;
5554 else
5555 s = vim_strnsave(s + n1, 1);
5556 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 clear_tv(rettv);
5558 rettv->v_type = VAR_STRING;
5559 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 break;
5561
5562 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 if (n1 < 0)
5565 n1 = len + n1;
5566 if (!empty1 && (n1 < 0 || n1 >= len))
5567 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005568 /* For a range we allow invalid values and return an empty
5569 * list. A list index out of range is an error. */
5570 if (!range)
5571 {
5572 if (verbose)
5573 EMSGN(_(e_listidx), n1);
5574 return FAIL;
5575 }
5576 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 }
5578 if (range)
5579 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005580 list_T *l;
5581 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582
5583 if (n2 < 0)
5584 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005585 else if (n2 >= len)
5586 n2 = len - 1;
5587 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005588 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005589 l = list_alloc();
5590 if (l == NULL)
5591 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005592 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 n1 <= n2; ++n1)
5594 {
5595 if (list_append_tv(l, &item->li_tv) == FAIL)
5596 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005597 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 return FAIL;
5599 }
5600 item = item->li_next;
5601 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005602 clear_tv(rettv);
5603 rettv->v_type = VAR_LIST;
5604 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005605 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005606 }
5607 else
5608 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005609 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005610 clear_tv(rettv);
5611 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614
5615 case VAR_DICT:
5616 if (range)
5617 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005618 if (verbose)
5619 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620 if (len == -1)
5621 clear_tv(&var1);
5622 return FAIL;
5623 }
5624 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005625 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626
5627 if (len == -1)
5628 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005629 key = get_tv_string_chk(&var1);
5630 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632 clear_tv(&var1);
5633 return FAIL;
5634 }
5635 }
5636
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005637 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005639 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005640 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 if (len == -1)
5642 clear_tv(&var1);
5643 if (item == NULL)
5644 return FAIL;
5645
5646 copy_tv(&item->di_tv, &var1);
5647 clear_tv(rettv);
5648 *rettv = var1;
5649 }
5650 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651 }
5652 }
5653
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654 return OK;
5655}
5656
5657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 * Get an option value.
5659 * "arg" points to the '&' or '+' before the option name.
5660 * "arg" is advanced to character after the option name.
5661 * Return OK or FAIL.
5662 */
5663 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005664get_option_tv(
5665 char_u **arg,
5666 typval_T *rettv, /* when NULL, only check if option exists */
5667 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
5669 char_u *option_end;
5670 long numval;
5671 char_u *stringval;
5672 int opt_type;
5673 int c;
5674 int working = (**arg == '+'); /* has("+option") */
5675 int ret = OK;
5676 int opt_flags;
5677
5678 /*
5679 * Isolate the option name and find its value.
5680 */
5681 option_end = find_option_end(arg, &opt_flags);
5682 if (option_end == NULL)
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 EMSG2(_("E112: Option name missing: %s"), *arg);
5686 return FAIL;
5687 }
5688
5689 if (!evaluate)
5690 {
5691 *arg = option_end;
5692 return OK;
5693 }
5694
5695 c = *option_end;
5696 *option_end = NUL;
5697 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005698 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700 if (opt_type == -3) /* invalid name */
5701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005702 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 EMSG2(_("E113: Unknown option: %s"), *arg);
5704 ret = FAIL;
5705 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005706 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
5708 if (opt_type == -2) /* hidden string option */
5709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005710 rettv->v_type = VAR_STRING;
5711 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713 else if (opt_type == -1) /* hidden number option */
5714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005715 rettv->v_type = VAR_NUMBER;
5716 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
5718 else if (opt_type == 1) /* number option */
5719 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005720 rettv->v_type = VAR_NUMBER;
5721 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
5723 else /* string option */
5724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005725 rettv->v_type = VAR_STRING;
5726 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 }
5728 }
5729 else if (working && (opt_type == -2 || opt_type == -1))
5730 ret = FAIL;
5731
5732 *option_end = c; /* put back for error messages */
5733 *arg = option_end;
5734
5735 return ret;
5736}
5737
5738/*
5739 * Allocate a variable for a string constant.
5740 * Return OK or FAIL.
5741 */
5742 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005743get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
5745 char_u *p;
5746 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 int extra = 0;
5748
5749 /*
5750 * Find the end of the string, skipping backslashed characters.
5751 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
5754 if (*p == '\\' && p[1] != NUL)
5755 {
5756 ++p;
5757 /* A "\<x>" form occupies at least 4 characters, and produces up
5758 * to 6 characters: reserve space for 2 extra */
5759 if (*p == '<')
5760 extra += 2;
5761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 }
5763
5764 if (*p != '"')
5765 {
5766 EMSG2(_("E114: Missing quote: %s"), *arg);
5767 return FAIL;
5768 }
5769
5770 /* If only parsing, set *arg and return here */
5771 if (!evaluate)
5772 {
5773 *arg = p + 1;
5774 return OK;
5775 }
5776
5777 /*
5778 * Copy the string into allocated memory, handling backslashed
5779 * characters.
5780 */
5781 name = alloc((unsigned)(p - *arg + extra));
5782 if (name == NULL)
5783 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 rettv->v_type = VAR_STRING;
5785 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 {
5789 if (*p == '\\')
5790 {
5791 switch (*++p)
5792 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 case 'b': *name++ = BS; ++p; break;
5794 case 'e': *name++ = ESC; ++p; break;
5795 case 'f': *name++ = FF; ++p; break;
5796 case 'n': *name++ = NL; ++p; break;
5797 case 'r': *name++ = CAR; ++p; break;
5798 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
5800 case 'X': /* hex: "\x1", "\x12" */
5801 case 'x':
5802 case 'u': /* Unicode: "\u0023" */
5803 case 'U':
5804 if (vim_isxdigit(p[1]))
5805 {
5806 int n, nr;
5807 int c = toupper(*p);
5808
5809 if (c == 'X')
5810 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005811 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005813 else
5814 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 nr = 0;
5816 while (--n >= 0 && vim_isxdigit(p[1]))
5817 {
5818 ++p;
5819 nr = (nr << 4) + hex2nr(*p);
5820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822#ifdef FEAT_MBYTE
5823 /* For "\u" store the number according to
5824 * 'encoding'. */
5825 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 else
5828#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 break;
5832
5833 /* octal: "\1", "\12", "\123" */
5834 case '0':
5835 case '1':
5836 case '2':
5837 case '3':
5838 case '4':
5839 case '5':
5840 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 case '7': *name = *p++ - '0';
5842 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 *name = (*name << 3) + *p++ - '0';
5845 if (*p >= '0' && *p <= '7')
5846 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005848 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 break;
5850
5851 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 if (extra != 0)
5854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 break;
5857 }
5858 /* FALLTHROUGH */
5859
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 break;
5862 }
5863 }
5864 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 *arg = p + 1;
5870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 return OK;
5872}
5873
5874/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 * Return OK or FAIL.
5877 */
5878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005879get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005883 int reduce = 0;
5884
5885 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 */
5888 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5889 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 break;
5894 ++reduce;
5895 ++p;
5896 }
5897 }
5898
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 return FAIL;
5903 }
5904
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 if (!evaluate)
5907 {
5908 *arg = p + 1;
5909 return OK;
5910 }
5911
5912 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005913 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914 */
5915 str = alloc((unsigned)((p - *arg) - reduce));
5916 if (str == NULL)
5917 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 rettv->v_type = VAR_STRING;
5919 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 break;
5927 ++p;
5928 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005930 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005932 *arg = p + 1;
5933
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 return OK;
5935}
5936
Bram Moolenaarddecc252016-04-06 22:59:37 +02005937 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005938partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005939{
5940 int i;
5941
5942 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005943 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005944 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005945 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005946 func_unref(pt->pt_name);
5947 vim_free(pt->pt_name);
5948 vim_free(pt);
5949}
5950
5951/*
5952 * Unreference a closure: decrement the reference count and free it when it
5953 * becomes zero.
5954 */
5955 void
5956partial_unref(partial_T *pt)
5957{
5958 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005959 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005960}
5961
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005962/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 * Allocate a variable for a List and fill it from "*arg".
5964 * Return OK or FAIL.
5965 */
5966 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005967get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 list_T *l = NULL;
5970 typval_T tv;
5971 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972
5973 if (evaluate)
5974 {
5975 l = list_alloc();
5976 if (l == NULL)
5977 return FAIL;
5978 }
5979
5980 *arg = skipwhite(*arg + 1);
5981 while (**arg != ']' && **arg != NUL)
5982 {
5983 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5984 goto failret;
5985 if (evaluate)
5986 {
5987 item = listitem_alloc();
5988 if (item != NULL)
5989 {
5990 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005991 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992 list_append(l, item);
5993 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005994 else
5995 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 }
5997
5998 if (**arg == ']')
5999 break;
6000 if (**arg != ',')
6001 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006002 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 goto failret;
6004 }
6005 *arg = skipwhite(*arg + 1);
6006 }
6007
6008 if (**arg != ']')
6009 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006010 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011failret:
6012 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006013 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 return FAIL;
6015 }
6016
6017 *arg = skipwhite(*arg + 1);
6018 if (evaluate)
6019 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006020 rettv->v_type = VAR_LIST;
6021 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022 ++l->lv_refcount;
6023 }
6024
6025 return OK;
6026}
6027
6028/*
6029 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006030 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006032 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006035 list_T *l;
6036
6037 l = (list_T *)alloc_clear(sizeof(list_T));
6038 if (l != NULL)
6039 {
6040 /* Prepend the list to the list of lists for garbage collection. */
6041 if (first_list != NULL)
6042 first_list->lv_used_prev = l;
6043 l->lv_used_prev = NULL;
6044 l->lv_used_next = first_list;
6045 first_list = l;
6046 }
6047 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048}
6049
6050/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006051 * Allocate an empty list for a return value.
6052 * Returns OK or FAIL.
6053 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006055rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006056{
6057 list_T *l = list_alloc();
6058
6059 if (l == NULL)
6060 return FAIL;
6061
6062 rettv->vval.v_list = l;
6063 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006064 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006065 ++l->lv_refcount;
6066 return OK;
6067}
6068
6069/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 * Unreference a list: decrement the reference count and free it when it
6071 * becomes zero.
6072 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006074list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006076 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006077 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078}
6079
6080/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006081 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 * Ignores the reference count.
6083 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006084 static void
6085list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086{
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006089 for (item = l->lv_first; item != NULL; item = l->lv_first)
6090 {
6091 /* Remove the item before deleting it. */
6092 l->lv_first = item->li_next;
6093 clear_tv(&item->li_tv);
6094 vim_free(item);
6095 }
6096}
6097
6098 static void
6099list_free_list(list_T *l)
6100{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006101 /* Remove the list from the list of lists for garbage collection. */
6102 if (l->lv_used_prev == NULL)
6103 first_list = l->lv_used_next;
6104 else
6105 l->lv_used_prev->lv_used_next = l->lv_used_next;
6106 if (l->lv_used_next != NULL)
6107 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6108
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006109 vim_free(l);
6110}
6111
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006112 void
6113list_free(list_T *l)
6114{
6115 if (!in_free_unref_items)
6116 {
6117 list_free_contents(l);
6118 list_free_list(l);
6119 }
6120}
6121
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122/*
6123 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006124 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006126 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006127listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128{
Bram Moolenaar33570922005-01-25 22:26:29 +00006129 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006130}
6131
6132/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006133 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006134 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006135 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006136listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006137{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006138 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139 vim_free(item);
6140}
6141
6142/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006143 * Remove a list item from a List and free it. Also clears the value.
6144 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006146listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006147{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006148 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006149 listitem_free(item);
6150}
6151
6152/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 * Get the number of items in a list.
6154 */
6155 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006156list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 if (l == NULL)
6159 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006160 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161}
6162
6163/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006164 * Return TRUE when two lists have exactly the same values.
6165 */
6166 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167list_equal(
6168 list_T *l1,
6169 list_T *l2,
6170 int ic, /* ignore case for strings */
6171 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006172{
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006175 if (l1 == NULL || l2 == NULL)
6176 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006177 if (l1 == l2)
6178 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006179 if (list_len(l1) != list_len(l2))
6180 return FALSE;
6181
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006182 for (item1 = l1->lv_first, item2 = l2->lv_first;
6183 item1 != NULL && item2 != NULL;
6184 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186 return FALSE;
6187 return item1 == NULL && item2 == NULL;
6188}
6189
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006190/*
6191 * Return the dictitem that an entry in a hashtable points to.
6192 */
6193 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006194dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006195{
6196 return HI2DI(hi);
6197}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006198
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006200 * Return TRUE when two dictionaries have exactly the same key/values.
6201 */
6202 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006203dict_equal(
6204 dict_T *d1,
6205 dict_T *d2,
6206 int ic, /* ignore case for strings */
6207 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006208{
Bram Moolenaar33570922005-01-25 22:26:29 +00006209 hashitem_T *hi;
6210 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006211 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006212
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006213 if (d1 == NULL || d2 == NULL)
6214 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006215 if (d1 == d2)
6216 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006217 if (dict_len(d1) != dict_len(d2))
6218 return FALSE;
6219
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006220 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006221 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006222 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006223 if (!HASHITEM_EMPTY(hi))
6224 {
6225 item2 = dict_find(d2, hi->hi_key, -1);
6226 if (item2 == NULL)
6227 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006228 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006229 return FALSE;
6230 --todo;
6231 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006232 }
6233 return TRUE;
6234}
6235
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006236static int tv_equal_recurse_limit;
6237
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006238/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006240 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006241 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006242 */
6243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006244tv_equal(
6245 typval_T *tv1,
6246 typval_T *tv2,
6247 int ic, /* ignore case */
6248 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006249{
6250 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006251 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006252 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006253 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006254
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006255 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6256 if ((tv1->v_type == VAR_FUNC
6257 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6258 && (tv2->v_type == VAR_FUNC
6259 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6260 {
6261 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6262 : tv1->vval.v_partial->pt_name;
6263 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6264 : tv2->vval.v_partial->pt_name;
6265 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6266 }
6267
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006268 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006269 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006270
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006271 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006272 * recursiveness to a limit. We guess they are equal then.
6273 * A fixed limit has the problem of still taking an awful long time.
6274 * Reduce the limit every time running into it. That should work fine for
6275 * deeply linked structures that are not recursively linked and catch
6276 * recursiveness quickly. */
6277 if (!recursive)
6278 tv_equal_recurse_limit = 1000;
6279 if (recursive_cnt >= tv_equal_recurse_limit)
6280 {
6281 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006282 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006283 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006284
6285 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006286 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006287 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006288 ++recursive_cnt;
6289 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6290 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006291 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006292
6293 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006294 ++recursive_cnt;
6295 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6296 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006297 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006298
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006299 case VAR_NUMBER:
6300 return tv1->vval.v_number == tv2->vval.v_number;
6301
6302 case VAR_STRING:
6303 s1 = get_tv_string_buf(tv1, buf1);
6304 s2 = get_tv_string_buf(tv2, buf2);
6305 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006306
6307 case VAR_SPECIAL:
6308 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006309
6310 case VAR_FLOAT:
6311#ifdef FEAT_FLOAT
6312 return tv1->vval.v_float == tv2->vval.v_float;
6313#endif
6314 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006315#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006316 return tv1->vval.v_job == tv2->vval.v_job;
6317#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006318 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006319#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006320 return tv1->vval.v_channel == tv2->vval.v_channel;
6321#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006322 case VAR_FUNC:
6323 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006324 case VAR_UNKNOWN:
6325 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006326 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006327
Bram Moolenaara03f2332016-02-06 18:09:59 +01006328 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6329 * does not equal anything, not even itself. */
6330 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331}
6332
6333/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334 * Locate item with index "n" in list "l" and return it.
6335 * A negative index is counted from the end; -1 is the last item.
6336 * Returns NULL when "n" is out of range.
6337 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006338 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006339list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340{
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342 long idx;
6343
6344 if (l == NULL)
6345 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006346
6347 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006349 n = l->lv_len + n;
6350
6351 /* Check for index out of range. */
6352 if (n < 0 || n >= l->lv_len)
6353 return NULL;
6354
6355 /* When there is a cached index may start search from there. */
6356 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006358 if (n < l->lv_idx / 2)
6359 {
6360 /* closest to the start of the list */
6361 item = l->lv_first;
6362 idx = 0;
6363 }
6364 else if (n > (l->lv_idx + l->lv_len) / 2)
6365 {
6366 /* closest to the end of the list */
6367 item = l->lv_last;
6368 idx = l->lv_len - 1;
6369 }
6370 else
6371 {
6372 /* closest to the cached index */
6373 item = l->lv_idx_item;
6374 idx = l->lv_idx;
6375 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376 }
6377 else
6378 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006379 if (n < l->lv_len / 2)
6380 {
6381 /* closest to the start of the list */
6382 item = l->lv_first;
6383 idx = 0;
6384 }
6385 else
6386 {
6387 /* closest to the end of the list */
6388 item = l->lv_last;
6389 idx = l->lv_len - 1;
6390 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006391 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006392
6393 while (n > idx)
6394 {
6395 /* search forward */
6396 item = item->li_next;
6397 ++idx;
6398 }
6399 while (n < idx)
6400 {
6401 /* search backward */
6402 item = item->li_prev;
6403 --idx;
6404 }
6405
6406 /* cache the used index */
6407 l->lv_idx = idx;
6408 l->lv_idx_item = item;
6409
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410 return item;
6411}
6412
6413/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006414 * Get list item "l[idx]" as a number.
6415 */
6416 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006417list_find_nr(
6418 list_T *l,
6419 long idx,
6420 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006421{
6422 listitem_T *li;
6423
6424 li = list_find(l, idx);
6425 if (li == NULL)
6426 {
6427 if (errorp != NULL)
6428 *errorp = TRUE;
6429 return -1L;
6430 }
6431 return get_tv_number_chk(&li->li_tv, errorp);
6432}
6433
6434/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006435 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6436 */
6437 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006438list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006439{
6440 listitem_T *li;
6441
6442 li = list_find(l, idx - 1);
6443 if (li == NULL)
6444 {
6445 EMSGN(_(e_listidx), idx);
6446 return NULL;
6447 }
6448 return get_tv_string(&li->li_tv);
6449}
6450
6451/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006452 * Locate "item" list "l" and return its index.
6453 * Returns -1 when "item" is not in the list.
6454 */
6455 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006457{
6458 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006460
6461 if (l == NULL)
6462 return -1;
6463 idx = 0;
6464 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6465 ++idx;
6466 if (li == NULL)
6467 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006468 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006469}
6470
6471/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006472 * Append item "item" to the end of list "l".
6473 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006475list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006476{
6477 if (l->lv_last == NULL)
6478 {
6479 /* empty list */
6480 l->lv_first = item;
6481 l->lv_last = item;
6482 item->li_prev = NULL;
6483 }
6484 else
6485 {
6486 l->lv_last->li_next = item;
6487 item->li_prev = l->lv_last;
6488 l->lv_last = item;
6489 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006490 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 item->li_next = NULL;
6492}
6493
6494/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006499list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006501 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502
Bram Moolenaar05159a02005-02-26 23:04:13 +00006503 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006505 copy_tv(tv, &li->li_tv);
6506 list_append(l, li);
6507 return OK;
6508}
6509
6510/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006511 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006512 * Return FAIL when out of memory.
6513 */
6514 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006515list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006516{
6517 listitem_T *li = listitem_alloc();
6518
6519 if (li == NULL)
6520 return FAIL;
6521 li->li_tv.v_type = VAR_DICT;
6522 li->li_tv.v_lock = 0;
6523 li->li_tv.vval.v_dict = dict;
6524 list_append(list, li);
6525 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006526 return OK;
6527}
6528
6529/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006530 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006531 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006532 * Returns FAIL when out of memory.
6533 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006535list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006536{
6537 listitem_T *li = listitem_alloc();
6538
6539 if (li == NULL)
6540 return FAIL;
6541 list_append(l, li);
6542 li->li_tv.v_type = VAR_STRING;
6543 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006544 if (str == NULL)
6545 li->li_tv.vval.v_string = NULL;
6546 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006547 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006548 return FAIL;
6549 return OK;
6550}
6551
6552/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006553 * Append "n" to list "l".
6554 * Returns FAIL when out of memory.
6555 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006556 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006557list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006558{
6559 listitem_T *li;
6560
6561 li = listitem_alloc();
6562 if (li == NULL)
6563 return FAIL;
6564 li->li_tv.v_type = VAR_NUMBER;
6565 li->li_tv.v_lock = 0;
6566 li->li_tv.vval.v_number = n;
6567 list_append(l, li);
6568 return OK;
6569}
6570
6571/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006572 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573 * If "item" is NULL append at the end.
6574 * Return FAIL when out of memory.
6575 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578{
Bram Moolenaar33570922005-01-25 22:26:29 +00006579 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580
6581 if (ni == NULL)
6582 return FAIL;
6583 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006584 list_insert(l, ni, item);
6585 return OK;
6586}
6587
6588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006589list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006590{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591 if (item == NULL)
6592 /* Append new item at end of list. */
6593 list_append(l, ni);
6594 else
6595 {
6596 /* Insert new item before existing item. */
6597 ni->li_prev = item->li_prev;
6598 ni->li_next = item;
6599 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006600 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006601 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006602 ++l->lv_idx;
6603 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006604 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006605 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006607 l->lv_idx_item = NULL;
6608 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006609 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006610 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006611 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612}
6613
6614/*
6615 * Extend "l1" with "l2".
6616 * If "bef" is NULL append at the end, otherwise insert before this item.
6617 * Returns FAIL when out of memory.
6618 */
6619 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006620list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006621{
Bram Moolenaar33570922005-01-25 22:26:29 +00006622 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006623 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006625 /* We also quit the loop when we have inserted the original item count of
6626 * the list, avoid a hang when we extend a list with itself. */
6627 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006628 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6629 return FAIL;
6630 return OK;
6631}
6632
6633/*
6634 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6635 * Return FAIL when out of memory.
6636 */
6637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006638list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639{
Bram Moolenaar33570922005-01-25 22:26:29 +00006640 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006642 if (l1 == NULL || l2 == NULL)
6643 return FAIL;
6644
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006645 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006646 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 if (l == NULL)
6648 return FAIL;
6649 tv->v_type = VAR_LIST;
6650 tv->vval.v_list = l;
6651
6652 /* append all items from the second list */
6653 return list_extend(l, l2, NULL);
6654}
6655
6656/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006657 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006659 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660 * Returns NULL when out of memory.
6661 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006662 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006663list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664{
Bram Moolenaar33570922005-01-25 22:26:29 +00006665 list_T *copy;
6666 listitem_T *item;
6667 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006668
6669 if (orig == NULL)
6670 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671
6672 copy = list_alloc();
6673 if (copy != NULL)
6674 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006675 if (copyID != 0)
6676 {
6677 /* Do this before adding the items, because one of the items may
6678 * refer back to this list. */
6679 orig->lv_copyID = copyID;
6680 orig->lv_copylist = copy;
6681 }
6682 for (item = orig->lv_first; item != NULL && !got_int;
6683 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684 {
6685 ni = listitem_alloc();
6686 if (ni == NULL)
6687 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006688 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006689 {
6690 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6691 {
6692 vim_free(ni);
6693 break;
6694 }
6695 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006697 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006698 list_append(copy, ni);
6699 }
6700 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006701 if (item != NULL)
6702 {
6703 list_unref(copy);
6704 copy = NULL;
6705 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006706 }
6707
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006708 return copy;
6709}
6710
6711/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006712 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006713 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006714 * This used to be called list_remove, but that conflicts with a Sun header
6715 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006716 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006717 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006718vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006719{
Bram Moolenaar33570922005-01-25 22:26:29 +00006720 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006721
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006722 /* notify watchers */
6723 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006724 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006725 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006726 list_fix_watch(l, ip);
6727 if (ip == item2)
6728 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006729 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006730
6731 if (item2->li_next == NULL)
6732 l->lv_last = item->li_prev;
6733 else
6734 item2->li_next->li_prev = item->li_prev;
6735 if (item->li_prev == NULL)
6736 l->lv_first = item2->li_next;
6737 else
6738 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006739 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006740}
6741
6742/*
6743 * Return an allocated string with the string representation of a list.
6744 * May return NULL.
6745 */
6746 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006747list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006748{
6749 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750
6751 if (tv->vval.v_list == NULL)
6752 return NULL;
6753 ga_init2(&ga, (int)sizeof(char), 80);
6754 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006755 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006756 {
6757 vim_free(ga.ga_data);
6758 return NULL;
6759 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006760 ga_append(&ga, ']');
6761 ga_append(&ga, NUL);
6762 return (char_u *)ga.ga_data;
6763}
6764
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006765typedef struct join_S {
6766 char_u *s;
6767 char_u *tofree;
6768} join_T;
6769
6770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771list_join_inner(
6772 garray_T *gap, /* to store the result in */
6773 list_T *l,
6774 char_u *sep,
6775 int echo_style,
6776 int copyID,
6777 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006778{
6779 int i;
6780 join_T *p;
6781 int len;
6782 int sumlen = 0;
6783 int first = TRUE;
6784 char_u *tofree;
6785 char_u numbuf[NUMBUFLEN];
6786 listitem_T *item;
6787 char_u *s;
6788
6789 /* Stringify each item in the list. */
6790 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6791 {
6792 if (echo_style)
6793 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6794 else
6795 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6796 if (s == NULL)
6797 return FAIL;
6798
6799 len = (int)STRLEN(s);
6800 sumlen += len;
6801
Bram Moolenaarcde88542015-08-11 19:14:00 +02006802 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006803 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6804 if (tofree != NULL || s != numbuf)
6805 {
6806 p->s = s;
6807 p->tofree = tofree;
6808 }
6809 else
6810 {
6811 p->s = vim_strnsave(s, len);
6812 p->tofree = p->s;
6813 }
6814
6815 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006816 if (did_echo_string_emsg) /* recursion error, bail out */
6817 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006818 }
6819
6820 /* Allocate result buffer with its total size, avoid re-allocation and
6821 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6822 if (join_gap->ga_len >= 2)
6823 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6824 if (ga_grow(gap, sumlen + 2) == FAIL)
6825 return FAIL;
6826
6827 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6828 {
6829 if (first)
6830 first = FALSE;
6831 else
6832 ga_concat(gap, sep);
6833 p = ((join_T *)join_gap->ga_data) + i;
6834
6835 if (p->s != NULL)
6836 ga_concat(gap, p->s);
6837 line_breakcheck();
6838 }
6839
6840 return OK;
6841}
6842
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006843/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006844 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006845 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006846 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006847 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006848 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006849list_join(
6850 garray_T *gap,
6851 list_T *l,
6852 char_u *sep,
6853 int echo_style,
6854 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006855{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006856 garray_T join_ga;
6857 int retval;
6858 join_T *p;
6859 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006860
Bram Moolenaard39a7512015-04-16 22:51:22 +02006861 if (l->lv_len < 1)
6862 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006863 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6864 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6865
6866 /* Dispose each item in join_ga. */
6867 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006868 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006869 p = (join_T *)join_ga.ga_data;
6870 for (i = 0; i < join_ga.ga_len; ++i)
6871 {
6872 vim_free(p->tofree);
6873 ++p;
6874 }
6875 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006876 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006877
6878 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006879}
6880
6881/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006882 * Return the next (unique) copy ID.
6883 * Used for serializing nested structures.
6884 */
6885 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006886get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006887{
6888 current_copyID += COPYID_INC;
6889 return current_copyID;
6890}
6891
6892/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 * Garbage collection for lists and dictionaries.
6894 *
6895 * We use reference counts to be able to free most items right away when they
6896 * are no longer used. But for composite items it's possible that it becomes
6897 * unused while the reference count is > 0: When there is a recursive
6898 * reference. Example:
6899 * :let l = [1, 2, 3]
6900 * :let d = {9: l}
6901 * :let l[1] = d
6902 *
6903 * Since this is quite unusual we handle this with garbage collection: every
6904 * once in a while find out which lists and dicts are not referenced from any
6905 * variable.
6906 *
6907 * Here is a good reference text about garbage collection (refers to Python
6908 * but it applies to all reference-counting mechanisms):
6909 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006910 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006911
6912/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913 * Do garbage collection for lists and dicts.
6914 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006915 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006917garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006918{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006919 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 buf_T *buf;
6922 win_T *wp;
6923 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006924 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006925 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006926 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006927#ifdef FEAT_WINDOWS
6928 tabpage_T *tp;
6929#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006931 /* Only do this once. */
6932 want_garbage_collect = FALSE;
6933 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006934 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006935
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006936 /* We advance by two because we add one for items referenced through
6937 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006938 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006939
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 /*
6941 * 1. Go through all accessible variables and mark all lists and dicts
6942 * with copyID.
6943 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006944
6945 /* Don't free variables in the previous_funccal list unless they are only
6946 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006947 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006948 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6949 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6951 NULL);
6952 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6953 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
6955
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956 /* script-local variables */
6957 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006959
6960 /* buffer-local variables */
6961 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006962 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6963 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964
6965 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006966 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006967 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6968 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006969#ifdef FEAT_AUTOCMD
6970 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006971 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6972 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006973#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006975#ifdef FEAT_WINDOWS
6976 /* tabpage-local variables */
6977 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006978 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6979 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006980#endif
6981
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006983 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984
6985 /* function-local variables */
6986 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6987 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006988 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6989 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 }
6991
Bram Moolenaard812df62008-11-09 12:46:09 +00006992 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006993 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006994
Bram Moolenaar1dced572012-04-05 16:54:08 +02006995#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006997#endif
6998
Bram Moolenaardb913952012-06-29 12:54:53 +02006999#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007000 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007001#endif
7002
7003#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007004 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007005#endif
7006
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007007#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007008// abort = abort || set_ref_in_channel(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007009#endif
7010
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007012 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007013 /*
7014 * 2. Free lists and dictionaries that are not referenced.
7015 */
7016 did_free = free_unref_items(copyID);
7017
7018 /*
7019 * 3. Check if any funccal can be freed now.
7020 */
7021 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007022 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 if (can_free_funccal(*pfc, copyID))
7024 {
7025 fc = *pfc;
7026 *pfc = fc->caller;
7027 free_funccal(fc, TRUE);
7028 did_free = TRUE;
7029 did_free_funccal = TRUE;
7030 }
7031 else
7032 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007033 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 if (did_free_funccal)
7035 /* When a funccal was freed some more items might be garbage
7036 * collected, so run again. */
7037 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007038 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 else if (p_verbose > 0)
7040 {
7041 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7042 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007043
7044 return did_free;
7045}
7046
7047/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007048 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049 */
7050 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007051free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007052{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007053 dict_T *dd, *dd_next;
7054 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007055 int did_free = FALSE;
7056
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007057 /* Let all "free" functions know that we are here. This means no
7058 * dictionaries, lists, channels or jobs are to be freed, because we will
7059 * do that here. */
7060 in_free_unref_items = TRUE;
7061
7062 /*
7063 * PASS 1: free the contents of the items. We don't free the items
7064 * themselves yet, so that it is possible to decrement refcount counters
7065 */
7066
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007068 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007070 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007071 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007073 /* Free the Dictionary and ordinary items it contains, but don't
7074 * recurse into Lists and Dictionaries, they will be in the list
7075 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007076 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079
7080 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007081 * Go through the list of lists and free items without the copyID.
7082 * But don't free a list that has a watcher (used in a for loop), these
7083 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007085 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7086 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7087 && ll->lv_watch == NULL)
7088 {
7089 /* Free the List and ordinary items it contains, but don't recurse
7090 * into Lists and Dictionaries, they will be in the list of dicts
7091 * or list of lists. */
7092 list_free_contents(ll);
7093 did_free = TRUE;
7094 }
7095
7096#ifdef FEAT_JOB_CHANNEL
7097 /* Go through the list of jobs and free items without the copyID. This
7098 * must happen before doing channels, because jobs refer to channels, but
7099 * the reference from the channel to the job isn't tracked. */
7100 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7101
7102 /* Go through the list of channels and free items without the copyID. */
7103 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7104#endif
7105
7106 /*
7107 * PASS 2: free the items themselves.
7108 */
7109 for (dd = first_dict; dd != NULL; dd = dd_next)
7110 {
7111 dd_next = dd->dv_used_next;
7112 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7113 dict_free_dict(dd);
7114 }
7115
7116 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007117 {
7118 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007119 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7120 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007121 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007122 /* Free the List and ordinary items it contains, but don't recurse
7123 * into Lists and Dictionaries, they will be in the list of dicts
7124 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007125 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007126 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007127 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007128
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007129#ifdef FEAT_JOB_CHANNEL
7130 /* Go through the list of jobs and free items without the copyID. This
7131 * must happen before doing channels, because jobs refer to channels, but
7132 * the reference from the channel to the job isn't tracked. */
7133 free_unused_jobs(copyID, COPYID_MASK);
7134
7135 /* Go through the list of channels and free items without the copyID. */
7136 free_unused_channels(copyID, COPYID_MASK);
7137#endif
7138
7139 in_free_unref_items = FALSE;
7140
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007141 return did_free;
7142}
7143
7144/*
7145 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007146 * "list_stack" is used to add lists to be marked. Can be NULL.
7147 *
7148 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007149 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007151set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007152{
7153 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007155 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 hashtab_T *cur_ht;
7157 ht_stack_T *ht_stack = NULL;
7158 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007159
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007160 cur_ht = ht;
7161 for (;;)
7162 {
7163 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007164 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007165 /* Mark each item in the hashtab. If the item contains a hashtab
7166 * it is added to ht_stack, if it contains a list it is added to
7167 * list_stack. */
7168 todo = (int)cur_ht->ht_used;
7169 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7170 if (!HASHITEM_EMPTY(hi))
7171 {
7172 --todo;
7173 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7174 &ht_stack, list_stack);
7175 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007176 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007177
7178 if (ht_stack == NULL)
7179 break;
7180
7181 /* take an item from the stack */
7182 cur_ht = ht_stack->ht;
7183 tempitem = ht_stack;
7184 ht_stack = ht_stack->prev;
7185 free(tempitem);
7186 }
7187
7188 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007189}
7190
7191/*
7192 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007193 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7194 *
7195 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007196 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007197 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007198set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007199{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007200 listitem_T *li;
7201 int abort = FALSE;
7202 list_T *cur_l;
7203 list_stack_T *list_stack = NULL;
7204 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007205
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007206 cur_l = l;
7207 for (;;)
7208 {
7209 if (!abort)
7210 /* Mark each item in the list. If the item contains a hashtab
7211 * it is added to ht_stack, if it contains a list it is added to
7212 * list_stack. */
7213 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7214 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7215 ht_stack, &list_stack);
7216 if (list_stack == NULL)
7217 break;
7218
7219 /* take an item from the stack */
7220 cur_l = list_stack->list;
7221 tempitem = list_stack;
7222 list_stack = list_stack->prev;
7223 free(tempitem);
7224 }
7225
7226 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007227}
7228
7229/*
7230 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007231 * "list_stack" is used to add lists to be marked. Can be NULL.
7232 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7233 *
7234 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007235 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237set_ref_in_item(
7238 typval_T *tv,
7239 int copyID,
7240 ht_stack_T **ht_stack,
7241 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007242{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007243 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007244
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007245 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007246 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007247 dict_T *dd = tv->vval.v_dict;
7248
Bram Moolenaara03f2332016-02-06 18:09:59 +01007249 if (dd != NULL && dd->dv_copyID != copyID)
7250 {
7251 /* Didn't see this dict yet. */
7252 dd->dv_copyID = copyID;
7253 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007254 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007255 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7256 }
7257 else
7258 {
7259 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7260 if (newitem == NULL)
7261 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007262 else
7263 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007264 newitem->ht = &dd->dv_hashtab;
7265 newitem->prev = *ht_stack;
7266 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007267 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007268 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007269 }
7270 }
7271 else if (tv->v_type == VAR_LIST)
7272 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007273 list_T *ll = tv->vval.v_list;
7274
Bram Moolenaara03f2332016-02-06 18:09:59 +01007275 if (ll != NULL && ll->lv_copyID != copyID)
7276 {
7277 /* Didn't see this list yet. */
7278 ll->lv_copyID = copyID;
7279 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007280 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007281 abort = set_ref_in_list(ll, copyID, ht_stack);
7282 }
7283 else
7284 {
7285 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007286 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007287 if (newitem == NULL)
7288 abort = TRUE;
7289 else
7290 {
7291 newitem->list = ll;
7292 newitem->prev = *list_stack;
7293 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007294 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007295 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007296 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007297 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007298 else if (tv->v_type == VAR_PARTIAL)
7299 {
7300 partial_T *pt = tv->vval.v_partial;
7301 int i;
7302
7303 /* A partial does not have a copyID, because it cannot contain itself.
7304 */
7305 if (pt != NULL)
7306 {
7307 if (pt->pt_dict != NULL)
7308 {
7309 typval_T dtv;
7310
7311 dtv.v_type = VAR_DICT;
7312 dtv.vval.v_dict = pt->pt_dict;
7313 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7314 }
7315
7316 for (i = 0; i < pt->pt_argc; ++i)
7317 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7318 ht_stack, list_stack);
7319 }
7320 }
7321#ifdef FEAT_JOB_CHANNEL
7322 else if (tv->v_type == VAR_JOB)
7323 {
7324 job_T *job = tv->vval.v_job;
7325 typval_T dtv;
7326
7327 if (job != NULL && job->jv_copyID != copyID)
7328 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007329 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007330 if (job->jv_channel != NULL)
7331 {
7332 dtv.v_type = VAR_CHANNEL;
7333 dtv.vval.v_channel = job->jv_channel;
7334 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7335 }
7336 if (job->jv_exit_partial != NULL)
7337 {
7338 dtv.v_type = VAR_PARTIAL;
7339 dtv.vval.v_partial = job->jv_exit_partial;
7340 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7341 }
7342 }
7343 }
7344 else if (tv->v_type == VAR_CHANNEL)
7345 {
7346 channel_T *ch =tv->vval.v_channel;
7347 int part;
7348 typval_T dtv;
7349 jsonq_T *jq;
7350 cbq_T *cq;
7351
7352 if (ch != NULL && ch->ch_copyID != copyID)
7353 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007354 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007355 for (part = PART_SOCK; part <= PART_IN; ++part)
7356 {
7357 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7358 jq = jq->jq_next)
7359 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7360 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7361 cq = cq->cq_next)
7362 if (cq->cq_partial != NULL)
7363 {
7364 dtv.v_type = VAR_PARTIAL;
7365 dtv.vval.v_partial = cq->cq_partial;
7366 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7367 }
7368 if (ch->ch_part[part].ch_partial != NULL)
7369 {
7370 dtv.v_type = VAR_PARTIAL;
7371 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7372 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7373 }
7374 }
7375 if (ch->ch_partial != NULL)
7376 {
7377 dtv.v_type = VAR_PARTIAL;
7378 dtv.vval.v_partial = ch->ch_partial;
7379 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7380 }
7381 if (ch->ch_close_partial != NULL)
7382 {
7383 dtv.v_type = VAR_PARTIAL;
7384 dtv.vval.v_partial = ch->ch_close_partial;
7385 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7386 }
7387 }
7388 }
7389#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007390 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007391}
7392
7393/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007394 * Allocate an empty header for a dictionary.
7395 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007396 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007397dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398{
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007403 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007404 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007405 if (first_dict != NULL)
7406 first_dict->dv_used_prev = d;
7407 d->dv_used_next = first_dict;
7408 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007409 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007410
Bram Moolenaar33570922005-01-25 22:26:29 +00007411 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007412 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007413 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007414 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007415 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007416 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007417 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418}
7419
7420/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007421 * Allocate an empty dict for a return value.
7422 * Returns OK or FAIL.
7423 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007424 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007425rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007426{
7427 dict_T *d = dict_alloc();
7428
7429 if (d == NULL)
7430 return FAIL;
7431
7432 rettv->vval.v_dict = d;
7433 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007434 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007435 ++d->dv_refcount;
7436 return OK;
7437}
7438
7439
7440/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007441 * Unreference a Dictionary: decrement the reference count and free it when it
7442 * becomes zero.
7443 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007445dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007447 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007448 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449}
7450
7451/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007452 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453 * Ignores the reference count.
7454 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007455 static void
7456dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007457{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007458 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007459 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007460 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007462 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007463 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007464 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007466 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007467 if (!HASHITEM_EMPTY(hi))
7468 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007469 /* Remove the item before deleting it, just in case there is
7470 * something recursive causing trouble. */
7471 di = HI2DI(hi);
7472 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007473 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007474 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007475 --todo;
7476 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007479}
7480
7481 static void
7482dict_free_dict(dict_T *d)
7483{
7484 /* Remove the dict from the list of dicts for garbage collection. */
7485 if (d->dv_used_prev == NULL)
7486 first_dict = d->dv_used_next;
7487 else
7488 d->dv_used_prev->dv_used_next = d->dv_used_next;
7489 if (d->dv_used_next != NULL)
7490 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491 vim_free(d);
7492}
7493
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007494 void
7495dict_free(dict_T *d)
7496{
7497 if (!in_free_unref_items)
7498 {
7499 dict_free_contents(d);
7500 dict_free_dict(d);
7501 }
7502}
7503
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504/*
7505 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007506 * The "key" is copied to the new item.
7507 * Note that the value of the item "di_tv" still needs to be initialized!
7508 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007510 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007511dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007512{
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007515 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007516 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007519 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007520 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522}
7523
7524/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007525 * Make a copy of a Dictionary item.
7526 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007528dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007529{
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007532 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7533 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007534 if (di != NULL)
7535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007536 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007537 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007538 copy_tv(&org->di_tv, &di->di_tv);
7539 }
7540 return di;
7541}
7542
7543/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007544 * Remove item "item" from Dictionary "dict" and free it.
7545 */
7546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007548{
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550
Bram Moolenaar33570922005-01-25 22:26:29 +00007551 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 if (HASHITEM_EMPTY(hi))
7553 EMSG2(_(e_intern2), "dictitem_remove()");
7554 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007555 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007556 dictitem_free(item);
7557}
7558
7559/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 * Free a dict item. Also clears the value.
7561 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007562 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007563dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007566 if (item->di_flags & DI_FLAGS_ALLOC)
7567 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568}
7569
7570/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007571 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7572 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007573 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 * Returns NULL when out of memory.
7575 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007577dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007578{
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 dict_T *copy;
7580 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007581 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583
7584 if (orig == NULL)
7585 return NULL;
7586
7587 copy = dict_alloc();
7588 if (copy != NULL)
7589 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007590 if (copyID != 0)
7591 {
7592 orig->dv_copyID = copyID;
7593 orig->dv_copydict = copy;
7594 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007595 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007596 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007597 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007600 --todo;
7601
7602 di = dictitem_alloc(hi->hi_key);
7603 if (di == NULL)
7604 break;
7605 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007606 {
7607 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7608 copyID) == FAIL)
7609 {
7610 vim_free(di);
7611 break;
7612 }
7613 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 else
7615 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7616 if (dict_add(copy, di) == FAIL)
7617 {
7618 dictitem_free(di);
7619 break;
7620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007622 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007623
Bram Moolenaare9a41262005-01-15 22:18:47 +00007624 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007625 if (todo > 0)
7626 {
7627 dict_unref(copy);
7628 copy = NULL;
7629 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007630 }
7631
7632 return copy;
7633}
7634
7635/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007637 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007638 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007639 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007640dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641{
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643}
7644
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007646 * Add a number or string entry to dictionary "d".
7647 * When "str" is NULL use number "nr", otherwise use "str".
7648 * Returns FAIL when out of memory and when key already exists.
7649 */
7650 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007651dict_add_nr_str(
7652 dict_T *d,
7653 char *key,
7654 long nr,
7655 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007656{
7657 dictitem_T *item;
7658
7659 item = dictitem_alloc((char_u *)key);
7660 if (item == NULL)
7661 return FAIL;
7662 item->di_tv.v_lock = 0;
7663 if (str == NULL)
7664 {
7665 item->di_tv.v_type = VAR_NUMBER;
7666 item->di_tv.vval.v_number = nr;
7667 }
7668 else
7669 {
7670 item->di_tv.v_type = VAR_STRING;
7671 item->di_tv.vval.v_string = vim_strsave(str);
7672 }
7673 if (dict_add(d, item) == FAIL)
7674 {
7675 dictitem_free(item);
7676 return FAIL;
7677 }
7678 return OK;
7679}
7680
7681/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007682 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007683 * Returns FAIL when out of memory and when key already exists.
7684 */
7685 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007686dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007687{
7688 dictitem_T *item;
7689
7690 item = dictitem_alloc((char_u *)key);
7691 if (item == NULL)
7692 return FAIL;
7693 item->di_tv.v_lock = 0;
7694 item->di_tv.v_type = VAR_LIST;
7695 item->di_tv.vval.v_list = list;
7696 if (dict_add(d, item) == FAIL)
7697 {
7698 dictitem_free(item);
7699 return FAIL;
7700 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007701 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007702 return OK;
7703}
7704
7705/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007706 * Get the number of items in a Dictionary.
7707 */
7708 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007709dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007711 if (d == NULL)
7712 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007713 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714}
7715
7716/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 * Find item "key[len]" in Dictionary "d".
7718 * If "len" is negative use strlen(key).
7719 * Returns NULL when not found.
7720 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007721 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007722dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007724#define AKEYLEN 200
7725 char_u buf[AKEYLEN];
7726 char_u *akey;
7727 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007728 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007730 if (len < 0)
7731 akey = key;
7732 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007733 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007734 tofree = akey = vim_strnsave(key, len);
7735 if (akey == NULL)
7736 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007737 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007738 else
7739 {
7740 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007741 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007742 akey = buf;
7743 }
7744
Bram Moolenaar33570922005-01-25 22:26:29 +00007745 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007746 vim_free(tofree);
7747 if (HASHITEM_EMPTY(hi))
7748 return NULL;
7749 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750}
7751
7752/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007753 * Get a string item from a dictionary.
7754 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007755 * Returns NULL if the entry doesn't exist or out of memory.
7756 */
7757 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007758get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007759{
7760 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007761 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007762
7763 di = dict_find(d, key, -1);
7764 if (di == NULL)
7765 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007766 s = get_tv_string(&di->di_tv);
7767 if (save && s != NULL)
7768 s = vim_strsave(s);
7769 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007770}
7771
7772/*
7773 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007774 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007775 */
7776 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007777get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007778{
7779 dictitem_T *di;
7780
7781 di = dict_find(d, key, -1);
7782 if (di == NULL)
7783 return 0;
7784 return get_tv_number(&di->di_tv);
7785}
7786
7787/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007788 * Return an allocated string with the string representation of a Dictionary.
7789 * May return NULL.
7790 */
7791 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007792dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007793{
7794 garray_T ga;
7795 int first = TRUE;
7796 char_u *tofree;
7797 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007798 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007799 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007800 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007801 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007802
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007803 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007804 return NULL;
7805 ga_init2(&ga, (int)sizeof(char), 80);
7806 ga_append(&ga, '{');
7807
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007808 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007809 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007810 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007811 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007812 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007813 --todo;
7814
7815 if (first)
7816 first = FALSE;
7817 else
7818 ga_concat(&ga, (char_u *)", ");
7819
7820 tofree = string_quote(hi->hi_key, FALSE);
7821 if (tofree != NULL)
7822 {
7823 ga_concat(&ga, tofree);
7824 vim_free(tofree);
7825 }
7826 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007827 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007828 if (s != NULL)
7829 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007830 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007831 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007832 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007833 line_breakcheck();
7834
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007836 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007837 if (todo > 0)
7838 {
7839 vim_free(ga.ga_data);
7840 return NULL;
7841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007842
7843 ga_append(&ga, '}');
7844 ga_append(&ga, NUL);
7845 return (char_u *)ga.ga_data;
7846}
7847
7848/*
7849 * Allocate a variable for a Dictionary and fill it from "*arg".
7850 * Return OK or FAIL. Returns NOTDONE for {expr}.
7851 */
7852 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007853get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007854{
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 dict_T *d = NULL;
7856 typval_T tvkey;
7857 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007858 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007859 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007861 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007862
7863 /*
7864 * First check if it's not a curly-braces thing: {expr}.
7865 * Must do this without evaluating, otherwise a function may be called
7866 * twice. Unfortunately this means we need to call eval1() twice for the
7867 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007869 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007870 if (*start != '}')
7871 {
7872 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7873 return FAIL;
7874 if (*start == '}')
7875 return NOTDONE;
7876 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007877
7878 if (evaluate)
7879 {
7880 d = dict_alloc();
7881 if (d == NULL)
7882 return FAIL;
7883 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007884 tvkey.v_type = VAR_UNKNOWN;
7885 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007886
7887 *arg = skipwhite(*arg + 1);
7888 while (**arg != '}' && **arg != NUL)
7889 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007890 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007891 goto failret;
7892 if (**arg != ':')
7893 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007894 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007895 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007896 goto failret;
7897 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007898 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007899 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007900 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007901 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007902 {
7903 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007904 clear_tv(&tvkey);
7905 goto failret;
7906 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007907 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007908
7909 *arg = skipwhite(*arg + 1);
7910 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7911 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007912 if (evaluate)
7913 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007914 goto failret;
7915 }
7916 if (evaluate)
7917 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007918 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007919 if (item != NULL)
7920 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007921 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007922 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923 clear_tv(&tv);
7924 goto failret;
7925 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007926 item = dictitem_alloc(key);
7927 clear_tv(&tvkey);
7928 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007930 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007931 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007932 if (dict_add(d, item) == FAIL)
7933 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 }
7935 }
7936
7937 if (**arg == '}')
7938 break;
7939 if (**arg != ',')
7940 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007941 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007942 goto failret;
7943 }
7944 *arg = skipwhite(*arg + 1);
7945 }
7946
7947 if (**arg != '}')
7948 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007949 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950failret:
7951 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007952 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 return FAIL;
7954 }
7955
7956 *arg = skipwhite(*arg + 1);
7957 if (evaluate)
7958 {
7959 rettv->v_type = VAR_DICT;
7960 rettv->vval.v_dict = d;
7961 ++d->dv_refcount;
7962 }
7963
7964 return OK;
7965}
7966
Bram Moolenaar17a13432016-01-24 14:22:10 +01007967 static char *
7968get_var_special_name(int nr)
7969{
7970 switch (nr)
7971 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007972 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007973 case VVAL_TRUE: return "v:true";
7974 case VVAL_NONE: return "v:none";
7975 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007976 }
7977 EMSG2(_(e_intern2), "get_var_special_name()");
7978 return "42";
7979}
7980
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007982 * Return a string with the string representation of a variable.
7983 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007984 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007985 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007986 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007987 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007988 */
7989 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007990echo_string(
7991 typval_T *tv,
7992 char_u **tofree,
7993 char_u *numbuf,
7994 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007995{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007996 static int recurse = 0;
7997 char_u *r = NULL;
7998
Bram Moolenaar33570922005-01-25 22:26:29 +00007999 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008000 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008001 if (!did_echo_string_emsg)
8002 {
8003 /* Only give this message once for a recursive call to avoid
8004 * flooding the user with errors. And stop iterating over lists
8005 * and dicts. */
8006 did_echo_string_emsg = TRUE;
8007 EMSG(_("E724: variable nested too deep for displaying"));
8008 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008009 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008010 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008011 }
8012 ++recurse;
8013
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008014 switch (tv->v_type)
8015 {
8016 case VAR_FUNC:
8017 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008018 r = tv->vval.v_string;
8019 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008020
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008021 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008022 {
8023 partial_T *pt = tv->vval.v_partial;
8024 char_u *fname = string_quote(pt == NULL ? NULL
8025 : pt->pt_name, FALSE);
8026 garray_T ga;
8027 int i;
8028 char_u *tf;
8029
8030 ga_init2(&ga, 1, 100);
8031 ga_concat(&ga, (char_u *)"function(");
8032 if (fname != NULL)
8033 {
8034 ga_concat(&ga, fname);
8035 vim_free(fname);
8036 }
8037 if (pt != NULL && pt->pt_argc > 0)
8038 {
8039 ga_concat(&ga, (char_u *)", [");
8040 for (i = 0; i < pt->pt_argc; ++i)
8041 {
8042 if (i > 0)
8043 ga_concat(&ga, (char_u *)", ");
8044 ga_concat(&ga,
8045 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8046 vim_free(tf);
8047 }
8048 ga_concat(&ga, (char_u *)"]");
8049 }
8050 if (pt != NULL && pt->pt_dict != NULL)
8051 {
8052 typval_T dtv;
8053
8054 ga_concat(&ga, (char_u *)", ");
8055 dtv.v_type = VAR_DICT;
8056 dtv.vval.v_dict = pt->pt_dict;
8057 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8058 vim_free(tf);
8059 }
8060 ga_concat(&ga, (char_u *)")");
8061
8062 *tofree = ga.ga_data;
8063 r = *tofree;
8064 break;
8065 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008066
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008067 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008068 if (tv->vval.v_list == NULL)
8069 {
8070 *tofree = NULL;
8071 r = NULL;
8072 }
8073 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8074 {
8075 *tofree = NULL;
8076 r = (char_u *)"[...]";
8077 }
8078 else
8079 {
8080 tv->vval.v_list->lv_copyID = copyID;
8081 *tofree = list2string(tv, copyID);
8082 r = *tofree;
8083 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008084 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008085
Bram Moolenaar8c711452005-01-14 21:53:12 +00008086 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008087 if (tv->vval.v_dict == NULL)
8088 {
8089 *tofree = NULL;
8090 r = NULL;
8091 }
8092 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8093 {
8094 *tofree = NULL;
8095 r = (char_u *)"{...}";
8096 }
8097 else
8098 {
8099 tv->vval.v_dict->dv_copyID = copyID;
8100 *tofree = dict2string(tv, copyID);
8101 r = *tofree;
8102 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008103 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008104
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008105 case VAR_STRING:
8106 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008107 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008108 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008109 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008110 *tofree = NULL;
8111 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008112 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008113
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008115#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008116 *tofree = NULL;
8117 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8118 r = numbuf;
8119 break;
8120#endif
8121
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008122 case VAR_SPECIAL:
8123 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008124 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008125 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008126 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008127
Bram Moolenaar8502c702014-06-17 12:51:16 +02008128 if (--recurse == 0)
8129 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008130 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008131}
8132
8133/*
8134 * Return a string with the string representation of a variable.
8135 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8136 * "numbuf" is used for a number.
8137 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008138 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008139 */
8140 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008141tv2string(
8142 typval_T *tv,
8143 char_u **tofree,
8144 char_u *numbuf,
8145 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008146{
8147 switch (tv->v_type)
8148 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008149 case VAR_FUNC:
8150 *tofree = string_quote(tv->vval.v_string, TRUE);
8151 return *tofree;
8152 case VAR_STRING:
8153 *tofree = string_quote(tv->vval.v_string, FALSE);
8154 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008155 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008156#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008157 *tofree = NULL;
8158 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8159 return numbuf;
8160#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008161 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008162 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008163 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008164 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008165 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008166 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008167 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008168 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008169 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008170 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008171 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008172}
8173
8174/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008175 * Return string "str" in ' quotes, doubling ' characters.
8176 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008177 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008178 */
8179 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008180string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008181{
Bram Moolenaar33570922005-01-25 22:26:29 +00008182 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008183 char_u *p, *r, *s;
8184
Bram Moolenaar33570922005-01-25 22:26:29 +00008185 len = (function ? 13 : 3);
8186 if (str != NULL)
8187 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008188 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008189 for (p = str; *p != NUL; mb_ptr_adv(p))
8190 if (*p == '\'')
8191 ++len;
8192 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008193 s = r = alloc(len);
8194 if (r != NULL)
8195 {
8196 if (function)
8197 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008198 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008199 r += 10;
8200 }
8201 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008202 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 if (str != NULL)
8204 for (p = str; *p != NUL; )
8205 {
8206 if (*p == '\'')
8207 *r++ = '\'';
8208 MB_COPY_CHAR(p, r);
8209 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008210 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008211 if (function)
8212 *r++ = ')';
8213 *r++ = NUL;
8214 }
8215 return s;
8216}
8217
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008218#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008219/*
8220 * Convert the string "text" to a floating point number.
8221 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8222 * this always uses a decimal point.
8223 * Returns the length of the text that was consumed.
8224 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008225 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008226string2float(
8227 char_u *text,
8228 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229{
8230 char *s = (char *)text;
8231 float_T f;
8232
8233 f = strtod(s, &s);
8234 *value = f;
8235 return (int)((char_u *)s - text);
8236}
8237#endif
8238
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 * Get the value of an environment variable.
8241 * "arg" is pointing to the '$'. It is advanced to after the name.
8242 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008243 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 */
8245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008246get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
8248 char_u *string = NULL;
8249 int len;
8250 int cc;
8251 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008252 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253
8254 ++*arg;
8255 name = *arg;
8256 len = get_env_len(arg);
8257 if (evaluate)
8258 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008259 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008260 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008261
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008262 cc = name[len];
8263 name[len] = NUL;
8264 /* first try vim_getenv(), fast for normal environment vars */
8265 string = vim_getenv(name, &mustfree);
8266 if (string != NULL && *string != NUL)
8267 {
8268 if (!mustfree)
8269 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008271 else
8272 {
8273 if (mustfree)
8274 vim_free(string);
8275
8276 /* next try expanding things like $VIM and ${HOME} */
8277 string = expand_env_save(name - 1);
8278 if (string != NULL && *string == '$')
8279 {
8280 vim_free(string);
8281 string = NULL;
8282 }
8283 }
8284 name[len] = cc;
8285
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008286 rettv->v_type = VAR_STRING;
8287 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 }
8289
8290 return OK;
8291}
8292
8293/*
8294 * Array with names and number of arguments of all internal functions
8295 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8296 */
8297static struct fst
8298{
8299 char *f_name; /* function name */
8300 char f_min_argc; /* minimal number of arguments */
8301 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008302 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008303 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304} functions[] =
8305{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008306#ifdef FEAT_FLOAT
8307 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008308 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008309#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008310 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008311 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008312 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {"append", 2, 2, f_append},
8314 {"argc", 0, 0, f_argc},
8315 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008316 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008317 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008318#ifdef FEAT_FLOAT
8319 {"asin", 1, 1, f_asin}, /* WJMc */
8320#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008321 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008322 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008323 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008324 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008325 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008326 {"assert_notequal", 2, 3, f_assert_notequal},
8327 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008328 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008329#ifdef FEAT_FLOAT
8330 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008331 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008334 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"bufexists", 1, 1, f_bufexists},
8336 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8337 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8338 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8339 {"buflisted", 1, 1, f_buflisted},
8340 {"bufloaded", 1, 1, f_bufloaded},
8341 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008342 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 {"bufwinnr", 1, 1, f_bufwinnr},
8344 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008345 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008346 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008347 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008348#ifdef FEAT_FLOAT
8349 {"ceil", 1, 1, f_ceil},
8350#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008351#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008352 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008353 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8354 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008355 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008356 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008357 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008358 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008359 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008360 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008361 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008362 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008363 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8364 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008365 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008366 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008367#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008368 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008369 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008371 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008373#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008374 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008375 {"complete_add", 1, 1, f_complete_add},
8376 {"complete_check", 0, 0, f_complete_check},
8377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008379 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008380#ifdef FEAT_FLOAT
8381 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008382 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008383#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008384 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008386 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008387 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008388 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008390 {"diff_filler", 1, 1, f_diff_filler},
8391 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008392 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008393 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008395 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"eventhandler", 0, 0, f_eventhandler},
8397 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008398 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008400#ifdef FEAT_FLOAT
8401 {"exp", 1, 1, f_exp},
8402#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008403 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008404 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008405 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8407 {"filereadable", 1, 1, f_filereadable},
8408 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008409 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008410 {"finddir", 1, 3, f_finddir},
8411 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008412#ifdef FEAT_FLOAT
8413 {"float2nr", 1, 1, f_float2nr},
8414 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008415 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008416#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008417 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {"fnamemodify", 2, 2, f_fnamemodify},
8419 {"foldclosed", 1, 1, f_foldclosed},
8420 {"foldclosedend", 1, 1, f_foldclosedend},
8421 {"foldlevel", 1, 1, f_foldlevel},
8422 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008423 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008425 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008426 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008427 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008428 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008429 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"getchar", 0, 1, f_getchar},
8431 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008432 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"getcmdline", 0, 0, f_getcmdline},
8434 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008435 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008436 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008437 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008438 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008439 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008440 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 {"getfsize", 1, 1, f_getfsize},
8442 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008443 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008444 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008445 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008446 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008447 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008448 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008449 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008450 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008452 {"gettabvar", 2, 3, f_gettabvar},
8453 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {"getwinposx", 0, 0, f_getwinposx},
8455 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008456 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008457 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008458 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008459 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008461 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008462 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008463 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8465 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8466 {"histadd", 2, 2, f_histadd},
8467 {"histdel", 1, 2, f_histdel},
8468 {"histget", 1, 2, f_histget},
8469 {"histnr", 1, 1, f_histnr},
8470 {"hlID", 1, 1, f_hlID},
8471 {"hlexists", 1, 1, f_hlexists},
8472 {"hostname", 0, 0, f_hostname},
8473 {"iconv", 3, 3, f_iconv},
8474 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008475 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008476 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008478 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"inputrestore", 0, 0, f_inputrestore},
8480 {"inputsave", 0, 0, f_inputsave},
8481 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008482 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008483 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008485 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008486#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8487 {"isnan", 1, 1, f_isnan},
8488#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008489 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008490#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008491 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008492 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008493 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008494 {"job_start", 1, 2, f_job_start},
8495 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008496 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008497#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008498 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008499 {"js_decode", 1, 1, f_js_decode},
8500 {"js_encode", 1, 1, f_js_encode},
8501 {"json_decode", 1, 1, f_json_decode},
8502 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008503 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008505 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"libcall", 3, 3, f_libcall},
8507 {"libcallnr", 3, 3, f_libcallnr},
8508 {"line", 1, 1, f_line},
8509 {"line2byte", 1, 1, f_line2byte},
8510 {"lispindent", 1, 1, f_lispindent},
8511 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008512#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008513 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008514 {"log10", 1, 1, f_log10},
8515#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008516#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008517 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008518#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008519 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008520 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008521 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008522 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008523 {"matchadd", 2, 5, f_matchadd},
8524 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008525 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008526 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008527 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008528 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008529 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008530 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008531 {"max", 1, 1, f_max},
8532 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008533#ifdef vim_mkdir
8534 {"mkdir", 1, 3, f_mkdir},
8535#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008536 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008537#ifdef FEAT_MZSCHEME
8538 {"mzeval", 1, 1, f_mzeval},
8539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008541 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008542 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008543 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008544#ifdef FEAT_PERL
8545 {"perleval", 1, 1, f_perleval},
8546#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008547#ifdef FEAT_FLOAT
8548 {"pow", 2, 2, f_pow},
8549#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008551 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008552 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008553#ifdef FEAT_PYTHON3
8554 {"py3eval", 1, 1, f_py3eval},
8555#endif
8556#ifdef FEAT_PYTHON
8557 {"pyeval", 1, 1, f_pyeval},
8558#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008559 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008560 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008561 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008562#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008563 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008564#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008565 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 {"remote_expr", 2, 3, f_remote_expr},
8567 {"remote_foreground", 1, 1, f_remote_foreground},
8568 {"remote_peek", 1, 2, f_remote_peek},
8569 {"remote_read", 1, 1, f_remote_read},
8570 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008571 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008573 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008575 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008576#ifdef FEAT_FLOAT
8577 {"round", 1, 1, f_round},
8578#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008579 {"screenattr", 2, 2, f_screenattr},
8580 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008581 {"screencol", 0, 0, f_screencol},
8582 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008583 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008584 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008585 {"searchpair", 3, 7, f_searchpair},
8586 {"searchpairpos", 3, 7, f_searchpairpos},
8587 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 {"server2client", 2, 2, f_server2client},
8589 {"serverlist", 0, 0, f_serverlist},
8590 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008591 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008593 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008595 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008596 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008597 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008598 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008600 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008601 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008603#ifdef FEAT_CRYPT
8604 {"sha256", 1, 1, f_sha256},
8605#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008606 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008607 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008609#ifdef FEAT_FLOAT
8610 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008611 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008612#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008613 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008614 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008615 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008616 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008617 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008618#ifdef FEAT_FLOAT
8619 {"sqrt", 1, 1, f_sqrt},
8620 {"str2float", 1, 1, f_str2float},
8621#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008622 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008623 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008624 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625#ifdef HAVE_STRFTIME
8626 {"strftime", 1, 2, f_strftime},
8627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008628 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008629 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"strlen", 1, 1, f_strlen},
8631 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008632 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008634 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008635 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {"substitute", 4, 4, f_substitute},
8637 {"synID", 3, 3, f_synID},
8638 {"synIDattr", 2, 3, f_synIDattr},
8639 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008640 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008641 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008642 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008643 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008644 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008645 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008646 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008647 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008648 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008649#ifdef FEAT_FLOAT
8650 {"tan", 1, 1, f_tan},
8651 {"tanh", 1, 1, f_tanh},
8652#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008654 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008655#ifdef FEAT_TIMERS
8656 {"timer_start", 2, 3, f_timer_start},
8657 {"timer_stop", 1, 1, f_timer_stop},
8658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 {"tolower", 1, 1, f_tolower},
8660 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008661 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008662#ifdef FEAT_FLOAT
8663 {"trunc", 1, 1, f_trunc},
8664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008666 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008667 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008668 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008669 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 {"virtcol", 1, 1, f_virtcol},
8671 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008672 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008673 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008674 {"win_getid", 0, 2, f_win_getid},
8675 {"win_gotoid", 1, 1, f_win_gotoid},
8676 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8677 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 {"winbufnr", 1, 1, f_winbufnr},
8679 {"wincol", 0, 0, f_wincol},
8680 {"winheight", 1, 1, f_winheight},
8681 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008682 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008684 {"winrestview", 1, 1, f_winrestview},
8685 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008687 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008688 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008689 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690};
8691
8692#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8693
8694/*
8695 * Function given to ExpandGeneric() to obtain the list of internal
8696 * or user defined function names.
8697 */
8698 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008699get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701 static int intidx = -1;
8702 char_u *name;
8703
8704 if (idx == 0)
8705 intidx = -1;
8706 if (intidx < 0)
8707 {
8708 name = get_user_func_name(xp, idx);
8709 if (name != NULL)
8710 return name;
8711 }
8712 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8713 {
8714 STRCPY(IObuff, functions[intidx].f_name);
8715 STRCAT(IObuff, "(");
8716 if (functions[intidx].f_max_argc == 0)
8717 STRCAT(IObuff, ")");
8718 return IObuff;
8719 }
8720
8721 return NULL;
8722}
8723
8724/*
8725 * Function given to ExpandGeneric() to obtain the list of internal or
8726 * user defined variable or function names.
8727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008729get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 static int intidx = -1;
8732 char_u *name;
8733
8734 if (idx == 0)
8735 intidx = -1;
8736 if (intidx < 0)
8737 {
8738 name = get_function_name(xp, idx);
8739 if (name != NULL)
8740 return name;
8741 }
8742 return get_user_var_name(xp, ++intidx);
8743}
8744
8745#endif /* FEAT_CMDL_COMPL */
8746
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008747#if defined(EBCDIC) || defined(PROTO)
8748/*
8749 * Compare struct fst by function name.
8750 */
8751 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008752compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008753{
8754 struct fst *p1 = (struct fst *)s1;
8755 struct fst *p2 = (struct fst *)s2;
8756
8757 return STRCMP(p1->f_name, p2->f_name);
8758}
8759
8760/*
8761 * Sort the function table by function name.
8762 * The sorting of the table above is ASCII dependant.
8763 * On machines using EBCDIC we have to sort it.
8764 */
8765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008766sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008767{
8768 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8769
8770 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8771}
8772#endif
8773
8774
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775/*
8776 * Find internal function in table above.
8777 * Return index, or -1 if not found
8778 */
8779 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008780find_internal_func(
8781 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008782{
8783 int first = 0;
8784 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8785 int cmp;
8786 int x;
8787
8788 /*
8789 * Find the function name in the table. Binary search.
8790 */
8791 while (first <= last)
8792 {
8793 x = first + ((unsigned)(last - first) >> 1);
8794 cmp = STRCMP(name, functions[x].f_name);
8795 if (cmp < 0)
8796 last = x - 1;
8797 else if (cmp > 0)
8798 first = x + 1;
8799 else
8800 return x;
8801 }
8802 return -1;
8803}
8804
8805/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008806 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8807 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008808 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8809 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008810 */
8811 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008812deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008813{
Bram Moolenaar33570922005-01-25 22:26:29 +00008814 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008815 int cc;
8816
Bram Moolenaar65639032016-03-16 21:40:30 +01008817 if (partialp != NULL)
8818 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008820 cc = name[*lenp];
8821 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008822 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008823 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008824 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008825 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008826 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008827 {
8828 *lenp = 0;
8829 return (char_u *)""; /* just in case */
8830 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008831 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008833 }
8834
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008835 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8836 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008837 partial_T *pt = v->di_tv.vval.v_partial;
8838
8839 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008840 {
8841 *lenp = 0;
8842 return (char_u *)""; /* just in case */
8843 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008844 if (partialp != NULL)
8845 *partialp = pt;
8846 *lenp = (int)STRLEN(pt->pt_name);
8847 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008848 }
8849
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008850 return name;
8851}
8852
8853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 * Allocate a variable for the result of a function.
8855 * Return OK or FAIL.
8856 */
8857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008858get_func_tv(
8859 char_u *name, /* name of the function */
8860 int len, /* length of "name" */
8861 typval_T *rettv,
8862 char_u **arg, /* argument, pointing to the '(' */
8863 linenr_T firstline, /* first line of range */
8864 linenr_T lastline, /* last line of range */
8865 int *doesrange, /* return: function handled range */
8866 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008867 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008868 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
8870 char_u *argp;
8871 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008872 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 int argcount = 0; /* number of arguments found */
8874
8875 /*
8876 * Get the arguments.
8877 */
8878 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008879 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 {
8881 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8882 if (*argp == ')' || *argp == ',' || *argp == NUL)
8883 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8885 {
8886 ret = FAIL;
8887 break;
8888 }
8889 ++argcount;
8890 if (*argp != ',')
8891 break;
8892 }
8893 if (*argp == ')')
8894 ++argp;
8895 else
8896 ret = FAIL;
8897
8898 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008900 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 {
8903 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008904 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008905 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008906 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
8909 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008910 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911
8912 *arg = skipwhite(argp);
8913 return ret;
8914}
8915
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008916#define ERROR_UNKNOWN 0
8917#define ERROR_TOOMANY 1
8918#define ERROR_TOOFEW 2
8919#define ERROR_SCRIPT 3
8920#define ERROR_DICT 4
8921#define ERROR_NONE 5
8922#define ERROR_OTHER 6
8923#define FLEN_FIXED 40
8924
8925/*
8926 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8927 * Change <SNR>123_name() to K_SNR 123_name().
8928 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8929 * (slow).
8930 */
8931 static char_u *
8932fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8933{
8934 int llen;
8935 char_u *fname;
8936 int i;
8937
8938 llen = eval_fname_script(name);
8939 if (llen > 0)
8940 {
8941 fname_buf[0] = K_SPECIAL;
8942 fname_buf[1] = KS_EXTRA;
8943 fname_buf[2] = (int)KE_SNR;
8944 i = 3;
8945 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8946 {
8947 if (current_SID <= 0)
8948 *error = ERROR_SCRIPT;
8949 else
8950 {
8951 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8952 i = (int)STRLEN(fname_buf);
8953 }
8954 }
8955 if (i + STRLEN(name + llen) < FLEN_FIXED)
8956 {
8957 STRCPY(fname_buf + i, name + llen);
8958 fname = fname_buf;
8959 }
8960 else
8961 {
8962 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8963 if (fname == NULL)
8964 *error = ERROR_OTHER;
8965 else
8966 {
8967 *tofree = fname;
8968 mch_memmove(fname, fname_buf, (size_t)i);
8969 STRCPY(fname + i, name + llen);
8970 }
8971 }
8972 }
8973 else
8974 fname = name;
8975 return fname;
8976}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
8978/*
8979 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008980 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008981 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008983 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008984call_func(
8985 char_u *funcname, /* name of the function */
8986 int len, /* length of "name" */
8987 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008988 int argcount_in, /* number of "argvars" */
8989 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008990 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008991 linenr_T firstline, /* first line of range */
8992 linenr_T lastline, /* last line of range */
8993 int *doesrange, /* return: function handled range */
8994 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008995 partial_T *partial, /* optional, can be NULL */
8996 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
8998 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 int error = ERROR_NONE;
9000 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009003 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009005 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009006 int argcount = argcount_in;
9007 typval_T *argvars = argvars_in;
9008 dict_T *selfdict = selfdict_in;
9009 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9010 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009011
9012 /* Make a copy of the name, if it comes from a funcref variable it could
9013 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009014 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009015 if (name == NULL)
9016 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009018 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
9020 *doesrange = FALSE;
9021
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009022 if (partial != NULL)
9023 {
9024 if (partial->pt_dict != NULL)
9025 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009026 /* When the function has a partial with a dict and there is a dict
9027 * argument, use the dict argument. That is backwards compatible.
9028 */
9029 if (selfdict_in == NULL)
9030 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009031 }
9032 if (error == ERROR_NONE && partial->pt_argc > 0)
9033 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009034 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9035 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9036 for (i = 0; i < argcount_in; ++i)
9037 argv[i + argv_clear] = argvars_in[i];
9038 argvars = argv;
9039 argcount = partial->pt_argc + argcount_in;
9040 }
9041 }
9042
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
9044 /* execute the function if no errors detected and executing */
9045 if (evaluate && error == ERROR_NONE)
9046 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009047 char_u *rfname = fname;
9048
9049 /* Ignore "g:" before a function name. */
9050 if (fname[0] == 'g' && fname[1] == ':')
9051 rfname = fname + 2;
9052
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009053 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9054 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 error = ERROR_UNKNOWN;
9056
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009057 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 {
9059 /*
9060 * User defined function.
9061 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009062 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009063
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009065 /* Trigger FuncUndefined event, may load the function. */
9066 if (fp == NULL
9067 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009068 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009069 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009071 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009072 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 }
9074#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009075 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009076 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009077 {
9078 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009079 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009080 }
9081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 if (fp != NULL)
9083 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009084 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009086 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009088 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009090 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009091 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 else
9093 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009094 int did_save_redo = FALSE;
9095
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 /*
9097 * Call the user function.
9098 * Save and restore search patterns, script variables and
9099 * redo buffer.
9100 */
9101 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009102#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009103 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009104#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009105 {
9106 saveRedobuff();
9107 did_save_redo = TRUE;
9108 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009109 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009111 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009112 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9113 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9114 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009115 /* Function was unreferenced while being used, free it
9116 * now. */
9117 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009118 if (did_save_redo)
9119 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 restore_search_patterns();
9121 error = ERROR_NONE;
9122 }
9123 }
9124 }
9125 else
9126 {
9127 /*
9128 * Find the function name in the table, call its implementation.
9129 */
9130 i = find_internal_func(fname);
9131 if (i >= 0)
9132 {
9133 if (argcount < functions[i].f_min_argc)
9134 error = ERROR_TOOFEW;
9135 else if (argcount > functions[i].f_max_argc)
9136 error = ERROR_TOOMANY;
9137 else
9138 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009139 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 error = ERROR_NONE;
9142 }
9143 }
9144 }
9145 /*
9146 * The function call (or "FuncUndefined" autocommand sequence) might
9147 * have been aborted by an error, an interrupt, or an explicitly thrown
9148 * exception that has not been caught so far. This situation can be
9149 * tested for by calling aborting(). For an error in an internal
9150 * function or for the "E132" error in call_user_func(), however, the
9151 * throw point at which the "force_abort" flag (temporarily reset by
9152 * emsg()) is normally updated has not been reached yet. We need to
9153 * update that flag first to make aborting() reliable.
9154 */
9155 update_force_abort();
9156 }
9157 if (error == ERROR_NONE)
9158 ret = OK;
9159
9160 /*
9161 * Report an error unless the argument evaluation or function call has been
9162 * cancelled due to an aborting error, an interrupt, or an exception.
9163 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009164 if (!aborting())
9165 {
9166 switch (error)
9167 {
9168 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009169 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009170 break;
9171 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009172 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009173 break;
9174 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009175 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009176 name);
9177 break;
9178 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009179 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009180 name);
9181 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009182 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009183 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009184 name);
9185 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009186 }
9187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009189 while (argv_clear > 0)
9190 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009191 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009192 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193
9194 return ret;
9195}
9196
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009197/*
9198 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009199 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009200 */
9201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009202emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009203{
9204 char_u *p;
9205
9206 if (*name == K_SPECIAL)
9207 p = concat_str((char_u *)"<SNR>", name + 3);
9208 else
9209 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009210 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009211 if (p != name)
9212 vim_free(p);
9213}
9214
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009215/*
9216 * Return TRUE for a non-zero Number and a non-empty String.
9217 */
9218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009219non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009220{
9221 return ((argvars[0].v_type == VAR_NUMBER
9222 && argvars[0].vval.v_number != 0)
9223 || (argvars[0].v_type == VAR_STRING
9224 && argvars[0].vval.v_string != NULL
9225 && *argvars[0].vval.v_string != NUL));
9226}
9227
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228/*********************************************
9229 * Implementation of the built-in functions
9230 */
9231
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009232#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009233static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009234
9235/*
9236 * Get the float value of "argvars[0]" into "f".
9237 * Returns FAIL when the argument is not a Number or Float.
9238 */
9239 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009240get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009241{
9242 if (argvars[0].v_type == VAR_FLOAT)
9243 {
9244 *f = argvars[0].vval.v_float;
9245 return OK;
9246 }
9247 if (argvars[0].v_type == VAR_NUMBER)
9248 {
9249 *f = (float_T)argvars[0].vval.v_number;
9250 return OK;
9251 }
9252 EMSG(_("E808: Number or Float required"));
9253 return FAIL;
9254}
9255
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009256/*
9257 * "abs(expr)" function
9258 */
9259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009260f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009261{
9262 if (argvars[0].v_type == VAR_FLOAT)
9263 {
9264 rettv->v_type = VAR_FLOAT;
9265 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9266 }
9267 else
9268 {
9269 varnumber_T n;
9270 int error = FALSE;
9271
9272 n = get_tv_number_chk(&argvars[0], &error);
9273 if (error)
9274 rettv->vval.v_number = -1;
9275 else if (n > 0)
9276 rettv->vval.v_number = n;
9277 else
9278 rettv->vval.v_number = -n;
9279 }
9280}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009281
9282/*
9283 * "acos()" function
9284 */
9285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009286f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009287{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009288 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009289
9290 rettv->v_type = VAR_FLOAT;
9291 if (get_float_arg(argvars, &f) == OK)
9292 rettv->vval.v_float = acos(f);
9293 else
9294 rettv->vval.v_float = 0.0;
9295}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009296#endif
9297
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009299 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 */
9301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009302f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303{
Bram Moolenaar33570922005-01-25 22:26:29 +00009304 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009307 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009309 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009310 && !tv_check_lock(l->lv_lock,
9311 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009312 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009313 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009314 }
9315 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009316 EMSG(_(e_listreq));
9317}
9318
9319/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009320 * "alloc_fail(id, countdown, repeat)" function
9321 */
9322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009323f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009324{
9325 if (argvars[0].v_type != VAR_NUMBER
9326 || argvars[0].vval.v_number <= 0
9327 || argvars[1].v_type != VAR_NUMBER
9328 || argvars[1].vval.v_number < 0
9329 || argvars[2].v_type != VAR_NUMBER)
9330 EMSG(_(e_invarg));
9331 else
9332 {
9333 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009334 if (alloc_fail_id >= aid_last)
9335 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009336 alloc_fail_countdown = argvars[1].vval.v_number;
9337 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009338 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009339 }
9340}
9341
9342/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009343 * "and(expr, expr)" function
9344 */
9345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009346f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009347{
9348 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9349 & get_tv_number_chk(&argvars[1], NULL);
9350}
9351
9352/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009353 * "append(lnum, string/list)" function
9354 */
9355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009356f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009357{
9358 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009359 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009360 list_T *l = NULL;
9361 listitem_T *li = NULL;
9362 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009363 long added = 0;
9364
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009365 /* When coming here from Insert mode, sync undo, so that this can be
9366 * undone separately from what was previously inserted. */
9367 if (u_sync_once == 2)
9368 {
9369 u_sync_once = 1; /* notify that u_sync() was called */
9370 u_sync(TRUE);
9371 }
9372
Bram Moolenaar0d660222005-01-07 21:51:51 +00009373 lnum = get_tv_lnum(argvars);
9374 if (lnum >= 0
9375 && lnum <= curbuf->b_ml.ml_line_count
9376 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009377 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009378 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009379 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009380 l = argvars[1].vval.v_list;
9381 if (l == NULL)
9382 return;
9383 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009384 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009385 for (;;)
9386 {
9387 if (l == NULL)
9388 tv = &argvars[1]; /* append a string */
9389 else if (li == NULL)
9390 break; /* end of list */
9391 else
9392 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009393 line = get_tv_string_chk(tv);
9394 if (line == NULL) /* type error */
9395 {
9396 rettv->vval.v_number = 1; /* Failed */
9397 break;
9398 }
9399 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009400 ++added;
9401 if (l == NULL)
9402 break;
9403 li = li->li_next;
9404 }
9405
9406 appended_lines_mark(lnum, added);
9407 if (curwin->w_cursor.lnum > lnum)
9408 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009410 else
9411 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412}
9413
9414/*
9415 * "argc()" function
9416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009418f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421}
9422
9423/*
9424 * "argidx()" function
9425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009427f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009429 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430}
9431
9432/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009433 * "arglistid()" function
9434 */
9435 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009436f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009437{
9438 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009439
9440 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009441 wp = find_tabwin(&argvars[0], &argvars[1]);
9442 if (wp != NULL)
9443 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009444}
9445
9446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 * "argv(nr)" function
9448 */
9449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009450f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451{
9452 int idx;
9453
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009454 if (argvars[0].v_type != VAR_UNKNOWN)
9455 {
9456 idx = get_tv_number_chk(&argvars[0], NULL);
9457 if (idx >= 0 && idx < ARGCOUNT)
9458 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9459 else
9460 rettv->vval.v_string = NULL;
9461 rettv->v_type = VAR_STRING;
9462 }
9463 else if (rettv_list_alloc(rettv) == OK)
9464 for (idx = 0; idx < ARGCOUNT; ++idx)
9465 list_append_string(rettv->vval.v_list,
9466 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467}
9468
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009469typedef enum
9470{
9471 ASSERT_EQUAL,
9472 ASSERT_NOTEQUAL,
9473 ASSERT_MATCH,
9474 ASSERT_NOTMATCH,
9475 ASSERT_OTHER,
9476} assert_type_T;
9477
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009478static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009479static 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 +01009480static void assert_error(garray_T *gap);
9481static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009482
9483/*
9484 * Prepare "gap" for an assert error and add the sourcing position.
9485 */
9486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009488{
9489 char buf[NUMBUFLEN];
9490
9491 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009492 if (sourcing_name != NULL)
9493 {
9494 ga_concat(gap, sourcing_name);
9495 if (sourcing_lnum > 0)
9496 ga_concat(gap, (char_u *)" ");
9497 }
9498 if (sourcing_lnum > 0)
9499 {
9500 sprintf(buf, "line %ld", (long)sourcing_lnum);
9501 ga_concat(gap, (char_u *)buf);
9502 }
9503 if (sourcing_name != NULL || sourcing_lnum > 0)
9504 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009505}
9506
9507/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009508 * Append "str" to "gap", escaping unprintable characters.
9509 * Changes NL to \n, CR to \r, etc.
9510 */
9511 static void
9512ga_concat_esc(garray_T *gap, char_u *str)
9513{
9514 char_u *p;
9515 char_u buf[NUMBUFLEN];
9516
Bram Moolenaarf1551962016-03-15 12:55:58 +01009517 if (str == NULL)
9518 {
9519 ga_concat(gap, (char_u *)"NULL");
9520 return;
9521 }
9522
Bram Moolenaar23689172016-02-15 22:37:37 +01009523 for (p = str; *p != NUL; ++p)
9524 switch (*p)
9525 {
9526 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9527 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9528 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9529 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9530 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9531 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9532 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9533 default:
9534 if (*p < ' ')
9535 {
9536 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9537 ga_concat(gap, buf);
9538 }
9539 else
9540 ga_append(gap, *p);
9541 break;
9542 }
9543}
9544
9545/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009546 * Fill "gap" with information about an assert error.
9547 */
9548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009549fill_assert_error(
9550 garray_T *gap,
9551 typval_T *opt_msg_tv,
9552 char_u *exp_str,
9553 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009554 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009555 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009556{
9557 char_u numbuf[NUMBUFLEN];
9558 char_u *tofree;
9559
9560 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9561 {
9562 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9563 vim_free(tofree);
9564 }
9565 else
9566 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009567 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009568 ga_concat(gap, (char_u *)"Pattern ");
9569 else
9570 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009571 if (exp_str == NULL)
9572 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009573 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009574 vim_free(tofree);
9575 }
9576 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009577 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009578 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009579 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009580 else if (atype == ASSERT_NOTMATCH)
9581 ga_concat(gap, (char_u *)" does match ");
9582 else if (atype == ASSERT_NOTEQUAL)
9583 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009584 else
9585 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009586 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009587 vim_free(tofree);
9588 }
9589}
Bram Moolenaar43345542015-11-29 17:35:35 +01009590
9591/*
9592 * Add an assert error to v:errors.
9593 */
9594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009595assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009596{
9597 struct vimvar *vp = &vimvars[VV_ERRORS];
9598
9599 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9600 /* Make sure v:errors is a list. */
9601 set_vim_var_list(VV_ERRORS, list_alloc());
9602 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9603}
9604
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009605 static void
9606assert_equal_common(typval_T *argvars, assert_type_T atype)
9607{
9608 garray_T ga;
9609
9610 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9611 != (atype == ASSERT_EQUAL))
9612 {
9613 prepare_assert_error(&ga);
9614 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9615 atype);
9616 assert_error(&ga);
9617 ga_clear(&ga);
9618 }
9619}
9620
Bram Moolenaar43345542015-11-29 17:35:35 +01009621/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009622 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009623 */
9624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009625f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009626{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009627 assert_equal_common(argvars, ASSERT_EQUAL);
9628}
Bram Moolenaar43345542015-11-29 17:35:35 +01009629
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009630/*
9631 * "assert_notequal(expected, actual[, msg])" function
9632 */
9633 static void
9634f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9635{
9636 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009637}
9638
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009639/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009640 * "assert_exception(string[, msg])" function
9641 */
9642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009643f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009644{
9645 garray_T ga;
9646 char *error;
9647
9648 error = (char *)get_tv_string_chk(&argvars[0]);
9649 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9650 {
9651 prepare_assert_error(&ga);
9652 ga_concat(&ga, (char_u *)"v:exception is not set");
9653 assert_error(&ga);
9654 ga_clear(&ga);
9655 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009656 else if (error != NULL
9657 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009658 {
9659 prepare_assert_error(&ga);
9660 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009661 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009662 assert_error(&ga);
9663 ga_clear(&ga);
9664 }
9665}
9666
9667/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009668 * "assert_fails(cmd [, error])" function
9669 */
9670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009671f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009672{
9673 char_u *cmd = get_tv_string_chk(&argvars[0]);
9674 garray_T ga;
9675
9676 called_emsg = FALSE;
9677 suppress_errthrow = TRUE;
9678 emsg_silent = TRUE;
9679 do_cmdline_cmd(cmd);
9680 if (!called_emsg)
9681 {
9682 prepare_assert_error(&ga);
9683 ga_concat(&ga, (char_u *)"command did not fail: ");
9684 ga_concat(&ga, cmd);
9685 assert_error(&ga);
9686 ga_clear(&ga);
9687 }
9688 else if (argvars[1].v_type != VAR_UNKNOWN)
9689 {
9690 char_u buf[NUMBUFLEN];
9691 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9692
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009693 if (error == NULL
9694 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009695 {
9696 prepare_assert_error(&ga);
9697 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009698 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009699 assert_error(&ga);
9700 ga_clear(&ga);
9701 }
9702 }
9703
9704 called_emsg = FALSE;
9705 suppress_errthrow = FALSE;
9706 emsg_silent = FALSE;
9707 emsg_on_display = FALSE;
9708 set_vim_var_string(VV_ERRMSG, NULL, 0);
9709}
9710
9711/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009712 * Common for assert_true() and assert_false().
9713 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009715assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009716{
9717 int error = FALSE;
9718 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009719
Bram Moolenaar37127922016-02-06 20:29:28 +01009720 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009721 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009722 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009723 if (argvars[0].v_type != VAR_NUMBER
9724 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9725 || error)
9726 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009727 prepare_assert_error(&ga);
9728 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009729 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009730 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009731 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009732 ga_clear(&ga);
9733 }
9734}
9735
9736/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009737 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009738 */
9739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009740f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009741{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009742 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009743}
9744
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009745 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009746assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009747{
9748 garray_T ga;
9749 char_u buf1[NUMBUFLEN];
9750 char_u buf2[NUMBUFLEN];
9751 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9752 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9753
Bram Moolenaar72188e92016-03-28 22:48:29 +02009754 if (pat == NULL || text == NULL)
9755 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009756 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009757 {
9758 prepare_assert_error(&ga);
9759 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009760 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009761 assert_error(&ga);
9762 ga_clear(&ga);
9763 }
9764}
9765
9766/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009767 * "assert_match(pattern, actual[, msg])" function
9768 */
9769 static void
9770f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9771{
9772 assert_match_common(argvars, ASSERT_MATCH);
9773}
9774
9775/*
9776 * "assert_notmatch(pattern, actual[, msg])" function
9777 */
9778 static void
9779f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9780{
9781 assert_match_common(argvars, ASSERT_NOTMATCH);
9782}
9783
9784/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009785 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009786 */
9787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009788f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009789{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009790 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009791}
9792
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009793#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009794/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009795 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009796 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009798f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009799{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009800 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009801
9802 rettv->v_type = VAR_FLOAT;
9803 if (get_float_arg(argvars, &f) == OK)
9804 rettv->vval.v_float = asin(f);
9805 else
9806 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009807}
9808
9809/*
9810 * "atan()" function
9811 */
9812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009813f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009814{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009815 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009816
9817 rettv->v_type = VAR_FLOAT;
9818 if (get_float_arg(argvars, &f) == OK)
9819 rettv->vval.v_float = atan(f);
9820 else
9821 rettv->vval.v_float = 0.0;
9822}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009823
9824/*
9825 * "atan2()" function
9826 */
9827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009828f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009829{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009830 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009831
9832 rettv->v_type = VAR_FLOAT;
9833 if (get_float_arg(argvars, &fx) == OK
9834 && get_float_arg(&argvars[1], &fy) == OK)
9835 rettv->vval.v_float = atan2(fx, fy);
9836 else
9837 rettv->vval.v_float = 0.0;
9838}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009839#endif
9840
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841/*
9842 * "browse(save, title, initdir, default)" function
9843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846{
9847#ifdef FEAT_BROWSE
9848 int save;
9849 char_u *title;
9850 char_u *initdir;
9851 char_u *defname;
9852 char_u buf[NUMBUFLEN];
9853 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009854 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009856 save = get_tv_number_chk(&argvars[0], &error);
9857 title = get_tv_string_chk(&argvars[1]);
9858 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9859 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009861 if (error || title == NULL || initdir == NULL || defname == NULL)
9862 rettv->vval.v_string = NULL;
9863 else
9864 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009865 do_browse(save ? BROWSE_SAVE : 0,
9866 title, defname, NULL, initdir, NULL, curbuf);
9867#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009868 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009869#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009871}
9872
9873/*
9874 * "browsedir(title, initdir)" function
9875 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009877f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009878{
9879#ifdef FEAT_BROWSE
9880 char_u *title;
9881 char_u *initdir;
9882 char_u buf[NUMBUFLEN];
9883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 title = get_tv_string_chk(&argvars[0]);
9885 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009887 if (title == NULL || initdir == NULL)
9888 rettv->vval.v_string = NULL;
9889 else
9890 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009891 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009893 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009895 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896}
9897
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009898static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009899
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900/*
9901 * Find a buffer by number or exact name.
9902 */
9903 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009904find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905{
9906 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009908 if (avar->v_type == VAR_NUMBER)
9909 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009910 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009912 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009913 if (buf == NULL)
9914 {
9915 /* No full path name match, try a match with a URL or a "nofile"
9916 * buffer, these don't use the full path. */
9917 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9918 if (buf->b_fname != NULL
9919 && (path_with_url(buf->b_fname)
9920#ifdef FEAT_QUICKFIX
9921 || bt_nofile(buf)
9922#endif
9923 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009924 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009925 break;
9926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 }
9928 return buf;
9929}
9930
9931/*
9932 * "bufexists(expr)" function
9933 */
9934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009935f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938}
9939
9940/*
9941 * "buflisted(expr)" function
9942 */
9943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009944f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945{
9946 buf_T *buf;
9947
9948 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009949 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950}
9951
9952/*
9953 * "bufloaded(expr)" function
9954 */
9955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009956f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957{
9958 buf_T *buf;
9959
9960 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962}
9963
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009964 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009965buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 int save_magic;
9968 char_u *save_cpo;
9969 buf_T *buf;
9970
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9972 save_magic = p_magic;
9973 p_magic = TRUE;
9974 save_cpo = p_cpo;
9975 p_cpo = (char_u *)"";
9976
9977 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009978 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979
9980 p_magic = save_magic;
9981 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009982 return buf;
9983}
9984
9985/*
9986 * Get buffer by number or pattern.
9987 */
9988 static buf_T *
9989get_buf_tv(typval_T *tv, int curtab_only)
9990{
9991 char_u *name = tv->vval.v_string;
9992 buf_T *buf;
9993
9994 if (tv->v_type == VAR_NUMBER)
9995 return buflist_findnr((int)tv->vval.v_number);
9996 if (tv->v_type != VAR_STRING)
9997 return NULL;
9998 if (name == NULL || *name == NUL)
9999 return curbuf;
10000 if (name[0] == '$' && name[1] == NUL)
10001 return lastbuf;
10002
10003 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004
10005 /* If not found, try expanding the name, like done for bufexists(). */
10006 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008
10009 return buf;
10010}
10011
10012/*
10013 * "bufname(expr)" function
10014 */
10015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010016f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017{
10018 buf_T *buf;
10019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010020 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010022 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 --emsg_off;
10029}
10030
10031/*
10032 * "bufnr(expr)" function
10033 */
10034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010035f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036{
10037 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010038 int error = FALSE;
10039 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010041 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010043 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010044 --emsg_off;
10045
10046 /* If the buffer isn't found and the second argument is not zero create a
10047 * new buffer. */
10048 if (buf == NULL
10049 && argvars[1].v_type != VAR_UNKNOWN
10050 && get_tv_number_chk(&argvars[1], &error) != 0
10051 && !error
10052 && (name = get_tv_string_chk(&argvars[0])) != NULL
10053 && !error)
10054 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10055
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010057 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060}
10061
10062/*
10063 * "bufwinnr(nr)" function
10064 */
10065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010066f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067{
10068#ifdef FEAT_WINDOWS
10069 win_T *wp;
10070 int winnr = 0;
10071#endif
10072 buf_T *buf;
10073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010074 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010076 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077#ifdef FEAT_WINDOWS
10078 for (wp = firstwin; wp; wp = wp->w_next)
10079 {
10080 ++winnr;
10081 if (wp->w_buffer == buf)
10082 break;
10083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010084 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010086 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087#endif
10088 --emsg_off;
10089}
10090
10091/*
10092 * "byte2line(byte)" function
10093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010095f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096{
10097#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099#else
10100 long boff = 0;
10101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010102 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010104 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010106 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 (linenr_T)0, &boff);
10108#endif
10109}
10110
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010112byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010113{
10114#ifdef FEAT_MBYTE
10115 char_u *t;
10116#endif
10117 char_u *str;
10118 long idx;
10119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010120 str = get_tv_string_chk(&argvars[0]);
10121 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010124 return;
10125
10126#ifdef FEAT_MBYTE
10127 t = str;
10128 for ( ; idx > 0; idx--)
10129 {
10130 if (*t == NUL) /* EOL reached */
10131 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010132 if (enc_utf8 && comp)
10133 t += utf_ptr2len(t);
10134 else
10135 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010136 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010137 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010138#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010139 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010140 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010141#endif
10142}
10143
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010144/*
10145 * "byteidx()" function
10146 */
10147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010148f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010149{
10150 byteidx(argvars, rettv, FALSE);
10151}
10152
10153/*
10154 * "byteidxcomp()" function
10155 */
10156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010157f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010158{
10159 byteidx(argvars, rettv, TRUE);
10160}
10161
Bram Moolenaardb913952012-06-29 12:54:53 +020010162 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010163func_call(
10164 char_u *name,
10165 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010166 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010167 dict_T *selfdict,
10168 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010169{
10170 listitem_T *item;
10171 typval_T argv[MAX_FUNC_ARGS + 1];
10172 int argc = 0;
10173 int dummy;
10174 int r = 0;
10175
10176 for (item = args->vval.v_list->lv_first; item != NULL;
10177 item = item->li_next)
10178 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010179 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010180 {
10181 EMSG(_("E699: Too many arguments"));
10182 break;
10183 }
10184 /* Make a copy of each argument. This is needed to be able to set
10185 * v_lock to VAR_FIXED in the copy without changing the original list.
10186 */
10187 copy_tv(&item->li_tv, &argv[argc++]);
10188 }
10189
10190 if (item == NULL)
10191 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10192 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010193 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010194
10195 /* Free the arguments. */
10196 while (argc > 0)
10197 clear_tv(&argv[--argc]);
10198
10199 return r;
10200}
10201
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010202/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010203 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010204 */
10205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010206f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010207{
10208 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010209 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010210 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010211
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010212 if (argvars[1].v_type != VAR_LIST)
10213 {
10214 EMSG(_(e_listreq));
10215 return;
10216 }
10217 if (argvars[1].vval.v_list == NULL)
10218 return;
10219
10220 if (argvars[0].v_type == VAR_FUNC)
10221 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010222 else if (argvars[0].v_type == VAR_PARTIAL)
10223 {
10224 partial = argvars[0].vval.v_partial;
10225 func = partial->pt_name;
10226 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010227 else
10228 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 if (*func == NUL)
10230 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010231
Bram Moolenaare9a41262005-01-15 22:18:47 +000010232 if (argvars[2].v_type != VAR_UNKNOWN)
10233 {
10234 if (argvars[2].v_type != VAR_DICT)
10235 {
10236 EMSG(_(e_dictreq));
10237 return;
10238 }
10239 selfdict = argvars[2].vval.v_dict;
10240 }
10241
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010242 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010243}
10244
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010245#ifdef FEAT_FLOAT
10246/*
10247 * "ceil({float})" function
10248 */
10249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010250f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010251{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010252 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010253
10254 rettv->v_type = VAR_FLOAT;
10255 if (get_float_arg(argvars, &f) == OK)
10256 rettv->vval.v_float = ceil(f);
10257 else
10258 rettv->vval.v_float = 0.0;
10259}
10260#endif
10261
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010262#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010263/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010264 * "ch_close()" function
10265 */
10266 static void
10267f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10268{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010269 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010270
10271 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010272 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010273 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010274 channel_clear(channel);
10275 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010276}
10277
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010278/*
10279 * "ch_getbufnr()" function
10280 */
10281 static void
10282f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10283{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010284 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010285
10286 rettv->vval.v_number = -1;
10287 if (channel != NULL)
10288 {
10289 char_u *what = get_tv_string(&argvars[1]);
10290 int part;
10291
10292 if (STRCMP(what, "err") == 0)
10293 part = PART_ERR;
10294 else if (STRCMP(what, "out") == 0)
10295 part = PART_OUT;
10296 else if (STRCMP(what, "in") == 0)
10297 part = PART_IN;
10298 else
10299 part = PART_SOCK;
10300 if (channel->ch_part[part].ch_buffer != NULL)
10301 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10302 }
10303}
10304
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010305/*
10306 * "ch_getjob()" function
10307 */
10308 static void
10309f_ch_getjob(typval_T *argvars, typval_T *rettv)
10310{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010311 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010312
10313 if (channel != NULL)
10314 {
10315 rettv->v_type = VAR_JOB;
10316 rettv->vval.v_job = channel->ch_job;
10317 if (channel->ch_job != NULL)
10318 ++channel->ch_job->jv_refcount;
10319 }
10320}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010321
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010322/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010323 * "ch_info()" function
10324 */
10325 static void
10326f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10327{
10328 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10329
10330 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10331 channel_info(channel, rettv->vval.v_dict);
10332}
10333
10334/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010335 * "ch_log()" function
10336 */
10337 static void
10338f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10339{
10340 char_u *msg = get_tv_string(&argvars[0]);
10341 channel_T *channel = NULL;
10342
10343 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010344 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010345
10346 ch_log(channel, (char *)msg);
10347}
10348
10349/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010350 * "ch_logfile()" function
10351 */
10352 static void
10353f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10354{
10355 char_u *fname;
10356 char_u *opt = (char_u *)"";
10357 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010358
10359 fname = get_tv_string(&argvars[0]);
10360 if (argvars[1].v_type == VAR_STRING)
10361 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010362 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010363}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010364
10365/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010366 * "ch_open()" function
10367 */
10368 static void
10369f_ch_open(typval_T *argvars, typval_T *rettv)
10370{
Bram Moolenaar77073442016-02-13 23:23:53 +010010371 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010372 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010373}
10374
10375/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010376 * "ch_read()" function
10377 */
10378 static void
10379f_ch_read(typval_T *argvars, typval_T *rettv)
10380{
10381 common_channel_read(argvars, rettv, FALSE);
10382}
10383
10384/*
10385 * "ch_readraw()" function
10386 */
10387 static void
10388f_ch_readraw(typval_T *argvars, typval_T *rettv)
10389{
10390 common_channel_read(argvars, rettv, TRUE);
10391}
10392
10393/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010394 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010395 */
10396 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010397f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10398{
10399 ch_expr_common(argvars, rettv, TRUE);
10400}
10401
10402/*
10403 * "ch_sendexpr()" function
10404 */
10405 static void
10406f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10407{
10408 ch_expr_common(argvars, rettv, FALSE);
10409}
10410
10411/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010412 * "ch_evalraw()" function
10413 */
10414 static void
10415f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10416{
10417 ch_raw_common(argvars, rettv, TRUE);
10418}
10419
10420/*
10421 * "ch_sendraw()" function
10422 */
10423 static void
10424f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10425{
10426 ch_raw_common(argvars, rettv, FALSE);
10427}
10428
10429/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010430 * "ch_setoptions()" function
10431 */
10432 static void
10433f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10434{
10435 channel_T *channel;
10436 jobopt_T opt;
10437
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010438 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010439 if (channel == NULL)
10440 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010441 clear_job_options(&opt);
10442 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010443 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10444 channel_set_options(channel, &opt);
10445 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010446}
10447
10448/*
10449 * "ch_status()" function
10450 */
10451 static void
10452f_ch_status(typval_T *argvars, typval_T *rettv)
10453{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010454 channel_T *channel;
10455
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010456 /* return an empty string by default */
10457 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010458 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010459
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010460 channel = get_channel_arg(&argvars[0], FALSE);
10461 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010462}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010463#endif
10464
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010465/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010466 * "changenr()" function
10467 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010469f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010470{
10471 rettv->vval.v_number = curbuf->b_u_seq_cur;
10472}
10473
10474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 * "char2nr(string)" function
10476 */
10477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010478f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479{
10480#ifdef FEAT_MBYTE
10481 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010482 {
10483 int utf8 = 0;
10484
10485 if (argvars[1].v_type != VAR_UNKNOWN)
10486 utf8 = get_tv_number_chk(&argvars[1], NULL);
10487
10488 if (utf8)
10489 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10490 else
10491 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 else
10494#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010495 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496}
10497
10498/*
10499 * "cindent(lnum)" function
10500 */
10501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010502f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503{
10504#ifdef FEAT_CINDENT
10505 pos_T pos;
10506 linenr_T lnum;
10507
10508 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10511 {
10512 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010513 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 curwin->w_cursor = pos;
10515 }
10516 else
10517#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010518 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519}
10520
10521/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010522 * "clearmatches()" function
10523 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010525f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010526{
10527#ifdef FEAT_SEARCH_EXTRA
10528 clear_matches(curwin);
10529#endif
10530}
10531
10532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 * "col(string)" function
10534 */
10535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010536f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537{
10538 colnr_T col = 0;
10539 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010540 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010542 fp = var2fpos(&argvars[0], FALSE, &fnum);
10543 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544 {
10545 if (fp->col == MAXCOL)
10546 {
10547 /* '> can be MAXCOL, get the length of the line then */
10548 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010549 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 else
10551 col = MAXCOL;
10552 }
10553 else
10554 {
10555 col = fp->col + 1;
10556#ifdef FEAT_VIRTUALEDIT
10557 /* col(".") when the cursor is on the NUL at the end of the line
10558 * because of "coladd" can be seen as an extra column. */
10559 if (virtual_active() && fp == &curwin->w_cursor)
10560 {
10561 char_u *p = ml_get_cursor();
10562
10563 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10564 curwin->w_virtcol - curwin->w_cursor.coladd))
10565 {
10566# ifdef FEAT_MBYTE
10567 int l;
10568
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010569 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 col += l;
10571# else
10572 if (*p != NUL && p[1] == NUL)
10573 ++col;
10574# endif
10575 }
10576 }
10577#endif
10578 }
10579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581}
10582
Bram Moolenaar572cb562005-08-05 21:35:02 +000010583#if defined(FEAT_INS_EXPAND)
10584/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010585 * "complete()" function
10586 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010588f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010589{
10590 int startcol;
10591
10592 if ((State & INSERT) == 0)
10593 {
10594 EMSG(_("E785: complete() can only be used in Insert mode"));
10595 return;
10596 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010597
10598 /* Check for undo allowed here, because if something was already inserted
10599 * the line was already saved for undo and this check isn't done. */
10600 if (!undo_allowed())
10601 return;
10602
Bram Moolenaarade00832006-03-10 21:46:58 +000010603 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10604 {
10605 EMSG(_(e_invarg));
10606 return;
10607 }
10608
10609 startcol = get_tv_number_chk(&argvars[0], NULL);
10610 if (startcol <= 0)
10611 return;
10612
10613 set_completion(startcol - 1, argvars[1].vval.v_list);
10614}
10615
10616/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010617 * "complete_add()" function
10618 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010620f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010621{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010622 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010623}
10624
10625/*
10626 * "complete_check()" function
10627 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010629f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010630{
10631 int saved = RedrawingDisabled;
10632
10633 RedrawingDisabled = 0;
10634 ins_compl_check_keys(0);
10635 rettv->vval.v_number = compl_interrupted;
10636 RedrawingDisabled = saved;
10637}
10638#endif
10639
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640/*
10641 * "confirm(message, buttons[, default [, type]])" function
10642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010644f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645{
10646#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10647 char_u *message;
10648 char_u *buttons = NULL;
10649 char_u buf[NUMBUFLEN];
10650 char_u buf2[NUMBUFLEN];
10651 int def = 1;
10652 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010653 char_u *typestr;
10654 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010656 message = get_tv_string_chk(&argvars[0]);
10657 if (message == NULL)
10658 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010659 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010661 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10662 if (buttons == NULL)
10663 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010664 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010666 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010667 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10670 if (typestr == NULL)
10671 error = TRUE;
10672 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 switch (TOUPPER_ASC(*typestr))
10675 {
10676 case 'E': type = VIM_ERROR; break;
10677 case 'Q': type = VIM_QUESTION; break;
10678 case 'I': type = VIM_INFO; break;
10679 case 'W': type = VIM_WARNING; break;
10680 case 'G': type = VIM_GENERIC; break;
10681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 }
10683 }
10684 }
10685 }
10686
10687 if (buttons == NULL || *buttons == NUL)
10688 buttons = (char_u *)_("&Ok");
10689
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010690 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010691 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010692 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693#endif
10694}
10695
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010696/*
10697 * "copy()" function
10698 */
10699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010700f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010701{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010702 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010703}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010705#ifdef FEAT_FLOAT
10706/*
10707 * "cos()" function
10708 */
10709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010710f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010711{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010712 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010713
10714 rettv->v_type = VAR_FLOAT;
10715 if (get_float_arg(argvars, &f) == OK)
10716 rettv->vval.v_float = cos(f);
10717 else
10718 rettv->vval.v_float = 0.0;
10719}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010720
10721/*
10722 * "cosh()" function
10723 */
10724 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010725f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010726{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010727 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010728
10729 rettv->v_type = VAR_FLOAT;
10730 if (get_float_arg(argvars, &f) == OK)
10731 rettv->vval.v_float = cosh(f);
10732 else
10733 rettv->vval.v_float = 0.0;
10734}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010735#endif
10736
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010738 * "count()" function
10739 */
10740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010741f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010742{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010743 long n = 0;
10744 int ic = FALSE;
10745
Bram Moolenaare9a41262005-01-15 22:18:47 +000010746 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010747 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010748 listitem_T *li;
10749 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010750 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010751
Bram Moolenaare9a41262005-01-15 22:18:47 +000010752 if ((l = argvars[0].vval.v_list) != NULL)
10753 {
10754 li = l->lv_first;
10755 if (argvars[2].v_type != VAR_UNKNOWN)
10756 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010757 int error = FALSE;
10758
10759 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010760 if (argvars[3].v_type != VAR_UNKNOWN)
10761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010762 idx = get_tv_number_chk(&argvars[3], &error);
10763 if (!error)
10764 {
10765 li = list_find(l, idx);
10766 if (li == NULL)
10767 EMSGN(_(e_listidx), idx);
10768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010770 if (error)
10771 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010772 }
10773
10774 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010775 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010776 ++n;
10777 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010778 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779 else if (argvars[0].v_type == VAR_DICT)
10780 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010781 int todo;
10782 dict_T *d;
10783 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010784
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010785 if ((d = argvars[0].vval.v_dict) != NULL)
10786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010787 int error = FALSE;
10788
Bram Moolenaare9a41262005-01-15 22:18:47 +000010789 if (argvars[2].v_type != VAR_UNKNOWN)
10790 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010792 if (argvars[3].v_type != VAR_UNKNOWN)
10793 EMSG(_(e_invarg));
10794 }
10795
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010796 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010797 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010798 {
10799 if (!HASHITEM_EMPTY(hi))
10800 {
10801 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010802 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010803 ++n;
10804 }
10805 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010806 }
10807 }
10808 else
10809 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010810 rettv->vval.v_number = n;
10811}
10812
10813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10815 *
10816 * Checks the existence of a cscope connection.
10817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010819f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820{
10821#ifdef FEAT_CSCOPE
10822 int num = 0;
10823 char_u *dbpath = NULL;
10824 char_u *prepend = NULL;
10825 char_u buf[NUMBUFLEN];
10826
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010827 if (argvars[0].v_type != VAR_UNKNOWN
10828 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830 num = (int)get_tv_number(&argvars[0]);
10831 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010832 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010833 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 }
10835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837#endif
10838}
10839
10840/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010841 * "cursor(lnum, col)" function, or
10842 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010844 * Moves the cursor to the specified line and column.
10845 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010848f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849{
10850 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010851#ifdef FEAT_VIRTUALEDIT
10852 long coladd = 0;
10853#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010854 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010856 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010857 if (argvars[1].v_type == VAR_UNKNOWN)
10858 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010859 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010860 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010861
Bram Moolenaar493c1782014-05-28 14:34:46 +020010862 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010863 {
10864 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010865 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010866 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010867 line = pos.lnum;
10868 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010869#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010870 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010871#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010872 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010873 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010874 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010875 set_curswant = FALSE;
10876 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010877 }
10878 else
10879 {
10880 line = get_tv_lnum(argvars);
10881 col = get_tv_number_chk(&argvars[1], NULL);
10882#ifdef FEAT_VIRTUALEDIT
10883 if (argvars[2].v_type != VAR_UNKNOWN)
10884 coladd = get_tv_number_chk(&argvars[2], NULL);
10885#endif
10886 }
10887 if (line < 0 || col < 0
10888#ifdef FEAT_VIRTUALEDIT
10889 || coladd < 0
10890#endif
10891 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010892 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 if (line > 0)
10894 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 if (col > 0)
10896 curwin->w_cursor.col = col - 1;
10897#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010898 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899#endif
10900
10901 /* Make sure the cursor is in a valid position. */
10902 check_cursor();
10903#ifdef FEAT_MBYTE
10904 /* Correct cursor for multi-byte character. */
10905 if (has_mbyte)
10906 mb_adjust_cursor();
10907#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010908
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010909 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010910 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911}
10912
10913/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010914 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 */
10916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010917f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010919 int noref = 0;
10920
10921 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010922 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010923 if (noref < 0 || noref > 1)
10924 EMSG(_(e_invarg));
10925 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010926 {
10927 current_copyID += COPYID_INC;
10928 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930}
10931
10932/*
10933 * "delete()" function
10934 */
10935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010936f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010938 char_u nbuf[NUMBUFLEN];
10939 char_u *name;
10940 char_u *flags;
10941
10942 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010944 return;
10945
10946 name = get_tv_string(&argvars[0]);
10947 if (name == NULL || *name == NUL)
10948 {
10949 EMSG(_(e_invarg));
10950 return;
10951 }
10952
10953 if (argvars[1].v_type != VAR_UNKNOWN)
10954 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010956 flags = (char_u *)"";
10957
10958 if (*flags == NUL)
10959 /* delete a file */
10960 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10961 else if (STRCMP(flags, "d") == 0)
10962 /* delete an empty directory */
10963 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10964 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010965 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010966 rettv->vval.v_number = delete_recursive(name);
10967 else
10968 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969}
10970
10971/*
10972 * "did_filetype()" function
10973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010975f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976{
10977#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979#endif
10980}
10981
10982/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010983 * "diff_filler()" function
10984 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010986f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010987{
10988#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010989 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010990#endif
10991}
10992
10993/*
10994 * "diff_hlID()" function
10995 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010997f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010998{
10999#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011001 static linenr_T prev_lnum = 0;
11002 static int changedtick = 0;
11003 static int fnum = 0;
11004 static int change_start = 0;
11005 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011006 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011007 int filler_lines;
11008 int col;
11009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011010 if (lnum < 0) /* ignore type error in {lnum} arg */
11011 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011012 if (lnum != prev_lnum
11013 || changedtick != curbuf->b_changedtick
11014 || fnum != curbuf->b_fnum)
11015 {
11016 /* New line, buffer, change: need to get the values. */
11017 filler_lines = diff_check(curwin, lnum);
11018 if (filler_lines < 0)
11019 {
11020 if (filler_lines == -1)
11021 {
11022 change_start = MAXCOL;
11023 change_end = -1;
11024 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11025 hlID = HLF_ADD; /* added line */
11026 else
11027 hlID = HLF_CHD; /* changed line */
11028 }
11029 else
11030 hlID = HLF_ADD; /* added line */
11031 }
11032 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011033 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011034 prev_lnum = lnum;
11035 changedtick = curbuf->b_changedtick;
11036 fnum = curbuf->b_fnum;
11037 }
11038
11039 if (hlID == HLF_CHD || hlID == HLF_TXD)
11040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011042 if (col >= change_start && col <= change_end)
11043 hlID = HLF_TXD; /* changed text */
11044 else
11045 hlID = HLF_CHD; /* changed line */
11046 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011047 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011048#endif
11049}
11050
11051/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011052 * "disable_char_avail_for_testing({expr})" function
11053 */
11054 static void
11055f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11056{
11057 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11058}
11059
11060/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011061 * "empty({expr})" function
11062 */
11063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011064f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011065{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011066 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011067
11068 switch (argvars[0].v_type)
11069 {
11070 case VAR_STRING:
11071 case VAR_FUNC:
11072 n = argvars[0].vval.v_string == NULL
11073 || *argvars[0].vval.v_string == NUL;
11074 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011075 case VAR_PARTIAL:
11076 n = FALSE;
11077 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011078 case VAR_NUMBER:
11079 n = argvars[0].vval.v_number == 0;
11080 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011081 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011082#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011083 n = argvars[0].vval.v_float == 0.0;
11084 break;
11085#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011086 case VAR_LIST:
11087 n = argvars[0].vval.v_list == NULL
11088 || argvars[0].vval.v_list->lv_first == NULL;
11089 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011090 case VAR_DICT:
11091 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011092 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011093 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011094 case VAR_SPECIAL:
11095 n = argvars[0].vval.v_number != VVAL_TRUE;
11096 break;
11097
Bram Moolenaar835dc632016-02-07 14:27:38 +010011098 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011099#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011100 n = argvars[0].vval.v_job == NULL
11101 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11102 break;
11103#endif
11104 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011106 n = argvars[0].vval.v_channel == NULL
11107 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011108 break;
11109#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011110 case VAR_UNKNOWN:
11111 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11112 n = TRUE;
11113 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011114 }
11115
11116 rettv->vval.v_number = n;
11117}
11118
11119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120 * "escape({string}, {chars})" function
11121 */
11122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011123f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124{
11125 char_u buf[NUMBUFLEN];
11126
Bram Moolenaar758711c2005-02-02 23:11:38 +000011127 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11128 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130}
11131
11132/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011133 * "eval()" function
11134 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011136f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011137{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011138 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011140 s = get_tv_string_chk(&argvars[0]);
11141 if (s != NULL)
11142 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011143
Bram Moolenaar615b9972015-01-14 17:15:05 +010011144 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11146 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011147 if (p != NULL && !aborting())
11148 EMSG2(_(e_invexpr2), p);
11149 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011151 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011152 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011153 else if (*s != NUL)
11154 EMSG(_(e_trailing));
11155}
11156
11157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 * "eventhandler()" function
11159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011161f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011162{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011163 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011164}
11165
11166/*
11167 * "executable()" function
11168 */
11169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011170f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011172 char_u *name = get_tv_string(&argvars[0]);
11173
11174 /* Check in $PATH and also check directly if there is a directory name. */
11175 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11176 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011177}
11178
11179/*
11180 * "exepath()" function
11181 */
11182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011183f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011184{
11185 char_u *p = NULL;
11186
Bram Moolenaarb5971142015-03-21 17:32:19 +010011187 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011188 rettv->v_type = VAR_STRING;
11189 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190}
11191
11192/*
11193 * "exists()" function
11194 */
11195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011196f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197{
11198 char_u *p;
11199 char_u *name;
11200 int n = FALSE;
11201 int len = 0;
11202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 if (*p == '$') /* environment variable */
11205 {
11206 /* first try "normal" environment variables (fast) */
11207 if (mch_getenv(p + 1) != NULL)
11208 n = TRUE;
11209 else
11210 {
11211 /* try expanding things like $VIM and ${HOME} */
11212 p = expand_env_save(p);
11213 if (p != NULL && *p != '$')
11214 n = TRUE;
11215 vim_free(p);
11216 }
11217 }
11218 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011220 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011221 if (*skipwhite(p) != NUL)
11222 n = FALSE; /* trailing garbage */
11223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 else if (*p == '*') /* internal or user defined function */
11225 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011226 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227 }
11228 else if (*p == ':')
11229 {
11230 n = cmd_exists(p + 1);
11231 }
11232 else if (*p == '#')
11233 {
11234#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011235 if (p[1] == '#')
11236 n = autocmd_supported(p + 2);
11237 else
11238 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011239#endif
11240 }
11241 else /* internal variable */
11242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011243 char_u *tofree;
11244 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011245
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011246 /* get_name_len() takes care of expanding curly braces */
11247 name = p;
11248 len = get_name_len(&p, &tofree, TRUE, FALSE);
11249 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011251 if (tofree != NULL)
11252 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011253 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011254 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011256 /* handle d.key, l[idx], f(expr) */
11257 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11258 if (n)
11259 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 }
11261 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011262 if (*p != NUL)
11263 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011265 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 }
11267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269}
11270
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011271#ifdef FEAT_FLOAT
11272/*
11273 * "exp()" function
11274 */
11275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011276f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011277{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011278 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011279
11280 rettv->v_type = VAR_FLOAT;
11281 if (get_float_arg(argvars, &f) == OK)
11282 rettv->vval.v_float = exp(f);
11283 else
11284 rettv->vval.v_float = 0.0;
11285}
11286#endif
11287
Bram Moolenaar071d4272004-06-13 20:20:40 +000011288/*
11289 * "expand()" function
11290 */
11291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011292f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
11294 char_u *s;
11295 int len;
11296 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011297 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011298 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011299 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011300 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011302 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011303 if (argvars[1].v_type != VAR_UNKNOWN
11304 && argvars[2].v_type != VAR_UNKNOWN
11305 && get_tv_number_chk(&argvars[2], &error)
11306 && !error)
11307 {
11308 rettv->v_type = VAR_LIST;
11309 rettv->vval.v_list = NULL;
11310 }
11311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011312 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 if (*s == '%' || *s == '#' || *s == '<')
11314 {
11315 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011316 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011318 if (rettv->v_type == VAR_LIST)
11319 {
11320 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11321 list_append_string(rettv->vval.v_list, result, -1);
11322 else
11323 vim_free(result);
11324 }
11325 else
11326 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 }
11328 else
11329 {
11330 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011331 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011332 if (argvars[1].v_type != VAR_UNKNOWN
11333 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011334 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011335 if (!error)
11336 {
11337 ExpandInit(&xpc);
11338 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011339 if (p_wic)
11340 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011341 if (rettv->v_type == VAR_STRING)
11342 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11343 options, WILD_ALL);
11344 else if (rettv_list_alloc(rettv) != FAIL)
11345 {
11346 int i;
11347
11348 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11349 for (i = 0; i < xpc.xp_numfiles; i++)
11350 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11351 ExpandCleanup(&xpc);
11352 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011353 }
11354 else
11355 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 }
11357}
11358
11359/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011360 * Go over all entries in "d2" and add them to "d1".
11361 * When "action" is "error" then a duplicate key is an error.
11362 * When "action" is "force" then a duplicate key is overwritten.
11363 * Otherwise duplicate keys are ignored ("action" is "keep").
11364 */
11365 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011366dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011367{
11368 dictitem_T *di1;
11369 hashitem_T *hi2;
11370 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011371 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011372
11373 todo = (int)d2->dv_hashtab.ht_used;
11374 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11375 {
11376 if (!HASHITEM_EMPTY(hi2))
11377 {
11378 --todo;
11379 di1 = dict_find(d1, hi2->hi_key, -1);
11380 if (d1->dv_scope != 0)
11381 {
11382 /* Disallow replacing a builtin function in l: and g:.
11383 * Check the key to be valid when adding to any
11384 * scope. */
11385 if (d1->dv_scope == VAR_DEF_SCOPE
11386 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11387 && var_check_func_name(hi2->hi_key,
11388 di1 == NULL))
11389 break;
11390 if (!valid_varname(hi2->hi_key))
11391 break;
11392 }
11393 if (di1 == NULL)
11394 {
11395 di1 = dictitem_copy(HI2DI(hi2));
11396 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11397 dictitem_free(di1);
11398 }
11399 else if (*action == 'e')
11400 {
11401 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11402 break;
11403 }
11404 else if (*action == 'f' && HI2DI(hi2) != di1)
11405 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011406 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11407 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011408 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011409 clear_tv(&di1->di_tv);
11410 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11411 }
11412 }
11413 }
11414}
11415
11416/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011417 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011418 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011419 */
11420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011421f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011422{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011423 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011424
Bram Moolenaare9a41262005-01-15 22:18:47 +000011425 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011426 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011427 list_T *l1, *l2;
11428 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011429 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011431
Bram Moolenaare9a41262005-01-15 22:18:47 +000011432 l1 = argvars[0].vval.v_list;
11433 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011434 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011435 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011436 {
11437 if (argvars[2].v_type != VAR_UNKNOWN)
11438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011439 before = get_tv_number_chk(&argvars[2], &error);
11440 if (error)
11441 return; /* type error; errmsg already given */
11442
Bram Moolenaar758711c2005-02-02 23:11:38 +000011443 if (before == l1->lv_len)
11444 item = NULL;
11445 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011446 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011447 item = list_find(l1, before);
11448 if (item == NULL)
11449 {
11450 EMSGN(_(e_listidx), before);
11451 return;
11452 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011453 }
11454 }
11455 else
11456 item = NULL;
11457 list_extend(l1, l2, item);
11458
Bram Moolenaare9a41262005-01-15 22:18:47 +000011459 copy_tv(&argvars[0], rettv);
11460 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011461 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011462 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11463 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011464 dict_T *d1, *d2;
11465 char_u *action;
11466 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011467
11468 d1 = argvars[0].vval.v_dict;
11469 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011470 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011471 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011472 {
11473 /* Check the third argument. */
11474 if (argvars[2].v_type != VAR_UNKNOWN)
11475 {
11476 static char *(av[]) = {"keep", "force", "error"};
11477
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 action = get_tv_string_chk(&argvars[2]);
11479 if (action == NULL)
11480 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011481 for (i = 0; i < 3; ++i)
11482 if (STRCMP(action, av[i]) == 0)
11483 break;
11484 if (i == 3)
11485 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011486 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011487 return;
11488 }
11489 }
11490 else
11491 action = (char_u *)"force";
11492
Bram Moolenaara9922d62013-05-30 13:01:18 +020011493 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011494
Bram Moolenaare9a41262005-01-15 22:18:47 +000011495 copy_tv(&argvars[0], rettv);
11496 }
11497 }
11498 else
11499 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011500}
11501
11502/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011503 * "feedkeys()" function
11504 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011506f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011507{
11508 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011509 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011510 char_u *keys, *flags;
11511 char_u nbuf[NUMBUFLEN];
11512 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011513 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011514 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011515
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011516 /* This is not allowed in the sandbox. If the commands would still be
11517 * executed in the sandbox it would be OK, but it probably happens later,
11518 * when "sandbox" is no longer set. */
11519 if (check_secure())
11520 return;
11521
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011522 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011523
11524 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011525 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011526 flags = get_tv_string_buf(&argvars[1], nbuf);
11527 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011528 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011529 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011530 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011531 case 'n': remap = FALSE; break;
11532 case 'm': remap = TRUE; break;
11533 case 't': typed = TRUE; break;
11534 case 'i': insert = TRUE; break;
11535 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011536 }
11537 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011538 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011539
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011540 if (*keys != NUL || execute)
11541 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011542 /* Need to escape K_SPECIAL and CSI before putting the string in the
11543 * typeahead buffer. */
11544 keys_esc = vim_strsave_escape_csi(keys);
11545 if (keys_esc != NULL)
11546 {
11547 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011548 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011549 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011550 if (vgetc_busy)
11551 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011552 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011553 {
11554 int save_msg_scroll = msg_scroll;
11555
11556 /* Avoid a 1 second delay when the keys start Insert mode. */
11557 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011558
11559 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011560 exec_normal(TRUE);
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011561 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011562 msg_scroll |= save_msg_scroll;
11563 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011564 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011565 }
11566}
11567
11568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569 * "filereadable()" function
11570 */
11571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011572f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011573{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011574 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575 char_u *p;
11576 int n;
11577
Bram Moolenaarc236c162008-07-13 17:41:49 +000011578#ifndef O_NONBLOCK
11579# define O_NONBLOCK 0
11580#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011581 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011582 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11583 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011584 {
11585 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011586 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587 }
11588 else
11589 n = FALSE;
11590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011591 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592}
11593
11594/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011595 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011596 * rights to write into.
11597 */
11598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011599f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011601 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602}
11603
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011604 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011605findfilendir(
11606 typval_T *argvars UNUSED,
11607 typval_T *rettv,
11608 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011609{
11610#ifdef FEAT_SEARCHPATH
11611 char_u *fname;
11612 char_u *fresult = NULL;
11613 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11614 char_u *p;
11615 char_u pathbuf[NUMBUFLEN];
11616 int count = 1;
11617 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011618 int error = FALSE;
11619#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011620
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011621 rettv->vval.v_string = NULL;
11622 rettv->v_type = VAR_STRING;
11623
11624#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011625 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011626
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011627 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011629 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11630 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011631 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011632 else
11633 {
11634 if (*p != NUL)
11635 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011637 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011638 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011639 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011640 }
11641
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011642 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11643 error = TRUE;
11644
11645 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011646 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 do
11648 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011649 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011650 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011651 fresult = find_file_in_path_option(first ? fname : NULL,
11652 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011653 0, first, path,
11654 find_what,
11655 curbuf->b_ffname,
11656 find_what == FINDFILE_DIR
11657 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011658 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011659
11660 if (fresult != NULL && rettv->v_type == VAR_LIST)
11661 list_append_string(rettv->vval.v_list, fresult, -1);
11662
11663 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011664 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011665
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011666 if (rettv->v_type == VAR_STRING)
11667 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011668#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011669}
11670
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011671static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11672static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011673
11674/*
11675 * Implementation of map() and filter().
11676 */
11677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011678filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011679{
11680 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011681 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011682 listitem_T *li, *nli;
11683 list_T *l = NULL;
11684 dictitem_T *di;
11685 hashtab_T *ht;
11686 hashitem_T *hi;
11687 dict_T *d = NULL;
11688 typval_T save_val;
11689 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011690 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011691 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011692 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011693 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011694 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011695 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011696 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011697
Bram Moolenaare9a41262005-01-15 22:18:47 +000011698 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011699 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011700 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011701 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011702 return;
11703 }
11704 else if (argvars[0].v_type == VAR_DICT)
11705 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011706 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011707 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011708 return;
11709 }
11710 else
11711 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011712 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011713 return;
11714 }
11715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 expr = get_tv_string_buf_chk(&argvars[1], buf);
11717 /* On type errors, the preceding call has already displayed an error
11718 * message. Avoid a misleading error message for an empty string that
11719 * was not passed as argument. */
11720 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011722 prepare_vimvar(VV_VAL, &save_val);
11723 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011724
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011725 /* We reset "did_emsg" to be able to detect whether an error
11726 * occurred during evaluation of the expression. */
11727 save_did_emsg = did_emsg;
11728 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011729
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011730 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011731 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733 vimvars[VV_KEY].vv_type = VAR_STRING;
11734
11735 ht = &d->dv_hashtab;
11736 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011737 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011740 if (!HASHITEM_EMPTY(hi))
11741 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011742 int r;
11743
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 --todo;
11745 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011746 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011747 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11748 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011749 break;
11750 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011751 r = filter_map_one(&di->di_tv, expr, map, &rem);
11752 clear_tv(&vimvars[VV_KEY].vv_tv);
11753 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 break;
11755 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011756 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011757 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11758 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011759 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011760 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011762 }
11763 }
11764 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 }
11766 else
11767 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011768 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011770 for (li = l->lv_first; li != NULL; li = nli)
11771 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011772 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011773 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011775 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011776 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011777 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011778 break;
11779 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011780 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011781 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011782 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011783 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011784
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011785 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011787
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011788 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011789 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011790
11791 copy_tv(&argvars[0], rettv);
11792}
11793
11794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011795filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011796{
Bram Moolenaar33570922005-01-25 22:26:29 +000011797 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011798 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011799 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011800
Bram Moolenaar33570922005-01-25 22:26:29 +000011801 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011802 s = expr;
11803 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011804 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011805 if (*s != NUL) /* check for trailing chars after expr */
11806 {
11807 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011808 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011809 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011810 }
11811 if (map)
11812 {
11813 /* map(): replace the list item value */
11814 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011815 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011816 *tv = rettv;
11817 }
11818 else
11819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011820 int error = FALSE;
11821
Bram Moolenaare9a41262005-01-15 22:18:47 +000011822 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011824 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011825 /* On type error, nothing has been removed; return FAIL to stop the
11826 * loop. The error message was given by get_tv_number_chk(). */
11827 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011828 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011829 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011830 retval = OK;
11831theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011832 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011833 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011834}
11835
11836/*
11837 * "filter()" function
11838 */
11839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011840f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011841{
11842 filter_map(argvars, rettv, FALSE);
11843}
11844
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011845/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011846 * "finddir({fname}[, {path}[, {count}]])" function
11847 */
11848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011849f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011850{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011851 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011852}
11853
11854/*
11855 * "findfile({fname}[, {path}[, {count}]])" function
11856 */
11857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011858f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011859{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011860 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011861}
11862
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011863#ifdef FEAT_FLOAT
11864/*
11865 * "float2nr({float})" function
11866 */
11867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011868f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011869{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011870 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011871
11872 if (get_float_arg(argvars, &f) == OK)
11873 {
11874 if (f < -0x7fffffff)
11875 rettv->vval.v_number = -0x7fffffff;
11876 else if (f > 0x7fffffff)
11877 rettv->vval.v_number = 0x7fffffff;
11878 else
11879 rettv->vval.v_number = (varnumber_T)f;
11880 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011881}
11882
11883/*
11884 * "floor({float})" function
11885 */
11886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011887f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011888{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011889 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011890
11891 rettv->v_type = VAR_FLOAT;
11892 if (get_float_arg(argvars, &f) == OK)
11893 rettv->vval.v_float = floor(f);
11894 else
11895 rettv->vval.v_float = 0.0;
11896}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011897
11898/*
11899 * "fmod()" function
11900 */
11901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011902f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011903{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011904 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011905
11906 rettv->v_type = VAR_FLOAT;
11907 if (get_float_arg(argvars, &fx) == OK
11908 && get_float_arg(&argvars[1], &fy) == OK)
11909 rettv->vval.v_float = fmod(fx, fy);
11910 else
11911 rettv->vval.v_float = 0.0;
11912}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011913#endif
11914
Bram Moolenaar0d660222005-01-07 21:51:51 +000011915/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011916 * "fnameescape({string})" function
11917 */
11918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011919f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011920{
11921 rettv->vval.v_string = vim_strsave_fnameescape(
11922 get_tv_string(&argvars[0]), FALSE);
11923 rettv->v_type = VAR_STRING;
11924}
11925
11926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 * "fnamemodify({fname}, {mods})" function
11928 */
11929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011930f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011931{
11932 char_u *fname;
11933 char_u *mods;
11934 int usedlen = 0;
11935 int len;
11936 char_u *fbuf = NULL;
11937 char_u buf[NUMBUFLEN];
11938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011939 fname = get_tv_string_chk(&argvars[0]);
11940 mods = get_tv_string_buf_chk(&argvars[1], buf);
11941 if (fname == NULL || mods == NULL)
11942 fname = NULL;
11943 else
11944 {
11945 len = (int)STRLEN(fname);
11946 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011949 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011953 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954 vim_free(fbuf);
11955}
11956
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011957static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958
11959/*
11960 * "foldclosed()" function
11961 */
11962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011963foldclosed_both(
11964 typval_T *argvars UNUSED,
11965 typval_T *rettv,
11966 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011967{
11968#ifdef FEAT_FOLDING
11969 linenr_T lnum;
11970 linenr_T first, last;
11971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011972 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11974 {
11975 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11976 {
11977 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011978 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 return;
11982 }
11983 }
11984#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011985 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986}
11987
11988/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011989 * "foldclosed()" function
11990 */
11991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011992f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011993{
11994 foldclosed_both(argvars, rettv, FALSE);
11995}
11996
11997/*
11998 * "foldclosedend()" function
11999 */
12000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012001f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012002{
12003 foldclosed_both(argvars, rettv, TRUE);
12004}
12005
12006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 * "foldlevel()" function
12008 */
12009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012010f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012011{
12012#ifdef FEAT_FOLDING
12013 linenr_T lnum;
12014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012017 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019}
12020
12021/*
12022 * "foldtext()" function
12023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012025f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026{
12027#ifdef FEAT_FOLDING
12028 linenr_T lnum;
12029 char_u *s;
12030 char_u *r;
12031 int len;
12032 char *txt;
12033#endif
12034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012035 rettv->v_type = VAR_STRING;
12036 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012038 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12039 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12040 <= curbuf->b_ml.ml_line_count
12041 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042 {
12043 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012044 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12045 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 {
12047 if (!linewhite(lnum))
12048 break;
12049 ++lnum;
12050 }
12051
12052 /* Find interesting text in this line. */
12053 s = skipwhite(ml_get(lnum));
12054 /* skip C comment-start */
12055 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012058 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012059 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012060 {
12061 s = skipwhite(ml_get(lnum + 1));
12062 if (*s == '*')
12063 s = skipwhite(s + 1);
12064 }
12065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012066 txt = _("+-%s%3ld lines: ");
12067 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012068 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069 + 20 /* for %3ld */
12070 + STRLEN(s))); /* concatenated */
12071 if (r != NULL)
12072 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012073 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12074 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12075 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012076 len = (int)STRLEN(r);
12077 STRCAT(r, s);
12078 /* remove 'foldmarker' and 'commentstring' */
12079 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012080 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081 }
12082 }
12083#endif
12084}
12085
12086/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012087 * "foldtextresult(lnum)" function
12088 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012089 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012090f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012091{
12092#ifdef FEAT_FOLDING
12093 linenr_T lnum;
12094 char_u *text;
12095 char_u buf[51];
12096 foldinfo_T foldinfo;
12097 int fold_count;
12098#endif
12099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012100 rettv->v_type = VAR_STRING;
12101 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012102#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012104 /* treat illegal types and illegal string values for {lnum} the same */
12105 if (lnum < 0)
12106 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012107 fold_count = foldedCount(curwin, lnum, &foldinfo);
12108 if (fold_count > 0)
12109 {
12110 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12111 &foldinfo, buf);
12112 if (text == buf)
12113 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012114 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012115 }
12116#endif
12117}
12118
12119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120 * "foreground()" function
12121 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012123f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125#ifdef FEAT_GUI
12126 if (gui.in_use)
12127 gui_mch_set_foreground();
12128#else
12129# ifdef WIN32
12130 win32_set_foreground();
12131# endif
12132#endif
12133}
12134
12135/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012136 * "function()" function
12137 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012139f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012140{
12141 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012142 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012143 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012144 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012145
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012146 if (argvars[0].v_type == VAR_FUNC)
12147 {
12148 /* function(MyFunc, [arg], dict) */
12149 s = argvars[0].vval.v_string;
12150 }
12151 else if (argvars[0].v_type == VAR_PARTIAL
12152 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012153 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012154 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012155 arg_pt = argvars[0].vval.v_partial;
12156 s = arg_pt->pt_name;
12157 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012158 else
12159 {
12160 /* function('MyFunc', [arg], dict) */
12161 s = get_tv_string(&argvars[0]);
12162 use_string = TRUE;
12163 }
12164
12165 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012166 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012167 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012168 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12169 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012170 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012171 else
12172 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012173 int dict_idx = 0;
12174 int arg_idx = 0;
12175 list_T *list = NULL;
12176
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012177 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012178 {
12179 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012180 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012181
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012182 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12183 * also be called from another script. Using trans_function_name()
12184 * would also work, but some plugins depend on the name being
12185 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012186 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012187 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12188 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012189 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012190 STRCPY(name, sid_buf);
12191 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012192 }
12193 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012194 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012195 name = vim_strsave(s);
12196
12197 if (argvars[1].v_type != VAR_UNKNOWN)
12198 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012199 if (argvars[2].v_type != VAR_UNKNOWN)
12200 {
12201 /* function(name, [args], dict) */
12202 arg_idx = 1;
12203 dict_idx = 2;
12204 }
12205 else if (argvars[1].v_type == VAR_DICT)
12206 /* function(name, dict) */
12207 dict_idx = 1;
12208 else
12209 /* function(name, [args]) */
12210 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012211 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012212 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012213 if (argvars[dict_idx].v_type != VAR_DICT)
12214 {
12215 EMSG(_("E922: expected a dict"));
12216 vim_free(name);
12217 return;
12218 }
12219 if (argvars[dict_idx].vval.v_dict == NULL)
12220 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012221 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012222 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012223 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012224 if (argvars[arg_idx].v_type != VAR_LIST)
12225 {
12226 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12227 vim_free(name);
12228 return;
12229 }
12230 list = argvars[arg_idx].vval.v_list;
12231 if (list == NULL || list->lv_len == 0)
12232 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012233 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012234 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012235 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012236 {
12237 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012238
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012239 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012240 if (pt == NULL)
12241 vim_free(name);
12242 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012243 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012244 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012245 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012246 listitem_T *li;
12247 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012248 int arg_len = 0;
12249 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012250
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012251 if (arg_pt != NULL)
12252 arg_len = arg_pt->pt_argc;
12253 if (list != NULL)
12254 lv_len = list->lv_len;
12255 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012256 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012257 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012258 if (pt->pt_argv == NULL)
12259 {
12260 vim_free(pt);
12261 vim_free(name);
12262 return;
12263 }
12264 else
12265 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012266 for (i = 0; i < arg_len; i++)
12267 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12268 if (lv_len > 0)
12269 for (li = list->lv_first; li != NULL;
12270 li = li->li_next)
12271 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012272 }
12273 }
12274
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012275 /* For "function(dict.func, [], dict)" and "func" is a partial
12276 * use "dict". That is backwards compatible. */
12277 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012278 {
12279 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12280 ++pt->pt_dict->dv_refcount;
12281 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012282 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012283 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012284 pt->pt_dict = arg_pt->pt_dict;
12285 if (pt->pt_dict != NULL)
12286 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012287 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012288
12289 pt->pt_refcount = 1;
12290 pt->pt_name = name;
12291 func_ref(pt->pt_name);
12292 }
12293 rettv->v_type = VAR_PARTIAL;
12294 rettv->vval.v_partial = pt;
12295 }
12296 else
12297 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012298 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012299 rettv->v_type = VAR_FUNC;
12300 rettv->vval.v_string = name;
12301 func_ref(name);
12302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012303 }
12304}
12305
12306/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012307 * "garbagecollect()" function
12308 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012310f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012311{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012312 /* This is postponed until we are back at the toplevel, because we may be
12313 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12314 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012315
12316 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12317 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012318}
12319
12320/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012321 * "get()" function
12322 */
12323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012324f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012325{
Bram Moolenaar33570922005-01-25 22:26:29 +000012326 listitem_T *li;
12327 list_T *l;
12328 dictitem_T *di;
12329 dict_T *d;
12330 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012331
Bram Moolenaare9a41262005-01-15 22:18:47 +000012332 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012333 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012334 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012336 int error = FALSE;
12337
12338 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12339 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012340 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012341 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012342 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012343 else if (argvars[0].v_type == VAR_DICT)
12344 {
12345 if ((d = argvars[0].vval.v_dict) != NULL)
12346 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012347 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012348 if (di != NULL)
12349 tv = &di->di_tv;
12350 }
12351 }
12352 else
12353 EMSG2(_(e_listdictarg), "get()");
12354
12355 if (tv == NULL)
12356 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012357 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012358 copy_tv(&argvars[2], rettv);
12359 }
12360 else
12361 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012362}
12363
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012364static 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 +000012365
12366/*
12367 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012368 * Return a range (from start to end) of lines in rettv from the specified
12369 * buffer.
12370 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012371 */
12372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012373get_buffer_lines(
12374 buf_T *buf,
12375 linenr_T start,
12376 linenr_T end,
12377 int retlist,
12378 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012379{
12380 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012381
Bram Moolenaar959a1432013-12-14 12:17:38 +010012382 rettv->v_type = VAR_STRING;
12383 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012384 if (retlist && rettv_list_alloc(rettv) == FAIL)
12385 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012386
12387 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12388 return;
12389
12390 if (!retlist)
12391 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012392 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12393 p = ml_get_buf(buf, start, FALSE);
12394 else
12395 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012396 rettv->vval.v_string = vim_strsave(p);
12397 }
12398 else
12399 {
12400 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012401 return;
12402
12403 if (start < 1)
12404 start = 1;
12405 if (end > buf->b_ml.ml_line_count)
12406 end = buf->b_ml.ml_line_count;
12407 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012408 if (list_append_string(rettv->vval.v_list,
12409 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012410 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012411 }
12412}
12413
12414/*
12415 * "getbufline()" function
12416 */
12417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012418f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012419{
12420 linenr_T lnum;
12421 linenr_T end;
12422 buf_T *buf;
12423
12424 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12425 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012426 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012427 --emsg_off;
12428
Bram Moolenaar661b1822005-07-28 22:36:45 +000012429 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012430 if (argvars[2].v_type == VAR_UNKNOWN)
12431 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012432 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012433 end = get_tv_lnum_buf(&argvars[2], buf);
12434
Bram Moolenaar342337a2005-07-21 21:11:17 +000012435 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012436}
12437
Bram Moolenaar0d660222005-01-07 21:51:51 +000012438/*
12439 * "getbufvar()" function
12440 */
12441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012442f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443{
12444 buf_T *buf;
12445 buf_T *save_curbuf;
12446 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012447 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012448 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012450 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12451 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012452 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012453 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012454
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012455 rettv->v_type = VAR_STRING;
12456 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012457
12458 if (buf != NULL && varname != NULL)
12459 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012460 /* set curbuf to be our buf, temporarily */
12461 save_curbuf = curbuf;
12462 curbuf = buf;
12463
Bram Moolenaar0d660222005-01-07 21:51:51 +000012464 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012465 {
12466 if (get_option_tv(&varname, rettv, TRUE) == OK)
12467 done = TRUE;
12468 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012469 else if (STRCMP(varname, "changedtick") == 0)
12470 {
12471 rettv->v_type = VAR_NUMBER;
12472 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012473 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012474 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012475 else
12476 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012477 /* Look up the variable. */
12478 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12479 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12480 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012481 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012482 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012483 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012484 done = TRUE;
12485 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012486 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012487
12488 /* restore previous notion of curbuf */
12489 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012490 }
12491
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012492 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12493 /* use the default value */
12494 copy_tv(&argvars[2], rettv);
12495
Bram Moolenaar0d660222005-01-07 21:51:51 +000012496 --emsg_off;
12497}
12498
12499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 * "getchar()" function
12501 */
12502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012503f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012504{
12505 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012506 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012508 /* Position the cursor. Needed after a message that ends in a space. */
12509 windgoto(msg_row, msg_col);
12510
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 ++no_mapping;
12512 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012513 for (;;)
12514 {
12515 if (argvars[0].v_type == VAR_UNKNOWN)
12516 /* getchar(): blocking wait. */
12517 n = safe_vgetc();
12518 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12519 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012520 n = vpeekc_any();
12521 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012522 /* illegal argument or getchar(0) and no char avail: return zero */
12523 n = 0;
12524 else
12525 /* getchar(0) and char avail: return char */
12526 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012527
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012528 if (n == K_IGNORE)
12529 continue;
12530 break;
12531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 --no_mapping;
12533 --allow_keys;
12534
Bram Moolenaar219b8702006-11-01 14:32:36 +000012535 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12536 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12537 vimvars[VV_MOUSE_COL].vv_nr = 0;
12538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012539 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012540 if (IS_SPECIAL(n) || mod_mask != 0)
12541 {
12542 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12543 int i = 0;
12544
12545 /* Turn a special key into three bytes, plus modifier. */
12546 if (mod_mask != 0)
12547 {
12548 temp[i++] = K_SPECIAL;
12549 temp[i++] = KS_MODIFIER;
12550 temp[i++] = mod_mask;
12551 }
12552 if (IS_SPECIAL(n))
12553 {
12554 temp[i++] = K_SPECIAL;
12555 temp[i++] = K_SECOND(n);
12556 temp[i++] = K_THIRD(n);
12557 }
12558#ifdef FEAT_MBYTE
12559 else if (has_mbyte)
12560 i += (*mb_char2bytes)(n, temp + i);
12561#endif
12562 else
12563 temp[i++] = n;
12564 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012565 rettv->v_type = VAR_STRING;
12566 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012567
12568#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012569 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012570 {
12571 int row = mouse_row;
12572 int col = mouse_col;
12573 win_T *win;
12574 linenr_T lnum;
12575# ifdef FEAT_WINDOWS
12576 win_T *wp;
12577# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012578 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012579
12580 if (row >= 0 && col >= 0)
12581 {
12582 /* Find the window at the mouse coordinates and compute the
12583 * text position. */
12584 win = mouse_find_win(&row, &col);
12585 (void)mouse_comp_pos(win, &row, &col, &lnum);
12586# ifdef FEAT_WINDOWS
12587 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012588 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012589# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012590 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012591 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12592 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12593 }
12594 }
12595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596 }
12597}
12598
12599/*
12600 * "getcharmod()" function
12601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012603f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606}
12607
12608/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012609 * "getcharsearch()" function
12610 */
12611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012612f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012613{
12614 if (rettv_dict_alloc(rettv) != FAIL)
12615 {
12616 dict_T *dict = rettv->vval.v_dict;
12617
12618 dict_add_nr_str(dict, "char", 0L, last_csearch());
12619 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12620 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12621 }
12622}
12623
12624/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625 * "getcmdline()" function
12626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012628f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 rettv->v_type = VAR_STRING;
12631 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632}
12633
12634/*
12635 * "getcmdpos()" function
12636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012638f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012640 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641}
12642
12643/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012644 * "getcmdtype()" function
12645 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012647f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012648{
12649 rettv->v_type = VAR_STRING;
12650 rettv->vval.v_string = alloc(2);
12651 if (rettv->vval.v_string != NULL)
12652 {
12653 rettv->vval.v_string[0] = get_cmdline_type();
12654 rettv->vval.v_string[1] = NUL;
12655 }
12656}
12657
12658/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012659 * "getcmdwintype()" function
12660 */
12661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012662f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012663{
12664 rettv->v_type = VAR_STRING;
12665 rettv->vval.v_string = NULL;
12666#ifdef FEAT_CMDWIN
12667 rettv->vval.v_string = alloc(2);
12668 if (rettv->vval.v_string != NULL)
12669 {
12670 rettv->vval.v_string[0] = cmdwin_type;
12671 rettv->vval.v_string[1] = NUL;
12672 }
12673#endif
12674}
12675
12676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 * "getcwd()" function
12678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012680f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012682 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012683 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012685 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012686 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012687
12688 wp = find_tabwin(&argvars[0], &argvars[1]);
12689 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012691 if (wp->w_localdir != NULL)
12692 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12693 else if(globaldir != NULL)
12694 rettv->vval.v_string = vim_strsave(globaldir);
12695 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012696 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012697 cwd = alloc(MAXPATHL);
12698 if (cwd != NULL)
12699 {
12700 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12701 rettv->vval.v_string = vim_strsave(cwd);
12702 vim_free(cwd);
12703 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012704 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012705#ifdef BACKSLASH_IN_FILENAME
12706 if (rettv->vval.v_string != NULL)
12707 slash_adjust(rettv->vval.v_string);
12708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709 }
12710}
12711
12712/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012713 * "getfontname()" function
12714 */
12715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012716f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012717{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718 rettv->v_type = VAR_STRING;
12719 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012720#ifdef FEAT_GUI
12721 if (gui.in_use)
12722 {
12723 GuiFont font;
12724 char_u *name = NULL;
12725
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012726 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012727 {
12728 /* Get the "Normal" font. Either the name saved by
12729 * hl_set_font_name() or from the font ID. */
12730 font = gui.norm_font;
12731 name = hl_get_font_name();
12732 }
12733 else
12734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012736 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12737 return;
12738 font = gui_mch_get_font(name, FALSE);
12739 if (font == NOFONT)
12740 return; /* Invalid font name, return empty string. */
12741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012744 gui_mch_free_font(font);
12745 }
12746#endif
12747}
12748
12749/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012750 * "getfperm({fname})" function
12751 */
12752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012753f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012754{
12755 char_u *fname;
12756 struct stat st;
12757 char_u *perm = NULL;
12758 char_u flags[] = "rwx";
12759 int i;
12760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012762
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012764 if (mch_stat((char *)fname, &st) >= 0)
12765 {
12766 perm = vim_strsave((char_u *)"---------");
12767 if (perm != NULL)
12768 {
12769 for (i = 0; i < 9; i++)
12770 {
12771 if (st.st_mode & (1 << (8 - i)))
12772 perm[i] = flags[i % 3];
12773 }
12774 }
12775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012777}
12778
12779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 * "getfsize({fname})" function
12781 */
12782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012783f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784{
12785 char_u *fname;
12786 struct stat st;
12787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791
12792 if (mch_stat((char *)fname, &st) >= 0)
12793 {
12794 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012797 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012799
12800 /* non-perfect check for overflow */
12801 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12802 rettv->vval.v_number = -2;
12803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 }
12805 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807}
12808
12809/*
12810 * "getftime({fname})" function
12811 */
12812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012813f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814{
12815 char_u *fname;
12816 struct stat st;
12817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819
12820 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824}
12825
12826/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012827 * "getftype({fname})" function
12828 */
12829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012830f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012831{
12832 char_u *fname;
12833 struct stat st;
12834 char_u *type = NULL;
12835 char *t;
12836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012840 if (mch_lstat((char *)fname, &st) >= 0)
12841 {
12842#ifdef S_ISREG
12843 if (S_ISREG(st.st_mode))
12844 t = "file";
12845 else if (S_ISDIR(st.st_mode))
12846 t = "dir";
12847# ifdef S_ISLNK
12848 else if (S_ISLNK(st.st_mode))
12849 t = "link";
12850# endif
12851# ifdef S_ISBLK
12852 else if (S_ISBLK(st.st_mode))
12853 t = "bdev";
12854# endif
12855# ifdef S_ISCHR
12856 else if (S_ISCHR(st.st_mode))
12857 t = "cdev";
12858# endif
12859# ifdef S_ISFIFO
12860 else if (S_ISFIFO(st.st_mode))
12861 t = "fifo";
12862# endif
12863# ifdef S_ISSOCK
12864 else if (S_ISSOCK(st.st_mode))
12865 t = "fifo";
12866# endif
12867 else
12868 t = "other";
12869#else
12870# ifdef S_IFMT
12871 switch (st.st_mode & S_IFMT)
12872 {
12873 case S_IFREG: t = "file"; break;
12874 case S_IFDIR: t = "dir"; break;
12875# ifdef S_IFLNK
12876 case S_IFLNK: t = "link"; break;
12877# endif
12878# ifdef S_IFBLK
12879 case S_IFBLK: t = "bdev"; break;
12880# endif
12881# ifdef S_IFCHR
12882 case S_IFCHR: t = "cdev"; break;
12883# endif
12884# ifdef S_IFIFO
12885 case S_IFIFO: t = "fifo"; break;
12886# endif
12887# ifdef S_IFSOCK
12888 case S_IFSOCK: t = "socket"; break;
12889# endif
12890 default: t = "other";
12891 }
12892# else
12893 if (mch_isdir(fname))
12894 t = "dir";
12895 else
12896 t = "file";
12897# endif
12898#endif
12899 type = vim_strsave((char_u *)t);
12900 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012902}
12903
12904/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012905 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012906 */
12907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012908f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012909{
12910 linenr_T lnum;
12911 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012912 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012913
12914 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012915 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012916 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012917 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012918 retlist = FALSE;
12919 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012920 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012921 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012922 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012923 retlist = TRUE;
12924 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012925
Bram Moolenaar342337a2005-07-21 21:11:17 +000012926 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012927}
12928
12929/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012930 * "getmatches()" function
12931 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012933f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012934{
12935#ifdef FEAT_SEARCH_EXTRA
12936 dict_T *dict;
12937 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012938 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012939
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012940 if (rettv_list_alloc(rettv) == OK)
12941 {
12942 while (cur != NULL)
12943 {
12944 dict = dict_alloc();
12945 if (dict == NULL)
12946 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012947 if (cur->match.regprog == NULL)
12948 {
12949 /* match added with matchaddpos() */
12950 for (i = 0; i < MAXPOSMATCH; ++i)
12951 {
12952 llpos_T *llpos;
12953 char buf[6];
12954 list_T *l;
12955
12956 llpos = &cur->pos.pos[i];
12957 if (llpos->lnum == 0)
12958 break;
12959 l = list_alloc();
12960 if (l == NULL)
12961 break;
12962 list_append_number(l, (varnumber_T)llpos->lnum);
12963 if (llpos->col > 0)
12964 {
12965 list_append_number(l, (varnumber_T)llpos->col);
12966 list_append_number(l, (varnumber_T)llpos->len);
12967 }
12968 sprintf(buf, "pos%d", i + 1);
12969 dict_add_list(dict, buf, l);
12970 }
12971 }
12972 else
12973 {
12974 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12975 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012976 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012977 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12978 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020012979# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020012980 if (cur->conceal_char)
12981 {
12982 char_u buf[MB_MAXBYTES + 1];
12983
12984 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12985 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12986 }
12987# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012988 list_append_dict(rettv->vval.v_list, dict);
12989 cur = cur->next;
12990 }
12991 }
12992#endif
12993}
12994
12995/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012996 * "getpid()" function
12997 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012999f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013000{
13001 rettv->vval.v_number = mch_get_pid();
13002}
13003
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013004static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013005
13006/*
13007 * "getcurpos()" function
13008 */
13009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013010f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013011{
13012 getpos_both(argvars, rettv, TRUE);
13013}
13014
Bram Moolenaar18081e32008-02-20 19:11:07 +000013015/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013016 * "getpos(string)" function
13017 */
13018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013019f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013020{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013021 getpos_both(argvars, rettv, FALSE);
13022}
13023
13024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013025getpos_both(
13026 typval_T *argvars,
13027 typval_T *rettv,
13028 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013029{
Bram Moolenaara5525202006-03-02 22:52:09 +000013030 pos_T *fp;
13031 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013032 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013033
13034 if (rettv_list_alloc(rettv) == OK)
13035 {
13036 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013037 if (getcurpos)
13038 fp = &curwin->w_cursor;
13039 else
13040 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013041 if (fnum != -1)
13042 list_append_number(l, (varnumber_T)fnum);
13043 else
13044 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013045 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13046 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013047 list_append_number(l, (fp != NULL)
13048 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013049 : (varnumber_T)0);
13050 list_append_number(l,
13051#ifdef FEAT_VIRTUALEDIT
13052 (fp != NULL) ? (varnumber_T)fp->coladd :
13053#endif
13054 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013055 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013056 {
13057 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013058 list_append_number(l, curwin->w_curswant == MAXCOL ?
13059 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013060 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013061 }
13062 else
13063 rettv->vval.v_number = FALSE;
13064}
13065
13066/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013067 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013068 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013070f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013071{
13072#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013073 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013074#endif
13075
Bram Moolenaar2641f772005-03-25 21:58:17 +000013076#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013077 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013078 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013079 wp = NULL;
13080 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13081 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013082 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013083 if (wp == NULL)
13084 return;
13085 }
13086
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013087 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013088 }
13089#endif
13090}
13091
13092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 * "getreg()" function
13094 */
13095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013096f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097{
13098 char_u *strregname;
13099 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013100 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013101 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013104 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013106 strregname = get_tv_string_chk(&argvars[0]);
13107 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013108 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013109 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013110 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013111 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13112 return_list = get_tv_number_chk(&argvars[2], &error);
13113 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013116 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013117
13118 if (error)
13119 return;
13120
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 regname = (strregname == NULL ? '"' : *strregname);
13122 if (regname == 0)
13123 regname = '"';
13124
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013125 if (return_list)
13126 {
13127 rettv->v_type = VAR_LIST;
13128 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13129 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013130 if (rettv->vval.v_list != NULL)
13131 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013132 }
13133 else
13134 {
13135 rettv->v_type = VAR_STRING;
13136 rettv->vval.v_string = get_reg_contents(regname,
13137 arg2 ? GREG_EXPR_SRC : 0);
13138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139}
13140
13141/*
13142 * "getregtype()" function
13143 */
13144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013145f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146{
13147 char_u *strregname;
13148 int regname;
13149 char_u buf[NUMBUFLEN + 2];
13150 long reglen = 0;
13151
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013152 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013153 {
13154 strregname = get_tv_string_chk(&argvars[0]);
13155 if (strregname == NULL) /* type error; errmsg already given */
13156 {
13157 rettv->v_type = VAR_STRING;
13158 rettv->vval.v_string = NULL;
13159 return;
13160 }
13161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 else
13163 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013164 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165
13166 regname = (strregname == NULL ? '"' : *strregname);
13167 if (regname == 0)
13168 regname = '"';
13169
13170 buf[0] = NUL;
13171 buf[1] = NUL;
13172 switch (get_reg_type(regname, &reglen))
13173 {
13174 case MLINE: buf[0] = 'V'; break;
13175 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 case MBLOCK:
13177 buf[0] = Ctrl_V;
13178 sprintf((char *)buf + 1, "%ld", reglen + 1);
13179 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181 rettv->v_type = VAR_STRING;
13182 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183}
13184
13185/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013186 * "gettabvar()" function
13187 */
13188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013189f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013190{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013191 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013192 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013193 dictitem_T *v;
13194 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013195 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013196
13197 rettv->v_type = VAR_STRING;
13198 rettv->vval.v_string = NULL;
13199
13200 varname = get_tv_string_chk(&argvars[1]);
13201 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13202 if (tp != NULL && varname != NULL)
13203 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013204 /* Set tp to be our tabpage, temporarily. Also set the window to the
13205 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013206 if (switch_win(&oldcurwin, &oldtabpage,
13207 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013208 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013209 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013210 /* look up the variable */
13211 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13212 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13213 if (v != NULL)
13214 {
13215 copy_tv(&v->di_tv, rettv);
13216 done = TRUE;
13217 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013218 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013219
13220 /* restore previous notion of curwin */
13221 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013222 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013223
13224 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013225 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013226}
13227
13228/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013229 * "gettabwinvar()" function
13230 */
13231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013232f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013233{
13234 getwinvar(argvars, rettv, 1);
13235}
13236
13237/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 * "getwinposx()" function
13239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013241f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244#ifdef FEAT_GUI
13245 if (gui.in_use)
13246 {
13247 int x, y;
13248
13249 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013250 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 }
13252#endif
13253}
13254
13255/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013256 * "win_findbuf()" function
13257 */
13258 static void
13259f_win_findbuf(typval_T *argvars, typval_T *rettv)
13260{
13261 if (rettv_list_alloc(rettv) != FAIL)
13262 win_findbuf(argvars, rettv->vval.v_list);
13263}
13264
13265/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013266 * "win_getid()" function
13267 */
13268 static void
13269f_win_getid(typval_T *argvars, typval_T *rettv)
13270{
13271 rettv->vval.v_number = win_getid(argvars);
13272}
13273
13274/*
13275 * "win_gotoid()" function
13276 */
13277 static void
13278f_win_gotoid(typval_T *argvars, typval_T *rettv)
13279{
13280 rettv->vval.v_number = win_gotoid(argvars);
13281}
13282
13283/*
13284 * "win_id2tabwin()" function
13285 */
13286 static void
13287f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13288{
13289 if (rettv_list_alloc(rettv) != FAIL)
13290 win_id2tabwin(argvars, rettv->vval.v_list);
13291}
13292
13293/*
13294 * "win_id2win()" function
13295 */
13296 static void
13297f_win_id2win(typval_T *argvars, typval_T *rettv)
13298{
13299 rettv->vval.v_number = win_id2win(argvars);
13300}
13301
13302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 * "getwinposy()" function
13304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013306f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013308 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309#ifdef FEAT_GUI
13310 if (gui.in_use)
13311 {
13312 int x, y;
13313
13314 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013315 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 }
13317#endif
13318}
13319
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013320/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013321 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013322 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013323 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013324find_win_by_nr(
13325 typval_T *vp,
13326 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013327{
13328#ifdef FEAT_WINDOWS
13329 win_T *wp;
13330#endif
13331 int nr;
13332
13333 nr = get_tv_number_chk(vp, NULL);
13334
13335#ifdef FEAT_WINDOWS
13336 if (nr < 0)
13337 return NULL;
13338 if (nr == 0)
13339 return curwin;
13340
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013341 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13342 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013343 if (--nr <= 0)
13344 break;
13345 return wp;
13346#else
13347 if (nr == 0 || nr == 1)
13348 return curwin;
13349 return NULL;
13350#endif
13351}
13352
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013354 * Find window specified by "wvp" in tabpage "tvp".
13355 */
13356 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013357find_tabwin(
13358 typval_T *wvp, /* VAR_UNKNOWN for current window */
13359 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013360{
13361 win_T *wp = NULL;
13362 tabpage_T *tp = NULL;
13363 long n;
13364
13365 if (wvp->v_type != VAR_UNKNOWN)
13366 {
13367 if (tvp->v_type != VAR_UNKNOWN)
13368 {
13369 n = get_tv_number(tvp);
13370 if (n >= 0)
13371 tp = find_tabpage(n);
13372 }
13373 else
13374 tp = curtab;
13375
13376 if (tp != NULL)
13377 wp = find_win_by_nr(wvp, tp);
13378 }
13379 else
13380 wp = curwin;
13381
13382 return wp;
13383}
13384
13385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 * "getwinvar()" function
13387 */
13388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013389f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013391 getwinvar(argvars, rettv, 0);
13392}
13393
13394/*
13395 * getwinvar() and gettabwinvar()
13396 */
13397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013398getwinvar(
13399 typval_T *argvars,
13400 typval_T *rettv,
13401 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013402{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013403 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013405 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013406 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013407 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013408#ifdef FEAT_WINDOWS
13409 win_T *oldcurwin;
13410 tabpage_T *oldtabpage;
13411 int need_switch_win;
13412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013414#ifdef FEAT_WINDOWS
13415 if (off == 1)
13416 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13417 else
13418 tp = curtab;
13419#endif
13420 win = find_win_by_nr(&argvars[off], tp);
13421 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013422 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013424 rettv->v_type = VAR_STRING;
13425 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426
13427 if (win != NULL && varname != NULL)
13428 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013429#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013430 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013431 * otherwise the window is not valid. Only do this when needed,
13432 * autocommands get blocked. */
13433 need_switch_win = !(tp == curtab && win == curwin);
13434 if (!need_switch_win
13435 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13436#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013437 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013438 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013439 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013440 if (get_option_tv(&varname, rettv, 1) == OK)
13441 done = TRUE;
13442 }
13443 else
13444 {
13445 /* Look up the variable. */
13446 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13447 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13448 varname, FALSE);
13449 if (v != NULL)
13450 {
13451 copy_tv(&v->di_tv, rettv);
13452 done = TRUE;
13453 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013456
Bram Moolenaarba117c22015-09-29 16:53:22 +020013457#ifdef FEAT_WINDOWS
13458 if (need_switch_win)
13459 /* restore previous notion of curwin */
13460 restore_win(oldcurwin, oldtabpage, TRUE);
13461#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462 }
13463
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013464 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13465 /* use the default return value */
13466 copy_tv(&argvars[off + 2], rettv);
13467
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468 --emsg_off;
13469}
13470
13471/*
13472 * "glob()" function
13473 */
13474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013475f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013477 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013479 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013480
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013481 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013482 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013484 if (argvars[1].v_type != VAR_UNKNOWN)
13485 {
13486 if (get_tv_number_chk(&argvars[1], &error))
13487 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013489 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013490 if (get_tv_number_chk(&argvars[2], &error))
13491 {
13492 rettv->v_type = VAR_LIST;
13493 rettv->vval.v_list = NULL;
13494 }
13495 if (argvars[3].v_type != VAR_UNKNOWN
13496 && get_tv_number_chk(&argvars[3], &error))
13497 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013498 }
13499 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013500 if (!error)
13501 {
13502 ExpandInit(&xpc);
13503 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013504 if (p_wic)
13505 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013506 if (rettv->v_type == VAR_STRING)
13507 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013508 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013509 else if (rettv_list_alloc(rettv) != FAIL)
13510 {
13511 int i;
13512
13513 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13514 NULL, options, WILD_ALL_KEEP);
13515 for (i = 0; i < xpc.xp_numfiles; i++)
13516 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13517
13518 ExpandCleanup(&xpc);
13519 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013520 }
13521 else
13522 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523}
13524
13525/*
13526 * "globpath()" function
13527 */
13528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013529f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013531 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013533 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013534 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013535 garray_T ga;
13536 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013538 /* When the optional second argument is non-zero, don't remove matches
13539 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013541 if (argvars[2].v_type != VAR_UNKNOWN)
13542 {
13543 if (get_tv_number_chk(&argvars[2], &error))
13544 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013545 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013546 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013547 if (get_tv_number_chk(&argvars[3], &error))
13548 {
13549 rettv->v_type = VAR_LIST;
13550 rettv->vval.v_list = NULL;
13551 }
13552 if (argvars[4].v_type != VAR_UNKNOWN
13553 && get_tv_number_chk(&argvars[4], &error))
13554 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013555 }
13556 }
13557 if (file != NULL && !error)
13558 {
13559 ga_init2(&ga, (int)sizeof(char_u *), 10);
13560 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13561 if (rettv->v_type == VAR_STRING)
13562 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13563 else if (rettv_list_alloc(rettv) != FAIL)
13564 for (i = 0; i < ga.ga_len; ++i)
13565 list_append_string(rettv->vval.v_list,
13566 ((char_u **)(ga.ga_data))[i], -1);
13567 ga_clear_strings(&ga);
13568 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013570 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571}
13572
13573/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013574 * "glob2regpat()" function
13575 */
13576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013577f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013578{
13579 char_u *pat = get_tv_string_chk(&argvars[0]);
13580
13581 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013582 rettv->vval.v_string = (pat == NULL)
13583 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013584}
13585
13586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 * "has()" function
13588 */
13589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013590f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591{
13592 int i;
13593 char_u *name;
13594 int n = FALSE;
13595 static char *(has_list[]) =
13596 {
13597#ifdef AMIGA
13598 "amiga",
13599# ifdef FEAT_ARP
13600 "arp",
13601# endif
13602#endif
13603#ifdef __BEOS__
13604 "beos",
13605#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013606#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 "mac",
13608#endif
13609#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013610 "macunix", /* built with 'darwin' enabled */
13611#endif
13612#if defined(__APPLE__) && __APPLE__ == 1
13613 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615#ifdef __QNX__
13616 "qnx",
13617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618#ifdef UNIX
13619 "unix",
13620#endif
13621#ifdef VMS
13622 "vms",
13623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624#ifdef WIN32
13625 "win32",
13626#endif
13627#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13628 "win32unix",
13629#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013630#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013631 "win64",
13632#endif
13633#ifdef EBCDIC
13634 "ebcdic",
13635#endif
13636#ifndef CASE_INSENSITIVE_FILENAME
13637 "fname_case",
13638#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013639#ifdef HAVE_ACL
13640 "acl",
13641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642#ifdef FEAT_ARABIC
13643 "arabic",
13644#endif
13645#ifdef FEAT_AUTOCMD
13646 "autocmd",
13647#endif
13648#ifdef FEAT_BEVAL
13649 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013650# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13651 "balloon_multiline",
13652# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653#endif
13654#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13655 "builtin_terms",
13656# ifdef ALL_BUILTIN_TCAPS
13657 "all_builtin_terms",
13658# endif
13659#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013660#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13661 || defined(FEAT_GUI_W32) \
13662 || defined(FEAT_GUI_MOTIF))
13663 "browsefilter",
13664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665#ifdef FEAT_BYTEOFF
13666 "byte_offset",
13667#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013668#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013669 "channel",
13670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671#ifdef FEAT_CINDENT
13672 "cindent",
13673#endif
13674#ifdef FEAT_CLIENTSERVER
13675 "clientserver",
13676#endif
13677#ifdef FEAT_CLIPBOARD
13678 "clipboard",
13679#endif
13680#ifdef FEAT_CMDL_COMPL
13681 "cmdline_compl",
13682#endif
13683#ifdef FEAT_CMDHIST
13684 "cmdline_hist",
13685#endif
13686#ifdef FEAT_COMMENTS
13687 "comments",
13688#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013689#ifdef FEAT_CONCEAL
13690 "conceal",
13691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692#ifdef FEAT_CRYPT
13693 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013694 "crypt-blowfish",
13695 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696#endif
13697#ifdef FEAT_CSCOPE
13698 "cscope",
13699#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013700#ifdef FEAT_CURSORBIND
13701 "cursorbind",
13702#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013703#ifdef CURSOR_SHAPE
13704 "cursorshape",
13705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706#ifdef DEBUG
13707 "debug",
13708#endif
13709#ifdef FEAT_CON_DIALOG
13710 "dialog_con",
13711#endif
13712#ifdef FEAT_GUI_DIALOG
13713 "dialog_gui",
13714#endif
13715#ifdef FEAT_DIFF
13716 "diff",
13717#endif
13718#ifdef FEAT_DIGRAPHS
13719 "digraphs",
13720#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013721#ifdef FEAT_DIRECTX
13722 "directx",
13723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724#ifdef FEAT_DND
13725 "dnd",
13726#endif
13727#ifdef FEAT_EMACS_TAGS
13728 "emacs_tags",
13729#endif
13730 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013731 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#ifdef FEAT_SEARCH_EXTRA
13733 "extra_search",
13734#endif
13735#ifdef FEAT_FKMAP
13736 "farsi",
13737#endif
13738#ifdef FEAT_SEARCHPATH
13739 "file_in_path",
13740#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013741#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013742 "filterpipe",
13743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744#ifdef FEAT_FIND_ID
13745 "find_in_path",
13746#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013747#ifdef FEAT_FLOAT
13748 "float",
13749#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013750#ifdef FEAT_FOLDING
13751 "folding",
13752#endif
13753#ifdef FEAT_FOOTER
13754 "footer",
13755#endif
13756#if !defined(USE_SYSTEM) && defined(UNIX)
13757 "fork",
13758#endif
13759#ifdef FEAT_GETTEXT
13760 "gettext",
13761#endif
13762#ifdef FEAT_GUI
13763 "gui",
13764#endif
13765#ifdef FEAT_GUI_ATHENA
13766# ifdef FEAT_GUI_NEXTAW
13767 "gui_neXtaw",
13768# else
13769 "gui_athena",
13770# endif
13771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772#ifdef FEAT_GUI_GTK
13773 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013774# ifdef USE_GTK3
13775 "gui_gtk3",
13776# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013780#ifdef FEAT_GUI_GNOME
13781 "gui_gnome",
13782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783#ifdef FEAT_GUI_MAC
13784 "gui_mac",
13785#endif
13786#ifdef FEAT_GUI_MOTIF
13787 "gui_motif",
13788#endif
13789#ifdef FEAT_GUI_PHOTON
13790 "gui_photon",
13791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013792#ifdef FEAT_GUI_W32
13793 "gui_win32",
13794#endif
13795#ifdef FEAT_HANGULIN
13796 "hangul_input",
13797#endif
13798#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13799 "iconv",
13800#endif
13801#ifdef FEAT_INS_EXPAND
13802 "insert_expand",
13803#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013804#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013805 "job",
13806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807#ifdef FEAT_JUMPLIST
13808 "jumplist",
13809#endif
13810#ifdef FEAT_KEYMAP
13811 "keymap",
13812#endif
13813#ifdef FEAT_LANGMAP
13814 "langmap",
13815#endif
13816#ifdef FEAT_LIBCALL
13817 "libcall",
13818#endif
13819#ifdef FEAT_LINEBREAK
13820 "linebreak",
13821#endif
13822#ifdef FEAT_LISP
13823 "lispindent",
13824#endif
13825#ifdef FEAT_LISTCMDS
13826 "listcmds",
13827#endif
13828#ifdef FEAT_LOCALMAP
13829 "localmap",
13830#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013831#ifdef FEAT_LUA
13832# ifndef DYNAMIC_LUA
13833 "lua",
13834# endif
13835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836#ifdef FEAT_MENU
13837 "menu",
13838#endif
13839#ifdef FEAT_SESSION
13840 "mksession",
13841#endif
13842#ifdef FEAT_MODIFY_FNAME
13843 "modify_fname",
13844#endif
13845#ifdef FEAT_MOUSE
13846 "mouse",
13847#endif
13848#ifdef FEAT_MOUSESHAPE
13849 "mouseshape",
13850#endif
13851#if defined(UNIX) || defined(VMS)
13852# ifdef FEAT_MOUSE_DEC
13853 "mouse_dec",
13854# endif
13855# ifdef FEAT_MOUSE_GPM
13856 "mouse_gpm",
13857# endif
13858# ifdef FEAT_MOUSE_JSB
13859 "mouse_jsbterm",
13860# endif
13861# ifdef FEAT_MOUSE_NET
13862 "mouse_netterm",
13863# endif
13864# ifdef FEAT_MOUSE_PTERM
13865 "mouse_pterm",
13866# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013867# ifdef FEAT_MOUSE_SGR
13868 "mouse_sgr",
13869# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013870# ifdef FEAT_SYSMOUSE
13871 "mouse_sysmouse",
13872# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013873# ifdef FEAT_MOUSE_URXVT
13874 "mouse_urxvt",
13875# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876# ifdef FEAT_MOUSE_XTERM
13877 "mouse_xterm",
13878# endif
13879#endif
13880#ifdef FEAT_MBYTE
13881 "multi_byte",
13882#endif
13883#ifdef FEAT_MBYTE_IME
13884 "multi_byte_ime",
13885#endif
13886#ifdef FEAT_MULTI_LANG
13887 "multi_lang",
13888#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013889#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013890#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013891 "mzscheme",
13892#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894#ifdef FEAT_OLE
13895 "ole",
13896#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013897 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898#ifdef FEAT_PATH_EXTRA
13899 "path_extra",
13900#endif
13901#ifdef FEAT_PERL
13902#ifndef DYNAMIC_PERL
13903 "perl",
13904#endif
13905#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013906#ifdef FEAT_PERSISTENT_UNDO
13907 "persistent_undo",
13908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909#ifdef FEAT_PYTHON
13910#ifndef DYNAMIC_PYTHON
13911 "python",
13912#endif
13913#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013914#ifdef FEAT_PYTHON3
13915#ifndef DYNAMIC_PYTHON3
13916 "python3",
13917#endif
13918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919#ifdef FEAT_POSTSCRIPT
13920 "postscript",
13921#endif
13922#ifdef FEAT_PRINTER
13923 "printer",
13924#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013925#ifdef FEAT_PROFILE
13926 "profile",
13927#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013928#ifdef FEAT_RELTIME
13929 "reltime",
13930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931#ifdef FEAT_QUICKFIX
13932 "quickfix",
13933#endif
13934#ifdef FEAT_RIGHTLEFT
13935 "rightleft",
13936#endif
13937#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13938 "ruby",
13939#endif
13940#ifdef FEAT_SCROLLBIND
13941 "scrollbind",
13942#endif
13943#ifdef FEAT_CMDL_INFO
13944 "showcmd",
13945 "cmdline_info",
13946#endif
13947#ifdef FEAT_SIGNS
13948 "signs",
13949#endif
13950#ifdef FEAT_SMARTINDENT
13951 "smartindent",
13952#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013953#ifdef STARTUPTIME
13954 "startuptime",
13955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956#ifdef FEAT_STL_OPT
13957 "statusline",
13958#endif
13959#ifdef FEAT_SUN_WORKSHOP
13960 "sun_workshop",
13961#endif
13962#ifdef FEAT_NETBEANS_INTG
13963 "netbeans_intg",
13964#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013965#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013966 "spell",
13967#endif
13968#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 "syntax",
13970#endif
13971#if defined(USE_SYSTEM) || !defined(UNIX)
13972 "system",
13973#endif
13974#ifdef FEAT_TAG_BINS
13975 "tag_binary",
13976#endif
13977#ifdef FEAT_TAG_OLDSTATIC
13978 "tag_old_static",
13979#endif
13980#ifdef FEAT_TAG_ANYWHITE
13981 "tag_any_white",
13982#endif
13983#ifdef FEAT_TCL
13984# ifndef DYNAMIC_TCL
13985 "tcl",
13986# endif
13987#endif
13988#ifdef TERMINFO
13989 "terminfo",
13990#endif
13991#ifdef FEAT_TERMRESPONSE
13992 "termresponse",
13993#endif
13994#ifdef FEAT_TEXTOBJ
13995 "textobjects",
13996#endif
13997#ifdef HAVE_TGETENT
13998 "tgetent",
13999#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014000#ifdef FEAT_TIMERS
14001 "timers",
14002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003#ifdef FEAT_TITLE
14004 "title",
14005#endif
14006#ifdef FEAT_TOOLBAR
14007 "toolbar",
14008#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014009#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14010 "unnamedplus",
14011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012#ifdef FEAT_USR_CMDS
14013 "user-commands", /* was accidentally included in 5.4 */
14014 "user_commands",
14015#endif
14016#ifdef FEAT_VIMINFO
14017 "viminfo",
14018#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014019#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 "vertsplit",
14021#endif
14022#ifdef FEAT_VIRTUALEDIT
14023 "virtualedit",
14024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026#ifdef FEAT_VISUALEXTRA
14027 "visualextra",
14028#endif
14029#ifdef FEAT_VREPLACE
14030 "vreplace",
14031#endif
14032#ifdef FEAT_WILDIGN
14033 "wildignore",
14034#endif
14035#ifdef FEAT_WILDMENU
14036 "wildmenu",
14037#endif
14038#ifdef FEAT_WINDOWS
14039 "windows",
14040#endif
14041#ifdef FEAT_WAK
14042 "winaltkeys",
14043#endif
14044#ifdef FEAT_WRITEBACKUP
14045 "writebackup",
14046#endif
14047#ifdef FEAT_XIM
14048 "xim",
14049#endif
14050#ifdef FEAT_XFONTSET
14051 "xfontset",
14052#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014053#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014054 "xpm",
14055 "xpm_w32", /* for backward compatibility */
14056#else
14057# if defined(HAVE_XPM)
14058 "xpm",
14059# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061#ifdef USE_XSMP
14062 "xsmp",
14063#endif
14064#ifdef USE_XSMP_INTERACT
14065 "xsmp_interact",
14066#endif
14067#ifdef FEAT_XCLIPBOARD
14068 "xterm_clipboard",
14069#endif
14070#ifdef FEAT_XTERM_SAVE
14071 "xterm_save",
14072#endif
14073#if defined(UNIX) && defined(FEAT_X11)
14074 "X11",
14075#endif
14076 NULL
14077 };
14078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014079 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 for (i = 0; has_list[i] != NULL; ++i)
14081 if (STRICMP(name, has_list[i]) == 0)
14082 {
14083 n = TRUE;
14084 break;
14085 }
14086
14087 if (n == FALSE)
14088 {
14089 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014090 {
14091 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014092 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014093 && vim_isdigit(name[6])
14094 && vim_isdigit(name[8])
14095 && vim_isdigit(name[10]))
14096 {
14097 int major = atoi((char *)name + 6);
14098 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014099
14100 /* Expect "patch-9.9.01234". */
14101 n = (major < VIM_VERSION_MAJOR
14102 || (major == VIM_VERSION_MAJOR
14103 && (minor < VIM_VERSION_MINOR
14104 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014105 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014106 }
14107 else
14108 n = has_patch(atoi((char *)name + 5));
14109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110 else if (STRICMP(name, "vim_starting") == 0)
14111 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014112#ifdef FEAT_MBYTE
14113 else if (STRICMP(name, "multi_byte_encoding") == 0)
14114 n = has_mbyte;
14115#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014116#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14117 else if (STRICMP(name, "balloon_multiline") == 0)
14118 n = multiline_balloon_available();
14119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120#ifdef DYNAMIC_TCL
14121 else if (STRICMP(name, "tcl") == 0)
14122 n = tcl_enabled(FALSE);
14123#endif
14124#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14125 else if (STRICMP(name, "iconv") == 0)
14126 n = iconv_enabled(FALSE);
14127#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014128#ifdef DYNAMIC_LUA
14129 else if (STRICMP(name, "lua") == 0)
14130 n = lua_enabled(FALSE);
14131#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014132#ifdef DYNAMIC_MZSCHEME
14133 else if (STRICMP(name, "mzscheme") == 0)
14134 n = mzscheme_enabled(FALSE);
14135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136#ifdef DYNAMIC_RUBY
14137 else if (STRICMP(name, "ruby") == 0)
14138 n = ruby_enabled(FALSE);
14139#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014140#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141#ifdef DYNAMIC_PYTHON
14142 else if (STRICMP(name, "python") == 0)
14143 n = python_enabled(FALSE);
14144#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014145#endif
14146#ifdef FEAT_PYTHON3
14147#ifdef DYNAMIC_PYTHON3
14148 else if (STRICMP(name, "python3") == 0)
14149 n = python3_enabled(FALSE);
14150#endif
14151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152#ifdef DYNAMIC_PERL
14153 else if (STRICMP(name, "perl") == 0)
14154 n = perl_enabled(FALSE);
14155#endif
14156#ifdef FEAT_GUI
14157 else if (STRICMP(name, "gui_running") == 0)
14158 n = (gui.in_use || gui.starting);
14159# ifdef FEAT_GUI_W32
14160 else if (STRICMP(name, "gui_win32s") == 0)
14161 n = gui_is_win32s();
14162# endif
14163# ifdef FEAT_BROWSE
14164 else if (STRICMP(name, "browse") == 0)
14165 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14166# endif
14167#endif
14168#ifdef FEAT_SYN_HL
14169 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014170 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171#endif
14172#if defined(WIN3264)
14173 else if (STRICMP(name, "win95") == 0)
14174 n = mch_windows95();
14175#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014176#ifdef FEAT_NETBEANS_INTG
14177 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014178 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 }
14181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183}
14184
14185/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014186 * "has_key()" function
14187 */
14188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014189f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014190{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014191 if (argvars[0].v_type != VAR_DICT)
14192 {
14193 EMSG(_(e_dictreq));
14194 return;
14195 }
14196 if (argvars[0].vval.v_dict == NULL)
14197 return;
14198
14199 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014200 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014201}
14202
14203/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014204 * "haslocaldir()" function
14205 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014207f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014208{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014209 win_T *wp = NULL;
14210
14211 wp = find_tabwin(&argvars[0], &argvars[1]);
14212 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014213}
14214
14215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216 * "hasmapto()" function
14217 */
14218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014219f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220{
14221 char_u *name;
14222 char_u *mode;
14223 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014224 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014227 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228 mode = (char_u *)"nvo";
14229 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014231 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014232 if (argvars[2].v_type != VAR_UNKNOWN)
14233 abbr = get_tv_number(&argvars[2]);
14234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235
Bram Moolenaar2c932302006-03-18 21:42:09 +000014236 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240}
14241
14242/*
14243 * "histadd()" function
14244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014246f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247{
14248#ifdef FEAT_CMDHIST
14249 int histype;
14250 char_u *str;
14251 char_u buf[NUMBUFLEN];
14252#endif
14253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014254 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255 if (check_restricted() || check_secure())
14256 return;
14257#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014258 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14259 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 if (histype >= 0)
14261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014262 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263 if (*str != NUL)
14264 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014265 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014267 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 return;
14269 }
14270 }
14271#endif
14272}
14273
14274/*
14275 * "histdel()" function
14276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014278f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279{
14280#ifdef FEAT_CMDHIST
14281 int n;
14282 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014285 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14286 if (str == NULL)
14287 n = 0;
14288 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014290 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014291 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014293 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014294 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 else
14296 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014297 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014298 get_tv_string_buf(&argvars[1], buf));
14299 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300#endif
14301}
14302
14303/*
14304 * "histget()" function
14305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014307f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308{
14309#ifdef FEAT_CMDHIST
14310 int type;
14311 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014312 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014314 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14315 if (str == NULL)
14316 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014318 {
14319 type = get_histtype(str);
14320 if (argvars[1].v_type == VAR_UNKNOWN)
14321 idx = get_history_idx(type);
14322 else
14323 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14324 /* -1 on type error */
14325 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014328 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331}
14332
14333/*
14334 * "histnr()" function
14335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014337f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338{
14339 int i;
14340
14341#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 char_u *history = get_tv_string_chk(&argvars[0]);
14343
14344 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014345 if (i >= HIST_CMD && i < HIST_COUNT)
14346 i = get_history_idx(i);
14347 else
14348#endif
14349 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014350 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351}
14352
14353/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 * "highlightID(name)" function
14355 */
14356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014357f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014359 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360}
14361
14362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014363 * "highlight_exists()" function
14364 */
14365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014366f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014367{
14368 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14369}
14370
14371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 * "hostname()" function
14373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014375f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376{
14377 char_u hostname[256];
14378
14379 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380 rettv->v_type = VAR_STRING;
14381 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382}
14383
14384/*
14385 * iconv() function
14386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014388f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389{
14390#ifdef FEAT_MBYTE
14391 char_u buf1[NUMBUFLEN];
14392 char_u buf2[NUMBUFLEN];
14393 char_u *from, *to, *str;
14394 vimconv_T vimconv;
14395#endif
14396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->v_type = VAR_STRING;
14398 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399
14400#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014401 str = get_tv_string(&argvars[0]);
14402 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14403 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 vimconv.vc_type = CONV_NONE;
14405 convert_setup(&vimconv, from, to);
14406
14407 /* If the encodings are equal, no conversion needed. */
14408 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014409 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412
14413 convert_setup(&vimconv, NULL, NULL);
14414 vim_free(from);
14415 vim_free(to);
14416#endif
14417}
14418
14419/*
14420 * "indent()" function
14421 */
14422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014423f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424{
14425 linenr_T lnum;
14426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014427 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432}
14433
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014434/*
14435 * "index()" function
14436 */
14437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014438f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014439{
Bram Moolenaar33570922005-01-25 22:26:29 +000014440 list_T *l;
14441 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014442 long idx = 0;
14443 int ic = FALSE;
14444
14445 rettv->vval.v_number = -1;
14446 if (argvars[0].v_type != VAR_LIST)
14447 {
14448 EMSG(_(e_listreq));
14449 return;
14450 }
14451 l = argvars[0].vval.v_list;
14452 if (l != NULL)
14453 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014454 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014455 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014457 int error = FALSE;
14458
Bram Moolenaar758711c2005-02-02 23:11:38 +000014459 /* Start at specified item. Use the cached index that list_find()
14460 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014461 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014462 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014463 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014464 ic = get_tv_number_chk(&argvars[3], &error);
14465 if (error)
14466 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014467 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014468
Bram Moolenaar758711c2005-02-02 23:11:38 +000014469 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014470 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014471 {
14472 rettv->vval.v_number = idx;
14473 break;
14474 }
14475 }
14476}
14477
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478static int inputsecret_flag = 0;
14479
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014480static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014481
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014483 * This function is used by f_input() and f_inputdialog() functions. The third
14484 * argument to f_input() specifies the type of completion to use at the
14485 * prompt. The third argument to f_inputdialog() specifies the value to return
14486 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 */
14488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014489get_user_input(
14490 typval_T *argvars,
14491 typval_T *rettv,
14492 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 char_u *p = NULL;
14496 int c;
14497 char_u buf[NUMBUFLEN];
14498 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014499 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014500 int xp_type = EXPAND_NOTHING;
14501 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014503 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014504 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505
14506#ifdef NO_CONSOLE_INPUT
14507 /* While starting up, there is no place to enter text. */
14508 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510#endif
14511
14512 cmd_silent = FALSE; /* Want to see the prompt. */
14513 if (prompt != NULL)
14514 {
14515 /* Only the part of the message after the last NL is considered as
14516 * prompt for the command line */
14517 p = vim_strrchr(prompt, '\n');
14518 if (p == NULL)
14519 p = prompt;
14520 else
14521 {
14522 ++p;
14523 c = *p;
14524 *p = NUL;
14525 msg_start();
14526 msg_clr_eos();
14527 msg_puts_attr(prompt, echo_attr);
14528 msg_didout = FALSE;
14529 msg_starthere();
14530 *p = c;
14531 }
14532 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014534 if (argvars[1].v_type != VAR_UNKNOWN)
14535 {
14536 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14537 if (defstr != NULL)
14538 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014540 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014541 {
14542 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014543 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014544 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014545
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014546 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014547 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014548
Bram Moolenaar4463f292005-09-25 22:20:24 +000014549 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14550 if (xp_name == NULL)
14551 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014552
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014553 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014554
Bram Moolenaar4463f292005-09-25 22:20:24 +000014555 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14556 &xp_arg) == FAIL)
14557 return;
14558 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014559 }
14560
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014561 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014562 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014563 int save_ex_normal_busy = ex_normal_busy;
14564 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014565 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014566 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14567 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014568 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014569 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014570 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014571 && argvars[1].v_type != VAR_UNKNOWN
14572 && argvars[2].v_type != VAR_UNKNOWN)
14573 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14574 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014575
14576 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014578 /* since the user typed this, no need to wait for return */
14579 need_wait_return = FALSE;
14580 msg_didout = FALSE;
14581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 cmd_silent = cmd_silent_save;
14583}
14584
14585/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014586 * "input()" function
14587 * Also handles inputsecret() when inputsecret is set.
14588 */
14589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014590f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014591{
14592 get_user_input(argvars, rettv, FALSE);
14593}
14594
14595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 * "inputdialog()" function
14597 */
14598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014599f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014600{
14601#if defined(FEAT_GUI_TEXTDIALOG)
14602 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14603 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14604 {
14605 char_u *message;
14606 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014607 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014609 message = get_tv_string_chk(&argvars[0]);
14610 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014611 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014612 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613 else
14614 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014615 if (message != NULL && defstr != NULL
14616 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014617 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014618 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619 else
14620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 if (message != NULL && defstr != NULL
14622 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014623 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 rettv->vval.v_string = vim_strsave(
14625 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014627 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014628 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014629 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 }
14631 else
14632#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014633 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014634}
14635
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014636/*
14637 * "inputlist()" function
14638 */
14639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014640f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014641{
14642 listitem_T *li;
14643 int selected;
14644 int mouse_used;
14645
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014646#ifdef NO_CONSOLE_INPUT
14647 /* While starting up, there is no place to enter text. */
14648 if (no_console_input())
14649 return;
14650#endif
14651 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14652 {
14653 EMSG2(_(e_listarg), "inputlist()");
14654 return;
14655 }
14656
14657 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014658 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014659 lines_left = Rows; /* avoid more prompt */
14660 msg_scroll = TRUE;
14661 msg_clr_eos();
14662
14663 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14664 {
14665 msg_puts(get_tv_string(&li->li_tv));
14666 msg_putchar('\n');
14667 }
14668
14669 /* Ask for choice. */
14670 selected = prompt_for_number(&mouse_used);
14671 if (mouse_used)
14672 selected -= lines_left;
14673
14674 rettv->vval.v_number = selected;
14675}
14676
14677
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14679
14680/*
14681 * "inputrestore()" function
14682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014684f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685{
14686 if (ga_userinput.ga_len > 0)
14687 {
14688 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14690 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014691 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692 }
14693 else if (p_verbose > 1)
14694 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014695 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014696 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697 }
14698}
14699
14700/*
14701 * "inputsave()" function
14702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014704f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014705{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014706 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 if (ga_grow(&ga_userinput, 1) == OK)
14708 {
14709 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14710 + ga_userinput.ga_len);
14711 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014712 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014713 }
14714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014715 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716}
14717
14718/*
14719 * "inputsecret()" function
14720 */
14721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014722f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723{
14724 ++cmdline_star;
14725 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014726 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727 --cmdline_star;
14728 --inputsecret_flag;
14729}
14730
14731/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014732 * "insert()" function
14733 */
14734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014735f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014736{
14737 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014738 listitem_T *item;
14739 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014740 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014741
14742 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014743 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014744 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014745 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014746 {
14747 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014748 before = get_tv_number_chk(&argvars[2], &error);
14749 if (error)
14750 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014751
Bram Moolenaar758711c2005-02-02 23:11:38 +000014752 if (before == l->lv_len)
14753 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014754 else
14755 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014756 item = list_find(l, before);
14757 if (item == NULL)
14758 {
14759 EMSGN(_(e_listidx), before);
14760 l = NULL;
14761 }
14762 }
14763 if (l != NULL)
14764 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014765 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014766 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014767 }
14768 }
14769}
14770
14771/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014772 * "invert(expr)" function
14773 */
14774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014775f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014776{
14777 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14778}
14779
14780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781 * "isdirectory()" function
14782 */
14783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014784f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014786 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014787}
14788
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014789/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014790 * "islocked()" function
14791 */
14792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014793f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014794{
14795 lval_T lv;
14796 char_u *end;
14797 dictitem_T *di;
14798
14799 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014800 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14801 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014802 if (end != NULL && lv.ll_name != NULL)
14803 {
14804 if (*end != NUL)
14805 EMSG(_(e_trailing));
14806 else
14807 {
14808 if (lv.ll_tv == NULL)
14809 {
14810 if (check_changedtick(lv.ll_name))
14811 rettv->vval.v_number = 1; /* always locked */
14812 else
14813 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014814 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014815 if (di != NULL)
14816 {
14817 /* Consider a variable locked when:
14818 * 1. the variable itself is locked
14819 * 2. the value of the variable is locked.
14820 * 3. the List or Dict value is locked.
14821 */
14822 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14823 || tv_islocked(&di->di_tv));
14824 }
14825 }
14826 }
14827 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014828 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014829 else if (lv.ll_newkey != NULL)
14830 EMSG2(_(e_dictkey), lv.ll_newkey);
14831 else if (lv.ll_list != NULL)
14832 /* List item. */
14833 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14834 else
14835 /* Dictionary item. */
14836 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14837 }
14838 }
14839
14840 clear_lval(&lv);
14841}
14842
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014843#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14844/*
14845 * "isnan()" function
14846 */
14847 static void
14848f_isnan(typval_T *argvars, typval_T *rettv)
14849{
14850 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14851 && isnan(argvars[0].vval.v_float);
14852}
14853#endif
14854
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014855static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014856
14857/*
14858 * Turn a dict into a list:
14859 * "what" == 0: list of keys
14860 * "what" == 1: list of values
14861 * "what" == 2: list of items
14862 */
14863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014864dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014865{
Bram Moolenaar33570922005-01-25 22:26:29 +000014866 list_T *l2;
14867 dictitem_T *di;
14868 hashitem_T *hi;
14869 listitem_T *li;
14870 listitem_T *li2;
14871 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014872 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014873
Bram Moolenaar8c711452005-01-14 21:53:12 +000014874 if (argvars[0].v_type != VAR_DICT)
14875 {
14876 EMSG(_(e_dictreq));
14877 return;
14878 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014879 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014880 return;
14881
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014882 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014883 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014884
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014885 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014886 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014887 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014888 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014889 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014890 --todo;
14891 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014892
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014893 li = listitem_alloc();
14894 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014895 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014896 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014897
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014898 if (what == 0)
14899 {
14900 /* keys() */
14901 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014902 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014903 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14904 }
14905 else if (what == 1)
14906 {
14907 /* values() */
14908 copy_tv(&di->di_tv, &li->li_tv);
14909 }
14910 else
14911 {
14912 /* items() */
14913 l2 = list_alloc();
14914 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014915 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014916 li->li_tv.vval.v_list = l2;
14917 if (l2 == NULL)
14918 break;
14919 ++l2->lv_refcount;
14920
14921 li2 = listitem_alloc();
14922 if (li2 == NULL)
14923 break;
14924 list_append(l2, li2);
14925 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014926 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014927 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14928
14929 li2 = listitem_alloc();
14930 if (li2 == NULL)
14931 break;
14932 list_append(l2, li2);
14933 copy_tv(&di->di_tv, &li2->li_tv);
14934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014935 }
14936 }
14937}
14938
14939/*
14940 * "items(dict)" function
14941 */
14942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014943f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014944{
14945 dict_list(argvars, rettv, 2);
14946}
14947
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014948#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014949/*
14950 * Get the job from the argument.
14951 * Returns NULL if the job is invalid.
14952 */
14953 static job_T *
14954get_job_arg(typval_T *tv)
14955{
14956 job_T *job;
14957
14958 if (tv->v_type != VAR_JOB)
14959 {
14960 EMSG2(_(e_invarg2), get_tv_string(tv));
14961 return NULL;
14962 }
14963 job = tv->vval.v_job;
14964
14965 if (job == NULL)
14966 EMSG(_("E916: not a valid job"));
14967 return job;
14968}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014969
Bram Moolenaar835dc632016-02-07 14:27:38 +010014970/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014971 * "job_getchannel()" function
14972 */
14973 static void
14974f_job_getchannel(typval_T *argvars, typval_T *rettv)
14975{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014976 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014977
Bram Moolenaar65edff82016-02-21 16:40:11 +010014978 if (job != NULL)
14979 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014980 rettv->v_type = VAR_CHANNEL;
14981 rettv->vval.v_channel = job->jv_channel;
14982 if (job->jv_channel != NULL)
14983 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014984 }
14985}
14986
14987/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014988 * "job_info()" function
14989 */
14990 static void
14991f_job_info(typval_T *argvars, typval_T *rettv)
14992{
14993 job_T *job = get_job_arg(&argvars[0]);
14994
14995 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14996 job_info(job, rettv->vval.v_dict);
14997}
14998
14999/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015000 * "job_setoptions()" function
15001 */
15002 static void
15003f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15004{
15005 job_T *job = get_job_arg(&argvars[0]);
15006 jobopt_T opt;
15007
15008 if (job == NULL)
15009 return;
15010 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015011 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15012 job_set_options(job, &opt);
15013 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015014}
15015
15016/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015017 * "job_start()" function
15018 */
15019 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015020f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015021{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015022 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015023 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015024}
15025
15026/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015027 * "job_status()" function
15028 */
15029 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015030f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015031{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015032 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015033
Bram Moolenaar65edff82016-02-21 16:40:11 +010015034 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015035 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015036 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015037 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015038 }
15039}
15040
15041/*
15042 * "job_stop()" function
15043 */
15044 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015045f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015046{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015047 job_T *job = get_job_arg(&argvars[0]);
15048
15049 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015050 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015051}
15052#endif
15053
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015055 * "join()" function
15056 */
15057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015058f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015059{
15060 garray_T ga;
15061 char_u *sep;
15062
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015063 if (argvars[0].v_type != VAR_LIST)
15064 {
15065 EMSG(_(e_listreq));
15066 return;
15067 }
15068 if (argvars[0].vval.v_list == NULL)
15069 return;
15070 if (argvars[1].v_type == VAR_UNKNOWN)
15071 sep = (char_u *)" ";
15072 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015073 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015074
15075 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076
15077 if (sep != NULL)
15078 {
15079 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015080 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015081 ga_append(&ga, NUL);
15082 rettv->vval.v_string = (char_u *)ga.ga_data;
15083 }
15084 else
15085 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015086}
15087
15088/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015089 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015090 */
15091 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015092f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015093{
15094 js_read_T reader;
15095
15096 reader.js_buf = get_tv_string(&argvars[0]);
15097 reader.js_fill = NULL;
15098 reader.js_used = 0;
15099 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15100 EMSG(_(e_invarg));
15101}
15102
15103/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015104 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015105 */
15106 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015107f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015108{
15109 rettv->v_type = VAR_STRING;
15110 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15111}
15112
15113/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015114 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015115 */
15116 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015117f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015118{
15119 js_read_T reader;
15120
15121 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015122 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015123 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015124 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015125 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015126}
15127
15128/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015129 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015130 */
15131 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015132f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015133{
15134 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015135 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015136}
15137
15138/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015139 * "keys()" function
15140 */
15141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015142f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015143{
15144 dict_list(argvars, rettv, 0);
15145}
15146
15147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148 * "last_buffer_nr()" function.
15149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015151f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152{
15153 int n = 0;
15154 buf_T *buf;
15155
15156 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15157 if (n < buf->b_fnum)
15158 n = buf->b_fnum;
15159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015160 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015161}
15162
15163/*
15164 * "len()" function
15165 */
15166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015167f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015168{
15169 switch (argvars[0].v_type)
15170 {
15171 case VAR_STRING:
15172 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015173 rettv->vval.v_number = (varnumber_T)STRLEN(
15174 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015175 break;
15176 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015177 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015178 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015179 case VAR_DICT:
15180 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15181 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015182 case VAR_UNKNOWN:
15183 case VAR_SPECIAL:
15184 case VAR_FLOAT:
15185 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015186 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015187 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015188 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015189 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015190 break;
15191 }
15192}
15193
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015194static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015195
15196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015197libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015198{
15199#ifdef FEAT_LIBCALL
15200 char_u *string_in;
15201 char_u **string_result;
15202 int nr_result;
15203#endif
15204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015205 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015206 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015207 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015208
15209 if (check_restricted() || check_secure())
15210 return;
15211
15212#ifdef FEAT_LIBCALL
15213 /* The first two args must be strings, otherwise its meaningless */
15214 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15215 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015216 string_in = NULL;
15217 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015218 string_in = argvars[2].vval.v_string;
15219 if (type == VAR_NUMBER)
15220 string_result = NULL;
15221 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015222 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015223 if (mch_libcall(argvars[0].vval.v_string,
15224 argvars[1].vval.v_string,
15225 string_in,
15226 argvars[2].vval.v_number,
15227 string_result,
15228 &nr_result) == OK
15229 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015230 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015231 }
15232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233}
15234
15235/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236 * "libcall()" function
15237 */
15238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015239f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015240{
15241 libcall_common(argvars, rettv, VAR_STRING);
15242}
15243
15244/*
15245 * "libcallnr()" function
15246 */
15247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015248f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015249{
15250 libcall_common(argvars, rettv, VAR_NUMBER);
15251}
15252
15253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254 * "line(string)" function
15255 */
15256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015257f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258{
15259 linenr_T lnum = 0;
15260 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015261 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015263 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 if (fp != NULL)
15265 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015266 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267}
15268
15269/*
15270 * "line2byte(lnum)" function
15271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015273f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274{
15275#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015276 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277#else
15278 linenr_T lnum;
15279
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015280 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015282 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015284 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15285 if (rettv->vval.v_number >= 0)
15286 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015287#endif
15288}
15289
15290/*
15291 * "lispindent(lnum)" function
15292 */
15293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015294f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015295{
15296#ifdef FEAT_LISP
15297 pos_T pos;
15298 linenr_T lnum;
15299
15300 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15303 {
15304 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015305 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 curwin->w_cursor = pos;
15307 }
15308 else
15309#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015310 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311}
15312
15313/*
15314 * "localtime()" function
15315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015317f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015319 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320}
15321
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015322static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323
15324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015325get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326{
15327 char_u *keys;
15328 char_u *which;
15329 char_u buf[NUMBUFLEN];
15330 char_u *keys_buf = NULL;
15331 char_u *rhs;
15332 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015333 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015334 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015335 mapblock_T *mp;
15336 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337
15338 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015339 rettv->v_type = VAR_STRING;
15340 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 if (*keys == NUL)
15344 return;
15345
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015346 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015348 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015349 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015350 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015351 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015352 if (argvars[3].v_type != VAR_UNKNOWN)
15353 get_dict = get_tv_number(&argvars[3]);
15354 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 else
15357 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015358 if (which == NULL)
15359 return;
15360
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 mode = get_map_mode(&which, 0);
15362
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015363 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015364 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015366
15367 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015369 /* Return a string. */
15370 if (rhs != NULL)
15371 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015372
Bram Moolenaarbd743252010-10-20 21:23:33 +020015373 }
15374 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15375 {
15376 /* Return a dictionary. */
15377 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15378 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15379 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380
Bram Moolenaarbd743252010-10-20 21:23:33 +020015381 dict_add_nr_str(dict, "lhs", 0L, lhs);
15382 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15383 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15384 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15385 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15386 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15387 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015388 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015389 dict_add_nr_str(dict, "mode", 0L, mapmode);
15390
15391 vim_free(lhs);
15392 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 }
15394}
15395
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015396#ifdef FEAT_FLOAT
15397/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015398 * "log()" function
15399 */
15400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015401f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015402{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015403 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015404
15405 rettv->v_type = VAR_FLOAT;
15406 if (get_float_arg(argvars, &f) == OK)
15407 rettv->vval.v_float = log(f);
15408 else
15409 rettv->vval.v_float = 0.0;
15410}
15411
15412/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015413 * "log10()" function
15414 */
15415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015416f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015417{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015418 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015419
15420 rettv->v_type = VAR_FLOAT;
15421 if (get_float_arg(argvars, &f) == OK)
15422 rettv->vval.v_float = log10(f);
15423 else
15424 rettv->vval.v_float = 0.0;
15425}
15426#endif
15427
Bram Moolenaar1dced572012-04-05 16:54:08 +020015428#ifdef FEAT_LUA
15429/*
15430 * "luaeval()" function
15431 */
15432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015433f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015434{
15435 char_u *str;
15436 char_u buf[NUMBUFLEN];
15437
15438 str = get_tv_string_buf(&argvars[0], buf);
15439 do_luaeval(str, argvars + 1, rettv);
15440}
15441#endif
15442
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015444 * "map()" function
15445 */
15446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015447f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015448{
15449 filter_map(argvars, rettv, TRUE);
15450}
15451
15452/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015453 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 */
15455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015456f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015458 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459}
15460
15461/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015462 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 */
15464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015465f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015467 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468}
15469
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015470static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471
15472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015473find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015475 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015476 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015477 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478 char_u *pat;
15479 regmatch_T regmatch;
15480 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015481 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 char_u *save_cpo;
15483 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015484 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015485 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015486 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015487 list_T *l = NULL;
15488 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015489 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015490 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491
15492 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15493 save_cpo = p_cpo;
15494 p_cpo = (char_u *)"";
15495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015496 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015497 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015498 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015499 /* type 3: return empty list when there are no matches.
15500 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015501 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015502 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015503 if (type == 4
15504 && (list_append_string(rettv->vval.v_list,
15505 (char_u *)"", 0) == FAIL
15506 || list_append_number(rettv->vval.v_list,
15507 (varnumber_T)-1) == FAIL
15508 || list_append_number(rettv->vval.v_list,
15509 (varnumber_T)-1) == FAIL
15510 || list_append_number(rettv->vval.v_list,
15511 (varnumber_T)-1) == FAIL))
15512 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015513 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015514 rettv->vval.v_list = NULL;
15515 goto theend;
15516 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015517 }
15518 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015520 rettv->v_type = VAR_STRING;
15521 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015524 if (argvars[0].v_type == VAR_LIST)
15525 {
15526 if ((l = argvars[0].vval.v_list) == NULL)
15527 goto theend;
15528 li = l->lv_first;
15529 }
15530 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015531 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015532 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015533 len = (long)STRLEN(str);
15534 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015535
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015536 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15537 if (pat == NULL)
15538 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015539
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015540 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015542 int error = FALSE;
15543
15544 start = get_tv_number_chk(&argvars[2], &error);
15545 if (error)
15546 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015547 if (l != NULL)
15548 {
15549 li = list_find(l, start);
15550 if (li == NULL)
15551 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015552 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015553 }
15554 else
15555 {
15556 if (start < 0)
15557 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015558 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015559 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015560 /* When "count" argument is there ignore matches before "start",
15561 * otherwise skip part of the string. Differs when pattern is "^"
15562 * or "\<". */
15563 if (argvars[3].v_type != VAR_UNKNOWN)
15564 startcol = start;
15565 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015566 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015567 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015568 len -= start;
15569 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015570 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015571
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015572 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015573 nth = get_tv_number_chk(&argvars[3], &error);
15574 if (error)
15575 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 }
15577
15578 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15579 if (regmatch.regprog != NULL)
15580 {
15581 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015582
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015583 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015584 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015585 if (l != NULL)
15586 {
15587 if (li == NULL)
15588 {
15589 match = FALSE;
15590 break;
15591 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015592 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015593 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015594 if (str == NULL)
15595 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015596 }
15597
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015598 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015599
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015600 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015601 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015602 if (l == NULL && !match)
15603 break;
15604
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015605 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015606 if (l != NULL)
15607 {
15608 li = li->li_next;
15609 ++idx;
15610 }
15611 else
15612 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015613#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015614 startcol = (colnr_T)(regmatch.startp[0]
15615 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015616#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015617 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015618#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015619 if (startcol > (colnr_T)len
15620 || str + startcol <= regmatch.startp[0])
15621 {
15622 match = FALSE;
15623 break;
15624 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015625 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015626 }
15627
15628 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015630 if (type == 4)
15631 {
15632 listitem_T *li1 = rettv->vval.v_list->lv_first;
15633 listitem_T *li2 = li1->li_next;
15634 listitem_T *li3 = li2->li_next;
15635 listitem_T *li4 = li3->li_next;
15636
15637 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15638 (int)(regmatch.endp[0] - regmatch.startp[0]));
15639 li3->li_tv.vval.v_number =
15640 (varnumber_T)(regmatch.startp[0] - expr);
15641 li4->li_tv.vval.v_number =
15642 (varnumber_T)(regmatch.endp[0] - expr);
15643 if (l != NULL)
15644 li2->li_tv.vval.v_number = (varnumber_T)idx;
15645 }
15646 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015647 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015648 int i;
15649
15650 /* return list with matched string and submatches */
15651 for (i = 0; i < NSUBEXP; ++i)
15652 {
15653 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015654 {
15655 if (list_append_string(rettv->vval.v_list,
15656 (char_u *)"", 0) == FAIL)
15657 break;
15658 }
15659 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015660 regmatch.startp[i],
15661 (int)(regmatch.endp[i] - regmatch.startp[i]))
15662 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015663 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015664 }
15665 }
15666 else if (type == 2)
15667 {
15668 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015669 if (l != NULL)
15670 copy_tv(&li->li_tv, rettv);
15671 else
15672 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015674 }
15675 else if (l != NULL)
15676 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 else
15678 {
15679 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015680 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 (varnumber_T)(regmatch.startp[0] - str);
15682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015683 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015685 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015686 }
15687 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015688 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689 }
15690
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015691 if (type == 4 && l == NULL)
15692 /* matchstrpos() without a list: drop the second item. */
15693 listitem_remove(rettv->vval.v_list,
15694 rettv->vval.v_list->lv_first->li_next);
15695
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015697 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 p_cpo = save_cpo;
15699}
15700
15701/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702 * "match()" function
15703 */
15704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015705f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015706{
15707 find_some_match(argvars, rettv, 1);
15708}
15709
15710/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015711 * "matchadd()" function
15712 */
15713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015714f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015715{
15716#ifdef FEAT_SEARCH_EXTRA
15717 char_u buf[NUMBUFLEN];
15718 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15719 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15720 int prio = 10; /* default priority */
15721 int id = -1;
15722 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015723 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015724
15725 rettv->vval.v_number = -1;
15726
15727 if (grp == NULL || pat == NULL)
15728 return;
15729 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015730 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015731 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015732 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015733 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015734 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015735 if (argvars[4].v_type != VAR_UNKNOWN)
15736 {
15737 if (argvars[4].v_type != VAR_DICT)
15738 {
15739 EMSG(_(e_dictreq));
15740 return;
15741 }
15742 if (dict_find(argvars[4].vval.v_dict,
15743 (char_u *)"conceal", -1) != NULL)
15744 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15745 (char_u *)"conceal", FALSE);
15746 }
15747 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015748 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015749 if (error == TRUE)
15750 return;
15751 if (id >= 1 && id <= 3)
15752 {
15753 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15754 return;
15755 }
15756
Bram Moolenaar6561d522015-07-21 15:48:27 +020015757 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15758 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015759#endif
15760}
15761
15762/*
15763 * "matchaddpos()" function
15764 */
15765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015766f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015767{
15768#ifdef FEAT_SEARCH_EXTRA
15769 char_u buf[NUMBUFLEN];
15770 char_u *group;
15771 int prio = 10;
15772 int id = -1;
15773 int error = FALSE;
15774 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015775 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015776
15777 rettv->vval.v_number = -1;
15778
15779 group = get_tv_string_buf_chk(&argvars[0], buf);
15780 if (group == NULL)
15781 return;
15782
15783 if (argvars[1].v_type != VAR_LIST)
15784 {
15785 EMSG2(_(e_listarg), "matchaddpos()");
15786 return;
15787 }
15788 l = argvars[1].vval.v_list;
15789 if (l == NULL)
15790 return;
15791
15792 if (argvars[2].v_type != VAR_UNKNOWN)
15793 {
15794 prio = get_tv_number_chk(&argvars[2], &error);
15795 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015796 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015797 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015798 if (argvars[4].v_type != VAR_UNKNOWN)
15799 {
15800 if (argvars[4].v_type != VAR_DICT)
15801 {
15802 EMSG(_(e_dictreq));
15803 return;
15804 }
15805 if (dict_find(argvars[4].vval.v_dict,
15806 (char_u *)"conceal", -1) != NULL)
15807 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15808 (char_u *)"conceal", FALSE);
15809 }
15810 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015811 }
15812 if (error == TRUE)
15813 return;
15814
15815 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15816 if (id == 1 || id == 2)
15817 {
15818 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15819 return;
15820 }
15821
Bram Moolenaar6561d522015-07-21 15:48:27 +020015822 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15823 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015824#endif
15825}
15826
15827/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015828 * "matcharg()" function
15829 */
15830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015831f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015832{
15833 if (rettv_list_alloc(rettv) == OK)
15834 {
15835#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015836 int id = get_tv_number(&argvars[0]);
15837 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015838
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015839 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015840 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015841 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15842 {
15843 list_append_string(rettv->vval.v_list,
15844 syn_id2name(m->hlg_id), -1);
15845 list_append_string(rettv->vval.v_list, m->pattern, -1);
15846 }
15847 else
15848 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015849 list_append_string(rettv->vval.v_list, NULL, -1);
15850 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015851 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015852 }
15853#endif
15854 }
15855}
15856
15857/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015858 * "matchdelete()" function
15859 */
15860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015861f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015862{
15863#ifdef FEAT_SEARCH_EXTRA
15864 rettv->vval.v_number = match_delete(curwin,
15865 (int)get_tv_number(&argvars[0]), TRUE);
15866#endif
15867}
15868
15869/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015870 * "matchend()" function
15871 */
15872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015873f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015874{
15875 find_some_match(argvars, rettv, 0);
15876}
15877
15878/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015879 * "matchlist()" function
15880 */
15881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015882f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015883{
15884 find_some_match(argvars, rettv, 3);
15885}
15886
15887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015888 * "matchstr()" function
15889 */
15890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015891f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015892{
15893 find_some_match(argvars, rettv, 2);
15894}
15895
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015896/*
15897 * "matchstrpos()" function
15898 */
15899 static void
15900f_matchstrpos(typval_T *argvars, typval_T *rettv)
15901{
15902 find_some_match(argvars, rettv, 4);
15903}
15904
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015905static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015906
15907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015908max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015909{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015910 long n = 0;
15911 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015912 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015913
15914 if (argvars[0].v_type == VAR_LIST)
15915 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015916 list_T *l;
15917 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015918
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015919 l = argvars[0].vval.v_list;
15920 if (l != NULL)
15921 {
15922 li = l->lv_first;
15923 if (li != NULL)
15924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015925 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015926 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015927 {
15928 li = li->li_next;
15929 if (li == NULL)
15930 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015931 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015932 if (domax ? i > n : i < n)
15933 n = i;
15934 }
15935 }
15936 }
15937 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015938 else if (argvars[0].v_type == VAR_DICT)
15939 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015940 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015941 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015942 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015943 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015944
15945 d = argvars[0].vval.v_dict;
15946 if (d != NULL)
15947 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015948 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015949 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015950 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015951 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015953 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015954 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015955 if (first)
15956 {
15957 n = i;
15958 first = FALSE;
15959 }
15960 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015961 n = i;
15962 }
15963 }
15964 }
15965 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015966 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015967 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015968 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015969}
15970
15971/*
15972 * "max()" function
15973 */
15974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015975f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015976{
15977 max_min(argvars, rettv, TRUE);
15978}
15979
15980/*
15981 * "min()" function
15982 */
15983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015984f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015985{
15986 max_min(argvars, rettv, FALSE);
15987}
15988
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015989static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015990
15991/*
15992 * Create the directory in which "dir" is located, and higher levels when
15993 * needed.
15994 */
15995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015996mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015997{
15998 char_u *p;
15999 char_u *updir;
16000 int r = FAIL;
16001
16002 /* Get end of directory name in "dir".
16003 * We're done when it's "/" or "c:/". */
16004 p = gettail_sep(dir);
16005 if (p <= get_past_head(dir))
16006 return OK;
16007
16008 /* If the directory exists we're done. Otherwise: create it.*/
16009 updir = vim_strnsave(dir, (int)(p - dir));
16010 if (updir == NULL)
16011 return FAIL;
16012 if (mch_isdir(updir))
16013 r = OK;
16014 else if (mkdir_recurse(updir, prot) == OK)
16015 r = vim_mkdir_emsg(updir, prot);
16016 vim_free(updir);
16017 return r;
16018}
16019
16020#ifdef vim_mkdir
16021/*
16022 * "mkdir()" function
16023 */
16024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016025f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016026{
16027 char_u *dir;
16028 char_u buf[NUMBUFLEN];
16029 int prot = 0755;
16030
16031 rettv->vval.v_number = FAIL;
16032 if (check_restricted() || check_secure())
16033 return;
16034
16035 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016036 if (*dir == NUL)
16037 rettv->vval.v_number = FAIL;
16038 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016039 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016040 if (*gettail(dir) == NUL)
16041 /* remove trailing slashes */
16042 *gettail_sep(dir) = NUL;
16043
16044 if (argvars[1].v_type != VAR_UNKNOWN)
16045 {
16046 if (argvars[2].v_type != VAR_UNKNOWN)
16047 prot = get_tv_number_chk(&argvars[2], NULL);
16048 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16049 mkdir_recurse(dir, prot);
16050 }
16051 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016052 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016053}
16054#endif
16055
Bram Moolenaar0d660222005-01-07 21:51:51 +000016056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 * "mode()" function
16058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016060f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016061{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016062 char_u buf[3];
16063
16064 buf[1] = NUL;
16065 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066
Bram Moolenaar071d4272004-06-13 20:20:40 +000016067 if (VIsual_active)
16068 {
16069 if (VIsual_select)
16070 buf[0] = VIsual_mode + 's' - 'v';
16071 else
16072 buf[0] = VIsual_mode;
16073 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016074 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016075 || State == CONFIRM)
16076 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016078 if (State == ASKMORE)
16079 buf[1] = 'm';
16080 else if (State == CONFIRM)
16081 buf[1] = '?';
16082 }
16083 else if (State == EXTERNCMD)
16084 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016085 else if (State & INSERT)
16086 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016087#ifdef FEAT_VREPLACE
16088 if (State & VREPLACE_FLAG)
16089 {
16090 buf[0] = 'R';
16091 buf[1] = 'v';
16092 }
16093 else
16094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 if (State & REPLACE_FLAG)
16096 buf[0] = 'R';
16097 else
16098 buf[0] = 'i';
16099 }
16100 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016102 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016103 if (exmode_active)
16104 buf[1] = 'v';
16105 }
16106 else if (exmode_active)
16107 {
16108 buf[0] = 'c';
16109 buf[1] = 'e';
16110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016114 if (finish_op)
16115 buf[1] = 'o';
16116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016118 /* Clear out the minor mode when the argument is not a non-zero number or
16119 * non-empty string. */
16120 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016121 buf[1] = NUL;
16122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016123 rettv->vval.v_string = vim_strsave(buf);
16124 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125}
16126
Bram Moolenaar429fa852013-04-15 12:27:36 +020016127#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016128/*
16129 * "mzeval()" function
16130 */
16131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016132f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016133{
16134 char_u *str;
16135 char_u buf[NUMBUFLEN];
16136
16137 str = get_tv_string_buf(&argvars[0], buf);
16138 do_mzeval(str, rettv);
16139}
Bram Moolenaar75676462013-01-30 14:55:42 +010016140
16141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016142mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016143{
16144 typval_T argvars[3];
16145
16146 argvars[0].v_type = VAR_STRING;
16147 argvars[0].vval.v_string = name;
16148 copy_tv(args, &argvars[1]);
16149 argvars[2].v_type = VAR_UNKNOWN;
16150 f_call(argvars, rettv);
16151 clear_tv(&argvars[1]);
16152}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016153#endif
16154
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016156 * "nextnonblank()" function
16157 */
16158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016159f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016160{
16161 linenr_T lnum;
16162
16163 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16164 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016165 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016166 {
16167 lnum = 0;
16168 break;
16169 }
16170 if (*skipwhite(ml_get(lnum)) != NUL)
16171 break;
16172 }
16173 rettv->vval.v_number = lnum;
16174}
16175
16176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177 * "nr2char()" function
16178 */
16179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016180f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181{
16182 char_u buf[NUMBUFLEN];
16183
16184#ifdef FEAT_MBYTE
16185 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016186 {
16187 int utf8 = 0;
16188
16189 if (argvars[1].v_type != VAR_UNKNOWN)
16190 utf8 = get_tv_number_chk(&argvars[1], NULL);
16191 if (utf8)
16192 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16193 else
16194 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 else
16197#endif
16198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016199 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 buf[1] = NUL;
16201 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016202 rettv->v_type = VAR_STRING;
16203 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016204}
16205
16206/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016207 * "or(expr, expr)" function
16208 */
16209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016210f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016211{
16212 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16213 | get_tv_number_chk(&argvars[1], NULL);
16214}
16215
16216/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016217 * "pathshorten()" function
16218 */
16219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016220f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016221{
16222 char_u *p;
16223
16224 rettv->v_type = VAR_STRING;
16225 p = get_tv_string_chk(&argvars[0]);
16226 if (p == NULL)
16227 rettv->vval.v_string = NULL;
16228 else
16229 {
16230 p = vim_strsave(p);
16231 rettv->vval.v_string = p;
16232 if (p != NULL)
16233 shorten_dir(p);
16234 }
16235}
16236
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016237#ifdef FEAT_PERL
16238/*
16239 * "perleval()" function
16240 */
16241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016242f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016243{
16244 char_u *str;
16245 char_u buf[NUMBUFLEN];
16246
16247 str = get_tv_string_buf(&argvars[0], buf);
16248 do_perleval(str, rettv);
16249}
16250#endif
16251
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016252#ifdef FEAT_FLOAT
16253/*
16254 * "pow()" function
16255 */
16256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016257f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016258{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016259 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016260
16261 rettv->v_type = VAR_FLOAT;
16262 if (get_float_arg(argvars, &fx) == OK
16263 && get_float_arg(&argvars[1], &fy) == OK)
16264 rettv->vval.v_float = pow(fx, fy);
16265 else
16266 rettv->vval.v_float = 0.0;
16267}
16268#endif
16269
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016270/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016271 * "prevnonblank()" function
16272 */
16273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016274f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275{
16276 linenr_T lnum;
16277
16278 lnum = get_tv_lnum(argvars);
16279 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16280 lnum = 0;
16281 else
16282 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16283 --lnum;
16284 rettv->vval.v_number = lnum;
16285}
16286
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016287/* This dummy va_list is here because:
16288 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16289 * - locally in the function results in a "used before set" warning
16290 * - using va_start() to initialize it gives "function with fixed args" error */
16291static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016292
Bram Moolenaar8c711452005-01-14 21:53:12 +000016293/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016294 * "printf()" function
16295 */
16296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016297f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016298{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016299 char_u buf[NUMBUFLEN];
16300 int len;
16301 char_u *s;
16302 int saved_did_emsg = did_emsg;
16303 char *fmt;
16304
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016305 rettv->v_type = VAR_STRING;
16306 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016307
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016308 /* Get the required length, allocate the buffer and do it for real. */
16309 did_emsg = FALSE;
16310 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16311 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16312 if (!did_emsg)
16313 {
16314 s = alloc(len + 1);
16315 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016316 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016317 rettv->vval.v_string = s;
16318 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016319 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016320 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016321 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016322}
16323
16324/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016325 * "pumvisible()" function
16326 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016328f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016329{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016330#ifdef FEAT_INS_EXPAND
16331 if (pum_visible())
16332 rettv->vval.v_number = 1;
16333#endif
16334}
16335
Bram Moolenaardb913952012-06-29 12:54:53 +020016336#ifdef FEAT_PYTHON3
16337/*
16338 * "py3eval()" function
16339 */
16340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016341f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016342{
16343 char_u *str;
16344 char_u buf[NUMBUFLEN];
16345
16346 str = get_tv_string_buf(&argvars[0], buf);
16347 do_py3eval(str, rettv);
16348}
16349#endif
16350
16351#ifdef FEAT_PYTHON
16352/*
16353 * "pyeval()" function
16354 */
16355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016356f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016357{
16358 char_u *str;
16359 char_u buf[NUMBUFLEN];
16360
16361 str = get_tv_string_buf(&argvars[0], buf);
16362 do_pyeval(str, rettv);
16363}
16364#endif
16365
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016367 * "range()" function
16368 */
16369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016370f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016371{
16372 long start;
16373 long end;
16374 long stride = 1;
16375 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016376 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016378 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016379 if (argvars[1].v_type == VAR_UNKNOWN)
16380 {
16381 end = start - 1;
16382 start = 0;
16383 }
16384 else
16385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016386 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016387 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016388 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016389 }
16390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016391 if (error)
16392 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016393 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016394 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016395 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016396 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016397 else
16398 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016399 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016400 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016401 if (list_append_number(rettv->vval.v_list,
16402 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016403 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016404 }
16405}
16406
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016407/*
16408 * "readfile()" function
16409 */
16410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016411f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016412{
16413 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016414 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016415 char_u *fname;
16416 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016417 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16418 int io_size = sizeof(buf);
16419 int readlen; /* size of last fread() */
16420 char_u *prev = NULL; /* previously read bytes, if any */
16421 long prevlen = 0; /* length of data in prev */
16422 long prevsize = 0; /* size of prev buffer */
16423 long maxline = MAXLNUM;
16424 long cnt = 0;
16425 char_u *p; /* position in buf */
16426 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016427
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016428 if (argvars[1].v_type != VAR_UNKNOWN)
16429 {
16430 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16431 binary = TRUE;
16432 if (argvars[2].v_type != VAR_UNKNOWN)
16433 maxline = get_tv_number(&argvars[2]);
16434 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016435
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016436 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016437 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016438
16439 /* Always open the file in binary mode, library functions have a mind of
16440 * their own about CR-LF conversion. */
16441 fname = get_tv_string(&argvars[0]);
16442 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16443 {
16444 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16445 return;
16446 }
16447
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016448 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016449 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016450 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016451
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016452 /* This for loop processes what was read, but is also entered at end
16453 * of file so that either:
16454 * - an incomplete line gets written
16455 * - a "binary" file gets an empty line at the end if it ends in a
16456 * newline. */
16457 for (p = buf, start = buf;
16458 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16459 ++p)
16460 {
16461 if (*p == '\n' || readlen <= 0)
16462 {
16463 listitem_T *li;
16464 char_u *s = NULL;
16465 long_u len = p - start;
16466
16467 /* Finished a line. Remove CRs before NL. */
16468 if (readlen > 0 && !binary)
16469 {
16470 while (len > 0 && start[len - 1] == '\r')
16471 --len;
16472 /* removal may cross back to the "prev" string */
16473 if (len == 0)
16474 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16475 --prevlen;
16476 }
16477 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016478 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016479 else
16480 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016481 /* Change "prev" buffer to be the right size. This way
16482 * the bytes are only copied once, and very long lines are
16483 * allocated only once. */
16484 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016485 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016486 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016487 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016488 prev = NULL; /* the list will own the string */
16489 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016490 }
16491 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016492 if (s == NULL)
16493 {
16494 do_outofmem_msg((long_u) prevlen + len + 1);
16495 failed = TRUE;
16496 break;
16497 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016498
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016499 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016500 {
16501 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016502 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016503 break;
16504 }
16505 li->li_tv.v_type = VAR_STRING;
16506 li->li_tv.v_lock = 0;
16507 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016508 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016509
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016510 start = p + 1; /* step over newline */
16511 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016512 break;
16513 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016514 else if (*p == NUL)
16515 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016516#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016517 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16518 * when finding the BF and check the previous two bytes. */
16519 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016520 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016521 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16522 * + 1, these may be in the "prev" string. */
16523 char_u back1 = p >= buf + 1 ? p[-1]
16524 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16525 char_u back2 = p >= buf + 2 ? p[-2]
16526 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16527 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016528
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016529 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016530 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016531 char_u *dest = p - 2;
16532
16533 /* Usually a BOM is at the beginning of a file, and so at
16534 * the beginning of a line; then we can just step over it.
16535 */
16536 if (start == dest)
16537 start = p + 1;
16538 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016539 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016540 /* have to shuffle buf to close gap */
16541 int adjust_prevlen = 0;
16542
16543 if (dest < buf)
16544 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016545 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016546 dest = buf;
16547 }
16548 if (readlen > p - buf + 1)
16549 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16550 readlen -= 3 - adjust_prevlen;
16551 prevlen -= adjust_prevlen;
16552 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016553 }
16554 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016555 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016556#endif
16557 } /* for */
16558
16559 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16560 break;
16561 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016562 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016563 /* There's part of a line in buf, store it in "prev". */
16564 if (p - start + prevlen >= prevsize)
16565 {
16566 /* need bigger "prev" buffer */
16567 char_u *newprev;
16568
16569 /* A common use case is ordinary text files and "prev" gets a
16570 * fragment of a line, so the first allocation is made
16571 * small, to avoid repeatedly 'allocing' large and
16572 * 'reallocing' small. */
16573 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016574 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016575 else
16576 {
16577 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016578 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016579 prevsize = grow50pc > growmin ? grow50pc : growmin;
16580 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016581 newprev = prev == NULL ? alloc(prevsize)
16582 : vim_realloc(prev, prevsize);
16583 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016584 {
16585 do_outofmem_msg((long_u)prevsize);
16586 failed = TRUE;
16587 break;
16588 }
16589 prev = newprev;
16590 }
16591 /* Add the line part to end of "prev". */
16592 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016593 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016594 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016595 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016596
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016597 /*
16598 * For a negative line count use only the lines at the end of the file,
16599 * free the rest.
16600 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016601 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016602 while (cnt > -maxline)
16603 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016604 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016605 --cnt;
16606 }
16607
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016608 if (failed)
16609 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016610 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016611 /* readfile doc says an empty list is returned on error */
16612 rettv->vval.v_list = list_alloc();
16613 }
16614
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016615 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016616 fclose(fd);
16617}
16618
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016619#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016620static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016621
16622/*
16623 * Convert a List to proftime_T.
16624 * Return FAIL when there is something wrong.
16625 */
16626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016627list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016628{
16629 long n1, n2;
16630 int error = FALSE;
16631
16632 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16633 || arg->vval.v_list->lv_len != 2)
16634 return FAIL;
16635 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16636 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16637# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016638 tm->HighPart = n1;
16639 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016640# else
16641 tm->tv_sec = n1;
16642 tm->tv_usec = n2;
16643# endif
16644 return error ? FAIL : OK;
16645}
16646#endif /* FEAT_RELTIME */
16647
16648/*
16649 * "reltime()" function
16650 */
16651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016652f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016653{
16654#ifdef FEAT_RELTIME
16655 proftime_T res;
16656 proftime_T start;
16657
16658 if (argvars[0].v_type == VAR_UNKNOWN)
16659 {
16660 /* No arguments: get current time. */
16661 profile_start(&res);
16662 }
16663 else if (argvars[1].v_type == VAR_UNKNOWN)
16664 {
16665 if (list2proftime(&argvars[0], &res) == FAIL)
16666 return;
16667 profile_end(&res);
16668 }
16669 else
16670 {
16671 /* Two arguments: compute the difference. */
16672 if (list2proftime(&argvars[0], &start) == FAIL
16673 || list2proftime(&argvars[1], &res) == FAIL)
16674 return;
16675 profile_sub(&res, &start);
16676 }
16677
16678 if (rettv_list_alloc(rettv) == OK)
16679 {
16680 long n1, n2;
16681
16682# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016683 n1 = res.HighPart;
16684 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016685# else
16686 n1 = res.tv_sec;
16687 n2 = res.tv_usec;
16688# endif
16689 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16690 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16691 }
16692#endif
16693}
16694
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016695#ifdef FEAT_FLOAT
16696/*
16697 * "reltimefloat()" function
16698 */
16699 static void
16700f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16701{
16702# ifdef FEAT_RELTIME
16703 proftime_T tm;
16704# endif
16705
16706 rettv->v_type = VAR_FLOAT;
16707 rettv->vval.v_float = 0;
16708# ifdef FEAT_RELTIME
16709 if (list2proftime(&argvars[0], &tm) == OK)
16710 rettv->vval.v_float = profile_float(&tm);
16711# endif
16712}
16713#endif
16714
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016715/*
16716 * "reltimestr()" function
16717 */
16718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016719f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016720{
16721#ifdef FEAT_RELTIME
16722 proftime_T tm;
16723#endif
16724
16725 rettv->v_type = VAR_STRING;
16726 rettv->vval.v_string = NULL;
16727#ifdef FEAT_RELTIME
16728 if (list2proftime(&argvars[0], &tm) == OK)
16729 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16730#endif
16731}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016732
Bram Moolenaar0d660222005-01-07 21:51:51 +000016733#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016734static void make_connection(void);
16735static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016736
16737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016738make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016739{
16740 if (X_DISPLAY == NULL
16741# ifdef FEAT_GUI
16742 && !gui.in_use
16743# endif
16744 )
16745 {
16746 x_force_connect = TRUE;
16747 setup_term_clip();
16748 x_force_connect = FALSE;
16749 }
16750}
16751
16752 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016753check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016754{
16755 make_connection();
16756 if (X_DISPLAY == NULL)
16757 {
16758 EMSG(_("E240: No connection to Vim server"));
16759 return FAIL;
16760 }
16761 return OK;
16762}
16763#endif
16764
16765#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016766static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016767
16768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016769remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016770{
16771 char_u *server_name;
16772 char_u *keys;
16773 char_u *r = NULL;
16774 char_u buf[NUMBUFLEN];
16775# ifdef WIN32
16776 HWND w;
16777# else
16778 Window w;
16779# endif
16780
16781 if (check_restricted() || check_secure())
16782 return;
16783
16784# ifdef FEAT_X11
16785 if (check_connection() == FAIL)
16786 return;
16787# endif
16788
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016789 server_name = get_tv_string_chk(&argvars[0]);
16790 if (server_name == NULL)
16791 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016792 keys = get_tv_string_buf(&argvars[1], buf);
16793# ifdef WIN32
16794 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16795# else
16796 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16797 < 0)
16798# endif
16799 {
16800 if (r != NULL)
16801 EMSG(r); /* sending worked but evaluation failed */
16802 else
16803 EMSG2(_("E241: Unable to send to %s"), server_name);
16804 return;
16805 }
16806
16807 rettv->vval.v_string = r;
16808
16809 if (argvars[2].v_type != VAR_UNKNOWN)
16810 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016811 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016812 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016813 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016814
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016815 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016816 v.di_tv.v_type = VAR_STRING;
16817 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016818 idvar = get_tv_string_chk(&argvars[2]);
16819 if (idvar != NULL)
16820 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016821 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016822 }
16823}
16824#endif
16825
16826/*
16827 * "remote_expr()" function
16828 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016830f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016831{
16832 rettv->v_type = VAR_STRING;
16833 rettv->vval.v_string = NULL;
16834#ifdef FEAT_CLIENTSERVER
16835 remote_common(argvars, rettv, TRUE);
16836#endif
16837}
16838
16839/*
16840 * "remote_foreground()" function
16841 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016843f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016844{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016845#ifdef FEAT_CLIENTSERVER
16846# ifdef WIN32
16847 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016848 {
16849 char_u *server_name = get_tv_string_chk(&argvars[0]);
16850
16851 if (server_name != NULL)
16852 serverForeground(server_name);
16853 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016854# else
16855 /* Send a foreground() expression to the server. */
16856 argvars[1].v_type = VAR_STRING;
16857 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16858 argvars[2].v_type = VAR_UNKNOWN;
16859 remote_common(argvars, rettv, TRUE);
16860 vim_free(argvars[1].vval.v_string);
16861# endif
16862#endif
16863}
16864
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016866f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016867{
16868#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016869 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016870 char_u *s = NULL;
16871# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016872 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016874 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016875
16876 if (check_restricted() || check_secure())
16877 {
16878 rettv->vval.v_number = -1;
16879 return;
16880 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016881 serverid = get_tv_string_chk(&argvars[0]);
16882 if (serverid == NULL)
16883 {
16884 rettv->vval.v_number = -1;
16885 return; /* type error; errmsg already given */
16886 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016888 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889 if (n == 0)
16890 rettv->vval.v_number = -1;
16891 else
16892 {
16893 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16894 rettv->vval.v_number = (s != NULL);
16895 }
16896# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897 if (check_connection() == FAIL)
16898 return;
16899
16900 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016901 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902# endif
16903
16904 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16905 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016906 char_u *retvar;
16907
Bram Moolenaar33570922005-01-25 22:26:29 +000016908 v.di_tv.v_type = VAR_STRING;
16909 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016910 retvar = get_tv_string_chk(&argvars[1]);
16911 if (retvar != NULL)
16912 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016913 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914 }
16915#else
16916 rettv->vval.v_number = -1;
16917#endif
16918}
16919
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016921f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922{
16923 char_u *r = NULL;
16924
16925#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016926 char_u *serverid = get_tv_string_chk(&argvars[0]);
16927
16928 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929 {
16930# ifdef WIN32
16931 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016932 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016934 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935 if (n != 0)
16936 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16937 if (r == NULL)
16938# else
16939 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016940 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941# endif
16942 EMSG(_("E277: Unable to read a server reply"));
16943 }
16944#endif
16945 rettv->v_type = VAR_STRING;
16946 rettv->vval.v_string = r;
16947}
16948
16949/*
16950 * "remote_send()" function
16951 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016953f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954{
16955 rettv->v_type = VAR_STRING;
16956 rettv->vval.v_string = NULL;
16957#ifdef FEAT_CLIENTSERVER
16958 remote_common(argvars, rettv, FALSE);
16959#endif
16960}
16961
16962/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016963 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016964 */
16965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016966f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016967{
Bram Moolenaar33570922005-01-25 22:26:29 +000016968 list_T *l;
16969 listitem_T *item, *item2;
16970 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016971 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016972 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016973 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016974 dict_T *d;
16975 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016976 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016977
Bram Moolenaar8c711452005-01-14 21:53:12 +000016978 if (argvars[0].v_type == VAR_DICT)
16979 {
16980 if (argvars[2].v_type != VAR_UNKNOWN)
16981 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016982 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016983 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016985 key = get_tv_string_chk(&argvars[1]);
16986 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016987 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016988 di = dict_find(d, key, -1);
16989 if (di == NULL)
16990 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016991 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16992 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016993 {
16994 *rettv = di->di_tv;
16995 init_tv(&di->di_tv);
16996 dictitem_remove(d, di);
16997 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016998 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016999 }
17000 }
17001 else if (argvars[0].v_type != VAR_LIST)
17002 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017003 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017004 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 int error = FALSE;
17007
17008 idx = get_tv_number_chk(&argvars[1], &error);
17009 if (error)
17010 ; /* type error: do nothing, errmsg already given */
17011 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017012 EMSGN(_(e_listidx), idx);
17013 else
17014 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017015 if (argvars[2].v_type == VAR_UNKNOWN)
17016 {
17017 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017018 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017019 *rettv = item->li_tv;
17020 vim_free(item);
17021 }
17022 else
17023 {
17024 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017025 end = get_tv_number_chk(&argvars[2], &error);
17026 if (error)
17027 ; /* type error: do nothing */
17028 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017029 EMSGN(_(e_listidx), end);
17030 else
17031 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017032 int cnt = 0;
17033
17034 for (li = item; li != NULL; li = li->li_next)
17035 {
17036 ++cnt;
17037 if (li == item2)
17038 break;
17039 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017040 if (li == NULL) /* didn't find "item2" after "item" */
17041 EMSG(_(e_invrange));
17042 else
17043 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017044 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017045 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017046 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017047 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017048 l->lv_first = item;
17049 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017050 item->li_prev = NULL;
17051 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017052 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017053 }
17054 }
17055 }
17056 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017057 }
17058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059}
17060
17061/*
17062 * "rename({from}, {to})" function
17063 */
17064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017065f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066{
17067 char_u buf[NUMBUFLEN];
17068
17069 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017070 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017072 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17073 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074}
17075
17076/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017077 * "repeat()" function
17078 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017080f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017081{
17082 char_u *p;
17083 int n;
17084 int slen;
17085 int len;
17086 char_u *r;
17087 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017088
17089 n = get_tv_number(&argvars[1]);
17090 if (argvars[0].v_type == VAR_LIST)
17091 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017092 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017093 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017094 if (list_extend(rettv->vval.v_list,
17095 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017096 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017097 }
17098 else
17099 {
17100 p = get_tv_string(&argvars[0]);
17101 rettv->v_type = VAR_STRING;
17102 rettv->vval.v_string = NULL;
17103
17104 slen = (int)STRLEN(p);
17105 len = slen * n;
17106 if (len <= 0)
17107 return;
17108
17109 r = alloc(len + 1);
17110 if (r != NULL)
17111 {
17112 for (i = 0; i < n; i++)
17113 mch_memmove(r + i * slen, p, (size_t)slen);
17114 r[len] = NUL;
17115 }
17116
17117 rettv->vval.v_string = r;
17118 }
17119}
17120
17121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 * "resolve()" function
17123 */
17124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017125f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126{
17127 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017128#ifdef HAVE_READLINK
17129 char_u *buf = NULL;
17130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017132 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133#ifdef FEAT_SHORTCUT
17134 {
17135 char_u *v = NULL;
17136
17137 v = mch_resolve_shortcut(p);
17138 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017139 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017141 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 }
17143#else
17144# ifdef HAVE_READLINK
17145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146 char_u *cpy;
17147 int len;
17148 char_u *remain = NULL;
17149 char_u *q;
17150 int is_relative_to_current = FALSE;
17151 int has_trailing_pathsep = FALSE;
17152 int limit = 100;
17153
17154 p = vim_strsave(p);
17155
17156 if (p[0] == '.' && (vim_ispathsep(p[1])
17157 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17158 is_relative_to_current = TRUE;
17159
17160 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017161 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017164 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166
17167 q = getnextcomp(p);
17168 if (*q != NUL)
17169 {
17170 /* Separate the first path component in "p", and keep the
17171 * remainder (beginning with the path separator). */
17172 remain = vim_strsave(q - 1);
17173 q[-1] = NUL;
17174 }
17175
Bram Moolenaard9462e32011-04-11 21:35:11 +020017176 buf = alloc(MAXPATHL + 1);
17177 if (buf == NULL)
17178 goto fail;
17179
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 for (;;)
17181 {
17182 for (;;)
17183 {
17184 len = readlink((char *)p, (char *)buf, MAXPATHL);
17185 if (len <= 0)
17186 break;
17187 buf[len] = NUL;
17188
17189 if (limit-- == 0)
17190 {
17191 vim_free(p);
17192 vim_free(remain);
17193 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017194 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195 goto fail;
17196 }
17197
17198 /* Ensure that the result will have a trailing path separator
17199 * if the argument has one. */
17200 if (remain == NULL && has_trailing_pathsep)
17201 add_pathsep(buf);
17202
17203 /* Separate the first path component in the link value and
17204 * concatenate the remainders. */
17205 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17206 if (*q != NUL)
17207 {
17208 if (remain == NULL)
17209 remain = vim_strsave(q - 1);
17210 else
17211 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017212 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213 if (cpy != NULL)
17214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 vim_free(remain);
17216 remain = cpy;
17217 }
17218 }
17219 q[-1] = NUL;
17220 }
17221
17222 q = gettail(p);
17223 if (q > p && *q == NUL)
17224 {
17225 /* Ignore trailing path separator. */
17226 q[-1] = NUL;
17227 q = gettail(p);
17228 }
17229 if (q > p && !mch_isFullName(buf))
17230 {
17231 /* symlink is relative to directory of argument */
17232 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17233 if (cpy != NULL)
17234 {
17235 STRCPY(cpy, p);
17236 STRCPY(gettail(cpy), buf);
17237 vim_free(p);
17238 p = cpy;
17239 }
17240 }
17241 else
17242 {
17243 vim_free(p);
17244 p = vim_strsave(buf);
17245 }
17246 }
17247
17248 if (remain == NULL)
17249 break;
17250
17251 /* Append the first path component of "remain" to "p". */
17252 q = getnextcomp(remain + 1);
17253 len = q - remain - (*q != NUL);
17254 cpy = vim_strnsave(p, STRLEN(p) + len);
17255 if (cpy != NULL)
17256 {
17257 STRNCAT(cpy, remain, len);
17258 vim_free(p);
17259 p = cpy;
17260 }
17261 /* Shorten "remain". */
17262 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017263 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 else
17265 {
17266 vim_free(remain);
17267 remain = NULL;
17268 }
17269 }
17270
17271 /* If the result is a relative path name, make it explicitly relative to
17272 * the current directory if and only if the argument had this form. */
17273 if (!vim_ispathsep(*p))
17274 {
17275 if (is_relative_to_current
17276 && *p != NUL
17277 && !(p[0] == '.'
17278 && (p[1] == NUL
17279 || vim_ispathsep(p[1])
17280 || (p[1] == '.'
17281 && (p[2] == NUL
17282 || vim_ispathsep(p[2]))))))
17283 {
17284 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017285 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 if (cpy != NULL)
17287 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288 vim_free(p);
17289 p = cpy;
17290 }
17291 }
17292 else if (!is_relative_to_current)
17293 {
17294 /* Strip leading "./". */
17295 q = p;
17296 while (q[0] == '.' && vim_ispathsep(q[1]))
17297 q += 2;
17298 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017299 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300 }
17301 }
17302
17303 /* Ensure that the result will have no trailing path separator
17304 * if the argument had none. But keep "/" or "//". */
17305 if (!has_trailing_pathsep)
17306 {
17307 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017308 if (after_pathsep(p, q))
17309 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 }
17311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017312 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313 }
17314# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017315 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316# endif
17317#endif
17318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017319 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320
17321#ifdef HAVE_READLINK
17322fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017323 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017325 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017326}
17327
17328/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017329 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 */
17331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017332f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333{
Bram Moolenaar33570922005-01-25 22:26:29 +000017334 list_T *l;
17335 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336
Bram Moolenaar0d660222005-01-07 21:51:51 +000017337 if (argvars[0].v_type != VAR_LIST)
17338 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017339 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017340 && !tv_check_lock(l->lv_lock,
17341 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017342 {
17343 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017344 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017345 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017346 while (li != NULL)
17347 {
17348 ni = li->li_prev;
17349 list_append(l, li);
17350 li = ni;
17351 }
17352 rettv->vval.v_list = l;
17353 rettv->v_type = VAR_LIST;
17354 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017355 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357}
17358
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017359#define SP_NOMOVE 0x01 /* don't move cursor */
17360#define SP_REPEAT 0x02 /* repeat to find outer pair */
17361#define SP_RETCOUNT 0x04 /* return matchcount */
17362#define SP_SETPCMARK 0x08 /* set previous context mark */
17363#define SP_START 0x10 /* accept match at start position */
17364#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17365#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017366#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017367
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017368static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017369
17370/*
17371 * Get flags for a search function.
17372 * Possibly sets "p_ws".
17373 * Returns BACKWARD, FORWARD or zero (for an error).
17374 */
17375 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017376get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017377{
17378 int dir = FORWARD;
17379 char_u *flags;
17380 char_u nbuf[NUMBUFLEN];
17381 int mask;
17382
17383 if (varp->v_type != VAR_UNKNOWN)
17384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017385 flags = get_tv_string_buf_chk(varp, nbuf);
17386 if (flags == NULL)
17387 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017388 while (*flags != NUL)
17389 {
17390 switch (*flags)
17391 {
17392 case 'b': dir = BACKWARD; break;
17393 case 'w': p_ws = TRUE; break;
17394 case 'W': p_ws = FALSE; break;
17395 default: mask = 0;
17396 if (flagsp != NULL)
17397 switch (*flags)
17398 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017399 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017400 case 'e': mask = SP_END; break;
17401 case 'm': mask = SP_RETCOUNT; break;
17402 case 'n': mask = SP_NOMOVE; break;
17403 case 'p': mask = SP_SUBPAT; break;
17404 case 'r': mask = SP_REPEAT; break;
17405 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017406 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017407 }
17408 if (mask == 0)
17409 {
17410 EMSG2(_(e_invarg2), flags);
17411 dir = 0;
17412 }
17413 else
17414 *flagsp |= mask;
17415 }
17416 if (dir == 0)
17417 break;
17418 ++flags;
17419 }
17420 }
17421 return dir;
17422}
17423
Bram Moolenaar071d4272004-06-13 20:20:40 +000017424/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017425 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017427 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017428search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017430 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 char_u *pat;
17432 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017433 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 int save_p_ws = p_ws;
17435 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017436 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017437 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017438 proftime_T tm;
17439#ifdef FEAT_RELTIME
17440 long time_limit = 0;
17441#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017442 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017443 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017445 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017446 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017447 if (dir == 0)
17448 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017449 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017450 if (flags & SP_START)
17451 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017452 if (flags & SP_END)
17453 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017454 if (flags & SP_COLUMN)
17455 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017456
Bram Moolenaar76929292008-01-06 19:07:36 +000017457 /* Optional arguments: line number to stop searching and timeout. */
17458 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017459 {
17460 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17461 if (lnum_stop < 0)
17462 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017463#ifdef FEAT_RELTIME
17464 if (argvars[3].v_type != VAR_UNKNOWN)
17465 {
17466 time_limit = get_tv_number_chk(&argvars[3], NULL);
17467 if (time_limit < 0)
17468 goto theend;
17469 }
17470#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017471 }
17472
Bram Moolenaar76929292008-01-06 19:07:36 +000017473#ifdef FEAT_RELTIME
17474 /* Set the time limit, if there is one. */
17475 profile_setlimit(time_limit, &tm);
17476#endif
17477
Bram Moolenaar231334e2005-07-25 20:46:57 +000017478 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017479 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017480 * Check to make sure only those flags are set.
17481 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17482 * flags cannot be set. Check for that condition also.
17483 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017484 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017485 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017487 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017488 goto theend;
17489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017491 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017492 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017493 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017494 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017496 if (flags & SP_SUBPAT)
17497 retval = subpatnum;
17498 else
17499 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017500 if (flags & SP_SETPCMARK)
17501 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017503 if (match_pos != NULL)
17504 {
17505 /* Store the match cursor position */
17506 match_pos->lnum = pos.lnum;
17507 match_pos->col = pos.col + 1;
17508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 /* "/$" will put the cursor after the end of the line, may need to
17510 * correct that here */
17511 check_cursor();
17512 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017513
17514 /* If 'n' flag is used: restore cursor position. */
17515 if (flags & SP_NOMOVE)
17516 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017517 else
17518 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017519theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017521
17522 return retval;
17523}
17524
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017525#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017526
17527/*
17528 * round() is not in C90, use ceil() or floor() instead.
17529 */
17530 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017531vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017532{
17533 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17534}
17535
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017536/*
17537 * "round({float})" function
17538 */
17539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017540f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017541{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017542 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017543
17544 rettv->v_type = VAR_FLOAT;
17545 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017546 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017547 else
17548 rettv->vval.v_float = 0.0;
17549}
17550#endif
17551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017552/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017553 * "screenattr()" function
17554 */
17555 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017556f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017557{
17558 int row;
17559 int col;
17560 int c;
17561
17562 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17563 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17564 if (row < 0 || row >= screen_Rows
17565 || col < 0 || col >= screen_Columns)
17566 c = -1;
17567 else
17568 c = ScreenAttrs[LineOffset[row] + col];
17569 rettv->vval.v_number = c;
17570}
17571
17572/*
17573 * "screenchar()" function
17574 */
17575 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017576f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017577{
17578 int row;
17579 int col;
17580 int off;
17581 int c;
17582
17583 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17584 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17585 if (row < 0 || row >= screen_Rows
17586 || col < 0 || col >= screen_Columns)
17587 c = -1;
17588 else
17589 {
17590 off = LineOffset[row] + col;
17591#ifdef FEAT_MBYTE
17592 if (enc_utf8 && ScreenLinesUC[off] != 0)
17593 c = ScreenLinesUC[off];
17594 else
17595#endif
17596 c = ScreenLines[off];
17597 }
17598 rettv->vval.v_number = c;
17599}
17600
17601/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017602 * "screencol()" function
17603 *
17604 * First column is 1 to be consistent with virtcol().
17605 */
17606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017607f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017608{
17609 rettv->vval.v_number = screen_screencol() + 1;
17610}
17611
17612/*
17613 * "screenrow()" function
17614 */
17615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017616f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017617{
17618 rettv->vval.v_number = screen_screenrow() + 1;
17619}
17620
17621/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017622 * "search()" function
17623 */
17624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017625f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017626{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017627 int flags = 0;
17628
17629 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630}
17631
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017633 * "searchdecl()" function
17634 */
17635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017636f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017637{
17638 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017639 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017640 int error = FALSE;
17641 char_u *name;
17642
17643 rettv->vval.v_number = 1; /* default: FAIL */
17644
17645 name = get_tv_string_chk(&argvars[0]);
17646 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017647 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017648 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017649 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17650 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17651 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017652 if (!error && name != NULL)
17653 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017654 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017655}
17656
17657/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017658 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017661searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662{
17663 char_u *spat, *mpat, *epat;
17664 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666 int dir;
17667 int flags = 0;
17668 char_u nbuf1[NUMBUFLEN];
17669 char_u nbuf2[NUMBUFLEN];
17670 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017671 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017672 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017673 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017676 spat = get_tv_string_chk(&argvars[0]);
17677 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17678 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17679 if (spat == NULL || mpat == NULL || epat == NULL)
17680 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 /* Handle the optional fourth argument: flags */
17683 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017684 if (dir == 0)
17685 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017686
17687 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017688 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17689 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017690 if ((flags & (SP_END | SP_SUBPAT)) != 0
17691 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017692 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017693 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017694 goto theend;
17695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017696
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017697 /* Using 'r' implies 'W', otherwise it doesn't work. */
17698 if (flags & SP_REPEAT)
17699 p_ws = FALSE;
17700
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017701 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017702 if (argvars[3].v_type == VAR_UNKNOWN
17703 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704 skip = (char_u *)"";
17705 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017707 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017708 if (argvars[5].v_type != VAR_UNKNOWN)
17709 {
17710 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17711 if (lnum_stop < 0)
17712 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017713#ifdef FEAT_RELTIME
17714 if (argvars[6].v_type != VAR_UNKNOWN)
17715 {
17716 time_limit = get_tv_number_chk(&argvars[6], NULL);
17717 if (time_limit < 0)
17718 goto theend;
17719 }
17720#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017721 }
17722 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017723 if (skip == NULL)
17724 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017726 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017727 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017728
17729theend:
17730 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017731
17732 return retval;
17733}
17734
17735/*
17736 * "searchpair()" function
17737 */
17738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017739f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017740{
17741 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17742}
17743
17744/*
17745 * "searchpairpos()" function
17746 */
17747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017748f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017749{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017750 pos_T match_pos;
17751 int lnum = 0;
17752 int col = 0;
17753
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017754 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017755 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017756
17757 if (searchpair_cmn(argvars, &match_pos) > 0)
17758 {
17759 lnum = match_pos.lnum;
17760 col = match_pos.col;
17761 }
17762
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017763 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17764 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017765}
17766
17767/*
17768 * Search for a start/middle/end thing.
17769 * Used by searchpair(), see its documentation for the details.
17770 * Returns 0 or -1 for no match,
17771 */
17772 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017773do_searchpair(
17774 char_u *spat, /* start pattern */
17775 char_u *mpat, /* middle pattern */
17776 char_u *epat, /* end pattern */
17777 int dir, /* BACKWARD or FORWARD */
17778 char_u *skip, /* skip expression */
17779 int flags, /* SP_SETPCMARK and other SP_ values */
17780 pos_T *match_pos,
17781 linenr_T lnum_stop, /* stop at this line if not zero */
17782 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017783{
17784 char_u *save_cpo;
17785 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17786 long retval = 0;
17787 pos_T pos;
17788 pos_T firstpos;
17789 pos_T foundpos;
17790 pos_T save_cursor;
17791 pos_T save_pos;
17792 int n;
17793 int r;
17794 int nest = 1;
17795 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017796 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017797 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017798
17799 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17800 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017801 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017802
Bram Moolenaar76929292008-01-06 19:07:36 +000017803#ifdef FEAT_RELTIME
17804 /* Set the time limit, if there is one. */
17805 profile_setlimit(time_limit, &tm);
17806#endif
17807
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017808 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17809 * start/middle/end (pat3, for the top pair). */
17810 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17811 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17812 if (pat2 == NULL || pat3 == NULL)
17813 goto theend;
17814 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17815 if (*mpat == NUL)
17816 STRCPY(pat3, pat2);
17817 else
17818 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17819 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017820 if (flags & SP_START)
17821 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017822
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 save_cursor = curwin->w_cursor;
17824 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017825 clearpos(&firstpos);
17826 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 pat = pat3;
17828 for (;;)
17829 {
17830 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017831 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17833 /* didn't find it or found the first match again: FAIL */
17834 break;
17835
17836 if (firstpos.lnum == 0)
17837 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017838 if (equalpos(pos, foundpos))
17839 {
17840 /* Found the same position again. Can happen with a pattern that
17841 * has "\zs" at the end and searching backwards. Advance one
17842 * character and try again. */
17843 if (dir == BACKWARD)
17844 decl(&pos);
17845 else
17846 incl(&pos);
17847 }
17848 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017850 /* clear the start flag to avoid getting stuck here */
17851 options &= ~SEARCH_START;
17852
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853 /* If the skip pattern matches, ignore this match. */
17854 if (*skip != NUL)
17855 {
17856 save_pos = curwin->w_cursor;
17857 curwin->w_cursor = pos;
17858 r = eval_to_bool(skip, &err, NULL, FALSE);
17859 curwin->w_cursor = save_pos;
17860 if (err)
17861 {
17862 /* Evaluating {skip} caused an error, break here. */
17863 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017864 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017865 break;
17866 }
17867 if (r)
17868 continue;
17869 }
17870
17871 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17872 {
17873 /* Found end when searching backwards or start when searching
17874 * forward: nested pair. */
17875 ++nest;
17876 pat = pat2; /* nested, don't search for middle */
17877 }
17878 else
17879 {
17880 /* Found end when searching forward or start when searching
17881 * backward: end of (nested) pair; or found middle in outer pair. */
17882 if (--nest == 1)
17883 pat = pat3; /* outer level, search for middle */
17884 }
17885
17886 if (nest == 0)
17887 {
17888 /* Found the match: return matchcount or line number. */
17889 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017890 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017892 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017893 if (flags & SP_SETPCMARK)
17894 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895 curwin->w_cursor = pos;
17896 if (!(flags & SP_REPEAT))
17897 break;
17898 nest = 1; /* search for next unmatched */
17899 }
17900 }
17901
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017902 if (match_pos != NULL)
17903 {
17904 /* Store the match cursor position */
17905 match_pos->lnum = curwin->w_cursor.lnum;
17906 match_pos->col = curwin->w_cursor.col + 1;
17907 }
17908
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017910 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 curwin->w_cursor = save_cursor;
17912
17913theend:
17914 vim_free(pat2);
17915 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017916 if (p_cpo == empty_option)
17917 p_cpo = save_cpo;
17918 else
17919 /* Darn, evaluating the {skip} expression changed the value. */
17920 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017921
17922 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923}
17924
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017925/*
17926 * "searchpos()" function
17927 */
17928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017929f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017930{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017931 pos_T match_pos;
17932 int lnum = 0;
17933 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017934 int n;
17935 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017936
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017937 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017938 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017939
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017940 n = search_cmn(argvars, &match_pos, &flags);
17941 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017942 {
17943 lnum = match_pos.lnum;
17944 col = match_pos.col;
17945 }
17946
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017947 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17948 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017949 if (flags & SP_SUBPAT)
17950 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017951}
17952
Bram Moolenaar0d660222005-01-07 21:51:51 +000017953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017954f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017956#ifdef FEAT_CLIENTSERVER
17957 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017958 char_u *server = get_tv_string_chk(&argvars[0]);
17959 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960
Bram Moolenaar0d660222005-01-07 21:51:51 +000017961 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017962 if (server == NULL || reply == NULL)
17963 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017964 if (check_restricted() || check_secure())
17965 return;
17966# ifdef FEAT_X11
17967 if (check_connection() == FAIL)
17968 return;
17969# endif
17970
17971 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017973 EMSG(_("E258: Unable to send to client"));
17974 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017976 rettv->vval.v_number = 0;
17977#else
17978 rettv->vval.v_number = -1;
17979#endif
17980}
17981
Bram Moolenaar0d660222005-01-07 21:51:51 +000017982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017983f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017984{
17985 char_u *r = NULL;
17986
17987#ifdef FEAT_CLIENTSERVER
17988# ifdef WIN32
17989 r = serverGetVimNames();
17990# else
17991 make_connection();
17992 if (X_DISPLAY != NULL)
17993 r = serverGetVimNames(X_DISPLAY);
17994# endif
17995#endif
17996 rettv->v_type = VAR_STRING;
17997 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998}
17999
18000/*
18001 * "setbufvar()" function
18002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018004f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018005{
18006 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018009 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010 char_u nbuf[NUMBUFLEN];
18011
18012 if (check_restricted() || check_secure())
18013 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018014 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18015 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018016 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 varp = &argvars[2];
18018
18019 if (buf != NULL && varname != NULL && varp != NULL)
18020 {
18021 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023
18024 if (*varname == '&')
18025 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018026 long numval;
18027 char_u *strval;
18028 int error = FALSE;
18029
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018031 numval = get_tv_number_chk(varp, &error);
18032 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018033 if (!error && strval != NULL)
18034 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 }
18036 else
18037 {
18038 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18039 if (bufvarname != NULL)
18040 {
18041 STRCPY(bufvarname, "b:");
18042 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018043 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 vim_free(bufvarname);
18045 }
18046 }
18047
18048 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051}
18052
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018054f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018055{
18056 dict_T *d;
18057 dictitem_T *di;
18058 char_u *csearch;
18059
18060 if (argvars[0].v_type != VAR_DICT)
18061 {
18062 EMSG(_(e_dictreq));
18063 return;
18064 }
18065
18066 if ((d = argvars[0].vval.v_dict) != NULL)
18067 {
18068 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18069 if (csearch != NULL)
18070 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018071#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018072 if (enc_utf8)
18073 {
18074 int pcc[MAX_MCO];
18075 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018076
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018077 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18078 }
18079 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018080#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018081 set_last_csearch(PTR2CHAR(csearch),
18082 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018083 }
18084
18085 di = dict_find(d, (char_u *)"forward", -1);
18086 if (di != NULL)
18087 set_csearch_direction(get_tv_number(&di->di_tv)
18088 ? FORWARD : BACKWARD);
18089
18090 di = dict_find(d, (char_u *)"until", -1);
18091 if (di != NULL)
18092 set_csearch_until(!!get_tv_number(&di->di_tv));
18093 }
18094}
18095
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096/*
18097 * "setcmdpos()" function
18098 */
18099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018100f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018102 int pos = (int)get_tv_number(&argvars[0]) - 1;
18103
18104 if (pos >= 0)
18105 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106}
18107
18108/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018109 * "setfperm({fname}, {mode})" function
18110 */
18111 static void
18112f_setfperm(typval_T *argvars, typval_T *rettv)
18113{
18114 char_u *fname;
18115 char_u modebuf[NUMBUFLEN];
18116 char_u *mode_str;
18117 int i;
18118 int mask;
18119 int mode = 0;
18120
18121 rettv->vval.v_number = 0;
18122 fname = get_tv_string_chk(&argvars[0]);
18123 if (fname == NULL)
18124 return;
18125 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18126 if (mode_str == NULL)
18127 return;
18128 if (STRLEN(mode_str) != 9)
18129 {
18130 EMSG2(_(e_invarg2), mode_str);
18131 return;
18132 }
18133
18134 mask = 1;
18135 for (i = 8; i >= 0; --i)
18136 {
18137 if (mode_str[i] != '-')
18138 mode |= mask;
18139 mask = mask << 1;
18140 }
18141 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18142}
18143
18144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 * "setline()" function
18146 */
18147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018148f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149{
18150 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018151 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018152 list_T *l = NULL;
18153 listitem_T *li = NULL;
18154 long added = 0;
18155 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018157 lnum = get_tv_lnum(&argvars[0]);
18158 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018160 l = argvars[1].vval.v_list;
18161 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018163 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018164 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018165
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018166 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018167 for (;;)
18168 {
18169 if (l != NULL)
18170 {
18171 /* list argument, get next string */
18172 if (li == NULL)
18173 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018174 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018175 li = li->li_next;
18176 }
18177
18178 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018179 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018180 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018181
18182 /* When coming here from Insert mode, sync undo, so that this can be
18183 * undone separately from what was previously inserted. */
18184 if (u_sync_once == 2)
18185 {
18186 u_sync_once = 1; /* notify that u_sync() was called */
18187 u_sync(TRUE);
18188 }
18189
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018190 if (lnum <= curbuf->b_ml.ml_line_count)
18191 {
18192 /* existing line, replace it */
18193 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18194 {
18195 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018196 if (lnum == curwin->w_cursor.lnum)
18197 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018198 rettv->vval.v_number = 0; /* OK */
18199 }
18200 }
18201 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18202 {
18203 /* lnum is one past the last line, append the line */
18204 ++added;
18205 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18206 rettv->vval.v_number = 0; /* OK */
18207 }
18208
18209 if (l == NULL) /* only one string argument */
18210 break;
18211 ++lnum;
18212 }
18213
18214 if (added > 0)
18215 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216}
18217
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018218static 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 +000018219
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018221 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018222 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018224set_qf_ll_list(
18225 win_T *wp UNUSED,
18226 typval_T *list_arg UNUSED,
18227 typval_T *action_arg UNUSED,
18228 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018229{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018230#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018231 char_u *act;
18232 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018233#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018234
Bram Moolenaar2641f772005-03-25 21:58:17 +000018235 rettv->vval.v_number = -1;
18236
18237#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018238 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018239 EMSG(_(e_listreq));
18240 else
18241 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018242 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018243
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018244 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018245 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018246 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018247 if (act == NULL)
18248 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018249 if (*act == 'a' || *act == 'r')
18250 action = *act;
18251 }
18252
Bram Moolenaar81484f42012-12-05 15:16:47 +010018253 if (l != NULL && set_errorlist(wp, l, action,
18254 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018255 rettv->vval.v_number = 0;
18256 }
18257#endif
18258}
18259
18260/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018261 * "setloclist()" function
18262 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018264f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018265{
18266 win_T *win;
18267
18268 rettv->vval.v_number = -1;
18269
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018270 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018271 if (win != NULL)
18272 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18273}
18274
18275/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018276 * "setmatches()" function
18277 */
18278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018279f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018280{
18281#ifdef FEAT_SEARCH_EXTRA
18282 list_T *l;
18283 listitem_T *li;
18284 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018285 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018286
18287 rettv->vval.v_number = -1;
18288 if (argvars[0].v_type != VAR_LIST)
18289 {
18290 EMSG(_(e_listreq));
18291 return;
18292 }
18293 if ((l = argvars[0].vval.v_list) != NULL)
18294 {
18295
18296 /* To some extent make sure that we are dealing with a list from
18297 * "getmatches()". */
18298 li = l->lv_first;
18299 while (li != NULL)
18300 {
18301 if (li->li_tv.v_type != VAR_DICT
18302 || (d = li->li_tv.vval.v_dict) == NULL)
18303 {
18304 EMSG(_(e_invarg));
18305 return;
18306 }
18307 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018308 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18309 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018310 && dict_find(d, (char_u *)"priority", -1) != NULL
18311 && dict_find(d, (char_u *)"id", -1) != NULL))
18312 {
18313 EMSG(_(e_invarg));
18314 return;
18315 }
18316 li = li->li_next;
18317 }
18318
18319 clear_matches(curwin);
18320 li = l->lv_first;
18321 while (li != NULL)
18322 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018323 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018324 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018325 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018326 char_u *group;
18327 int priority;
18328 int id;
18329 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018330
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018331 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018332 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18333 {
18334 if (s == NULL)
18335 {
18336 s = list_alloc();
18337 if (s == NULL)
18338 return;
18339 }
18340
18341 /* match from matchaddpos() */
18342 for (i = 1; i < 9; i++)
18343 {
18344 sprintf((char *)buf, (char *)"pos%d", i);
18345 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18346 {
18347 if (di->di_tv.v_type != VAR_LIST)
18348 return;
18349
18350 list_append_tv(s, &di->di_tv);
18351 s->lv_refcount++;
18352 }
18353 else
18354 break;
18355 }
18356 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018357
18358 group = get_dict_string(d, (char_u *)"group", FALSE);
18359 priority = (int)get_dict_number(d, (char_u *)"priority");
18360 id = (int)get_dict_number(d, (char_u *)"id");
18361 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18362 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18363 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018364 if (i == 0)
18365 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018366 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018367 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018368 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018369 }
18370 else
18371 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018372 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018373 list_unref(s);
18374 s = NULL;
18375 }
18376
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018377 li = li->li_next;
18378 }
18379 rettv->vval.v_number = 0;
18380 }
18381#endif
18382}
18383
18384/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018385 * "setpos()" function
18386 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018388f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018389{
18390 pos_T pos;
18391 int fnum;
18392 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018393 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018394
Bram Moolenaar08250432008-02-13 11:42:46 +000018395 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018396 name = get_tv_string_chk(argvars);
18397 if (name != NULL)
18398 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018399 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018400 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018401 if (--pos.col < 0)
18402 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018403 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018404 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018405 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018406 if (fnum == curbuf->b_fnum)
18407 {
18408 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018409 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018410 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018411 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018412 curwin->w_set_curswant = FALSE;
18413 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018414 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018415 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018416 }
18417 else
18418 EMSG(_(e_invarg));
18419 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018420 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18421 {
18422 /* set mark */
18423 if (setmark_pos(name[1], &pos, fnum) == OK)
18424 rettv->vval.v_number = 0;
18425 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018426 else
18427 EMSG(_(e_invarg));
18428 }
18429 }
18430}
18431
18432/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018433 * "setqflist()" function
18434 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018436f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018437{
18438 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18439}
18440
18441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 * "setreg()" function
18443 */
18444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018445f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018446{
18447 int regname;
18448 char_u *strregname;
18449 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018450 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018451 int append;
18452 char_u yank_type;
18453 long block_len;
18454
18455 block_len = -1;
18456 yank_type = MAUTO;
18457 append = FALSE;
18458
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018459 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018460 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018462 if (strregname == NULL)
18463 return; /* type error; errmsg already given */
18464 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465 if (regname == 0 || regname == '@')
18466 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018468 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018470 stropt = get_tv_string_chk(&argvars[2]);
18471 if (stropt == NULL)
18472 return; /* type error */
18473 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018474 switch (*stropt)
18475 {
18476 case 'a': case 'A': /* append */
18477 append = TRUE;
18478 break;
18479 case 'v': case 'c': /* character-wise selection */
18480 yank_type = MCHAR;
18481 break;
18482 case 'V': case 'l': /* line-wise selection */
18483 yank_type = MLINE;
18484 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 case 'b': case Ctrl_V: /* block-wise selection */
18486 yank_type = MBLOCK;
18487 if (VIM_ISDIGIT(stropt[1]))
18488 {
18489 ++stropt;
18490 block_len = getdigits(&stropt) - 1;
18491 --stropt;
18492 }
18493 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 }
18495 }
18496
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018497 if (argvars[1].v_type == VAR_LIST)
18498 {
18499 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018500 char_u **allocval;
18501 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018502 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018503 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018504 int len = argvars[1].vval.v_list->lv_len;
18505 listitem_T *li;
18506
Bram Moolenaar7d647822014-04-05 21:28:56 +020018507 /* First half: use for pointers to result lines; second half: use for
18508 * pointers to allocated copies. */
18509 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018510 if (lstval == NULL)
18511 return;
18512 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018513 allocval = lstval + len + 2;
18514 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018515
18516 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18517 li = li->li_next)
18518 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018519 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018520 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018521 goto free_lstval;
18522 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018523 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018524 /* Need to make a copy, next get_tv_string_buf_chk() will
18525 * overwrite the string. */
18526 strval = vim_strsave(buf);
18527 if (strval == NULL)
18528 goto free_lstval;
18529 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018530 }
18531 *curval++ = strval;
18532 }
18533 *curval++ = NULL;
18534
18535 write_reg_contents_lst(regname, lstval, -1,
18536 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018537free_lstval:
18538 while (curallocval > allocval)
18539 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018540 vim_free(lstval);
18541 }
18542 else
18543 {
18544 strval = get_tv_string_chk(&argvars[1]);
18545 if (strval == NULL)
18546 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018547 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018549 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018550 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551}
18552
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018553/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018554 * "settabvar()" function
18555 */
18556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018557f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018558{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018559#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018560 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018561 tabpage_T *tp;
18562#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018563 char_u *varname, *tabvarname;
18564 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018565
18566 rettv->vval.v_number = 0;
18567
18568 if (check_restricted() || check_secure())
18569 return;
18570
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018571#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018572 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018573#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018574 varname = get_tv_string_chk(&argvars[1]);
18575 varp = &argvars[2];
18576
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018577 if (varname != NULL && varp != NULL
18578#ifdef FEAT_WINDOWS
18579 && tp != NULL
18580#endif
18581 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018582 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018583#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018584 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018585 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018586#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018587
18588 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18589 if (tabvarname != NULL)
18590 {
18591 STRCPY(tabvarname, "t:");
18592 STRCPY(tabvarname + 2, varname);
18593 set_var(tabvarname, varp, TRUE);
18594 vim_free(tabvarname);
18595 }
18596
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018597#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018598 /* Restore current tabpage */
18599 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018600 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018601#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018602 }
18603}
18604
18605/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018606 * "settabwinvar()" function
18607 */
18608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018609f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018610{
18611 setwinvar(argvars, rettv, 1);
18612}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613
18614/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018615 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018618f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018620 setwinvar(argvars, rettv, 0);
18621}
18622
18623/*
18624 * "setwinvar()" and "settabwinvar()" functions
18625 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018626
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018628setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018629{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 win_T *win;
18631#ifdef FEAT_WINDOWS
18632 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018633 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018634 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635#endif
18636 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018637 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018639 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640
18641 if (check_restricted() || check_secure())
18642 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018643
18644#ifdef FEAT_WINDOWS
18645 if (off == 1)
18646 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18647 else
18648 tp = curtab;
18649#endif
18650 win = find_win_by_nr(&argvars[off], tp);
18651 varname = get_tv_string_chk(&argvars[off + 1]);
18652 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653
18654 if (win != NULL && varname != NULL && varp != NULL)
18655 {
18656#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018657 need_switch_win = !(tp == curtab && win == curwin);
18658 if (!need_switch_win
18659 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018662 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018664 long numval;
18665 char_u *strval;
18666 int error = FALSE;
18667
18668 ++varname;
18669 numval = get_tv_number_chk(varp, &error);
18670 strval = get_tv_string_buf_chk(varp, nbuf);
18671 if (!error && strval != NULL)
18672 set_option_value(varname, numval, strval, OPT_LOCAL);
18673 }
18674 else
18675 {
18676 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18677 if (winvarname != NULL)
18678 {
18679 STRCPY(winvarname, "w:");
18680 STRCPY(winvarname + 2, varname);
18681 set_var(winvarname, varp, TRUE);
18682 vim_free(winvarname);
18683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 }
18685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018687 if (need_switch_win)
18688 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689#endif
18690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018691}
18692
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018693#ifdef FEAT_CRYPT
18694/*
18695 * "sha256({string})" function
18696 */
18697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018698f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018699{
18700 char_u *p;
18701
18702 p = get_tv_string(&argvars[0]);
18703 rettv->vval.v_string = vim_strsave(
18704 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18705 rettv->v_type = VAR_STRING;
18706}
18707#endif /* FEAT_CRYPT */
18708
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018710 * "shellescape({string})" function
18711 */
18712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018713f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018714{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018715 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018716 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018717 rettv->v_type = VAR_STRING;
18718}
18719
18720/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018721 * shiftwidth() function
18722 */
18723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018724f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018725{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018726 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018727}
18728
18729/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018730 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 */
18732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018733f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018735 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736
Bram Moolenaar0d660222005-01-07 21:51:51 +000018737 p = get_tv_string(&argvars[0]);
18738 rettv->vval.v_string = vim_strsave(p);
18739 simplify_filename(rettv->vval.v_string); /* simplify in place */
18740 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741}
18742
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018743#ifdef FEAT_FLOAT
18744/*
18745 * "sin()" function
18746 */
18747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018748f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018749{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018750 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018751
18752 rettv->v_type = VAR_FLOAT;
18753 if (get_float_arg(argvars, &f) == OK)
18754 rettv->vval.v_float = sin(f);
18755 else
18756 rettv->vval.v_float = 0.0;
18757}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018758
18759/*
18760 * "sinh()" function
18761 */
18762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018763f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018764{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018765 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018766
18767 rettv->v_type = VAR_FLOAT;
18768 if (get_float_arg(argvars, &f) == OK)
18769 rettv->vval.v_float = sinh(f);
18770 else
18771 rettv->vval.v_float = 0.0;
18772}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018773#endif
18774
Bram Moolenaar0d660222005-01-07 21:51:51 +000018775static int
18776#ifdef __BORLANDC__
18777 _RTLENTRYF
18778#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018779 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018780static int
18781#ifdef __BORLANDC__
18782 _RTLENTRYF
18783#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018784 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018785
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018786/* struct used in the array that's given to qsort() */
18787typedef struct
18788{
18789 listitem_T *item;
18790 int idx;
18791} sortItem_T;
18792
Bram Moolenaar0b962472016-02-22 22:51:33 +010018793/* struct storing information about current sort */
18794typedef struct
18795{
18796 int item_compare_ic;
18797 int item_compare_numeric;
18798 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018799#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018800 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018801#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018802 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018803 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018804 dict_T *item_compare_selfdict;
18805 int item_compare_func_err;
18806 int item_compare_keep_zero;
18807} sortinfo_T;
18808static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018809static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018810#define ITEM_COMPARE_FAIL 999
18811
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018813 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018815 static int
18816#ifdef __BORLANDC__
18817_RTLENTRYF
18818#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018819item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018821 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018822 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018823 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018824 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018825 int res;
18826 char_u numbuf1[NUMBUFLEN];
18827 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018829 si1 = (sortItem_T *)s1;
18830 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018831 tv1 = &si1->item->li_tv;
18832 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018833
Bram Moolenaar0b962472016-02-22 22:51:33 +010018834 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018835 {
18836 long v1 = get_tv_number(tv1);
18837 long v2 = get_tv_number(tv2);
18838
18839 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18840 }
18841
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018842#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018843 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018844 {
18845 float_T v1 = get_tv_float(tv1);
18846 float_T v2 = get_tv_float(tv2);
18847
18848 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18849 }
18850#endif
18851
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018852 /* tv2string() puts quotes around a string and allocates memory. Don't do
18853 * that for string variables. Use a single quote when comparing with a
18854 * non-string to do what the docs promise. */
18855 if (tv1->v_type == VAR_STRING)
18856 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018857 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018858 p1 = (char_u *)"'";
18859 else
18860 p1 = tv1->vval.v_string;
18861 }
18862 else
18863 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18864 if (tv2->v_type == VAR_STRING)
18865 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018866 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018867 p2 = (char_u *)"'";
18868 else
18869 p2 = tv2->vval.v_string;
18870 }
18871 else
18872 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018873 if (p1 == NULL)
18874 p1 = (char_u *)"";
18875 if (p2 == NULL)
18876 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018877 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018878 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018879 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018880 res = STRICMP(p1, p2);
18881 else
18882 res = STRCMP(p1, p2);
18883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018884 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018885 {
18886 double n1, n2;
18887 n1 = strtod((char *)p1, (char **)&p1);
18888 n2 = strtod((char *)p2, (char **)&p2);
18889 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18890 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018891
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018892 /* When the result would be zero, compare the item indexes. Makes the
18893 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018894 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018895 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018896
Bram Moolenaar0d660222005-01-07 21:51:51 +000018897 vim_free(tofree1);
18898 vim_free(tofree2);
18899 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018900}
18901
18902 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018903#ifdef __BORLANDC__
18904_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018906item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018907{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018908 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018909 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018910 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018911 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018912 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018913 char_u *func_name;
18914 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018916 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018917 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018918 return 0;
18919
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018920 si1 = (sortItem_T *)s1;
18921 si2 = (sortItem_T *)s2;
18922
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018923 if (partial == NULL)
18924 func_name = sortinfo->item_compare_func;
18925 else
18926 func_name = partial->pt_name;
18927
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018928 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018929 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018930 copy_tv(&si1->item->li_tv, &argv[0]);
18931 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018932
18933 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018934 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018935 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018936 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018937 clear_tv(&argv[0]);
18938 clear_tv(&argv[1]);
18939
18940 if (res == FAIL)
18941 res = ITEM_COMPARE_FAIL;
18942 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018943 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18944 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018945 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018946 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018947
18948 /* When the result would be zero, compare the pointers themselves. Makes
18949 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018950 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018951 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018952
Bram Moolenaar0d660222005-01-07 21:51:51 +000018953 return res;
18954}
18955
18956/*
18957 * "sort({list})" function
18958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018960do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018961{
Bram Moolenaar33570922005-01-25 22:26:29 +000018962 list_T *l;
18963 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018964 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018965 sortinfo_T *old_sortinfo;
18966 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018967 long len;
18968 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969
Bram Moolenaar0b962472016-02-22 22:51:33 +010018970 /* Pointer to current info struct used in compare function. Save and
18971 * restore the current one for nested calls. */
18972 old_sortinfo = sortinfo;
18973 sortinfo = &info;
18974
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018976 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 else
18978 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018980 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018981 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18982 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018983 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018984 rettv->vval.v_list = l;
18985 rettv->v_type = VAR_LIST;
18986 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988 len = list_len(l);
18989 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018990 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991
Bram Moolenaar0b962472016-02-22 22:51:33 +010018992 info.item_compare_ic = FALSE;
18993 info.item_compare_numeric = FALSE;
18994 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018995#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018996 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018997#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018998 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018999 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019000 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019001 if (argvars[1].v_type != VAR_UNKNOWN)
19002 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019003 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019005 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019006 else if (argvars[1].v_type == VAR_PARTIAL)
19007 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019008 else
19009 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019010 int error = FALSE;
19011
19012 i = get_tv_number_chk(&argvars[1], &error);
19013 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019014 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019015 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019016 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019017 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019018 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019019 else if (i != 0)
19020 {
19021 EMSG(_(e_invarg));
19022 goto theend;
19023 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019024 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019025 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019026 if (*info.item_compare_func == NUL)
19027 {
19028 /* empty string means default sort */
19029 info.item_compare_func = NULL;
19030 }
19031 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019032 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019033 info.item_compare_func = NULL;
19034 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019035 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019036 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019037 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019038 info.item_compare_func = NULL;
19039 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019040 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019041#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019042 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019043 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019044 info.item_compare_func = NULL;
19045 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019046 }
19047#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019048 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019049 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019050 info.item_compare_func = NULL;
19051 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019052 }
19053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019054 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019055
19056 if (argvars[2].v_type != VAR_UNKNOWN)
19057 {
19058 /* optional third argument: {dict} */
19059 if (argvars[2].v_type != VAR_DICT)
19060 {
19061 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019062 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019063 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019064 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019065 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067
Bram Moolenaar0d660222005-01-07 21:51:51 +000019068 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019069 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019070 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019071 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072
Bram Moolenaar327aa022014-03-25 18:24:23 +010019073 i = 0;
19074 if (sort)
19075 {
19076 /* sort(): ptrs will be the list to sort */
19077 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019078 {
19079 ptrs[i].item = li;
19080 ptrs[i].idx = i;
19081 ++i;
19082 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019083
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 info.item_compare_func_err = FALSE;
19085 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019086 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019087 if ((info.item_compare_func != NULL
19088 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019089 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019090 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019091 EMSG(_("E702: Sort compare function failed"));
19092 else
19093 {
19094 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019095 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019096 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019097 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019098 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019099
Bram Moolenaar0b962472016-02-22 22:51:33 +010019100 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019101 {
19102 /* Clear the List and append the items in sorted order. */
19103 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19104 l->lv_len = 0;
19105 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019106 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019107 }
19108 }
19109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019111 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019112 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019113
19114 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019115 info.item_compare_func_err = FALSE;
19116 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019117 item_compare_func_ptr = info.item_compare_func != NULL
19118 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019119 ? item_compare2 : item_compare;
19120
19121 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19122 li = li->li_next)
19123 {
19124 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19125 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019126 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019127 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019128 {
19129 EMSG(_("E882: Uniq compare function failed"));
19130 break;
19131 }
19132 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019133
Bram Moolenaar0b962472016-02-22 22:51:33 +010019134 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019135 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019136 while (--i >= 0)
19137 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019138 li = ptrs[i].item->li_next;
19139 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019140 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019141 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019142 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019143 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019144 list_fix_watch(l, li);
19145 listitem_free(li);
19146 l->lv_len--;
19147 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019148 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019149 }
19150
19151 vim_free(ptrs);
19152 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019153theend:
19154 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019155}
19156
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019157/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019158 * "sort({list})" function
19159 */
19160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019161f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019162{
19163 do_sort_uniq(argvars, rettv, TRUE);
19164}
19165
19166/*
19167 * "uniq({list})" function
19168 */
19169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019170f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019171{
19172 do_sort_uniq(argvars, rettv, FALSE);
19173}
19174
19175/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019176 * "soundfold({word})" function
19177 */
19178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019179f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019180{
19181 char_u *s;
19182
19183 rettv->v_type = VAR_STRING;
19184 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019185#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019186 rettv->vval.v_string = eval_soundfold(s);
19187#else
19188 rettv->vval.v_string = vim_strsave(s);
19189#endif
19190}
19191
19192/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019193 * "spellbadword()" function
19194 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019196f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019197{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019198 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019199 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019200 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019201
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019202 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019203 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019204
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019205#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019206 if (argvars[0].v_type == VAR_UNKNOWN)
19207 {
19208 /* Find the start and length of the badly spelled word. */
19209 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19210 if (len != 0)
19211 word = ml_get_cursor();
19212 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019213 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019214 {
19215 char_u *str = get_tv_string_chk(&argvars[0]);
19216 int capcol = -1;
19217
19218 if (str != NULL)
19219 {
19220 /* Check the argument for spelling. */
19221 while (*str != NUL)
19222 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019223 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019224 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019225 {
19226 word = str;
19227 break;
19228 }
19229 str += len;
19230 }
19231 }
19232 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019233#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019234
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019235 list_append_string(rettv->vval.v_list, word, len);
19236 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019237 attr == HLF_SPB ? "bad" :
19238 attr == HLF_SPR ? "rare" :
19239 attr == HLF_SPL ? "local" :
19240 attr == HLF_SPC ? "caps" :
19241 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019242}
19243
19244/*
19245 * "spellsuggest()" function
19246 */
19247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019248f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019249{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019250#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019251 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019252 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019253 int maxcount;
19254 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019255 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019256 listitem_T *li;
19257 int need_capital = FALSE;
19258#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019259
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019260 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019261 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019262
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019263#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019264 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019265 {
19266 str = get_tv_string(&argvars[0]);
19267 if (argvars[1].v_type != VAR_UNKNOWN)
19268 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019269 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019270 if (maxcount <= 0)
19271 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019272 if (argvars[2].v_type != VAR_UNKNOWN)
19273 {
19274 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19275 if (typeerr)
19276 return;
19277 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019278 }
19279 else
19280 maxcount = 25;
19281
Bram Moolenaar4770d092006-01-12 23:22:24 +000019282 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019283
19284 for (i = 0; i < ga.ga_len; ++i)
19285 {
19286 str = ((char_u **)ga.ga_data)[i];
19287
19288 li = listitem_alloc();
19289 if (li == NULL)
19290 vim_free(str);
19291 else
19292 {
19293 li->li_tv.v_type = VAR_STRING;
19294 li->li_tv.v_lock = 0;
19295 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019296 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019297 }
19298 }
19299 ga_clear(&ga);
19300 }
19301#endif
19302}
19303
Bram Moolenaar0d660222005-01-07 21:51:51 +000019304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019305f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019306{
19307 char_u *str;
19308 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019309 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019310 regmatch_T regmatch;
19311 char_u patbuf[NUMBUFLEN];
19312 char_u *save_cpo;
19313 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019314 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019315 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019316 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019317
19318 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19319 save_cpo = p_cpo;
19320 p_cpo = (char_u *)"";
19321
19322 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019323 if (argvars[1].v_type != VAR_UNKNOWN)
19324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019325 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19326 if (pat == NULL)
19327 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019328 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019329 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019330 }
19331 if (pat == NULL || *pat == NUL)
19332 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019333
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019334 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019336 if (typeerr)
19337 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338
Bram Moolenaar0d660222005-01-07 21:51:51 +000019339 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19340 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019342 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019343 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019344 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019345 if (*str == NUL)
19346 match = FALSE; /* empty item at the end */
19347 else
19348 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019349 if (match)
19350 end = regmatch.startp[0];
19351 else
19352 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019353 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19354 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019355 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019356 if (list_append_string(rettv->vval.v_list, str,
19357 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019358 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019359 }
19360 if (!match)
19361 break;
19362 /* Advance to just after the match. */
19363 if (regmatch.endp[0] > str)
19364 col = 0;
19365 else
19366 {
19367 /* Don't get stuck at the same match. */
19368#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019369 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019370#else
19371 col = 1;
19372#endif
19373 }
19374 str = regmatch.endp[0];
19375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376
Bram Moolenaar473de612013-06-08 18:19:48 +020019377 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379
Bram Moolenaar0d660222005-01-07 21:51:51 +000019380 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381}
19382
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019383#ifdef FEAT_FLOAT
19384/*
19385 * "sqrt()" function
19386 */
19387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019388f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019389{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019390 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019391
19392 rettv->v_type = VAR_FLOAT;
19393 if (get_float_arg(argvars, &f) == OK)
19394 rettv->vval.v_float = sqrt(f);
19395 else
19396 rettv->vval.v_float = 0.0;
19397}
19398
19399/*
19400 * "str2float()" function
19401 */
19402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019403f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019404{
19405 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19406
19407 if (*p == '+')
19408 p = skipwhite(p + 1);
19409 (void)string2float(p, &rettv->vval.v_float);
19410 rettv->v_type = VAR_FLOAT;
19411}
19412#endif
19413
Bram Moolenaar2c932302006-03-18 21:42:09 +000019414/*
19415 * "str2nr()" function
19416 */
19417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019418f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019419{
19420 int base = 10;
19421 char_u *p;
19422 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019423 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019424
19425 if (argvars[1].v_type != VAR_UNKNOWN)
19426 {
19427 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019428 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019429 {
19430 EMSG(_(e_invarg));
19431 return;
19432 }
19433 }
19434
19435 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019436 if (*p == '+')
19437 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019438 switch (base)
19439 {
19440 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19441 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19442 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19443 default: what = 0;
19444 }
19445 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019446 rettv->vval.v_number = n;
19447}
19448
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449#ifdef HAVE_STRFTIME
19450/*
19451 * "strftime({format}[, {time}])" function
19452 */
19453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019454f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455{
19456 char_u result_buf[256];
19457 struct tm *curtime;
19458 time_t seconds;
19459 char_u *p;
19460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019461 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019463 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019464 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465 seconds = time(NULL);
19466 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019467 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019468 curtime = localtime(&seconds);
19469 /* MSVC returns NULL for an invalid value of seconds. */
19470 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019471 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 else
19473 {
19474# ifdef FEAT_MBYTE
19475 vimconv_T conv;
19476 char_u *enc;
19477
19478 conv.vc_type = CONV_NONE;
19479 enc = enc_locale();
19480 convert_setup(&conv, p_enc, enc);
19481 if (conv.vc_type != CONV_NONE)
19482 p = string_convert(&conv, p, NULL);
19483# endif
19484 if (p != NULL)
19485 (void)strftime((char *)result_buf, sizeof(result_buf),
19486 (char *)p, curtime);
19487 else
19488 result_buf[0] = NUL;
19489
19490# ifdef FEAT_MBYTE
19491 if (conv.vc_type != CONV_NONE)
19492 vim_free(p);
19493 convert_setup(&conv, enc, p_enc);
19494 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019495 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496 else
19497# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019498 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499
19500# ifdef FEAT_MBYTE
19501 /* Release conversion descriptors */
19502 convert_setup(&conv, NULL, NULL);
19503 vim_free(enc);
19504# endif
19505 }
19506}
19507#endif
19508
19509/*
19510 * "stridx()" function
19511 */
19512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019513f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514{
19515 char_u buf[NUMBUFLEN];
19516 char_u *needle;
19517 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019518 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019520 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019522 needle = get_tv_string_chk(&argvars[1]);
19523 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019524 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019525 if (needle == NULL || haystack == NULL)
19526 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527
Bram Moolenaar33570922005-01-25 22:26:29 +000019528 if (argvars[2].v_type != VAR_UNKNOWN)
19529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019530 int error = FALSE;
19531
19532 start_idx = get_tv_number_chk(&argvars[2], &error);
19533 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019535 if (start_idx >= 0)
19536 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019537 }
19538
19539 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19540 if (pos != NULL)
19541 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542}
19543
19544/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019545 * "string()" function
19546 */
19547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019548f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019549{
19550 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019551 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019553 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019554 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19555 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019556 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019557 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019558 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019559}
19560
19561/*
19562 * "strlen()" function
19563 */
19564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019565f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019567 rettv->vval.v_number = (varnumber_T)(STRLEN(
19568 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569}
19570
19571/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019572 * "strchars()" function
19573 */
19574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019575f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019576{
19577 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019578 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019579#ifdef FEAT_MBYTE
19580 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019581 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019582#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019583
19584 if (argvars[1].v_type != VAR_UNKNOWN)
19585 skipcc = get_tv_number_chk(&argvars[1], NULL);
19586 if (skipcc < 0 || skipcc > 1)
19587 EMSG(_(e_invarg));
19588 else
19589 {
19590#ifdef FEAT_MBYTE
19591 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19592 while (*s != NUL)
19593 {
19594 func_mb_ptr2char_adv(&s);
19595 ++len;
19596 }
19597 rettv->vval.v_number = len;
19598#else
19599 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19600#endif
19601 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019602}
19603
19604/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019605 * "strdisplaywidth()" function
19606 */
19607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019608f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019609{
19610 char_u *s = get_tv_string(&argvars[0]);
19611 int col = 0;
19612
19613 if (argvars[1].v_type != VAR_UNKNOWN)
19614 col = get_tv_number(&argvars[1]);
19615
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019616 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019617}
19618
19619/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019620 * "strwidth()" function
19621 */
19622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019623f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019624{
19625 char_u *s = get_tv_string(&argvars[0]);
19626
19627 rettv->vval.v_number = (varnumber_T)(
19628#ifdef FEAT_MBYTE
19629 mb_string2cells(s, -1)
19630#else
19631 STRLEN(s)
19632#endif
19633 );
19634}
19635
19636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637 * "strpart()" function
19638 */
19639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019640f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641{
19642 char_u *p;
19643 int n;
19644 int len;
19645 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019646 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 slen = (int)STRLEN(p);
19650
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019651 n = get_tv_number_chk(&argvars[1], &error);
19652 if (error)
19653 len = 0;
19654 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019655 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 else
19657 len = slen - n; /* default len: all bytes that are available. */
19658
19659 /*
19660 * Only return the overlap between the specified part and the actual
19661 * string.
19662 */
19663 if (n < 0)
19664 {
19665 len += n;
19666 n = 0;
19667 }
19668 else if (n > slen)
19669 n = slen;
19670 if (len < 0)
19671 len = 0;
19672 else if (n + len > slen)
19673 len = slen - n;
19674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019675 rettv->v_type = VAR_STRING;
19676 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677}
19678
19679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019680 * "strridx()" function
19681 */
19682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019683f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019684{
19685 char_u buf[NUMBUFLEN];
19686 char_u *needle;
19687 char_u *haystack;
19688 char_u *rest;
19689 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019690 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019692 needle = get_tv_string_chk(&argvars[1]);
19693 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019694
19695 rettv->vval.v_number = -1;
19696 if (needle == NULL || haystack == NULL)
19697 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019698
19699 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019700 if (argvars[2].v_type != VAR_UNKNOWN)
19701 {
19702 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019703 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019704 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019705 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019706 }
19707 else
19708 end_idx = haystack_len;
19709
Bram Moolenaar0d660222005-01-07 21:51:51 +000019710 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019711 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019712 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019713 lastmatch = haystack + end_idx;
19714 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019715 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019716 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019717 for (rest = haystack; *rest != '\0'; ++rest)
19718 {
19719 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019720 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019721 break;
19722 lastmatch = rest;
19723 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019724 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019725
19726 if (lastmatch == NULL)
19727 rettv->vval.v_number = -1;
19728 else
19729 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19730}
19731
19732/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 * "strtrans()" function
19734 */
19735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019736f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019738 rettv->v_type = VAR_STRING;
19739 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740}
19741
19742/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019743 * "submatch()" function
19744 */
19745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019746f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019747{
Bram Moolenaar41571762014-04-02 19:00:58 +020019748 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019749 int no;
19750 int retList = 0;
19751
19752 no = (int)get_tv_number_chk(&argvars[0], &error);
19753 if (error)
19754 return;
19755 error = FALSE;
19756 if (argvars[1].v_type != VAR_UNKNOWN)
19757 retList = get_tv_number_chk(&argvars[1], &error);
19758 if (error)
19759 return;
19760
19761 if (retList == 0)
19762 {
19763 rettv->v_type = VAR_STRING;
19764 rettv->vval.v_string = reg_submatch(no);
19765 }
19766 else
19767 {
19768 rettv->v_type = VAR_LIST;
19769 rettv->vval.v_list = reg_submatch_list(no);
19770 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019771}
19772
19773/*
19774 * "substitute()" function
19775 */
19776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019777f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019778{
19779 char_u patbuf[NUMBUFLEN];
19780 char_u subbuf[NUMBUFLEN];
19781 char_u flagsbuf[NUMBUFLEN];
19782
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019783 char_u *str = get_tv_string_chk(&argvars[0]);
19784 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19785 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19786 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19787
Bram Moolenaar0d660222005-01-07 21:51:51 +000019788 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019789 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19790 rettv->vval.v_string = NULL;
19791 else
19792 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019793}
19794
19795/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019796 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019799f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800{
19801 int id = 0;
19802#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019803 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 long col;
19805 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019806 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019808 lnum = get_tv_lnum(argvars); /* -1 on type error */
19809 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19810 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019812 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019813 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019814 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815#endif
19816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019817 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818}
19819
19820/*
19821 * "synIDattr(id, what [, mode])" function
19822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019824f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825{
19826 char_u *p = NULL;
19827#ifdef FEAT_SYN_HL
19828 int id;
19829 char_u *what;
19830 char_u *mode;
19831 char_u modebuf[NUMBUFLEN];
19832 int modec;
19833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 id = get_tv_number(&argvars[0]);
19835 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019836 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019838 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019839 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019840 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 modec = 0; /* replace invalid with current */
19842 }
19843 else
19844 {
19845#ifdef FEAT_GUI
19846 if (gui.in_use)
19847 modec = 'g';
19848 else
19849#endif
19850 if (t_colors > 1)
19851 modec = 'c';
19852 else
19853 modec = 't';
19854 }
19855
19856
19857 switch (TOLOWER_ASC(what[0]))
19858 {
19859 case 'b':
19860 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19861 p = highlight_color(id, what, modec);
19862 else /* bold */
19863 p = highlight_has_attr(id, HL_BOLD, modec);
19864 break;
19865
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019866 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867 p = highlight_color(id, what, modec);
19868 break;
19869
19870 case 'i':
19871 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19872 p = highlight_has_attr(id, HL_INVERSE, modec);
19873 else /* italic */
19874 p = highlight_has_attr(id, HL_ITALIC, modec);
19875 break;
19876
19877 case 'n': /* name */
19878 p = get_highlight_name(NULL, id - 1);
19879 break;
19880
19881 case 'r': /* reverse */
19882 p = highlight_has_attr(id, HL_INVERSE, modec);
19883 break;
19884
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019885 case 's':
19886 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19887 p = highlight_color(id, what, modec);
19888 else /* standout */
19889 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 break;
19891
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019892 case 'u':
19893 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19894 /* underline */
19895 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19896 else
19897 /* undercurl */
19898 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 break;
19900 }
19901
19902 if (p != NULL)
19903 p = vim_strsave(p);
19904#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019905 rettv->v_type = VAR_STRING;
19906 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907}
19908
19909/*
19910 * "synIDtrans(id)" function
19911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019913f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019914{
19915 int id;
19916
19917#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019918 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919
19920 if (id > 0)
19921 id = syn_get_final_id(id);
19922 else
19923#endif
19924 id = 0;
19925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019926 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019927}
19928
19929/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019930 * "synconcealed(lnum, col)" function
19931 */
19932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019933f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019934{
19935#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19936 long lnum;
19937 long col;
19938 int syntax_flags = 0;
19939 int cchar;
19940 int matchid = 0;
19941 char_u str[NUMBUFLEN];
19942#endif
19943
19944 rettv->v_type = VAR_LIST;
19945 rettv->vval.v_list = NULL;
19946
19947#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19948 lnum = get_tv_lnum(argvars); /* -1 on type error */
19949 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19950
19951 vim_memset(str, NUL, sizeof(str));
19952
19953 if (rettv_list_alloc(rettv) != FAIL)
19954 {
19955 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19956 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19957 && curwin->w_p_cole > 0)
19958 {
19959 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19960 syntax_flags = get_syntax_info(&matchid);
19961
19962 /* get the conceal character */
19963 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19964 {
19965 cchar = syn_get_sub_char();
19966 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19967 cchar = lcs_conceal;
19968 if (cchar != NUL)
19969 {
19970# ifdef FEAT_MBYTE
19971 if (has_mbyte)
19972 (*mb_char2bytes)(cchar, str);
19973 else
19974# endif
19975 str[0] = cchar;
19976 }
19977 }
19978 }
19979
19980 list_append_number(rettv->vval.v_list,
19981 (syntax_flags & HL_CONCEAL) != 0);
19982 /* -1 to auto-determine strlen */
19983 list_append_string(rettv->vval.v_list, str, -1);
19984 list_append_number(rettv->vval.v_list, matchid);
19985 }
19986#endif
19987}
19988
19989/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019990 * "synstack(lnum, col)" function
19991 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019993f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019994{
19995#ifdef FEAT_SYN_HL
19996 long lnum;
19997 long col;
19998 int i;
19999 int id;
20000#endif
20001
20002 rettv->v_type = VAR_LIST;
20003 rettv->vval.v_list = NULL;
20004
20005#ifdef FEAT_SYN_HL
20006 lnum = get_tv_lnum(argvars); /* -1 on type error */
20007 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20008
20009 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020010 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020011 && rettv_list_alloc(rettv) != FAIL)
20012 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020013 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020014 for (i = 0; ; ++i)
20015 {
20016 id = syn_get_stack_item(i);
20017 if (id < 0)
20018 break;
20019 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20020 break;
20021 }
20022 }
20023#endif
20024}
20025
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020027get_cmd_output_as_rettv(
20028 typval_T *argvars,
20029 typval_T *rettv,
20030 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020032 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020034 char_u *infile = NULL;
20035 char_u buf[NUMBUFLEN];
20036 int err = FALSE;
20037 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020038 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020039 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020041 rettv->v_type = VAR_STRING;
20042 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020043 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020044 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020045
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020046 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020047 {
20048 /*
20049 * Write the string to a temp file, to be used for input of the shell
20050 * command.
20051 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020052 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020053 {
20054 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020055 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020056 }
20057
20058 fd = mch_fopen((char *)infile, WRITEBIN);
20059 if (fd == NULL)
20060 {
20061 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020062 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020063 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020064 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020065 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020066 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20067 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020068 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020069 else
20070 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020071 size_t len;
20072
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020073 p = get_tv_string_buf_chk(&argvars[1], buf);
20074 if (p == NULL)
20075 {
20076 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020077 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020078 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020079 len = STRLEN(p);
20080 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020081 err = TRUE;
20082 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020083 if (fclose(fd) != 0)
20084 err = TRUE;
20085 if (err)
20086 {
20087 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020088 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020089 }
20090 }
20091
Bram Moolenaar52a72462014-08-29 15:53:52 +020020092 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20093 * echoes typeahead, that messes up the display. */
20094 if (!msg_silent)
20095 flags += SHELL_COOKED;
20096
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020097 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020099 int len;
20100 listitem_T *li;
20101 char_u *s = NULL;
20102 char_u *start;
20103 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020104 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105
Bram Moolenaar52a72462014-08-29 15:53:52 +020020106 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020107 if (res == NULL)
20108 goto errret;
20109
20110 list = list_alloc();
20111 if (list == NULL)
20112 goto errret;
20113
20114 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020116 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020117 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020118 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020119 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020120
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020121 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020122 if (s == NULL)
20123 goto errret;
20124
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020125 for (p = s; start < end; ++p, ++start)
20126 *p = *start == NUL ? NL : *start;
20127 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020128
20129 li = listitem_alloc();
20130 if (li == NULL)
20131 {
20132 vim_free(s);
20133 goto errret;
20134 }
20135 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020136 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020137 li->li_tv.vval.v_string = s;
20138 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020140
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020141 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020142 rettv->v_type = VAR_LIST;
20143 rettv->vval.v_list = list;
20144 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020146 else
20147 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020148 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020149#ifdef USE_CR
20150 /* translate <CR> into <NL> */
20151 if (res != NULL)
20152 {
20153 char_u *s;
20154
20155 for (s = res; *s; ++s)
20156 {
20157 if (*s == CAR)
20158 *s = NL;
20159 }
20160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020161#else
20162# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020163 /* translate <CR><NL> into <NL> */
20164 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020166 char_u *s, *d;
20167
20168 d = res;
20169 for (s = res; *s; ++s)
20170 {
20171 if (s[0] == CAR && s[1] == NL)
20172 ++s;
20173 *d++ = *s;
20174 }
20175 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177# endif
20178#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020179 rettv->vval.v_string = res;
20180 res = NULL;
20181 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020182
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020183errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020184 if (infile != NULL)
20185 {
20186 mch_remove(infile);
20187 vim_free(infile);
20188 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020189 if (res != NULL)
20190 vim_free(res);
20191 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020192 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020193}
20194
20195/*
20196 * "system()" function
20197 */
20198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020199f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020200{
20201 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20202}
20203
20204/*
20205 * "systemlist()" function
20206 */
20207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020208f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020209{
20210 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211}
20212
20213/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020214 * "tabpagebuflist()" function
20215 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020217f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020218{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020219#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020220 tabpage_T *tp;
20221 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020222
20223 if (argvars[0].v_type == VAR_UNKNOWN)
20224 wp = firstwin;
20225 else
20226 {
20227 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20228 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020229 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020230 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020231 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020232 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020233 for (; wp != NULL; wp = wp->w_next)
20234 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020235 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020236 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020237 }
20238#endif
20239}
20240
20241
20242/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020243 * "tabpagenr()" function
20244 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020246f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020247{
20248 int nr = 1;
20249#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020250 char_u *arg;
20251
20252 if (argvars[0].v_type != VAR_UNKNOWN)
20253 {
20254 arg = get_tv_string_chk(&argvars[0]);
20255 nr = 0;
20256 if (arg != NULL)
20257 {
20258 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020259 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020260 else
20261 EMSG2(_(e_invexpr2), arg);
20262 }
20263 }
20264 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020265 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020266#endif
20267 rettv->vval.v_number = nr;
20268}
20269
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020270
20271#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020272static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020273
20274/*
20275 * Common code for tabpagewinnr() and winnr().
20276 */
20277 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020278get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020279{
20280 win_T *twin;
20281 int nr = 1;
20282 win_T *wp;
20283 char_u *arg;
20284
20285 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20286 if (argvar->v_type != VAR_UNKNOWN)
20287 {
20288 arg = get_tv_string_chk(argvar);
20289 if (arg == NULL)
20290 nr = 0; /* type error; errmsg already given */
20291 else if (STRCMP(arg, "$") == 0)
20292 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20293 else if (STRCMP(arg, "#") == 0)
20294 {
20295 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20296 if (twin == NULL)
20297 nr = 0;
20298 }
20299 else
20300 {
20301 EMSG2(_(e_invexpr2), arg);
20302 nr = 0;
20303 }
20304 }
20305
20306 if (nr > 0)
20307 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20308 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020309 {
20310 if (wp == NULL)
20311 {
20312 /* didn't find it in this tabpage */
20313 nr = 0;
20314 break;
20315 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020316 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020317 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020318 return nr;
20319}
20320#endif
20321
20322/*
20323 * "tabpagewinnr()" function
20324 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020326f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020327{
20328 int nr = 1;
20329#ifdef FEAT_WINDOWS
20330 tabpage_T *tp;
20331
20332 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20333 if (tp == NULL)
20334 nr = 0;
20335 else
20336 nr = get_winnr(tp, &argvars[1]);
20337#endif
20338 rettv->vval.v_number = nr;
20339}
20340
20341
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020342/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020343 * "tagfiles()" function
20344 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020346f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020347{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020348 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020349 tagname_T tn;
20350 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020351
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020352 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020353 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020354 fname = alloc(MAXPATHL);
20355 if (fname == NULL)
20356 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020357
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020358 for (first = TRUE; ; first = FALSE)
20359 if (get_tagfname(&tn, first, fname) == FAIL
20360 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020361 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020362 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020363 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020364}
20365
20366/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020367 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020368 */
20369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020370f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020371{
20372 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020373
20374 tag_pattern = get_tv_string(&argvars[0]);
20375
20376 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020377 if (*tag_pattern == NUL)
20378 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020379
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020380 if (rettv_list_alloc(rettv) == OK)
20381 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020382}
20383
20384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385 * "tempname()" function
20386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020388f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389{
20390 static int x = 'A';
20391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020392 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020393 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394
20395 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20396 * names. Skip 'I' and 'O', they are used for shell redirection. */
20397 do
20398 {
20399 if (x == 'Z')
20400 x = '0';
20401 else if (x == '9')
20402 x = 'A';
20403 else
20404 {
20405#ifdef EBCDIC
20406 if (x == 'I')
20407 x = 'J';
20408 else if (x == 'R')
20409 x = 'S';
20410 else
20411#endif
20412 ++x;
20413 }
20414 } while (x == 'I' || x == 'O');
20415}
20416
20417/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020418 * "test(list)" function: Just checking the walls...
20419 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020421f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020422{
20423 /* Used for unit testing. Change the code below to your liking. */
20424#if 0
20425 listitem_T *li;
20426 list_T *l;
20427 char_u *bad, *good;
20428
20429 if (argvars[0].v_type != VAR_LIST)
20430 return;
20431 l = argvars[0].vval.v_list;
20432 if (l == NULL)
20433 return;
20434 li = l->lv_first;
20435 if (li == NULL)
20436 return;
20437 bad = get_tv_string(&li->li_tv);
20438 li = li->li_next;
20439 if (li == NULL)
20440 return;
20441 good = get_tv_string(&li->li_tv);
20442 rettv->vval.v_number = test_edit_score(bad, good);
20443#endif
20444}
20445
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020446#ifdef FEAT_FLOAT
20447/*
20448 * "tan()" function
20449 */
20450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020451f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020452{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020453 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020454
20455 rettv->v_type = VAR_FLOAT;
20456 if (get_float_arg(argvars, &f) == OK)
20457 rettv->vval.v_float = tan(f);
20458 else
20459 rettv->vval.v_float = 0.0;
20460}
20461
20462/*
20463 * "tanh()" function
20464 */
20465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020466f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020467{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020468 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020469
20470 rettv->v_type = VAR_FLOAT;
20471 if (get_float_arg(argvars, &f) == OK)
20472 rettv->vval.v_float = tanh(f);
20473 else
20474 rettv->vval.v_float = 0.0;
20475}
20476#endif
20477
Bram Moolenaar975b5272016-03-15 23:10:59 +010020478#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20479/*
20480 * Get a callback from "arg". It can be a Funcref or a function name.
20481 * When "arg" is zero return an empty string.
20482 * Return NULL for an invalid argument.
20483 */
20484 char_u *
20485get_callback(typval_T *arg, partial_T **pp)
20486{
20487 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20488 {
20489 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020490 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020491 return (*pp)->pt_name;
20492 }
20493 *pp = NULL;
20494 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20495 return arg->vval.v_string;
20496 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20497 return (char_u *)"";
20498 EMSG(_("E921: Invalid callback argument"));
20499 return NULL;
20500}
20501#endif
20502
20503#ifdef FEAT_TIMERS
20504/*
20505 * "timer_start(time, callback [, options])" function
20506 */
20507 static void
20508f_timer_start(typval_T *argvars, typval_T *rettv)
20509{
20510 long msec = get_tv_number(&argvars[0]);
20511 timer_T *timer;
20512 int repeat = 0;
20513 char_u *callback;
20514 dict_T *dict;
20515
20516 if (argvars[2].v_type != VAR_UNKNOWN)
20517 {
20518 if (argvars[2].v_type != VAR_DICT
20519 || (dict = argvars[2].vval.v_dict) == NULL)
20520 {
20521 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20522 return;
20523 }
20524 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20525 repeat = get_dict_number(dict, (char_u *)"repeat");
20526 }
20527
20528 timer = create_timer(msec, repeat);
20529 callback = get_callback(&argvars[1], &timer->tr_partial);
20530 if (callback == NULL)
20531 {
20532 stop_timer(timer);
20533 rettv->vval.v_number = -1;
20534 }
20535 else
20536 {
20537 timer->tr_callback = vim_strsave(callback);
20538 rettv->vval.v_number = timer->tr_id;
20539 }
20540}
20541
20542/*
20543 * "timer_stop(timer)" function
20544 */
20545 static void
20546f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20547{
20548 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20549
20550 if (timer != NULL)
20551 stop_timer(timer);
20552}
20553#endif
20554
Bram Moolenaard52d9742005-08-21 22:20:28 +000020555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 * "tolower(string)" function
20557 */
20558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020559f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560{
20561 char_u *p;
20562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020563 p = vim_strsave(get_tv_string(&argvars[0]));
20564 rettv->v_type = VAR_STRING;
20565 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566
20567 if (p != NULL)
20568 while (*p != NUL)
20569 {
20570#ifdef FEAT_MBYTE
20571 int l;
20572
20573 if (enc_utf8)
20574 {
20575 int c, lc;
20576
20577 c = utf_ptr2char(p);
20578 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020579 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 /* TODO: reallocate string when byte count changes. */
20581 if (utf_char2len(lc) == l)
20582 utf_char2bytes(lc, p);
20583 p += l;
20584 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020585 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 p += l; /* skip multi-byte character */
20587 else
20588#endif
20589 {
20590 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20591 ++p;
20592 }
20593 }
20594}
20595
20596/*
20597 * "toupper(string)" function
20598 */
20599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020600f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020602 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020603 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020604}
20605
20606/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020607 * "tr(string, fromstr, tostr)" function
20608 */
20609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020610f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020611{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020612 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020613 char_u *fromstr;
20614 char_u *tostr;
20615 char_u *p;
20616#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020617 int inlen;
20618 int fromlen;
20619 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020620 int idx;
20621 char_u *cpstr;
20622 int cplen;
20623 int first = TRUE;
20624#endif
20625 char_u buf[NUMBUFLEN];
20626 char_u buf2[NUMBUFLEN];
20627 garray_T ga;
20628
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020629 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020630 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20631 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020632
20633 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634 rettv->v_type = VAR_STRING;
20635 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020636 if (fromstr == NULL || tostr == NULL)
20637 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020638 ga_init2(&ga, (int)sizeof(char), 80);
20639
20640#ifdef FEAT_MBYTE
20641 if (!has_mbyte)
20642#endif
20643 /* not multi-byte: fromstr and tostr must be the same length */
20644 if (STRLEN(fromstr) != STRLEN(tostr))
20645 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020646#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020647error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020648#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020649 EMSG2(_(e_invarg2), fromstr);
20650 ga_clear(&ga);
20651 return;
20652 }
20653
20654 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020655 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020656 {
20657#ifdef FEAT_MBYTE
20658 if (has_mbyte)
20659 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020660 inlen = (*mb_ptr2len)(in_str);
20661 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020662 cplen = inlen;
20663 idx = 0;
20664 for (p = fromstr; *p != NUL; p += fromlen)
20665 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020666 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020667 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020668 {
20669 for (p = tostr; *p != NUL; p += tolen)
20670 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020671 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020672 if (idx-- == 0)
20673 {
20674 cplen = tolen;
20675 cpstr = p;
20676 break;
20677 }
20678 }
20679 if (*p == NUL) /* tostr is shorter than fromstr */
20680 goto error;
20681 break;
20682 }
20683 ++idx;
20684 }
20685
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020686 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020687 {
20688 /* Check that fromstr and tostr have the same number of
20689 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020690 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020691 first = FALSE;
20692 for (p = tostr; *p != NUL; p += tolen)
20693 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020694 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020695 --idx;
20696 }
20697 if (idx != 0)
20698 goto error;
20699 }
20700
Bram Moolenaarcde88542015-08-11 19:14:00 +020020701 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020702 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020703 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020704
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020705 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020706 }
20707 else
20708#endif
20709 {
20710 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020711 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020712 if (p != NULL)
20713 ga_append(&ga, tostr[p - fromstr]);
20714 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020715 ga_append(&ga, *in_str);
20716 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020717 }
20718 }
20719
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020720 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020721 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020722 ga_append(&ga, NUL);
20723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020724 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020725}
20726
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020727#ifdef FEAT_FLOAT
20728/*
20729 * "trunc({float})" function
20730 */
20731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020732f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020733{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020734 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020735
20736 rettv->v_type = VAR_FLOAT;
20737 if (get_float_arg(argvars, &f) == OK)
20738 /* trunc() is not in C90, use floor() or ceil() instead. */
20739 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20740 else
20741 rettv->vval.v_float = 0.0;
20742}
20743#endif
20744
Bram Moolenaar8299df92004-07-10 09:47:34 +000020745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746 * "type(expr)" function
20747 */
20748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020749f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020751 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020752
20753 switch (argvars[0].v_type)
20754 {
20755 case VAR_NUMBER: n = 0; break;
20756 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020757 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020758 case VAR_FUNC: n = 2; break;
20759 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020760 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020761 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020762 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020763 if (argvars[0].vval.v_number == VVAL_FALSE
20764 || argvars[0].vval.v_number == VVAL_TRUE)
20765 n = 6;
20766 else
20767 n = 7;
20768 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020769 case VAR_JOB: n = 8; break;
20770 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020771 case VAR_UNKNOWN:
20772 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20773 n = -1;
20774 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020775 }
20776 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020777}
20778
20779/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020780 * "undofile(name)" function
20781 */
20782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020783f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020784{
20785 rettv->v_type = VAR_STRING;
20786#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020787 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020788 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020789
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020790 if (*fname == NUL)
20791 {
20792 /* If there is no file name there will be no undo file. */
20793 rettv->vval.v_string = NULL;
20794 }
20795 else
20796 {
20797 char_u *ffname = FullName_save(fname, FALSE);
20798
20799 if (ffname != NULL)
20800 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20801 vim_free(ffname);
20802 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020803 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020804#else
20805 rettv->vval.v_string = NULL;
20806#endif
20807}
20808
20809/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020810 * "undotree()" function
20811 */
20812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020813f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020814{
20815 if (rettv_dict_alloc(rettv) == OK)
20816 {
20817 dict_T *dict = rettv->vval.v_dict;
20818 list_T *list;
20819
Bram Moolenaar730cde92010-06-27 05:18:54 +020020820 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020821 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020822 dict_add_nr_str(dict, "save_last",
20823 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020824 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20825 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020826 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020827
20828 list = list_alloc();
20829 if (list != NULL)
20830 {
20831 u_eval_tree(curbuf->b_u_oldhead, list);
20832 dict_add_list(dict, "entries", list);
20833 }
20834 }
20835}
20836
20837/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020838 * "values(dict)" function
20839 */
20840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020841f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020842{
20843 dict_list(argvars, rettv, 1);
20844}
20845
20846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847 * "virtcol(string)" function
20848 */
20849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020850f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851{
20852 colnr_T vcol = 0;
20853 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020854 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020856 fp = var2fpos(&argvars[0], FALSE, &fnum);
20857 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20858 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 {
20860 getvvcol(curwin, fp, NULL, NULL, &vcol);
20861 ++vcol;
20862 }
20863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020864 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865}
20866
20867/*
20868 * "visualmode()" function
20869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020871f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873 char_u str[2];
20874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020875 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 str[0] = curbuf->b_visual_mode_eval;
20877 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020878 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879
20880 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020881 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883}
20884
20885/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020886 * "wildmenumode()" function
20887 */
20888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020889f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020890{
20891#ifdef FEAT_WILDMENU
20892 if (wild_menu_showing)
20893 rettv->vval.v_number = 1;
20894#endif
20895}
20896
20897/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898 * "winbufnr(nr)" function
20899 */
20900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020901f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020902{
20903 win_T *wp;
20904
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020905 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020907 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020909 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910}
20911
20912/*
20913 * "wincol()" function
20914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020916f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917{
20918 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020919 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920}
20921
20922/*
20923 * "winheight(nr)" function
20924 */
20925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020926f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927{
20928 win_T *wp;
20929
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020930 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020932 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020934 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935}
20936
20937/*
20938 * "winline()" function
20939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020941f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942{
20943 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020944 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945}
20946
20947/*
20948 * "winnr()" function
20949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020951f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952{
20953 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020954
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020956 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020958 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959}
20960
20961/*
20962 * "winrestcmd()" function
20963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020965f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966{
20967#ifdef FEAT_WINDOWS
20968 win_T *wp;
20969 int winnr = 1;
20970 garray_T ga;
20971 char_u buf[50];
20972
20973 ga_init2(&ga, (int)sizeof(char), 70);
20974 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20975 {
20976 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20977 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20979 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 ++winnr;
20981 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020982 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020984 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020986 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020988 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989}
20990
20991/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020992 * "winrestview()" function
20993 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020995f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020996{
20997 dict_T *dict;
20998
20999 if (argvars[0].v_type != VAR_DICT
21000 || (dict = argvars[0].vval.v_dict) == NULL)
21001 EMSG(_(e_invarg));
21002 else
21003 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021004 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21005 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21006 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21007 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021008#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021009 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21010 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021011#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021012 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21013 {
21014 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21015 curwin->w_set_curswant = FALSE;
21016 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021017
Bram Moolenaar82c25852014-05-28 16:47:16 +020021018 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21019 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021020#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021021 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21022 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021023#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021024 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21025 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21026 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21027 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021028
21029 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021030 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021031# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021032 win_new_width(curwin, W_WIDTH(curwin));
21033# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021034 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021035
Bram Moolenaarb851a962014-10-31 15:45:52 +010021036 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021037 curwin->w_topline = 1;
21038 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21039 curwin->w_topline = curbuf->b_ml.ml_line_count;
21040#ifdef FEAT_DIFF
21041 check_topfill(curwin, TRUE);
21042#endif
21043 }
21044}
21045
21046/*
21047 * "winsaveview()" function
21048 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021050f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021051{
21052 dict_T *dict;
21053
Bram Moolenaara800b422010-06-27 01:15:55 +020021054 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021055 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021056 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021057
21058 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21059 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21060#ifdef FEAT_VIRTUALEDIT
21061 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21062#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021063 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021064 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21065
21066 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21067#ifdef FEAT_DIFF
21068 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21069#endif
21070 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21071 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21072}
21073
21074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075 * "winwidth(nr)" function
21076 */
21077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021078f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079{
21080 win_T *wp;
21081
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021082 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021084 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021086#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021087 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021089 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090#endif
21091}
21092
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021094 * "wordcount()" function
21095 */
21096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021097f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021098{
21099 if (rettv_dict_alloc(rettv) == FAIL)
21100 return;
21101 cursor_pos_info(rettv->vval.v_dict);
21102}
21103
21104/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021105 * Write list of strings to file
21106 */
21107 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021108write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021109{
21110 listitem_T *li;
21111 int c;
21112 int ret = OK;
21113 char_u *s;
21114
21115 for (li = list->lv_first; li != NULL; li = li->li_next)
21116 {
21117 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21118 {
21119 if (*s == '\n')
21120 c = putc(NUL, fd);
21121 else
21122 c = putc(*s, fd);
21123 if (c == EOF)
21124 {
21125 ret = FAIL;
21126 break;
21127 }
21128 }
21129 if (!binary || li->li_next != NULL)
21130 if (putc('\n', fd) == EOF)
21131 {
21132 ret = FAIL;
21133 break;
21134 }
21135 if (ret == FAIL)
21136 {
21137 EMSG(_(e_write));
21138 break;
21139 }
21140 }
21141 return ret;
21142}
21143
21144/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021145 * "writefile()" function
21146 */
21147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021148f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021149{
21150 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021151 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021152 char_u *fname;
21153 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021154 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021155
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021156 if (check_restricted() || check_secure())
21157 return;
21158
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021159 if (argvars[0].v_type != VAR_LIST)
21160 {
21161 EMSG2(_(e_listarg), "writefile()");
21162 return;
21163 }
21164 if (argvars[0].vval.v_list == NULL)
21165 return;
21166
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021167 if (argvars[2].v_type != VAR_UNKNOWN)
21168 {
21169 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21170 binary = TRUE;
21171 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21172 append = TRUE;
21173 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021174
21175 /* Always open the file in binary mode, library functions have a mind of
21176 * their own about CR-LF conversion. */
21177 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021178 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21179 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021180 {
21181 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21182 ret = -1;
21183 }
21184 else
21185 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021186 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21187 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021188 fclose(fd);
21189 }
21190
21191 rettv->vval.v_number = ret;
21192}
21193
21194/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021195 * "xor(expr, expr)" function
21196 */
21197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021198f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021199{
21200 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21201 ^ get_tv_number_chk(&argvars[1], NULL);
21202}
21203
21204
21205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021207 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 */
21209 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210var2fpos(
21211 typval_T *varp,
21212 int dollar_lnum, /* TRUE when $ is last line */
21213 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021215 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021217 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218
Bram Moolenaara5525202006-03-02 22:52:09 +000021219 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021220 if (varp->v_type == VAR_LIST)
21221 {
21222 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021223 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021224 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021225 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021226
21227 l = varp->vval.v_list;
21228 if (l == NULL)
21229 return NULL;
21230
21231 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021232 pos.lnum = list_find_nr(l, 0L, &error);
21233 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021234 return NULL; /* invalid line number */
21235
21236 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021237 pos.col = list_find_nr(l, 1L, &error);
21238 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021239 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021240 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021241
21242 /* We accept "$" for the column number: last column. */
21243 li = list_find(l, 1L);
21244 if (li != NULL && li->li_tv.v_type == VAR_STRING
21245 && li->li_tv.vval.v_string != NULL
21246 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21247 pos.col = len + 1;
21248
Bram Moolenaara5525202006-03-02 22:52:09 +000021249 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021250 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021251 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021252 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021253
Bram Moolenaara5525202006-03-02 22:52:09 +000021254#ifdef FEAT_VIRTUALEDIT
21255 /* Get the virtual offset. Defaults to zero. */
21256 pos.coladd = list_find_nr(l, 2L, &error);
21257 if (error)
21258 pos.coladd = 0;
21259#endif
21260
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021261 return &pos;
21262 }
21263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021264 name = get_tv_string_chk(varp);
21265 if (name == NULL)
21266 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021267 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021268 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021269 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21270 {
21271 if (VIsual_active)
21272 return &VIsual;
21273 return &curwin->w_cursor;
21274 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021275 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021277 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21279 return NULL;
21280 return pp;
21281 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021282
21283#ifdef FEAT_VIRTUALEDIT
21284 pos.coladd = 0;
21285#endif
21286
Bram Moolenaar477933c2007-07-17 14:32:23 +000021287 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021288 {
21289 pos.col = 0;
21290 if (name[1] == '0') /* "w0": first visible line */
21291 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021292 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021293 pos.lnum = curwin->w_topline;
21294 return &pos;
21295 }
21296 else if (name[1] == '$') /* "w$": last visible line */
21297 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021298 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021299 pos.lnum = curwin->w_botline - 1;
21300 return &pos;
21301 }
21302 }
21303 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021305 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 {
21307 pos.lnum = curbuf->b_ml.ml_line_count;
21308 pos.col = 0;
21309 }
21310 else
21311 {
21312 pos.lnum = curwin->w_cursor.lnum;
21313 pos.col = (colnr_T)STRLEN(ml_get_curline());
21314 }
21315 return &pos;
21316 }
21317 return NULL;
21318}
21319
21320/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021321 * Convert list in "arg" into a position and optional file number.
21322 * When "fnump" is NULL there is no file number, only 3 items.
21323 * Note that the column is passed on as-is, the caller may want to decrement
21324 * it to use 1 for the first column.
21325 * Return FAIL when conversion is not possible, doesn't check the position for
21326 * validity.
21327 */
21328 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021329list2fpos(
21330 typval_T *arg,
21331 pos_T *posp,
21332 int *fnump,
21333 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021334{
21335 list_T *l = arg->vval.v_list;
21336 long i = 0;
21337 long n;
21338
Bram Moolenaar493c1782014-05-28 14:34:46 +020021339 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21340 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021341 if (arg->v_type != VAR_LIST
21342 || l == NULL
21343 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021344 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021345 return FAIL;
21346
21347 if (fnump != NULL)
21348 {
21349 n = list_find_nr(l, i++, NULL); /* fnum */
21350 if (n < 0)
21351 return FAIL;
21352 if (n == 0)
21353 n = curbuf->b_fnum; /* current buffer */
21354 *fnump = n;
21355 }
21356
21357 n = list_find_nr(l, i++, NULL); /* lnum */
21358 if (n < 0)
21359 return FAIL;
21360 posp->lnum = n;
21361
21362 n = list_find_nr(l, i++, NULL); /* col */
21363 if (n < 0)
21364 return FAIL;
21365 posp->col = n;
21366
21367#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021368 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021369 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021370 posp->coladd = 0;
21371 else
21372 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021373#endif
21374
Bram Moolenaar493c1782014-05-28 14:34:46 +020021375 if (curswantp != NULL)
21376 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21377
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021378 return OK;
21379}
21380
21381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 * Get the length of an environment variable name.
21383 * Advance "arg" to the first character after the name.
21384 * Return 0 for error.
21385 */
21386 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021387get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388{
21389 char_u *p;
21390 int len;
21391
21392 for (p = *arg; vim_isIDc(*p); ++p)
21393 ;
21394 if (p == *arg) /* no name found */
21395 return 0;
21396
21397 len = (int)(p - *arg);
21398 *arg = p;
21399 return len;
21400}
21401
21402/*
21403 * Get the length of the name of a function or internal variable.
21404 * "arg" is advanced to the first non-white character after the name.
21405 * Return 0 if something is wrong.
21406 */
21407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021408get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409{
21410 char_u *p;
21411 int len;
21412
21413 /* Find the end of the name. */
21414 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021415 {
21416 if (*p == ':')
21417 {
21418 /* "s:" is start of "s:var", but "n:" is not and can be used in
21419 * slice "[n:]". Also "xx:" is not a namespace. */
21420 len = (int)(p - *arg);
21421 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21422 || len > 1)
21423 break;
21424 }
21425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021426 if (p == *arg) /* no name found */
21427 return 0;
21428
21429 len = (int)(p - *arg);
21430 *arg = skipwhite(p);
21431
21432 return len;
21433}
21434
21435/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021436 * Get the length of the name of a variable or function.
21437 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021439 * Return -1 if curly braces expansion failed.
21440 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021441 * If the name contains 'magic' {}'s, expand them and return the
21442 * expanded name in an allocated string via 'alias' - caller must free.
21443 */
21444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021445get_name_len(
21446 char_u **arg,
21447 char_u **alias,
21448 int evaluate,
21449 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450{
21451 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 char_u *p;
21453 char_u *expr_start;
21454 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455
21456 *alias = NULL; /* default to no alias */
21457
21458 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21459 && (*arg)[2] == (int)KE_SNR)
21460 {
21461 /* hard coded <SNR>, already translated */
21462 *arg += 3;
21463 return get_id_len(arg) + 3;
21464 }
21465 len = eval_fname_script(*arg);
21466 if (len > 0)
21467 {
21468 /* literal "<SID>", "s:" or "<SNR>" */
21469 *arg += len;
21470 }
21471
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021473 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021475 p = find_name_end(*arg, &expr_start, &expr_end,
21476 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 if (expr_start != NULL)
21478 {
21479 char_u *temp_string;
21480
21481 if (!evaluate)
21482 {
21483 len += (int)(p - *arg);
21484 *arg = skipwhite(p);
21485 return len;
21486 }
21487
21488 /*
21489 * Include any <SID> etc in the expanded string:
21490 * Thus the -len here.
21491 */
21492 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21493 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021494 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 *alias = temp_string;
21496 *arg = skipwhite(p);
21497 return (int)STRLEN(temp_string);
21498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499
21500 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021501 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 EMSG2(_(e_invexpr2), *arg);
21503
21504 return len;
21505}
21506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021507/*
21508 * Find the end of a variable or function name, taking care of magic braces.
21509 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21510 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021511 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021512 * Return a pointer to just after the name. Equal to "arg" if there is no
21513 * valid name.
21514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021515 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021516find_name_end(
21517 char_u *arg,
21518 char_u **expr_start,
21519 char_u **expr_end,
21520 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021521{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021522 int mb_nest = 0;
21523 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021525 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021527 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021529 *expr_start = NULL;
21530 *expr_end = NULL;
21531 }
21532
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021533 /* Quick check for valid starting character. */
21534 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21535 return arg;
21536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021537 for (p = arg; *p != NUL
21538 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021539 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021540 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021541 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021542 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021543 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021544 if (*p == '\'')
21545 {
21546 /* skip over 'string' to avoid counting [ and ] inside it. */
21547 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21548 ;
21549 if (*p == NUL)
21550 break;
21551 }
21552 else if (*p == '"')
21553 {
21554 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21555 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21556 if (*p == '\\' && p[1] != NUL)
21557 ++p;
21558 if (*p == NUL)
21559 break;
21560 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021561 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21562 {
21563 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021564 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021565 len = (int)(p - arg);
21566 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021567 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021568 break;
21569 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021571 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021573 if (*p == '[')
21574 ++br_nest;
21575 else if (*p == ']')
21576 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021579 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581 if (*p == '{')
21582 {
21583 mb_nest++;
21584 if (expr_start != NULL && *expr_start == NULL)
21585 *expr_start = p;
21586 }
21587 else if (*p == '}')
21588 {
21589 mb_nest--;
21590 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21591 *expr_end = p;
21592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 }
21595
21596 return p;
21597}
21598
21599/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021600 * Expands out the 'magic' {}'s in a variable/function name.
21601 * Note that this can call itself recursively, to deal with
21602 * constructs like foo{bar}{baz}{bam}
21603 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21604 * "in_start" ^
21605 * "expr_start" ^
21606 * "expr_end" ^
21607 * "in_end" ^
21608 *
21609 * Returns a new allocated string, which the caller must free.
21610 * Returns NULL for failure.
21611 */
21612 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021613make_expanded_name(
21614 char_u *in_start,
21615 char_u *expr_start,
21616 char_u *expr_end,
21617 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021618{
21619 char_u c1;
21620 char_u *retval = NULL;
21621 char_u *temp_result;
21622 char_u *nextcmd = NULL;
21623
21624 if (expr_end == NULL || in_end == NULL)
21625 return NULL;
21626 *expr_start = NUL;
21627 *expr_end = NUL;
21628 c1 = *in_end;
21629 *in_end = NUL;
21630
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021631 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021632 if (temp_result != NULL && nextcmd == NULL)
21633 {
21634 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21635 + (in_end - expr_end) + 1));
21636 if (retval != NULL)
21637 {
21638 STRCPY(retval, in_start);
21639 STRCAT(retval, temp_result);
21640 STRCAT(retval, expr_end + 1);
21641 }
21642 }
21643 vim_free(temp_result);
21644
21645 *in_end = c1; /* put char back for error messages */
21646 *expr_start = '{';
21647 *expr_end = '}';
21648
21649 if (retval != NULL)
21650 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021651 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021652 if (expr_start != NULL)
21653 {
21654 /* Further expansion! */
21655 temp_result = make_expanded_name(retval, expr_start,
21656 expr_end, temp_result);
21657 vim_free(retval);
21658 retval = temp_result;
21659 }
21660 }
21661
21662 return retval;
21663}
21664
21665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021666 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021667 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668 */
21669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021670eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021672 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21673}
21674
21675/*
21676 * Return TRUE if character "c" can be used as the first character in a
21677 * variable or function name (excluding '{' and '}').
21678 */
21679 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021680eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021681{
21682 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683}
21684
21685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686 * Set number v: variable to "val".
21687 */
21688 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021689set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021691 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021692}
21693
21694/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021695 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696 */
21697 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021698get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021700 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701}
21702
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021703/*
21704 * Get string v: variable value. Uses a static buffer, can only be used once.
21705 */
21706 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021707get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021708{
21709 return get_tv_string(&vimvars[idx].vv_tv);
21710}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021711
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021713 * Get List v: variable value. Caller must take care of reference count when
21714 * needed.
21715 */
21716 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021717get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021718{
21719 return vimvars[idx].vv_list;
21720}
21721
21722/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021723 * Set v:char to character "c".
21724 */
21725 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021726set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021727{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021728 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021729
21730#ifdef FEAT_MBYTE
21731 if (has_mbyte)
21732 buf[(*mb_char2bytes)(c, buf)] = NUL;
21733 else
21734#endif
21735 {
21736 buf[0] = c;
21737 buf[1] = NUL;
21738 }
21739 set_vim_var_string(VV_CHAR, buf, -1);
21740}
21741
21742/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021743 * Set v:count to "count" and v:count1 to "count1".
21744 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 */
21746 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021747set_vcount(
21748 long count,
21749 long count1,
21750 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021752 if (set_prevcount)
21753 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021754 vimvars[VV_COUNT].vv_nr = count;
21755 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756}
21757
21758/*
21759 * Set string v: variable to a copy of "val".
21760 */
21761 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021762set_vim_var_string(
21763 int idx,
21764 char_u *val,
21765 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766{
Bram Moolenaara542c682016-01-31 16:28:04 +010021767 clear_tv(&vimvars[idx].vv_di.di_tv);
21768 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021770 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021772 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021774 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775}
21776
21777/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021778 * Set List v: variable to "val".
21779 */
21780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021781set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021782{
Bram Moolenaara542c682016-01-31 16:28:04 +010021783 clear_tv(&vimvars[idx].vv_di.di_tv);
21784 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021785 vimvars[idx].vv_list = val;
21786 if (val != NULL)
21787 ++val->lv_refcount;
21788}
21789
21790/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021791 * Set Dictionary v: variable to "val".
21792 */
21793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021794set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021795{
21796 int todo;
21797 hashitem_T *hi;
21798
Bram Moolenaara542c682016-01-31 16:28:04 +010021799 clear_tv(&vimvars[idx].vv_di.di_tv);
21800 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021801 vimvars[idx].vv_dict = val;
21802 if (val != NULL)
21803 {
21804 ++val->dv_refcount;
21805
21806 /* Set readonly */
21807 todo = (int)val->dv_hashtab.ht_used;
21808 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21809 {
21810 if (HASHITEM_EMPTY(hi))
21811 continue;
21812 --todo;
21813 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21814 }
21815 }
21816}
21817
21818/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 * Set v:register if needed.
21820 */
21821 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021822set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823{
21824 char_u regname;
21825
21826 if (c == 0 || c == ' ')
21827 regname = '"';
21828 else
21829 regname = c;
21830 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021831 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 set_vim_var_string(VV_REG, &regname, 1);
21833}
21834
21835/*
21836 * Get or set v:exception. If "oldval" == NULL, return the current value.
21837 * Otherwise, restore the value to "oldval" and return NULL.
21838 * Must always be called in pairs to save and restore v:exception! Does not
21839 * take care of memory allocations.
21840 */
21841 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021842v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021843{
21844 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021845 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846
Bram Moolenaare9a41262005-01-15 22:18:47 +000021847 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848 return NULL;
21849}
21850
21851/*
21852 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21853 * Otherwise, restore the value to "oldval" and return NULL.
21854 * Must always be called in pairs to save and restore v:throwpoint! Does not
21855 * take care of memory allocations.
21856 */
21857 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021858v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859{
21860 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021861 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862
Bram Moolenaare9a41262005-01-15 22:18:47 +000021863 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 return NULL;
21865}
21866
21867#if defined(FEAT_AUTOCMD) || defined(PROTO)
21868/*
21869 * Set v:cmdarg.
21870 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21871 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21872 * Must always be called in pairs!
21873 */
21874 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021875set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021876{
21877 char_u *oldval;
21878 char_u *newval;
21879 unsigned len;
21880
Bram Moolenaare9a41262005-01-15 22:18:47 +000021881 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021882 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021884 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021885 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021886 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 }
21888
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021889 if (eap->force_bin == FORCE_BIN)
21890 len = 6;
21891 else if (eap->force_bin == FORCE_NOBIN)
21892 len = 8;
21893 else
21894 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021895
21896 if (eap->read_edit)
21897 len += 7;
21898
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021899 if (eap->force_ff != 0)
21900 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21901# ifdef FEAT_MBYTE
21902 if (eap->force_enc != 0)
21903 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021904 if (eap->bad_char != 0)
21905 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021906# endif
21907
21908 newval = alloc(len + 1);
21909 if (newval == NULL)
21910 return NULL;
21911
21912 if (eap->force_bin == FORCE_BIN)
21913 sprintf((char *)newval, " ++bin");
21914 else if (eap->force_bin == FORCE_NOBIN)
21915 sprintf((char *)newval, " ++nobin");
21916 else
21917 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021918
21919 if (eap->read_edit)
21920 STRCAT(newval, " ++edit");
21921
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021922 if (eap->force_ff != 0)
21923 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21924 eap->cmd + eap->force_ff);
21925# ifdef FEAT_MBYTE
21926 if (eap->force_enc != 0)
21927 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21928 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021929 if (eap->bad_char == BAD_KEEP)
21930 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21931 else if (eap->bad_char == BAD_DROP)
21932 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21933 else if (eap->bad_char != 0)
21934 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021935# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021936 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021937 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938}
21939#endif
21940
21941/*
21942 * Get the value of internal variable "name".
21943 * Return OK or FAIL.
21944 */
21945 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021946get_var_tv(
21947 char_u *name,
21948 int len, /* length of "name" */
21949 typval_T *rettv, /* NULL when only checking existence */
21950 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21951 int verbose, /* may give error message */
21952 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021953{
21954 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021955 typval_T *tv = NULL;
21956 typval_T atv;
21957 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959
21960 /* truncate the name, so that we can use strcmp() */
21961 cc = name[len];
21962 name[len] = NUL;
21963
21964 /*
21965 * Check for "b:changedtick".
21966 */
21967 if (STRCMP(name, "b:changedtick") == 0)
21968 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021969 atv.v_type = VAR_NUMBER;
21970 atv.vval.v_number = curbuf->b_changedtick;
21971 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 }
21973
21974 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 * Check for user-defined variables.
21976 */
21977 else
21978 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021979 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021981 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021982 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021983 if (dip != NULL)
21984 *dip = v;
21985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021986 }
21987
Bram Moolenaare9a41262005-01-15 22:18:47 +000021988 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021990 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021991 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 ret = FAIL;
21993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021994 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021995 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996
21997 name[len] = cc;
21998
21999 return ret;
22000}
22001
22002/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022003 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22004 * Also handle function call with Funcref variable: func(expr)
22005 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22006 */
22007 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022008handle_subscript(
22009 char_u **arg,
22010 typval_T *rettv,
22011 int evaluate, /* do more than finding the end */
22012 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022013{
22014 int ret = OK;
22015 dict_T *selfdict = NULL;
22016 char_u *s;
22017 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022018 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022019
22020 while (ret == OK
22021 && (**arg == '['
22022 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022023 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22024 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022025 && !vim_iswhite(*(*arg - 1)))
22026 {
22027 if (**arg == '(')
22028 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022029 partial_T *pt = NULL;
22030
Bram Moolenaard9fba312005-06-26 22:34:35 +000022031 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022032 if (evaluate)
22033 {
22034 functv = *rettv;
22035 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022036
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022037 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022038 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022039 {
22040 pt = functv.vval.v_partial;
22041 s = pt->pt_name;
22042 }
22043 else
22044 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022045 }
22046 else
22047 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022048 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022049 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022050 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022051
22052 /* Clear the funcref afterwards, so that deleting it while
22053 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022054 if (evaluate)
22055 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022056
22057 /* Stop the expression evaluation when immediately aborting on
22058 * error, or when an interrupt occurred or an exception was thrown
22059 * but not caught. */
22060 if (aborting())
22061 {
22062 if (ret == OK)
22063 clear_tv(rettv);
22064 ret = FAIL;
22065 }
22066 dict_unref(selfdict);
22067 selfdict = NULL;
22068 }
22069 else /* **arg == '[' || **arg == '.' */
22070 {
22071 dict_unref(selfdict);
22072 if (rettv->v_type == VAR_DICT)
22073 {
22074 selfdict = rettv->vval.v_dict;
22075 if (selfdict != NULL)
22076 ++selfdict->dv_refcount;
22077 }
22078 else
22079 selfdict = NULL;
22080 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22081 {
22082 clear_tv(rettv);
22083 ret = FAIL;
22084 }
22085 }
22086 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022087
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022088 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
22089 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022090 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022091 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22092 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022093 char_u *tofree = NULL;
22094 ufunc_T *fp;
22095 char_u fname_buf[FLEN_FIXED + 1];
22096 int error;
22097
22098 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022099 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022100 fp = find_func(fname);
22101 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022102
22103 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010022104 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022105 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022106 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22107
22108 if (pt != NULL)
22109 {
22110 pt->pt_refcount = 1;
22111 pt->pt_dict = selfdict;
22112 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022113 if (rettv->v_type == VAR_FUNC)
22114 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022115 /* Just a function: Take over the function name and use
22116 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022117 pt->pt_name = rettv->vval.v_string;
22118 }
22119 else
22120 {
22121 partial_T *ret_pt = rettv->vval.v_partial;
22122 int i;
22123
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022124 /* Partial: copy the function name, use selfdict and copy
22125 * args. Can't take over name or args, the partial might
22126 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022127 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022128 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022129 if (ret_pt->pt_argc > 0)
22130 {
22131 pt->pt_argv = (typval_T *)alloc(
22132 sizeof(typval_T) * ret_pt->pt_argc);
22133 if (pt->pt_argv == NULL)
22134 /* out of memory: drop the arguments */
22135 pt->pt_argc = 0;
22136 else
22137 {
22138 pt->pt_argc = ret_pt->pt_argc;
22139 for (i = 0; i < pt->pt_argc; i++)
22140 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22141 }
22142 }
22143 partial_unref(ret_pt);
22144 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022145 rettv->v_type = VAR_PARTIAL;
22146 rettv->vval.v_partial = pt;
22147 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022148 }
22149 }
22150
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022151 dict_unref(selfdict);
22152 return ret;
22153}
22154
22155/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022156 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022157 * value).
22158 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022159 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022160alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022161{
Bram Moolenaar33570922005-01-25 22:26:29 +000022162 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022163}
22164
22165/*
22166 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 * The string "s" must have been allocated, it is consumed.
22168 * Return NULL for out of memory, the variable otherwise.
22169 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022170 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022171alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172{
Bram Moolenaar33570922005-01-25 22:26:29 +000022173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022175 rettv = alloc_tv();
22176 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022178 rettv->v_type = VAR_STRING;
22179 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 }
22181 else
22182 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022183 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184}
22185
22186/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022187 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022190free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191{
22192 if (varp != NULL)
22193 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022194 switch (varp->v_type)
22195 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022196 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022197 func_unref(varp->vval.v_string);
22198 /*FALLTHROUGH*/
22199 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022200 vim_free(varp->vval.v_string);
22201 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022202 case VAR_PARTIAL:
22203 partial_unref(varp->vval.v_partial);
22204 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022205 case VAR_LIST:
22206 list_unref(varp->vval.v_list);
22207 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022208 case VAR_DICT:
22209 dict_unref(varp->vval.v_dict);
22210 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022211 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022212#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022213 job_unref(varp->vval.v_job);
22214 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022215#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022216 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022217#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022218 channel_unref(varp->vval.v_channel);
22219 break;
22220#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022221 case VAR_NUMBER:
22222 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022223 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022224 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022225 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227 vim_free(varp);
22228 }
22229}
22230
22231/*
22232 * Free the memory for a variable value and set the value to NULL or 0.
22233 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022235clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236{
22237 if (varp != NULL)
22238 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022239 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022241 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022242 func_unref(varp->vval.v_string);
22243 /*FALLTHROUGH*/
22244 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022245 vim_free(varp->vval.v_string);
22246 varp->vval.v_string = NULL;
22247 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022248 case VAR_PARTIAL:
22249 partial_unref(varp->vval.v_partial);
22250 varp->vval.v_partial = NULL;
22251 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022252 case VAR_LIST:
22253 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022254 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022255 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022256 case VAR_DICT:
22257 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022258 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022259 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022260 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022261 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022262 varp->vval.v_number = 0;
22263 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022264 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022265#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022266 varp->vval.v_float = 0.0;
22267 break;
22268#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022269 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022270#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022271 job_unref(varp->vval.v_job);
22272 varp->vval.v_job = NULL;
22273#endif
22274 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022275 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022276#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022277 channel_unref(varp->vval.v_channel);
22278 varp->vval.v_channel = NULL;
22279#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022280 case VAR_UNKNOWN:
22281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022283 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 }
22285}
22286
22287/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022288 * Set the value of a variable to NULL without freeing items.
22289 */
22290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022291init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022292{
22293 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022294 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022295}
22296
22297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298 * Get the number value of a variable.
22299 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022300 * For incompatible types, return 0.
22301 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22302 * caller of incompatible types: it sets *denote to TRUE if "denote"
22303 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022305 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022306get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022307{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022308 int error = FALSE;
22309
22310 return get_tv_number_chk(varp, &error); /* return 0L on error */
22311}
22312
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022313 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022314get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022315{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022316 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022318 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022320 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022321 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022322 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022323#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022324 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022325 break;
22326#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022327 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022328 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022329 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022330 break;
22331 case VAR_STRING:
22332 if (varp->vval.v_string != NULL)
22333 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022334 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022335 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022336 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022337 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022338 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022339 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022340 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022341 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022342 case VAR_SPECIAL:
22343 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22344 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022345 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022346#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022347 EMSG(_("E910: Using a Job as a Number"));
22348 break;
22349#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022350 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022351#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022352 EMSG(_("E913: Using a Channel as a Number"));
22353 break;
22354#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022355 case VAR_UNKNOWN:
22356 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022357 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022359 if (denote == NULL) /* useful for values that must be unsigned */
22360 n = -1;
22361 else
22362 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022363 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022364}
22365
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022366#ifdef FEAT_FLOAT
22367 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022368get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022369{
22370 switch (varp->v_type)
22371 {
22372 case VAR_NUMBER:
22373 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022374 case VAR_FLOAT:
22375 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022376 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022377 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022378 EMSG(_("E891: Using a Funcref as a Float"));
22379 break;
22380 case VAR_STRING:
22381 EMSG(_("E892: Using a String as a Float"));
22382 break;
22383 case VAR_LIST:
22384 EMSG(_("E893: Using a List as a Float"));
22385 break;
22386 case VAR_DICT:
22387 EMSG(_("E894: Using a Dictionary as a Float"));
22388 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022389 case VAR_SPECIAL:
22390 EMSG(_("E907: Using a special value as a Float"));
22391 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022392 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022393# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022394 EMSG(_("E911: Using a Job as a Float"));
22395 break;
22396# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022397 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022398# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022399 EMSG(_("E914: Using a Channel as a Float"));
22400 break;
22401# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022402 case VAR_UNKNOWN:
22403 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022404 break;
22405 }
22406 return 0;
22407}
22408#endif
22409
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022411 * Get the lnum from the first argument.
22412 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022413 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 */
22415 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022416get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417{
Bram Moolenaar33570922005-01-25 22:26:29 +000022418 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419 linenr_T lnum;
22420
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022421 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 if (lnum == 0) /* no valid number, try using line() */
22423 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022424 rettv.v_type = VAR_NUMBER;
22425 f_line(argvars, &rettv);
22426 lnum = rettv.vval.v_number;
22427 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 }
22429 return lnum;
22430}
22431
22432/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022433 * Get the lnum from the first argument.
22434 * Also accepts "$", then "buf" is used.
22435 * Returns 0 on error.
22436 */
22437 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022438get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022439{
22440 if (argvars[0].v_type == VAR_STRING
22441 && argvars[0].vval.v_string != NULL
22442 && argvars[0].vval.v_string[0] == '$'
22443 && buf != NULL)
22444 return buf->b_ml.ml_line_count;
22445 return get_tv_number_chk(&argvars[0], NULL);
22446}
22447
22448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449 * Get the string value of a variable.
22450 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022451 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22452 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 * If the String variable has never been set, return an empty string.
22454 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022455 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22456 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022458 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022459get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460{
22461 static char_u mybuf[NUMBUFLEN];
22462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022463 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464}
22465
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022466 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022467get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022469 char_u *res = get_tv_string_buf_chk(varp, buf);
22470
22471 return res != NULL ? res : (char_u *)"";
22472}
22473
Bram Moolenaar7d647822014-04-05 21:28:56 +020022474/*
22475 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22476 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022477 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022478get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022479{
22480 static char_u mybuf[NUMBUFLEN];
22481
22482 return get_tv_string_buf_chk(varp, mybuf);
22483}
22484
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022485 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022486get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022487{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022488 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022490 case VAR_NUMBER:
22491 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22492 return buf;
22493 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022494 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022495 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022496 break;
22497 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022498 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022499 break;
22500 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022501 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022502 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022503 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022504#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022505 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022506 break;
22507#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022508 case VAR_STRING:
22509 if (varp->vval.v_string != NULL)
22510 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022511 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022512 case VAR_SPECIAL:
22513 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22514 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022515 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022517 {
22518 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022519 char *status;
22520
22521 if (job == NULL)
22522 return (char_u *)"no process";
22523 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022524 : job->jv_status == JOB_ENDED ? "dead"
22525 : "run";
22526# ifdef UNIX
22527 vim_snprintf((char *)buf, NUMBUFLEN,
22528 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022529# elif defined(WIN32)
22530 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022531 "process %ld %s",
22532 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022533 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022534# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022535 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022536 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22537# endif
22538 return buf;
22539 }
22540#endif
22541 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022542 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022543#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022544 {
22545 channel_T *channel = varp->vval.v_channel;
22546 char *status = channel_status(channel);
22547
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022548 if (channel == NULL)
22549 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22550 else
22551 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022552 "channel %d %s", channel->ch_id, status);
22553 return buf;
22554 }
22555#endif
22556 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022557 case VAR_UNKNOWN:
22558 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022561 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562}
22563
22564/*
22565 * Find variable "name" in the list of variables.
22566 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022567 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022568 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022569 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022571 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022572find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022575 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576
Bram Moolenaara7043832005-01-21 11:56:39 +000022577 ht = find_var_ht(name, &varname);
22578 if (htp != NULL)
22579 *htp = ht;
22580 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022582 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583}
22584
22585/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022586 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022587 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022590find_var_in_ht(
22591 hashtab_T *ht,
22592 int htname,
22593 char_u *varname,
22594 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022595{
Bram Moolenaar33570922005-01-25 22:26:29 +000022596 hashitem_T *hi;
22597
22598 if (*varname == NUL)
22599 {
22600 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022601 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022603 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022604 case 'g': return &globvars_var;
22605 case 'v': return &vimvars_var;
22606 case 'b': return &curbuf->b_bufvar;
22607 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022608#ifdef FEAT_WINDOWS
22609 case 't': return &curtab->tp_winvar;
22610#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022611 case 'l': return current_funccal == NULL
22612 ? NULL : &current_funccal->l_vars_var;
22613 case 'a': return current_funccal == NULL
22614 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022615 }
22616 return NULL;
22617 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022618
22619 hi = hash_find(ht, varname);
22620 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022621 {
22622 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022623 * worked find the variable again. Don't auto-load a script if it was
22624 * loaded already, otherwise it would be loaded every time when
22625 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022626 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022627 {
22628 /* Note: script_autoload() may make "hi" invalid. It must either
22629 * be obtained again or not used. */
22630 if (!script_autoload(varname, FALSE) || aborting())
22631 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022632 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022633 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022634 if (HASHITEM_EMPTY(hi))
22635 return NULL;
22636 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022637 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022638}
22639
22640/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022641 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022642 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022643 * Set "varname" to the start of name without ':'.
22644 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022645 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022646find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022648 hashitem_T *hi;
22649
Bram Moolenaar73627d02015-08-11 15:46:09 +020022650 if (name[0] == NUL)
22651 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 if (name[1] != ':')
22653 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022654 /* The name must not start with a colon or #. */
22655 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 return NULL;
22657 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022658
22659 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022660 hi = hash_find(&compat_hashtab, name);
22661 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022662 return &compat_hashtab;
22663
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022665 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022666 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 }
22668 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022669 if (*name == 'g') /* global variable */
22670 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022671 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22672 */
22673 if (vim_strchr(name + 2, ':') != NULL
22674 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022675 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022677 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022679 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022680#ifdef FEAT_WINDOWS
22681 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022682 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022683#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022684 if (*name == 'v') /* v: variable */
22685 return &vimvarht;
22686 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022687 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022689 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 if (*name == 's' /* script variable */
22691 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22692 return &SCRIPT_VARS(current_SID);
22693 return NULL;
22694}
22695
22696/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022697 * Get function call environment based on bactrace debug level
22698 */
22699 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022700get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022701{
22702 int i;
22703 funccall_T *funccal;
22704 funccall_T *temp_funccal;
22705
22706 funccal = current_funccal;
22707 if (debug_backtrace_level > 0)
22708 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022709 for (i = 0; i < debug_backtrace_level; i++)
22710 {
22711 temp_funccal = funccal->caller;
22712 if (temp_funccal)
22713 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022714 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022715 /* backtrace level overflow. reset to max */
22716 debug_backtrace_level = i;
22717 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022718 }
22719 return funccal;
22720}
22721
22722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022724 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022725 * Returns NULL when it doesn't exist.
22726 */
22727 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022728get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729{
Bram Moolenaar33570922005-01-25 22:26:29 +000022730 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022732 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022733 if (v == NULL)
22734 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022735 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736}
22737
22738/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 * sourcing this script and when executing functions defined in the script.
22741 */
22742 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022743new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744{
Bram Moolenaara7043832005-01-21 11:56:39 +000022745 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022746 hashtab_T *ht;
22747 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022748
Bram Moolenaar071d4272004-06-13 20:20:40 +000022749 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22750 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022751 /* Re-allocating ga_data means that an ht_array pointing to
22752 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022753 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022754 for (i = 1; i <= ga_scripts.ga_len; ++i)
22755 {
22756 ht = &SCRIPT_VARS(i);
22757 if (ht->ht_mask == HT_INIT_SIZE - 1)
22758 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022759 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022760 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022761 }
22762
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 while (ga_scripts.ga_len < id)
22764 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022765 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022766 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022767 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 }
22770 }
22771}
22772
22773/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022774 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22775 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 */
22777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022778init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779{
Bram Moolenaar33570922005-01-25 22:26:29 +000022780 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022781 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022782 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022783 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022784 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022785 dict_var->di_tv.vval.v_dict = dict;
22786 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022787 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022788 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22789 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790}
22791
22792/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022793 * Unreference a dictionary initialized by init_var_dict().
22794 */
22795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022796unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022797{
22798 /* Now the dict needs to be freed if no one else is using it, go back to
22799 * normal reference counting. */
22800 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22801 dict_unref(dict);
22802}
22803
22804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022806 * Frees all allocated variables and the value they contain.
22807 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 */
22809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022810vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022811{
22812 vars_clear_ext(ht, TRUE);
22813}
22814
22815/*
22816 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22817 */
22818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022819vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820{
Bram Moolenaara7043832005-01-21 11:56:39 +000022821 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022822 hashitem_T *hi;
22823 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824
Bram Moolenaar33570922005-01-25 22:26:29 +000022825 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022826 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022827 for (hi = ht->ht_array; todo > 0; ++hi)
22828 {
22829 if (!HASHITEM_EMPTY(hi))
22830 {
22831 --todo;
22832
Bram Moolenaar33570922005-01-25 22:26:29 +000022833 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022834 * ht_array might change then. hash_clear() takes care of it
22835 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022836 v = HI2DI(hi);
22837 if (free_val)
22838 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022839 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022840 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022841 }
22842 }
22843 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022844 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845}
22846
Bram Moolenaara7043832005-01-21 11:56:39 +000022847/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022848 * Delete a variable from hashtab "ht" at item "hi".
22849 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022852delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853{
Bram Moolenaar33570922005-01-25 22:26:29 +000022854 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022855
22856 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022857 clear_tv(&di->di_tv);
22858 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859}
22860
22861/*
22862 * List the value of one internal variable.
22863 */
22864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022865list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022867 char_u *tofree;
22868 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022869 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022870
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022871 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022872 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022873 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022874 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875}
22876
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022878list_one_var_a(
22879 char_u *prefix,
22880 char_u *name,
22881 int type,
22882 char_u *string,
22883 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884{
Bram Moolenaar31859182007-08-14 20:41:13 +000022885 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22886 msg_start();
22887 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 if (name != NULL) /* "a:" vars don't have a name stored */
22889 msg_puts(name);
22890 msg_putchar(' ');
22891 msg_advance(22);
22892 if (type == VAR_NUMBER)
22893 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022894 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022895 msg_putchar('*');
22896 else if (type == VAR_LIST)
22897 {
22898 msg_putchar('[');
22899 if (*string == '[')
22900 ++string;
22901 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022902 else if (type == VAR_DICT)
22903 {
22904 msg_putchar('{');
22905 if (*string == '{')
22906 ++string;
22907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 else
22909 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022910
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022912
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022913 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022914 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022915 if (*first)
22916 {
22917 msg_clr_eos();
22918 *first = FALSE;
22919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022920}
22921
22922/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022923 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924 * If the variable already exists, the value is updated.
22925 * Otherwise the variable is created.
22926 */
22927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022928set_var(
22929 char_u *name,
22930 typval_T *tv,
22931 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932{
Bram Moolenaar33570922005-01-25 22:26:29 +000022933 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022935 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022937 ht = find_var_ht(name, &varname);
22938 if (ht == NULL || *varname == NUL)
22939 {
22940 EMSG2(_(e_illvar), name);
22941 return;
22942 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022943 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022944
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022945 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22946 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022947 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022948
Bram Moolenaar33570922005-01-25 22:26:29 +000022949 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022951 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022952 if (var_check_ro(v->di_flags, name, FALSE)
22953 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022954 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022955
22956 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022957 * Handle setting internal v: variables separately where needed to
22958 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022959 */
22960 if (ht == &vimvarht)
22961 {
22962 if (v->di_tv.v_type == VAR_STRING)
22963 {
22964 vim_free(v->di_tv.vval.v_string);
22965 if (copy || tv->v_type != VAR_STRING)
22966 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22967 else
22968 {
22969 /* Take over the string to avoid an extra alloc/free. */
22970 v->di_tv.vval.v_string = tv->vval.v_string;
22971 tv->vval.v_string = NULL;
22972 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022973 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022974 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022975 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022976 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022977 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022978 if (STRCMP(varname, "searchforward") == 0)
22979 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022980#ifdef FEAT_SEARCH_EXTRA
22981 else if (STRCMP(varname, "hlsearch") == 0)
22982 {
22983 no_hlsearch = !v->di_tv.vval.v_number;
22984 redraw_all_later(SOME_VALID);
22985 }
22986#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022987 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022988 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022989 else if (v->di_tv.v_type != tv->v_type)
22990 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022991 }
22992
22993 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994 }
22995 else /* add a new variable */
22996 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022997 /* Can't add "v:" variable. */
22998 if (ht == &vimvarht)
22999 {
23000 EMSG2(_(e_illvar), name);
23001 return;
23002 }
23003
Bram Moolenaar92124a32005-06-17 22:03:40 +000023004 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023005 if (!valid_varname(varname))
23006 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023007
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023008 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23009 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023010 if (v == NULL)
23011 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023012 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023013 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023014 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023015 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 return;
23017 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023018 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023020
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023021 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023022 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023023 else
23024 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023026 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023027 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029}
23030
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023031/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023032 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023033 * Also give an error message.
23034 */
23035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023036var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023037{
23038 if (flags & DI_FLAGS_RO)
23039 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023040 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023041 return TRUE;
23042 }
23043 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23044 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023045 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023046 return TRUE;
23047 }
23048 return FALSE;
23049}
23050
23051/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023052 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23053 * Also give an error message.
23054 */
23055 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023056var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023057{
23058 if (flags & DI_FLAGS_FIX)
23059 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023060 EMSG2(_("E795: Cannot delete variable %s"),
23061 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023062 return TRUE;
23063 }
23064 return FALSE;
23065}
23066
23067/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023068 * Check if a funcref is assigned to a valid variable name.
23069 * Return TRUE and give an error if not.
23070 */
23071 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023072var_check_func_name(
23073 char_u *name, /* points to start of variable name */
23074 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023075{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023076 /* Allow for w: b: s: and t:. */
23077 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023078 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23079 ? name[2] : name[0]))
23080 {
23081 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23082 name);
23083 return TRUE;
23084 }
23085 /* Don't allow hiding a function. When "v" is not NULL we might be
23086 * assigning another function to the same var, the type is checked
23087 * below. */
23088 if (new_var && function_exists(name))
23089 {
23090 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23091 name);
23092 return TRUE;
23093 }
23094 return FALSE;
23095}
23096
23097/*
23098 * Check if a variable name is valid.
23099 * Return FALSE and give an error if not.
23100 */
23101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023102valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023103{
23104 char_u *p;
23105
23106 for (p = varname; *p != NUL; ++p)
23107 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23108 && *p != AUTOLOAD_CHAR)
23109 {
23110 EMSG2(_(e_illvar), varname);
23111 return FALSE;
23112 }
23113 return TRUE;
23114}
23115
23116/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023117 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023118 * Also give an error message, using "name" or _("name") when use_gettext is
23119 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023120 */
23121 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023122tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023123{
23124 if (lock & VAR_LOCKED)
23125 {
23126 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023127 name == NULL ? (char_u *)_("Unknown")
23128 : use_gettext ? (char_u *)_(name)
23129 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023130 return TRUE;
23131 }
23132 if (lock & VAR_FIXED)
23133 {
23134 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023135 name == NULL ? (char_u *)_("Unknown")
23136 : use_gettext ? (char_u *)_(name)
23137 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023138 return TRUE;
23139 }
23140 return FALSE;
23141}
23142
23143/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023144 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023145 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023146 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023147 * It is OK for "from" and "to" to point to the same item. This is used to
23148 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023149 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023151copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023153 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023154 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023155 switch (from->v_type)
23156 {
23157 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023158 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023159 to->vval.v_number = from->vval.v_number;
23160 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023161 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023162#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023163 to->vval.v_float = from->vval.v_float;
23164 break;
23165#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023166 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023167#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023168 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023169 if (to->vval.v_job != NULL)
23170 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023171 break;
23172#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023173 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023174#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023175 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023176 if (to->vval.v_channel != NULL)
23177 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023178 break;
23179#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023180 case VAR_STRING:
23181 case VAR_FUNC:
23182 if (from->vval.v_string == NULL)
23183 to->vval.v_string = NULL;
23184 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023185 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023186 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023187 if (from->v_type == VAR_FUNC)
23188 func_ref(to->vval.v_string);
23189 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023190 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023191 case VAR_PARTIAL:
23192 if (from->vval.v_partial == NULL)
23193 to->vval.v_partial = NULL;
23194 else
23195 {
23196 to->vval.v_partial = from->vval.v_partial;
23197 ++to->vval.v_partial->pt_refcount;
23198 }
23199 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023200 case VAR_LIST:
23201 if (from->vval.v_list == NULL)
23202 to->vval.v_list = NULL;
23203 else
23204 {
23205 to->vval.v_list = from->vval.v_list;
23206 ++to->vval.v_list->lv_refcount;
23207 }
23208 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023209 case VAR_DICT:
23210 if (from->vval.v_dict == NULL)
23211 to->vval.v_dict = NULL;
23212 else
23213 {
23214 to->vval.v_dict = from->vval.v_dict;
23215 ++to->vval.v_dict->dv_refcount;
23216 }
23217 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023218 case VAR_UNKNOWN:
23219 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023220 break;
23221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222}
23223
23224/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023225 * Make a copy of an item.
23226 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023227 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23228 * reference to an already copied list/dict can be used.
23229 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023230 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023231 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023232item_copy(
23233 typval_T *from,
23234 typval_T *to,
23235 int deep,
23236 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023237{
23238 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023239 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023240
Bram Moolenaar33570922005-01-25 22:26:29 +000023241 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023242 {
23243 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023244 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023245 }
23246 ++recurse;
23247
23248 switch (from->v_type)
23249 {
23250 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023251 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023252 case VAR_STRING:
23253 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023254 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023255 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023256 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023257 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023258 copy_tv(from, to);
23259 break;
23260 case VAR_LIST:
23261 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023262 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023263 if (from->vval.v_list == NULL)
23264 to->vval.v_list = NULL;
23265 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23266 {
23267 /* use the copy made earlier */
23268 to->vval.v_list = from->vval.v_list->lv_copylist;
23269 ++to->vval.v_list->lv_refcount;
23270 }
23271 else
23272 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23273 if (to->vval.v_list == NULL)
23274 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023275 break;
23276 case VAR_DICT:
23277 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023278 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023279 if (from->vval.v_dict == NULL)
23280 to->vval.v_dict = NULL;
23281 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23282 {
23283 /* use the copy made earlier */
23284 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23285 ++to->vval.v_dict->dv_refcount;
23286 }
23287 else
23288 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23289 if (to->vval.v_dict == NULL)
23290 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023291 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023292 case VAR_UNKNOWN:
23293 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023294 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023295 }
23296 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023297 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023298}
23299
23300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 * ":echo expr1 ..." print each argument separated with a space, add a
23302 * newline at the end.
23303 * ":echon expr1 ..." print each argument plain.
23304 */
23305 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023306ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307{
23308 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023309 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023310 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 char_u *p;
23312 int needclr = TRUE;
23313 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023314 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315
23316 if (eap->skip)
23317 ++emsg_skip;
23318 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23319 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023320 /* If eval1() causes an error message the text from the command may
23321 * still need to be cleared. E.g., "echo 22,44". */
23322 need_clr_eos = needclr;
23323
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023325 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023326 {
23327 /*
23328 * Report the invalid expression unless the expression evaluation
23329 * has been cancelled due to an aborting error, an interrupt, or an
23330 * exception.
23331 */
23332 if (!aborting())
23333 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023334 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335 break;
23336 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023337 need_clr_eos = FALSE;
23338
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339 if (!eap->skip)
23340 {
23341 if (atstart)
23342 {
23343 atstart = FALSE;
23344 /* Call msg_start() after eval1(), evaluating the expression
23345 * may cause a message to appear. */
23346 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023347 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023348 /* Mark the saved text as finishing the line, so that what
23349 * follows is displayed on a new line when scrolling back
23350 * at the more prompt. */
23351 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 }
23355 else if (eap->cmdidx == CMD_echo)
23356 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023357 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023358 if (p != NULL)
23359 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023361 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023363 if (*p != TAB && needclr)
23364 {
23365 /* remove any text still there from the command */
23366 msg_clr_eos();
23367 needclr = FALSE;
23368 }
23369 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 }
23371 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023372 {
23373#ifdef FEAT_MBYTE
23374 if (has_mbyte)
23375 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023376 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023377
23378 (void)msg_outtrans_len_attr(p, i, echo_attr);
23379 p += i - 1;
23380 }
23381 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023383 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023385 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023386 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023388 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389 arg = skipwhite(arg);
23390 }
23391 eap->nextcmd = check_nextcmd(arg);
23392
23393 if (eap->skip)
23394 --emsg_skip;
23395 else
23396 {
23397 /* remove text that may still be there from the command */
23398 if (needclr)
23399 msg_clr_eos();
23400 if (eap->cmdidx == CMD_echo)
23401 msg_end();
23402 }
23403}
23404
23405/*
23406 * ":echohl {name}".
23407 */
23408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023409ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410{
23411 int id;
23412
23413 id = syn_name2id(eap->arg);
23414 if (id == 0)
23415 echo_attr = 0;
23416 else
23417 echo_attr = syn_id2attr(id);
23418}
23419
23420/*
23421 * ":execute expr1 ..." execute the result of an expression.
23422 * ":echomsg expr1 ..." Print a message
23423 * ":echoerr expr1 ..." Print an error
23424 * Each gets spaces around each argument and a newline at the end for
23425 * echo commands
23426 */
23427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023428ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429{
23430 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023431 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 int ret = OK;
23433 char_u *p;
23434 garray_T ga;
23435 int len;
23436 int save_did_emsg;
23437
23438 ga_init2(&ga, 1, 80);
23439
23440 if (eap->skip)
23441 ++emsg_skip;
23442 while (*arg != NUL && *arg != '|' && *arg != '\n')
23443 {
23444 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023445 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 {
23447 /*
23448 * Report the invalid expression unless the expression evaluation
23449 * has been cancelled due to an aborting error, an interrupt, or an
23450 * exception.
23451 */
23452 if (!aborting())
23453 EMSG2(_(e_invexpr2), p);
23454 ret = FAIL;
23455 break;
23456 }
23457
23458 if (!eap->skip)
23459 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023460 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 len = (int)STRLEN(p);
23462 if (ga_grow(&ga, len + 2) == FAIL)
23463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023464 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465 ret = FAIL;
23466 break;
23467 }
23468 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023470 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 ga.ga_len += len;
23472 }
23473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023474 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 arg = skipwhite(arg);
23476 }
23477
23478 if (ret != FAIL && ga.ga_data != NULL)
23479 {
23480 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023483 out_flush();
23484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 else if (eap->cmdidx == CMD_echoerr)
23486 {
23487 /* We don't want to abort following commands, restore did_emsg. */
23488 save_did_emsg = did_emsg;
23489 EMSG((char_u *)ga.ga_data);
23490 if (!force_abort)
23491 did_emsg = save_did_emsg;
23492 }
23493 else if (eap->cmdidx == CMD_execute)
23494 do_cmdline((char_u *)ga.ga_data,
23495 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23496 }
23497
23498 ga_clear(&ga);
23499
23500 if (eap->skip)
23501 --emsg_skip;
23502
23503 eap->nextcmd = check_nextcmd(arg);
23504}
23505
23506/*
23507 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23508 * "arg" points to the "&" or '+' when called, to "option" when returning.
23509 * Returns NULL when no option name found. Otherwise pointer to the char
23510 * after the option name.
23511 */
23512 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023513find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514{
23515 char_u *p = *arg;
23516
23517 ++p;
23518 if (*p == 'g' && p[1] == ':')
23519 {
23520 *opt_flags = OPT_GLOBAL;
23521 p += 2;
23522 }
23523 else if (*p == 'l' && p[1] == ':')
23524 {
23525 *opt_flags = OPT_LOCAL;
23526 p += 2;
23527 }
23528 else
23529 *opt_flags = 0;
23530
23531 if (!ASCII_ISALPHA(*p))
23532 return NULL;
23533 *arg = p;
23534
23535 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23536 p += 4; /* termcap option */
23537 else
23538 while (ASCII_ISALPHA(*p))
23539 ++p;
23540 return p;
23541}
23542
23543/*
23544 * ":function"
23545 */
23546 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023547ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548{
23549 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023550 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023551 int j;
23552 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023553 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023554 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 char_u *name = NULL;
23556 char_u *p;
23557 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023558 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 garray_T newargs;
23560 garray_T newlines;
23561 int varargs = FALSE;
23562 int mustend = FALSE;
23563 int flags = 0;
23564 ufunc_T *fp;
23565 int indent;
23566 int nesting;
23567 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023568 dictitem_T *v;
23569 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023570 static int func_nr = 0; /* number for nameless function */
23571 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023572 hashtab_T *ht;
23573 int todo;
23574 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023575 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576
23577 /*
23578 * ":function" without argument: list functions.
23579 */
23580 if (ends_excmd(*eap->arg))
23581 {
23582 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023583 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023584 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023585 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023586 {
23587 if (!HASHITEM_EMPTY(hi))
23588 {
23589 --todo;
23590 fp = HI2UF(hi);
23591 if (!isdigit(*fp->uf_name))
23592 list_func_head(fp, FALSE);
23593 }
23594 }
23595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 eap->nextcmd = check_nextcmd(eap->arg);
23597 return;
23598 }
23599
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023600 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023601 * ":function /pat": list functions matching pattern.
23602 */
23603 if (*eap->arg == '/')
23604 {
23605 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23606 if (!eap->skip)
23607 {
23608 regmatch_T regmatch;
23609
23610 c = *p;
23611 *p = NUL;
23612 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23613 *p = c;
23614 if (regmatch.regprog != NULL)
23615 {
23616 regmatch.rm_ic = p_ic;
23617
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023618 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023619 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23620 {
23621 if (!HASHITEM_EMPTY(hi))
23622 {
23623 --todo;
23624 fp = HI2UF(hi);
23625 if (!isdigit(*fp->uf_name)
23626 && vim_regexec(&regmatch, fp->uf_name, 0))
23627 list_func_head(fp, FALSE);
23628 }
23629 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023630 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023631 }
23632 }
23633 if (*p == '/')
23634 ++p;
23635 eap->nextcmd = check_nextcmd(p);
23636 return;
23637 }
23638
23639 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023640 * Get the function name. There are these situations:
23641 * func normal function name
23642 * "name" == func, "fudi.fd_dict" == NULL
23643 * dict.func new dictionary entry
23644 * "name" == NULL, "fudi.fd_dict" set,
23645 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23646 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023647 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023648 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23649 * dict.func existing dict entry that's not a Funcref
23650 * "name" == NULL, "fudi.fd_dict" set,
23651 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023652 * s:func script-local function name
23653 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023656 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023657 paren = (vim_strchr(p, '(') != NULL);
23658 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 {
23660 /*
23661 * Return on an invalid expression in braces, unless the expression
23662 * evaluation has been cancelled due to an aborting error, an
23663 * interrupt, or an exception.
23664 */
23665 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023667 if (!eap->skip && fudi.fd_newkey != NULL)
23668 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023669 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 else
23673 eap->skip = TRUE;
23674 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023675
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 /* An error in a function call during evaluation of an expression in magic
23677 * braces should not cause the function not to be defined. */
23678 saved_did_emsg = did_emsg;
23679 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680
23681 /*
23682 * ":function func" with only function name: list function.
23683 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023684 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 {
23686 if (!ends_excmd(*skipwhite(p)))
23687 {
23688 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023689 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023690 }
23691 eap->nextcmd = check_nextcmd(p);
23692 if (eap->nextcmd != NULL)
23693 *p = NUL;
23694 if (!eap->skip && !got_int)
23695 {
23696 fp = find_func(name);
23697 if (fp != NULL)
23698 {
23699 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023700 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023702 if (FUNCLINE(fp, j) == NULL)
23703 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 msg_putchar('\n');
23705 msg_outnum((long)(j + 1));
23706 if (j < 9)
23707 msg_putchar(' ');
23708 if (j < 99)
23709 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023710 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023711 out_flush(); /* show a line at a time */
23712 ui_breakcheck();
23713 }
23714 if (!got_int)
23715 {
23716 msg_putchar('\n');
23717 msg_puts((char_u *)" endfunction");
23718 }
23719 }
23720 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023721 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023723 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023724 }
23725
23726 /*
23727 * ":function name(arg1, arg2)" Define function.
23728 */
23729 p = skipwhite(p);
23730 if (*p != '(')
23731 {
23732 if (!eap->skip)
23733 {
23734 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023735 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 }
23737 /* attempt to continue by skipping some text */
23738 if (vim_strchr(p, '(') != NULL)
23739 p = vim_strchr(p, '(');
23740 }
23741 p = skipwhite(p + 1);
23742
23743 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23744 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23745
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023746 if (!eap->skip)
23747 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023748 /* Check the name of the function. Unless it's a dictionary function
23749 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023750 if (name != NULL)
23751 arg = name;
23752 else
23753 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023754 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023755 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23756 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023757 {
23758 if (*arg == K_SPECIAL)
23759 j = 3;
23760 else
23761 j = 0;
23762 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23763 : eval_isnamec(arg[j])))
23764 ++j;
23765 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023766 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023767 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023768 /* Disallow using the g: dict. */
23769 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23770 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023771 }
23772
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 /*
23774 * Isolate the arguments: "arg1, arg2, ...)"
23775 */
23776 while (*p != ')')
23777 {
23778 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23779 {
23780 varargs = TRUE;
23781 p += 3;
23782 mustend = TRUE;
23783 }
23784 else
23785 {
23786 arg = p;
23787 while (ASCII_ISALNUM(*p) || *p == '_')
23788 ++p;
23789 if (arg == p || isdigit(*arg)
23790 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23791 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23792 {
23793 if (!eap->skip)
23794 EMSG2(_("E125: Illegal argument: %s"), arg);
23795 break;
23796 }
23797 if (ga_grow(&newargs, 1) == FAIL)
23798 goto erret;
23799 c = *p;
23800 *p = NUL;
23801 arg = vim_strsave(arg);
23802 if (arg == NULL)
23803 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023804
23805 /* Check for duplicate argument name. */
23806 for (i = 0; i < newargs.ga_len; ++i)
23807 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23808 {
23809 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023810 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023811 goto erret;
23812 }
23813
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23815 *p = c;
23816 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 if (*p == ',')
23818 ++p;
23819 else
23820 mustend = TRUE;
23821 }
23822 p = skipwhite(p);
23823 if (mustend && *p != ')')
23824 {
23825 if (!eap->skip)
23826 EMSG2(_(e_invarg2), eap->arg);
23827 break;
23828 }
23829 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023830 if (*p != ')')
23831 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832 ++p; /* skip the ')' */
23833
Bram Moolenaare9a41262005-01-15 22:18:47 +000023834 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 for (;;)
23836 {
23837 p = skipwhite(p);
23838 if (STRNCMP(p, "range", 5) == 0)
23839 {
23840 flags |= FC_RANGE;
23841 p += 5;
23842 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023843 else if (STRNCMP(p, "dict", 4) == 0)
23844 {
23845 flags |= FC_DICT;
23846 p += 4;
23847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023848 else if (STRNCMP(p, "abort", 5) == 0)
23849 {
23850 flags |= FC_ABORT;
23851 p += 5;
23852 }
23853 else
23854 break;
23855 }
23856
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023857 /* When there is a line break use what follows for the function body.
23858 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23859 if (*p == '\n')
23860 line_arg = p + 1;
23861 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 EMSG(_(e_trailing));
23863
23864 /*
23865 * Read the body of the function, until ":endfunction" is found.
23866 */
23867 if (KeyTyped)
23868 {
23869 /* Check if the function already exists, don't let the user type the
23870 * whole function before telling him it doesn't work! For a script we
23871 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023872 if (!eap->skip && !eap->forceit)
23873 {
23874 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23875 EMSG(_(e_funcdict));
23876 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023877 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023880 if (!eap->skip && did_emsg)
23881 goto erret;
23882
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 msg_putchar('\n'); /* don't overwrite the function name */
23884 cmdline_row = msg_row;
23885 }
23886
23887 indent = 2;
23888 nesting = 0;
23889 for (;;)
23890 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023891 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023892 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023893 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023894 saved_wait_return = FALSE;
23895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023897 sourcing_lnum_off = sourcing_lnum;
23898
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023899 if (line_arg != NULL)
23900 {
23901 /* Use eap->arg, split up in parts by line breaks. */
23902 theline = line_arg;
23903 p = vim_strchr(theline, '\n');
23904 if (p == NULL)
23905 line_arg += STRLEN(line_arg);
23906 else
23907 {
23908 *p = NUL;
23909 line_arg = p + 1;
23910 }
23911 }
23912 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913 theline = getcmdline(':', 0L, indent);
23914 else
23915 theline = eap->getline(':', eap->cookie, indent);
23916 if (KeyTyped)
23917 lines_left = Rows - 1;
23918 if (theline == NULL)
23919 {
23920 EMSG(_("E126: Missing :endfunction"));
23921 goto erret;
23922 }
23923
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023924 /* Detect line continuation: sourcing_lnum increased more than one. */
23925 if (sourcing_lnum > sourcing_lnum_off + 1)
23926 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23927 else
23928 sourcing_lnum_off = 0;
23929
Bram Moolenaar071d4272004-06-13 20:20:40 +000023930 if (skip_until != NULL)
23931 {
23932 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23933 * don't check for ":endfunc". */
23934 if (STRCMP(theline, skip_until) == 0)
23935 {
23936 vim_free(skip_until);
23937 skip_until = NULL;
23938 }
23939 }
23940 else
23941 {
23942 /* skip ':' and blanks*/
23943 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23944 ;
23945
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023946 /* Check for "endfunction". */
23947 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023949 if (line_arg == NULL)
23950 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951 break;
23952 }
23953
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023954 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 * at "end". */
23956 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23957 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023958 else if (STRNCMP(p, "if", 2) == 0
23959 || STRNCMP(p, "wh", 2) == 0
23960 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 || STRNCMP(p, "try", 3) == 0)
23962 indent += 2;
23963
23964 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023965 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023967 if (*p == '!')
23968 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023970 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023971 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023973 ++nesting;
23974 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975 }
23976 }
23977
23978 /* Check for ":append" or ":insert". */
23979 p = skip_range(p, NULL);
23980 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23981 || (p[0] == 'i'
23982 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23983 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23984 skip_until = vim_strsave((char_u *)".");
23985
23986 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23987 arg = skipwhite(skiptowhite(p));
23988 if (arg[0] == '<' && arg[1] =='<'
23989 && ((p[0] == 'p' && p[1] == 'y'
23990 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23991 || (p[0] == 'p' && p[1] == 'e'
23992 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23993 || (p[0] == 't' && p[1] == 'c'
23994 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023995 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23996 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23998 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023999 || (p[0] == 'm' && p[1] == 'z'
24000 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 ))
24002 {
24003 /* ":python <<" continues until a dot, like ":append" */
24004 p = skipwhite(arg + 2);
24005 if (*p == NUL)
24006 skip_until = vim_strsave((char_u *)".");
24007 else
24008 skip_until = vim_strsave(p);
24009 }
24010 }
24011
24012 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024013 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024014 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024015 if (line_arg == NULL)
24016 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024017 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024018 }
24019
24020 /* Copy the line to newly allocated memory. get_one_sourceline()
24021 * allocates 250 bytes per line, this saves 80% on average. The cost
24022 * is an extra alloc/free. */
24023 p = vim_strsave(theline);
24024 if (p != NULL)
24025 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024026 if (line_arg == NULL)
24027 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024028 theline = p;
24029 }
24030
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024031 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24032
24033 /* Add NULL lines for continuation lines, so that the line count is
24034 * equal to the index in the growarray. */
24035 while (sourcing_lnum_off-- > 0)
24036 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024037
24038 /* Check for end of eap->arg. */
24039 if (line_arg != NULL && *line_arg == NUL)
24040 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 }
24042
24043 /* Don't define the function when skipping commands or when an error was
24044 * detected. */
24045 if (eap->skip || did_emsg)
24046 goto erret;
24047
24048 /*
24049 * If there are no errors, add the function
24050 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024051 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024052 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024053 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024054 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024055 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024056 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024057 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024058 goto erret;
24059 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024060
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024061 fp = find_func(name);
24062 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024064 if (!eap->forceit)
24065 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024066 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024067 goto erret;
24068 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024069 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024070 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024071 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024072 name);
24073 goto erret;
24074 }
24075 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024076 ga_clear_strings(&(fp->uf_args));
24077 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024078 vim_free(name);
24079 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024081 }
24082 else
24083 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024084 char numbuf[20];
24085
24086 fp = NULL;
24087 if (fudi.fd_newkey == NULL && !eap->forceit)
24088 {
24089 EMSG(_(e_funcdict));
24090 goto erret;
24091 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024092 if (fudi.fd_di == NULL)
24093 {
24094 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024095 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024096 goto erret;
24097 }
24098 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024099 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024100 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024101
24102 /* Give the function a sequential number. Can only be used with a
24103 * Funcref! */
24104 vim_free(name);
24105 sprintf(numbuf, "%d", ++func_nr);
24106 name = vim_strsave((char_u *)numbuf);
24107 if (name == NULL)
24108 goto erret;
24109 }
24110
24111 if (fp == NULL)
24112 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024113 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024114 {
24115 int slen, plen;
24116 char_u *scriptname;
24117
24118 /* Check that the autoload name matches the script name. */
24119 j = FAIL;
24120 if (sourcing_name != NULL)
24121 {
24122 scriptname = autoload_name(name);
24123 if (scriptname != NULL)
24124 {
24125 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024126 plen = (int)STRLEN(p);
24127 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024128 if (slen > plen && fnamecmp(p,
24129 sourcing_name + slen - plen) == 0)
24130 j = OK;
24131 vim_free(scriptname);
24132 }
24133 }
24134 if (j == FAIL)
24135 {
24136 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24137 goto erret;
24138 }
24139 }
24140
24141 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142 if (fp == NULL)
24143 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024144
24145 if (fudi.fd_dict != NULL)
24146 {
24147 if (fudi.fd_di == NULL)
24148 {
24149 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024150 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024151 if (fudi.fd_di == NULL)
24152 {
24153 vim_free(fp);
24154 goto erret;
24155 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024156 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24157 {
24158 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024159 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024160 goto erret;
24161 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024162 }
24163 else
24164 /* overwrite existing dict entry */
24165 clear_tv(&fudi.fd_di->di_tv);
24166 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024167 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024168 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024169 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024170
24171 /* behave like "dict" was used */
24172 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024173 }
24174
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024176 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024177 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24178 {
24179 vim_free(fp);
24180 goto erret;
24181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024183 fp->uf_args = newargs;
24184 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024185#ifdef FEAT_PROFILE
24186 fp->uf_tml_count = NULL;
24187 fp->uf_tml_total = NULL;
24188 fp->uf_tml_self = NULL;
24189 fp->uf_profiling = FALSE;
24190 if (prof_def_func())
24191 func_do_profile(fp);
24192#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024193 fp->uf_varargs = varargs;
24194 fp->uf_flags = flags;
24195 fp->uf_calls = 0;
24196 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024197 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198
24199erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024200 ga_clear_strings(&newargs);
24201 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024202ret_free:
24203 vim_free(skip_until);
24204 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024207 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024208}
24209
24210/*
24211 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024212 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024214 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024215 * TFN_INT: internal function name OK
24216 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024217 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218 * Advances "pp" to just after the function name (if no error).
24219 */
24220 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024221trans_function_name(
24222 char_u **pp,
24223 int skip, /* only find the end, don't evaluate */
24224 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024225 funcdict_T *fdp, /* return: info about dictionary used */
24226 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024228 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 char_u *start;
24230 char_u *end;
24231 int lead;
24232 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024234 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024235
24236 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024237 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024239
24240 /* Check for hard coded <SNR>: already translated function ID (from a user
24241 * command). */
24242 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24243 && (*pp)[2] == (int)KE_SNR)
24244 {
24245 *pp += 3;
24246 len = get_id_len(pp) + 3;
24247 return vim_strnsave(start, len);
24248 }
24249
24250 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24251 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024253 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024255
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024256 /* Note that TFN_ flags use the same values as GLV_ flags. */
24257 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024258 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024259 if (end == start)
24260 {
24261 if (!skip)
24262 EMSG(_("E129: Function name required"));
24263 goto theend;
24264 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024265 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024266 {
24267 /*
24268 * Report an invalid expression in braces, unless the expression
24269 * evaluation has been cancelled due to an aborting error, an
24270 * interrupt, or an exception.
24271 */
24272 if (!aborting())
24273 {
24274 if (end != NULL)
24275 EMSG2(_(e_invarg2), start);
24276 }
24277 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024278 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024279 goto theend;
24280 }
24281
24282 if (lv.ll_tv != NULL)
24283 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024284 if (fdp != NULL)
24285 {
24286 fdp->fd_dict = lv.ll_dict;
24287 fdp->fd_newkey = lv.ll_newkey;
24288 lv.ll_newkey = NULL;
24289 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024290 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024291 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24292 {
24293 name = vim_strsave(lv.ll_tv->vval.v_string);
24294 *pp = end;
24295 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024296 else if (lv.ll_tv->v_type == VAR_PARTIAL
24297 && lv.ll_tv->vval.v_partial != NULL)
24298 {
24299 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24300 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024301 if (partial != NULL)
24302 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024303 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024304 else
24305 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024306 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24307 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024308 EMSG(_(e_funcref));
24309 else
24310 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024311 name = NULL;
24312 }
24313 goto theend;
24314 }
24315
24316 if (lv.ll_name == NULL)
24317 {
24318 /* Error found, but continue after the function name. */
24319 *pp = end;
24320 goto theend;
24321 }
24322
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024323 /* Check if the name is a Funcref. If so, use the value. */
24324 if (lv.ll_exp_name != NULL)
24325 {
24326 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024327 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024328 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024329 if (name == lv.ll_exp_name)
24330 name = NULL;
24331 }
24332 else
24333 {
24334 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024335 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024336 if (name == *pp)
24337 name = NULL;
24338 }
24339 if (name != NULL)
24340 {
24341 name = vim_strsave(name);
24342 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024343 if (STRNCMP(name, "<SNR>", 5) == 0)
24344 {
24345 /* Change "<SNR>" to the byte sequence. */
24346 name[0] = K_SPECIAL;
24347 name[1] = KS_EXTRA;
24348 name[2] = (int)KE_SNR;
24349 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24350 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024351 goto theend;
24352 }
24353
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024354 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024355 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024356 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024357 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24358 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24359 {
24360 /* When there was "s:" already or the name expanded to get a
24361 * leading "s:" then remove it. */
24362 lv.ll_name += 2;
24363 len -= 2;
24364 lead = 2;
24365 }
24366 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024367 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024368 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024369 /* skip over "s:" and "g:" */
24370 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024371 lv.ll_name += 2;
24372 len = (int)(end - lv.ll_name);
24373 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024374
24375 /*
24376 * Copy the function name to allocated memory.
24377 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24378 * Accept <SNR>123_name() outside a script.
24379 */
24380 if (skip)
24381 lead = 0; /* do nothing */
24382 else if (lead > 0)
24383 {
24384 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024385 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24386 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024387 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024388 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024389 if (current_SID <= 0)
24390 {
24391 EMSG(_(e_usingsid));
24392 goto theend;
24393 }
24394 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24395 lead += (int)STRLEN(sid_buf);
24396 }
24397 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024398 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024399 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024400 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024401 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024402 goto theend;
24403 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024404 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024405 {
24406 char_u *cp = vim_strchr(lv.ll_name, ':');
24407
24408 if (cp != NULL && cp < end)
24409 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024410 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024411 goto theend;
24412 }
24413 }
24414
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024415 name = alloc((unsigned)(len + lead + 1));
24416 if (name != NULL)
24417 {
24418 if (lead > 0)
24419 {
24420 name[0] = K_SPECIAL;
24421 name[1] = KS_EXTRA;
24422 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024423 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024424 STRCPY(name + 3, sid_buf);
24425 }
24426 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024427 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024428 }
24429 *pp = end;
24430
24431theend:
24432 clear_lval(&lv);
24433 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434}
24435
24436/*
24437 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24438 * Return 2 if "p" starts with "s:".
24439 * Return 0 otherwise.
24440 */
24441 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024442eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024444 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24445 * the standard library function. */
24446 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24447 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448 return 5;
24449 if (p[0] == 's' && p[1] == ':')
24450 return 2;
24451 return 0;
24452}
24453
24454/*
24455 * Return TRUE if "p" starts with "<SID>" or "s:".
24456 * Only works if eval_fname_script() returned non-zero for "p"!
24457 */
24458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024459eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024460{
24461 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24462}
24463
24464/*
24465 * List the head of the function: "name(arg1, arg2)".
24466 */
24467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024468list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024469{
24470 int j;
24471
24472 msg_start();
24473 if (indent)
24474 MSG_PUTS(" ");
24475 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024476 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477 {
24478 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024479 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480 }
24481 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024482 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024484 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485 {
24486 if (j)
24487 MSG_PUTS(", ");
24488 msg_puts(FUNCARG(fp, j));
24489 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024490 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024491 {
24492 if (j)
24493 MSG_PUTS(", ");
24494 MSG_PUTS("...");
24495 }
24496 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024497 if (fp->uf_flags & FC_ABORT)
24498 MSG_PUTS(" abort");
24499 if (fp->uf_flags & FC_RANGE)
24500 MSG_PUTS(" range");
24501 if (fp->uf_flags & FC_DICT)
24502 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024503 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024504 if (p_verbose > 0)
24505 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024506}
24507
24508/*
24509 * Find a function by name, return pointer to it in ufuncs.
24510 * Return NULL for unknown function.
24511 */
24512 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024513find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024514{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024515 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024517 hi = hash_find(&func_hashtab, name);
24518 if (!HASHITEM_EMPTY(hi))
24519 return HI2UF(hi);
24520 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521}
24522
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024523#if defined(EXITFREE) || defined(PROTO)
24524 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024525free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024526{
24527 hashitem_T *hi;
24528
24529 /* Need to start all over every time, because func_free() may change the
24530 * hash table. */
24531 while (func_hashtab.ht_used > 0)
24532 for (hi = func_hashtab.ht_array; ; ++hi)
24533 if (!HASHITEM_EMPTY(hi))
24534 {
24535 func_free(HI2UF(hi));
24536 break;
24537 }
24538}
24539#endif
24540
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024541 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024542translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024543{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024544 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024545 return find_internal_func(name) >= 0;
24546 return find_func(name) != NULL;
24547}
24548
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024549/*
24550 * Return TRUE if a function "name" exists.
24551 */
24552 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024553function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024554{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024555 char_u *nm = name;
24556 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024557 int n = FALSE;
24558
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024559 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024560 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024561 nm = skipwhite(nm);
24562
24563 /* Only accept "funcname", "funcname ", "funcname (..." and
24564 * "funcname(...", not "funcname!...". */
24565 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024566 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024567 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024568 return n;
24569}
24570
Bram Moolenaara1544c02013-05-30 12:35:52 +020024571 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024572get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024573{
24574 char_u *nm = name;
24575 char_u *p;
24576
Bram Moolenaar65639032016-03-16 21:40:30 +010024577 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024578
24579 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024580 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024581 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024582
Bram Moolenaara1544c02013-05-30 12:35:52 +020024583 vim_free(p);
24584 return NULL;
24585}
24586
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024587/*
24588 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024589 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24590 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024591 */
24592 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024593builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024594{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024595 char_u *p;
24596
24597 if (!ASCII_ISLOWER(name[0]))
24598 return FALSE;
24599 p = vim_strchr(name, AUTOLOAD_CHAR);
24600 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024601}
24602
Bram Moolenaar05159a02005-02-26 23:04:13 +000024603#if defined(FEAT_PROFILE) || defined(PROTO)
24604/*
24605 * Start profiling function "fp".
24606 */
24607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024608func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024609{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024610 int len = fp->uf_lines.ga_len;
24611
24612 if (len == 0)
24613 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024614 fp->uf_tm_count = 0;
24615 profile_zero(&fp->uf_tm_self);
24616 profile_zero(&fp->uf_tm_total);
24617 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024618 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024619 if (fp->uf_tml_total == NULL)
24620 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024621 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024622 if (fp->uf_tml_self == NULL)
24623 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024624 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024625 fp->uf_tml_idx = -1;
24626 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24627 || fp->uf_tml_self == NULL)
24628 return; /* out of memory */
24629
24630 fp->uf_profiling = TRUE;
24631}
24632
24633/*
24634 * Dump the profiling results for all functions in file "fd".
24635 */
24636 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024637func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024638{
24639 hashitem_T *hi;
24640 int todo;
24641 ufunc_T *fp;
24642 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024643 ufunc_T **sorttab;
24644 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024645
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024646 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024647 if (todo == 0)
24648 return; /* nothing to dump */
24649
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024650 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024651
Bram Moolenaar05159a02005-02-26 23:04:13 +000024652 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24653 {
24654 if (!HASHITEM_EMPTY(hi))
24655 {
24656 --todo;
24657 fp = HI2UF(hi);
24658 if (fp->uf_profiling)
24659 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024660 if (sorttab != NULL)
24661 sorttab[st_len++] = fp;
24662
Bram Moolenaar05159a02005-02-26 23:04:13 +000024663 if (fp->uf_name[0] == K_SPECIAL)
24664 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24665 else
24666 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24667 if (fp->uf_tm_count == 1)
24668 fprintf(fd, "Called 1 time\n");
24669 else
24670 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24671 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24672 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24673 fprintf(fd, "\n");
24674 fprintf(fd, "count total (s) self (s)\n");
24675
24676 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24677 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024678 if (FUNCLINE(fp, i) == NULL)
24679 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024680 prof_func_line(fd, fp->uf_tml_count[i],
24681 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024682 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24683 }
24684 fprintf(fd, "\n");
24685 }
24686 }
24687 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024688
24689 if (sorttab != NULL && st_len > 0)
24690 {
24691 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24692 prof_total_cmp);
24693 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24694 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24695 prof_self_cmp);
24696 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24697 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024698
24699 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024700}
Bram Moolenaar73830342005-02-28 22:48:19 +000024701
24702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024703prof_sort_list(
24704 FILE *fd,
24705 ufunc_T **sorttab,
24706 int st_len,
24707 char *title,
24708 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024709{
24710 int i;
24711 ufunc_T *fp;
24712
24713 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24714 fprintf(fd, "count total (s) self (s) function\n");
24715 for (i = 0; i < 20 && i < st_len; ++i)
24716 {
24717 fp = sorttab[i];
24718 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24719 prefer_self);
24720 if (fp->uf_name[0] == K_SPECIAL)
24721 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24722 else
24723 fprintf(fd, " %s()\n", fp->uf_name);
24724 }
24725 fprintf(fd, "\n");
24726}
24727
24728/*
24729 * Print the count and times for one function or function line.
24730 */
24731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024732prof_func_line(
24733 FILE *fd,
24734 int count,
24735 proftime_T *total,
24736 proftime_T *self,
24737 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024738{
24739 if (count > 0)
24740 {
24741 fprintf(fd, "%5d ", count);
24742 if (prefer_self && profile_equal(total, self))
24743 fprintf(fd, " ");
24744 else
24745 fprintf(fd, "%s ", profile_msg(total));
24746 if (!prefer_self && profile_equal(total, self))
24747 fprintf(fd, " ");
24748 else
24749 fprintf(fd, "%s ", profile_msg(self));
24750 }
24751 else
24752 fprintf(fd, " ");
24753}
24754
24755/*
24756 * Compare function for total time sorting.
24757 */
24758 static int
24759#ifdef __BORLANDC__
24760_RTLENTRYF
24761#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024762prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024763{
24764 ufunc_T *p1, *p2;
24765
24766 p1 = *(ufunc_T **)s1;
24767 p2 = *(ufunc_T **)s2;
24768 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24769}
24770
24771/*
24772 * Compare function for self time sorting.
24773 */
24774 static int
24775#ifdef __BORLANDC__
24776_RTLENTRYF
24777#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024778prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024779{
24780 ufunc_T *p1, *p2;
24781
24782 p1 = *(ufunc_T **)s1;
24783 p2 = *(ufunc_T **)s2;
24784 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24785}
24786
Bram Moolenaar05159a02005-02-26 23:04:13 +000024787#endif
24788
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024789/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024790 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024791 * Return TRUE if a package was loaded.
24792 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024793 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024794script_autoload(
24795 char_u *name,
24796 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024797{
24798 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024799 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024800 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024801 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024802
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024803 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024804 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024805 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024806 return FALSE;
24807
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024808 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024809
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024810 /* Find the name in the list of previously loaded package names. Skip
24811 * "autoload/", it's always the same. */
24812 for (i = 0; i < ga_loaded.ga_len; ++i)
24813 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24814 break;
24815 if (!reload && i < ga_loaded.ga_len)
24816 ret = FALSE; /* was loaded already */
24817 else
24818 {
24819 /* Remember the name if it wasn't loaded already. */
24820 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24821 {
24822 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24823 tofree = NULL;
24824 }
24825
24826 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024827 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024828 ret = TRUE;
24829 }
24830
24831 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024832 return ret;
24833}
24834
24835/*
24836 * Return the autoload script name for a function or variable name.
24837 * Returns NULL when out of memory.
24838 */
24839 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024840autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024841{
24842 char_u *p;
24843 char_u *scriptname;
24844
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024845 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024846 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24847 if (scriptname == NULL)
24848 return FALSE;
24849 STRCPY(scriptname, "autoload/");
24850 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024851 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024852 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024853 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024854 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024855 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024856}
24857
Bram Moolenaar071d4272004-06-13 20:20:40 +000024858#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24859
24860/*
24861 * Function given to ExpandGeneric() to obtain the list of user defined
24862 * function names.
24863 */
24864 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024865get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024866{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024867 static long_u done;
24868 static hashitem_T *hi;
24869 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870
24871 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024873 done = 0;
24874 hi = func_hashtab.ht_array;
24875 }
24876 if (done < func_hashtab.ht_used)
24877 {
24878 if (done++ > 0)
24879 ++hi;
24880 while (HASHITEM_EMPTY(hi))
24881 ++hi;
24882 fp = HI2UF(hi);
24883
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024884 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024885 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024886
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024887 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24888 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024889
24890 cat_func_name(IObuff, fp);
24891 if (xp->xp_context != EXPAND_USER_FUNC)
24892 {
24893 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024894 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 STRCAT(IObuff, ")");
24896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024897 return IObuff;
24898 }
24899 return NULL;
24900}
24901
24902#endif /* FEAT_CMDL_COMPL */
24903
24904/*
24905 * Copy the function name of "fp" to buffer "buf".
24906 * "buf" must be able to hold the function name plus three bytes.
24907 * Takes care of script-local function names.
24908 */
24909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024910cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024912 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 {
24914 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024915 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916 }
24917 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024918 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919}
24920
24921/*
24922 * ":delfunction {name}"
24923 */
24924 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024925ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024926{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024927 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 char_u *p;
24929 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024930 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931
24932 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024933 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024934 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024936 {
24937 if (fudi.fd_dict != NULL && !eap->skip)
24938 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024939 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941 if (!ends_excmd(*skipwhite(p)))
24942 {
24943 vim_free(name);
24944 EMSG(_(e_trailing));
24945 return;
24946 }
24947 eap->nextcmd = check_nextcmd(p);
24948 if (eap->nextcmd != NULL)
24949 *p = NUL;
24950
24951 if (!eap->skip)
24952 fp = find_func(name);
24953 vim_free(name);
24954
24955 if (!eap->skip)
24956 {
24957 if (fp == NULL)
24958 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024959 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960 return;
24961 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024962 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963 {
24964 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24965 return;
24966 }
24967
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024968 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024970 /* Delete the dict item that refers to the function, it will
24971 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024972 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024973 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024974 else
24975 func_free(fp);
24976 }
24977}
24978
24979/*
24980 * Free a function and remove it from the list of functions.
24981 */
24982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024983func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024984{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024985 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024986
24987 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024988 ga_clear_strings(&(fp->uf_args));
24989 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024990#ifdef FEAT_PROFILE
24991 vim_free(fp->uf_tml_count);
24992 vim_free(fp->uf_tml_total);
24993 vim_free(fp->uf_tml_self);
24994#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024995
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024996 /* remove the function from the function hashtable */
24997 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24998 if (HASHITEM_EMPTY(hi))
24999 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025000 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025001 hash_remove(&func_hashtab, hi);
25002
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025003 vim_free(fp);
25004}
25005
25006/*
25007 * Unreference a Function: decrement the reference count and free it when it
25008 * becomes zero. Only for numbered functions.
25009 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025011func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025012{
25013 ufunc_T *fp;
25014
25015 if (name != NULL && isdigit(*name))
25016 {
25017 fp = find_func(name);
25018 if (fp == NULL)
25019 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025020 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025021 {
25022 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025023 * when "uf_calls" becomes zero. */
25024 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025025 func_free(fp);
25026 }
25027 }
25028}
25029
25030/*
25031 * Count a reference to a Function.
25032 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025033 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025034func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025035{
25036 ufunc_T *fp;
25037
25038 if (name != NULL && isdigit(*name))
25039 {
25040 fp = find_func(name);
25041 if (fp == NULL)
25042 EMSG2(_(e_intern2), "func_ref()");
25043 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025044 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025045 }
25046}
25047
25048/*
25049 * Call a user function.
25050 */
25051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025052call_user_func(
25053 ufunc_T *fp, /* pointer to function */
25054 int argcount, /* nr of args */
25055 typval_T *argvars, /* arguments */
25056 typval_T *rettv, /* return value */
25057 linenr_T firstline, /* first line of range */
25058 linenr_T lastline, /* last line of range */
25059 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060{
Bram Moolenaar33570922005-01-25 22:26:29 +000025061 char_u *save_sourcing_name;
25062 linenr_T save_sourcing_lnum;
25063 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025064 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025065 int save_did_emsg;
25066 static int depth = 0;
25067 dictitem_T *v;
25068 int fixvar_idx = 0; /* index in fixvar[] */
25069 int i;
25070 int ai;
25071 char_u numbuf[NUMBUFLEN];
25072 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025073 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025074#ifdef FEAT_PROFILE
25075 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025076 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078
25079 /* If depth of calling is getting too high, don't execute the function */
25080 if (depth >= p_mfd)
25081 {
25082 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025083 rettv->v_type = VAR_NUMBER;
25084 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 return;
25086 }
25087 ++depth;
25088
25089 line_breakcheck(); /* check for CTRL-C hit */
25090
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025091 fc = (funccall_T *)alloc(sizeof(funccall_T));
25092 fc->caller = current_funccal;
25093 current_funccal = fc;
25094 fc->func = fp;
25095 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025096 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025097 fc->linenr = 0;
25098 fc->returned = FALSE;
25099 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025101 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25102 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103
Bram Moolenaar33570922005-01-25 22:26:29 +000025104 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025105 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025106 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25107 * each argument variable and saves a lot of time.
25108 */
25109 /*
25110 * Init l: variables.
25111 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025112 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025113 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025114 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025115 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25116 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025117 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025118 name = v->di_key;
25119 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025120 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025121 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025122 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025123 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025124 v->di_tv.vval.v_dict = selfdict;
25125 ++selfdict->dv_refcount;
25126 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025127
Bram Moolenaar33570922005-01-25 22:26:29 +000025128 /*
25129 * Init a: variables.
25130 * Set a:0 to "argcount".
25131 * Set a:000 to a list with room for the "..." arguments.
25132 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025133 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025134 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025135 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025136 /* Use "name" to avoid a warning from some compiler that checks the
25137 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025138 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025139 name = v->di_key;
25140 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025141 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025142 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025143 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025144 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025145 v->di_tv.vval.v_list = &fc->l_varlist;
25146 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25147 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25148 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025149
25150 /*
25151 * Set a:firstline to "firstline" and a:lastline to "lastline".
25152 * Set a:name to named arguments.
25153 * Set a:N to the "..." arguments.
25154 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025155 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025156 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025157 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025158 (varnumber_T)lastline);
25159 for (i = 0; i < argcount; ++i)
25160 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025161 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025162 if (ai < 0)
25163 /* named argument a:name */
25164 name = FUNCARG(fp, i);
25165 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025166 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025167 /* "..." argument a:1, a:2, etc. */
25168 sprintf((char *)numbuf, "%d", ai + 1);
25169 name = numbuf;
25170 }
25171 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25172 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025173 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025174 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25175 }
25176 else
25177 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025178 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25179 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025180 if (v == NULL)
25181 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025182 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025183 }
25184 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025185 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025186
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025187 /* Note: the values are copied directly to avoid alloc/free.
25188 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025189 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025190 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025191
25192 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25193 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025194 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25195 fc->l_listitems[ai].li_tv = argvars[i];
25196 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025197 }
25198 }
25199
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200 /* Don't redraw while executing the function. */
25201 ++RedrawingDisabled;
25202 save_sourcing_name = sourcing_name;
25203 save_sourcing_lnum = sourcing_lnum;
25204 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025205 /* need space for function name + ("function " + 3) or "[number]" */
25206 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25207 + STRLEN(fp->uf_name) + 20;
25208 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 if (sourcing_name != NULL)
25210 {
25211 if (save_sourcing_name != NULL
25212 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025213 sprintf((char *)sourcing_name, "%s[%d]..",
25214 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 else
25216 STRCPY(sourcing_name, "function ");
25217 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25218
25219 if (p_verbose >= 12)
25220 {
25221 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025222 verbose_enter_scroll();
25223
Bram Moolenaar555b2802005-05-19 21:08:39 +000025224 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 if (p_verbose >= 14)
25226 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025228 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025229 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025230 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231
25232 msg_puts((char_u *)"(");
25233 for (i = 0; i < argcount; ++i)
25234 {
25235 if (i > 0)
25236 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025237 if (argvars[i].v_type == VAR_NUMBER)
25238 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239 else
25240 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025241 /* Do not want errors such as E724 here. */
25242 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025243 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025244 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025245 if (s != NULL)
25246 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025247 if (vim_strsize(s) > MSG_BUF_CLEN)
25248 {
25249 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25250 s = buf;
25251 }
25252 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025253 vim_free(tofree);
25254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 }
25256 }
25257 msg_puts((char_u *)")");
25258 }
25259 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025260
25261 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 --no_wait_return;
25263 }
25264 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025265#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025266 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025267 {
25268 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25269 func_do_profile(fp);
25270 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025271 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025272 {
25273 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025274 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025275 profile_zero(&fp->uf_tm_children);
25276 }
25277 script_prof_save(&wait_start);
25278 }
25279#endif
25280
Bram Moolenaar071d4272004-06-13 20:20:40 +000025281 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025282 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025283 save_did_emsg = did_emsg;
25284 did_emsg = FALSE;
25285
25286 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025287 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25289
25290 --RedrawingDisabled;
25291
25292 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025293 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025295 clear_tv(rettv);
25296 rettv->v_type = VAR_NUMBER;
25297 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298 }
25299
Bram Moolenaar05159a02005-02-26 23:04:13 +000025300#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025301 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025302 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025303 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025304 profile_end(&call_start);
25305 profile_sub_wait(&wait_start, &call_start);
25306 profile_add(&fp->uf_tm_total, &call_start);
25307 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025308 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025309 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025310 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25311 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025312 }
25313 }
25314#endif
25315
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 /* when being verbose, mention the return value */
25317 if (p_verbose >= 12)
25318 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025320 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025323 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025324 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025325 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025326 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025327 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025328 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025329 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025330 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025331 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025332 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025333
Bram Moolenaar555b2802005-05-19 21:08:39 +000025334 /* The value may be very long. Skip the middle part, so that we
25335 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025336 * truncate it at the end. Don't want errors such as E724 here. */
25337 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025338 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025339 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025340 if (s != NULL)
25341 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025342 if (vim_strsize(s) > MSG_BUF_CLEN)
25343 {
25344 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25345 s = buf;
25346 }
25347 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025348 vim_free(tofree);
25349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350 }
25351 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025352
25353 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 --no_wait_return;
25355 }
25356
25357 vim_free(sourcing_name);
25358 sourcing_name = save_sourcing_name;
25359 sourcing_lnum = save_sourcing_lnum;
25360 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025361#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025362 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025363 script_prof_restore(&wait_start);
25364#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025365
25366 if (p_verbose >= 12 && sourcing_name != NULL)
25367 {
25368 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025369 verbose_enter_scroll();
25370
Bram Moolenaar555b2802005-05-19 21:08:39 +000025371 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025373
25374 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375 --no_wait_return;
25376 }
25377
25378 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025379 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025381
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025382 /* If the a:000 list and the l: and a: dicts are not referenced we can
25383 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025384 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25385 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25386 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25387 {
25388 free_funccal(fc, FALSE);
25389 }
25390 else
25391 {
25392 hashitem_T *hi;
25393 listitem_T *li;
25394 int todo;
25395
25396 /* "fc" is still in use. This can happen when returning "a:000" or
25397 * assigning "l:" to a global variable.
25398 * Link "fc" in the list for garbage collection later. */
25399 fc->caller = previous_funccal;
25400 previous_funccal = fc;
25401
25402 /* Make a copy of the a: variables, since we didn't do that above. */
25403 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25404 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25405 {
25406 if (!HASHITEM_EMPTY(hi))
25407 {
25408 --todo;
25409 v = HI2DI(hi);
25410 copy_tv(&v->di_tv, &v->di_tv);
25411 }
25412 }
25413
25414 /* Make a copy of the a:000 items, since we didn't do that above. */
25415 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25416 copy_tv(&li->li_tv, &li->li_tv);
25417 }
25418}
25419
25420/*
25421 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025422 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025423 */
25424 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025425can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025426{
25427 return (fc->l_varlist.lv_copyID != copyID
25428 && fc->l_vars.dv_copyID != copyID
25429 && fc->l_avars.dv_copyID != copyID);
25430}
25431
25432/*
25433 * Free "fc" and what it contains.
25434 */
25435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025436free_funccal(
25437 funccall_T *fc,
25438 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025439{
25440 listitem_T *li;
25441
25442 /* The a: variables typevals may not have been allocated, only free the
25443 * allocated variables. */
25444 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25445
25446 /* free all l: variables */
25447 vars_clear(&fc->l_vars.dv_hashtab);
25448
25449 /* Free the a:000 variables if they were allocated. */
25450 if (free_val)
25451 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25452 clear_tv(&li->li_tv);
25453
25454 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455}
25456
25457/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025458 * Add a number variable "name" to dict "dp" with value "nr".
25459 */
25460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025461add_nr_var(
25462 dict_T *dp,
25463 dictitem_T *v,
25464 char *name,
25465 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025466{
25467 STRCPY(v->di_key, name);
25468 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25469 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25470 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025471 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025472 v->di_tv.vval.v_number = nr;
25473}
25474
25475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 * ":return [expr]"
25477 */
25478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025479ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480{
25481 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025482 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483 int returning = FALSE;
25484
25485 if (current_funccal == NULL)
25486 {
25487 EMSG(_("E133: :return not inside a function"));
25488 return;
25489 }
25490
25491 if (eap->skip)
25492 ++emsg_skip;
25493
25494 eap->nextcmd = NULL;
25495 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025496 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497 {
25498 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025499 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025501 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025502 }
25503 /* It's safer to return also on error. */
25504 else if (!eap->skip)
25505 {
25506 /*
25507 * Return unless the expression evaluation has been cancelled due to an
25508 * aborting error, an interrupt, or an exception.
25509 */
25510 if (!aborting())
25511 returning = do_return(eap, FALSE, TRUE, NULL);
25512 }
25513
25514 /* When skipping or the return gets pending, advance to the next command
25515 * in this line (!returning). Otherwise, ignore the rest of the line.
25516 * Following lines will be ignored by get_func_line(). */
25517 if (returning)
25518 eap->nextcmd = NULL;
25519 else if (eap->nextcmd == NULL) /* no argument */
25520 eap->nextcmd = check_nextcmd(arg);
25521
25522 if (eap->skip)
25523 --emsg_skip;
25524}
25525
25526/*
25527 * Return from a function. Possibly makes the return pending. Also called
25528 * for a pending return at the ":endtry" or after returning from an extra
25529 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025530 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025531 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532 * FALSE when the return gets pending.
25533 */
25534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025535do_return(
25536 exarg_T *eap,
25537 int reanimate,
25538 int is_cmd,
25539 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540{
25541 int idx;
25542 struct condstack *cstack = eap->cstack;
25543
25544 if (reanimate)
25545 /* Undo the return. */
25546 current_funccal->returned = FALSE;
25547
25548 /*
25549 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25550 * not in its finally clause (which then is to be executed next) is found.
25551 * In this case, make the ":return" pending for execution at the ":endtry".
25552 * Otherwise, return normally.
25553 */
25554 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25555 if (idx >= 0)
25556 {
25557 cstack->cs_pending[idx] = CSTP_RETURN;
25558
25559 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025560 /* A pending return again gets pending. "rettv" points to an
25561 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025563 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025564 else
25565 {
25566 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025567 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025569 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025571 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025572 {
25573 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025574 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025575 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025576 else
25577 EMSG(_(e_outofmem));
25578 }
25579 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025580 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025581
25582 if (reanimate)
25583 {
25584 /* The pending return value could be overwritten by a ":return"
25585 * without argument in a finally clause; reset the default
25586 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025587 current_funccal->rettv->v_type = VAR_NUMBER;
25588 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025589 }
25590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025591 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592 }
25593 else
25594 {
25595 current_funccal->returned = TRUE;
25596
25597 /* If the return is carried out now, store the return value. For
25598 * a return immediately after reanimation, the value is already
25599 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025600 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025602 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025603 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025604 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025605 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606 }
25607 }
25608
25609 return idx < 0;
25610}
25611
25612/*
25613 * Free the variable with a pending return value.
25614 */
25615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025616discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617{
Bram Moolenaar33570922005-01-25 22:26:29 +000025618 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025619}
25620
25621/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025622 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623 * is an allocated string. Used by report_pending() for verbose messages.
25624 */
25625 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025626get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025627{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025628 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025629 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025630 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025631
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025632 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025633 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025634 if (s == NULL)
25635 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025636
25637 STRCPY(IObuff, ":return ");
25638 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25639 if (STRLEN(s) + 8 >= IOSIZE)
25640 STRCPY(IObuff + IOSIZE - 4, "...");
25641 vim_free(tofree);
25642 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025643}
25644
25645/*
25646 * Get next function line.
25647 * Called by do_cmdline() to get the next line.
25648 * Returns allocated string, or NULL for end of function.
25649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025651get_func_line(
25652 int c UNUSED,
25653 void *cookie,
25654 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655{
Bram Moolenaar33570922005-01-25 22:26:29 +000025656 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025657 ufunc_T *fp = fcp->func;
25658 char_u *retval;
25659 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660
25661 /* If breakpoints have been added/deleted need to check for it. */
25662 if (fcp->dbg_tick != debug_tick)
25663 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025664 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025665 sourcing_lnum);
25666 fcp->dbg_tick = debug_tick;
25667 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025668#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025669 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025670 func_line_end(cookie);
25671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025672
Bram Moolenaar05159a02005-02-26 23:04:13 +000025673 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025674 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25675 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676 retval = NULL;
25677 else
25678 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025679 /* Skip NULL lines (continuation lines). */
25680 while (fcp->linenr < gap->ga_len
25681 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25682 ++fcp->linenr;
25683 if (fcp->linenr >= gap->ga_len)
25684 retval = NULL;
25685 else
25686 {
25687 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25688 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025689#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025690 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025691 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025692#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694 }
25695
25696 /* Did we encounter a breakpoint? */
25697 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25698 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025699 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025700 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025701 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025702 sourcing_lnum);
25703 fcp->dbg_tick = debug_tick;
25704 }
25705
25706 return retval;
25707}
25708
Bram Moolenaar05159a02005-02-26 23:04:13 +000025709#if defined(FEAT_PROFILE) || defined(PROTO)
25710/*
25711 * Called when starting to read a function line.
25712 * "sourcing_lnum" must be correct!
25713 * When skipping lines it may not actually be executed, but we won't find out
25714 * until later and we need to store the time now.
25715 */
25716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025717func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025718{
25719 funccall_T *fcp = (funccall_T *)cookie;
25720 ufunc_T *fp = fcp->func;
25721
25722 if (fp->uf_profiling && sourcing_lnum >= 1
25723 && sourcing_lnum <= fp->uf_lines.ga_len)
25724 {
25725 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025726 /* Skip continuation lines. */
25727 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25728 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025729 fp->uf_tml_execed = FALSE;
25730 profile_start(&fp->uf_tml_start);
25731 profile_zero(&fp->uf_tml_children);
25732 profile_get_wait(&fp->uf_tml_wait);
25733 }
25734}
25735
25736/*
25737 * Called when actually executing a function line.
25738 */
25739 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025740func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025741{
25742 funccall_T *fcp = (funccall_T *)cookie;
25743 ufunc_T *fp = fcp->func;
25744
25745 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25746 fp->uf_tml_execed = TRUE;
25747}
25748
25749/*
25750 * Called when done with a function line.
25751 */
25752 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025753func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025754{
25755 funccall_T *fcp = (funccall_T *)cookie;
25756 ufunc_T *fp = fcp->func;
25757
25758 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25759 {
25760 if (fp->uf_tml_execed)
25761 {
25762 ++fp->uf_tml_count[fp->uf_tml_idx];
25763 profile_end(&fp->uf_tml_start);
25764 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025765 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025766 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25767 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025768 }
25769 fp->uf_tml_idx = -1;
25770 }
25771}
25772#endif
25773
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774/*
25775 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025776 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777 */
25778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025779func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780{
Bram Moolenaar33570922005-01-25 22:26:29 +000025781 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025782
25783 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25784 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025785 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 || fcp->returned);
25787}
25788
25789/*
25790 * return TRUE if cookie indicates a function which "abort"s on errors.
25791 */
25792 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025793func_has_abort(
25794 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025796 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025797}
25798
25799#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25800typedef enum
25801{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025802 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25803 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25804 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805} var_flavour_T;
25806
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025807static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808
25809 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025810var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025811{
25812 char_u *p = varname;
25813
25814 if (ASCII_ISUPPER(*p))
25815 {
25816 while (*(++p))
25817 if (ASCII_ISLOWER(*p))
25818 return VAR_FLAVOUR_SESSION;
25819 return VAR_FLAVOUR_VIMINFO;
25820 }
25821 else
25822 return VAR_FLAVOUR_DEFAULT;
25823}
25824#endif
25825
25826#if defined(FEAT_VIMINFO) || defined(PROTO)
25827/*
25828 * Restore global vars that start with a capital from the viminfo file
25829 */
25830 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025831read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025832{
25833 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025834 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025835 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025836 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025837
25838 if (!writing && (find_viminfo_parameter('!') != NULL))
25839 {
25840 tab = vim_strchr(virp->vir_line + 1, '\t');
25841 if (tab != NULL)
25842 {
25843 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025844 switch (*tab)
25845 {
25846 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025847#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025848 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025849#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025850 case 'D': type = VAR_DICT; break;
25851 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025852 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854
25855 tab = vim_strchr(tab, '\t');
25856 if (tab != NULL)
25857 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025858 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025859 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025860 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025861 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025862#ifdef FEAT_FLOAT
25863 else if (type == VAR_FLOAT)
25864 (void)string2float(tab + 1, &tv.vval.v_float);
25865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025867 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025868 if (type == VAR_DICT || type == VAR_LIST)
25869 {
25870 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25871
25872 if (etv == NULL)
25873 /* Failed to parse back the dict or list, use it as a
25874 * string. */
25875 tv.v_type = VAR_STRING;
25876 else
25877 {
25878 vim_free(tv.vval.v_string);
25879 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025880 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025881 }
25882 }
25883
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025884 /* when in a function use global variables */
25885 save_funccal = current_funccal;
25886 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025887 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025888 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025889
25890 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025891 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025892 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25893 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025894 }
25895 }
25896 }
25897
25898 return viminfo_readline(virp);
25899}
25900
25901/*
25902 * Write global vars that start with a capital to the viminfo file
25903 */
25904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025905write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025906{
Bram Moolenaar33570922005-01-25 22:26:29 +000025907 hashitem_T *hi;
25908 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025909 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025910 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025911 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025912 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025913 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025914
25915 if (find_viminfo_parameter('!') == NULL)
25916 return;
25917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025918 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025919
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025920 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025921 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025923 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025924 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025925 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025926 this_var = HI2DI(hi);
25927 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025928 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025929 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025930 {
25931 case VAR_STRING: s = "STR"; break;
25932 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025933 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025934 case VAR_DICT: s = "DIC"; break;
25935 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025936 case VAR_SPECIAL: s = "XPL"; break;
25937
25938 case VAR_UNKNOWN:
25939 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025940 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025941 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025942 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025943 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025944 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025945 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025946 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025947 if (p != NULL)
25948 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025949 vim_free(tofree);
25950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025951 }
25952 }
25953}
25954#endif
25955
25956#if defined(FEAT_SESSION) || defined(PROTO)
25957 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025958store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959{
Bram Moolenaar33570922005-01-25 22:26:29 +000025960 hashitem_T *hi;
25961 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025962 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025963 char_u *p, *t;
25964
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025965 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025966 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025968 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025969 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025970 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025971 this_var = HI2DI(hi);
25972 if ((this_var->di_tv.v_type == VAR_NUMBER
25973 || this_var->di_tv.v_type == VAR_STRING)
25974 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025975 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025976 /* Escape special characters with a backslash. Turn a LF and
25977 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025978 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025979 (char_u *)"\\\"\n\r");
25980 if (p == NULL) /* out of memory */
25981 break;
25982 for (t = p; *t != NUL; ++t)
25983 if (*t == '\n')
25984 *t = 'n';
25985 else if (*t == '\r')
25986 *t = 'r';
25987 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025988 this_var->di_key,
25989 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25990 : ' ',
25991 p,
25992 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25993 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025994 || put_eol(fd) == FAIL)
25995 {
25996 vim_free(p);
25997 return FAIL;
25998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 vim_free(p);
26000 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026001#ifdef FEAT_FLOAT
26002 else if (this_var->di_tv.v_type == VAR_FLOAT
26003 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26004 {
26005 float_T f = this_var->di_tv.vval.v_float;
26006 int sign = ' ';
26007
26008 if (f < 0)
26009 {
26010 f = -f;
26011 sign = '-';
26012 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026013 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026014 this_var->di_key, sign, f) < 0)
26015 || put_eol(fd) == FAIL)
26016 return FAIL;
26017 }
26018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019 }
26020 }
26021 return OK;
26022}
26023#endif
26024
Bram Moolenaar661b1822005-07-28 22:36:45 +000026025/*
26026 * Display script name where an item was last set.
26027 * Should only be invoked when 'verbose' is non-zero.
26028 */
26029 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026030last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026031{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026032 char_u *p;
26033
Bram Moolenaar661b1822005-07-28 22:36:45 +000026034 if (scriptID != 0)
26035 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026036 p = home_replace_save(NULL, get_scriptname(scriptID));
26037 if (p != NULL)
26038 {
26039 verbose_enter();
26040 MSG_PUTS(_("\n\tLast set from "));
26041 MSG_PUTS(p);
26042 vim_free(p);
26043 verbose_leave();
26044 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026045 }
26046}
26047
Bram Moolenaard812df62008-11-09 12:46:09 +000026048/*
26049 * List v:oldfiles in a nice way.
26050 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026051 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026052ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026053{
26054 list_T *l = vimvars[VV_OLDFILES].vv_list;
26055 listitem_T *li;
26056 int nr = 0;
26057
26058 if (l == NULL)
26059 msg((char_u *)_("No old files"));
26060 else
26061 {
26062 msg_start();
26063 msg_scroll = TRUE;
26064 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26065 {
26066 msg_outnum((long)++nr);
26067 MSG_PUTS(": ");
26068 msg_outtrans(get_tv_string(&li->li_tv));
26069 msg_putchar('\n');
26070 out_flush(); /* output one line at a time */
26071 ui_breakcheck();
26072 }
26073 /* Assume "got_int" was set to truncate the listing. */
26074 got_int = FALSE;
26075
26076#ifdef FEAT_BROWSE_CMD
26077 if (cmdmod.browse)
26078 {
26079 quit_more = FALSE;
26080 nr = prompt_for_number(FALSE);
26081 msg_starthere();
26082 if (nr > 0)
26083 {
26084 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26085 (long)nr);
26086
26087 if (p != NULL)
26088 {
26089 p = expand_env_save(p);
26090 eap->arg = p;
26091 eap->cmdidx = CMD_edit;
26092 cmdmod.browse = FALSE;
26093 do_exedit(eap, NULL);
26094 vim_free(p);
26095 }
26096 }
26097 }
26098#endif
26099 }
26100}
26101
Bram Moolenaar53744302015-07-17 17:38:22 +020026102/* reset v:option_new, v:option_old and v:option_type */
26103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026104reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026105{
26106 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26107 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26108 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26109}
26110
26111
Bram Moolenaar071d4272004-06-13 20:20:40 +000026112#endif /* FEAT_EVAL */
26113
Bram Moolenaar071d4272004-06-13 20:20:40 +000026114
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026115#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026116
26117#ifdef WIN3264
26118/*
26119 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26120 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026121static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26122static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26123static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026124
26125/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026126 * Get the short path (8.3) for the filename in "fnamep".
26127 * Only works for a valid file name.
26128 * When the path gets longer "fnamep" is changed and the allocated buffer
26129 * is put in "bufp".
26130 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26131 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026132 */
26133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026134get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026135{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026136 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137 char_u *newbuf;
26138
26139 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026140 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141 if (l > len - 1)
26142 {
26143 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026144 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026145 newbuf = vim_strnsave(*fnamep, l);
26146 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026147 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026148
26149 vim_free(*bufp);
26150 *fnamep = *bufp = newbuf;
26151
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026152 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026153 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026154 }
26155
26156 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026157 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026158}
26159
26160/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026161 * Get the short path (8.3) for the filename in "fname". The converted
26162 * path is returned in "bufp".
26163 *
26164 * Some of the directories specified in "fname" may not exist. This function
26165 * will shorten the existing directories at the beginning of the path and then
26166 * append the remaining non-existing path.
26167 *
26168 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026169 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026170 * bufp - Pointer to an allocated buffer for the filename.
26171 * fnamelen - Length of the filename pointed to by fname
26172 *
26173 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026174 */
26175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026176shortpath_for_invalid_fname(
26177 char_u **fname,
26178 char_u **bufp,
26179 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026180{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026181 char_u *short_fname, *save_fname, *pbuf_unused;
26182 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026183 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026184 int old_len, len;
26185 int new_len, sfx_len;
26186 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026187
26188 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026189 old_len = *fnamelen;
26190 save_fname = vim_strnsave(*fname, old_len);
26191 pbuf_unused = NULL;
26192 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026193
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026194 endp = save_fname + old_len - 1; /* Find the end of the copy */
26195 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026196
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026197 /*
26198 * Try shortening the supplied path till it succeeds by removing one
26199 * directory at a time from the tail of the path.
26200 */
26201 len = 0;
26202 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026203 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026204 /* go back one path-separator */
26205 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26206 --endp;
26207 if (endp <= save_fname)
26208 break; /* processed the complete path */
26209
26210 /*
26211 * Replace the path separator with a NUL and try to shorten the
26212 * resulting path.
26213 */
26214 ch = *endp;
26215 *endp = 0;
26216 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026217 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026218 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26219 {
26220 retval = FAIL;
26221 goto theend;
26222 }
26223 *endp = ch; /* preserve the string */
26224
26225 if (len > 0)
26226 break; /* successfully shortened the path */
26227
26228 /* failed to shorten the path. Skip the path separator */
26229 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230 }
26231
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026232 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026233 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026234 /*
26235 * Succeeded in shortening the path. Now concatenate the shortened
26236 * path with the remaining path at the tail.
26237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026239 /* Compute the length of the new path. */
26240 sfx_len = (int)(save_endp - endp) + 1;
26241 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026242
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026243 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026244 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026245 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026247 /* There is not enough space in the currently allocated string,
26248 * copy it to a buffer big enough. */
26249 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026250 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026251 {
26252 retval = FAIL;
26253 goto theend;
26254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255 }
26256 else
26257 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026258 /* Transfer short_fname to the main buffer (it's big enough),
26259 * unless get_short_pathname() did its work in-place. */
26260 *fname = *bufp = save_fname;
26261 if (short_fname != save_fname)
26262 vim_strncpy(save_fname, short_fname, len);
26263 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026264 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026265
26266 /* concat the not-shortened part of the path */
26267 vim_strncpy(*fname + len, endp, sfx_len);
26268 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026270
26271theend:
26272 vim_free(pbuf_unused);
26273 vim_free(save_fname);
26274
26275 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026276}
26277
26278/*
26279 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026280 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026281 */
26282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026283shortpath_for_partial(
26284 char_u **fnamep,
26285 char_u **bufp,
26286 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026287{
26288 int sepcount, len, tflen;
26289 char_u *p;
26290 char_u *pbuf, *tfname;
26291 int hasTilde;
26292
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026293 /* Count up the path separators from the RHS.. so we know which part
26294 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026295 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026296 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297 if (vim_ispathsep(*p))
26298 ++sepcount;
26299
26300 /* Need full path first (use expand_env() to remove a "~/") */
26301 hasTilde = (**fnamep == '~');
26302 if (hasTilde)
26303 pbuf = tfname = expand_env_save(*fnamep);
26304 else
26305 pbuf = tfname = FullName_save(*fnamep, FALSE);
26306
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026307 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026309 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26310 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026311
26312 if (len == 0)
26313 {
26314 /* Don't have a valid filename, so shorten the rest of the
26315 * path if we can. This CAN give us invalid 8.3 filenames, but
26316 * there's not a lot of point in guessing what it might be.
26317 */
26318 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026319 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26320 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026321 }
26322
26323 /* Count the paths backward to find the beginning of the desired string. */
26324 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026325 {
26326#ifdef FEAT_MBYTE
26327 if (has_mbyte)
26328 p -= mb_head_off(tfname, p);
26329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026330 if (vim_ispathsep(*p))
26331 {
26332 if (sepcount == 0 || (hasTilde && sepcount == 1))
26333 break;
26334 else
26335 sepcount --;
26336 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026338 if (hasTilde)
26339 {
26340 --p;
26341 if (p >= tfname)
26342 *p = '~';
26343 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026345 }
26346 else
26347 ++p;
26348
26349 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26350 vim_free(*bufp);
26351 *fnamelen = (int)STRLEN(p);
26352 *bufp = pbuf;
26353 *fnamep = p;
26354
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026355 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026356}
26357#endif /* WIN3264 */
26358
26359/*
26360 * Adjust a filename, according to a string of modifiers.
26361 * *fnamep must be NUL terminated when called. When returning, the length is
26362 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026363 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026364 * When there is an error, *fnamep is set to NULL.
26365 */
26366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026367modify_fname(
26368 char_u *src, /* string with modifiers */
26369 int *usedlen, /* characters after src that are used */
26370 char_u **fnamep, /* file name so far */
26371 char_u **bufp, /* buffer for allocated file name or NULL */
26372 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026373{
26374 int valid = 0;
26375 char_u *tail;
26376 char_u *s, *p, *pbuf;
26377 char_u dirname[MAXPATHL];
26378 int c;
26379 int has_fullname = 0;
26380#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026381 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382 int has_shortname = 0;
26383#endif
26384
26385repeat:
26386 /* ":p" - full path/file_name */
26387 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26388 {
26389 has_fullname = 1;
26390
26391 valid |= VALID_PATH;
26392 *usedlen += 2;
26393
26394 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26395 if ((*fnamep)[0] == '~'
26396#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26397 && ((*fnamep)[1] == '/'
26398# ifdef BACKSLASH_IN_FILENAME
26399 || (*fnamep)[1] == '\\'
26400# endif
26401 || (*fnamep)[1] == NUL)
26402
26403#endif
26404 )
26405 {
26406 *fnamep = expand_env_save(*fnamep);
26407 vim_free(*bufp); /* free any allocated file name */
26408 *bufp = *fnamep;
26409 if (*fnamep == NULL)
26410 return -1;
26411 }
26412
26413 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026414 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026415 {
26416 if (vim_ispathsep(*p)
26417 && p[1] == '.'
26418 && (p[2] == NUL
26419 || vim_ispathsep(p[2])
26420 || (p[2] == '.'
26421 && (p[3] == NUL || vim_ispathsep(p[3])))))
26422 break;
26423 }
26424
26425 /* FullName_save() is slow, don't use it when not needed. */
26426 if (*p != NUL || !vim_isAbsName(*fnamep))
26427 {
26428 *fnamep = FullName_save(*fnamep, *p != NUL);
26429 vim_free(*bufp); /* free any allocated file name */
26430 *bufp = *fnamep;
26431 if (*fnamep == NULL)
26432 return -1;
26433 }
26434
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026435#ifdef WIN3264
26436# if _WIN32_WINNT >= 0x0500
26437 if (vim_strchr(*fnamep, '~') != NULL)
26438 {
26439 /* Expand 8.3 filename to full path. Needed to make sure the same
26440 * file does not have two different names.
26441 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26442 p = alloc(_MAX_PATH + 1);
26443 if (p != NULL)
26444 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026445 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026446 {
26447 vim_free(*bufp);
26448 *bufp = *fnamep = p;
26449 }
26450 else
26451 vim_free(p);
26452 }
26453 }
26454# endif
26455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026456 /* Append a path separator to a directory. */
26457 if (mch_isdir(*fnamep))
26458 {
26459 /* Make room for one or two extra characters. */
26460 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26461 vim_free(*bufp); /* free any allocated file name */
26462 *bufp = *fnamep;
26463 if (*fnamep == NULL)
26464 return -1;
26465 add_pathsep(*fnamep);
26466 }
26467 }
26468
26469 /* ":." - path relative to the current directory */
26470 /* ":~" - path relative to the home directory */
26471 /* ":8" - shortname path - postponed till after */
26472 while (src[*usedlen] == ':'
26473 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26474 {
26475 *usedlen += 2;
26476 if (c == '8')
26477 {
26478#ifdef WIN3264
26479 has_shortname = 1; /* Postpone this. */
26480#endif
26481 continue;
26482 }
26483 pbuf = NULL;
26484 /* Need full path first (use expand_env() to remove a "~/") */
26485 if (!has_fullname)
26486 {
26487 if (c == '.' && **fnamep == '~')
26488 p = pbuf = expand_env_save(*fnamep);
26489 else
26490 p = pbuf = FullName_save(*fnamep, FALSE);
26491 }
26492 else
26493 p = *fnamep;
26494
26495 has_fullname = 0;
26496
26497 if (p != NULL)
26498 {
26499 if (c == '.')
26500 {
26501 mch_dirname(dirname, MAXPATHL);
26502 s = shorten_fname(p, dirname);
26503 if (s != NULL)
26504 {
26505 *fnamep = s;
26506 if (pbuf != NULL)
26507 {
26508 vim_free(*bufp); /* free any allocated file name */
26509 *bufp = pbuf;
26510 pbuf = NULL;
26511 }
26512 }
26513 }
26514 else
26515 {
26516 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26517 /* Only replace it when it starts with '~' */
26518 if (*dirname == '~')
26519 {
26520 s = vim_strsave(dirname);
26521 if (s != NULL)
26522 {
26523 *fnamep = s;
26524 vim_free(*bufp);
26525 *bufp = s;
26526 }
26527 }
26528 }
26529 vim_free(pbuf);
26530 }
26531 }
26532
26533 tail = gettail(*fnamep);
26534 *fnamelen = (int)STRLEN(*fnamep);
26535
26536 /* ":h" - head, remove "/file_name", can be repeated */
26537 /* Don't remove the first "/" or "c:\" */
26538 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26539 {
26540 valid |= VALID_HEAD;
26541 *usedlen += 2;
26542 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026543 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026544 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026545 *fnamelen = (int)(tail - *fnamep);
26546#ifdef VMS
26547 if (*fnamelen > 0)
26548 *fnamelen += 1; /* the path separator is part of the path */
26549#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026550 if (*fnamelen == 0)
26551 {
26552 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26553 p = vim_strsave((char_u *)".");
26554 if (p == NULL)
26555 return -1;
26556 vim_free(*bufp);
26557 *bufp = *fnamep = tail = p;
26558 *fnamelen = 1;
26559 }
26560 else
26561 {
26562 while (tail > s && !after_pathsep(s, tail))
26563 mb_ptr_back(*fnamep, tail);
26564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026565 }
26566
26567 /* ":8" - shortname */
26568 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26569 {
26570 *usedlen += 2;
26571#ifdef WIN3264
26572 has_shortname = 1;
26573#endif
26574 }
26575
26576#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026577 /*
26578 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026579 */
26580 if (has_shortname)
26581 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026582 /* Copy the string if it is shortened by :h and when it wasn't copied
26583 * yet, because we are going to change it in place. Avoids changing
26584 * the buffer name for "%:8". */
26585 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026586 {
26587 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026588 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026589 return -1;
26590 vim_free(*bufp);
26591 *bufp = *fnamep = p;
26592 }
26593
26594 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026595 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026596 if (!has_fullname && !vim_isAbsName(*fnamep))
26597 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026598 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026599 return -1;
26600 }
26601 else
26602 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026603 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026604
Bram Moolenaardc935552011-08-17 15:23:23 +020026605 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026606 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026607 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026608 return -1;
26609
26610 if (l == 0)
26611 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026612 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026613 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026614 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026615 return -1;
26616 }
26617 *fnamelen = l;
26618 }
26619 }
26620#endif /* WIN3264 */
26621
26622 /* ":t" - tail, just the basename */
26623 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26624 {
26625 *usedlen += 2;
26626 *fnamelen -= (int)(tail - *fnamep);
26627 *fnamep = tail;
26628 }
26629
26630 /* ":e" - extension, can be repeated */
26631 /* ":r" - root, without extension, can be repeated */
26632 while (src[*usedlen] == ':'
26633 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26634 {
26635 /* find a '.' in the tail:
26636 * - for second :e: before the current fname
26637 * - otherwise: The last '.'
26638 */
26639 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26640 s = *fnamep - 2;
26641 else
26642 s = *fnamep + *fnamelen - 1;
26643 for ( ; s > tail; --s)
26644 if (s[0] == '.')
26645 break;
26646 if (src[*usedlen + 1] == 'e') /* :e */
26647 {
26648 if (s > tail)
26649 {
26650 *fnamelen += (int)(*fnamep - (s + 1));
26651 *fnamep = s + 1;
26652#ifdef VMS
26653 /* cut version from the extension */
26654 s = *fnamep + *fnamelen - 1;
26655 for ( ; s > *fnamep; --s)
26656 if (s[0] == ';')
26657 break;
26658 if (s > *fnamep)
26659 *fnamelen = s - *fnamep;
26660#endif
26661 }
26662 else if (*fnamep <= tail)
26663 *fnamelen = 0;
26664 }
26665 else /* :r */
26666 {
26667 if (s > tail) /* remove one extension */
26668 *fnamelen = (int)(s - *fnamep);
26669 }
26670 *usedlen += 2;
26671 }
26672
26673 /* ":s?pat?foo?" - substitute */
26674 /* ":gs?pat?foo?" - global substitute */
26675 if (src[*usedlen] == ':'
26676 && (src[*usedlen + 1] == 's'
26677 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26678 {
26679 char_u *str;
26680 char_u *pat;
26681 char_u *sub;
26682 int sep;
26683 char_u *flags;
26684 int didit = FALSE;
26685
26686 flags = (char_u *)"";
26687 s = src + *usedlen + 2;
26688 if (src[*usedlen + 1] == 'g')
26689 {
26690 flags = (char_u *)"g";
26691 ++s;
26692 }
26693
26694 sep = *s++;
26695 if (sep)
26696 {
26697 /* find end of pattern */
26698 p = vim_strchr(s, sep);
26699 if (p != NULL)
26700 {
26701 pat = vim_strnsave(s, (int)(p - s));
26702 if (pat != NULL)
26703 {
26704 s = p + 1;
26705 /* find end of substitution */
26706 p = vim_strchr(s, sep);
26707 if (p != NULL)
26708 {
26709 sub = vim_strnsave(s, (int)(p - s));
26710 str = vim_strnsave(*fnamep, *fnamelen);
26711 if (sub != NULL && str != NULL)
26712 {
26713 *usedlen = (int)(p + 1 - src);
26714 s = do_string_sub(str, pat, sub, flags);
26715 if (s != NULL)
26716 {
26717 *fnamep = s;
26718 *fnamelen = (int)STRLEN(s);
26719 vim_free(*bufp);
26720 *bufp = s;
26721 didit = TRUE;
26722 }
26723 }
26724 vim_free(sub);
26725 vim_free(str);
26726 }
26727 vim_free(pat);
26728 }
26729 }
26730 /* after using ":s", repeat all the modifiers */
26731 if (didit)
26732 goto repeat;
26733 }
26734 }
26735
Bram Moolenaar26df0922014-02-23 23:39:13 +010026736 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26737 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026738 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026739 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026740 if (c != NUL)
26741 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026742 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026743 if (c != NUL)
26744 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026745 if (p == NULL)
26746 return -1;
26747 vim_free(*bufp);
26748 *bufp = *fnamep = p;
26749 *fnamelen = (int)STRLEN(p);
26750 *usedlen += 2;
26751 }
26752
Bram Moolenaar071d4272004-06-13 20:20:40 +000026753 return valid;
26754}
26755
26756/*
26757 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26758 * "flags" can be "g" to do a global substitute.
26759 * Returns an allocated string, NULL for error.
26760 */
26761 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026762do_string_sub(
26763 char_u *str,
26764 char_u *pat,
26765 char_u *sub,
26766 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026767{
26768 int sublen;
26769 regmatch_T regmatch;
26770 int i;
26771 int do_all;
26772 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026773 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026774 garray_T ga;
26775 char_u *ret;
26776 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026777 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026778
26779 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26780 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026781 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026782
26783 ga_init2(&ga, 1, 200);
26784
26785 do_all = (flags[0] == 'g');
26786
26787 regmatch.rm_ic = p_ic;
26788 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26789 if (regmatch.regprog != NULL)
26790 {
26791 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026792 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026793 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26794 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026795 /* Skip empty match except for first match. */
26796 if (regmatch.startp[0] == regmatch.endp[0])
26797 {
26798 if (zero_width == regmatch.startp[0])
26799 {
26800 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026801 i = MB_PTR2LEN(tail);
26802 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26803 (size_t)i);
26804 ga.ga_len += i;
26805 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026806 continue;
26807 }
26808 zero_width = regmatch.startp[0];
26809 }
26810
Bram Moolenaar071d4272004-06-13 20:20:40 +000026811 /*
26812 * Get some space for a temporary buffer to do the substitution
26813 * into. It will contain:
26814 * - The text up to where the match is.
26815 * - The substituted text.
26816 * - The text after the match.
26817 */
26818 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026819 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026820 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26821 {
26822 ga_clear(&ga);
26823 break;
26824 }
26825
26826 /* copy the text up to where the match is */
26827 i = (int)(regmatch.startp[0] - tail);
26828 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26829 /* add the substituted text */
26830 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26831 + ga.ga_len + i, TRUE, TRUE, FALSE);
26832 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026833 tail = regmatch.endp[0];
26834 if (*tail == NUL)
26835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026836 if (!do_all)
26837 break;
26838 }
26839
26840 if (ga.ga_data != NULL)
26841 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26842
Bram Moolenaar473de612013-06-08 18:19:48 +020026843 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026844 }
26845
26846 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26847 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026848 if (p_cpo == empty_option)
26849 p_cpo = save_cpo;
26850 else
26851 /* Darn, evaluating {sub} expression changed the value. */
26852 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026853
26854 return ret;
26855}
26856
26857#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */