blob: e6175f6ba9f58f3b8472adb22ce4b529d15b5a65 [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 Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200358 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100364 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000365 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200366 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200367 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000368};
369
370/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000371#define vv_type vv_di.di_tv.v_type
372#define vv_nr vv_di.di_tv.vval.v_number
373#define vv_float vv_di.di_tv.vval.v_float
374#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000375#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000376#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000377
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200378static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000379#define vimvarht vimvardict.dv_hashtab
380
Bram Moolenaara40058a2005-07-11 22:42:07 +0000381static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
382static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
384static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
385static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
387static void list_glob_vars __ARGS((int *first));
388static void list_buf_vars __ARGS((int *first));
389static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000390#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000392#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000393static void list_vim_vars __ARGS((int *first));
394static void list_script_vars __ARGS((int *first));
395static void list_func_vars __ARGS((int *first));
396static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000397static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
398static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100399static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000400static void clear_lval __ARGS((lval_T *lp));
401static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
402static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000403static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
404static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
405static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
406static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
407static void item_lock __ARGS((typval_T *tv, int deep, int lock));
408static int tv_islocked __ARGS((typval_T *tv));
409
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
411static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
413static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000416static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
417static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000419static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000420static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000424static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100426static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
427static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
428static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000429static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000431static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
433static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000434static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000435static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100436static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000438static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200439static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
441static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000442static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000444static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000446static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
447static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000449#ifdef FEAT_FLOAT
450static int string2float __ARGS((char_u *text, float_T *value));
451#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000452static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
453static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar8822a9c2014-01-14 19:44:34 +0100454static char_u *deref_func_name __ARGS((char_u *name, int *lenp, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200456static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000457static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000458static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
461static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200462static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100465static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200469static void f_arglistid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200472static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200474static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000475#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000476static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100485static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100487static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489#ifdef FEAT_FLOAT
490static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000492static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000495static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000496static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000497#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000498static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000499static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
501#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#ifdef FEAT_FLOAT
505static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200506static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
511static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarc7f02552014-04-01 21:00:59 +0200521static void f_exepath __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200523#ifdef FEAT_FLOAT
524static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
525#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000526static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000528static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000534#ifdef FEAT_FLOAT
535static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000539static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000548static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000549static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000550static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000556static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c1329c2014-08-06 13:36:59 +0200557static void f_getcmdwintype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000565static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000566static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200567static void f_getcurpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000568static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000569static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200572static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000573static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000581static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000595static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000596static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100600static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000602static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000603static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000614#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200615static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000616static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
617#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200618#ifdef FEAT_LUA
619static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
620#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000625static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarb3414592014-06-17 17:48:32 +0200626static void f_matchaddpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000627static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000628static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000630static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000631static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000634#ifdef vim_mkdir
635static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
636#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100638#ifdef FEAT_MZSCHEME
639static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
640#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000641static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
642static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100643static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000644static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000645#ifdef FEAT_FLOAT
646static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000649static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000650static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200651#ifdef FEAT_PYTHON3
652static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
654#ifdef FEAT_PYTHON
655static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
656#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200674static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100676static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000679static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000681static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
684static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
685static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
686static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
687static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000688static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000689static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000690static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000691static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200693static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000694static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100696#ifdef FEAT_CRYPT
697static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
698#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000699static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200700static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200704static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000706static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000707static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000708static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000710static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000711#ifdef FEAT_FLOAT
712static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
714#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000715static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200716static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000717#ifdef HAVE_STRFTIME
718static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
719#endif
720static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
725static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200726static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200727static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000728static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
729static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
730static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
732static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000733static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200734static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200736static void f_systemlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000737static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000738static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000739static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000740static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000741static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000743static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200744#ifdef FEAT_FLOAT
745static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
747#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000751#ifdef FEAT_FLOAT
752static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
753#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200755static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200756static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar327aa022014-03-25 18:24:23 +0100757static void f_uniq __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
759static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
760static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100761static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
763static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
764static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
765static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
766static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
767static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000768static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
769static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000770static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000771static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100772static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000773
Bram Moolenaar493c1782014-05-28 14:34:46 +0200774static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000775static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static int get_env_len __ARGS((char_u **arg));
777static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000778static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000779static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
780#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
781#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
782 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000783static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000785static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose, int no_autoload));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000787static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static typval_T *alloc_tv __ARGS((void));
789static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000790static void init_tv __ARGS((typval_T *varp));
791static long get_tv_number __ARGS((typval_T *varp));
792static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000793static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000794static char_u *get_tv_string __ARGS((typval_T *varp));
795static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000796static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100797static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
798static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int no_autoload));
Bram Moolenaar33570922005-01-25 22:26:29 +0000799static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
800static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
801static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000802static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
803static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000804static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
805static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000806static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200807static int var_check_func_name __ARGS((char_u *name, int new_var));
808static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000809static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000810static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000811static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
812static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
813static int eval_fname_script __ARGS((char_u *p));
814static int eval_fname_sid __ARGS((char_u *p));
815static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000816static ufunc_T *find_func __ARGS((char_u *name));
817static int function_exists __ARGS((char_u *name));
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200818static int builtin_function __ARGS((char_u *name, int len));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#ifdef FEAT_PROFILE
820static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000821static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
822static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
823static int
824# ifdef __BORLANDC__
825 _RTLENTRYF
826# endif
827 prof_total_cmp __ARGS((const void *s1, const void *s2));
828static int
829# ifdef __BORLANDC__
830 _RTLENTRYF
831# endif
832 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200834static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000835static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000836static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000837static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000838static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000839static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
840static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000841static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000842static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
843static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000844static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000845static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000846static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +0200847static int write_list __ARGS((FILE *fd, list_T *list, int binary));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +0200848static void get_cmd_output_as_rettv __ARGS((typval_T *argvars, typval_T *rettv, int retlist));
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200850
851#ifdef EBCDIC
852static int compare_func_name __ARGS((const void *s1, const void *s2));
853static void sortFunctions __ARGS(());
854#endif
855
Bram Moolenaar33570922005-01-25 22:26:29 +0000856/*
857 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000858 */
859 void
860eval_init()
861{
Bram Moolenaar33570922005-01-25 22:26:29 +0000862 int i;
863 struct vimvar *p;
864
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200865 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
866 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200867 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000869 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870
871 for (i = 0; i < VV_LEN; ++i)
872 {
873 p = &vimvars[i];
874 STRCPY(p->vv_di.di_key, p->vv_name);
875 if (p->vv_flags & VV_RO)
876 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
877 else if (p->vv_flags & VV_RO_SBX)
878 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
879 else
880 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000881
882 /* add to v: scope dict, unless the value is not always available */
883 if (p->vv_type != VAR_UNKNOWN)
884 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000885 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000886 /* add to compat scope dict */
887 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000888 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000889 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100890 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200891 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200892
893#ifdef EBCDIC
894 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100895 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200896 */
897 sortFunctions();
898#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000899}
900
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000901#if defined(EXITFREE) || defined(PROTO)
902 void
903eval_clear()
904{
905 int i;
906 struct vimvar *p;
907
908 for (i = 0; i < VV_LEN; ++i)
909 {
910 p = &vimvars[i];
911 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000913 vim_free(p->vv_str);
914 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000915 }
916 else if (p->vv_di.di_tv.v_type == VAR_LIST)
917 {
918 list_unref(p->vv_list);
919 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000921 }
922 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000923 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000924 hash_clear(&compat_hashtab);
925
Bram Moolenaard9fba312005-06-26 22:34:35 +0000926 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100927# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200928 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100929# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000930
931 /* global variables */
932 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000933
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000934 /* autoloaded script names */
935 ga_clear_strings(&ga_loaded);
936
Bram Moolenaarcca74132013-09-25 21:00:28 +0200937 /* Script-local variables. First clear all the variables and in a second
938 * loop free the scriptvar_T, because a variable in one script might hold
939 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200940 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200941 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200942 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200943 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200944 ga_clear(&ga_scripts);
945
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000946 /* unreferenced lists and dicts */
947 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000948
949 /* functions */
950 free_all_functions();
951 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000952}
953#endif
954
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 * Return the name of the executed function.
957 */
958 char_u *
959func_name(cookie)
960 void *cookie;
961{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000962 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the address holding the next breakpoint line for a funccall cookie.
967 */
968 linenr_T *
969func_breakpoint(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/*
976 * Return the address holding the debug tick for a funccall cookie.
977 */
978 int *
979func_dbg_tick(cookie)
980 void *cookie;
981{
Bram Moolenaar33570922005-01-25 22:26:29 +0000982 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983}
984
985/*
986 * Return the nesting level for a funccall cookie.
987 */
988 int
989func_level(cookie)
990 void *cookie;
991{
Bram Moolenaar33570922005-01-25 22:26:29 +0000992 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993}
994
995/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000996funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000998/* pointer to list of previously used funccal, still around because some
999 * item in it is still being used. */
1000funccall_T *previous_funccal = NULL;
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002/*
1003 * Return TRUE when a function was ended by a ":return" command.
1004 */
1005 int
1006current_func_returned()
1007{
1008 return current_funccal->returned;
1009}
1010
1011
1012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 * Set an internal variable to a string value. Creates the variable if it does
1014 * not already exist.
1015 */
1016 void
1017set_internal_string_var(name, value)
1018 char_u *name;
1019 char_u *value;
1020{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001021 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001022 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
1024 val = vim_strsave(value);
1025 if (val != NULL)
1026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001028 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001030 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 }
1034}
1035
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001037static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038static char_u *redir_endp = NULL;
1039static char_u *redir_varname = NULL;
1040
1041/*
1042 * Start recording command output to a variable
1043 * Returns OK if successfully completed the setup. FAIL otherwise.
1044 */
1045 int
1046var_redir_start(name, append)
1047 char_u *name;
1048 int append; /* append to an existing variable */
1049{
1050 int save_emsg;
1051 int err;
1052 typval_T tv;
1053
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001054 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 {
1057 EMSG(_(e_invarg));
1058 return FAIL;
1059 }
1060
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001061 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 redir_varname = vim_strsave(name);
1063 if (redir_varname == NULL)
1064 return FAIL;
1065
1066 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1067 if (redir_lval == NULL)
1068 {
1069 var_redir_stop();
1070 return FAIL;
1071 }
1072
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001073 /* The output is stored in growarray "redir_ga" until redirection ends. */
1074 ga_init2(&redir_ga, (int)sizeof(char), 500);
1075
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001076 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001077 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001078 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1080 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001081 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (redir_endp != NULL && *redir_endp != NUL)
1083 /* Trailing characters are present after the variable name */
1084 EMSG(_(e_trailing));
1085 else
1086 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
1091
1092 /* check if we can write to the variable: set it to or append an empty
1093 * string */
1094 save_emsg = did_emsg;
1095 did_emsg = FALSE;
1096 tv.v_type = VAR_STRING;
1097 tv.vval.v_string = (char_u *)"";
1098 if (append)
1099 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1100 else
1101 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001102 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001104 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 if (err)
1106 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001107 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 var_redir_stop();
1109 return FAIL;
1110 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 * Append "value[value_len]" to the variable set by var_redir_start().
1117 * The actual appending is postponed until redirection ends, because the value
1118 * appended may in fact be the string we write to, changing it may cause freed
1119 * memory to be used:
1120 * :redir => foo
1121 * :let foo
1122 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 */
1124 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130
1131 if (redir_lval == NULL)
1132 return;
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001135 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001136 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001137 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001139 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001140 {
1141 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001142 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001143 }
1144 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146}
1147
1148/*
1149 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001150 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001151 */
1152 void
1153var_redir_stop()
1154{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155 typval_T tv;
1156
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001157 if (redir_lval != NULL)
1158 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 /* If there was no error: assign the text to the variable. */
1160 if (redir_endp != NULL)
1161 {
1162 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1163 tv.v_type = VAR_STRING;
1164 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 /* Call get_lval() again, if it's inside a Dict or List it may
1166 * have changed. */
1167 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001168 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001169 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1170 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1171 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001173
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001174 /* free the collected output */
1175 vim_free(redir_ga.ga_data);
1176 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 vim_free(redir_lval);
1179 redir_lval = NULL;
1180 }
1181 vim_free(redir_varname);
1182 redir_varname = NULL;
1183}
1184
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# if defined(FEAT_MBYTE) || defined(PROTO)
1186 int
1187eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1188 char_u *enc_from;
1189 char_u *enc_to;
1190 char_u *fname_from;
1191 char_u *fname_to;
1192{
1193 int err = FALSE;
1194
1195 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1196 set_vim_var_string(VV_CC_TO, enc_to, -1);
1197 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1198 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1199 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1200 err = TRUE;
1201 set_vim_var_string(VV_CC_FROM, NULL, -1);
1202 set_vim_var_string(VV_CC_TO, NULL, -1);
1203 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1204 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1205
1206 if (err)
1207 return FAIL;
1208 return OK;
1209}
1210# endif
1211
1212# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1213 int
1214eval_printexpr(fname, args)
1215 char_u *fname;
1216 char_u *args;
1217{
1218 int err = FALSE;
1219
1220 set_vim_var_string(VV_FNAME_IN, fname, -1);
1221 set_vim_var_string(VV_CMDARG, args, -1);
1222 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1223 err = TRUE;
1224 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1225 set_vim_var_string(VV_CMDARG, NULL, -1);
1226
1227 if (err)
1228 {
1229 mch_remove(fname);
1230 return FAIL;
1231 }
1232 return OK;
1233}
1234# endif
1235
1236# if defined(FEAT_DIFF) || defined(PROTO)
1237 void
1238eval_diff(origfile, newfile, outfile)
1239 char_u *origfile;
1240 char_u *newfile;
1241 char_u *outfile;
1242{
1243 int err = FALSE;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253
1254 void
1255eval_patch(origfile, difffile, outfile)
1256 char_u *origfile;
1257 char_u *difffile;
1258 char_u *outfile;
1259{
1260 int err;
1261
1262 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1263 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1264 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1265 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269}
1270# endif
1271
1272/*
1273 * Top level evaluation function, returning a boolean.
1274 * Sets "error" to TRUE if there was an error.
1275 * Return TRUE or FALSE.
1276 */
1277 int
1278eval_to_bool(arg, error, nextcmd, skip)
1279 char_u *arg;
1280 int *error;
1281 char_u **nextcmd;
1282 int skip; /* only parse, don't execute */
1283{
Bram Moolenaar33570922005-01-25 22:26:29 +00001284 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 int retval = FALSE;
1286
1287 if (skip)
1288 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001289 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 else
1292 {
1293 *error = FALSE;
1294 if (!skip)
1295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001296 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001297 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 }
1300 if (skip)
1301 --emsg_skip;
1302
1303 return retval;
1304}
1305
1306/*
1307 * Top level evaluation function, returning a string. If "skip" is TRUE,
1308 * only parsing to "nextcmd" is done, without reporting errors. Return
1309 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1310 */
1311 char_u *
1312eval_to_string_skip(arg, nextcmd, skip)
1313 char_u *arg;
1314 char_u **nextcmd;
1315 int skip; /* only parse, don't execute */
1316{
Bram Moolenaar33570922005-01-25 22:26:29 +00001317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *retval;
1319
1320 if (skip)
1321 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001322 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 retval = NULL;
1324 else
1325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 retval = vim_strsave(get_tv_string(&tv));
1327 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 if (skip)
1330 --emsg_skip;
1331
1332 return retval;
1333}
1334
1335/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001336 * Skip over an expression at "*pp".
1337 * Return FAIL for an error, OK otherwise.
1338 */
1339 int
1340skip_expr(pp)
1341 char_u **pp;
1342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001344
1345 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001347}
1348
1349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001351 * When "convert" is TRUE convert a List into a sequence of lines and convert
1352 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 * Return pointer to allocated memory, or NULL for failure.
1354 */
1355 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001356eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 char_u *arg;
1358 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360{
Bram Moolenaar33570922005-01-25 22:26:29 +00001361 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001363 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001364#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001368 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 retval = NULL;
1370 else
1371 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001372 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001373 {
1374 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001375 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001376 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001377 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001378 if (tv.vval.v_list->lv_len > 0)
1379 ga_append(&ga, NL);
1380 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001381 ga_append(&ga, NUL);
1382 retval = (char_u *)ga.ga_data;
1383 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001384#ifdef FEAT_FLOAT
1385 else if (convert && tv.v_type == VAR_FLOAT)
1386 {
1387 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1388 retval = vim_strsave(numbuf);
1389 }
1390#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001391 else
1392 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395
1396 return retval;
1397}
1398
1399/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 * Call eval_to_string() without using current local variables and using
1401 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 char_u *arg;
1406 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001407 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408{
1409 char_u *retval;
1410 void *save_funccalp;
1411
1412 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001413 if (use_sandbox)
1414 ++sandbox;
1415 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001417 if (use_sandbox)
1418 --sandbox;
1419 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 restore_funccal(save_funccalp);
1421 return retval;
1422}
1423
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424/*
1425 * Top level evaluation function, returning a number.
1426 * Evaluates "expr" silently.
1427 * Returns -1 for an error.
1428 */
1429 int
1430eval_to_number(expr)
1431 char_u *expr;
1432{
Bram Moolenaar33570922005-01-25 22:26:29 +00001433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001435 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
1437 ++emsg_off;
1438
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001439 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 retval = -1;
1441 else
1442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001443 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001444 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 }
1446 --emsg_off;
1447
1448 return retval;
1449}
1450
Bram Moolenaara40058a2005-07-11 22:42:07 +00001451/*
1452 * Prepare v: variable "idx" to be used.
1453 * Save the current typeval in "save_tv".
1454 * When not used yet add the variable to the v: hashtable.
1455 */
1456 static void
1457prepare_vimvar(idx, save_tv)
1458 int idx;
1459 typval_T *save_tv;
1460{
1461 *save_tv = vimvars[idx].vv_tv;
1462 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1463 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1464}
1465
1466/*
1467 * Restore v: variable "idx" to typeval "save_tv".
1468 * When no longer defined, remove the variable from the v: hashtable.
1469 */
1470 static void
1471restore_vimvar(idx, save_tv)
1472 int idx;
1473 typval_T *save_tv;
1474{
1475 hashitem_T *hi;
1476
Bram Moolenaara40058a2005-07-11 22:42:07 +00001477 vimvars[idx].vv_tv = *save_tv;
1478 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1479 {
1480 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1481 if (HASHITEM_EMPTY(hi))
1482 EMSG2(_(e_intern2), "restore_vimvar()");
1483 else
1484 hash_remove(&vimvarht, hi);
1485 }
1486}
1487
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001488#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001489/*
1490 * Evaluate an expression to a list with suggestions.
1491 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001492 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001493 */
1494 list_T *
1495eval_spell_expr(badword, expr)
1496 char_u *badword;
1497 char_u *expr;
1498{
1499 typval_T save_val;
1500 typval_T rettv;
1501 list_T *list = NULL;
1502 char_u *p = skipwhite(expr);
1503
1504 /* Set "v:val" to the bad word. */
1505 prepare_vimvar(VV_VAL, &save_val);
1506 vimvars[VV_VAL].vv_type = VAR_STRING;
1507 vimvars[VV_VAL].vv_str = badword;
1508 if (p_verbose == 0)
1509 ++emsg_off;
1510
1511 if (eval1(&p, &rettv, TRUE) == OK)
1512 {
1513 if (rettv.v_type != VAR_LIST)
1514 clear_tv(&rettv);
1515 else
1516 list = rettv.vval.v_list;
1517 }
1518
1519 if (p_verbose == 0)
1520 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001521 restore_vimvar(VV_VAL, &save_val);
1522
1523 return list;
1524}
1525
1526/*
1527 * "list" is supposed to contain two items: a word and a number. Return the
1528 * word in "pp" and the number as the return value.
1529 * Return -1 if anything isn't right.
1530 * Used to get the good word and score from the eval_spell_expr() result.
1531 */
1532 int
1533get_spellword(list, pp)
1534 list_T *list;
1535 char_u **pp;
1536{
1537 listitem_T *li;
1538
1539 li = list->lv_first;
1540 if (li == NULL)
1541 return -1;
1542 *pp = get_tv_string(&li->li_tv);
1543
1544 li = li->li_next;
1545 if (li == NULL)
1546 return -1;
1547 return get_tv_number(&li->li_tv);
1548}
1549#endif
1550
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001552 * Top level evaluation function.
1553 * Returns an allocated typval_T with the result.
1554 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 */
1556 typval_T *
1557eval_expr(arg, nextcmd)
1558 char_u *arg;
1559 char_u **nextcmd;
1560{
1561 typval_T *tv;
1562
1563 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001564 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001565 {
1566 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001567 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001568 }
1569
1570 return tv;
1571}
1572
1573
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001576 * Uses argv[argc] for the function arguments. Only Number and String
1577 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001580 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001581call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 char_u *func;
1583 int argc;
1584 char_u **argv;
1585 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001586 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588{
Bram Moolenaar33570922005-01-25 22:26:29 +00001589 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 long n;
1591 int len;
1592 int i;
1593 int doesrange;
1594 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001595 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001597 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001599 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 for (i = 0; i < argc; i++)
1602 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001603 /* Pass a NULL or empty argument as an empty string */
1604 if (argv[i] == NULL || *argv[i] == NUL)
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001608 continue;
1609 }
1610
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001611 if (str_arg_only)
1612 len = 0;
1613 else
1614 /* Recognize a number argument, the others must be strings. */
1615 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (len != 0 && len == (int)STRLEN(argv[i]))
1617 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001618 argvars[i].v_type = VAR_NUMBER;
1619 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 else
1622 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001623 argvars[i].v_type = VAR_STRING;
1624 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 }
1626 }
1627
1628 if (safe)
1629 {
1630 save_funccalp = save_funccal();
1631 ++sandbox;
1632 }
1633
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001634 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1635 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 if (safe)
1639 {
1640 --sandbox;
1641 restore_funccal(save_funccalp);
1642 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 vim_free(argvars);
1644
1645 if (ret == FAIL)
1646 clear_tv(rettv);
1647
1648 return ret;
1649}
1650
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001651/*
1652 * Call vimL function "func" and return the result as a number.
1653 * Returns -1 when calling the function fails.
1654 * Uses argv[argc] for the function arguments.
1655 */
1656 long
1657call_func_retnr(func, argc, argv, safe)
1658 char_u *func;
1659 int argc;
1660 char_u **argv;
1661 int safe; /* use the sandbox */
1662{
1663 typval_T rettv;
1664 long retval;
1665
1666 /* All arguments are passed as strings, no conversion to number. */
1667 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1668 return -1;
1669
1670 retval = get_tv_number_chk(&rettv, NULL);
1671 clear_tv(&rettv);
1672 return retval;
1673}
1674
1675#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1676 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1677
Bram Moolenaar4f688582007-07-24 12:34:30 +00001678# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001680 * Call vimL function "func" and return the result as a string.
1681 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 * Uses argv[argc] for the function arguments.
1683 */
1684 void *
1685call_func_retstr(func, argc, argv, safe)
1686 char_u *func;
1687 int argc;
1688 char_u **argv;
1689 int safe; /* use the sandbox */
1690{
1691 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001692 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001694 /* All arguments are passed as strings, no conversion to number. */
1695 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001696 return NULL;
1697
1698 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001699 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 return retval;
1701}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001702# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001704/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001705 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001706 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001707 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001708 */
1709 void *
1710call_func_retlist(func, argc, argv, safe)
1711 char_u *func;
1712 int argc;
1713 char_u **argv;
1714 int safe; /* use the sandbox */
1715{
1716 typval_T rettv;
1717
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001718 /* All arguments are passed as strings, no conversion to number. */
1719 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 return NULL;
1721
1722 if (rettv.v_type != VAR_LIST)
1723 {
1724 clear_tv(&rettv);
1725 return NULL;
1726 }
1727
1728 return rettv.vval.v_list;
1729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732/*
1733 * Save the current function call pointer, and set it to NULL.
1734 * Used when executing autocommands and for ":source".
1735 */
1736 void *
1737save_funccal()
1738{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001739 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 current_funccal = NULL;
1742 return (void *)fc;
1743}
1744
1745 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001746restore_funccal(vfc)
1747 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001749 funccall_T *fc = (funccall_T *)vfc;
1750
1751 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752}
1753
Bram Moolenaar05159a02005-02-26 23:04:13 +00001754#if defined(FEAT_PROFILE) || defined(PROTO)
1755/*
1756 * Prepare profiling for entering a child or something else that is not
1757 * counted for the script/function itself.
1758 * Should always be called in pair with prof_child_exit().
1759 */
1760 void
1761prof_child_enter(tm)
1762 proftime_T *tm; /* place to store waittime */
1763{
1764 funccall_T *fc = current_funccal;
1765
1766 if (fc != NULL && fc->func->uf_profiling)
1767 profile_start(&fc->prof_child);
1768 script_prof_save(tm);
1769}
1770
1771/*
1772 * Take care of time spent in a child.
1773 * Should always be called after prof_child_enter().
1774 */
1775 void
1776prof_child_exit(tm)
1777 proftime_T *tm; /* where waittime was stored */
1778{
1779 funccall_T *fc = current_funccal;
1780
1781 if (fc != NULL && fc->func->uf_profiling)
1782 {
1783 profile_end(&fc->prof_child);
1784 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1785 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1786 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1787 }
1788 script_prof_restore(tm);
1789}
1790#endif
1791
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_FOLDING
1794/*
1795 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1796 * it in "*cp". Doesn't give error messages.
1797 */
1798 int
1799eval_foldexpr(arg, cp)
1800 char_u *arg;
1801 int *cp;
1802{
Bram Moolenaar33570922005-01-25 22:26:29 +00001803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 int retval;
1805 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001806 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1807 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
1809 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001810 if (use_sandbox)
1811 ++sandbox;
1812 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 retval = 0;
1816 else
1817 {
1818 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 if (tv.v_type == VAR_NUMBER)
1820 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001821 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 retval = 0;
1823 else
1824 {
1825 /* If the result is a string, check if there is a non-digit before
1826 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if (!VIM_ISDIGIT(*s) && *s != '-')
1829 *cp = *s++;
1830 retval = atol((char *)s);
1831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001832 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001835 if (use_sandbox)
1836 --sandbox;
1837 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839 return retval;
1840}
1841#endif
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 * ":let" list all variable values
1845 * ":let var1 var2" list variable values
1846 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001847 * ":let var += expr" assignment command.
1848 * ":let var -= expr" assignment command.
1849 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001850 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 */
1852 void
1853ex_let(eap)
1854 exarg_T *eap;
1855{
1856 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001858 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 int var_count = 0;
1861 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001862 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001863 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaardb552d602006-03-23 22:59:57 +00001866 argend = skip_var_list(arg, &var_count, &semicolon);
1867 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001868 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001869 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1870 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001871 expr = skipwhite(argend);
1872 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1873 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001875 /*
1876 * ":let" without "=": list variables
1877 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001878 if (*arg == '[')
1879 EMSG(_(e_invarg));
1880 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001882 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001885 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001886 list_glob_vars(&first);
1887 list_buf_vars(&first);
1888 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001890 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001891#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001892 list_script_vars(&first);
1893 list_func_vars(&first);
1894 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 eap->nextcmd = check_nextcmd(arg);
1897 }
1898 else
1899 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op[0] = '=';
1901 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001902 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001904 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1905 op[0] = *expr; /* +=, -= or .= */
1906 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001908 else
1909 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (eap->skip)
1912 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 if (eap->skip)
1915 {
1916 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 --emsg_skip;
1919 }
1920 else if (i != FAIL)
1921 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001923 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 }
1926 }
1927}
1928
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929/*
1930 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1931 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 * When "nextchars" is not NULL it points to a string with characters that
1933 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1934 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 * Returns OK or FAIL;
1936 */
1937 static int
1938ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1939 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001940 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 int copy; /* copy values from "tv", don't move */
1942 int semicolon; /* from skip_var_list() */
1943 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945{
1946 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001947 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001948 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001949 listitem_T *item;
1950 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951
1952 if (*arg != '[')
1953 {
1954 /*
1955 * ":let var = expr" or ":for var in list"
1956 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 return OK;
1960 }
1961
1962 /*
1963 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1964 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001965 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 {
1967 EMSG(_(e_listreq));
1968 return FAIL;
1969 }
1970
1971 i = list_len(l);
1972 if (semicolon == 0 && var_count < i)
1973 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001974 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975 return FAIL;
1976 }
1977 if (var_count - semicolon > i)
1978 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001979 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 return FAIL;
1981 }
1982
1983 item = l->lv_first;
1984 while (*arg != ']')
1985 {
1986 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 item = item->li_next;
1989 if (arg == NULL)
1990 return FAIL;
1991
1992 arg = skipwhite(arg);
1993 if (*arg == ';')
1994 {
1995 /* Put the rest of the list (may be empty) in the var after ';'.
1996 * Create a new list for this. */
1997 l = list_alloc();
1998 if (l == NULL)
1999 return FAIL;
2000 while (item != NULL)
2001 {
2002 list_append_tv(l, &item->li_tv);
2003 item = item->li_next;
2004 }
2005
2006 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002007 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 ltv.vval.v_list = l;
2009 l->lv_refcount = 1;
2010
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2012 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 clear_tv(&ltv);
2014 if (arg == NULL)
2015 return FAIL;
2016 break;
2017 }
2018 else if (*arg != ',' && *arg != ']')
2019 {
2020 EMSG2(_(e_intern2), "ex_let_vars()");
2021 return FAIL;
2022 }
2023 }
2024
2025 return OK;
2026}
2027
2028/*
2029 * Skip over assignable variable "var" or list of variables "[var, var]".
2030 * Used for ":let varvar = expr" and ":for varvar in expr".
2031 * For "[var, var]" increment "*var_count" for each variable.
2032 * for "[var, var; var]" set "semicolon".
2033 * Return NULL for an error.
2034 */
2035 static char_u *
2036skip_var_list(arg, var_count, semicolon)
2037 char_u *arg;
2038 int *var_count;
2039 int *semicolon;
2040{
2041 char_u *p, *s;
2042
2043 if (*arg == '[')
2044 {
2045 /* "[var, var]": find the matching ']'. */
2046 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002047 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 {
2049 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2050 s = skip_var_one(p);
2051 if (s == p)
2052 {
2053 EMSG2(_(e_invarg2), p);
2054 return NULL;
2055 }
2056 ++*var_count;
2057
2058 p = skipwhite(s);
2059 if (*p == ']')
2060 break;
2061 else if (*p == ';')
2062 {
2063 if (*semicolon == 1)
2064 {
2065 EMSG(_("Double ; in list of variables"));
2066 return NULL;
2067 }
2068 *semicolon = 1;
2069 }
2070 else if (*p != ',')
2071 {
2072 EMSG2(_(e_invarg2), p);
2073 return NULL;
2074 }
2075 }
2076 return p + 1;
2077 }
2078 else
2079 return skip_var_one(arg);
2080}
2081
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002082/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002083 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002084 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002085 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 static char_u *
2087skip_var_one(arg)
2088 char_u *arg;
2089{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002090 if (*arg == '@' && arg[1] != NUL)
2091 return arg + 2;
2092 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2093 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094}
2095
Bram Moolenaara7043832005-01-21 11:56:39 +00002096/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 * List variables for hashtab "ht" with prefix "prefix".
2098 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002101list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002102 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002103 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002105 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002106{
Bram Moolenaar33570922005-01-25 22:26:29 +00002107 hashitem_T *hi;
2108 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109 int todo;
2110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2113 {
2114 if (!HASHITEM_EMPTY(hi))
2115 {
2116 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002117 di = HI2DI(hi);
2118 if (empty || di->di_tv.v_type != VAR_STRING
2119 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002120 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121 }
2122 }
2123}
2124
2125/*
2126 * List global variables.
2127 */
2128 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129list_glob_vars(first)
2130 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002131{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002133}
2134
2135/*
2136 * List buffer variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_buf_vars(first)
2140 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002141{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002142 char_u numbuf[NUMBUFLEN];
2143
Bram Moolenaar429fa852013-04-15 12:27:36 +02002144 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146
2147 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2149 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002150}
2151
2152/*
2153 * List window variables.
2154 */
2155 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156list_win_vars(first)
2157 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002158{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002159 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161}
2162
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002163#ifdef FEAT_WINDOWS
2164/*
2165 * List tab page variables.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_tab_vars(first)
2169 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002170{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002171 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002173}
2174#endif
2175
Bram Moolenaara7043832005-01-21 11:56:39 +00002176/*
2177 * List Vim variables.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_vim_vars(first)
2181 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184}
2185
2186/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002187 * List script-local variables, if there is a script.
2188 */
2189 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190list_script_vars(first)
2191 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192{
2193 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2195 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002196}
2197
2198/*
2199 * List function variables, if there is a function.
2200 */
2201 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202list_func_vars(first)
2203 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002204{
2205 if (current_funccal != NULL)
2206 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002208}
2209
2210/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * List variables in "arg".
2212 */
2213 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002214list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 exarg_T *eap;
2216 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002217 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218{
2219 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002222 char_u *name_start;
2223 char_u *arg_subsc;
2224 char_u *tofree;
2225 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226
2227 while (!ends_excmd(*arg) && !got_int)
2228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002229 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002231 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002232 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2233 {
2234 emsg_severe = TRUE;
2235 EMSG(_(e_trailing));
2236 break;
2237 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002239 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* get_name_len() takes care of expanding curly braces */
2242 name_start = name = arg;
2243 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2244 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002246 /* This is mainly to keep test 49 working: when expanding
2247 * curly braces fails overrule the exception error message. */
2248 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 emsg_severe = TRUE;
2251 EMSG2(_(e_invarg2), arg);
2252 break;
2253 }
2254 error = TRUE;
2255 }
2256 else
2257 {
2258 if (tofree != NULL)
2259 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002260 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262 else
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 /* handle d.key, l[idx], f(expr) */
2265 arg_subsc = arg;
2266 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002267 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002270 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002271 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002273 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002274 case 'g': list_glob_vars(first); break;
2275 case 'b': list_buf_vars(first); break;
2276 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002277#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002278 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002280 case 'v': list_vim_vars(first); break;
2281 case 's': list_script_vars(first); break;
2282 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 default:
2284 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002285 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
2287 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002288 {
2289 char_u numbuf[NUMBUFLEN];
2290 char_u *tf;
2291 int c;
2292 char_u *s;
2293
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002294 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 c = *arg;
2296 *arg = NUL;
2297 list_one_var_a((char_u *)"",
2298 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002299 tv.v_type,
2300 s == NULL ? (char_u *)"" : s,
2301 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 *arg = c;
2303 vim_free(tf);
2304 }
2305 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 }
2308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309
2310 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312
2313 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 }
2315
2316 return arg;
2317}
2318
2319/*
2320 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2321 * Returns a pointer to the char just after the var name.
2322 * Returns NULL if there is an error.
2323 */
2324 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002325ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002327 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331{
2332 int c1;
2333 char_u *name;
2334 char_u *p;
2335 char_u *arg_end = NULL;
2336 int len;
2337 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002338 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339
2340 /*
2341 * ":let $VAR = expr": Set environment variable.
2342 */
2343 if (*arg == '$')
2344 {
2345 /* Find the end of the name. */
2346 ++arg;
2347 name = arg;
2348 len = get_env_len(&arg);
2349 if (len == 0)
2350 EMSG2(_(e_invarg2), name - 1);
2351 else
2352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002353 if (op != NULL && (*op == '+' || *op == '-'))
2354 EMSG2(_(e_letwrong), op);
2355 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002356 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002358 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 {
2360 c1 = name[len];
2361 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 p = get_tv_string_chk(tv);
2363 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002364 {
2365 int mustfree = FALSE;
2366 char_u *s = vim_getenv(name, &mustfree);
2367
2368 if (s != NULL)
2369 {
2370 p = tofree = concat_str(s, p);
2371 if (mustfree)
2372 vim_free(s);
2373 }
2374 }
2375 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002378 if (STRICMP(name, "HOME") == 0)
2379 init_homedir();
2380 else if (didset_vim && STRICMP(name, "VIM") == 0)
2381 didset_vim = FALSE;
2382 else if (didset_vimruntime
2383 && STRICMP(name, "VIMRUNTIME") == 0)
2384 didset_vimruntime = FALSE;
2385 arg_end = arg;
2386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 }
2390 }
2391 }
2392
2393 /*
2394 * ":let &option = expr": Set option value.
2395 * ":let &l:option = expr": Set local option value.
2396 * ":let &g:option = expr": Set global option value.
2397 */
2398 else if (*arg == '&')
2399 {
2400 /* Find the end of the name. */
2401 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002402 if (p == NULL || (endchars != NULL
2403 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002404 EMSG(_(e_letunexp));
2405 else
2406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 long n;
2408 int opt_type;
2409 long numval;
2410 char_u *stringval = NULL;
2411 char_u *s;
2412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002413 c1 = *p;
2414 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415
2416 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002417 s = get_tv_string_chk(tv); /* != NULL if number or string */
2418 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 {
2420 opt_type = get_option_value(arg, &numval,
2421 &stringval, opt_flags);
2422 if ((opt_type == 1 && *op == '.')
2423 || (opt_type == 0 && *op != '.'))
2424 EMSG2(_(e_letwrong), op);
2425 else
2426 {
2427 if (opt_type == 1) /* number */
2428 {
2429 if (*op == '+')
2430 n = numval + n;
2431 else
2432 n = numval - n;
2433 }
2434 else if (opt_type == 0 && stringval != NULL) /* string */
2435 {
2436 s = concat_str(stringval, s);
2437 vim_free(stringval);
2438 stringval = s;
2439 }
2440 }
2441 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002442 if (s != NULL)
2443 {
2444 set_option_value(arg, n, s, opt_flags);
2445 arg_end = p;
2446 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 }
2450 }
2451
2452 /*
2453 * ":let @r = expr": Set register contents.
2454 */
2455 else if (*arg == '@')
2456 {
2457 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 if (op != NULL && (*op == '+' || *op == '-'))
2459 EMSG2(_(e_letwrong), op);
2460 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 EMSG(_(e_letunexp));
2463 else
2464 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002466 char_u *s;
2467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 p = get_tv_string_chk(tv);
2469 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 if (s != NULL)
2473 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002474 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(s);
2476 }
2477 }
2478 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002481 arg_end = arg + 1;
2482 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002483 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002489 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002490 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002491 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002493 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002494
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002495 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002496 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2499 EMSG(_(e_letunexp));
2500 else
2501 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 arg_end = p;
2504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002507 }
2508
2509 else
2510 EMSG2(_(e_invarg2), arg);
2511
2512 return arg_end;
2513}
2514
2515/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002516 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2517 */
2518 static int
2519check_changedtick(arg)
2520 char_u *arg;
2521{
2522 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2523 {
2524 EMSG2(_(e_readonlyvar), arg);
2525 return TRUE;
2526 }
2527 return FALSE;
2528}
2529
2530/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 * Get an lval: variable, Dict item or List item that can be assigned a value
2532 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2533 * "name.key", "name.key[expr]" etc.
2534 * Indexing only works if "name" is an existing List or Dictionary.
2535 * "name" points to the start of the name.
2536 * If "rettv" is not NULL it points to the value to be assigned.
2537 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2538 * wrong; must end in space or cmd separator.
2539 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002540 * flags:
2541 * GLV_QUIET: do not give error messages
2542 * GLV_NO_AUTOLOAD: do not use script autoloading
2543 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002545 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 */
2548 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002549get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002551 typval_T *rettv;
2552 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002553 int unlet;
2554 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002555 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002556 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 char_u *expr_start, *expr_end;
2560 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002561 dictitem_T *v;
2562 typval_T var1;
2563 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002566 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002567 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002568 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002571 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002572 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573
2574 if (skip)
2575 {
2576 /* When skipping just find the end of the name. */
2577 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002578 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 }
2580
2581 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002582 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (expr_start != NULL)
2584 {
2585 /* Don't expand the name when we already know there is an error. */
2586 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2587 && *p != '[' && *p != '.')
2588 {
2589 EMSG(_(e_trailing));
2590 return NULL;
2591 }
2592
2593 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2594 if (lp->ll_exp_name == NULL)
2595 {
2596 /* Report an invalid expression in braces, unless the
2597 * expression evaluation has been cancelled due to an
2598 * aborting error, an interrupt, or an exception. */
2599 if (!aborting() && !quiet)
2600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002601 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 EMSG2(_(e_invarg2), name);
2603 return NULL;
2604 }
2605 }
2606 lp->ll_name = lp->ll_exp_name;
2607 }
2608 else
2609 lp->ll_name = name;
2610
2611 /* Without [idx] or .key we are done. */
2612 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2613 return p;
2614
2615 cc = *p;
2616 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002617 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (v == NULL && !quiet)
2619 EMSG2(_(e_undefvar), lp->ll_name);
2620 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 if (v == NULL)
2622 return NULL;
2623
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 /*
2625 * Loop until no more [idx] or .key is following.
2626 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002629 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002630 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2631 && !(lp->ll_tv->v_type == VAR_DICT
2632 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 if (!quiet)
2635 EMSG(_("E689: Can only index a List or Dictionary"));
2636 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (!quiet)
2641 EMSG(_("E708: [:] must come last"));
2642 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002644
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 len = -1;
2646 if (*p == '.')
2647 {
2648 key = p + 1;
2649 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2650 ;
2651 if (len == 0)
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (!quiet)
2654 EMSG(_(e_emptykey));
2655 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 }
2657 p = key + len;
2658 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002659 else
2660 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002661 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002662 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 if (*p == ':')
2664 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002665 else
2666 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 empty1 = FALSE;
2668 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002670 if (get_tv_string_chk(&var1) == NULL)
2671 {
2672 /* not a number or string */
2673 clear_tv(&var1);
2674 return NULL;
2675 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 }
2677
2678 /* Optionally get the second index [ :expr]. */
2679 if (*p == ':')
2680 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002684 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 if (!empty1)
2686 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 if (rettv != NULL && (rettv->v_type != VAR_LIST
2690 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 }
2698 p = skipwhite(p + 1);
2699 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 else
2702 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 if (!empty1)
2707 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 if (get_tv_string_chk(&var2) == NULL)
2711 {
2712 /* not a number or string */
2713 if (!empty1)
2714 clear_tv(&var1);
2715 clear_tv(&var2);
2716 return NULL;
2717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 }
2734
2735 /* Skip to past ']'. */
2736 ++p;
2737 }
2738
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 {
2741 if (len == -1)
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (*key == NUL)
2746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 if (!quiet)
2748 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
2752 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002753 lp->ll_list = NULL;
2754 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002755 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002757 /* When assigning to a scope dictionary check that a function and
2758 * variable name is valid (only variable name unless it is l: or
2759 * g: dictionary). Disallow overwriting a builtin function. */
2760 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002761 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002762 int prevval;
2763 int wrong;
2764
2765 if (len != -1)
2766 {
2767 prevval = key[len];
2768 key[len] = NUL;
2769 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002770 else
2771 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002772 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2773 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002774 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002775 || !valid_varname(key);
2776 if (len != -1)
2777 key[len] = prevval;
2778 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002779 return NULL;
2780 }
2781
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002784 /* Can't add "v:" variable. */
2785 if (lp->ll_dict == &vimvardict)
2786 {
2787 EMSG2(_(e_illvar), name);
2788 return NULL;
2789 }
2790
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002791 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002795 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002796 if (len == -1)
2797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 }
2800 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002804 if (len == -1)
2805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 p = NULL;
2808 break;
2809 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* existing variable, need to check if it can be changed */
2811 else if (var_check_ro(lp->ll_di->di_flags, name))
2812 return NULL;
2813
Bram Moolenaar8c711452005-01-14 21:53:12 +00002814 if (len == -1)
2815 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 }
2818 else
2819 {
2820 /*
2821 * Get the number and item for the only or first index of the List.
2822 */
2823 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 else
2826 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002827 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var1);
2829 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 lp->ll_list = lp->ll_tv->vval.v_list;
2832 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2833 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002835 if (lp->ll_n1 < 0)
2836 {
2837 lp->ll_n1 = 0;
2838 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2839 }
2840 }
2841 if (lp->ll_li == NULL)
2842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002843 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002844 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002845 if (!quiet)
2846 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 }
2849
2850 /*
2851 * May need to find the item or absolute index for the second
2852 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 * When no index given: "lp->ll_empty2" is TRUE.
2854 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002858 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 {
2865 if (!quiet)
2866 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002868 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 }
2871
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2873 if (lp->ll_n1 < 0)
2874 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2875 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002876 {
2877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002880 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002881 }
2882
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 }
2886
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 return p;
2888}
2889
2890/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002891 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 */
2893 static void
2894clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002895 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896{
2897 vim_free(lp->ll_exp_name);
2898 vim_free(lp->ll_newkey);
2899}
2900
2901/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002902 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 */
2906 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002908 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002912 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913{
2914 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 listitem_T *ri;
2916 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917
2918 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 cc = *endp;
2923 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 if (op != NULL && *op != '=')
2925 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002926 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002927
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002928 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002929 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002930 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002931 {
2932 if (tv_op(&tv, rettv, op) == OK)
2933 set_var(lp->ll_name, &tv, FALSE);
2934 clear_tv(&tv);
2935 }
2936 }
2937 else
2938 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002940 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002942 else if (tv_check_lock(lp->ll_newkey == NULL
2943 ? lp->ll_tv->v_lock
2944 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2945 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 else if (lp->ll_range)
2947 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002948 listitem_T *ll_li = lp->ll_li;
2949 int ll_n1 = lp->ll_n1;
2950
2951 /*
2952 * Check whether any of the list items is locked
2953 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002954 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002955 {
2956 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name))
2957 return;
2958 ri = ri->li_next;
2959 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2960 break;
2961 ll_li = ll_li->li_next;
2962 ++ll_n1;
2963 }
2964
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002965 /*
2966 * Assign the List values to the list items.
2967 */
2968 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002969 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 if (op != NULL && *op != '=')
2971 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2972 else
2973 {
2974 clear_tv(&lp->ll_li->li_tv);
2975 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 ri = ri->li_next;
2978 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2979 break;
2980 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002981 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002983 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 ri = NULL;
2986 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002987 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 lp->ll_li = lp->ll_li->li_next;
2990 ++lp->ll_n1;
2991 }
2992 if (ri != NULL)
2993 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002994 else if (lp->ll_empty2
2995 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 : lp->ll_n1 != lp->ll_n2)
2997 EMSG(_("E711: List value has not enough items"));
2998 }
2999 else
3000 {
3001 /*
3002 * Assign to a List or Dictionary item.
3003 */
3004 if (lp->ll_newkey != NULL)
3005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 if (op != NULL && *op != '=')
3007 {
3008 EMSG2(_(e_letwrong), op);
3009 return;
3010 }
3011
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003013 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 if (di == NULL)
3015 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003016 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3017 {
3018 vim_free(di);
3019 return;
3020 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003022 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003023 else if (op != NULL && *op != '=')
3024 {
3025 tv_op(lp->ll_tv, rettv, op);
3026 return;
3027 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003028 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003030
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 /*
3032 * Assign the value to the variable or list item.
3033 */
3034 if (copy)
3035 copy_tv(rettv, lp->ll_tv);
3036 else
3037 {
3038 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003039 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 }
3042 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003043}
3044
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003045/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3047 * Returns OK or FAIL.
3048 */
3049 static int
3050tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003051 typval_T *tv1;
3052 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 char_u *op;
3054{
3055 long n;
3056 char_u numbuf[NUMBUFLEN];
3057 char_u *s;
3058
3059 /* Can't do anything with a Funcref or a Dict on the right. */
3060 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3061 {
3062 switch (tv1->v_type)
3063 {
3064 case VAR_DICT:
3065 case VAR_FUNC:
3066 break;
3067
3068 case VAR_LIST:
3069 if (*op != '+' || tv2->v_type != VAR_LIST)
3070 break;
3071 /* List += List */
3072 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3073 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3074 return OK;
3075
3076 case VAR_NUMBER:
3077 case VAR_STRING:
3078 if (tv2->v_type == VAR_LIST)
3079 break;
3080 if (*op == '+' || *op == '-')
3081 {
3082 /* nr += nr or nr -= nr*/
3083 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003084#ifdef FEAT_FLOAT
3085 if (tv2->v_type == VAR_FLOAT)
3086 {
3087 float_T f = n;
3088
3089 if (*op == '+')
3090 f += tv2->vval.v_float;
3091 else
3092 f -= tv2->vval.v_float;
3093 clear_tv(tv1);
3094 tv1->v_type = VAR_FLOAT;
3095 tv1->vval.v_float = f;
3096 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003098#endif
3099 {
3100 if (*op == '+')
3101 n += get_tv_number(tv2);
3102 else
3103 n -= get_tv_number(tv2);
3104 clear_tv(tv1);
3105 tv1->v_type = VAR_NUMBER;
3106 tv1->vval.v_number = n;
3107 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003108 }
3109 else
3110 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003111 if (tv2->v_type == VAR_FLOAT)
3112 break;
3113
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114 /* str .= str */
3115 s = get_tv_string(tv1);
3116 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3117 clear_tv(tv1);
3118 tv1->v_type = VAR_STRING;
3119 tv1->vval.v_string = s;
3120 }
3121 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122
3123#ifdef FEAT_FLOAT
3124 case VAR_FLOAT:
3125 {
3126 float_T f;
3127
3128 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3129 && tv2->v_type != VAR_NUMBER
3130 && tv2->v_type != VAR_STRING))
3131 break;
3132 if (tv2->v_type == VAR_FLOAT)
3133 f = tv2->vval.v_float;
3134 else
3135 f = get_tv_number(tv2);
3136 if (*op == '+')
3137 tv1->vval.v_float += f;
3138 else
3139 tv1->vval.v_float -= f;
3140 }
3141 return OK;
3142#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 }
3144 }
3145
3146 EMSG2(_(e_letwrong), op);
3147 return FAIL;
3148}
3149
3150/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 * Add a watcher to a list.
3152 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003153 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003155 list_T *l;
3156 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003157{
3158 lw->lw_next = l->lv_watch;
3159 l->lv_watch = lw;
3160}
3161
3162/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003163 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003164 * No warning when it isn't found...
3165 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003166 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003167list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003168 list_T *l;
3169 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170{
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003172
3173 lwp = &l->lv_watch;
3174 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3175 {
3176 if (lw == lwrem)
3177 {
3178 *lwp = lw->lw_next;
3179 break;
3180 }
3181 lwp = &lw->lw_next;
3182 }
3183}
3184
3185/*
3186 * Just before removing an item from a list: advance watchers to the next
3187 * item.
3188 */
3189 static void
3190list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003191 list_T *l;
3192 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
Bram Moolenaar33570922005-01-25 22:26:29 +00003194 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195
3196 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3197 if (lw->lw_item == item)
3198 lw->lw_item = item->li_next;
3199}
3200
3201/*
3202 * Evaluate the expression used in a ":for var in expr" command.
3203 * "arg" points to "var".
3204 * Set "*errp" to TRUE for an error, FALSE otherwise;
3205 * Return a pointer that holds the info. Null when there is an error.
3206 */
3207 void *
3208eval_for_line(arg, errp, nextcmdp, skip)
3209 char_u *arg;
3210 int *errp;
3211 char_u **nextcmdp;
3212 int skip;
3213{
Bram Moolenaar33570922005-01-25 22:26:29 +00003214 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003215 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003216 typval_T tv;
3217 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218
3219 *errp = TRUE; /* default: there is an error */
3220
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 if (fi == NULL)
3223 return NULL;
3224
3225 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3226 if (expr == NULL)
3227 return fi;
3228
3229 expr = skipwhite(expr);
3230 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3231 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003232 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 return fi;
3234 }
3235
3236 if (skip)
3237 ++emsg_skip;
3238 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3239 {
3240 *errp = FALSE;
3241 if (!skip)
3242 {
3243 l = tv.vval.v_list;
3244 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003245 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003247 clear_tv(&tv);
3248 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 else
3250 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003251 /* No need to increment the refcount, it's already set for the
3252 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253 fi->fi_list = l;
3254 list_add_watch(l, &fi->fi_lw);
3255 fi->fi_lw.lw_item = l->lv_first;
3256 }
3257 }
3258 }
3259 if (skip)
3260 --emsg_skip;
3261
3262 return fi;
3263}
3264
3265/*
3266 * Use the first item in a ":for" list. Advance to the next.
3267 * Assign the values to the variable (list). "arg" points to the first one.
3268 * Return TRUE when a valid item was found, FALSE when at end of list or
3269 * something wrong.
3270 */
3271 int
3272next_for_item(fi_void, arg)
3273 void *fi_void;
3274 char_u *arg;
3275{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003276 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003278 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279
3280 item = fi->fi_lw.lw_item;
3281 if (item == NULL)
3282 result = FALSE;
3283 else
3284 {
3285 fi->fi_lw.lw_item = item->li_next;
3286 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3287 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3288 }
3289 return result;
3290}
3291
3292/*
3293 * Free the structure used to store info used by ":for".
3294 */
3295 void
3296free_for_info(fi_void)
3297 void *fi_void;
3298{
Bram Moolenaar33570922005-01-25 22:26:29 +00003299 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003301 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003302 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003304 list_unref(fi->fi_list);
3305 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 vim_free(fi);
3307}
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3310
3311 void
3312set_context_for_expression(xp, arg, cmdidx)
3313 expand_T *xp;
3314 char_u *arg;
3315 cmdidx_T cmdidx;
3316{
3317 int got_eq = FALSE;
3318 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321 if (cmdidx == CMD_let)
3322 {
3323 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003324 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325 {
3326 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003327 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328 {
3329 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 if (vim_iswhite(*p))
3332 break;
3333 }
3334 return;
3335 }
3336 }
3337 else
3338 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3339 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 while ((xp->xp_pattern = vim_strpbrk(arg,
3341 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3342 {
3343 c = *xp->xp_pattern;
3344 if (c == '&')
3345 {
3346 c = xp->xp_pattern[1];
3347 if (c == '&')
3348 {
3349 ++xp->xp_pattern;
3350 xp->xp_context = cmdidx != CMD_let || got_eq
3351 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3352 }
3353 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003356 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3357 xp->xp_pattern += 2;
3358
3359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
3361 else if (c == '$')
3362 {
3363 /* environment variable */
3364 xp->xp_context = EXPAND_ENV_VARS;
3365 }
3366 else if (c == '=')
3367 {
3368 got_eq = TRUE;
3369 xp->xp_context = EXPAND_EXPRESSION;
3370 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003371 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 && xp->xp_context == EXPAND_FUNCTIONS
3373 && vim_strchr(xp->xp_pattern, '(') == NULL)
3374 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003375 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 break;
3377 }
3378 else if (cmdidx != CMD_let || got_eq)
3379 {
3380 if (c == '"') /* string */
3381 {
3382 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3383 if (c == '\\' && xp->xp_pattern[1] != NUL)
3384 ++xp->xp_pattern;
3385 xp->xp_context = EXPAND_NOTHING;
3386 }
3387 else if (c == '\'') /* literal string */
3388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003389 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3391 /* skip */ ;
3392 xp->xp_context = EXPAND_NOTHING;
3393 }
3394 else if (c == '|')
3395 {
3396 if (xp->xp_pattern[1] == '|')
3397 {
3398 ++xp->xp_pattern;
3399 xp->xp_context = EXPAND_EXPRESSION;
3400 }
3401 else
3402 xp->xp_context = EXPAND_COMMANDS;
3403 }
3404 else
3405 xp->xp_context = EXPAND_EXPRESSION;
3406 }
3407 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003408 /* Doesn't look like something valid, expand as an expression
3409 * anyway. */
3410 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 arg = xp->xp_pattern;
3412 if (*arg != NUL)
3413 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3414 /* skip */ ;
3415 }
3416 xp->xp_pattern = arg;
3417}
3418
3419#endif /* FEAT_CMDL_COMPL */
3420
3421/*
3422 * ":1,25call func(arg1, arg2)" function call.
3423 */
3424 void
3425ex_call(eap)
3426 exarg_T *eap;
3427{
3428 char_u *arg = eap->arg;
3429 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003431 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003433 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 linenr_T lnum;
3435 int doesrange;
3436 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003439 if (eap->skip)
3440 {
3441 /* trans_function_name() doesn't work well when skipping, use eval0()
3442 * instead to skip to any following command, e.g. for:
3443 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003444 ++emsg_skip;
3445 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3446 clear_tv(&rettv);
3447 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003448 return;
3449 }
3450
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003451 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003452 if (fudi.fd_newkey != NULL)
3453 {
3454 /* Still need to give an error message for missing key. */
3455 EMSG2(_(e_dictkey), fudi.fd_newkey);
3456 vim_free(fudi.fd_newkey);
3457 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003458 if (tofree == NULL)
3459 return;
3460
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003461 /* Increase refcount on dictionary, it could get deleted when evaluating
3462 * the arguments. */
3463 if (fudi.fd_dict != NULL)
3464 ++fudi.fd_dict->dv_refcount;
3465
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003466 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003467 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003468 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
Bram Moolenaar532c7802005-01-27 14:44:31 +00003470 /* Skip white space to allow ":call func ()". Not good, but required for
3471 * backward compatibility. */
3472 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003473 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 if (*startarg != '(')
3476 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003477 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 goto end;
3479 }
3480
3481 /*
3482 * When skipping, evaluate the function once, to find the end of the
3483 * arguments.
3484 * When the function takes a range, this is discovered after the first
3485 * call, and the loop is broken.
3486 */
3487 if (eap->skip)
3488 {
3489 ++emsg_skip;
3490 lnum = eap->line2; /* do it once, also with an invalid range */
3491 }
3492 else
3493 lnum = eap->line1;
3494 for ( ; lnum <= eap->line2; ++lnum)
3495 {
3496 if (!eap->skip && eap->addr_count > 0)
3497 {
3498 curwin->w_cursor.lnum = lnum;
3499 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003500#ifdef FEAT_VIRTUALEDIT
3501 curwin->w_cursor.coladd = 0;
3502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003505 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003506 eap->line1, eap->line2, &doesrange,
3507 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 failed = TRUE;
3510 break;
3511 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003512
3513 /* Handle a function returning a Funcref, Dictionary or List. */
3514 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3515 {
3516 failed = TRUE;
3517 break;
3518 }
3519
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003520 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (doesrange || eap->skip)
3522 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003523
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003525 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003526 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003527 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (aborting())
3529 break;
3530 }
3531 if (eap->skip)
3532 --emsg_skip;
3533
3534 if (!failed)
3535 {
3536 /* Check for trailing illegal characters and a following command. */
3537 if (!ends_excmd(*arg))
3538 {
3539 emsg_severe = TRUE;
3540 EMSG(_(e_trailing));
3541 }
3542 else
3543 eap->nextcmd = check_nextcmd(arg);
3544 }
3545
3546end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003547 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003548 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549}
3550
3551/*
3552 * ":unlet[!] var1 ... " command.
3553 */
3554 void
3555ex_unlet(eap)
3556 exarg_T *eap;
3557{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003558 ex_unletlock(eap, eap->arg, 0);
3559}
3560
3561/*
3562 * ":lockvar" and ":unlockvar" commands
3563 */
3564 void
3565ex_lockvar(eap)
3566 exarg_T *eap;
3567{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003569 int deep = 2;
3570
3571 if (eap->forceit)
3572 deep = -1;
3573 else if (vim_isdigit(*arg))
3574 {
3575 deep = getdigits(&arg);
3576 arg = skipwhite(arg);
3577 }
3578
3579 ex_unletlock(eap, arg, deep);
3580}
3581
3582/*
3583 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3584 */
3585 static void
3586ex_unletlock(eap, argstart, deep)
3587 exarg_T *eap;
3588 char_u *argstart;
3589 int deep;
3590{
3591 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003594 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595
3596 do
3597 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003599 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003600 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 if (lv.ll_name == NULL)
3602 error = TRUE; /* error but continue parsing */
3603 if (name_end == NULL || (!vim_iswhite(*name_end)
3604 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 if (name_end != NULL)
3607 {
3608 emsg_severe = TRUE;
3609 EMSG(_(e_trailing));
3610 }
3611 if (!(eap->skip || error))
3612 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 break;
3614 }
3615
3616 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 {
3618 if (eap->cmdidx == CMD_unlet)
3619 {
3620 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3621 error = TRUE;
3622 }
3623 else
3624 {
3625 if (do_lock_var(&lv, name_end, deep,
3626 eap->cmdidx == CMD_lockvar) == FAIL)
3627 error = TRUE;
3628 }
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003631 if (!eap->skip)
3632 clear_lval(&lv);
3633
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 arg = skipwhite(name_end);
3635 } while (!ends_excmd(*arg));
3636
3637 eap->nextcmd = check_nextcmd(arg);
3638}
3639
Bram Moolenaar8c711452005-01-14 21:53:12 +00003640 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003641do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003642 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003643 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003644 int forceit;
3645{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 int ret = OK;
3647 int cc;
3648
3649 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 cc = *name_end;
3652 *name_end = NUL;
3653
3654 /* Normal name or expanded name. */
3655 if (check_changedtick(lp->ll_name))
3656 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003657 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3662 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003663 else if (lp->ll_range)
3664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003666 listitem_T *ll_li = lp->ll_li;
3667 int ll_n1 = lp->ll_n1;
3668
3669 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3670 {
3671 li = ll_li->li_next;
3672 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name))
3673 return FAIL;
3674 ll_li = li;
3675 ++ll_n1;
3676 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677
3678 /* Delete a range of List items. */
3679 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3680 {
3681 li = lp->ll_li->li_next;
3682 listitem_remove(lp->ll_list, lp->ll_li);
3683 lp->ll_li = li;
3684 ++lp->ll_n1;
3685 }
3686 }
3687 else
3688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 /* unlet a List item. */
3691 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003694 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 }
3696
3697 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003698}
3699
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700/*
3701 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003702 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 */
3704 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003705do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003707 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
Bram Moolenaar33570922005-01-25 22:26:29 +00003709 hashtab_T *ht;
3710 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003711 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003712 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
Bram Moolenaar33570922005-01-25 22:26:29 +00003714 ht = find_var_ht(name, &varname);
3715 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 hi = hash_find(ht, varname);
3718 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003719 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003720 di = HI2DI(hi);
3721 if (var_check_fixed(di->di_flags, name)
3722 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003723 return FAIL;
3724 delete_var(ht, hi);
3725 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003728 if (forceit)
3729 return OK;
3730 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 return FAIL;
3732}
3733
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734/*
3735 * Lock or unlock variable indicated by "lp".
3736 * "deep" is the levels to go (-1 for unlimited);
3737 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3738 */
3739 static int
3740do_lock_var(lp, name_end, deep, lock)
3741 lval_T *lp;
3742 char_u *name_end;
3743 int deep;
3744 int lock;
3745{
3746 int ret = OK;
3747 int cc;
3748 dictitem_T *di;
3749
3750 if (deep == 0) /* nothing to do */
3751 return OK;
3752
3753 if (lp->ll_tv == NULL)
3754 {
3755 cc = *name_end;
3756 *name_end = NUL;
3757
3758 /* Normal name or expanded name. */
3759 if (check_changedtick(lp->ll_name))
3760 ret = FAIL;
3761 else
3762 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003763 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003764 if (di == NULL)
3765 ret = FAIL;
3766 else
3767 {
3768 if (lock)
3769 di->di_flags |= DI_FLAGS_LOCK;
3770 else
3771 di->di_flags &= ~DI_FLAGS_LOCK;
3772 item_lock(&di->di_tv, deep, lock);
3773 }
3774 }
3775 *name_end = cc;
3776 }
3777 else if (lp->ll_range)
3778 {
3779 listitem_T *li = lp->ll_li;
3780
3781 /* (un)lock a range of List items. */
3782 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3783 {
3784 item_lock(&li->li_tv, deep, lock);
3785 li = li->li_next;
3786 ++lp->ll_n1;
3787 }
3788 }
3789 else if (lp->ll_list != NULL)
3790 /* (un)lock a List item. */
3791 item_lock(&lp->ll_li->li_tv, deep, lock);
3792 else
3793 /* un(lock) a Dictionary item. */
3794 item_lock(&lp->ll_di->di_tv, deep, lock);
3795
3796 return ret;
3797}
3798
3799/*
3800 * Lock or unlock an item. "deep" is nr of levels to go.
3801 */
3802 static void
3803item_lock(tv, deep, lock)
3804 typval_T *tv;
3805 int deep;
3806 int lock;
3807{
3808 static int recurse = 0;
3809 list_T *l;
3810 listitem_T *li;
3811 dict_T *d;
3812 hashitem_T *hi;
3813 int todo;
3814
3815 if (recurse >= DICT_MAXNEST)
3816 {
3817 EMSG(_("E743: variable nested too deep for (un)lock"));
3818 return;
3819 }
3820 if (deep == 0)
3821 return;
3822 ++recurse;
3823
3824 /* lock/unlock the item itself */
3825 if (lock)
3826 tv->v_lock |= VAR_LOCKED;
3827 else
3828 tv->v_lock &= ~VAR_LOCKED;
3829
3830 switch (tv->v_type)
3831 {
3832 case VAR_LIST:
3833 if ((l = tv->vval.v_list) != NULL)
3834 {
3835 if (lock)
3836 l->lv_lock |= VAR_LOCKED;
3837 else
3838 l->lv_lock &= ~VAR_LOCKED;
3839 if (deep < 0 || deep > 1)
3840 /* recursive: lock/unlock the items the List contains */
3841 for (li = l->lv_first; li != NULL; li = li->li_next)
3842 item_lock(&li->li_tv, deep - 1, lock);
3843 }
3844 break;
3845 case VAR_DICT:
3846 if ((d = tv->vval.v_dict) != NULL)
3847 {
3848 if (lock)
3849 d->dv_lock |= VAR_LOCKED;
3850 else
3851 d->dv_lock &= ~VAR_LOCKED;
3852 if (deep < 0 || deep > 1)
3853 {
3854 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003855 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003856 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3857 {
3858 if (!HASHITEM_EMPTY(hi))
3859 {
3860 --todo;
3861 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3862 }
3863 }
3864 }
3865 }
3866 }
3867 --recurse;
3868}
3869
Bram Moolenaara40058a2005-07-11 22:42:07 +00003870/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003871 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3872 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003873 */
3874 static int
3875tv_islocked(tv)
3876 typval_T *tv;
3877{
3878 return (tv->v_lock & VAR_LOCKED)
3879 || (tv->v_type == VAR_LIST
3880 && tv->vval.v_list != NULL
3881 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3882 || (tv->v_type == VAR_DICT
3883 && tv->vval.v_dict != NULL
3884 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3885}
3886
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3888/*
3889 * Delete all "menutrans_" variables.
3890 */
3891 void
3892del_menutrans_vars()
3893{
Bram Moolenaar33570922005-01-25 22:26:29 +00003894 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003895 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896
Bram Moolenaar33570922005-01-25 22:26:29 +00003897 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003898 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003899 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003900 {
3901 if (!HASHITEM_EMPTY(hi))
3902 {
3903 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003904 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3905 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003906 }
3907 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003908 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909}
3910#endif
3911
3912#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3913
3914/*
3915 * Local string buffer for the next two functions to store a variable name
3916 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3917 * get_user_var_name().
3918 */
3919
3920static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3921
3922static char_u *varnamebuf = NULL;
3923static int varnamebuflen = 0;
3924
3925/*
3926 * Function to concatenate a prefix and a variable name.
3927 */
3928 static char_u *
3929cat_prefix_varname(prefix, name)
3930 int prefix;
3931 char_u *name;
3932{
3933 int len;
3934
3935 len = (int)STRLEN(name) + 3;
3936 if (len > varnamebuflen)
3937 {
3938 vim_free(varnamebuf);
3939 len += 10; /* some additional space */
3940 varnamebuf = alloc(len);
3941 if (varnamebuf == NULL)
3942 {
3943 varnamebuflen = 0;
3944 return NULL;
3945 }
3946 varnamebuflen = len;
3947 }
3948 *varnamebuf = prefix;
3949 varnamebuf[1] = ':';
3950 STRCPY(varnamebuf + 2, name);
3951 return varnamebuf;
3952}
3953
3954/*
3955 * Function given to ExpandGeneric() to obtain the list of user defined
3956 * (global/buffer/window/built-in) variable names.
3957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 char_u *
3959get_user_var_name(xp, idx)
3960 expand_T *xp;
3961 int idx;
3962{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003963 static long_u gdone;
3964 static long_u bdone;
3965 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003966#ifdef FEAT_WINDOWS
3967 static long_u tdone;
3968#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003969 static int vidx;
3970 static hashitem_T *hi;
3971 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972
3973 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003974 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003975 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003976#ifdef FEAT_WINDOWS
3977 tdone = 0;
3978#endif
3979 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003980
3981 /* Global variables */
3982 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003984 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003986 else
3987 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003988 while (HASHITEM_EMPTY(hi))
3989 ++hi;
3990 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3991 return cat_prefix_varname('g', hi->hi_key);
3992 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003994
3995 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003996 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003999 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004000 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004001 else
4002 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 while (HASHITEM_EMPTY(hi))
4004 ++hi;
4005 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004009 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 return (char_u *)"b:changedtick";
4011 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004012
4013 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004014 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004015 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004017 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004018 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004019 else
4020 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004021 while (HASHITEM_EMPTY(hi))
4022 ++hi;
4023 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004025
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004026#ifdef FEAT_WINDOWS
4027 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004028 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029 if (tdone < ht->ht_used)
4030 {
4031 if (tdone++ == 0)
4032 hi = ht->ht_array;
4033 else
4034 ++hi;
4035 while (HASHITEM_EMPTY(hi))
4036 ++hi;
4037 return cat_prefix_varname('t', hi->hi_key);
4038 }
4039#endif
4040
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 /* v: variables */
4042 if (vidx < VV_LEN)
4043 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 vim_free(varnamebuf);
4046 varnamebuf = NULL;
4047 varnamebuflen = 0;
4048 return NULL;
4049}
4050
4051#endif /* FEAT_CMDL_COMPL */
4052
4053/*
4054 * types for expressions.
4055 */
4056typedef enum
4057{
4058 TYPE_UNKNOWN = 0
4059 , TYPE_EQUAL /* == */
4060 , TYPE_NEQUAL /* != */
4061 , TYPE_GREATER /* > */
4062 , TYPE_GEQUAL /* >= */
4063 , TYPE_SMALLER /* < */
4064 , TYPE_SEQUAL /* <= */
4065 , TYPE_MATCH /* =~ */
4066 , TYPE_NOMATCH /* !~ */
4067} exptype_T;
4068
4069/*
4070 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004071 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4073 */
4074
4075/*
4076 * Handle zero level expression.
4077 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004078 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004079 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 * Return OK or FAIL.
4081 */
4082 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 char_u **nextcmd;
4087 int evaluate;
4088{
4089 int ret;
4090 char_u *p;
4091
4092 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (ret == FAIL || !ends_excmd(*p))
4095 {
4096 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 /*
4099 * Report the invalid expression unless the expression evaluation has
4100 * been cancelled due to an aborting error, an interrupt, or an
4101 * exception.
4102 */
4103 if (!aborting())
4104 EMSG2(_(e_invexpr2), arg);
4105 ret = FAIL;
4106 }
4107 if (nextcmd != NULL)
4108 *nextcmd = check_nextcmd(p);
4109
4110 return ret;
4111}
4112
4113/*
4114 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004115 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 *
4117 * "arg" must point to the first non-white of the expression.
4118 * "arg" is advanced to the next non-white after the recognized expression.
4119 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004120 * Note: "rettv.v_lock" is not set.
4121 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * Return OK or FAIL.
4123 */
4124 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 int evaluate;
4129{
4130 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004131 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 /*
4134 * Get the first variable.
4135 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 return FAIL;
4138
4139 if ((*arg)[0] == '?')
4140 {
4141 result = FALSE;
4142 if (evaluate)
4143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004144 int error = FALSE;
4145
4146 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004149 if (error)
4150 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152
4153 /*
4154 * Get the second variable.
4155 */
4156 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 /*
4161 * Check for the ":".
4162 */
4163 if ((*arg)[0] != ':')
4164 {
4165 EMSG(_("E109: Missing ':' after '?'"));
4166 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169 }
4170
4171 /*
4172 * Get the third variable.
4173 */
4174 *arg = skipwhite(*arg + 1);
4175 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4176 {
4177 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004178 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
4180 }
4181 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 }
4184
4185 return OK;
4186}
4187
4188/*
4189 * Handle first level expression:
4190 * expr2 || expr2 || expr2 logical OR
4191 *
4192 * "arg" must point to the first non-white of the expression.
4193 * "arg" is advanced to the next non-white after the recognized expression.
4194 *
4195 * Return OK or FAIL.
4196 */
4197 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 int evaluate;
4202{
Bram Moolenaar33570922005-01-25 22:26:29 +00004203 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 long result;
4205 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004206 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208 /*
4209 * Get the first variable.
4210 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 return FAIL;
4213
4214 /*
4215 * Repeat until there is no following "||".
4216 */
4217 first = TRUE;
4218 result = FALSE;
4219 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4220 {
4221 if (evaluate && first)
4222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004226 if (error)
4227 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 first = FALSE;
4229 }
4230
4231 /*
4232 * Get the second variable.
4233 */
4234 *arg = skipwhite(*arg + 2);
4235 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4236 return FAIL;
4237
4238 /*
4239 * Compute the result.
4240 */
4241 if (evaluate && !result)
4242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004246 if (error)
4247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 if (evaluate)
4250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 rettv->v_type = VAR_NUMBER;
4252 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 return OK;
4257}
4258
4259/*
4260 * Handle second level expression:
4261 * expr3 && expr3 && expr3 logical AND
4262 *
4263 * "arg" must point to the first non-white of the expression.
4264 * "arg" is advanced to the next non-white after the recognized expression.
4265 *
4266 * Return OK or FAIL.
4267 */
4268 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 int evaluate;
4273{
Bram Moolenaar33570922005-01-25 22:26:29 +00004274 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 long result;
4276 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004277 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 /*
4280 * Get the first variable.
4281 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return FAIL;
4284
4285 /*
4286 * Repeat until there is no following "&&".
4287 */
4288 first = TRUE;
4289 result = TRUE;
4290 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4291 {
4292 if (evaluate && first)
4293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004294 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004297 if (error)
4298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 first = FALSE;
4300 }
4301
4302 /*
4303 * Get the second variable.
4304 */
4305 *arg = skipwhite(*arg + 2);
4306 if (eval4(arg, &var2, evaluate && result) == FAIL)
4307 return FAIL;
4308
4309 /*
4310 * Compute the result.
4311 */
4312 if (evaluate && result)
4313 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004314 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 if (error)
4318 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 if (evaluate)
4321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 rettv->v_type = VAR_NUMBER;
4323 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 }
4325 }
4326
4327 return OK;
4328}
4329
4330/*
4331 * Handle third level expression:
4332 * var1 == var2
4333 * var1 =~ var2
4334 * var1 != var2
4335 * var1 !~ var2
4336 * var1 > var2
4337 * var1 >= var2
4338 * var1 < var2
4339 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004340 * var1 is var2
4341 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 *
4343 * "arg" must point to the first non-white of the expression.
4344 * "arg" is advanced to the next non-white after the recognized expression.
4345 *
4346 * Return OK or FAIL.
4347 */
4348 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 int evaluate;
4353{
Bram Moolenaar33570922005-01-25 22:26:29 +00004354 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 char_u *p;
4356 int i;
4357 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 int len = 2;
4360 long n1, n2;
4361 char_u *s1, *s2;
4362 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4363 regmatch_T regmatch;
4364 int ic;
4365 char_u *save_cpo;
4366
4367 /*
4368 * Get the first variable.
4369 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004370 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 return FAIL;
4372
4373 p = *arg;
4374 switch (p[0])
4375 {
4376 case '=': if (p[1] == '=')
4377 type = TYPE_EQUAL;
4378 else if (p[1] == '~')
4379 type = TYPE_MATCH;
4380 break;
4381 case '!': if (p[1] == '=')
4382 type = TYPE_NEQUAL;
4383 else if (p[1] == '~')
4384 type = TYPE_NOMATCH;
4385 break;
4386 case '>': if (p[1] != '=')
4387 {
4388 type = TYPE_GREATER;
4389 len = 1;
4390 }
4391 else
4392 type = TYPE_GEQUAL;
4393 break;
4394 case '<': if (p[1] != '=')
4395 {
4396 type = TYPE_SMALLER;
4397 len = 1;
4398 }
4399 else
4400 type = TYPE_SEQUAL;
4401 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004402 case 'i': if (p[1] == 's')
4403 {
4404 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4405 len = 5;
4406 if (!vim_isIDc(p[len]))
4407 {
4408 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4409 type_is = TRUE;
4410 }
4411 }
4412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414
4415 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004416 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 */
4418 if (type != TYPE_UNKNOWN)
4419 {
4420 /* extra question mark appended: ignore case */
4421 if (p[len] == '?')
4422 {
4423 ic = TRUE;
4424 ++len;
4425 }
4426 /* extra '#' appended: match case */
4427 else if (p[len] == '#')
4428 {
4429 ic = FALSE;
4430 ++len;
4431 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004432 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 else
4434 ic = p_ic;
4435
4436 /*
4437 * Get the second variable.
4438 */
4439 *arg = skipwhite(p + len);
4440 if (eval5(arg, &var2, evaluate) == FAIL)
4441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 return FAIL;
4444 }
4445
4446 if (evaluate)
4447 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 if (type_is && rettv->v_type != var2.v_type)
4449 {
4450 /* For "is" a different type always means FALSE, for "notis"
4451 * it means TRUE. */
4452 n1 = (type == TYPE_NEQUAL);
4453 }
4454 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4455 {
4456 if (type_is)
4457 {
4458 n1 = (rettv->v_type == var2.v_type
4459 && rettv->vval.v_list == var2.vval.v_list);
4460 if (type == TYPE_NEQUAL)
4461 n1 = !n1;
4462 }
4463 else if (rettv->v_type != var2.v_type
4464 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4465 {
4466 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004467 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004469 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 clear_tv(rettv);
4471 clear_tv(&var2);
4472 return FAIL;
4473 }
4474 else
4475 {
4476 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004477 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4478 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 if (type == TYPE_NEQUAL)
4480 n1 = !n1;
4481 }
4482 }
4483
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004484 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4485 {
4486 if (type_is)
4487 {
4488 n1 = (rettv->v_type == var2.v_type
4489 && rettv->vval.v_dict == var2.vval.v_dict);
4490 if (type == TYPE_NEQUAL)
4491 n1 = !n1;
4492 }
4493 else if (rettv->v_type != var2.v_type
4494 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4495 {
4496 if (rettv->v_type != var2.v_type)
4497 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4498 else
4499 EMSG(_("E736: Invalid operation for Dictionary"));
4500 clear_tv(rettv);
4501 clear_tv(&var2);
4502 return FAIL;
4503 }
4504 else
4505 {
4506 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004507 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4508 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004509 if (type == TYPE_NEQUAL)
4510 n1 = !n1;
4511 }
4512 }
4513
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004514 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4515 {
4516 if (rettv->v_type != var2.v_type
4517 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4518 {
4519 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004520 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004521 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004522 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 clear_tv(rettv);
4524 clear_tv(&var2);
4525 return FAIL;
4526 }
4527 else
4528 {
4529 /* Compare two Funcrefs for being equal or unequal. */
4530 if (rettv->vval.v_string == NULL
4531 || var2.vval.v_string == NULL)
4532 n1 = FALSE;
4533 else
4534 n1 = STRCMP(rettv->vval.v_string,
4535 var2.vval.v_string) == 0;
4536 if (type == TYPE_NEQUAL)
4537 n1 = !n1;
4538 }
4539 }
4540
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004541#ifdef FEAT_FLOAT
4542 /*
4543 * If one of the two variables is a float, compare as a float.
4544 * When using "=~" or "!~", always compare as string.
4545 */
4546 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4547 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4548 {
4549 float_T f1, f2;
4550
4551 if (rettv->v_type == VAR_FLOAT)
4552 f1 = rettv->vval.v_float;
4553 else
4554 f1 = get_tv_number(rettv);
4555 if (var2.v_type == VAR_FLOAT)
4556 f2 = var2.vval.v_float;
4557 else
4558 f2 = get_tv_number(&var2);
4559 n1 = FALSE;
4560 switch (type)
4561 {
4562 case TYPE_EQUAL: n1 = (f1 == f2); break;
4563 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4564 case TYPE_GREATER: n1 = (f1 > f2); break;
4565 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4566 case TYPE_SMALLER: n1 = (f1 < f2); break;
4567 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4568 case TYPE_UNKNOWN:
4569 case TYPE_MATCH:
4570 case TYPE_NOMATCH: break; /* avoid gcc warning */
4571 }
4572 }
4573#endif
4574
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 /*
4576 * If one of the two variables is a number, compare as a number.
4577 * When using "=~" or "!~", always compare as string.
4578 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004579 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 n1 = get_tv_number(rettv);
4583 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 switch (type)
4585 {
4586 case TYPE_EQUAL: n1 = (n1 == n2); break;
4587 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4588 case TYPE_GREATER: n1 = (n1 > n2); break;
4589 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4590 case TYPE_SMALLER: n1 = (n1 < n2); break;
4591 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4592 case TYPE_UNKNOWN:
4593 case TYPE_MATCH:
4594 case TYPE_NOMATCH: break; /* avoid gcc warning */
4595 }
4596 }
4597 else
4598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 s1 = get_tv_string_buf(rettv, buf1);
4600 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4602 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4603 else
4604 i = 0;
4605 n1 = FALSE;
4606 switch (type)
4607 {
4608 case TYPE_EQUAL: n1 = (i == 0); break;
4609 case TYPE_NEQUAL: n1 = (i != 0); break;
4610 case TYPE_GREATER: n1 = (i > 0); break;
4611 case TYPE_GEQUAL: n1 = (i >= 0); break;
4612 case TYPE_SMALLER: n1 = (i < 0); break;
4613 case TYPE_SEQUAL: n1 = (i <= 0); break;
4614
4615 case TYPE_MATCH:
4616 case TYPE_NOMATCH:
4617 /* avoid 'l' flag in 'cpoptions' */
4618 save_cpo = p_cpo;
4619 p_cpo = (char_u *)"";
4620 regmatch.regprog = vim_regcomp(s2,
4621 RE_MAGIC + RE_STRING);
4622 regmatch.rm_ic = ic;
4623 if (regmatch.regprog != NULL)
4624 {
4625 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004626 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (type == TYPE_NOMATCH)
4628 n1 = !n1;
4629 }
4630 p_cpo = save_cpo;
4631 break;
4632
4633 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4634 }
4635 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 clear_tv(rettv);
4637 clear_tv(&var2);
4638 rettv->v_type = VAR_NUMBER;
4639 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 }
4641 }
4642
4643 return OK;
4644}
4645
4646/*
4647 * Handle fourth level expression:
4648 * + number addition
4649 * - number subtraction
4650 * . string concatenation
4651 *
4652 * "arg" must point to the first non-white of the expression.
4653 * "arg" is advanced to the next non-white after the recognized expression.
4654 *
4655 * Return OK or FAIL.
4656 */
4657 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004658eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 int evaluate;
4662{
Bram Moolenaar33570922005-01-25 22:26:29 +00004663 typval_T var2;
4664 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 int op;
4666 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004667#ifdef FEAT_FLOAT
4668 float_T f1 = 0, f2 = 0;
4669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 char_u *s1, *s2;
4671 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4672 char_u *p;
4673
4674 /*
4675 * Get the first variable.
4676 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004677 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 return FAIL;
4679
4680 /*
4681 * Repeat computing, until no '+', '-' or '.' is following.
4682 */
4683 for (;;)
4684 {
4685 op = **arg;
4686 if (op != '+' && op != '-' && op != '.')
4687 break;
4688
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689 if ((op != '+' || rettv->v_type != VAR_LIST)
4690#ifdef FEAT_FLOAT
4691 && (op == '.' || rettv->v_type != VAR_FLOAT)
4692#endif
4693 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004694 {
4695 /* For "list + ...", an illegal use of the first operand as
4696 * a number cannot be determined before evaluating the 2nd
4697 * operand: if this is also a list, all is ok.
4698 * For "something . ...", "something - ..." or "non-list + ...",
4699 * we know that the first operand needs to be a string or number
4700 * without evaluating the 2nd operand. So check before to avoid
4701 * side effects after an error. */
4702 if (evaluate && get_tv_string_chk(rettv) == NULL)
4703 {
4704 clear_tv(rettv);
4705 return FAIL;
4706 }
4707 }
4708
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 /*
4710 * Get the second variable.
4711 */
4712 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004713 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004715 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return FAIL;
4717 }
4718
4719 if (evaluate)
4720 {
4721 /*
4722 * Compute the result.
4723 */
4724 if (op == '.')
4725 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004726 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4727 s2 = get_tv_string_buf_chk(&var2, buf2);
4728 if (s2 == NULL) /* type error ? */
4729 {
4730 clear_tv(rettv);
4731 clear_tv(&var2);
4732 return FAIL;
4733 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004734 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 clear_tv(rettv);
4736 rettv->v_type = VAR_STRING;
4737 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004739 else if (op == '+' && rettv->v_type == VAR_LIST
4740 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004741 {
4742 /* concatenate Lists */
4743 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4744 &var3) == FAIL)
4745 {
4746 clear_tv(rettv);
4747 clear_tv(&var2);
4748 return FAIL;
4749 }
4750 clear_tv(rettv);
4751 *rettv = var3;
4752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 else
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 int error = FALSE;
4756
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004757#ifdef FEAT_FLOAT
4758 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760 f1 = rettv->vval.v_float;
4761 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004763 else
4764#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004765 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004766 n1 = get_tv_number_chk(rettv, &error);
4767 if (error)
4768 {
4769 /* This can only happen for "list + non-list". For
4770 * "non-list + ..." or "something - ...", we returned
4771 * before evaluating the 2nd operand. */
4772 clear_tv(rettv);
4773 return FAIL;
4774 }
4775#ifdef FEAT_FLOAT
4776 if (var2.v_type == VAR_FLOAT)
4777 f1 = n1;
4778#endif
4779 }
4780#ifdef FEAT_FLOAT
4781 if (var2.v_type == VAR_FLOAT)
4782 {
4783 f2 = var2.vval.v_float;
4784 n2 = 0;
4785 }
4786 else
4787#endif
4788 {
4789 n2 = get_tv_number_chk(&var2, &error);
4790 if (error)
4791 {
4792 clear_tv(rettv);
4793 clear_tv(&var2);
4794 return FAIL;
4795 }
4796#ifdef FEAT_FLOAT
4797 if (rettv->v_type == VAR_FLOAT)
4798 f2 = n2;
4799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004801 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802
4803#ifdef FEAT_FLOAT
4804 /* If there is a float on either side the result is a float. */
4805 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4806 {
4807 if (op == '+')
4808 f1 = f1 + f2;
4809 else
4810 f1 = f1 - f2;
4811 rettv->v_type = VAR_FLOAT;
4812 rettv->vval.v_float = f1;
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004815#endif
4816 {
4817 if (op == '+')
4818 n1 = n1 + n2;
4819 else
4820 n1 = n1 - n2;
4821 rettv->v_type = VAR_NUMBER;
4822 rettv->vval.v_number = n1;
4823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004825 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827 }
4828 return OK;
4829}
4830
4831/*
4832 * Handle fifth level expression:
4833 * * number multiplication
4834 * / number division
4835 * % number modulo
4836 *
4837 * "arg" must point to the first non-white of the expression.
4838 * "arg" is advanced to the next non-white after the recognized expression.
4839 *
4840 * Return OK or FAIL.
4841 */
4842 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004843eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004847 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
Bram Moolenaar33570922005-01-25 22:26:29 +00004849 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 int op;
4851 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004852#ifdef FEAT_FLOAT
4853 int use_float = FALSE;
4854 float_T f1 = 0, f2;
4855#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004856 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
4858 /*
4859 * Get the first variable.
4860 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004861 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return FAIL;
4863
4864 /*
4865 * Repeat computing, until no '*', '/' or '%' is following.
4866 */
4867 for (;;)
4868 {
4869 op = **arg;
4870 if (op != '*' && op != '/' && op != '%')
4871 break;
4872
4873 if (evaluate)
4874 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004875#ifdef FEAT_FLOAT
4876 if (rettv->v_type == VAR_FLOAT)
4877 {
4878 f1 = rettv->vval.v_float;
4879 use_float = TRUE;
4880 n1 = 0;
4881 }
4882 else
4883#endif
4884 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004886 if (error)
4887 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 else
4890 n1 = 0;
4891
4892 /*
4893 * Get the second variable.
4894 */
4895 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004896 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 return FAIL;
4898
4899 if (evaluate)
4900 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901#ifdef FEAT_FLOAT
4902 if (var2.v_type == VAR_FLOAT)
4903 {
4904 if (!use_float)
4905 {
4906 f1 = n1;
4907 use_float = TRUE;
4908 }
4909 f2 = var2.vval.v_float;
4910 n2 = 0;
4911 }
4912 else
4913#endif
4914 {
4915 n2 = get_tv_number_chk(&var2, &error);
4916 clear_tv(&var2);
4917 if (error)
4918 return FAIL;
4919#ifdef FEAT_FLOAT
4920 if (use_float)
4921 f2 = n2;
4922#endif
4923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
4925 /*
4926 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#ifdef FEAT_FLOAT
4930 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004932 if (op == '*')
4933 f1 = f1 * f2;
4934 else if (op == '/')
4935 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004936# ifdef VMS
4937 /* VMS crashes on divide by zero, work around it */
4938 if (f2 == 0.0)
4939 {
4940 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004941 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004942 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004943 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004944 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004945 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004946 }
4947 else
4948 f1 = f1 / f2;
4949# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004950 /* We rely on the floating point library to handle divide
4951 * by zero to result in "inf" and not a crash. */
4952 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004953# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004957 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958 return FAIL;
4959 }
4960 rettv->v_type = VAR_FLOAT;
4961 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966 if (op == '*')
4967 n1 = n1 * n2;
4968 else if (op == '/')
4969 {
4970 if (n2 == 0) /* give an error message? */
4971 {
4972 if (n1 == 0)
4973 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4974 else if (n1 < 0)
4975 n1 = -0x7fffffffL;
4976 else
4977 n1 = 0x7fffffffL;
4978 }
4979 else
4980 n1 = n1 / n2;
4981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 {
4984 if (n2 == 0) /* give an error message? */
4985 n1 = 0;
4986 else
4987 n1 = n1 % n2;
4988 }
4989 rettv->v_type = VAR_NUMBER;
4990 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 }
4994
4995 return OK;
4996}
4997
4998/*
4999 * Handle sixth level expression:
5000 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005001 * "string" string constant
5002 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 * &option-name option value
5004 * @r register contents
5005 * identifier variable value
5006 * function() function call
5007 * $VAR environment variable
5008 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005009 * [expr, expr] List
5010 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 *
5012 * Also handle:
5013 * ! in front logical NOT
5014 * - in front unary minus
5015 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 * trailing [] subscript in String or List
5017 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 *
5019 * "arg" must point to the first non-white of the expression.
5020 * "arg" is advanced to the next non-white after the recognized expression.
5021 *
5022 * Return OK or FAIL.
5023 */
5024 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005025eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02005029 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 long n;
5032 int len;
5033 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 char_u *start_leader, *end_leader;
5035 int ret = OK;
5036 char_u *alias;
5037
5038 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005039 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005040 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005042 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
5044 /*
5045 * Skip '!' and '-' characters. They are handled later.
5046 */
5047 start_leader = *arg;
5048 while (**arg == '!' || **arg == '-' || **arg == '+')
5049 *arg = skipwhite(*arg + 1);
5050 end_leader = *arg;
5051
5052 switch (**arg)
5053 {
5054 /*
5055 * Number constant.
5056 */
5057 case '0':
5058 case '1':
5059 case '2':
5060 case '3':
5061 case '4':
5062 case '5':
5063 case '6':
5064 case '7':
5065 case '8':
5066 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005067 {
5068#ifdef FEAT_FLOAT
5069 char_u *p = skipdigits(*arg + 1);
5070 int get_float = FALSE;
5071
5072 /* We accept a float when the format matches
5073 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005074 * strict to avoid backwards compatibility problems.
5075 * Don't look for a float after the "." operator, so that
5076 * ":let vers = 1.2.3" doesn't fail. */
5077 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005079 get_float = TRUE;
5080 p = skipdigits(p + 2);
5081 if (*p == 'e' || *p == 'E')
5082 {
5083 ++p;
5084 if (*p == '-' || *p == '+')
5085 ++p;
5086 if (!vim_isdigit(*p))
5087 get_float = FALSE;
5088 else
5089 p = skipdigits(p + 1);
5090 }
5091 if (ASCII_ISALPHA(*p) || *p == '.')
5092 get_float = FALSE;
5093 }
5094 if (get_float)
5095 {
5096 float_T f;
5097
5098 *arg += string2float(*arg, &f);
5099 if (evaluate)
5100 {
5101 rettv->v_type = VAR_FLOAT;
5102 rettv->vval.v_float = f;
5103 }
5104 }
5105 else
5106#endif
5107 {
5108 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5109 *arg += len;
5110 if (evaluate)
5111 {
5112 rettv->v_type = VAR_NUMBER;
5113 rettv->vval.v_number = n;
5114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
5119 /*
5120 * String constant: "string".
5121 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 break;
5124
5125 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005128 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005129 break;
5130
5131 /*
5132 * List: [expr, expr]
5133 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005134 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 break;
5136
5137 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005138 * Dictionary: {key: val, key: val}
5139 */
5140 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5141 break;
5142
5143 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005144 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005146 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 break;
5148
5149 /*
5150 * Environment variable: $VAR.
5151 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 break;
5154
5155 /*
5156 * Register contents: @r.
5157 */
5158 case '@': ++*arg;
5159 if (evaluate)
5160 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005161 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005162 rettv->vval.v_string = get_reg_contents(**arg,
5163 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 if (**arg != NUL)
5166 ++*arg;
5167 break;
5168
5169 /*
5170 * nested expression: (expression).
5171 */
5172 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (**arg == ')')
5175 ++*arg;
5176 else if (ret == OK)
5177 {
5178 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 ret = FAIL;
5181 }
5182 break;
5183
Bram Moolenaar8c711452005-01-14 21:53:12 +00005184 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 break;
5186 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187
5188 if (ret == NOTDONE)
5189 {
5190 /*
5191 * Must be a variable or function name.
5192 * Can also be a curly-braces kind of name: {expr}.
5193 */
5194 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005195 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 if (alias != NULL)
5197 s = alias;
5198
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005199 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005200 ret = FAIL;
5201 else
5202 {
5203 if (**arg == '(') /* recursive! */
5204 {
5205 /* If "s" is the name of a variable of type VAR_FUNC
5206 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005207 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208
5209 /* Invoke the function. */
5210 ret = get_func_tv(s, len, rettv, arg,
5211 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005212 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005213
5214 /* If evaluate is FALSE rettv->v_type was not set in
5215 * get_func_tv, but it's needed in handle_subscript() to parse
5216 * what follows. So set it here. */
5217 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5218 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005219 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005220 rettv->v_type = VAR_FUNC;
5221 }
5222
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 /* Stop the expression evaluation when immediately
5224 * aborting on error, or when an interrupt occurred or
5225 * an exception was thrown but not caught. */
5226 if (aborting())
5227 {
5228 if (ret == OK)
5229 clear_tv(rettv);
5230 ret = FAIL;
5231 }
5232 }
5233 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005234 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005235 else
5236 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005238 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239 }
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 *arg = skipwhite(*arg);
5242
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005243 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5244 * expr(expr). */
5245 if (ret == OK)
5246 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
5248 /*
5249 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5250 */
5251 if (ret == OK && evaluate && end_leader > start_leader)
5252 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005253 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005254 int val = 0;
5255#ifdef FEAT_FLOAT
5256 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005257
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005258 if (rettv->v_type == VAR_FLOAT)
5259 f = rettv->vval.v_float;
5260 else
5261#endif
5262 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005263 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005265 clear_tv(rettv);
5266 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005268 else
5269 {
5270 while (end_leader > start_leader)
5271 {
5272 --end_leader;
5273 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005274 {
5275#ifdef FEAT_FLOAT
5276 if (rettv->v_type == VAR_FLOAT)
5277 f = !f;
5278 else
5279#endif
5280 val = !val;
5281 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005283 {
5284#ifdef FEAT_FLOAT
5285 if (rettv->v_type == VAR_FLOAT)
5286 f = -f;
5287 else
5288#endif
5289 val = -val;
5290 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005291 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005292#ifdef FEAT_FLOAT
5293 if (rettv->v_type == VAR_FLOAT)
5294 {
5295 clear_tv(rettv);
5296 rettv->vval.v_float = f;
5297 }
5298 else
5299#endif
5300 {
5301 clear_tv(rettv);
5302 rettv->v_type = VAR_NUMBER;
5303 rettv->vval.v_number = val;
5304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
5307
5308 return ret;
5309}
5310
5311/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005312 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5313 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5315 */
5316 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005317eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005318 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005319 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005320 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005321 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005322{
5323 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005324 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005325 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005326 long len = -1;
5327 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005328 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005329 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005331 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005333 if (verbose)
5334 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 return FAIL;
5336 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005337#ifdef FEAT_FLOAT
5338 else if (rettv->v_type == VAR_FLOAT)
5339 {
5340 if (verbose)
5341 EMSG(_(e_float_as_string));
5342 return FAIL;
5343 }
5344#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345
Bram Moolenaar8c711452005-01-14 21:53:12 +00005346 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005347 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005348 /*
5349 * dict.name
5350 */
5351 key = *arg + 1;
5352 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5353 ;
5354 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005356 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 }
5358 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 /*
5361 * something[idx]
5362 *
5363 * Get the (first) variable from inside the [].
5364 */
5365 *arg = skipwhite(*arg + 1);
5366 if (**arg == ':')
5367 empty1 = TRUE;
5368 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5369 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005370 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5371 {
5372 /* not a number or string */
5373 clear_tv(&var1);
5374 return FAIL;
5375 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376
5377 /*
5378 * Get the second variable from inside the [:].
5379 */
5380 if (**arg == ':')
5381 {
5382 range = TRUE;
5383 *arg = skipwhite(*arg + 1);
5384 if (**arg == ']')
5385 empty2 = TRUE;
5386 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5387 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005388 if (!empty1)
5389 clear_tv(&var1);
5390 return FAIL;
5391 }
5392 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5393 {
5394 /* not a number or string */
5395 if (!empty1)
5396 clear_tv(&var1);
5397 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 return FAIL;
5399 }
5400 }
5401
5402 /* Check for the ']'. */
5403 if (**arg != ']')
5404 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005405 if (verbose)
5406 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 clear_tv(&var1);
5408 if (range)
5409 clear_tv(&var2);
5410 return FAIL;
5411 }
5412 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 }
5414
5415 if (evaluate)
5416 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417 n1 = 0;
5418 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 n1 = get_tv_number(&var1);
5421 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 }
5423 if (range)
5424 {
5425 if (empty2)
5426 n2 = -1;
5427 else
5428 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005429 n2 = get_tv_number(&var2);
5430 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 }
5432 }
5433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005434 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 {
5436 case VAR_NUMBER:
5437 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005439 len = (long)STRLEN(s);
5440 if (range)
5441 {
5442 /* The resulting variable is a substring. If the indexes
5443 * are out of range the result is empty. */
5444 if (n1 < 0)
5445 {
5446 n1 = len + n1;
5447 if (n1 < 0)
5448 n1 = 0;
5449 }
5450 if (n2 < 0)
5451 n2 = len + n2;
5452 else if (n2 >= len)
5453 n2 = len;
5454 if (n1 >= len || n2 < 0 || n1 > n2)
5455 s = NULL;
5456 else
5457 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5458 }
5459 else
5460 {
5461 /* The resulting variable is a string of a single
5462 * character. If the index is too big or negative the
5463 * result is empty. */
5464 if (n1 >= len || n1 < 0)
5465 s = NULL;
5466 else
5467 s = vim_strnsave(s + n1, 1);
5468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 clear_tv(rettv);
5470 rettv->v_type = VAR_STRING;
5471 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005472 break;
5473
5474 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 if (n1 < 0)
5477 n1 = len + n1;
5478 if (!empty1 && (n1 < 0 || n1 >= len))
5479 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005480 /* For a range we allow invalid values and return an empty
5481 * list. A list index out of range is an error. */
5482 if (!range)
5483 {
5484 if (verbose)
5485 EMSGN(_(e_listidx), n1);
5486 return FAIL;
5487 }
5488 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 }
5490 if (range)
5491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 list_T *l;
5493 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494
5495 if (n2 < 0)
5496 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005497 else if (n2 >= len)
5498 n2 = len - 1;
5499 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005500 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 l = list_alloc();
5502 if (l == NULL)
5503 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005504 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 n1 <= n2; ++n1)
5506 {
5507 if (list_append_tv(l, &item->li_tv) == FAIL)
5508 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005509 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005510 return FAIL;
5511 }
5512 item = item->li_next;
5513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514 clear_tv(rettv);
5515 rettv->v_type = VAR_LIST;
5516 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005517 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 }
5519 else
5520 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005521 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 clear_tv(rettv);
5523 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005524 }
5525 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526
5527 case VAR_DICT:
5528 if (range)
5529 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005530 if (verbose)
5531 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005532 if (len == -1)
5533 clear_tv(&var1);
5534 return FAIL;
5535 }
5536 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005537 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005538
5539 if (len == -1)
5540 {
5541 key = get_tv_string(&var1);
5542 if (*key == NUL)
5543 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005544 if (verbose)
5545 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005546 clear_tv(&var1);
5547 return FAIL;
5548 }
5549 }
5550
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005551 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005552
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005553 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005554 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005555 if (len == -1)
5556 clear_tv(&var1);
5557 if (item == NULL)
5558 return FAIL;
5559
5560 copy_tv(&item->di_tv, &var1);
5561 clear_tv(rettv);
5562 *rettv = var1;
5563 }
5564 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 }
5566 }
5567
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 return OK;
5569}
5570
5571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 * Get an option value.
5573 * "arg" points to the '&' or '+' before the option name.
5574 * "arg" is advanced to character after the option name.
5575 * Return OK or FAIL.
5576 */
5577 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005580 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 int evaluate;
5582{
5583 char_u *option_end;
5584 long numval;
5585 char_u *stringval;
5586 int opt_type;
5587 int c;
5588 int working = (**arg == '+'); /* has("+option") */
5589 int ret = OK;
5590 int opt_flags;
5591
5592 /*
5593 * Isolate the option name and find its value.
5594 */
5595 option_end = find_option_end(arg, &opt_flags);
5596 if (option_end == NULL)
5597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 EMSG2(_("E112: Option name missing: %s"), *arg);
5600 return FAIL;
5601 }
5602
5603 if (!evaluate)
5604 {
5605 *arg = option_end;
5606 return OK;
5607 }
5608
5609 c = *option_end;
5610 *option_end = NUL;
5611 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 if (opt_type == -3) /* invalid name */
5615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 EMSG2(_("E113: Unknown option: %s"), *arg);
5618 ret = FAIL;
5619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005620 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 if (opt_type == -2) /* hidden string option */
5623 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005624 rettv->v_type = VAR_STRING;
5625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
5627 else if (opt_type == -1) /* hidden number option */
5628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 rettv->v_type = VAR_NUMBER;
5630 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 }
5632 else if (opt_type == 1) /* number option */
5633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634 rettv->v_type = VAR_NUMBER;
5635 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
5637 else /* string option */
5638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 rettv->v_type = VAR_STRING;
5640 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642 }
5643 else if (working && (opt_type == -2 || opt_type == -1))
5644 ret = FAIL;
5645
5646 *option_end = c; /* put back for error messages */
5647 *arg = option_end;
5648
5649 return ret;
5650}
5651
5652/*
5653 * Allocate a variable for a string constant.
5654 * Return OK or FAIL.
5655 */
5656 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 int evaluate;
5661{
5662 char_u *p;
5663 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 int extra = 0;
5665
5666 /*
5667 * Find the end of the string, skipping backslashed characters.
5668 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005669 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 if (*p == '\\' && p[1] != NUL)
5672 {
5673 ++p;
5674 /* A "\<x>" form occupies at least 4 characters, and produces up
5675 * to 6 characters: reserve space for 2 extra */
5676 if (*p == '<')
5677 extra += 2;
5678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680
5681 if (*p != '"')
5682 {
5683 EMSG2(_("E114: Missing quote: %s"), *arg);
5684 return FAIL;
5685 }
5686
5687 /* If only parsing, set *arg and return here */
5688 if (!evaluate)
5689 {
5690 *arg = p + 1;
5691 return OK;
5692 }
5693
5694 /*
5695 * Copy the string into allocated memory, handling backslashed
5696 * characters.
5697 */
5698 name = alloc((unsigned)(p - *arg + extra));
5699 if (name == NULL)
5700 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 rettv->v_type = VAR_STRING;
5702 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
5706 if (*p == '\\')
5707 {
5708 switch (*++p)
5709 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 case 'b': *name++ = BS; ++p; break;
5711 case 'e': *name++ = ESC; ++p; break;
5712 case 'f': *name++ = FF; ++p; break;
5713 case 'n': *name++ = NL; ++p; break;
5714 case 'r': *name++ = CAR; ++p; break;
5715 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 case 'X': /* hex: "\x1", "\x12" */
5718 case 'x':
5719 case 'u': /* Unicode: "\u0023" */
5720 case 'U':
5721 if (vim_isxdigit(p[1]))
5722 {
5723 int n, nr;
5724 int c = toupper(*p);
5725
5726 if (c == 'X')
5727 n = 2;
5728 else
5729 n = 4;
5730 nr = 0;
5731 while (--n >= 0 && vim_isxdigit(p[1]))
5732 {
5733 ++p;
5734 nr = (nr << 4) + hex2nr(*p);
5735 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737#ifdef FEAT_MBYTE
5738 /* For "\u" store the number according to
5739 * 'encoding'. */
5740 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005741 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 else
5743#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005744 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 break;
5747
5748 /* octal: "\1", "\12", "\123" */
5749 case '0':
5750 case '1':
5751 case '2':
5752 case '3':
5753 case '4':
5754 case '5':
5755 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 case '7': *name = *p++ - '0';
5757 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 *name = (*name << 3) + *p++ - '0';
5760 if (*p >= '0' && *p <= '7')
5761 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 break;
5765
5766 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 if (extra != 0)
5769 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005770 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 break;
5772 }
5773 /* FALLTHROUGH */
5774
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 break;
5777 }
5778 }
5779 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 *arg = p + 1;
5785
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 return OK;
5787}
5788
5789/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 * Return OK or FAIL.
5792 */
5793 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005794get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 int evaluate;
5798{
5799 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005800 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005801 int reduce = 0;
5802
5803 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 */
5806 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005808 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005810 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005811 break;
5812 ++reduce;
5813 ++p;
5814 }
5815 }
5816
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005818 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005819 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005820 return FAIL;
5821 }
5822
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005824 if (!evaluate)
5825 {
5826 *arg = p + 1;
5827 return OK;
5828 }
5829
5830 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005832 */
5833 str = alloc((unsigned)((p - *arg) - reduce));
5834 if (str == NULL)
5835 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 rettv->v_type = VAR_STRING;
5837 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005838
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005844 break;
5845 ++p;
5846 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005848 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 *arg = p + 1;
5851
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005852 return OK;
5853}
5854
5855/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 * Allocate a variable for a List and fill it from "*arg".
5857 * Return OK or FAIL.
5858 */
5859 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005860get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005862 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005863 int evaluate;
5864{
Bram Moolenaar33570922005-01-25 22:26:29 +00005865 list_T *l = NULL;
5866 typval_T tv;
5867 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868
5869 if (evaluate)
5870 {
5871 l = list_alloc();
5872 if (l == NULL)
5873 return FAIL;
5874 }
5875
5876 *arg = skipwhite(*arg + 1);
5877 while (**arg != ']' && **arg != NUL)
5878 {
5879 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5880 goto failret;
5881 if (evaluate)
5882 {
5883 item = listitem_alloc();
5884 if (item != NULL)
5885 {
5886 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005887 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 list_append(l, item);
5889 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005890 else
5891 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005892 }
5893
5894 if (**arg == ']')
5895 break;
5896 if (**arg != ',')
5897 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899 goto failret;
5900 }
5901 *arg = skipwhite(*arg + 1);
5902 }
5903
5904 if (**arg != ']')
5905 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907failret:
5908 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005909 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 return FAIL;
5911 }
5912
5913 *arg = skipwhite(*arg + 1);
5914 if (evaluate)
5915 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005916 rettv->v_type = VAR_LIST;
5917 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918 ++l->lv_refcount;
5919 }
5920
5921 return OK;
5922}
5923
5924/*
5925 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005926 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005928 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929list_alloc()
5930{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005931 list_T *l;
5932
5933 l = (list_T *)alloc_clear(sizeof(list_T));
5934 if (l != NULL)
5935 {
5936 /* Prepend the list to the list of lists for garbage collection. */
5937 if (first_list != NULL)
5938 first_list->lv_used_prev = l;
5939 l->lv_used_prev = NULL;
5940 l->lv_used_next = first_list;
5941 first_list = l;
5942 }
5943 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944}
5945
5946/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005947 * Allocate an empty list for a return value.
5948 * Returns OK or FAIL.
5949 */
5950 static int
5951rettv_list_alloc(rettv)
5952 typval_T *rettv;
5953{
5954 list_T *l = list_alloc();
5955
5956 if (l == NULL)
5957 return FAIL;
5958
5959 rettv->vval.v_list = l;
5960 rettv->v_type = VAR_LIST;
5961 ++l->lv_refcount;
5962 return OK;
5963}
5964
5965/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966 * Unreference a list: decrement the reference count and free it when it
5967 * becomes zero.
5968 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005969 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005973 if (l != NULL && --l->lv_refcount <= 0)
5974 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975}
5976
5977/*
5978 * Free a list, including all items it points to.
5979 * Ignores the reference count.
5980 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005981 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005982list_free(l, recurse)
5983 list_T *l;
5984 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985{
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005988 /* Remove the list from the list of lists for garbage collection. */
5989 if (l->lv_used_prev == NULL)
5990 first_list = l->lv_used_next;
5991 else
5992 l->lv_used_prev->lv_used_next = l->lv_used_next;
5993 if (l->lv_used_next != NULL)
5994 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5995
Bram Moolenaard9fba312005-06-26 22:34:35 +00005996 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005998 /* Remove the item before deleting it. */
5999 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006000 if (recurse || (item->li_tv.v_type != VAR_LIST
6001 && item->li_tv.v_type != VAR_DICT))
6002 clear_tv(&item->li_tv);
6003 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006004 }
6005 vim_free(l);
6006}
6007
6008/*
6009 * Allocate a list item.
6010 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006011 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012listitem_alloc()
6013{
Bram Moolenaar33570922005-01-25 22:26:29 +00006014 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006015}
6016
6017/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006018 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006019 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006020 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006024 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 vim_free(item);
6026}
6027
6028/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006029 * Remove a list item from a List and free it. Also clears the value.
6030 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006031 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006032listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006033 list_T *l;
6034 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006035{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006036 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006037 listitem_free(item);
6038}
6039
6040/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041 * Get the number of items in a list.
6042 */
6043 static long
6044list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 if (l == NULL)
6048 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006049 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050}
6051
6052/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006053 * Return TRUE when two lists have exactly the same values.
6054 */
6055 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006056list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 list_T *l1;
6058 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006059 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006060 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006061{
Bram Moolenaar33570922005-01-25 22:26:29 +00006062 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006063
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006064 if (l1 == NULL || l2 == NULL)
6065 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006066 if (l1 == l2)
6067 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006068 if (list_len(l1) != list_len(l2))
6069 return FALSE;
6070
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006071 for (item1 = l1->lv_first, item2 = l2->lv_first;
6072 item1 != NULL && item2 != NULL;
6073 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006074 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 return FALSE;
6076 return item1 == NULL && item2 == NULL;
6077}
6078
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006079#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6080 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006081/*
6082 * Return the dictitem that an entry in a hashtable points to.
6083 */
6084 dictitem_T *
6085dict_lookup(hi)
6086 hashitem_T *hi;
6087{
6088 return HI2DI(hi);
6089}
6090#endif
6091
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006092/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006093 * Return TRUE when two dictionaries have exactly the same key/values.
6094 */
6095 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006096dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006097 dict_T *d1;
6098 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006099 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006101{
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 hashitem_T *hi;
6103 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006104 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006105
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006106 if (d1 == NULL || d2 == NULL)
6107 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006108 if (d1 == d2)
6109 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110 if (dict_len(d1) != dict_len(d2))
6111 return FALSE;
6112
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006113 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006114 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006116 if (!HASHITEM_EMPTY(hi))
6117 {
6118 item2 = dict_find(d2, hi->hi_key, -1);
6119 if (item2 == NULL)
6120 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006121 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006122 return FALSE;
6123 --todo;
6124 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006125 }
6126 return TRUE;
6127}
6128
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129static int tv_equal_recurse_limit;
6130
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006132 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006134 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135 */
6136 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006137tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 typval_T *tv1;
6139 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006140 int ic; /* ignore case */
6141 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142{
6143 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006144 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006145 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006146 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006147
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006148 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006149 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006150
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006151 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006152 * recursiveness to a limit. We guess they are equal then.
6153 * A fixed limit has the problem of still taking an awful long time.
6154 * Reduce the limit every time running into it. That should work fine for
6155 * deeply linked structures that are not recursively linked and catch
6156 * recursiveness quickly. */
6157 if (!recursive)
6158 tv_equal_recurse_limit = 1000;
6159 if (recursive_cnt >= tv_equal_recurse_limit)
6160 {
6161 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006162 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006163 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006164
6165 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006166 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006167 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006168 ++recursive_cnt;
6169 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6170 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006171 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006172
6173 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006174 ++recursive_cnt;
6175 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6176 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006177 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006178
6179 case VAR_FUNC:
6180 return (tv1->vval.v_string != NULL
6181 && tv2->vval.v_string != NULL
6182 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6183
6184 case VAR_NUMBER:
6185 return tv1->vval.v_number == tv2->vval.v_number;
6186
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006187#ifdef FEAT_FLOAT
6188 case VAR_FLOAT:
6189 return tv1->vval.v_float == tv2->vval.v_float;
6190#endif
6191
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006192 case VAR_STRING:
6193 s1 = get_tv_string_buf(tv1, buf1);
6194 s2 = get_tv_string_buf(tv2, buf2);
6195 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006197
6198 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 return TRUE;
6200}
6201
6202/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006203 * Locate item with index "n" in list "l" and return it.
6204 * A negative index is counted from the end; -1 is the last item.
6205 * Returns NULL when "n" is out of range.
6206 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006207 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006209 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006210 long n;
6211{
Bram Moolenaar33570922005-01-25 22:26:29 +00006212 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006213 long idx;
6214
6215 if (l == NULL)
6216 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006217
6218 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006220 n = l->lv_len + n;
6221
6222 /* Check for index out of range. */
6223 if (n < 0 || n >= l->lv_len)
6224 return NULL;
6225
6226 /* When there is a cached index may start search from there. */
6227 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006229 if (n < l->lv_idx / 2)
6230 {
6231 /* closest to the start of the list */
6232 item = l->lv_first;
6233 idx = 0;
6234 }
6235 else if (n > (l->lv_idx + l->lv_len) / 2)
6236 {
6237 /* closest to the end of the list */
6238 item = l->lv_last;
6239 idx = l->lv_len - 1;
6240 }
6241 else
6242 {
6243 /* closest to the cached index */
6244 item = l->lv_idx_item;
6245 idx = l->lv_idx;
6246 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006247 }
6248 else
6249 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006250 if (n < l->lv_len / 2)
6251 {
6252 /* closest to the start of the list */
6253 item = l->lv_first;
6254 idx = 0;
6255 }
6256 else
6257 {
6258 /* closest to the end of the list */
6259 item = l->lv_last;
6260 idx = l->lv_len - 1;
6261 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006262 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006263
6264 while (n > idx)
6265 {
6266 /* search forward */
6267 item = item->li_next;
6268 ++idx;
6269 }
6270 while (n < idx)
6271 {
6272 /* search backward */
6273 item = item->li_prev;
6274 --idx;
6275 }
6276
6277 /* cache the used index */
6278 l->lv_idx = idx;
6279 l->lv_idx_item = item;
6280
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006281 return item;
6282}
6283
6284/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006285 * Get list item "l[idx]" as a number.
6286 */
6287 static long
6288list_find_nr(l, idx, errorp)
6289 list_T *l;
6290 long idx;
6291 int *errorp; /* set to TRUE when something wrong */
6292{
6293 listitem_T *li;
6294
6295 li = list_find(l, idx);
6296 if (li == NULL)
6297 {
6298 if (errorp != NULL)
6299 *errorp = TRUE;
6300 return -1L;
6301 }
6302 return get_tv_number_chk(&li->li_tv, errorp);
6303}
6304
6305/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006306 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6307 */
6308 char_u *
6309list_find_str(l, idx)
6310 list_T *l;
6311 long idx;
6312{
6313 listitem_T *li;
6314
6315 li = list_find(l, idx - 1);
6316 if (li == NULL)
6317 {
6318 EMSGN(_(e_listidx), idx);
6319 return NULL;
6320 }
6321 return get_tv_string(&li->li_tv);
6322}
6323
6324/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006325 * Locate "item" list "l" and return its index.
6326 * Returns -1 when "item" is not in the list.
6327 */
6328 static long
6329list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006330 list_T *l;
6331 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006332{
6333 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006335
6336 if (l == NULL)
6337 return -1;
6338 idx = 0;
6339 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6340 ++idx;
6341 if (li == NULL)
6342 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006343 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006344}
6345
6346/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006347 * Append item "item" to the end of list "l".
6348 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006349 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006351 list_T *l;
6352 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353{
6354 if (l->lv_last == NULL)
6355 {
6356 /* empty list */
6357 l->lv_first = item;
6358 l->lv_last = item;
6359 item->li_prev = NULL;
6360 }
6361 else
6362 {
6363 l->lv_last->li_next = item;
6364 item->li_prev = l->lv_last;
6365 l->lv_last = item;
6366 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006367 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 item->li_next = NULL;
6369}
6370
6371/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006372 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006373 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006374 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006375 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006376list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006377 list_T *l;
6378 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006379{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006380 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006381
Bram Moolenaar05159a02005-02-26 23:04:13 +00006382 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006383 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006384 copy_tv(tv, &li->li_tv);
6385 list_append(l, li);
6386 return OK;
6387}
6388
6389/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006390 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006391 * Return FAIL when out of memory.
6392 */
6393 int
6394list_append_dict(list, dict)
6395 list_T *list;
6396 dict_T *dict;
6397{
6398 listitem_T *li = listitem_alloc();
6399
6400 if (li == NULL)
6401 return FAIL;
6402 li->li_tv.v_type = VAR_DICT;
6403 li->li_tv.v_lock = 0;
6404 li->li_tv.vval.v_dict = dict;
6405 list_append(list, li);
6406 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 return OK;
6408}
6409
6410/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006411 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006412 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006413 * Returns FAIL when out of memory.
6414 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006415 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006416list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006417 list_T *l;
6418 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006419 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006420{
6421 listitem_T *li = listitem_alloc();
6422
6423 if (li == NULL)
6424 return FAIL;
6425 list_append(l, li);
6426 li->li_tv.v_type = VAR_STRING;
6427 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006428 if (str == NULL)
6429 li->li_tv.vval.v_string = NULL;
6430 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006431 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006432 return FAIL;
6433 return OK;
6434}
6435
6436/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006437 * Append "n" to list "l".
6438 * Returns FAIL when out of memory.
6439 */
6440 static int
6441list_append_number(l, n)
6442 list_T *l;
6443 varnumber_T n;
6444{
6445 listitem_T *li;
6446
6447 li = listitem_alloc();
6448 if (li == NULL)
6449 return FAIL;
6450 li->li_tv.v_type = VAR_NUMBER;
6451 li->li_tv.v_lock = 0;
6452 li->li_tv.vval.v_number = n;
6453 list_append(l, li);
6454 return OK;
6455}
6456
6457/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459 * If "item" is NULL append at the end.
6460 * Return FAIL when out of memory.
6461 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006462 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006463list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006464 list_T *l;
6465 typval_T *tv;
6466 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467{
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469
6470 if (ni == NULL)
6471 return FAIL;
6472 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006473 list_insert(l, ni, item);
6474 return OK;
6475}
6476
6477 void
6478list_insert(l, ni, item)
6479 list_T *l;
6480 listitem_T *ni;
6481 listitem_T *item;
6482{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006483 if (item == NULL)
6484 /* Append new item at end of list. */
6485 list_append(l, ni);
6486 else
6487 {
6488 /* Insert new item before existing item. */
6489 ni->li_prev = item->li_prev;
6490 ni->li_next = item;
6491 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006492 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006493 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006494 ++l->lv_idx;
6495 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006496 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006497 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006499 l->lv_idx_item = NULL;
6500 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006502 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006503 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504}
6505
6506/*
6507 * Extend "l1" with "l2".
6508 * If "bef" is NULL append at the end, otherwise insert before this item.
6509 * Returns FAIL when out of memory.
6510 */
6511 static int
6512list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 list_T *l1;
6514 list_T *l2;
6515 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516{
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006518 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006520 /* We also quit the loop when we have inserted the original item count of
6521 * the list, avoid a hang when we extend a list with itself. */
6522 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006523 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6524 return FAIL;
6525 return OK;
6526}
6527
6528/*
6529 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6530 * Return FAIL when out of memory.
6531 */
6532 static int
6533list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 list_T *l1;
6535 list_T *l2;
6536 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006537{
Bram Moolenaar33570922005-01-25 22:26:29 +00006538 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006540 if (l1 == NULL || l2 == NULL)
6541 return FAIL;
6542
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006543 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006544 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 if (l == NULL)
6546 return FAIL;
6547 tv->v_type = VAR_LIST;
6548 tv->vval.v_list = l;
6549
6550 /* append all items from the second list */
6551 return list_extend(l, l2, NULL);
6552}
6553
6554/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006556 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006557 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558 * Returns NULL when out of memory.
6559 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006560 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006561list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006562 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006564 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565{
Bram Moolenaar33570922005-01-25 22:26:29 +00006566 list_T *copy;
6567 listitem_T *item;
6568 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569
6570 if (orig == NULL)
6571 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006572
6573 copy = list_alloc();
6574 if (copy != NULL)
6575 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006576 if (copyID != 0)
6577 {
6578 /* Do this before adding the items, because one of the items may
6579 * refer back to this list. */
6580 orig->lv_copyID = copyID;
6581 orig->lv_copylist = copy;
6582 }
6583 for (item = orig->lv_first; item != NULL && !got_int;
6584 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 {
6586 ni = listitem_alloc();
6587 if (ni == NULL)
6588 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006589 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006590 {
6591 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6592 {
6593 vim_free(ni);
6594 break;
6595 }
6596 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006598 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 list_append(copy, ni);
6600 }
6601 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006602 if (item != NULL)
6603 {
6604 list_unref(copy);
6605 copy = NULL;
6606 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607 }
6608
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 return copy;
6610}
6611
6612/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006613 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006614 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006615 * This used to be called list_remove, but that conflicts with a Sun header
6616 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006618 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006619vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006620 list_T *l;
6621 listitem_T *item;
6622 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006623{
Bram Moolenaar33570922005-01-25 22:26:29 +00006624 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 /* notify watchers */
6627 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006628 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006629 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006630 list_fix_watch(l, ip);
6631 if (ip == item2)
6632 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634
6635 if (item2->li_next == NULL)
6636 l->lv_last = item->li_prev;
6637 else
6638 item2->li_next->li_prev = item->li_prev;
6639 if (item->li_prev == NULL)
6640 l->lv_first = item2->li_next;
6641 else
6642 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006643 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006644}
6645
6646/*
6647 * Return an allocated string with the string representation of a list.
6648 * May return NULL.
6649 */
6650 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006651list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006652 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006653 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006654{
6655 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656
6657 if (tv->vval.v_list == NULL)
6658 return NULL;
6659 ga_init2(&ga, (int)sizeof(char), 80);
6660 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006661 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006662 {
6663 vim_free(ga.ga_data);
6664 return NULL;
6665 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666 ga_append(&ga, ']');
6667 ga_append(&ga, NUL);
6668 return (char_u *)ga.ga_data;
6669}
6670
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006671typedef struct join_S {
6672 char_u *s;
6673 char_u *tofree;
6674} join_T;
6675
6676 static int
6677list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6678 garray_T *gap; /* to store the result in */
6679 list_T *l;
6680 char_u *sep;
6681 int echo_style;
6682 int copyID;
6683 garray_T *join_gap; /* to keep each list item string */
6684{
6685 int i;
6686 join_T *p;
6687 int len;
6688 int sumlen = 0;
6689 int first = TRUE;
6690 char_u *tofree;
6691 char_u numbuf[NUMBUFLEN];
6692 listitem_T *item;
6693 char_u *s;
6694
6695 /* Stringify each item in the list. */
6696 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6697 {
6698 if (echo_style)
6699 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6700 else
6701 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6702 if (s == NULL)
6703 return FAIL;
6704
6705 len = (int)STRLEN(s);
6706 sumlen += len;
6707
6708 ga_grow(join_gap, 1);
6709 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6710 if (tofree != NULL || s != numbuf)
6711 {
6712 p->s = s;
6713 p->tofree = tofree;
6714 }
6715 else
6716 {
6717 p->s = vim_strnsave(s, len);
6718 p->tofree = p->s;
6719 }
6720
6721 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006722 if (did_echo_string_emsg) /* recursion error, bail out */
6723 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 }
6725
6726 /* Allocate result buffer with its total size, avoid re-allocation and
6727 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6728 if (join_gap->ga_len >= 2)
6729 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6730 if (ga_grow(gap, sumlen + 2) == FAIL)
6731 return FAIL;
6732
6733 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6734 {
6735 if (first)
6736 first = FALSE;
6737 else
6738 ga_concat(gap, sep);
6739 p = ((join_T *)join_gap->ga_data) + i;
6740
6741 if (p->s != NULL)
6742 ga_concat(gap, p->s);
6743 line_breakcheck();
6744 }
6745
6746 return OK;
6747}
6748
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006749/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006750 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006751 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006752 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006753 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006754 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006755list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006756 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006757 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006758 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006759 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006760 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006761{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006762 garray_T join_ga;
6763 int retval;
6764 join_T *p;
6765 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006766
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006767 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6768 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6769
6770 /* Dispose each item in join_ga. */
6771 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006772 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006773 p = (join_T *)join_ga.ga_data;
6774 for (i = 0; i < join_ga.ga_len; ++i)
6775 {
6776 vim_free(p->tofree);
6777 ++p;
6778 }
6779 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006780 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006781
6782 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006783}
6784
6785/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006786 * Garbage collection for lists and dictionaries.
6787 *
6788 * We use reference counts to be able to free most items right away when they
6789 * are no longer used. But for composite items it's possible that it becomes
6790 * unused while the reference count is > 0: When there is a recursive
6791 * reference. Example:
6792 * :let l = [1, 2, 3]
6793 * :let d = {9: l}
6794 * :let l[1] = d
6795 *
6796 * Since this is quite unusual we handle this with garbage collection: every
6797 * once in a while find out which lists and dicts are not referenced from any
6798 * variable.
6799 *
6800 * Here is a good reference text about garbage collection (refers to Python
6801 * but it applies to all reference-counting mechanisms):
6802 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006803 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006804
6805/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006806 * Do garbage collection for lists and dicts.
6807 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006808 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006809 int
6810garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006811{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006812 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813 buf_T *buf;
6814 win_T *wp;
6815 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006816 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006817 int did_free;
6818 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006819#ifdef FEAT_WINDOWS
6820 tabpage_T *tp;
6821#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006822
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006823 /* Only do this once. */
6824 want_garbage_collect = FALSE;
6825 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006826 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006827
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006828 /* We advance by two because we add one for items referenced through
6829 * previous_funccal. */
6830 current_copyID += COPYID_INC;
6831 copyID = current_copyID;
6832
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006833 /*
6834 * 1. Go through all accessible variables and mark all lists and dicts
6835 * with copyID.
6836 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006837
6838 /* Don't free variables in the previous_funccal list unless they are only
6839 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006840 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006841 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6842 {
6843 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6844 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6845 }
6846
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006847 /* script-local variables */
6848 for (i = 1; i <= ga_scripts.ga_len; ++i)
6849 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6850
6851 /* buffer-local variables */
6852 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006853 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854
6855 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006856 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006857 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006858#ifdef FEAT_AUTOCMD
6859 if (aucmd_win != NULL)
6860 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6861#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006863#ifdef FEAT_WINDOWS
6864 /* tabpage-local variables */
6865 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006866 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006867#endif
6868
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 /* global variables */
6870 set_ref_in_ht(&globvarht, copyID);
6871
6872 /* function-local variables */
6873 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6874 {
6875 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6876 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6877 }
6878
Bram Moolenaard812df62008-11-09 12:46:09 +00006879 /* v: vars */
6880 set_ref_in_ht(&vimvarht, copyID);
6881
Bram Moolenaar1dced572012-04-05 16:54:08 +02006882#ifdef FEAT_LUA
6883 set_ref_in_lua(copyID);
6884#endif
6885
Bram Moolenaardb913952012-06-29 12:54:53 +02006886#ifdef FEAT_PYTHON
6887 set_ref_in_python(copyID);
6888#endif
6889
6890#ifdef FEAT_PYTHON3
6891 set_ref_in_python3(copyID);
6892#endif
6893
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006894 /*
6895 * 2. Free lists and dictionaries that are not referenced.
6896 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006897 did_free = free_unref_items(copyID);
6898
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006899 /*
6900 * 3. Check if any funccal can be freed now.
6901 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006902 for (pfc = &previous_funccal; *pfc != NULL; )
6903 {
6904 if (can_free_funccal(*pfc, copyID))
6905 {
6906 fc = *pfc;
6907 *pfc = fc->caller;
6908 free_funccal(fc, TRUE);
6909 did_free = TRUE;
6910 did_free_funccal = TRUE;
6911 }
6912 else
6913 pfc = &(*pfc)->caller;
6914 }
6915 if (did_free_funccal)
6916 /* When a funccal was freed some more items might be garbage
6917 * collected, so run again. */
6918 (void)garbage_collect();
6919
6920 return did_free;
6921}
6922
6923/*
6924 * Free lists and dictionaries that are no longer referenced.
6925 */
6926 static int
6927free_unref_items(copyID)
6928 int copyID;
6929{
6930 dict_T *dd;
6931 list_T *ll;
6932 int did_free = FALSE;
6933
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006934 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006935 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 */
6937 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006938 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006940 /* Free the Dictionary and ordinary items it contains, but don't
6941 * recurse into Lists and Dictionaries, they will be in the list
6942 * of dicts or list of lists. */
6943 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944 did_free = TRUE;
6945
6946 /* restart, next dict may also have been freed */
6947 dd = first_dict;
6948 }
6949 else
6950 dd = dd->dv_used_next;
6951
6952 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006953 * Go through the list of lists and free items without the copyID.
6954 * But don't free a list that has a watcher (used in a for loop), these
6955 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006956 */
6957 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006958 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6959 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006961 /* Free the List and ordinary items it contains, but don't recurse
6962 * into Lists and Dictionaries, they will be in the list of dicts
6963 * or list of lists. */
6964 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965 did_free = TRUE;
6966
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006967 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 ll = first_list;
6969 }
6970 else
6971 ll = ll->lv_used_next;
6972
6973 return did_free;
6974}
6975
6976/*
6977 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6978 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006979 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980set_ref_in_ht(ht, copyID)
6981 hashtab_T *ht;
6982 int copyID;
6983{
6984 int todo;
6985 hashitem_T *hi;
6986
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006987 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 for (hi = ht->ht_array; todo > 0; ++hi)
6989 if (!HASHITEM_EMPTY(hi))
6990 {
6991 --todo;
6992 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6993 }
6994}
6995
6996/*
6997 * Mark all lists and dicts referenced through list "l" with "copyID".
6998 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006999 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000set_ref_in_list(l, copyID)
7001 list_T *l;
7002 int copyID;
7003{
7004 listitem_T *li;
7005
7006 for (li = l->lv_first; li != NULL; li = li->li_next)
7007 set_ref_in_item(&li->li_tv, copyID);
7008}
7009
7010/*
7011 * Mark all lists and dicts referenced through typval "tv" with "copyID".
7012 */
Bram Moolenaardb913952012-06-29 12:54:53 +02007013 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014set_ref_in_item(tv, copyID)
7015 typval_T *tv;
7016 int copyID;
7017{
7018 dict_T *dd;
7019 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007020
7021 switch (tv->v_type)
7022 {
7023 case VAR_DICT:
7024 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007025 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007026 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027 /* Didn't see this dict yet. */
7028 dd->dv_copyID = copyID;
7029 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007030 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007032
7033 case VAR_LIST:
7034 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007035 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007036 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 /* Didn't see this list yet. */
7038 ll->lv_copyID = copyID;
7039 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007040 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007042 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007044}
7045
7046/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007047 * Allocate an empty header for a dictionary.
7048 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007049 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050dict_alloc()
7051{
Bram Moolenaar33570922005-01-25 22:26:29 +00007052 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007053
Bram Moolenaar33570922005-01-25 22:26:29 +00007054 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007055 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007056 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007057 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058 if (first_dict != NULL)
7059 first_dict->dv_used_prev = d;
7060 d->dv_used_next = first_dict;
7061 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007062 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063
Bram Moolenaar33570922005-01-25 22:26:29 +00007064 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007065 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007066 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007067 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007068 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007069 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007070 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007071}
7072
7073/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007074 * Allocate an empty dict for a return value.
7075 * Returns OK or FAIL.
7076 */
7077 static int
7078rettv_dict_alloc(rettv)
7079 typval_T *rettv;
7080{
7081 dict_T *d = dict_alloc();
7082
7083 if (d == NULL)
7084 return FAIL;
7085
7086 rettv->vval.v_dict = d;
7087 rettv->v_type = VAR_DICT;
7088 ++d->dv_refcount;
7089 return OK;
7090}
7091
7092
7093/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094 * Unreference a Dictionary: decrement the reference count and free it when it
7095 * becomes zero.
7096 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007097 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007101 if (d != NULL && --d->dv_refcount <= 0)
7102 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103}
7104
7105/*
7106 * Free a Dictionary, including all items it contains.
7107 * Ignores the reference count.
7108 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007109 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007110dict_free(d, recurse)
7111 dict_T *d;
7112 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007113{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007115 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007116 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007118 /* Remove the dict from the list of dicts for garbage collection. */
7119 if (d->dv_used_prev == NULL)
7120 first_dict = d->dv_used_next;
7121 else
7122 d->dv_used_prev->dv_used_next = d->dv_used_next;
7123 if (d->dv_used_next != NULL)
7124 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7125
7126 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007127 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007128 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007130 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 if (!HASHITEM_EMPTY(hi))
7132 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007133 /* Remove the item before deleting it, just in case there is
7134 * something recursive causing trouble. */
7135 di = HI2DI(hi);
7136 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007137 if (recurse || (di->di_tv.v_type != VAR_LIST
7138 && di->di_tv.v_type != VAR_DICT))
7139 clear_tv(&di->di_tv);
7140 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 --todo;
7142 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007143 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007144 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007145 vim_free(d);
7146}
7147
7148/*
7149 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007150 * The "key" is copied to the new item.
7151 * Note that the value of the item "di_tv" still needs to be initialized!
7152 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007153 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007154 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155dictitem_alloc(key)
7156 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007157{
Bram Moolenaar33570922005-01-25 22:26:29 +00007158 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007160 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007161 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007162 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007164 di->di_flags = 0;
7165 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007166 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007167}
7168
7169/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 * Make a copy of a Dictionary item.
7171 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007172 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007174 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007175{
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007177
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007178 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7179 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 if (di != NULL)
7181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007183 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007184 copy_tv(&org->di_tv, &di->di_tv);
7185 }
7186 return di;
7187}
7188
7189/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 * Remove item "item" from Dictionary "dict" and free it.
7191 */
7192 static void
7193dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007194 dict_T *dict;
7195 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196{
Bram Moolenaar33570922005-01-25 22:26:29 +00007197 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 if (HASHITEM_EMPTY(hi))
7201 EMSG2(_(e_intern2), "dictitem_remove()");
7202 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007203 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204 dictitem_free(item);
7205}
7206
7207/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208 * Free a dict item. Also clears the value.
7209 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007210 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007212 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 clear_tv(&item->di_tv);
7215 vim_free(item);
7216}
7217
7218/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007219 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7220 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007221 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007222 * Returns NULL when out of memory.
7223 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007224 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007225dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007227 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007228 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007229{
Bram Moolenaar33570922005-01-25 22:26:29 +00007230 dict_T *copy;
7231 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007232 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007233 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007234
7235 if (orig == NULL)
7236 return NULL;
7237
7238 copy = dict_alloc();
7239 if (copy != NULL)
7240 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007241 if (copyID != 0)
7242 {
7243 orig->dv_copyID = copyID;
7244 orig->dv_copydict = copy;
7245 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007246 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007247 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007248 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007250 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007251 --todo;
7252
7253 di = dictitem_alloc(hi->hi_key);
7254 if (di == NULL)
7255 break;
7256 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007257 {
7258 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7259 copyID) == FAIL)
7260 {
7261 vim_free(di);
7262 break;
7263 }
7264 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007265 else
7266 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7267 if (dict_add(copy, di) == FAIL)
7268 {
7269 dictitem_free(di);
7270 break;
7271 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007272 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274
Bram Moolenaare9a41262005-01-15 22:18:47 +00007275 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007276 if (todo > 0)
7277 {
7278 dict_unref(copy);
7279 copy = NULL;
7280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281 }
7282
7283 return copy;
7284}
7285
7286/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007288 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007290 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 dict_T *d;
7293 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294{
Bram Moolenaar33570922005-01-25 22:26:29 +00007295 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296}
7297
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007299 * Add a number or string entry to dictionary "d".
7300 * When "str" is NULL use number "nr", otherwise use "str".
7301 * Returns FAIL when out of memory and when key already exists.
7302 */
7303 int
7304dict_add_nr_str(d, key, nr, str)
7305 dict_T *d;
7306 char *key;
7307 long nr;
7308 char_u *str;
7309{
7310 dictitem_T *item;
7311
7312 item = dictitem_alloc((char_u *)key);
7313 if (item == NULL)
7314 return FAIL;
7315 item->di_tv.v_lock = 0;
7316 if (str == NULL)
7317 {
7318 item->di_tv.v_type = VAR_NUMBER;
7319 item->di_tv.vval.v_number = nr;
7320 }
7321 else
7322 {
7323 item->di_tv.v_type = VAR_STRING;
7324 item->di_tv.vval.v_string = vim_strsave(str);
7325 }
7326 if (dict_add(d, item) == FAIL)
7327 {
7328 dictitem_free(item);
7329 return FAIL;
7330 }
7331 return OK;
7332}
7333
7334/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007335 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007336 * Returns FAIL when out of memory and when key already exists.
7337 */
7338 int
7339dict_add_list(d, key, list)
7340 dict_T *d;
7341 char *key;
7342 list_T *list;
7343{
7344 dictitem_T *item;
7345
7346 item = dictitem_alloc((char_u *)key);
7347 if (item == NULL)
7348 return FAIL;
7349 item->di_tv.v_lock = 0;
7350 item->di_tv.v_type = VAR_LIST;
7351 item->di_tv.vval.v_list = list;
7352 if (dict_add(d, item) == FAIL)
7353 {
7354 dictitem_free(item);
7355 return FAIL;
7356 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007357 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007358 return OK;
7359}
7360
7361/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362 * Get the number of items in a Dictionary.
7363 */
7364 static long
7365dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007366 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007367{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 if (d == NULL)
7369 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007370 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371}
7372
7373/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007374 * Find item "key[len]" in Dictionary "d".
7375 * If "len" is negative use strlen(key).
7376 * Returns NULL when not found.
7377 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007378 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007379dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007381 char_u *key;
7382 int len;
7383{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007384#define AKEYLEN 200
7385 char_u buf[AKEYLEN];
7386 char_u *akey;
7387 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007389
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 if (len < 0)
7391 akey = key;
7392 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007394 tofree = akey = vim_strnsave(key, len);
7395 if (akey == NULL)
7396 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007397 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 else
7399 {
7400 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007401 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 akey = buf;
7403 }
7404
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007406 vim_free(tofree);
7407 if (HASHITEM_EMPTY(hi))
7408 return NULL;
7409 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410}
7411
7412/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007413 * Get a string item from a dictionary.
7414 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007415 * Returns NULL if the entry doesn't exist or out of memory.
7416 */
7417 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007418get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007419 dict_T *d;
7420 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007421 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007422{
7423 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007424 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007425
7426 di = dict_find(d, key, -1);
7427 if (di == NULL)
7428 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007429 s = get_tv_string(&di->di_tv);
7430 if (save && s != NULL)
7431 s = vim_strsave(s);
7432 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007433}
7434
7435/*
7436 * Get a number item from a dictionary.
7437 * Returns 0 if the entry doesn't exist or out of memory.
7438 */
7439 long
7440get_dict_number(d, key)
7441 dict_T *d;
7442 char_u *key;
7443{
7444 dictitem_T *di;
7445
7446 di = dict_find(d, key, -1);
7447 if (di == NULL)
7448 return 0;
7449 return get_tv_number(&di->di_tv);
7450}
7451
7452/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007453 * Return an allocated string with the string representation of a Dictionary.
7454 * May return NULL.
7455 */
7456 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007457dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007459 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460{
7461 garray_T ga;
7462 int first = TRUE;
7463 char_u *tofree;
7464 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007465 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007466 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007467 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007468 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007469
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007470 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471 return NULL;
7472 ga_init2(&ga, (int)sizeof(char), 80);
7473 ga_append(&ga, '{');
7474
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007475 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007476 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007478 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 --todo;
7481
7482 if (first)
7483 first = FALSE;
7484 else
7485 ga_concat(&ga, (char_u *)", ");
7486
7487 tofree = string_quote(hi->hi_key, FALSE);
7488 if (tofree != NULL)
7489 {
7490 ga_concat(&ga, tofree);
7491 vim_free(tofree);
7492 }
7493 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007494 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495 if (s != NULL)
7496 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007498 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007499 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007500 line_breakcheck();
7501
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007504 if (todo > 0)
7505 {
7506 vim_free(ga.ga_data);
7507 return NULL;
7508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509
7510 ga_append(&ga, '}');
7511 ga_append(&ga, NUL);
7512 return (char_u *)ga.ga_data;
7513}
7514
7515/*
7516 * Allocate a variable for a Dictionary and fill it from "*arg".
7517 * Return OK or FAIL. Returns NOTDONE for {expr}.
7518 */
7519 static int
7520get_dict_tv(arg, rettv, evaluate)
7521 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 int evaluate;
7524{
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 dict_T *d = NULL;
7526 typval_T tvkey;
7527 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007528 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532
7533 /*
7534 * First check if it's not a curly-braces thing: {expr}.
7535 * Must do this without evaluating, otherwise a function may be called
7536 * twice. Unfortunately this means we need to call eval1() twice for the
7537 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007538 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007540 if (*start != '}')
7541 {
7542 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7543 return FAIL;
7544 if (*start == '}')
7545 return NOTDONE;
7546 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007547
7548 if (evaluate)
7549 {
7550 d = dict_alloc();
7551 if (d == NULL)
7552 return FAIL;
7553 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 tvkey.v_type = VAR_UNKNOWN;
7555 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556
7557 *arg = skipwhite(*arg + 1);
7558 while (**arg != '}' && **arg != NUL)
7559 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007560 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561 goto failret;
7562 if (**arg != ':')
7563 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007564 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007566 goto failret;
7567 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007568 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007570 key = get_tv_string_buf_chk(&tvkey, buf);
7571 if (key == NULL || *key == NUL)
7572 {
7573 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7574 if (key != NULL)
7575 EMSG(_(e_emptykey));
7576 clear_tv(&tvkey);
7577 goto failret;
7578 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580
7581 *arg = skipwhite(*arg + 1);
7582 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7583 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007584 if (evaluate)
7585 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586 goto failret;
7587 }
7588 if (evaluate)
7589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591 if (item != NULL)
7592 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007593 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595 clear_tv(&tv);
7596 goto failret;
7597 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007598 item = dictitem_alloc(key);
7599 clear_tv(&tvkey);
7600 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007603 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007604 if (dict_add(d, item) == FAIL)
7605 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 }
7607 }
7608
7609 if (**arg == '}')
7610 break;
7611 if (**arg != ',')
7612 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007613 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 goto failret;
7615 }
7616 *arg = skipwhite(*arg + 1);
7617 }
7618
7619 if (**arg != '}')
7620 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007621 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622failret:
7623 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007624 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625 return FAIL;
7626 }
7627
7628 *arg = skipwhite(*arg + 1);
7629 if (evaluate)
7630 {
7631 rettv->v_type = VAR_DICT;
7632 rettv->vval.v_dict = d;
7633 ++d->dv_refcount;
7634 }
7635
7636 return OK;
7637}
7638
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007640 * Return a string with the string representation of a variable.
7641 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007642 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007643 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007644 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007645 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007646 */
7647 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007648echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007649 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007651 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007652 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007653{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654 static int recurse = 0;
7655 char_u *r = NULL;
7656
Bram Moolenaar33570922005-01-25 22:26:29 +00007657 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007658 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007659 if (!did_echo_string_emsg)
7660 {
7661 /* Only give this message once for a recursive call to avoid
7662 * flooding the user with errors. And stop iterating over lists
7663 * and dicts. */
7664 did_echo_string_emsg = TRUE;
7665 EMSG(_("E724: variable nested too deep for displaying"));
7666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007667 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007668 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 }
7670 ++recurse;
7671
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007672 switch (tv->v_type)
7673 {
7674 case VAR_FUNC:
7675 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 r = tv->vval.v_string;
7677 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007678
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007679 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007680 if (tv->vval.v_list == NULL)
7681 {
7682 *tofree = NULL;
7683 r = NULL;
7684 }
7685 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7686 {
7687 *tofree = NULL;
7688 r = (char_u *)"[...]";
7689 }
7690 else
7691 {
7692 tv->vval.v_list->lv_copyID = copyID;
7693 *tofree = list2string(tv, copyID);
7694 r = *tofree;
7695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007696 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007697
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007699 if (tv->vval.v_dict == NULL)
7700 {
7701 *tofree = NULL;
7702 r = NULL;
7703 }
7704 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7705 {
7706 *tofree = NULL;
7707 r = (char_u *)"{...}";
7708 }
7709 else
7710 {
7711 tv->vval.v_dict->dv_copyID = copyID;
7712 *tofree = dict2string(tv, copyID);
7713 r = *tofree;
7714 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007715 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007716
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007717 case VAR_STRING:
7718 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007719 *tofree = NULL;
7720 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007721 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007722
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007723#ifdef FEAT_FLOAT
7724 case VAR_FLOAT:
7725 *tofree = NULL;
7726 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7727 r = numbuf;
7728 break;
7729#endif
7730
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007731 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007732 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007733 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007734 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007735
Bram Moolenaar8502c702014-06-17 12:51:16 +02007736 if (--recurse == 0)
7737 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007739}
7740
7741/*
7742 * Return a string with the string representation of a variable.
7743 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7744 * "numbuf" is used for a number.
7745 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007746 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007747 */
7748 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007749tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007750 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007751 char_u **tofree;
7752 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007753 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007754{
7755 switch (tv->v_type)
7756 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007757 case VAR_FUNC:
7758 *tofree = string_quote(tv->vval.v_string, TRUE);
7759 return *tofree;
7760 case VAR_STRING:
7761 *tofree = string_quote(tv->vval.v_string, FALSE);
7762 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007763#ifdef FEAT_FLOAT
7764 case VAR_FLOAT:
7765 *tofree = NULL;
7766 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7767 return numbuf;
7768#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007769 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007770 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007772 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007773 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007775 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007776 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007777}
7778
7779/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007780 * Return string "str" in ' quotes, doubling ' characters.
7781 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007783 */
7784 static char_u *
7785string_quote(str, function)
7786 char_u *str;
7787 int function;
7788{
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007790 char_u *p, *r, *s;
7791
Bram Moolenaar33570922005-01-25 22:26:29 +00007792 len = (function ? 13 : 3);
7793 if (str != NULL)
7794 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007795 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 for (p = str; *p != NUL; mb_ptr_adv(p))
7797 if (*p == '\'')
7798 ++len;
7799 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007800 s = r = alloc(len);
7801 if (r != NULL)
7802 {
7803 if (function)
7804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007805 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007806 r += 10;
7807 }
7808 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007809 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 if (str != NULL)
7811 for (p = str; *p != NUL; )
7812 {
7813 if (*p == '\'')
7814 *r++ = '\'';
7815 MB_COPY_CHAR(p, r);
7816 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007817 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007818 if (function)
7819 *r++ = ')';
7820 *r++ = NUL;
7821 }
7822 return s;
7823}
7824
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007825#ifdef FEAT_FLOAT
7826/*
7827 * Convert the string "text" to a floating point number.
7828 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7829 * this always uses a decimal point.
7830 * Returns the length of the text that was consumed.
7831 */
7832 static int
7833string2float(text, value)
7834 char_u *text;
7835 float_T *value; /* result stored here */
7836{
7837 char *s = (char *)text;
7838 float_T f;
7839
7840 f = strtod(s, &s);
7841 *value = f;
7842 return (int)((char_u *)s - text);
7843}
7844#endif
7845
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 * Get the value of an environment variable.
7848 * "arg" is pointing to the '$'. It is advanced to after the name.
7849 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007850 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 */
7852 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 int evaluate;
7857{
7858 char_u *string = NULL;
7859 int len;
7860 int cc;
7861 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007862 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863
7864 ++*arg;
7865 name = *arg;
7866 len = get_env_len(arg);
7867 if (evaluate)
7868 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007869 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007870 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007871
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007872 cc = name[len];
7873 name[len] = NUL;
7874 /* first try vim_getenv(), fast for normal environment vars */
7875 string = vim_getenv(name, &mustfree);
7876 if (string != NULL && *string != NUL)
7877 {
7878 if (!mustfree)
7879 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007881 else
7882 {
7883 if (mustfree)
7884 vim_free(string);
7885
7886 /* next try expanding things like $VIM and ${HOME} */
7887 string = expand_env_save(name - 1);
7888 if (string != NULL && *string == '$')
7889 {
7890 vim_free(string);
7891 string = NULL;
7892 }
7893 }
7894 name[len] = cc;
7895
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896 rettv->v_type = VAR_STRING;
7897 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 }
7899
7900 return OK;
7901}
7902
7903/*
7904 * Array with names and number of arguments of all internal functions
7905 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7906 */
7907static struct fst
7908{
7909 char *f_name; /* function name */
7910 char f_min_argc; /* minimal number of arguments */
7911 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007912 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007913 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914} functions[] =
7915{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007916#ifdef FEAT_FLOAT
7917 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007918 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007920 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007921 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"append", 2, 2, f_append},
7923 {"argc", 0, 0, f_argc},
7924 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007925 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007926 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007927#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007928 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007929 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007930 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007933 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"bufexists", 1, 1, f_bufexists},
7935 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7936 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7937 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7938 {"buflisted", 1, 1, f_buflisted},
7939 {"bufloaded", 1, 1, f_bufloaded},
7940 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007941 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {"bufwinnr", 1, 1, f_bufwinnr},
7943 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007944 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007945 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007946 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007947#ifdef FEAT_FLOAT
7948 {"ceil", 1, 1, f_ceil},
7949#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007950 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007951 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007953 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007955#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007956 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007957 {"complete_add", 1, 1, f_complete_add},
7958 {"complete_check", 0, 0, f_complete_check},
7959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007961 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007962#ifdef FEAT_FLOAT
7963 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007964 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007966 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007968 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007969 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"delete", 1, 1, f_delete},
7971 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007972 {"diff_filler", 1, 1, f_diff_filler},
7973 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007974 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007976 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"eventhandler", 0, 0, f_eventhandler},
7978 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007979 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007981#ifdef FEAT_FLOAT
7982 {"exp", 1, 1, f_exp},
7983#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007984 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007985 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007986 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7988 {"filereadable", 1, 1, f_filereadable},
7989 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007991 {"finddir", 1, 3, f_finddir},
7992 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007993#ifdef FEAT_FLOAT
7994 {"float2nr", 1, 1, f_float2nr},
7995 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007996 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007997#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007998 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"fnamemodify", 2, 2, f_fnamemodify},
8000 {"foldclosed", 1, 1, f_foldclosed},
8001 {"foldclosedend", 1, 1, f_foldclosedend},
8002 {"foldlevel", 1, 1, f_foldlevel},
8003 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008004 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008006 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008007 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008008 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008009 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008010 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 {"getchar", 0, 1, f_getchar},
8012 {"getcharmod", 0, 0, f_getcharmod},
8013 {"getcmdline", 0, 0, f_getcmdline},
8014 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008015 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008016 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008017 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008019 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008020 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 {"getfsize", 1, 1, f_getfsize},
8022 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008023 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008024 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008025 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008026 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008027 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008028 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008029 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008030 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008032 {"gettabvar", 2, 3, f_gettabvar},
8033 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 {"getwinposx", 0, 0, f_getwinposx},
8035 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008036 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008037 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02008038 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008040 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008041 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008042 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8044 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8045 {"histadd", 2, 2, f_histadd},
8046 {"histdel", 1, 2, f_histdel},
8047 {"histget", 1, 2, f_histget},
8048 {"histnr", 1, 1, f_histnr},
8049 {"hlID", 1, 1, f_hlID},
8050 {"hlexists", 1, 1, f_hlexists},
8051 {"hostname", 0, 0, f_hostname},
8052 {"iconv", 3, 3, f_iconv},
8053 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008054 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008055 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008057 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"inputrestore", 0, 0, f_inputrestore},
8059 {"inputsave", 0, 0, f_inputsave},
8060 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008061 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008062 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008064 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008065 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008066 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008067 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008069 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"libcall", 3, 3, f_libcall},
8071 {"libcallnr", 3, 3, f_libcallnr},
8072 {"line", 1, 1, f_line},
8073 {"line2byte", 1, 1, f_line2byte},
8074 {"lispindent", 1, 1, f_lispindent},
8075 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008076#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008077 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078 {"log10", 1, 1, f_log10},
8079#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008080#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008081 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008082#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008083 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008084 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008085 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008086 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008087 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008088 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008089 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008090 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008091 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008092 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008093 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008094 {"max", 1, 1, f_max},
8095 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008096#ifdef vim_mkdir
8097 {"mkdir", 1, 3, f_mkdir},
8098#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008100#ifdef FEAT_MZSCHEME
8101 {"mzeval", 1, 1, f_mzeval},
8102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008104 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008105 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008106 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008107#ifdef FEAT_FLOAT
8108 {"pow", 2, 2, f_pow},
8109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008111 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008112 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008113#ifdef FEAT_PYTHON3
8114 {"py3eval", 1, 1, f_py3eval},
8115#endif
8116#ifdef FEAT_PYTHON
8117 {"pyeval", 1, 1, f_pyeval},
8118#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008119 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008120 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008121 {"reltime", 0, 2, f_reltime},
8122 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"remote_expr", 2, 3, f_remote_expr},
8124 {"remote_foreground", 1, 1, f_remote_foreground},
8125 {"remote_peek", 1, 2, f_remote_peek},
8126 {"remote_read", 1, 1, f_remote_read},
8127 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008128 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008130 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008132 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008133#ifdef FEAT_FLOAT
8134 {"round", 1, 1, f_round},
8135#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008136 {"screenattr", 2, 2, f_screenattr},
8137 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008138 {"screencol", 0, 0, f_screencol},
8139 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008140 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008141 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008142 {"searchpair", 3, 7, f_searchpair},
8143 {"searchpairpos", 3, 7, f_searchpairpos},
8144 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {"server2client", 2, 2, f_server2client},
8146 {"serverlist", 0, 0, f_serverlist},
8147 {"setbufvar", 3, 3, f_setbufvar},
8148 {"setcmdpos", 1, 1, f_setcmdpos},
8149 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008150 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008151 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008152 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008153 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008155 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008156 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008158#ifdef FEAT_CRYPT
8159 {"sha256", 1, 1, f_sha256},
8160#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008161 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008162 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#ifdef FEAT_FLOAT
8165 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008166 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008167#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008168 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008169 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008170 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008171 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008172 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008173#ifdef FEAT_FLOAT
8174 {"sqrt", 1, 1, f_sqrt},
8175 {"str2float", 1, 1, f_str2float},
8176#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008177 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008178 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008179 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180#ifdef HAVE_STRFTIME
8181 {"strftime", 1, 2, f_strftime},
8182#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008183 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008184 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 {"strlen", 1, 1, f_strlen},
8186 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008187 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008189 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008190 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 {"substitute", 4, 4, f_substitute},
8192 {"synID", 3, 3, f_synID},
8193 {"synIDattr", 2, 3, f_synIDattr},
8194 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008195 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008196 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008197 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008198 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008199 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008200 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008201 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008202 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008203 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008204#ifdef FEAT_FLOAT
8205 {"tan", 1, 1, f_tan},
8206 {"tanh", 1, 1, f_tanh},
8207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008209 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"tolower", 1, 1, f_tolower},
8211 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008212 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008213#ifdef FEAT_FLOAT
8214 {"trunc", 1, 1, f_trunc},
8215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008217 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008218 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008219 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008220 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"virtcol", 1, 1, f_virtcol},
8222 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008223 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {"winbufnr", 1, 1, f_winbufnr},
8225 {"wincol", 0, 0, f_wincol},
8226 {"winheight", 1, 1, f_winheight},
8227 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008228 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008230 {"winrestview", 1, 1, f_winrestview},
8231 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008233 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008234 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235};
8236
8237#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8238
8239/*
8240 * Function given to ExpandGeneric() to obtain the list of internal
8241 * or user defined function names.
8242 */
8243 char_u *
8244get_function_name(xp, idx)
8245 expand_T *xp;
8246 int idx;
8247{
8248 static int intidx = -1;
8249 char_u *name;
8250
8251 if (idx == 0)
8252 intidx = -1;
8253 if (intidx < 0)
8254 {
8255 name = get_user_func_name(xp, idx);
8256 if (name != NULL)
8257 return name;
8258 }
8259 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8260 {
8261 STRCPY(IObuff, functions[intidx].f_name);
8262 STRCAT(IObuff, "(");
8263 if (functions[intidx].f_max_argc == 0)
8264 STRCAT(IObuff, ")");
8265 return IObuff;
8266 }
8267
8268 return NULL;
8269}
8270
8271/*
8272 * Function given to ExpandGeneric() to obtain the list of internal or
8273 * user defined variable or function names.
8274 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 char_u *
8276get_expr_name(xp, idx)
8277 expand_T *xp;
8278 int idx;
8279{
8280 static int intidx = -1;
8281 char_u *name;
8282
8283 if (idx == 0)
8284 intidx = -1;
8285 if (intidx < 0)
8286 {
8287 name = get_function_name(xp, idx);
8288 if (name != NULL)
8289 return name;
8290 }
8291 return get_user_var_name(xp, ++intidx);
8292}
8293
8294#endif /* FEAT_CMDL_COMPL */
8295
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008296#if defined(EBCDIC) || defined(PROTO)
8297/*
8298 * Compare struct fst by function name.
8299 */
8300 static int
8301compare_func_name(s1, s2)
8302 const void *s1;
8303 const void *s2;
8304{
8305 struct fst *p1 = (struct fst *)s1;
8306 struct fst *p2 = (struct fst *)s2;
8307
8308 return STRCMP(p1->f_name, p2->f_name);
8309}
8310
8311/*
8312 * Sort the function table by function name.
8313 * The sorting of the table above is ASCII dependant.
8314 * On machines using EBCDIC we have to sort it.
8315 */
8316 static void
8317sortFunctions()
8318{
8319 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8320
8321 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8322}
8323#endif
8324
8325
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326/*
8327 * Find internal function in table above.
8328 * Return index, or -1 if not found
8329 */
8330 static int
8331find_internal_func(name)
8332 char_u *name; /* name of the function */
8333{
8334 int first = 0;
8335 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8336 int cmp;
8337 int x;
8338
8339 /*
8340 * Find the function name in the table. Binary search.
8341 */
8342 while (first <= last)
8343 {
8344 x = first + ((unsigned)(last - first) >> 1);
8345 cmp = STRCMP(name, functions[x].f_name);
8346 if (cmp < 0)
8347 last = x - 1;
8348 else if (cmp > 0)
8349 first = x + 1;
8350 else
8351 return x;
8352 }
8353 return -1;
8354}
8355
8356/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008357 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8358 * name it contains, otherwise return "name".
8359 */
8360 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008361deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008362 char_u *name;
8363 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008364 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008365{
Bram Moolenaar33570922005-01-25 22:26:29 +00008366 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008367 int cc;
8368
8369 cc = name[*lenp];
8370 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008371 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008372 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008374 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008376 {
8377 *lenp = 0;
8378 return (char_u *)""; /* just in case */
8379 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008380 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008381 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008382 }
8383
8384 return name;
8385}
8386
8387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 * Allocate a variable for the result of a function.
8389 * Return OK or FAIL.
8390 */
8391 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008392get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8393 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 char_u *name; /* name of the function */
8395 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 char_u **arg; /* argument, pointing to the '(' */
8398 linenr_T firstline; /* first line of range */
8399 linenr_T lastline; /* last line of range */
8400 int *doesrange; /* return: function handled range */
8401 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008402 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404 char_u *argp;
8405 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008406 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 int argcount = 0; /* number of arguments found */
8408
8409 /*
8410 * Get the arguments.
8411 */
8412 argp = *arg;
8413 while (argcount < MAX_FUNC_ARGS)
8414 {
8415 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8416 if (*argp == ')' || *argp == ',' || *argp == NUL)
8417 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8419 {
8420 ret = FAIL;
8421 break;
8422 }
8423 ++argcount;
8424 if (*argp != ',')
8425 break;
8426 }
8427 if (*argp == ')')
8428 ++argp;
8429 else
8430 ret = FAIL;
8431
8432 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008433 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008434 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008436 {
8437 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008438 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008439 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008440 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
8443 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008444 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445
8446 *arg = skipwhite(argp);
8447 return ret;
8448}
8449
8450
8451/*
8452 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008453 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008454 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 */
8456 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008457call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008458 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008459 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008461 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008463 typval_T *argvars; /* vars for arguments, must have "argcount"
8464 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 linenr_T firstline; /* first line of range */
8466 linenr_T lastline; /* last line of range */
8467 int *doesrange; /* return: function handled range */
8468 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008469 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472#define ERROR_UNKNOWN 0
8473#define ERROR_TOOMANY 1
8474#define ERROR_TOOFEW 2
8475#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008476#define ERROR_DICT 4
8477#define ERROR_NONE 5
8478#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 int error = ERROR_NONE;
8480 int i;
8481 int llen;
8482 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483#define FLEN_FIXED 40
8484 char_u fname_buf[FLEN_FIXED + 1];
8485 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008486 char_u *name;
8487
8488 /* Make a copy of the name, if it comes from a funcref variable it could
8489 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008490 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008491 if (name == NULL)
8492 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
8494 /*
8495 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8496 * Change <SNR>123_name() to K_SNR 123_name().
8497 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 llen = eval_fname_script(name);
8500 if (llen > 0)
8501 {
8502 fname_buf[0] = K_SPECIAL;
8503 fname_buf[1] = KS_EXTRA;
8504 fname_buf[2] = (int)KE_SNR;
8505 i = 3;
8506 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8507 {
8508 if (current_SID <= 0)
8509 error = ERROR_SCRIPT;
8510 else
8511 {
8512 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8513 i = (int)STRLEN(fname_buf);
8514 }
8515 }
8516 if (i + STRLEN(name + llen) < FLEN_FIXED)
8517 {
8518 STRCPY(fname_buf + i, name + llen);
8519 fname = fname_buf;
8520 }
8521 else
8522 {
8523 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8524 if (fname == NULL)
8525 error = ERROR_OTHER;
8526 else
8527 {
8528 mch_memmove(fname, fname_buf, (size_t)i);
8529 STRCPY(fname + i, name + llen);
8530 }
8531 }
8532 }
8533 else
8534 fname = name;
8535
8536 *doesrange = FALSE;
8537
8538
8539 /* execute the function if no errors detected and executing */
8540 if (evaluate && error == ERROR_NONE)
8541 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008542 char_u *rfname = fname;
8543
8544 /* Ignore "g:" before a function name. */
8545 if (fname[0] == 'g' && fname[1] == ':')
8546 rfname = fname + 2;
8547
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008548 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8549 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 error = ERROR_UNKNOWN;
8551
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008552 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 {
8554 /*
8555 * User defined function.
8556 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008557 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008558
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008560 /* Trigger FuncUndefined event, may load the function. */
8561 if (fp == NULL
8562 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008563 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008564 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008566 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008567 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 }
8569#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008570 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008571 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008572 {
8573 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008574 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008575 }
8576
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 if (fp != NULL)
8578 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008579 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008581 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008583 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008585 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008586 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 else
8588 {
8589 /*
8590 * Call the user function.
8591 * Save and restore search patterns, script variables and
8592 * redo buffer.
8593 */
8594 save_search_patterns();
8595 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008596 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008597 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008598 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008599 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8600 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8601 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008602 /* Function was unreferenced while being used, free it
8603 * now. */
8604 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 restoreRedobuff();
8606 restore_search_patterns();
8607 error = ERROR_NONE;
8608 }
8609 }
8610 }
8611 else
8612 {
8613 /*
8614 * Find the function name in the table, call its implementation.
8615 */
8616 i = find_internal_func(fname);
8617 if (i >= 0)
8618 {
8619 if (argcount < functions[i].f_min_argc)
8620 error = ERROR_TOOFEW;
8621 else if (argcount > functions[i].f_max_argc)
8622 error = ERROR_TOOMANY;
8623 else
8624 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008625 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008626 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 error = ERROR_NONE;
8628 }
8629 }
8630 }
8631 /*
8632 * The function call (or "FuncUndefined" autocommand sequence) might
8633 * have been aborted by an error, an interrupt, or an explicitly thrown
8634 * exception that has not been caught so far. This situation can be
8635 * tested for by calling aborting(). For an error in an internal
8636 * function or for the "E132" error in call_user_func(), however, the
8637 * throw point at which the "force_abort" flag (temporarily reset by
8638 * emsg()) is normally updated has not been reached yet. We need to
8639 * update that flag first to make aborting() reliable.
8640 */
8641 update_force_abort();
8642 }
8643 if (error == ERROR_NONE)
8644 ret = OK;
8645
8646 /*
8647 * Report an error unless the argument evaluation or function call has been
8648 * cancelled due to an aborting error, an interrupt, or an exception.
8649 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008650 if (!aborting())
8651 {
8652 switch (error)
8653 {
8654 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008655 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008656 break;
8657 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008658 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008659 break;
8660 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008661 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008662 name);
8663 break;
8664 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008665 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008666 name);
8667 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008668 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008669 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008670 name);
8671 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008672 }
8673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 if (fname != name && fname != fname_buf)
8676 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008677 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678
8679 return ret;
8680}
8681
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008682/*
8683 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008684 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008685 */
8686 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008687emsg_funcname(ermsg, name)
8688 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008689 char_u *name;
8690{
8691 char_u *p;
8692
8693 if (*name == K_SPECIAL)
8694 p = concat_str((char_u *)"<SNR>", name + 3);
8695 else
8696 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008697 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008698 if (p != name)
8699 vim_free(p);
8700}
8701
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008702/*
8703 * Return TRUE for a non-zero Number and a non-empty String.
8704 */
8705 static int
8706non_zero_arg(argvars)
8707 typval_T *argvars;
8708{
8709 return ((argvars[0].v_type == VAR_NUMBER
8710 && argvars[0].vval.v_number != 0)
8711 || (argvars[0].v_type == VAR_STRING
8712 && argvars[0].vval.v_string != NULL
8713 && *argvars[0].vval.v_string != NUL));
8714}
8715
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716/*********************************************
8717 * Implementation of the built-in functions
8718 */
8719
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008720#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008721static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8722
8723/*
8724 * Get the float value of "argvars[0]" into "f".
8725 * Returns FAIL when the argument is not a Number or Float.
8726 */
8727 static int
8728get_float_arg(argvars, f)
8729 typval_T *argvars;
8730 float_T *f;
8731{
8732 if (argvars[0].v_type == VAR_FLOAT)
8733 {
8734 *f = argvars[0].vval.v_float;
8735 return OK;
8736 }
8737 if (argvars[0].v_type == VAR_NUMBER)
8738 {
8739 *f = (float_T)argvars[0].vval.v_number;
8740 return OK;
8741 }
8742 EMSG(_("E808: Number or Float required"));
8743 return FAIL;
8744}
8745
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008746/*
8747 * "abs(expr)" function
8748 */
8749 static void
8750f_abs(argvars, rettv)
8751 typval_T *argvars;
8752 typval_T *rettv;
8753{
8754 if (argvars[0].v_type == VAR_FLOAT)
8755 {
8756 rettv->v_type = VAR_FLOAT;
8757 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8758 }
8759 else
8760 {
8761 varnumber_T n;
8762 int error = FALSE;
8763
8764 n = get_tv_number_chk(&argvars[0], &error);
8765 if (error)
8766 rettv->vval.v_number = -1;
8767 else if (n > 0)
8768 rettv->vval.v_number = n;
8769 else
8770 rettv->vval.v_number = -n;
8771 }
8772}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008773
8774/*
8775 * "acos()" function
8776 */
8777 static void
8778f_acos(argvars, rettv)
8779 typval_T *argvars;
8780 typval_T *rettv;
8781{
8782 float_T f;
8783
8784 rettv->v_type = VAR_FLOAT;
8785 if (get_float_arg(argvars, &f) == OK)
8786 rettv->vval.v_float = acos(f);
8787 else
8788 rettv->vval.v_float = 0.0;
8789}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008790#endif
8791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008793 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 */
8795 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008796f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008797 typval_T *argvars;
8798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008803 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008805 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008806 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008807 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008809 }
8810 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008811 EMSG(_(e_listreq));
8812}
8813
8814/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008815 * "and(expr, expr)" function
8816 */
8817 static void
8818f_and(argvars, rettv)
8819 typval_T *argvars;
8820 typval_T *rettv;
8821{
8822 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8823 & get_tv_number_chk(&argvars[1], NULL);
8824}
8825
8826/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008827 * "append(lnum, string/list)" function
8828 */
8829 static void
8830f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008831 typval_T *argvars;
8832 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008833{
8834 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008835 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008836 list_T *l = NULL;
8837 listitem_T *li = NULL;
8838 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008839 long added = 0;
8840
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008841 /* When coming here from Insert mode, sync undo, so that this can be
8842 * undone separately from what was previously inserted. */
8843 if (u_sync_once == 2)
8844 {
8845 u_sync_once = 1; /* notify that u_sync() was called */
8846 u_sync(TRUE);
8847 }
8848
Bram Moolenaar0d660222005-01-07 21:51:51 +00008849 lnum = get_tv_lnum(argvars);
8850 if (lnum >= 0
8851 && lnum <= curbuf->b_ml.ml_line_count
8852 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008853 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008854 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008855 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008856 l = argvars[1].vval.v_list;
8857 if (l == NULL)
8858 return;
8859 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008860 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008861 for (;;)
8862 {
8863 if (l == NULL)
8864 tv = &argvars[1]; /* append a string */
8865 else if (li == NULL)
8866 break; /* end of list */
8867 else
8868 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008869 line = get_tv_string_chk(tv);
8870 if (line == NULL) /* type error */
8871 {
8872 rettv->vval.v_number = 1; /* Failed */
8873 break;
8874 }
8875 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008876 ++added;
8877 if (l == NULL)
8878 break;
8879 li = li->li_next;
8880 }
8881
8882 appended_lines_mark(lnum, added);
8883 if (curwin->w_cursor.lnum > lnum)
8884 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008886 else
8887 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888}
8889
8890/*
8891 * "argc()" function
8892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008895 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008898 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899}
8900
8901/*
8902 * "argidx()" function
8903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008905f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008906 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910}
8911
8912/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008913 * "arglistid()" function
8914 */
8915 static void
8916f_arglistid(argvars, rettv)
8917 typval_T *argvars UNUSED;
8918 typval_T *rettv;
8919{
8920 win_T *wp;
8921 tabpage_T *tp = NULL;
8922 long n;
8923
8924 rettv->vval.v_number = -1;
8925 if (argvars[0].v_type != VAR_UNKNOWN)
8926 {
8927 if (argvars[1].v_type != VAR_UNKNOWN)
8928 {
8929 n = get_tv_number(&argvars[1]);
8930 if (n >= 0)
8931 tp = find_tabpage(n);
8932 }
8933 else
8934 tp = curtab;
8935
8936 if (tp != NULL)
8937 {
8938 wp = find_win_by_nr(&argvars[0], tp);
8939 if (wp != NULL)
8940 rettv->vval.v_number = wp->w_alist->id;
8941 }
8942 }
8943 else
8944 rettv->vval.v_number = curwin->w_alist->id;
8945}
8946
8947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 * "argv(nr)" function
8949 */
8950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008952 typval_T *argvars;
8953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954{
8955 int idx;
8956
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008957 if (argvars[0].v_type != VAR_UNKNOWN)
8958 {
8959 idx = get_tv_number_chk(&argvars[0], NULL);
8960 if (idx >= 0 && idx < ARGCOUNT)
8961 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8962 else
8963 rettv->vval.v_string = NULL;
8964 rettv->v_type = VAR_STRING;
8965 }
8966 else if (rettv_list_alloc(rettv) == OK)
8967 for (idx = 0; idx < ARGCOUNT; ++idx)
8968 list_append_string(rettv->vval.v_list,
8969 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970}
8971
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008972#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008973/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008974 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008975 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008976 static void
8977f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008978 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008979 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008980{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008981 float_T f;
8982
8983 rettv->v_type = VAR_FLOAT;
8984 if (get_float_arg(argvars, &f) == OK)
8985 rettv->vval.v_float = asin(f);
8986 else
8987 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008988}
8989
8990/*
8991 * "atan()" function
8992 */
8993 static void
8994f_atan(argvars, rettv)
8995 typval_T *argvars;
8996 typval_T *rettv;
8997{
8998 float_T f;
8999
9000 rettv->v_type = VAR_FLOAT;
9001 if (get_float_arg(argvars, &f) == OK)
9002 rettv->vval.v_float = atan(f);
9003 else
9004 rettv->vval.v_float = 0.0;
9005}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009006
9007/*
9008 * "atan2()" function
9009 */
9010 static void
9011f_atan2(argvars, rettv)
9012 typval_T *argvars;
9013 typval_T *rettv;
9014{
9015 float_T fx, fy;
9016
9017 rettv->v_type = VAR_FLOAT;
9018 if (get_float_arg(argvars, &fx) == OK
9019 && get_float_arg(&argvars[1], &fy) == OK)
9020 rettv->vval.v_float = atan2(fx, fy);
9021 else
9022 rettv->vval.v_float = 0.0;
9023}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009024#endif
9025
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026/*
9027 * "browse(save, title, initdir, default)" function
9028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009031 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033{
9034#ifdef FEAT_BROWSE
9035 int save;
9036 char_u *title;
9037 char_u *initdir;
9038 char_u *defname;
9039 char_u buf[NUMBUFLEN];
9040 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009041 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009043 save = get_tv_number_chk(&argvars[0], &error);
9044 title = get_tv_string_chk(&argvars[1]);
9045 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9046 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009048 if (error || title == NULL || initdir == NULL || defname == NULL)
9049 rettv->vval.v_string = NULL;
9050 else
9051 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009052 do_browse(save ? BROWSE_SAVE : 0,
9053 title, defname, NULL, initdir, NULL, curbuf);
9054#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009055 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009056#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009057 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009058}
9059
9060/*
9061 * "browsedir(title, initdir)" function
9062 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009064f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009065 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009066 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009067{
9068#ifdef FEAT_BROWSE
9069 char_u *title;
9070 char_u *initdir;
9071 char_u buf[NUMBUFLEN];
9072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009073 title = get_tv_string_chk(&argvars[0]);
9074 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009076 if (title == NULL || initdir == NULL)
9077 rettv->vval.v_string = NULL;
9078 else
9079 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009080 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085}
9086
Bram Moolenaar33570922005-01-25 22:26:29 +00009087static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009088
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089/*
9090 * Find a buffer by number or exact name.
9091 */
9092 static buf_T *
9093find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009094 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095{
9096 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009098 if (avar->v_type == VAR_NUMBER)
9099 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009100 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009102 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009103 if (buf == NULL)
9104 {
9105 /* No full path name match, try a match with a URL or a "nofile"
9106 * buffer, these don't use the full path. */
9107 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9108 if (buf->b_fname != NULL
9109 && (path_with_url(buf->b_fname)
9110#ifdef FEAT_QUICKFIX
9111 || bt_nofile(buf)
9112#endif
9113 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009114 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009115 break;
9116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 }
9118 return buf;
9119}
9120
9121/*
9122 * "bufexists(expr)" function
9123 */
9124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009126 typval_T *argvars;
9127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130}
9131
9132/*
9133 * "buflisted(expr)" function
9134 */
9135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 typval_T *argvars;
9138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140 buf_T *buf;
9141
9142 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144}
9145
9146/*
9147 * "bufloaded(expr)" function
9148 */
9149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009151 typval_T *argvars;
9152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153{
9154 buf_T *buf;
9155
9156 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009157 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158}
9159
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009160static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009161
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162/*
9163 * Get buffer by number or pattern.
9164 */
9165 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009166get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009168 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 int save_magic;
9172 char_u *save_cpo;
9173 buf_T *buf;
9174
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 if (tv->v_type == VAR_NUMBER)
9176 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009177 if (tv->v_type != VAR_STRING)
9178 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 if (name == NULL || *name == NUL)
9180 return curbuf;
9181 if (name[0] == '$' && name[1] == NUL)
9182 return lastbuf;
9183
9184 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9185 save_magic = p_magic;
9186 p_magic = TRUE;
9187 save_cpo = p_cpo;
9188 p_cpo = (char_u *)"";
9189
9190 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009191 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192
9193 p_magic = save_magic;
9194 p_cpo = save_cpo;
9195
9196 /* If not found, try expanding the name, like done for bufexists(). */
9197 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
9200 return buf;
9201}
9202
9203/*
9204 * "bufname(expr)" function
9205 */
9206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009207f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 typval_T *argvars;
9209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
9211 buf_T *buf;
9212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009215 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009218 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009220 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 --emsg_off;
9222}
9223
9224/*
9225 * "bufnr(expr)" function
9226 */
9227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009228f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009229 typval_T *argvars;
9230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231{
9232 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009233 int error = FALSE;
9234 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009236 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009238 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009239 --emsg_off;
9240
9241 /* If the buffer isn't found and the second argument is not zero create a
9242 * new buffer. */
9243 if (buf == NULL
9244 && argvars[1].v_type != VAR_UNKNOWN
9245 && get_tv_number_chk(&argvars[1], &error) != 0
9246 && !error
9247 && (name = get_tv_string_chk(&argvars[0])) != NULL
9248 && !error)
9249 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9250
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009252 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009254 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255}
9256
9257/*
9258 * "bufwinnr(nr)" function
9259 */
9260 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009261f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009262 typval_T *argvars;
9263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264{
9265#ifdef FEAT_WINDOWS
9266 win_T *wp;
9267 int winnr = 0;
9268#endif
9269 buf_T *buf;
9270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009273 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274#ifdef FEAT_WINDOWS
9275 for (wp = firstwin; wp; wp = wp->w_next)
9276 {
9277 ++winnr;
9278 if (wp->w_buffer == buf)
9279 break;
9280 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009281 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009283 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284#endif
9285 --emsg_off;
9286}
9287
9288/*
9289 * "byte2line(byte)" function
9290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009293 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009294 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295{
9296#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009297 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298#else
9299 long boff = 0;
9300
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306 (linenr_T)0, &boff);
9307#endif
9308}
9309
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009310 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009311byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009312 typval_T *argvars;
9313 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009314 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009315{
9316#ifdef FEAT_MBYTE
9317 char_u *t;
9318#endif
9319 char_u *str;
9320 long idx;
9321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009322 str = get_tv_string_chk(&argvars[0]);
9323 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009324 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009325 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009326 return;
9327
9328#ifdef FEAT_MBYTE
9329 t = str;
9330 for ( ; idx > 0; idx--)
9331 {
9332 if (*t == NUL) /* EOL reached */
9333 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009334 if (enc_utf8 && comp)
9335 t += utf_ptr2len(t);
9336 else
9337 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009338 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009339 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009340#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009341 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009342 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009343#endif
9344}
9345
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009346/*
9347 * "byteidx()" function
9348 */
9349 static void
9350f_byteidx(argvars, rettv)
9351 typval_T *argvars;
9352 typval_T *rettv;
9353{
9354 byteidx(argvars, rettv, FALSE);
9355}
9356
9357/*
9358 * "byteidxcomp()" function
9359 */
9360 static void
9361f_byteidxcomp(argvars, rettv)
9362 typval_T *argvars;
9363 typval_T *rettv;
9364{
9365 byteidx(argvars, rettv, TRUE);
9366}
9367
Bram Moolenaardb913952012-06-29 12:54:53 +02009368 int
9369func_call(name, args, selfdict, rettv)
9370 char_u *name;
9371 typval_T *args;
9372 dict_T *selfdict;
9373 typval_T *rettv;
9374{
9375 listitem_T *item;
9376 typval_T argv[MAX_FUNC_ARGS + 1];
9377 int argc = 0;
9378 int dummy;
9379 int r = 0;
9380
9381 for (item = args->vval.v_list->lv_first; item != NULL;
9382 item = item->li_next)
9383 {
9384 if (argc == MAX_FUNC_ARGS)
9385 {
9386 EMSG(_("E699: Too many arguments"));
9387 break;
9388 }
9389 /* Make a copy of each argument. This is needed to be able to set
9390 * v_lock to VAR_FIXED in the copy without changing the original list.
9391 */
9392 copy_tv(&item->li_tv, &argv[argc++]);
9393 }
9394
9395 if (item == NULL)
9396 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9397 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9398 &dummy, TRUE, selfdict);
9399
9400 /* Free the arguments. */
9401 while (argc > 0)
9402 clear_tv(&argv[--argc]);
9403
9404 return r;
9405}
9406
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009407/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009408 * "call(func, arglist)" function
9409 */
9410 static void
9411f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009412 typval_T *argvars;
9413 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009414{
9415 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009417
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009418 if (argvars[1].v_type != VAR_LIST)
9419 {
9420 EMSG(_(e_listreq));
9421 return;
9422 }
9423 if (argvars[1].vval.v_list == NULL)
9424 return;
9425
9426 if (argvars[0].v_type == VAR_FUNC)
9427 func = argvars[0].vval.v_string;
9428 else
9429 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009430 if (*func == NUL)
9431 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009432
Bram Moolenaare9a41262005-01-15 22:18:47 +00009433 if (argvars[2].v_type != VAR_UNKNOWN)
9434 {
9435 if (argvars[2].v_type != VAR_DICT)
9436 {
9437 EMSG(_(e_dictreq));
9438 return;
9439 }
9440 selfdict = argvars[2].vval.v_dict;
9441 }
9442
Bram Moolenaardb913952012-06-29 12:54:53 +02009443 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009444}
9445
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009446#ifdef FEAT_FLOAT
9447/*
9448 * "ceil({float})" function
9449 */
9450 static void
9451f_ceil(argvars, rettv)
9452 typval_T *argvars;
9453 typval_T *rettv;
9454{
9455 float_T f;
9456
9457 rettv->v_type = VAR_FLOAT;
9458 if (get_float_arg(argvars, &f) == OK)
9459 rettv->vval.v_float = ceil(f);
9460 else
9461 rettv->vval.v_float = 0.0;
9462}
9463#endif
9464
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009465/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009466 * "changenr()" function
9467 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009468 static void
9469f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009470 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009471 typval_T *rettv;
9472{
9473 rettv->vval.v_number = curbuf->b_u_seq_cur;
9474}
9475
9476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009477 * "char2nr(string)" function
9478 */
9479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009481 typval_T *argvars;
9482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
9484#ifdef FEAT_MBYTE
9485 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009486 {
9487 int utf8 = 0;
9488
9489 if (argvars[1].v_type != VAR_UNKNOWN)
9490 utf8 = get_tv_number_chk(&argvars[1], NULL);
9491
9492 if (utf8)
9493 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9494 else
9495 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 else
9498#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009499 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500}
9501
9502/*
9503 * "cindent(lnum)" function
9504 */
9505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510#ifdef FEAT_CINDENT
9511 pos_T pos;
9512 linenr_T lnum;
9513
9514 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9517 {
9518 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009519 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520 curwin->w_cursor = pos;
9521 }
9522 else
9523#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009524 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525}
9526
9527/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009528 * "clearmatches()" function
9529 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009530 static void
9531f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009532 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009533 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009534{
9535#ifdef FEAT_SEARCH_EXTRA
9536 clear_matches(curwin);
9537#endif
9538}
9539
9540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 * "col(string)" function
9542 */
9543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009544f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009545 typval_T *argvars;
9546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547{
9548 colnr_T col = 0;
9549 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009550 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009552 fp = var2fpos(&argvars[0], FALSE, &fnum);
9553 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 {
9555 if (fp->col == MAXCOL)
9556 {
9557 /* '> can be MAXCOL, get the length of the line then */
9558 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009559 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 else
9561 col = MAXCOL;
9562 }
9563 else
9564 {
9565 col = fp->col + 1;
9566#ifdef FEAT_VIRTUALEDIT
9567 /* col(".") when the cursor is on the NUL at the end of the line
9568 * because of "coladd" can be seen as an extra column. */
9569 if (virtual_active() && fp == &curwin->w_cursor)
9570 {
9571 char_u *p = ml_get_cursor();
9572
9573 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9574 curwin->w_virtcol - curwin->w_cursor.coladd))
9575 {
9576# ifdef FEAT_MBYTE
9577 int l;
9578
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009579 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 col += l;
9581# else
9582 if (*p != NUL && p[1] == NUL)
9583 ++col;
9584# endif
9585 }
9586 }
9587#endif
9588 }
9589 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591}
9592
Bram Moolenaar572cb562005-08-05 21:35:02 +00009593#if defined(FEAT_INS_EXPAND)
9594/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009595 * "complete()" function
9596 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009597 static void
9598f_complete(argvars, rettv)
9599 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009600 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009601{
9602 int startcol;
9603
9604 if ((State & INSERT) == 0)
9605 {
9606 EMSG(_("E785: complete() can only be used in Insert mode"));
9607 return;
9608 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009609
9610 /* Check for undo allowed here, because if something was already inserted
9611 * the line was already saved for undo and this check isn't done. */
9612 if (!undo_allowed())
9613 return;
9614
Bram Moolenaarade00832006-03-10 21:46:58 +00009615 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9616 {
9617 EMSG(_(e_invarg));
9618 return;
9619 }
9620
9621 startcol = get_tv_number_chk(&argvars[0], NULL);
9622 if (startcol <= 0)
9623 return;
9624
9625 set_completion(startcol - 1, argvars[1].vval.v_list);
9626}
9627
9628/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009629 * "complete_add()" function
9630 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009631 static void
9632f_complete_add(argvars, rettv)
9633 typval_T *argvars;
9634 typval_T *rettv;
9635{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009636 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009637}
9638
9639/*
9640 * "complete_check()" function
9641 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009642 static void
9643f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009644 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009645 typval_T *rettv;
9646{
9647 int saved = RedrawingDisabled;
9648
9649 RedrawingDisabled = 0;
9650 ins_compl_check_keys(0);
9651 rettv->vval.v_number = compl_interrupted;
9652 RedrawingDisabled = saved;
9653}
9654#endif
9655
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656/*
9657 * "confirm(message, buttons[, default [, type]])" function
9658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009661 typval_T *argvars UNUSED;
9662 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
9664#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9665 char_u *message;
9666 char_u *buttons = NULL;
9667 char_u buf[NUMBUFLEN];
9668 char_u buf2[NUMBUFLEN];
9669 int def = 1;
9670 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009671 char_u *typestr;
9672 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009674 message = get_tv_string_chk(&argvars[0]);
9675 if (message == NULL)
9676 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009677 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009679 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9680 if (buttons == NULL)
9681 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009682 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009684 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009685 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009687 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9688 if (typestr == NULL)
9689 error = TRUE;
9690 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009692 switch (TOUPPER_ASC(*typestr))
9693 {
9694 case 'E': type = VIM_ERROR; break;
9695 case 'Q': type = VIM_QUESTION; break;
9696 case 'I': type = VIM_INFO; break;
9697 case 'W': type = VIM_WARNING; break;
9698 case 'G': type = VIM_GENERIC; break;
9699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 }
9701 }
9702 }
9703 }
9704
9705 if (buttons == NULL || *buttons == NUL)
9706 buttons = (char_u *)_("&Ok");
9707
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009708 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009710 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#endif
9712}
9713
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009714/*
9715 * "copy()" function
9716 */
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009721{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009722 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009725#ifdef FEAT_FLOAT
9726/*
9727 * "cos()" function
9728 */
9729 static void
9730f_cos(argvars, rettv)
9731 typval_T *argvars;
9732 typval_T *rettv;
9733{
9734 float_T f;
9735
9736 rettv->v_type = VAR_FLOAT;
9737 if (get_float_arg(argvars, &f) == OK)
9738 rettv->vval.v_float = cos(f);
9739 else
9740 rettv->vval.v_float = 0.0;
9741}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009742
9743/*
9744 * "cosh()" function
9745 */
9746 static void
9747f_cosh(argvars, rettv)
9748 typval_T *argvars;
9749 typval_T *rettv;
9750{
9751 float_T f;
9752
9753 rettv->v_type = VAR_FLOAT;
9754 if (get_float_arg(argvars, &f) == OK)
9755 rettv->vval.v_float = cosh(f);
9756 else
9757 rettv->vval.v_float = 0.0;
9758}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009759#endif
9760
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009762 * "count()" function
9763 */
9764 static void
9765f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009766 typval_T *argvars;
9767 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009768{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009769 long n = 0;
9770 int ic = FALSE;
9771
Bram Moolenaare9a41262005-01-15 22:18:47 +00009772 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009773 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009774 listitem_T *li;
9775 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009776 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009777
Bram Moolenaare9a41262005-01-15 22:18:47 +00009778 if ((l = argvars[0].vval.v_list) != NULL)
9779 {
9780 li = l->lv_first;
9781 if (argvars[2].v_type != VAR_UNKNOWN)
9782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009783 int error = FALSE;
9784
9785 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009786 if (argvars[3].v_type != VAR_UNKNOWN)
9787 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009788 idx = get_tv_number_chk(&argvars[3], &error);
9789 if (!error)
9790 {
9791 li = list_find(l, idx);
9792 if (li == NULL)
9793 EMSGN(_(e_listidx), idx);
9794 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009795 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009796 if (error)
9797 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009798 }
9799
9800 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009801 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009802 ++n;
9803 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009804 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009805 else if (argvars[0].v_type == VAR_DICT)
9806 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009807 int todo;
9808 dict_T *d;
9809 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009810
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009811 if ((d = argvars[0].vval.v_dict) != NULL)
9812 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009813 int error = FALSE;
9814
Bram Moolenaare9a41262005-01-15 22:18:47 +00009815 if (argvars[2].v_type != VAR_UNKNOWN)
9816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009817 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009818 if (argvars[3].v_type != VAR_UNKNOWN)
9819 EMSG(_(e_invarg));
9820 }
9821
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009822 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009823 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009824 {
9825 if (!HASHITEM_EMPTY(hi))
9826 {
9827 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009828 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009829 ++n;
9830 }
9831 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009832 }
9833 }
9834 else
9835 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009836 rettv->vval.v_number = n;
9837}
9838
9839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9841 *
9842 * Checks the existence of a cscope connection.
9843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009845f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009846 typval_T *argvars UNUSED;
9847 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009848{
9849#ifdef FEAT_CSCOPE
9850 int num = 0;
9851 char_u *dbpath = NULL;
9852 char_u *prepend = NULL;
9853 char_u buf[NUMBUFLEN];
9854
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009855 if (argvars[0].v_type != VAR_UNKNOWN
9856 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009858 num = (int)get_tv_number(&argvars[0]);
9859 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009860 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 }
9863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865#endif
9866}
9867
9868/*
9869 * "cursor(lnum, col)" function
9870 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009871 * Moves the cursor to the specified line and column.
9872 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009876 typval_T *argvars;
9877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878{
9879 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009880#ifdef FEAT_VIRTUALEDIT
9881 long coladd = 0;
9882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009884 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009885 if (argvars[1].v_type == VAR_UNKNOWN)
9886 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009887 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009888 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009889
Bram Moolenaar493c1782014-05-28 14:34:46 +02009890 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009891 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009892 line = pos.lnum;
9893 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009894#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009895 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009896#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +02009897 if (curswant >= 0)
9898 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009899 }
9900 else
9901 {
9902 line = get_tv_lnum(argvars);
9903 col = get_tv_number_chk(&argvars[1], NULL);
9904#ifdef FEAT_VIRTUALEDIT
9905 if (argvars[2].v_type != VAR_UNKNOWN)
9906 coladd = get_tv_number_chk(&argvars[2], NULL);
9907#endif
9908 }
9909 if (line < 0 || col < 0
9910#ifdef FEAT_VIRTUALEDIT
9911 || coladd < 0
9912#endif
9913 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009914 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915 if (line > 0)
9916 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009917 if (col > 0)
9918 curwin->w_cursor.col = col - 1;
9919#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009920 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921#endif
9922
9923 /* Make sure the cursor is in a valid position. */
9924 check_cursor();
9925#ifdef FEAT_MBYTE
9926 /* Correct cursor for multi-byte character. */
9927 if (has_mbyte)
9928 mb_adjust_cursor();
9929#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009930
9931 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009932 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933}
9934
9935/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009936 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 */
9938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009940 typval_T *argvars;
9941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009943 int noref = 0;
9944
9945 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009946 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009947 if (noref < 0 || noref > 1)
9948 EMSG(_(e_invarg));
9949 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009950 {
9951 current_copyID += COPYID_INC;
9952 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954}
9955
9956/*
9957 * "delete()" function
9958 */
9959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009960f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009961 typval_T *argvars;
9962 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963{
9964 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009967 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968}
9969
9970/*
9971 * "did_filetype()" function
9972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009975 typval_T *argvars UNUSED;
9976 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
9978#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980#endif
9981}
9982
9983/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009984 * "diff_filler()" function
9985 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009988 typval_T *argvars UNUSED;
9989 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009990{
9991#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009992 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009993#endif
9994}
9995
9996/*
9997 * "diff_hlID()" function
9998 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010001 typval_T *argvars UNUSED;
10002 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010003{
10004#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010005 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010006 static linenr_T prev_lnum = 0;
10007 static int changedtick = 0;
10008 static int fnum = 0;
10009 static int change_start = 0;
10010 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010011 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010012 int filler_lines;
10013 int col;
10014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010015 if (lnum < 0) /* ignore type error in {lnum} arg */
10016 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010017 if (lnum != prev_lnum
10018 || changedtick != curbuf->b_changedtick
10019 || fnum != curbuf->b_fnum)
10020 {
10021 /* New line, buffer, change: need to get the values. */
10022 filler_lines = diff_check(curwin, lnum);
10023 if (filler_lines < 0)
10024 {
10025 if (filler_lines == -1)
10026 {
10027 change_start = MAXCOL;
10028 change_end = -1;
10029 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10030 hlID = HLF_ADD; /* added line */
10031 else
10032 hlID = HLF_CHD; /* changed line */
10033 }
10034 else
10035 hlID = HLF_ADD; /* added line */
10036 }
10037 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010038 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010039 prev_lnum = lnum;
10040 changedtick = curbuf->b_changedtick;
10041 fnum = curbuf->b_fnum;
10042 }
10043
10044 if (hlID == HLF_CHD || hlID == HLF_TXD)
10045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010046 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010047 if (col >= change_start && col <= change_end)
10048 hlID = HLF_TXD; /* changed text */
10049 else
10050 hlID = HLF_CHD; /* changed line */
10051 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010052 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010053#endif
10054}
10055
10056/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010057 * "empty({expr})" function
10058 */
10059 static void
10060f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010061 typval_T *argvars;
10062 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010063{
10064 int n;
10065
10066 switch (argvars[0].v_type)
10067 {
10068 case VAR_STRING:
10069 case VAR_FUNC:
10070 n = argvars[0].vval.v_string == NULL
10071 || *argvars[0].vval.v_string == NUL;
10072 break;
10073 case VAR_NUMBER:
10074 n = argvars[0].vval.v_number == 0;
10075 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010076#ifdef FEAT_FLOAT
10077 case VAR_FLOAT:
10078 n = argvars[0].vval.v_float == 0.0;
10079 break;
10080#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010081 case VAR_LIST:
10082 n = argvars[0].vval.v_list == NULL
10083 || argvars[0].vval.v_list->lv_first == NULL;
10084 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010085 case VAR_DICT:
10086 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010087 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010088 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010089 default:
10090 EMSG2(_(e_intern2), "f_empty()");
10091 n = 0;
10092 }
10093
10094 rettv->vval.v_number = n;
10095}
10096
10097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 * "escape({string}, {chars})" function
10099 */
10100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010101f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010102 typval_T *argvars;
10103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104{
10105 char_u buf[NUMBUFLEN];
10106
Bram Moolenaar758711c2005-02-02 23:11:38 +000010107 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10108 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110}
10111
10112/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010113 * "eval()" function
10114 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010115 static void
10116f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010117 typval_T *argvars;
10118 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010119{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010120 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010122 s = get_tv_string_chk(&argvars[0]);
10123 if (s != NULL)
10124 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010125
Bram Moolenaar615b9972015-01-14 17:15:05 +010010126 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10128 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010129 if (p != NULL && !aborting())
10130 EMSG2(_(e_invexpr2), p);
10131 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010133 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010135 else if (*s != NUL)
10136 EMSG(_(e_trailing));
10137}
10138
10139/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 * "eventhandler()" function
10141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148}
10149
10150/*
10151 * "executable()" function
10152 */
10153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010154f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010155 typval_T *argvars;
10156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010158 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10159}
10160
10161/*
10162 * "exepath()" function
10163 */
10164 static void
10165f_exepath(argvars, rettv)
10166 typval_T *argvars;
10167 typval_T *rettv;
10168{
10169 char_u *p = NULL;
10170
10171 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10172 rettv->v_type = VAR_STRING;
10173 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174}
10175
10176/*
10177 * "exists()" function
10178 */
10179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010181 typval_T *argvars;
10182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183{
10184 char_u *p;
10185 char_u *name;
10186 int n = FALSE;
10187 int len = 0;
10188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 if (*p == '$') /* environment variable */
10191 {
10192 /* first try "normal" environment variables (fast) */
10193 if (mch_getenv(p + 1) != NULL)
10194 n = TRUE;
10195 else
10196 {
10197 /* try expanding things like $VIM and ${HOME} */
10198 p = expand_env_save(p);
10199 if (p != NULL && *p != '$')
10200 n = TRUE;
10201 vim_free(p);
10202 }
10203 }
10204 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010205 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010206 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010207 if (*skipwhite(p) != NUL)
10208 n = FALSE; /* trailing garbage */
10209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 else if (*p == '*') /* internal or user defined function */
10211 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010212 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 }
10214 else if (*p == ':')
10215 {
10216 n = cmd_exists(p + 1);
10217 }
10218 else if (*p == '#')
10219 {
10220#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010221 if (p[1] == '#')
10222 n = autocmd_supported(p + 2);
10223 else
10224 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010225#endif
10226 }
10227 else /* internal variable */
10228 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010229 char_u *tofree;
10230 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010232 /* get_name_len() takes care of expanding curly braces */
10233 name = p;
10234 len = get_name_len(&p, &tofree, TRUE, FALSE);
10235 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010236 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010237 if (tofree != NULL)
10238 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010239 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010240 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010242 /* handle d.key, l[idx], f(expr) */
10243 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10244 if (n)
10245 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 }
10247 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010248 if (*p != NUL)
10249 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010250
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010251 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 }
10253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010254 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255}
10256
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010257#ifdef FEAT_FLOAT
10258/*
10259 * "exp()" function
10260 */
10261 static void
10262f_exp(argvars, rettv)
10263 typval_T *argvars;
10264 typval_T *rettv;
10265{
10266 float_T f;
10267
10268 rettv->v_type = VAR_FLOAT;
10269 if (get_float_arg(argvars, &f) == OK)
10270 rettv->vval.v_float = exp(f);
10271 else
10272 rettv->vval.v_float = 0.0;
10273}
10274#endif
10275
Bram Moolenaar071d4272004-06-13 20:20:40 +000010276/*
10277 * "expand()" function
10278 */
10279 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010281 typval_T *argvars;
10282 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283{
10284 char_u *s;
10285 int len;
10286 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010287 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010290 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010293 if (argvars[1].v_type != VAR_UNKNOWN
10294 && argvars[2].v_type != VAR_UNKNOWN
10295 && get_tv_number_chk(&argvars[2], &error)
10296 && !error)
10297 {
10298 rettv->v_type = VAR_LIST;
10299 rettv->vval.v_list = NULL;
10300 }
10301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010302 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 if (*s == '%' || *s == '#' || *s == '<')
10304 {
10305 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010306 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010308 if (rettv->v_type == VAR_LIST)
10309 {
10310 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10311 list_append_string(rettv->vval.v_list, result, -1);
10312 else
10313 vim_free(result);
10314 }
10315 else
10316 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 }
10318 else
10319 {
10320 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010321 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 if (argvars[1].v_type != VAR_UNKNOWN
10323 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010324 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010325 if (!error)
10326 {
10327 ExpandInit(&xpc);
10328 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010329 if (p_wic)
10330 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010331 if (rettv->v_type == VAR_STRING)
10332 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10333 options, WILD_ALL);
10334 else if (rettv_list_alloc(rettv) != FAIL)
10335 {
10336 int i;
10337
10338 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10339 for (i = 0; i < xpc.xp_numfiles; i++)
10340 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10341 ExpandCleanup(&xpc);
10342 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010343 }
10344 else
10345 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 }
10347}
10348
10349/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010350 * Go over all entries in "d2" and add them to "d1".
10351 * When "action" is "error" then a duplicate key is an error.
10352 * When "action" is "force" then a duplicate key is overwritten.
10353 * Otherwise duplicate keys are ignored ("action" is "keep").
10354 */
10355 void
10356dict_extend(d1, d2, action)
10357 dict_T *d1;
10358 dict_T *d2;
10359 char_u *action;
10360{
10361 dictitem_T *di1;
10362 hashitem_T *hi2;
10363 int todo;
10364
10365 todo = (int)d2->dv_hashtab.ht_used;
10366 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10367 {
10368 if (!HASHITEM_EMPTY(hi2))
10369 {
10370 --todo;
10371 di1 = dict_find(d1, hi2->hi_key, -1);
10372 if (d1->dv_scope != 0)
10373 {
10374 /* Disallow replacing a builtin function in l: and g:.
10375 * Check the key to be valid when adding to any
10376 * scope. */
10377 if (d1->dv_scope == VAR_DEF_SCOPE
10378 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10379 && var_check_func_name(hi2->hi_key,
10380 di1 == NULL))
10381 break;
10382 if (!valid_varname(hi2->hi_key))
10383 break;
10384 }
10385 if (di1 == NULL)
10386 {
10387 di1 = dictitem_copy(HI2DI(hi2));
10388 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10389 dictitem_free(di1);
10390 }
10391 else if (*action == 'e')
10392 {
10393 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10394 break;
10395 }
10396 else if (*action == 'f' && HI2DI(hi2) != di1)
10397 {
10398 clear_tv(&di1->di_tv);
10399 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10400 }
10401 }
10402 }
10403}
10404
10405/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010406 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010407 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010408 */
10409 static void
10410f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010411 typval_T *argvars;
10412 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010413{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010414 char *arg_errmsg = N_("extend() argument");
10415
Bram Moolenaare9a41262005-01-15 22:18:47 +000010416 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010417 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 list_T *l1, *l2;
10419 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010420 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010422
Bram Moolenaare9a41262005-01-15 22:18:47 +000010423 l1 = argvars[0].vval.v_list;
10424 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010425 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010426 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010427 {
10428 if (argvars[2].v_type != VAR_UNKNOWN)
10429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010430 before = get_tv_number_chk(&argvars[2], &error);
10431 if (error)
10432 return; /* type error; errmsg already given */
10433
Bram Moolenaar758711c2005-02-02 23:11:38 +000010434 if (before == l1->lv_len)
10435 item = NULL;
10436 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010437 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010438 item = list_find(l1, before);
10439 if (item == NULL)
10440 {
10441 EMSGN(_(e_listidx), before);
10442 return;
10443 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010444 }
10445 }
10446 else
10447 item = NULL;
10448 list_extend(l1, l2, item);
10449
Bram Moolenaare9a41262005-01-15 22:18:47 +000010450 copy_tv(&argvars[0], rettv);
10451 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010452 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010453 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10454 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010455 dict_T *d1, *d2;
10456 char_u *action;
10457 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010458
10459 d1 = argvars[0].vval.v_dict;
10460 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010461 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010462 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010463 {
10464 /* Check the third argument. */
10465 if (argvars[2].v_type != VAR_UNKNOWN)
10466 {
10467 static char *(av[]) = {"keep", "force", "error"};
10468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 action = get_tv_string_chk(&argvars[2]);
10470 if (action == NULL)
10471 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010472 for (i = 0; i < 3; ++i)
10473 if (STRCMP(action, av[i]) == 0)
10474 break;
10475 if (i == 3)
10476 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010477 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010478 return;
10479 }
10480 }
10481 else
10482 action = (char_u *)"force";
10483
Bram Moolenaara9922d62013-05-30 13:01:18 +020010484 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010485
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486 copy_tv(&argvars[0], rettv);
10487 }
10488 }
10489 else
10490 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010491}
10492
10493/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010494 * "feedkeys()" function
10495 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010496 static void
10497f_feedkeys(argvars, rettv)
10498 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010499 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010500{
10501 int remap = TRUE;
10502 char_u *keys, *flags;
10503 char_u nbuf[NUMBUFLEN];
10504 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010505 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010506
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010507 /* This is not allowed in the sandbox. If the commands would still be
10508 * executed in the sandbox it would be OK, but it probably happens later,
10509 * when "sandbox" is no longer set. */
10510 if (check_secure())
10511 return;
10512
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010513 keys = get_tv_string(&argvars[0]);
10514 if (*keys != NUL)
10515 {
10516 if (argvars[1].v_type != VAR_UNKNOWN)
10517 {
10518 flags = get_tv_string_buf(&argvars[1], nbuf);
10519 for ( ; *flags != NUL; ++flags)
10520 {
10521 switch (*flags)
10522 {
10523 case 'n': remap = FALSE; break;
10524 case 'm': remap = TRUE; break;
10525 case 't': typed = TRUE; break;
10526 }
10527 }
10528 }
10529
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010530 /* Need to escape K_SPECIAL and CSI before putting the string in the
10531 * typeahead buffer. */
10532 keys_esc = vim_strsave_escape_csi(keys);
10533 if (keys_esc != NULL)
10534 {
10535 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010536 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010537 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010538 if (vgetc_busy)
10539 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010540 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010541 }
10542}
10543
10544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 * "filereadable()" function
10546 */
10547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010549 typval_T *argvars;
10550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010552 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 char_u *p;
10554 int n;
10555
Bram Moolenaarc236c162008-07-13 17:41:49 +000010556#ifndef O_NONBLOCK
10557# define O_NONBLOCK 0
10558#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010560 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10561 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562 {
10563 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010564 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 }
10566 else
10567 n = FALSE;
10568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010569 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570}
10571
10572/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010573 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 * rights to write into.
10575 */
10576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010577f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010578 typval_T *argvars;
10579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010581 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582}
10583
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010584static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010585
10586 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010587findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010588 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010589 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010590 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010591{
10592#ifdef FEAT_SEARCHPATH
10593 char_u *fname;
10594 char_u *fresult = NULL;
10595 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10596 char_u *p;
10597 char_u pathbuf[NUMBUFLEN];
10598 int count = 1;
10599 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010600 int error = FALSE;
10601#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010602
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010603 rettv->vval.v_string = NULL;
10604 rettv->v_type = VAR_STRING;
10605
10606#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010607 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010608
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010609 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10612 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010613 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 else
10615 {
10616 if (*p != NUL)
10617 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010620 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010622 }
10623
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010624 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10625 error = TRUE;
10626
10627 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 do
10630 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010631 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010632 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 fresult = find_file_in_path_option(first ? fname : NULL,
10634 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010635 0, first, path,
10636 find_what,
10637 curbuf->b_ffname,
10638 find_what == FINDFILE_DIR
10639 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010640 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010641
10642 if (fresult != NULL && rettv->v_type == VAR_LIST)
10643 list_append_string(rettv->vval.v_list, fresult, -1);
10644
10645 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010647
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010648 if (rettv->v_type == VAR_STRING)
10649 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010650#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010651}
10652
Bram Moolenaar33570922005-01-25 22:26:29 +000010653static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10654static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010655
10656/*
10657 * Implementation of map() and filter().
10658 */
10659 static void
10660filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010661 typval_T *argvars;
10662 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010663 int map;
10664{
10665 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010666 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010667 listitem_T *li, *nli;
10668 list_T *l = NULL;
10669 dictitem_T *di;
10670 hashtab_T *ht;
10671 hashitem_T *hi;
10672 dict_T *d = NULL;
10673 typval_T save_val;
10674 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010675 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010676 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010677 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10678 char *arg_errmsg = (map ? N_("map() argument")
10679 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010680 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010681 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010682
Bram Moolenaare9a41262005-01-15 22:18:47 +000010683 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010684 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010685 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010686 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010687 return;
10688 }
10689 else if (argvars[0].v_type == VAR_DICT)
10690 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010691 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010692 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010693 return;
10694 }
10695 else
10696 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010697 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010698 return;
10699 }
10700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010701 expr = get_tv_string_buf_chk(&argvars[1], buf);
10702 /* On type errors, the preceding call has already displayed an error
10703 * message. Avoid a misleading error message for an empty string that
10704 * was not passed as argument. */
10705 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010707 prepare_vimvar(VV_VAL, &save_val);
10708 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010709
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010710 /* We reset "did_emsg" to be able to detect whether an error
10711 * occurred during evaluation of the expression. */
10712 save_did_emsg = did_emsg;
10713 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010714
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010715 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010716 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010717 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010718 vimvars[VV_KEY].vv_type = VAR_STRING;
10719
10720 ht = &d->dv_hashtab;
10721 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010722 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010723 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010725 if (!HASHITEM_EMPTY(hi))
10726 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010727 int r;
10728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010729 --todo;
10730 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010731 if (tv_check_lock(di->di_tv.v_lock,
10732 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010733 break;
10734 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010735 r = filter_map_one(&di->di_tv, expr, map, &rem);
10736 clear_tv(&vimvars[VV_KEY].vv_tv);
10737 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010738 break;
10739 if (!map && rem)
10740 dictitem_remove(d, di);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 }
10742 }
10743 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 }
10745 else
10746 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010747 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010749 for (li = l->lv_first; li != NULL; li = nli)
10750 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010751 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010752 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010753 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010754 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010755 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010756 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010757 break;
10758 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010760 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010761 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010762 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010763
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010764 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010765 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010766
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010767 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010768 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769
10770 copy_tv(&argvars[0], rettv);
10771}
10772
10773 static int
10774filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010775 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010776 char_u *expr;
10777 int map;
10778 int *remp;
10779{
Bram Moolenaar33570922005-01-25 22:26:29 +000010780 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010781 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010782 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010783
Bram Moolenaar33570922005-01-25 22:26:29 +000010784 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010785 s = expr;
10786 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010787 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010788 if (*s != NUL) /* check for trailing chars after expr */
10789 {
10790 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010791 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010792 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010793 }
10794 if (map)
10795 {
10796 /* map(): replace the list item value */
10797 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010798 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010799 *tv = rettv;
10800 }
10801 else
10802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010803 int error = FALSE;
10804
Bram Moolenaare9a41262005-01-15 22:18:47 +000010805 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010807 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010808 /* On type error, nothing has been removed; return FAIL to stop the
10809 * loop. The error message was given by get_tv_number_chk(). */
10810 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010811 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010812 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010813 retval = OK;
10814theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010815 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010816 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010817}
10818
10819/*
10820 * "filter()" function
10821 */
10822 static void
10823f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010824 typval_T *argvars;
10825 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010826{
10827 filter_map(argvars, rettv, FALSE);
10828}
10829
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010830/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010831 * "finddir({fname}[, {path}[, {count}]])" function
10832 */
10833 static void
10834f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010835 typval_T *argvars;
10836 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010837{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010838 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010839}
10840
10841/*
10842 * "findfile({fname}[, {path}[, {count}]])" function
10843 */
10844 static void
10845f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010846 typval_T *argvars;
10847 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010848{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010849 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010850}
10851
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010852#ifdef FEAT_FLOAT
10853/*
10854 * "float2nr({float})" function
10855 */
10856 static void
10857f_float2nr(argvars, rettv)
10858 typval_T *argvars;
10859 typval_T *rettv;
10860{
10861 float_T f;
10862
10863 if (get_float_arg(argvars, &f) == OK)
10864 {
10865 if (f < -0x7fffffff)
10866 rettv->vval.v_number = -0x7fffffff;
10867 else if (f > 0x7fffffff)
10868 rettv->vval.v_number = 0x7fffffff;
10869 else
10870 rettv->vval.v_number = (varnumber_T)f;
10871 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010872}
10873
10874/*
10875 * "floor({float})" function
10876 */
10877 static void
10878f_floor(argvars, rettv)
10879 typval_T *argvars;
10880 typval_T *rettv;
10881{
10882 float_T f;
10883
10884 rettv->v_type = VAR_FLOAT;
10885 if (get_float_arg(argvars, &f) == OK)
10886 rettv->vval.v_float = floor(f);
10887 else
10888 rettv->vval.v_float = 0.0;
10889}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010890
10891/*
10892 * "fmod()" function
10893 */
10894 static void
10895f_fmod(argvars, rettv)
10896 typval_T *argvars;
10897 typval_T *rettv;
10898{
10899 float_T fx, fy;
10900
10901 rettv->v_type = VAR_FLOAT;
10902 if (get_float_arg(argvars, &fx) == OK
10903 && get_float_arg(&argvars[1], &fy) == OK)
10904 rettv->vval.v_float = fmod(fx, fy);
10905 else
10906 rettv->vval.v_float = 0.0;
10907}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010908#endif
10909
Bram Moolenaar0d660222005-01-07 21:51:51 +000010910/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010911 * "fnameescape({string})" function
10912 */
10913 static void
10914f_fnameescape(argvars, rettv)
10915 typval_T *argvars;
10916 typval_T *rettv;
10917{
10918 rettv->vval.v_string = vim_strsave_fnameescape(
10919 get_tv_string(&argvars[0]), FALSE);
10920 rettv->v_type = VAR_STRING;
10921}
10922
10923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 * "fnamemodify({fname}, {mods})" function
10925 */
10926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010927f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010928 typval_T *argvars;
10929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930{
10931 char_u *fname;
10932 char_u *mods;
10933 int usedlen = 0;
10934 int len;
10935 char_u *fbuf = NULL;
10936 char_u buf[NUMBUFLEN];
10937
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 fname = get_tv_string_chk(&argvars[0]);
10939 mods = get_tv_string_buf_chk(&argvars[1], buf);
10940 if (fname == NULL || mods == NULL)
10941 fname = NULL;
10942 else
10943 {
10944 len = (int)STRLEN(fname);
10945 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010950 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010952 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 vim_free(fbuf);
10954}
10955
Bram Moolenaar33570922005-01-25 22:26:29 +000010956static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957
10958/*
10959 * "foldclosed()" function
10960 */
10961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010962foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010963 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010964 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010965 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966{
10967#ifdef FEAT_FOLDING
10968 linenr_T lnum;
10969 linenr_T first, last;
10970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010971 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10973 {
10974 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10975 {
10976 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 return;
10981 }
10982 }
10983#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985}
10986
10987/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010988 * "foldclosed()" function
10989 */
10990 static void
10991f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010992 typval_T *argvars;
10993 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010994{
10995 foldclosed_both(argvars, rettv, FALSE);
10996}
10997
10998/*
10999 * "foldclosedend()" function
11000 */
11001 static void
11002f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 typval_T *argvars;
11004 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011005{
11006 foldclosed_both(argvars, rettv, TRUE);
11007}
11008
11009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 * "foldlevel()" function
11011 */
11012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011014 typval_T *argvars UNUSED;
11015 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016{
11017#ifdef FEAT_FOLDING
11018 linenr_T lnum;
11019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011020 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024}
11025
11026/*
11027 * "foldtext()" function
11028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011030f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011031 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033{
11034#ifdef FEAT_FOLDING
11035 linenr_T lnum;
11036 char_u *s;
11037 char_u *r;
11038 int len;
11039 char *txt;
11040#endif
11041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 rettv->v_type = VAR_STRING;
11043 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011045 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11046 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11047 <= curbuf->b_ml.ml_line_count
11048 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 {
11050 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011051 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11052 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 {
11054 if (!linewhite(lnum))
11055 break;
11056 ++lnum;
11057 }
11058
11059 /* Find interesting text in this line. */
11060 s = skipwhite(ml_get(lnum));
11061 /* skip C comment-start */
11062 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011065 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011066 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011067 {
11068 s = skipwhite(ml_get(lnum + 1));
11069 if (*s == '*')
11070 s = skipwhite(s + 1);
11071 }
11072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 txt = _("+-%s%3ld lines: ");
11074 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011075 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 + 20 /* for %3ld */
11077 + STRLEN(s))); /* concatenated */
11078 if (r != NULL)
11079 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011080 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11081 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11082 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 len = (int)STRLEN(r);
11084 STRCAT(r, s);
11085 /* remove 'foldmarker' and 'commentstring' */
11086 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011087 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 }
11089 }
11090#endif
11091}
11092
11093/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011094 * "foldtextresult(lnum)" function
11095 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011097f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011100{
11101#ifdef FEAT_FOLDING
11102 linenr_T lnum;
11103 char_u *text;
11104 char_u buf[51];
11105 foldinfo_T foldinfo;
11106 int fold_count;
11107#endif
11108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 rettv->v_type = VAR_STRING;
11110 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011111#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011113 /* treat illegal types and illegal string values for {lnum} the same */
11114 if (lnum < 0)
11115 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011116 fold_count = foldedCount(curwin, lnum, &foldinfo);
11117 if (fold_count > 0)
11118 {
11119 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11120 &foldinfo, buf);
11121 if (text == buf)
11122 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011124 }
11125#endif
11126}
11127
11128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 * "foreground()" function
11130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011133 typval_T *argvars UNUSED;
11134 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136#ifdef FEAT_GUI
11137 if (gui.in_use)
11138 gui_mch_set_foreground();
11139#else
11140# ifdef WIN32
11141 win32_set_foreground();
11142# endif
11143#endif
11144}
11145
11146/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011147 * "function()" function
11148 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011149 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011151 typval_T *argvars;
11152 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011153{
11154 char_u *s;
11155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011156 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011157 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011158 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011159 /* Don't check an autoload name for existence here. */
11160 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011161 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011162 else
11163 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011164 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011165 {
11166 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011167 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011168
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011169 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11170 * also be called from another script. Using trans_function_name()
11171 * would also work, but some plugins depend on the name being
11172 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011173 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011174 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011175 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011176 if (rettv->vval.v_string != NULL)
11177 {
11178 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011179 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011180 }
11181 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011182 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011183 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011185 }
11186}
11187
11188/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011189 * "garbagecollect()" function
11190 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011191 static void
11192f_garbagecollect(argvars, rettv)
11193 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011194 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011195{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011196 /* This is postponed until we are back at the toplevel, because we may be
11197 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11198 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011199
11200 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11201 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011202}
11203
11204/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011205 * "get()" function
11206 */
11207 static void
11208f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011209 typval_T *argvars;
11210 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211{
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 listitem_T *li;
11213 list_T *l;
11214 dictitem_T *di;
11215 dict_T *d;
11216 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011217
Bram Moolenaare9a41262005-01-15 22:18:47 +000011218 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011219 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011220 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011222 int error = FALSE;
11223
11224 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11225 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011226 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011227 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011228 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011229 else if (argvars[0].v_type == VAR_DICT)
11230 {
11231 if ((d = argvars[0].vval.v_dict) != NULL)
11232 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011233 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011234 if (di != NULL)
11235 tv = &di->di_tv;
11236 }
11237 }
11238 else
11239 EMSG2(_(e_listdictarg), "get()");
11240
11241 if (tv == NULL)
11242 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011243 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011244 copy_tv(&argvars[2], rettv);
11245 }
11246 else
11247 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011248}
11249
Bram Moolenaar342337a2005-07-21 21:11:17 +000011250static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011251
11252/*
11253 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011254 * Return a range (from start to end) of lines in rettv from the specified
11255 * buffer.
11256 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011257 */
11258 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011259get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011260 buf_T *buf;
11261 linenr_T start;
11262 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011263 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011264 typval_T *rettv;
11265{
11266 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011267
Bram Moolenaar959a1432013-12-14 12:17:38 +010011268 rettv->v_type = VAR_STRING;
11269 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011270 if (retlist && rettv_list_alloc(rettv) == FAIL)
11271 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011272
11273 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11274 return;
11275
11276 if (!retlist)
11277 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011278 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11279 p = ml_get_buf(buf, start, FALSE);
11280 else
11281 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011282 rettv->vval.v_string = vim_strsave(p);
11283 }
11284 else
11285 {
11286 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011287 return;
11288
11289 if (start < 1)
11290 start = 1;
11291 if (end > buf->b_ml.ml_line_count)
11292 end = buf->b_ml.ml_line_count;
11293 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011294 if (list_append_string(rettv->vval.v_list,
11295 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011296 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011297 }
11298}
11299
11300/*
11301 * "getbufline()" function
11302 */
11303 static void
11304f_getbufline(argvars, rettv)
11305 typval_T *argvars;
11306 typval_T *rettv;
11307{
11308 linenr_T lnum;
11309 linenr_T end;
11310 buf_T *buf;
11311
11312 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11313 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011314 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011315 --emsg_off;
11316
Bram Moolenaar661b1822005-07-28 22:36:45 +000011317 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011318 if (argvars[2].v_type == VAR_UNKNOWN)
11319 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011320 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011321 end = get_tv_lnum_buf(&argvars[2], buf);
11322
Bram Moolenaar342337a2005-07-21 21:11:17 +000011323 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011324}
11325
Bram Moolenaar0d660222005-01-07 21:51:51 +000011326/*
11327 * "getbufvar()" function
11328 */
11329 static void
11330f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011331 typval_T *argvars;
11332 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011333{
11334 buf_T *buf;
11335 buf_T *save_curbuf;
11336 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011337 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011338 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011339
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011340 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11341 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011342 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011343 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011344
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011345 rettv->v_type = VAR_STRING;
11346 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011347
11348 if (buf != NULL && varname != NULL)
11349 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011350 /* set curbuf to be our buf, temporarily */
11351 save_curbuf = curbuf;
11352 curbuf = buf;
11353
Bram Moolenaar0d660222005-01-07 21:51:51 +000011354 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011355 {
11356 if (get_option_tv(&varname, rettv, TRUE) == OK)
11357 done = TRUE;
11358 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011359 else if (STRCMP(varname, "changedtick") == 0)
11360 {
11361 rettv->v_type = VAR_NUMBER;
11362 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011363 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011364 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011365 else
11366 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011367 /* Look up the variable. */
11368 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11369 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11370 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011371 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011372 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011374 done = TRUE;
11375 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011376 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011377
11378 /* restore previous notion of curbuf */
11379 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011380 }
11381
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011382 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11383 /* use the default value */
11384 copy_tv(&argvars[2], rettv);
11385
Bram Moolenaar0d660222005-01-07 21:51:51 +000011386 --emsg_off;
11387}
11388
11389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 * "getchar()" function
11391 */
11392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011393f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011394 typval_T *argvars;
11395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396{
11397 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011400 /* Position the cursor. Needed after a message that ends in a space. */
11401 windgoto(msg_row, msg_col);
11402
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403 ++no_mapping;
11404 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011405 for (;;)
11406 {
11407 if (argvars[0].v_type == VAR_UNKNOWN)
11408 /* getchar(): blocking wait. */
11409 n = safe_vgetc();
11410 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11411 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011412 n = vpeekc_any();
11413 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011414 /* illegal argument or getchar(0) and no char avail: return zero */
11415 n = 0;
11416 else
11417 /* getchar(0) and char avail: return char */
11418 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011419
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011420 if (n == K_IGNORE)
11421 continue;
11422 break;
11423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 --no_mapping;
11425 --allow_keys;
11426
Bram Moolenaar219b8702006-11-01 14:32:36 +000011427 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11428 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11429 vimvars[VV_MOUSE_COL].vv_nr = 0;
11430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 if (IS_SPECIAL(n) || mod_mask != 0)
11433 {
11434 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11435 int i = 0;
11436
11437 /* Turn a special key into three bytes, plus modifier. */
11438 if (mod_mask != 0)
11439 {
11440 temp[i++] = K_SPECIAL;
11441 temp[i++] = KS_MODIFIER;
11442 temp[i++] = mod_mask;
11443 }
11444 if (IS_SPECIAL(n))
11445 {
11446 temp[i++] = K_SPECIAL;
11447 temp[i++] = K_SECOND(n);
11448 temp[i++] = K_THIRD(n);
11449 }
11450#ifdef FEAT_MBYTE
11451 else if (has_mbyte)
11452 i += (*mb_char2bytes)(n, temp + i);
11453#endif
11454 else
11455 temp[i++] = n;
11456 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011457 rettv->v_type = VAR_STRING;
11458 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011459
11460#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011461 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011462 {
11463 int row = mouse_row;
11464 int col = mouse_col;
11465 win_T *win;
11466 linenr_T lnum;
11467# ifdef FEAT_WINDOWS
11468 win_T *wp;
11469# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011470 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011471
11472 if (row >= 0 && col >= 0)
11473 {
11474 /* Find the window at the mouse coordinates and compute the
11475 * text position. */
11476 win = mouse_find_win(&row, &col);
11477 (void)mouse_comp_pos(win, &row, &col, &lnum);
11478# ifdef FEAT_WINDOWS
11479 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011480 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011481# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011482 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011483 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11484 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11485 }
11486 }
11487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011488 }
11489}
11490
11491/*
11492 * "getcharmod()" function
11493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011496 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500}
11501
11502/*
11503 * "getcmdline()" function
11504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011506f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510 rettv->v_type = VAR_STRING;
11511 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512}
11513
11514/*
11515 * "getcmdpos()" function
11516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011519 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523}
11524
11525/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011526 * "getcmdtype()" function
11527 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011528 static void
11529f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011530 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011531 typval_T *rettv;
11532{
11533 rettv->v_type = VAR_STRING;
11534 rettv->vval.v_string = alloc(2);
11535 if (rettv->vval.v_string != NULL)
11536 {
11537 rettv->vval.v_string[0] = get_cmdline_type();
11538 rettv->vval.v_string[1] = NUL;
11539 }
11540}
11541
11542/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011543 * "getcmdwintype()" function
11544 */
11545 static void
11546f_getcmdwintype(argvars, rettv)
11547 typval_T *argvars UNUSED;
11548 typval_T *rettv;
11549{
11550 rettv->v_type = VAR_STRING;
11551 rettv->vval.v_string = NULL;
11552#ifdef FEAT_CMDWIN
11553 rettv->vval.v_string = alloc(2);
11554 if (rettv->vval.v_string != NULL)
11555 {
11556 rettv->vval.v_string[0] = cmdwin_type;
11557 rettv->vval.v_string[1] = NUL;
11558 }
11559#endif
11560}
11561
11562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563 * "getcwd()" function
11564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011566f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011567 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011569{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011570 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011573 rettv->vval.v_string = NULL;
11574 cwd = alloc(MAXPATHL);
11575 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011577 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11578 {
11579 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011581 if (rettv->vval.v_string != NULL)
11582 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011584 }
11585 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 }
11587}
11588
11589/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011590 * "getfontname()" function
11591 */
11592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011594 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011595 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011597 rettv->v_type = VAR_STRING;
11598 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011599#ifdef FEAT_GUI
11600 if (gui.in_use)
11601 {
11602 GuiFont font;
11603 char_u *name = NULL;
11604
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011605 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011606 {
11607 /* Get the "Normal" font. Either the name saved by
11608 * hl_set_font_name() or from the font ID. */
11609 font = gui.norm_font;
11610 name = hl_get_font_name();
11611 }
11612 else
11613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011615 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11616 return;
11617 font = gui_mch_get_font(name, FALSE);
11618 if (font == NOFONT)
11619 return; /* Invalid font name, return empty string. */
11620 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011622 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011623 gui_mch_free_font(font);
11624 }
11625#endif
11626}
11627
11628/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011629 * "getfperm({fname})" function
11630 */
11631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011632f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011633 typval_T *argvars;
11634 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011635{
11636 char_u *fname;
11637 struct stat st;
11638 char_u *perm = NULL;
11639 char_u flags[] = "rwx";
11640 int i;
11641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011642 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011644 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011645 if (mch_stat((char *)fname, &st) >= 0)
11646 {
11647 perm = vim_strsave((char_u *)"---------");
11648 if (perm != NULL)
11649 {
11650 for (i = 0; i < 9; i++)
11651 {
11652 if (st.st_mode & (1 << (8 - i)))
11653 perm[i] = flags[i % 3];
11654 }
11655 }
11656 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011657 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011658}
11659
11660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661 * "getfsize({fname})" function
11662 */
11663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011664f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011665 typval_T *argvars;
11666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667{
11668 char_u *fname;
11669 struct stat st;
11670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674
11675 if (mch_stat((char *)fname, &st) >= 0)
11676 {
11677 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011678 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011682
11683 /* non-perfect check for overflow */
11684 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11685 rettv->vval.v_number = -2;
11686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 }
11688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690}
11691
11692/*
11693 * "getftime({fname})" function
11694 */
11695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011697 typval_T *argvars;
11698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699{
11700 char_u *fname;
11701 struct stat st;
11702
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011703 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704
11705 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709}
11710
11711/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011712 * "getftype({fname})" function
11713 */
11714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011715f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011716 typval_T *argvars;
11717 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011718{
11719 char_u *fname;
11720 struct stat st;
11721 char_u *type = NULL;
11722 char *t;
11723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011724 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011725
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011727 if (mch_lstat((char *)fname, &st) >= 0)
11728 {
11729#ifdef S_ISREG
11730 if (S_ISREG(st.st_mode))
11731 t = "file";
11732 else if (S_ISDIR(st.st_mode))
11733 t = "dir";
11734# ifdef S_ISLNK
11735 else if (S_ISLNK(st.st_mode))
11736 t = "link";
11737# endif
11738# ifdef S_ISBLK
11739 else if (S_ISBLK(st.st_mode))
11740 t = "bdev";
11741# endif
11742# ifdef S_ISCHR
11743 else if (S_ISCHR(st.st_mode))
11744 t = "cdev";
11745# endif
11746# ifdef S_ISFIFO
11747 else if (S_ISFIFO(st.st_mode))
11748 t = "fifo";
11749# endif
11750# ifdef S_ISSOCK
11751 else if (S_ISSOCK(st.st_mode))
11752 t = "fifo";
11753# endif
11754 else
11755 t = "other";
11756#else
11757# ifdef S_IFMT
11758 switch (st.st_mode & S_IFMT)
11759 {
11760 case S_IFREG: t = "file"; break;
11761 case S_IFDIR: t = "dir"; break;
11762# ifdef S_IFLNK
11763 case S_IFLNK: t = "link"; break;
11764# endif
11765# ifdef S_IFBLK
11766 case S_IFBLK: t = "bdev"; break;
11767# endif
11768# ifdef S_IFCHR
11769 case S_IFCHR: t = "cdev"; break;
11770# endif
11771# ifdef S_IFIFO
11772 case S_IFIFO: t = "fifo"; break;
11773# endif
11774# ifdef S_IFSOCK
11775 case S_IFSOCK: t = "socket"; break;
11776# endif
11777 default: t = "other";
11778 }
11779# else
11780 if (mch_isdir(fname))
11781 t = "dir";
11782 else
11783 t = "file";
11784# endif
11785#endif
11786 type = vim_strsave((char_u *)t);
11787 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011789}
11790
11791/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011792 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011793 */
11794 static void
11795f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011796 typval_T *argvars;
11797 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011798{
11799 linenr_T lnum;
11800 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011801 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011802
11803 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011804 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011805 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011806 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011807 retlist = FALSE;
11808 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011809 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011810 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011811 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011812 retlist = TRUE;
11813 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011814
Bram Moolenaar342337a2005-07-21 21:11:17 +000011815 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011816}
11817
11818/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011819 * "getmatches()" function
11820 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011821 static void
11822f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011823 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011824 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011825{
11826#ifdef FEAT_SEARCH_EXTRA
11827 dict_T *dict;
11828 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011829 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011830
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011831 if (rettv_list_alloc(rettv) == OK)
11832 {
11833 while (cur != NULL)
11834 {
11835 dict = dict_alloc();
11836 if (dict == NULL)
11837 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011838 if (cur->match.regprog == NULL)
11839 {
11840 /* match added with matchaddpos() */
11841 for (i = 0; i < MAXPOSMATCH; ++i)
11842 {
11843 llpos_T *llpos;
11844 char buf[6];
11845 list_T *l;
11846
11847 llpos = &cur->pos.pos[i];
11848 if (llpos->lnum == 0)
11849 break;
11850 l = list_alloc();
11851 if (l == NULL)
11852 break;
11853 list_append_number(l, (varnumber_T)llpos->lnum);
11854 if (llpos->col > 0)
11855 {
11856 list_append_number(l, (varnumber_T)llpos->col);
11857 list_append_number(l, (varnumber_T)llpos->len);
11858 }
11859 sprintf(buf, "pos%d", i + 1);
11860 dict_add_list(dict, buf, l);
11861 }
11862 }
11863 else
11864 {
11865 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11866 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011867 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011868 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11869 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11870 list_append_dict(rettv->vval.v_list, dict);
11871 cur = cur->next;
11872 }
11873 }
11874#endif
11875}
11876
11877/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011878 * "getpid()" function
11879 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011880 static void
11881f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011882 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011883 typval_T *rettv;
11884{
11885 rettv->vval.v_number = mch_get_pid();
11886}
11887
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011888static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
11889
11890/*
11891 * "getcurpos()" function
11892 */
11893 static void
11894f_getcurpos(argvars, rettv)
11895 typval_T *argvars;
11896 typval_T *rettv;
11897{
11898 getpos_both(argvars, rettv, TRUE);
11899}
11900
Bram Moolenaar18081e32008-02-20 19:11:07 +000011901/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011902 * "getpos(string)" function
11903 */
11904 static void
11905f_getpos(argvars, rettv)
11906 typval_T *argvars;
11907 typval_T *rettv;
11908{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011909 getpos_both(argvars, rettv, FALSE);
11910}
11911
11912 static void
11913getpos_both(argvars, rettv, getcurpos)
11914 typval_T *argvars;
11915 typval_T *rettv;
11916 int getcurpos;
11917{
Bram Moolenaara5525202006-03-02 22:52:09 +000011918 pos_T *fp;
11919 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011920 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011921
11922 if (rettv_list_alloc(rettv) == OK)
11923 {
11924 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011925 if (getcurpos)
11926 fp = &curwin->w_cursor;
11927 else
11928 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011929 if (fnum != -1)
11930 list_append_number(l, (varnumber_T)fnum);
11931 else
11932 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011933 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11934 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011935 list_append_number(l, (fp != NULL)
11936 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011937 : (varnumber_T)0);
11938 list_append_number(l,
11939#ifdef FEAT_VIRTUALEDIT
11940 (fp != NULL) ? (varnumber_T)fp->coladd :
11941#endif
11942 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011943 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010011944 list_append_number(l, curwin->w_curswant == MAXCOL ?
11945 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000011946 }
11947 else
11948 rettv->vval.v_number = FALSE;
11949}
11950
11951/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011952 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011953 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011954 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011955f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011956 typval_T *argvars UNUSED;
11957 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011958{
11959#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011960 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011961#endif
11962
Bram Moolenaar2641f772005-03-25 21:58:17 +000011963#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011964 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011965 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011966 wp = NULL;
11967 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11968 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011969 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011970 if (wp == NULL)
11971 return;
11972 }
11973
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011974 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011975 }
11976#endif
11977}
11978
11979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011980 * "getreg()" function
11981 */
11982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011983f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011984 typval_T *argvars;
11985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986{
11987 char_u *strregname;
11988 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011989 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011990 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011991 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011992
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011993 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011994 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011995 strregname = get_tv_string_chk(&argvars[0]);
11996 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011997 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011999 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012000 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12001 return_list = get_tv_number_chk(&argvars[2], &error);
12002 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012005 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012006
12007 if (error)
12008 return;
12009
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 regname = (strregname == NULL ? '"' : *strregname);
12011 if (regname == 0)
12012 regname = '"';
12013
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012014 if (return_list)
12015 {
12016 rettv->v_type = VAR_LIST;
12017 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12018 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012019 if (rettv->vval.v_list != NULL)
12020 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012021 }
12022 else
12023 {
12024 rettv->v_type = VAR_STRING;
12025 rettv->vval.v_string = get_reg_contents(regname,
12026 arg2 ? GREG_EXPR_SRC : 0);
12027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028}
12029
12030/*
12031 * "getregtype()" function
12032 */
12033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012034f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012035 typval_T *argvars;
12036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037{
12038 char_u *strregname;
12039 int regname;
12040 char_u buf[NUMBUFLEN + 2];
12041 long reglen = 0;
12042
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012043 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012044 {
12045 strregname = get_tv_string_chk(&argvars[0]);
12046 if (strregname == NULL) /* type error; errmsg already given */
12047 {
12048 rettv->v_type = VAR_STRING;
12049 rettv->vval.v_string = NULL;
12050 return;
12051 }
12052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012053 else
12054 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012055 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056
12057 regname = (strregname == NULL ? '"' : *strregname);
12058 if (regname == 0)
12059 regname = '"';
12060
12061 buf[0] = NUL;
12062 buf[1] = NUL;
12063 switch (get_reg_type(regname, &reglen))
12064 {
12065 case MLINE: buf[0] = 'V'; break;
12066 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 case MBLOCK:
12068 buf[0] = Ctrl_V;
12069 sprintf((char *)buf + 1, "%ld", reglen + 1);
12070 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012072 rettv->v_type = VAR_STRING;
12073 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074}
12075
12076/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012077 * "gettabvar()" function
12078 */
12079 static void
12080f_gettabvar(argvars, rettv)
12081 typval_T *argvars;
12082 typval_T *rettv;
12083{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012084 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012085 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012086 dictitem_T *v;
12087 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012088 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012089
12090 rettv->v_type = VAR_STRING;
12091 rettv->vval.v_string = NULL;
12092
12093 varname = get_tv_string_chk(&argvars[1]);
12094 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12095 if (tp != NULL && varname != NULL)
12096 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012097 /* Set tp to be our tabpage, temporarily. Also set the window to the
12098 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012099 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12100 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012101 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012102 /* look up the variable */
12103 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12104 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12105 if (v != NULL)
12106 {
12107 copy_tv(&v->di_tv, rettv);
12108 done = TRUE;
12109 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012110 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012111
12112 /* restore previous notion of curwin */
12113 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012114 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012115
12116 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012117 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012118}
12119
12120/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012121 * "gettabwinvar()" function
12122 */
12123 static void
12124f_gettabwinvar(argvars, rettv)
12125 typval_T *argvars;
12126 typval_T *rettv;
12127{
12128 getwinvar(argvars, rettv, 1);
12129}
12130
12131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012132 * "getwinposx()" function
12133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012135f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012136 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012138{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012139 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012140#ifdef FEAT_GUI
12141 if (gui.in_use)
12142 {
12143 int x, y;
12144
12145 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012147 }
12148#endif
12149}
12150
12151/*
12152 * "getwinposy()" function
12153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012155f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012156 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012158{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160#ifdef FEAT_GUI
12161 if (gui.in_use)
12162 {
12163 int x, y;
12164
12165 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012166 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 }
12168#endif
12169}
12170
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012171/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012172 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012173 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012174 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012175find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012176 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012177 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012178{
12179#ifdef FEAT_WINDOWS
12180 win_T *wp;
12181#endif
12182 int nr;
12183
12184 nr = get_tv_number_chk(vp, NULL);
12185
12186#ifdef FEAT_WINDOWS
12187 if (nr < 0)
12188 return NULL;
12189 if (nr == 0)
12190 return curwin;
12191
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012192 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12193 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012194 if (--nr <= 0)
12195 break;
12196 return wp;
12197#else
12198 if (nr == 0 || nr == 1)
12199 return curwin;
12200 return NULL;
12201#endif
12202}
12203
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204/*
12205 * "getwinvar()" function
12206 */
12207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012208f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012209 typval_T *argvars;
12210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012211{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012212 getwinvar(argvars, rettv, 0);
12213}
12214
12215/*
12216 * getwinvar() and gettabwinvar()
12217 */
12218 static void
12219getwinvar(argvars, rettv, off)
12220 typval_T *argvars;
12221 typval_T *rettv;
12222 int off; /* 1 for gettabwinvar() */
12223{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 win_T *win, *oldcurwin;
12225 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012226 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012227 tabpage_T *tp = NULL;
12228 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012229 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012231#ifdef FEAT_WINDOWS
12232 if (off == 1)
12233 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12234 else
12235 tp = curtab;
12236#endif
12237 win = find_win_by_nr(&argvars[off], tp);
12238 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012239 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012241 rettv->v_type = VAR_STRING;
12242 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243
12244 if (win != NULL && varname != NULL)
12245 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012246 /* Set curwin to be our win, temporarily. Also set the tabpage,
12247 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012248 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012249 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012250 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012251 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012252 if (get_option_tv(&varname, rettv, 1) == OK)
12253 done = TRUE;
12254 }
12255 else
12256 {
12257 /* Look up the variable. */
12258 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12259 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12260 varname, FALSE);
12261 if (v != NULL)
12262 {
12263 copy_tv(&v->di_tv, rettv);
12264 done = TRUE;
12265 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012268
12269 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012270 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 }
12272
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012273 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12274 /* use the default return value */
12275 copy_tv(&argvars[off + 2], rettv);
12276
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 --emsg_off;
12278}
12279
12280/*
12281 * "glob()" function
12282 */
12283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012284f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012285 typval_T *argvars;
12286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012288 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012289 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012290 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012292 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012293 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012295 if (argvars[1].v_type != VAR_UNKNOWN)
12296 {
12297 if (get_tv_number_chk(&argvars[1], &error))
12298 options |= WILD_KEEP_ALL;
12299 if (argvars[2].v_type != VAR_UNKNOWN
12300 && get_tv_number_chk(&argvars[2], &error))
12301 {
12302 rettv->v_type = VAR_LIST;
12303 rettv->vval.v_list = NULL;
12304 }
12305 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012306 if (!error)
12307 {
12308 ExpandInit(&xpc);
12309 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012310 if (p_wic)
12311 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012312 if (rettv->v_type == VAR_STRING)
12313 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012314 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012315 else if (rettv_list_alloc(rettv) != FAIL)
12316 {
12317 int i;
12318
12319 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12320 NULL, options, WILD_ALL_KEEP);
12321 for (i = 0; i < xpc.xp_numfiles; i++)
12322 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12323
12324 ExpandCleanup(&xpc);
12325 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012326 }
12327 else
12328 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329}
12330
12331/*
12332 * "globpath()" function
12333 */
12334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012336 typval_T *argvars;
12337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012339 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012340 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012341 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012342 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012343 garray_T ga;
12344 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012346 /* When the optional second argument is non-zero, don't remove matches
12347 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012348 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012349 if (argvars[2].v_type != VAR_UNKNOWN)
12350 {
12351 if (get_tv_number_chk(&argvars[2], &error))
12352 flags |= WILD_KEEP_ALL;
12353 if (argvars[3].v_type != VAR_UNKNOWN
12354 && get_tv_number_chk(&argvars[3], &error))
12355 {
12356 rettv->v_type = VAR_LIST;
12357 rettv->vval.v_list = NULL;
12358 }
12359 }
12360 if (file != NULL && !error)
12361 {
12362 ga_init2(&ga, (int)sizeof(char_u *), 10);
12363 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12364 if (rettv->v_type == VAR_STRING)
12365 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12366 else if (rettv_list_alloc(rettv) != FAIL)
12367 for (i = 0; i < ga.ga_len; ++i)
12368 list_append_string(rettv->vval.v_list,
12369 ((char_u **)(ga.ga_data))[i], -1);
12370 ga_clear_strings(&ga);
12371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012372 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012373 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374}
12375
12376/*
12377 * "has()" function
12378 */
12379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012381 typval_T *argvars;
12382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383{
12384 int i;
12385 char_u *name;
12386 int n = FALSE;
12387 static char *(has_list[]) =
12388 {
12389#ifdef AMIGA
12390 "amiga",
12391# ifdef FEAT_ARP
12392 "arp",
12393# endif
12394#endif
12395#ifdef __BEOS__
12396 "beos",
12397#endif
12398#ifdef MSDOS
12399# ifdef DJGPP
12400 "dos32",
12401# else
12402 "dos16",
12403# endif
12404#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012405#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 "mac",
12407#endif
12408#if defined(MACOS_X_UNIX)
12409 "macunix",
12410#endif
12411#ifdef OS2
12412 "os2",
12413#endif
12414#ifdef __QNX__
12415 "qnx",
12416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417#ifdef UNIX
12418 "unix",
12419#endif
12420#ifdef VMS
12421 "vms",
12422#endif
12423#ifdef WIN16
12424 "win16",
12425#endif
12426#ifdef WIN32
12427 "win32",
12428#endif
12429#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12430 "win32unix",
12431#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012432#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433 "win64",
12434#endif
12435#ifdef EBCDIC
12436 "ebcdic",
12437#endif
12438#ifndef CASE_INSENSITIVE_FILENAME
12439 "fname_case",
12440#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012441#ifdef HAVE_ACL
12442 "acl",
12443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444#ifdef FEAT_ARABIC
12445 "arabic",
12446#endif
12447#ifdef FEAT_AUTOCMD
12448 "autocmd",
12449#endif
12450#ifdef FEAT_BEVAL
12451 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012452# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12453 "balloon_multiline",
12454# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455#endif
12456#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12457 "builtin_terms",
12458# ifdef ALL_BUILTIN_TCAPS
12459 "all_builtin_terms",
12460# endif
12461#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012462#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12463 || defined(FEAT_GUI_W32) \
12464 || defined(FEAT_GUI_MOTIF))
12465 "browsefilter",
12466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467#ifdef FEAT_BYTEOFF
12468 "byte_offset",
12469#endif
12470#ifdef FEAT_CINDENT
12471 "cindent",
12472#endif
12473#ifdef FEAT_CLIENTSERVER
12474 "clientserver",
12475#endif
12476#ifdef FEAT_CLIPBOARD
12477 "clipboard",
12478#endif
12479#ifdef FEAT_CMDL_COMPL
12480 "cmdline_compl",
12481#endif
12482#ifdef FEAT_CMDHIST
12483 "cmdline_hist",
12484#endif
12485#ifdef FEAT_COMMENTS
12486 "comments",
12487#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012488#ifdef FEAT_CONCEAL
12489 "conceal",
12490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491#ifdef FEAT_CRYPT
12492 "cryptv",
12493#endif
12494#ifdef FEAT_CSCOPE
12495 "cscope",
12496#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012497#ifdef FEAT_CURSORBIND
12498 "cursorbind",
12499#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012500#ifdef CURSOR_SHAPE
12501 "cursorshape",
12502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503#ifdef DEBUG
12504 "debug",
12505#endif
12506#ifdef FEAT_CON_DIALOG
12507 "dialog_con",
12508#endif
12509#ifdef FEAT_GUI_DIALOG
12510 "dialog_gui",
12511#endif
12512#ifdef FEAT_DIFF
12513 "diff",
12514#endif
12515#ifdef FEAT_DIGRAPHS
12516 "digraphs",
12517#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012518#ifdef FEAT_DIRECTX
12519 "directx",
12520#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521#ifdef FEAT_DND
12522 "dnd",
12523#endif
12524#ifdef FEAT_EMACS_TAGS
12525 "emacs_tags",
12526#endif
12527 "eval", /* always present, of course! */
12528#ifdef FEAT_EX_EXTRA
12529 "ex_extra",
12530#endif
12531#ifdef FEAT_SEARCH_EXTRA
12532 "extra_search",
12533#endif
12534#ifdef FEAT_FKMAP
12535 "farsi",
12536#endif
12537#ifdef FEAT_SEARCHPATH
12538 "file_in_path",
12539#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012540#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012541 "filterpipe",
12542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543#ifdef FEAT_FIND_ID
12544 "find_in_path",
12545#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012546#ifdef FEAT_FLOAT
12547 "float",
12548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549#ifdef FEAT_FOLDING
12550 "folding",
12551#endif
12552#ifdef FEAT_FOOTER
12553 "footer",
12554#endif
12555#if !defined(USE_SYSTEM) && defined(UNIX)
12556 "fork",
12557#endif
12558#ifdef FEAT_GETTEXT
12559 "gettext",
12560#endif
12561#ifdef FEAT_GUI
12562 "gui",
12563#endif
12564#ifdef FEAT_GUI_ATHENA
12565# ifdef FEAT_GUI_NEXTAW
12566 "gui_neXtaw",
12567# else
12568 "gui_athena",
12569# endif
12570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571#ifdef FEAT_GUI_GTK
12572 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012573 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012575#ifdef FEAT_GUI_GNOME
12576 "gui_gnome",
12577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578#ifdef FEAT_GUI_MAC
12579 "gui_mac",
12580#endif
12581#ifdef FEAT_GUI_MOTIF
12582 "gui_motif",
12583#endif
12584#ifdef FEAT_GUI_PHOTON
12585 "gui_photon",
12586#endif
12587#ifdef FEAT_GUI_W16
12588 "gui_win16",
12589#endif
12590#ifdef FEAT_GUI_W32
12591 "gui_win32",
12592#endif
12593#ifdef FEAT_HANGULIN
12594 "hangul_input",
12595#endif
12596#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12597 "iconv",
12598#endif
12599#ifdef FEAT_INS_EXPAND
12600 "insert_expand",
12601#endif
12602#ifdef FEAT_JUMPLIST
12603 "jumplist",
12604#endif
12605#ifdef FEAT_KEYMAP
12606 "keymap",
12607#endif
12608#ifdef FEAT_LANGMAP
12609 "langmap",
12610#endif
12611#ifdef FEAT_LIBCALL
12612 "libcall",
12613#endif
12614#ifdef FEAT_LINEBREAK
12615 "linebreak",
12616#endif
12617#ifdef FEAT_LISP
12618 "lispindent",
12619#endif
12620#ifdef FEAT_LISTCMDS
12621 "listcmds",
12622#endif
12623#ifdef FEAT_LOCALMAP
12624 "localmap",
12625#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012626#ifdef FEAT_LUA
12627# ifndef DYNAMIC_LUA
12628 "lua",
12629# endif
12630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631#ifdef FEAT_MENU
12632 "menu",
12633#endif
12634#ifdef FEAT_SESSION
12635 "mksession",
12636#endif
12637#ifdef FEAT_MODIFY_FNAME
12638 "modify_fname",
12639#endif
12640#ifdef FEAT_MOUSE
12641 "mouse",
12642#endif
12643#ifdef FEAT_MOUSESHAPE
12644 "mouseshape",
12645#endif
12646#if defined(UNIX) || defined(VMS)
12647# ifdef FEAT_MOUSE_DEC
12648 "mouse_dec",
12649# endif
12650# ifdef FEAT_MOUSE_GPM
12651 "mouse_gpm",
12652# endif
12653# ifdef FEAT_MOUSE_JSB
12654 "mouse_jsbterm",
12655# endif
12656# ifdef FEAT_MOUSE_NET
12657 "mouse_netterm",
12658# endif
12659# ifdef FEAT_MOUSE_PTERM
12660 "mouse_pterm",
12661# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012662# ifdef FEAT_MOUSE_SGR
12663 "mouse_sgr",
12664# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012665# ifdef FEAT_SYSMOUSE
12666 "mouse_sysmouse",
12667# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012668# ifdef FEAT_MOUSE_URXVT
12669 "mouse_urxvt",
12670# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671# ifdef FEAT_MOUSE_XTERM
12672 "mouse_xterm",
12673# endif
12674#endif
12675#ifdef FEAT_MBYTE
12676 "multi_byte",
12677#endif
12678#ifdef FEAT_MBYTE_IME
12679 "multi_byte_ime",
12680#endif
12681#ifdef FEAT_MULTI_LANG
12682 "multi_lang",
12683#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012684#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012685#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012686 "mzscheme",
12687#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689#ifdef FEAT_OLE
12690 "ole",
12691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692#ifdef FEAT_PATH_EXTRA
12693 "path_extra",
12694#endif
12695#ifdef FEAT_PERL
12696#ifndef DYNAMIC_PERL
12697 "perl",
12698#endif
12699#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012700#ifdef FEAT_PERSISTENT_UNDO
12701 "persistent_undo",
12702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703#ifdef FEAT_PYTHON
12704#ifndef DYNAMIC_PYTHON
12705 "python",
12706#endif
12707#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012708#ifdef FEAT_PYTHON3
12709#ifndef DYNAMIC_PYTHON3
12710 "python3",
12711#endif
12712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713#ifdef FEAT_POSTSCRIPT
12714 "postscript",
12715#endif
12716#ifdef FEAT_PRINTER
12717 "printer",
12718#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012719#ifdef FEAT_PROFILE
12720 "profile",
12721#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012722#ifdef FEAT_RELTIME
12723 "reltime",
12724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725#ifdef FEAT_QUICKFIX
12726 "quickfix",
12727#endif
12728#ifdef FEAT_RIGHTLEFT
12729 "rightleft",
12730#endif
12731#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12732 "ruby",
12733#endif
12734#ifdef FEAT_SCROLLBIND
12735 "scrollbind",
12736#endif
12737#ifdef FEAT_CMDL_INFO
12738 "showcmd",
12739 "cmdline_info",
12740#endif
12741#ifdef FEAT_SIGNS
12742 "signs",
12743#endif
12744#ifdef FEAT_SMARTINDENT
12745 "smartindent",
12746#endif
12747#ifdef FEAT_SNIFF
12748 "sniff",
12749#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012750#ifdef STARTUPTIME
12751 "startuptime",
12752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753#ifdef FEAT_STL_OPT
12754 "statusline",
12755#endif
12756#ifdef FEAT_SUN_WORKSHOP
12757 "sun_workshop",
12758#endif
12759#ifdef FEAT_NETBEANS_INTG
12760 "netbeans_intg",
12761#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012762#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012763 "spell",
12764#endif
12765#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 "syntax",
12767#endif
12768#if defined(USE_SYSTEM) || !defined(UNIX)
12769 "system",
12770#endif
12771#ifdef FEAT_TAG_BINS
12772 "tag_binary",
12773#endif
12774#ifdef FEAT_TAG_OLDSTATIC
12775 "tag_old_static",
12776#endif
12777#ifdef FEAT_TAG_ANYWHITE
12778 "tag_any_white",
12779#endif
12780#ifdef FEAT_TCL
12781# ifndef DYNAMIC_TCL
12782 "tcl",
12783# endif
12784#endif
12785#ifdef TERMINFO
12786 "terminfo",
12787#endif
12788#ifdef FEAT_TERMRESPONSE
12789 "termresponse",
12790#endif
12791#ifdef FEAT_TEXTOBJ
12792 "textobjects",
12793#endif
12794#ifdef HAVE_TGETENT
12795 "tgetent",
12796#endif
12797#ifdef FEAT_TITLE
12798 "title",
12799#endif
12800#ifdef FEAT_TOOLBAR
12801 "toolbar",
12802#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012803#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12804 "unnamedplus",
12805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806#ifdef FEAT_USR_CMDS
12807 "user-commands", /* was accidentally included in 5.4 */
12808 "user_commands",
12809#endif
12810#ifdef FEAT_VIMINFO
12811 "viminfo",
12812#endif
12813#ifdef FEAT_VERTSPLIT
12814 "vertsplit",
12815#endif
12816#ifdef FEAT_VIRTUALEDIT
12817 "virtualedit",
12818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820#ifdef FEAT_VISUALEXTRA
12821 "visualextra",
12822#endif
12823#ifdef FEAT_VREPLACE
12824 "vreplace",
12825#endif
12826#ifdef FEAT_WILDIGN
12827 "wildignore",
12828#endif
12829#ifdef FEAT_WILDMENU
12830 "wildmenu",
12831#endif
12832#ifdef FEAT_WINDOWS
12833 "windows",
12834#endif
12835#ifdef FEAT_WAK
12836 "winaltkeys",
12837#endif
12838#ifdef FEAT_WRITEBACKUP
12839 "writebackup",
12840#endif
12841#ifdef FEAT_XIM
12842 "xim",
12843#endif
12844#ifdef FEAT_XFONTSET
12845 "xfontset",
12846#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012847#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012848 "xpm",
12849 "xpm_w32", /* for backward compatibility */
12850#else
12851# if defined(HAVE_XPM)
12852 "xpm",
12853# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855#ifdef USE_XSMP
12856 "xsmp",
12857#endif
12858#ifdef USE_XSMP_INTERACT
12859 "xsmp_interact",
12860#endif
12861#ifdef FEAT_XCLIPBOARD
12862 "xterm_clipboard",
12863#endif
12864#ifdef FEAT_XTERM_SAVE
12865 "xterm_save",
12866#endif
12867#if defined(UNIX) && defined(FEAT_X11)
12868 "X11",
12869#endif
12870 NULL
12871 };
12872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874 for (i = 0; has_list[i] != NULL; ++i)
12875 if (STRICMP(name, has_list[i]) == 0)
12876 {
12877 n = TRUE;
12878 break;
12879 }
12880
12881 if (n == FALSE)
12882 {
12883 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012884 {
12885 if (name[5] == '-'
12886 && STRLEN(name) > 11
12887 && vim_isdigit(name[6])
12888 && vim_isdigit(name[8])
12889 && vim_isdigit(name[10]))
12890 {
12891 int major = atoi((char *)name + 6);
12892 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012893
12894 /* Expect "patch-9.9.01234". */
12895 n = (major < VIM_VERSION_MAJOR
12896 || (major == VIM_VERSION_MAJOR
12897 && (minor < VIM_VERSION_MINOR
12898 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012899 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012900 }
12901 else
12902 n = has_patch(atoi((char *)name + 5));
12903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 else if (STRICMP(name, "vim_starting") == 0)
12905 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012906#ifdef FEAT_MBYTE
12907 else if (STRICMP(name, "multi_byte_encoding") == 0)
12908 n = has_mbyte;
12909#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012910#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12911 else if (STRICMP(name, "balloon_multiline") == 0)
12912 n = multiline_balloon_available();
12913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914#ifdef DYNAMIC_TCL
12915 else if (STRICMP(name, "tcl") == 0)
12916 n = tcl_enabled(FALSE);
12917#endif
12918#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12919 else if (STRICMP(name, "iconv") == 0)
12920 n = iconv_enabled(FALSE);
12921#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012922#ifdef DYNAMIC_LUA
12923 else if (STRICMP(name, "lua") == 0)
12924 n = lua_enabled(FALSE);
12925#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012926#ifdef DYNAMIC_MZSCHEME
12927 else if (STRICMP(name, "mzscheme") == 0)
12928 n = mzscheme_enabled(FALSE);
12929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930#ifdef DYNAMIC_RUBY
12931 else if (STRICMP(name, "ruby") == 0)
12932 n = ruby_enabled(FALSE);
12933#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012934#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935#ifdef DYNAMIC_PYTHON
12936 else if (STRICMP(name, "python") == 0)
12937 n = python_enabled(FALSE);
12938#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012939#endif
12940#ifdef FEAT_PYTHON3
12941#ifdef DYNAMIC_PYTHON3
12942 else if (STRICMP(name, "python3") == 0)
12943 n = python3_enabled(FALSE);
12944#endif
12945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946#ifdef DYNAMIC_PERL
12947 else if (STRICMP(name, "perl") == 0)
12948 n = perl_enabled(FALSE);
12949#endif
12950#ifdef FEAT_GUI
12951 else if (STRICMP(name, "gui_running") == 0)
12952 n = (gui.in_use || gui.starting);
12953# ifdef FEAT_GUI_W32
12954 else if (STRICMP(name, "gui_win32s") == 0)
12955 n = gui_is_win32s();
12956# endif
12957# ifdef FEAT_BROWSE
12958 else if (STRICMP(name, "browse") == 0)
12959 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12960# endif
12961#endif
12962#ifdef FEAT_SYN_HL
12963 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012964 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965#endif
12966#if defined(WIN3264)
12967 else if (STRICMP(name, "win95") == 0)
12968 n = mch_windows95();
12969#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012970#ifdef FEAT_NETBEANS_INTG
12971 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012972 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012974 }
12975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012976 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977}
12978
12979/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012980 * "has_key()" function
12981 */
12982 static void
12983f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012984 typval_T *argvars;
12985 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012986{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012987 if (argvars[0].v_type != VAR_DICT)
12988 {
12989 EMSG(_(e_dictreq));
12990 return;
12991 }
12992 if (argvars[0].vval.v_dict == NULL)
12993 return;
12994
12995 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012996 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012997}
12998
12999/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013000 * "haslocaldir()" function
13001 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013002 static void
13003f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013004 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013005 typval_T *rettv;
13006{
13007 rettv->vval.v_number = (curwin->w_localdir != NULL);
13008}
13009
13010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011 * "hasmapto()" function
13012 */
13013 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013014f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013015 typval_T *argvars;
13016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017{
13018 char_u *name;
13019 char_u *mode;
13020 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013021 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013023 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013024 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 mode = (char_u *)"nvo";
13026 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013027 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013028 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013029 if (argvars[2].v_type != VAR_UNKNOWN)
13030 abbr = get_tv_number(&argvars[2]);
13031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013032
Bram Moolenaar2c932302006-03-18 21:42:09 +000013033 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013034 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013036 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037}
13038
13039/*
13040 * "histadd()" function
13041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013044 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046{
13047#ifdef FEAT_CMDHIST
13048 int histype;
13049 char_u *str;
13050 char_u buf[NUMBUFLEN];
13051#endif
13052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054 if (check_restricted() || check_secure())
13055 return;
13056#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013057 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13058 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059 if (histype >= 0)
13060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013061 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 if (*str != NUL)
13063 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013064 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013066 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067 return;
13068 }
13069 }
13070#endif
13071}
13072
13073/*
13074 * "histdel()" function
13075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013078 typval_T *argvars UNUSED;
13079 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080{
13081#ifdef FEAT_CMDHIST
13082 int n;
13083 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013086 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13087 if (str == NULL)
13088 n = 0;
13089 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013091 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013092 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013094 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 else
13097 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013098 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013099 get_tv_string_buf(&argvars[1], buf));
13100 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101#endif
13102}
13103
13104/*
13105 * "histget()" function
13106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013109 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111{
13112#ifdef FEAT_CMDHIST
13113 int type;
13114 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013115 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013117 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13118 if (str == NULL)
13119 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013121 {
13122 type = get_histtype(str);
13123 if (argvars[1].v_type == VAR_UNKNOWN)
13124 idx = get_history_idx(type);
13125 else
13126 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13127 /* -1 on type error */
13128 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013131 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013132#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013133 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013134}
13135
13136/*
13137 * "histnr()" function
13138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013141 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143{
13144 int i;
13145
13146#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013147 char_u *history = get_tv_string_chk(&argvars[0]);
13148
13149 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 if (i >= HIST_CMD && i < HIST_COUNT)
13151 i = get_history_idx(i);
13152 else
13153#endif
13154 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013155 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156}
13157
13158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 * "highlightID(name)" function
13160 */
13161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013162f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013163 typval_T *argvars;
13164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013166 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167}
13168
13169/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013170 * "highlight_exists()" function
13171 */
13172 static void
13173f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013174 typval_T *argvars;
13175 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013176{
13177 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13178}
13179
13180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 * "hostname()" function
13182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013184f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013185 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013186 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187{
13188 char_u hostname[256];
13189
13190 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013191 rettv->v_type = VAR_STRING;
13192 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193}
13194
13195/*
13196 * iconv() function
13197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013199f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013200 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202{
13203#ifdef FEAT_MBYTE
13204 char_u buf1[NUMBUFLEN];
13205 char_u buf2[NUMBUFLEN];
13206 char_u *from, *to, *str;
13207 vimconv_T vimconv;
13208#endif
13209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 rettv->v_type = VAR_STRING;
13211 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212
13213#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013214 str = get_tv_string(&argvars[0]);
13215 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13216 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 vimconv.vc_type = CONV_NONE;
13218 convert_setup(&vimconv, from, to);
13219
13220 /* If the encodings are equal, no conversion needed. */
13221 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013222 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013224 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225
13226 convert_setup(&vimconv, NULL, NULL);
13227 vim_free(from);
13228 vim_free(to);
13229#endif
13230}
13231
13232/*
13233 * "indent()" function
13234 */
13235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013237 typval_T *argvars;
13238 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239{
13240 linenr_T lnum;
13241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013242 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013244 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013247}
13248
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013249/*
13250 * "index()" function
13251 */
13252 static void
13253f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013254 typval_T *argvars;
13255 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013256{
Bram Moolenaar33570922005-01-25 22:26:29 +000013257 list_T *l;
13258 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013259 long idx = 0;
13260 int ic = FALSE;
13261
13262 rettv->vval.v_number = -1;
13263 if (argvars[0].v_type != VAR_LIST)
13264 {
13265 EMSG(_(e_listreq));
13266 return;
13267 }
13268 l = argvars[0].vval.v_list;
13269 if (l != NULL)
13270 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013271 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013272 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013273 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013274 int error = FALSE;
13275
Bram Moolenaar758711c2005-02-02 23:11:38 +000013276 /* Start at specified item. Use the cached index that list_find()
13277 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013278 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013279 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013280 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013281 ic = get_tv_number_chk(&argvars[3], &error);
13282 if (error)
13283 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013284 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013285
Bram Moolenaar758711c2005-02-02 23:11:38 +000013286 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013287 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013288 {
13289 rettv->vval.v_number = idx;
13290 break;
13291 }
13292 }
13293}
13294
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295static int inputsecret_flag = 0;
13296
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013297static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13298
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013300 * This function is used by f_input() and f_inputdialog() functions. The third
13301 * argument to f_input() specifies the type of completion to use at the
13302 * prompt. The third argument to f_inputdialog() specifies the value to return
13303 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 */
13305 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013306get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013307 typval_T *argvars;
13308 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013309 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013311 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 char_u *p = NULL;
13313 int c;
13314 char_u buf[NUMBUFLEN];
13315 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013316 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013317 int xp_type = EXPAND_NOTHING;
13318 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013320 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013321 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322
13323#ifdef NO_CONSOLE_INPUT
13324 /* While starting up, there is no place to enter text. */
13325 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327#endif
13328
13329 cmd_silent = FALSE; /* Want to see the prompt. */
13330 if (prompt != NULL)
13331 {
13332 /* Only the part of the message after the last NL is considered as
13333 * prompt for the command line */
13334 p = vim_strrchr(prompt, '\n');
13335 if (p == NULL)
13336 p = prompt;
13337 else
13338 {
13339 ++p;
13340 c = *p;
13341 *p = NUL;
13342 msg_start();
13343 msg_clr_eos();
13344 msg_puts_attr(prompt, echo_attr);
13345 msg_didout = FALSE;
13346 msg_starthere();
13347 *p = c;
13348 }
13349 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013351 if (argvars[1].v_type != VAR_UNKNOWN)
13352 {
13353 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13354 if (defstr != NULL)
13355 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013357 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013358 {
13359 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013360 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013361 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013362
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013363 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013364 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013365
Bram Moolenaar4463f292005-09-25 22:20:24 +000013366 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13367 if (xp_name == NULL)
13368 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013369
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013370 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013371
Bram Moolenaar4463f292005-09-25 22:20:24 +000013372 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13373 &xp_arg) == FAIL)
13374 return;
13375 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013376 }
13377
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013378 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013379 {
13380# ifdef FEAT_EX_EXTRA
13381 int save_ex_normal_busy = ex_normal_busy;
13382 ex_normal_busy = 0;
13383# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013384 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013385 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13386 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013387# ifdef FEAT_EX_EXTRA
13388 ex_normal_busy = save_ex_normal_busy;
13389# endif
13390 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013391 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013392 && argvars[1].v_type != VAR_UNKNOWN
13393 && argvars[2].v_type != VAR_UNKNOWN)
13394 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13395 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013396
13397 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013398
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013399 /* since the user typed this, no need to wait for return */
13400 need_wait_return = FALSE;
13401 msg_didout = FALSE;
13402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403 cmd_silent = cmd_silent_save;
13404}
13405
13406/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013407 * "input()" function
13408 * Also handles inputsecret() when inputsecret is set.
13409 */
13410 static void
13411f_input(argvars, rettv)
13412 typval_T *argvars;
13413 typval_T *rettv;
13414{
13415 get_user_input(argvars, rettv, FALSE);
13416}
13417
13418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 * "inputdialog()" function
13420 */
13421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013422f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 typval_T *argvars;
13424 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425{
13426#if defined(FEAT_GUI_TEXTDIALOG)
13427 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13428 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13429 {
13430 char_u *message;
13431 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013432 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013434 message = get_tv_string_chk(&argvars[0]);
13435 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013436 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013437 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 else
13439 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013440 if (message != NULL && defstr != NULL
13441 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013442 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013443 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444 else
13445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013446 if (message != NULL && defstr != NULL
13447 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013448 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 rettv->vval.v_string = vim_strsave(
13450 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013454 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 }
13456 else
13457#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013458 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459}
13460
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013461/*
13462 * "inputlist()" function
13463 */
13464 static void
13465f_inputlist(argvars, rettv)
13466 typval_T *argvars;
13467 typval_T *rettv;
13468{
13469 listitem_T *li;
13470 int selected;
13471 int mouse_used;
13472
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013473#ifdef NO_CONSOLE_INPUT
13474 /* While starting up, there is no place to enter text. */
13475 if (no_console_input())
13476 return;
13477#endif
13478 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13479 {
13480 EMSG2(_(e_listarg), "inputlist()");
13481 return;
13482 }
13483
13484 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013485 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013486 lines_left = Rows; /* avoid more prompt */
13487 msg_scroll = TRUE;
13488 msg_clr_eos();
13489
13490 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13491 {
13492 msg_puts(get_tv_string(&li->li_tv));
13493 msg_putchar('\n');
13494 }
13495
13496 /* Ask for choice. */
13497 selected = prompt_for_number(&mouse_used);
13498 if (mouse_used)
13499 selected -= lines_left;
13500
13501 rettv->vval.v_number = selected;
13502}
13503
13504
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13506
13507/*
13508 * "inputrestore()" function
13509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013512 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514{
13515 if (ga_userinput.ga_len > 0)
13516 {
13517 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013518 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13519 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013520 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 }
13522 else if (p_verbose > 1)
13523 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013524 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013525 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526 }
13527}
13528
13529/*
13530 * "inputsave()" function
13531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013533f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013534 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013535 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013537 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 if (ga_grow(&ga_userinput, 1) == OK)
13539 {
13540 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13541 + ga_userinput.ga_len);
13542 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013543 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 }
13545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547}
13548
13549/*
13550 * "inputsecret()" function
13551 */
13552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013554 typval_T *argvars;
13555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013556{
13557 ++cmdline_star;
13558 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 --cmdline_star;
13561 --inputsecret_flag;
13562}
13563
13564/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013565 * "insert()" function
13566 */
13567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013568f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013569 typval_T *argvars;
13570 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013571{
13572 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013573 listitem_T *item;
13574 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013575 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013576
13577 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013578 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013579 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013580 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013581 {
13582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013583 before = get_tv_number_chk(&argvars[2], &error);
13584 if (error)
13585 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013586
Bram Moolenaar758711c2005-02-02 23:11:38 +000013587 if (before == l->lv_len)
13588 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589 else
13590 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013591 item = list_find(l, before);
13592 if (item == NULL)
13593 {
13594 EMSGN(_(e_listidx), before);
13595 l = NULL;
13596 }
13597 }
13598 if (l != NULL)
13599 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013600 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013601 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013602 }
13603 }
13604}
13605
13606/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013607 * "invert(expr)" function
13608 */
13609 static void
13610f_invert(argvars, rettv)
13611 typval_T *argvars;
13612 typval_T *rettv;
13613{
13614 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13615}
13616
13617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 * "isdirectory()" function
13619 */
13620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013621f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013622 typval_T *argvars;
13623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626}
13627
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013628/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013629 * "islocked()" function
13630 */
13631 static void
13632f_islocked(argvars, rettv)
13633 typval_T *argvars;
13634 typval_T *rettv;
13635{
13636 lval_T lv;
13637 char_u *end;
13638 dictitem_T *di;
13639
13640 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013641 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13642 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013643 if (end != NULL && lv.ll_name != NULL)
13644 {
13645 if (*end != NUL)
13646 EMSG(_(e_trailing));
13647 else
13648 {
13649 if (lv.ll_tv == NULL)
13650 {
13651 if (check_changedtick(lv.ll_name))
13652 rettv->vval.v_number = 1; /* always locked */
13653 else
13654 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013655 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013656 if (di != NULL)
13657 {
13658 /* Consider a variable locked when:
13659 * 1. the variable itself is locked
13660 * 2. the value of the variable is locked.
13661 * 3. the List or Dict value is locked.
13662 */
13663 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13664 || tv_islocked(&di->di_tv));
13665 }
13666 }
13667 }
13668 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013669 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013670 else if (lv.ll_newkey != NULL)
13671 EMSG2(_(e_dictkey), lv.ll_newkey);
13672 else if (lv.ll_list != NULL)
13673 /* List item. */
13674 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13675 else
13676 /* Dictionary item. */
13677 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13678 }
13679 }
13680
13681 clear_lval(&lv);
13682}
13683
Bram Moolenaar33570922005-01-25 22:26:29 +000013684static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013685
13686/*
13687 * Turn a dict into a list:
13688 * "what" == 0: list of keys
13689 * "what" == 1: list of values
13690 * "what" == 2: list of items
13691 */
13692 static void
13693dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013694 typval_T *argvars;
13695 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013696 int what;
13697{
Bram Moolenaar33570922005-01-25 22:26:29 +000013698 list_T *l2;
13699 dictitem_T *di;
13700 hashitem_T *hi;
13701 listitem_T *li;
13702 listitem_T *li2;
13703 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013704 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013705
Bram Moolenaar8c711452005-01-14 21:53:12 +000013706 if (argvars[0].v_type != VAR_DICT)
13707 {
13708 EMSG(_(e_dictreq));
13709 return;
13710 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013711 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013712 return;
13713
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013714 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013715 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013716
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013717 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013718 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013719 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013720 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013721 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013722 --todo;
13723 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013724
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013725 li = listitem_alloc();
13726 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013727 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013728 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013729
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013730 if (what == 0)
13731 {
13732 /* keys() */
13733 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013734 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013735 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13736 }
13737 else if (what == 1)
13738 {
13739 /* values() */
13740 copy_tv(&di->di_tv, &li->li_tv);
13741 }
13742 else
13743 {
13744 /* items() */
13745 l2 = list_alloc();
13746 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013747 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013748 li->li_tv.vval.v_list = l2;
13749 if (l2 == NULL)
13750 break;
13751 ++l2->lv_refcount;
13752
13753 li2 = listitem_alloc();
13754 if (li2 == NULL)
13755 break;
13756 list_append(l2, li2);
13757 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013758 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013759 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13760
13761 li2 = listitem_alloc();
13762 if (li2 == NULL)
13763 break;
13764 list_append(l2, li2);
13765 copy_tv(&di->di_tv, &li2->li_tv);
13766 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013767 }
13768 }
13769}
13770
13771/*
13772 * "items(dict)" function
13773 */
13774 static void
13775f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013776 typval_T *argvars;
13777 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013778{
13779 dict_list(argvars, rettv, 2);
13780}
13781
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013783 * "join()" function
13784 */
13785 static void
13786f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013787 typval_T *argvars;
13788 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013789{
13790 garray_T ga;
13791 char_u *sep;
13792
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013793 if (argvars[0].v_type != VAR_LIST)
13794 {
13795 EMSG(_(e_listreq));
13796 return;
13797 }
13798 if (argvars[0].vval.v_list == NULL)
13799 return;
13800 if (argvars[1].v_type == VAR_UNKNOWN)
13801 sep = (char_u *)" ";
13802 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013803 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013804
13805 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013806
13807 if (sep != NULL)
13808 {
13809 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013810 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013811 ga_append(&ga, NUL);
13812 rettv->vval.v_string = (char_u *)ga.ga_data;
13813 }
13814 else
13815 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013816}
13817
13818/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013819 * "keys()" function
13820 */
13821 static void
13822f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013823 typval_T *argvars;
13824 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013825{
13826 dict_list(argvars, rettv, 0);
13827}
13828
13829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 * "last_buffer_nr()" function.
13831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013833f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013834 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836{
13837 int n = 0;
13838 buf_T *buf;
13839
13840 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13841 if (n < buf->b_fnum)
13842 n = buf->b_fnum;
13843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013844 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013845}
13846
13847/*
13848 * "len()" function
13849 */
13850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013851f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013852 typval_T *argvars;
13853 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013854{
13855 switch (argvars[0].v_type)
13856 {
13857 case VAR_STRING:
13858 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013859 rettv->vval.v_number = (varnumber_T)STRLEN(
13860 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013861 break;
13862 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013863 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013864 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013865 case VAR_DICT:
13866 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13867 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013868 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013869 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013870 break;
13871 }
13872}
13873
Bram Moolenaar33570922005-01-25 22:26:29 +000013874static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013875
13876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013877libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013878 typval_T *argvars;
13879 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013880 int type;
13881{
13882#ifdef FEAT_LIBCALL
13883 char_u *string_in;
13884 char_u **string_result;
13885 int nr_result;
13886#endif
13887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013888 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013889 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013890 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013891
13892 if (check_restricted() || check_secure())
13893 return;
13894
13895#ifdef FEAT_LIBCALL
13896 /* The first two args must be strings, otherwise its meaningless */
13897 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13898 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013899 string_in = NULL;
13900 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013901 string_in = argvars[2].vval.v_string;
13902 if (type == VAR_NUMBER)
13903 string_result = NULL;
13904 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013905 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013906 if (mch_libcall(argvars[0].vval.v_string,
13907 argvars[1].vval.v_string,
13908 string_in,
13909 argvars[2].vval.v_number,
13910 string_result,
13911 &nr_result) == OK
13912 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013914 }
13915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916}
13917
13918/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013919 * "libcall()" function
13920 */
13921 static void
13922f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013923 typval_T *argvars;
13924 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013925{
13926 libcall_common(argvars, rettv, VAR_STRING);
13927}
13928
13929/*
13930 * "libcallnr()" function
13931 */
13932 static void
13933f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013934 typval_T *argvars;
13935 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013936{
13937 libcall_common(argvars, rettv, VAR_NUMBER);
13938}
13939
13940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013941 * "line(string)" function
13942 */
13943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013945 typval_T *argvars;
13946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013947{
13948 linenr_T lnum = 0;
13949 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013950 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013952 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 if (fp != NULL)
13954 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013955 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956}
13957
13958/*
13959 * "line2byte(lnum)" function
13960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013962f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013963 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013964 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965{
13966#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013967 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968#else
13969 linenr_T lnum;
13970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013971 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013975 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13976 if (rettv->vval.v_number >= 0)
13977 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978#endif
13979}
13980
13981/*
13982 * "lispindent(lnum)" function
13983 */
13984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013985f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013986 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988{
13989#ifdef FEAT_LISP
13990 pos_T pos;
13991 linenr_T lnum;
13992
13993 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013994 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13996 {
13997 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013998 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 curwin->w_cursor = pos;
14000 }
14001 else
14002#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014003 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004}
14005
14006/*
14007 * "localtime()" function
14008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014010f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014011 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014014 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015}
14016
Bram Moolenaar33570922005-01-25 22:26:29 +000014017static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018
14019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014020get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014021 typval_T *argvars;
14022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 int exact;
14024{
14025 char_u *keys;
14026 char_u *which;
14027 char_u buf[NUMBUFLEN];
14028 char_u *keys_buf = NULL;
14029 char_u *rhs;
14030 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014031 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014032 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014033 mapblock_T *mp;
14034 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035
14036 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037 rettv->v_type = VAR_STRING;
14038 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014040 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 if (*keys == NUL)
14042 return;
14043
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014044 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014046 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014047 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014048 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014049 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014050 if (argvars[3].v_type != VAR_UNKNOWN)
14051 get_dict = get_tv_number(&argvars[3]);
14052 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 else
14055 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014056 if (which == NULL)
14057 return;
14058
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 mode = get_map_mode(&which, 0);
14060
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014061 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014062 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014063 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014064
14065 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014067 /* Return a string. */
14068 if (rhs != NULL)
14069 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070
Bram Moolenaarbd743252010-10-20 21:23:33 +020014071 }
14072 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14073 {
14074 /* Return a dictionary. */
14075 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14076 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14077 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078
Bram Moolenaarbd743252010-10-20 21:23:33 +020014079 dict_add_nr_str(dict, "lhs", 0L, lhs);
14080 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14081 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14082 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14083 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14084 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14085 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014086 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014087 dict_add_nr_str(dict, "mode", 0L, mapmode);
14088
14089 vim_free(lhs);
14090 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 }
14092}
14093
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014094#ifdef FEAT_FLOAT
14095/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014096 * "log()" function
14097 */
14098 static void
14099f_log(argvars, rettv)
14100 typval_T *argvars;
14101 typval_T *rettv;
14102{
14103 float_T f;
14104
14105 rettv->v_type = VAR_FLOAT;
14106 if (get_float_arg(argvars, &f) == OK)
14107 rettv->vval.v_float = log(f);
14108 else
14109 rettv->vval.v_float = 0.0;
14110}
14111
14112/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014113 * "log10()" function
14114 */
14115 static void
14116f_log10(argvars, rettv)
14117 typval_T *argvars;
14118 typval_T *rettv;
14119{
14120 float_T f;
14121
14122 rettv->v_type = VAR_FLOAT;
14123 if (get_float_arg(argvars, &f) == OK)
14124 rettv->vval.v_float = log10(f);
14125 else
14126 rettv->vval.v_float = 0.0;
14127}
14128#endif
14129
Bram Moolenaar1dced572012-04-05 16:54:08 +020014130#ifdef FEAT_LUA
14131/*
14132 * "luaeval()" function
14133 */
14134 static void
14135f_luaeval(argvars, rettv)
14136 typval_T *argvars;
14137 typval_T *rettv;
14138{
14139 char_u *str;
14140 char_u buf[NUMBUFLEN];
14141
14142 str = get_tv_string_buf(&argvars[0], buf);
14143 do_luaeval(str, argvars + 1, rettv);
14144}
14145#endif
14146
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014148 * "map()" function
14149 */
14150 static void
14151f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014152 typval_T *argvars;
14153 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014154{
14155 filter_map(argvars, rettv, TRUE);
14156}
14157
14158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014159 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 */
14161 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014163 typval_T *argvars;
14164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014165{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014166 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014167}
14168
14169/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014170 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 */
14172 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014174 typval_T *argvars;
14175 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014177 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178}
14179
Bram Moolenaar33570922005-01-25 22:26:29 +000014180static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181
14182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014184 typval_T *argvars;
14185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 int type;
14187{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014188 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014189 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014190 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 char_u *pat;
14192 regmatch_T regmatch;
14193 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014194 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195 char_u *save_cpo;
14196 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014197 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014198 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014199 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 list_T *l = NULL;
14201 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014202 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014203 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204
14205 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14206 save_cpo = p_cpo;
14207 p_cpo = (char_u *)"";
14208
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014209 rettv->vval.v_number = -1;
14210 if (type == 3)
14211 {
14212 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014213 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014214 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014215 }
14216 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014218 rettv->v_type = VAR_STRING;
14219 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014222 if (argvars[0].v_type == VAR_LIST)
14223 {
14224 if ((l = argvars[0].vval.v_list) == NULL)
14225 goto theend;
14226 li = l->lv_first;
14227 }
14228 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014229 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014230 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014231 len = (long)STRLEN(str);
14232 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014233
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14235 if (pat == NULL)
14236 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014237
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014238 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014240 int error = FALSE;
14241
14242 start = get_tv_number_chk(&argvars[2], &error);
14243 if (error)
14244 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014245 if (l != NULL)
14246 {
14247 li = list_find(l, start);
14248 if (li == NULL)
14249 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014250 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014251 }
14252 else
14253 {
14254 if (start < 0)
14255 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014256 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014257 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014258 /* When "count" argument is there ignore matches before "start",
14259 * otherwise skip part of the string. Differs when pattern is "^"
14260 * or "\<". */
14261 if (argvars[3].v_type != VAR_UNKNOWN)
14262 startcol = start;
14263 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014264 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014265 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014266 len -= start;
14267 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014268 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014269
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014270 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 nth = get_tv_number_chk(&argvars[3], &error);
14272 if (error)
14273 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 }
14275
14276 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14277 if (regmatch.regprog != NULL)
14278 {
14279 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014280
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014281 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014282 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014283 if (l != NULL)
14284 {
14285 if (li == NULL)
14286 {
14287 match = FALSE;
14288 break;
14289 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014290 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014291 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014292 if (str == NULL)
14293 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014294 }
14295
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014296 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014297
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014298 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014299 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014300 if (l == NULL && !match)
14301 break;
14302
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014303 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014304 if (l != NULL)
14305 {
14306 li = li->li_next;
14307 ++idx;
14308 }
14309 else
14310 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014311#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014312 startcol = (colnr_T)(regmatch.startp[0]
14313 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014314#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014315 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014316#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014317 if (startcol > (colnr_T)len
14318 || str + startcol <= regmatch.startp[0])
14319 {
14320 match = FALSE;
14321 break;
14322 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014323 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014324 }
14325
14326 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014328 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014329 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014330 int i;
14331
14332 /* return list with matched string and submatches */
14333 for (i = 0; i < NSUBEXP; ++i)
14334 {
14335 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014336 {
14337 if (list_append_string(rettv->vval.v_list,
14338 (char_u *)"", 0) == FAIL)
14339 break;
14340 }
14341 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014342 regmatch.startp[i],
14343 (int)(regmatch.endp[i] - regmatch.startp[i]))
14344 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014345 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014346 }
14347 }
14348 else if (type == 2)
14349 {
14350 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014351 if (l != NULL)
14352 copy_tv(&li->li_tv, rettv);
14353 else
14354 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014356 }
14357 else if (l != NULL)
14358 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 else
14360 {
14361 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014362 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363 (varnumber_T)(regmatch.startp[0] - str);
14364 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014367 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 }
14369 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014370 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 }
14372
14373theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014374 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 p_cpo = save_cpo;
14376}
14377
14378/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014379 * "match()" function
14380 */
14381 static void
14382f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014383 typval_T *argvars;
14384 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014385{
14386 find_some_match(argvars, rettv, 1);
14387}
14388
14389/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014390 * "matchadd()" function
14391 */
14392 static void
14393f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014394 typval_T *argvars UNUSED;
14395 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014396{
14397#ifdef FEAT_SEARCH_EXTRA
14398 char_u buf[NUMBUFLEN];
14399 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14400 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14401 int prio = 10; /* default priority */
14402 int id = -1;
14403 int error = FALSE;
14404
14405 rettv->vval.v_number = -1;
14406
14407 if (grp == NULL || pat == NULL)
14408 return;
14409 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014410 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014411 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014412 if (argvars[3].v_type != VAR_UNKNOWN)
14413 id = get_tv_number_chk(&argvars[3], &error);
14414 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014415 if (error == TRUE)
14416 return;
14417 if (id >= 1 && id <= 3)
14418 {
14419 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14420 return;
14421 }
14422
Bram Moolenaarb3414592014-06-17 17:48:32 +020014423 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14424#endif
14425}
14426
14427/*
14428 * "matchaddpos()" function
14429 */
14430 static void
14431f_matchaddpos(argvars, rettv)
14432 typval_T *argvars UNUSED;
14433 typval_T *rettv UNUSED;
14434{
14435#ifdef FEAT_SEARCH_EXTRA
14436 char_u buf[NUMBUFLEN];
14437 char_u *group;
14438 int prio = 10;
14439 int id = -1;
14440 int error = FALSE;
14441 list_T *l;
14442
14443 rettv->vval.v_number = -1;
14444
14445 group = get_tv_string_buf_chk(&argvars[0], buf);
14446 if (group == NULL)
14447 return;
14448
14449 if (argvars[1].v_type != VAR_LIST)
14450 {
14451 EMSG2(_(e_listarg), "matchaddpos()");
14452 return;
14453 }
14454 l = argvars[1].vval.v_list;
14455 if (l == NULL)
14456 return;
14457
14458 if (argvars[2].v_type != VAR_UNKNOWN)
14459 {
14460 prio = get_tv_number_chk(&argvars[2], &error);
14461 if (argvars[3].v_type != VAR_UNKNOWN)
14462 id = get_tv_number_chk(&argvars[3], &error);
14463 }
14464 if (error == TRUE)
14465 return;
14466
14467 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14468 if (id == 1 || id == 2)
14469 {
14470 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14471 return;
14472 }
14473
14474 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014475#endif
14476}
14477
14478/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014479 * "matcharg()" function
14480 */
14481 static void
14482f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014483 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014484 typval_T *rettv;
14485{
14486 if (rettv_list_alloc(rettv) == OK)
14487 {
14488#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014489 int id = get_tv_number(&argvars[0]);
14490 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014491
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014492 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014493 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014494 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14495 {
14496 list_append_string(rettv->vval.v_list,
14497 syn_id2name(m->hlg_id), -1);
14498 list_append_string(rettv->vval.v_list, m->pattern, -1);
14499 }
14500 else
14501 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014502 list_append_string(rettv->vval.v_list, NULL, -1);
14503 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014504 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014505 }
14506#endif
14507 }
14508}
14509
14510/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014511 * "matchdelete()" function
14512 */
14513 static void
14514f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014515 typval_T *argvars UNUSED;
14516 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014517{
14518#ifdef FEAT_SEARCH_EXTRA
14519 rettv->vval.v_number = match_delete(curwin,
14520 (int)get_tv_number(&argvars[0]), TRUE);
14521#endif
14522}
14523
14524/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014525 * "matchend()" function
14526 */
14527 static void
14528f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014529 typval_T *argvars;
14530 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014531{
14532 find_some_match(argvars, rettv, 0);
14533}
14534
14535/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014536 * "matchlist()" function
14537 */
14538 static void
14539f_matchlist(argvars, rettv)
14540 typval_T *argvars;
14541 typval_T *rettv;
14542{
14543 find_some_match(argvars, rettv, 3);
14544}
14545
14546/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014547 * "matchstr()" function
14548 */
14549 static void
14550f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014551 typval_T *argvars;
14552 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014553{
14554 find_some_match(argvars, rettv, 2);
14555}
14556
Bram Moolenaar33570922005-01-25 22:26:29 +000014557static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014558
14559 static void
14560max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014561 typval_T *argvars;
14562 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014563 int domax;
14564{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014565 long n = 0;
14566 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014568
14569 if (argvars[0].v_type == VAR_LIST)
14570 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014571 list_T *l;
14572 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014573
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014574 l = argvars[0].vval.v_list;
14575 if (l != NULL)
14576 {
14577 li = l->lv_first;
14578 if (li != NULL)
14579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014580 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014581 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014582 {
14583 li = li->li_next;
14584 if (li == NULL)
14585 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014586 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014587 if (domax ? i > n : i < n)
14588 n = i;
14589 }
14590 }
14591 }
14592 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014593 else if (argvars[0].v_type == VAR_DICT)
14594 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014595 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014596 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014597 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014598 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014599
14600 d = argvars[0].vval.v_dict;
14601 if (d != NULL)
14602 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014603 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014604 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014605 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014606 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014607 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014608 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014609 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014610 if (first)
14611 {
14612 n = i;
14613 first = FALSE;
14614 }
14615 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014616 n = i;
14617 }
14618 }
14619 }
14620 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014621 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014622 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014624}
14625
14626/*
14627 * "max()" function
14628 */
14629 static void
14630f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014631 typval_T *argvars;
14632 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014633{
14634 max_min(argvars, rettv, TRUE);
14635}
14636
14637/*
14638 * "min()" function
14639 */
14640 static void
14641f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 typval_T *argvars;
14643 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014644{
14645 max_min(argvars, rettv, FALSE);
14646}
14647
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014648static int mkdir_recurse __ARGS((char_u *dir, int prot));
14649
14650/*
14651 * Create the directory in which "dir" is located, and higher levels when
14652 * needed.
14653 */
14654 static int
14655mkdir_recurse(dir, prot)
14656 char_u *dir;
14657 int prot;
14658{
14659 char_u *p;
14660 char_u *updir;
14661 int r = FAIL;
14662
14663 /* Get end of directory name in "dir".
14664 * We're done when it's "/" or "c:/". */
14665 p = gettail_sep(dir);
14666 if (p <= get_past_head(dir))
14667 return OK;
14668
14669 /* If the directory exists we're done. Otherwise: create it.*/
14670 updir = vim_strnsave(dir, (int)(p - dir));
14671 if (updir == NULL)
14672 return FAIL;
14673 if (mch_isdir(updir))
14674 r = OK;
14675 else if (mkdir_recurse(updir, prot) == OK)
14676 r = vim_mkdir_emsg(updir, prot);
14677 vim_free(updir);
14678 return r;
14679}
14680
14681#ifdef vim_mkdir
14682/*
14683 * "mkdir()" function
14684 */
14685 static void
14686f_mkdir(argvars, rettv)
14687 typval_T *argvars;
14688 typval_T *rettv;
14689{
14690 char_u *dir;
14691 char_u buf[NUMBUFLEN];
14692 int prot = 0755;
14693
14694 rettv->vval.v_number = FAIL;
14695 if (check_restricted() || check_secure())
14696 return;
14697
14698 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014699 if (*dir == NUL)
14700 rettv->vval.v_number = FAIL;
14701 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014702 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014703 if (*gettail(dir) == NUL)
14704 /* remove trailing slashes */
14705 *gettail_sep(dir) = NUL;
14706
14707 if (argvars[1].v_type != VAR_UNKNOWN)
14708 {
14709 if (argvars[2].v_type != VAR_UNKNOWN)
14710 prot = get_tv_number_chk(&argvars[2], NULL);
14711 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14712 mkdir_recurse(dir, prot);
14713 }
14714 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014715 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014716}
14717#endif
14718
Bram Moolenaar0d660222005-01-07 21:51:51 +000014719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014720 * "mode()" function
14721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014723f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014724 typval_T *argvars;
14725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014727 char_u buf[3];
14728
14729 buf[1] = NUL;
14730 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014731
Bram Moolenaar071d4272004-06-13 20:20:40 +000014732 if (VIsual_active)
14733 {
14734 if (VIsual_select)
14735 buf[0] = VIsual_mode + 's' - 'v';
14736 else
14737 buf[0] = VIsual_mode;
14738 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014739 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014740 || State == CONFIRM)
14741 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014743 if (State == ASKMORE)
14744 buf[1] = 'm';
14745 else if (State == CONFIRM)
14746 buf[1] = '?';
14747 }
14748 else if (State == EXTERNCMD)
14749 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 else if (State & INSERT)
14751 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014752#ifdef FEAT_VREPLACE
14753 if (State & VREPLACE_FLAG)
14754 {
14755 buf[0] = 'R';
14756 buf[1] = 'v';
14757 }
14758 else
14759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 if (State & REPLACE_FLAG)
14761 buf[0] = 'R';
14762 else
14763 buf[0] = 'i';
14764 }
14765 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014766 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014768 if (exmode_active)
14769 buf[1] = 'v';
14770 }
14771 else if (exmode_active)
14772 {
14773 buf[0] = 'c';
14774 buf[1] = 'e';
14775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014778 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014779 if (finish_op)
14780 buf[1] = 'o';
14781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014783 /* Clear out the minor mode when the argument is not a non-zero number or
14784 * non-empty string. */
14785 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014786 buf[1] = NUL;
14787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788 rettv->vval.v_string = vim_strsave(buf);
14789 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790}
14791
Bram Moolenaar429fa852013-04-15 12:27:36 +020014792#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014793/*
14794 * "mzeval()" function
14795 */
14796 static void
14797f_mzeval(argvars, rettv)
14798 typval_T *argvars;
14799 typval_T *rettv;
14800{
14801 char_u *str;
14802 char_u buf[NUMBUFLEN];
14803
14804 str = get_tv_string_buf(&argvars[0], buf);
14805 do_mzeval(str, rettv);
14806}
Bram Moolenaar75676462013-01-30 14:55:42 +010014807
14808 void
14809mzscheme_call_vim(name, args, rettv)
14810 char_u *name;
14811 typval_T *args;
14812 typval_T *rettv;
14813{
14814 typval_T argvars[3];
14815
14816 argvars[0].v_type = VAR_STRING;
14817 argvars[0].vval.v_string = name;
14818 copy_tv(args, &argvars[1]);
14819 argvars[2].v_type = VAR_UNKNOWN;
14820 f_call(argvars, rettv);
14821 clear_tv(&argvars[1]);
14822}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014823#endif
14824
Bram Moolenaar071d4272004-06-13 20:20:40 +000014825/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014826 * "nextnonblank()" function
14827 */
14828 static void
14829f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014830 typval_T *argvars;
14831 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014832{
14833 linenr_T lnum;
14834
14835 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014837 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014838 {
14839 lnum = 0;
14840 break;
14841 }
14842 if (*skipwhite(ml_get(lnum)) != NUL)
14843 break;
14844 }
14845 rettv->vval.v_number = lnum;
14846}
14847
14848/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849 * "nr2char()" function
14850 */
14851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014852f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014853 typval_T *argvars;
14854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855{
14856 char_u buf[NUMBUFLEN];
14857
14858#ifdef FEAT_MBYTE
14859 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014860 {
14861 int utf8 = 0;
14862
14863 if (argvars[1].v_type != VAR_UNKNOWN)
14864 utf8 = get_tv_number_chk(&argvars[1], NULL);
14865 if (utf8)
14866 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14867 else
14868 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014870 else
14871#endif
14872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014873 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014874 buf[1] = NUL;
14875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014876 rettv->v_type = VAR_STRING;
14877 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014878}
14879
14880/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014881 * "or(expr, expr)" function
14882 */
14883 static void
14884f_or(argvars, rettv)
14885 typval_T *argvars;
14886 typval_T *rettv;
14887{
14888 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14889 | get_tv_number_chk(&argvars[1], NULL);
14890}
14891
14892/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014893 * "pathshorten()" function
14894 */
14895 static void
14896f_pathshorten(argvars, rettv)
14897 typval_T *argvars;
14898 typval_T *rettv;
14899{
14900 char_u *p;
14901
14902 rettv->v_type = VAR_STRING;
14903 p = get_tv_string_chk(&argvars[0]);
14904 if (p == NULL)
14905 rettv->vval.v_string = NULL;
14906 else
14907 {
14908 p = vim_strsave(p);
14909 rettv->vval.v_string = p;
14910 if (p != NULL)
14911 shorten_dir(p);
14912 }
14913}
14914
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014915#ifdef FEAT_FLOAT
14916/*
14917 * "pow()" function
14918 */
14919 static void
14920f_pow(argvars, rettv)
14921 typval_T *argvars;
14922 typval_T *rettv;
14923{
14924 float_T fx, fy;
14925
14926 rettv->v_type = VAR_FLOAT;
14927 if (get_float_arg(argvars, &fx) == OK
14928 && get_float_arg(&argvars[1], &fy) == OK)
14929 rettv->vval.v_float = pow(fx, fy);
14930 else
14931 rettv->vval.v_float = 0.0;
14932}
14933#endif
14934
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014935/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014936 * "prevnonblank()" function
14937 */
14938 static void
14939f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014940 typval_T *argvars;
14941 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014942{
14943 linenr_T lnum;
14944
14945 lnum = get_tv_lnum(argvars);
14946 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14947 lnum = 0;
14948 else
14949 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14950 --lnum;
14951 rettv->vval.v_number = lnum;
14952}
14953
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014954#ifdef HAVE_STDARG_H
14955/* This dummy va_list is here because:
14956 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14957 * - locally in the function results in a "used before set" warning
14958 * - using va_start() to initialize it gives "function with fixed args" error */
14959static va_list ap;
14960#endif
14961
Bram Moolenaar8c711452005-01-14 21:53:12 +000014962/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014963 * "printf()" function
14964 */
14965 static void
14966f_printf(argvars, rettv)
14967 typval_T *argvars;
14968 typval_T *rettv;
14969{
14970 rettv->v_type = VAR_STRING;
14971 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014972#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014973 {
14974 char_u buf[NUMBUFLEN];
14975 int len;
14976 char_u *s;
14977 int saved_did_emsg = did_emsg;
14978 char *fmt;
14979
14980 /* Get the required length, allocate the buffer and do it for real. */
14981 did_emsg = FALSE;
14982 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014983 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014984 if (!did_emsg)
14985 {
14986 s = alloc(len + 1);
14987 if (s != NULL)
14988 {
14989 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014990 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014991 }
14992 }
14993 did_emsg |= saved_did_emsg;
14994 }
14995#endif
14996}
14997
14998/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014999 * "pumvisible()" function
15000 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015001 static void
15002f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015003 typval_T *argvars UNUSED;
15004 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015005{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015006#ifdef FEAT_INS_EXPAND
15007 if (pum_visible())
15008 rettv->vval.v_number = 1;
15009#endif
15010}
15011
Bram Moolenaardb913952012-06-29 12:54:53 +020015012#ifdef FEAT_PYTHON3
15013/*
15014 * "py3eval()" function
15015 */
15016 static void
15017f_py3eval(argvars, rettv)
15018 typval_T *argvars;
15019 typval_T *rettv;
15020{
15021 char_u *str;
15022 char_u buf[NUMBUFLEN];
15023
15024 str = get_tv_string_buf(&argvars[0], buf);
15025 do_py3eval(str, rettv);
15026}
15027#endif
15028
15029#ifdef FEAT_PYTHON
15030/*
15031 * "pyeval()" function
15032 */
15033 static void
15034f_pyeval(argvars, rettv)
15035 typval_T *argvars;
15036 typval_T *rettv;
15037{
15038 char_u *str;
15039 char_u buf[NUMBUFLEN];
15040
15041 str = get_tv_string_buf(&argvars[0], buf);
15042 do_pyeval(str, rettv);
15043}
15044#endif
15045
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015046/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015047 * "range()" function
15048 */
15049 static void
15050f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015051 typval_T *argvars;
15052 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015053{
15054 long start;
15055 long end;
15056 long stride = 1;
15057 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015058 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015060 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015061 if (argvars[1].v_type == VAR_UNKNOWN)
15062 {
15063 end = start - 1;
15064 start = 0;
15065 }
15066 else
15067 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015068 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015071 }
15072
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015073 if (error)
15074 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015075 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015076 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015077 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015078 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015079 else
15080 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015081 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015082 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015083 if (list_append_number(rettv->vval.v_list,
15084 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015085 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015086 }
15087}
15088
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015089/*
15090 * "readfile()" function
15091 */
15092 static void
15093f_readfile(argvars, rettv)
15094 typval_T *argvars;
15095 typval_T *rettv;
15096{
15097 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015098 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015099 char_u *fname;
15100 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015101 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15102 int io_size = sizeof(buf);
15103 int readlen; /* size of last fread() */
15104 char_u *prev = NULL; /* previously read bytes, if any */
15105 long prevlen = 0; /* length of data in prev */
15106 long prevsize = 0; /* size of prev buffer */
15107 long maxline = MAXLNUM;
15108 long cnt = 0;
15109 char_u *p; /* position in buf */
15110 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015111
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015112 if (argvars[1].v_type != VAR_UNKNOWN)
15113 {
15114 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15115 binary = TRUE;
15116 if (argvars[2].v_type != VAR_UNKNOWN)
15117 maxline = get_tv_number(&argvars[2]);
15118 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015119
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015120 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015121 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015122
15123 /* Always open the file in binary mode, library functions have a mind of
15124 * their own about CR-LF conversion. */
15125 fname = get_tv_string(&argvars[0]);
15126 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15127 {
15128 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15129 return;
15130 }
15131
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015132 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015133 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015134 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015135
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015136 /* This for loop processes what was read, but is also entered at end
15137 * of file so that either:
15138 * - an incomplete line gets written
15139 * - a "binary" file gets an empty line at the end if it ends in a
15140 * newline. */
15141 for (p = buf, start = buf;
15142 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15143 ++p)
15144 {
15145 if (*p == '\n' || readlen <= 0)
15146 {
15147 listitem_T *li;
15148 char_u *s = NULL;
15149 long_u len = p - start;
15150
15151 /* Finished a line. Remove CRs before NL. */
15152 if (readlen > 0 && !binary)
15153 {
15154 while (len > 0 && start[len - 1] == '\r')
15155 --len;
15156 /* removal may cross back to the "prev" string */
15157 if (len == 0)
15158 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15159 --prevlen;
15160 }
15161 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015162 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015163 else
15164 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015165 /* Change "prev" buffer to be the right size. This way
15166 * the bytes are only copied once, and very long lines are
15167 * allocated only once. */
15168 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015169 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015170 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015171 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015172 prev = NULL; /* the list will own the string */
15173 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015174 }
15175 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015176 if (s == NULL)
15177 {
15178 do_outofmem_msg((long_u) prevlen + len + 1);
15179 failed = TRUE;
15180 break;
15181 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015182
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015183 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015184 {
15185 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015186 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015187 break;
15188 }
15189 li->li_tv.v_type = VAR_STRING;
15190 li->li_tv.v_lock = 0;
15191 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015192 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015193
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015194 start = p + 1; /* step over newline */
15195 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015196 break;
15197 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015198 else if (*p == NUL)
15199 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015200#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015201 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15202 * when finding the BF and check the previous two bytes. */
15203 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015204 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015205 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15206 * + 1, these may be in the "prev" string. */
15207 char_u back1 = p >= buf + 1 ? p[-1]
15208 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15209 char_u back2 = p >= buf + 2 ? p[-2]
15210 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15211 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015212
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015213 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015214 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015215 char_u *dest = p - 2;
15216
15217 /* Usually a BOM is at the beginning of a file, and so at
15218 * the beginning of a line; then we can just step over it.
15219 */
15220 if (start == dest)
15221 start = p + 1;
15222 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015223 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015224 /* have to shuffle buf to close gap */
15225 int adjust_prevlen = 0;
15226
15227 if (dest < buf)
15228 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015229 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015230 dest = buf;
15231 }
15232 if (readlen > p - buf + 1)
15233 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15234 readlen -= 3 - adjust_prevlen;
15235 prevlen -= adjust_prevlen;
15236 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015237 }
15238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015239 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015240#endif
15241 } /* for */
15242
15243 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15244 break;
15245 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015246 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015247 /* There's part of a line in buf, store it in "prev". */
15248 if (p - start + prevlen >= prevsize)
15249 {
15250 /* need bigger "prev" buffer */
15251 char_u *newprev;
15252
15253 /* A common use case is ordinary text files and "prev" gets a
15254 * fragment of a line, so the first allocation is made
15255 * small, to avoid repeatedly 'allocing' large and
15256 * 'reallocing' small. */
15257 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015258 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015259 else
15260 {
15261 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015262 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015263 prevsize = grow50pc > growmin ? grow50pc : growmin;
15264 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015265 newprev = prev == NULL ? alloc(prevsize)
15266 : vim_realloc(prev, prevsize);
15267 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015268 {
15269 do_outofmem_msg((long_u)prevsize);
15270 failed = TRUE;
15271 break;
15272 }
15273 prev = newprev;
15274 }
15275 /* Add the line part to end of "prev". */
15276 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015277 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015278 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015279 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015280
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015281 /*
15282 * For a negative line count use only the lines at the end of the file,
15283 * free the rest.
15284 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015285 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015286 while (cnt > -maxline)
15287 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015288 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015289 --cnt;
15290 }
15291
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015292 if (failed)
15293 {
15294 list_free(rettv->vval.v_list, TRUE);
15295 /* readfile doc says an empty list is returned on error */
15296 rettv->vval.v_list = list_alloc();
15297 }
15298
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015299 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015300 fclose(fd);
15301}
15302
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015303#if defined(FEAT_RELTIME)
15304static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15305
15306/*
15307 * Convert a List to proftime_T.
15308 * Return FAIL when there is something wrong.
15309 */
15310 static int
15311list2proftime(arg, tm)
15312 typval_T *arg;
15313 proftime_T *tm;
15314{
15315 long n1, n2;
15316 int error = FALSE;
15317
15318 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15319 || arg->vval.v_list->lv_len != 2)
15320 return FAIL;
15321 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15322 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15323# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015324 tm->HighPart = n1;
15325 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015326# else
15327 tm->tv_sec = n1;
15328 tm->tv_usec = n2;
15329# endif
15330 return error ? FAIL : OK;
15331}
15332#endif /* FEAT_RELTIME */
15333
15334/*
15335 * "reltime()" function
15336 */
15337 static void
15338f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015339 typval_T *argvars UNUSED;
15340 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015341{
15342#ifdef FEAT_RELTIME
15343 proftime_T res;
15344 proftime_T start;
15345
15346 if (argvars[0].v_type == VAR_UNKNOWN)
15347 {
15348 /* No arguments: get current time. */
15349 profile_start(&res);
15350 }
15351 else if (argvars[1].v_type == VAR_UNKNOWN)
15352 {
15353 if (list2proftime(&argvars[0], &res) == FAIL)
15354 return;
15355 profile_end(&res);
15356 }
15357 else
15358 {
15359 /* Two arguments: compute the difference. */
15360 if (list2proftime(&argvars[0], &start) == FAIL
15361 || list2proftime(&argvars[1], &res) == FAIL)
15362 return;
15363 profile_sub(&res, &start);
15364 }
15365
15366 if (rettv_list_alloc(rettv) == OK)
15367 {
15368 long n1, n2;
15369
15370# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015371 n1 = res.HighPart;
15372 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015373# else
15374 n1 = res.tv_sec;
15375 n2 = res.tv_usec;
15376# endif
15377 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15378 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15379 }
15380#endif
15381}
15382
15383/*
15384 * "reltimestr()" function
15385 */
15386 static void
15387f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015388 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015389 typval_T *rettv;
15390{
15391#ifdef FEAT_RELTIME
15392 proftime_T tm;
15393#endif
15394
15395 rettv->v_type = VAR_STRING;
15396 rettv->vval.v_string = NULL;
15397#ifdef FEAT_RELTIME
15398 if (list2proftime(&argvars[0], &tm) == OK)
15399 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15400#endif
15401}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015402
Bram Moolenaar0d660222005-01-07 21:51:51 +000015403#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15404static void make_connection __ARGS((void));
15405static int check_connection __ARGS((void));
15406
15407 static void
15408make_connection()
15409{
15410 if (X_DISPLAY == NULL
15411# ifdef FEAT_GUI
15412 && !gui.in_use
15413# endif
15414 )
15415 {
15416 x_force_connect = TRUE;
15417 setup_term_clip();
15418 x_force_connect = FALSE;
15419 }
15420}
15421
15422 static int
15423check_connection()
15424{
15425 make_connection();
15426 if (X_DISPLAY == NULL)
15427 {
15428 EMSG(_("E240: No connection to Vim server"));
15429 return FAIL;
15430 }
15431 return OK;
15432}
15433#endif
15434
15435#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015436static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015437
15438 static void
15439remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015440 typval_T *argvars;
15441 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015442 int expr;
15443{
15444 char_u *server_name;
15445 char_u *keys;
15446 char_u *r = NULL;
15447 char_u buf[NUMBUFLEN];
15448# ifdef WIN32
15449 HWND w;
15450# else
15451 Window w;
15452# endif
15453
15454 if (check_restricted() || check_secure())
15455 return;
15456
15457# ifdef FEAT_X11
15458 if (check_connection() == FAIL)
15459 return;
15460# endif
15461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015462 server_name = get_tv_string_chk(&argvars[0]);
15463 if (server_name == NULL)
15464 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015465 keys = get_tv_string_buf(&argvars[1], buf);
15466# ifdef WIN32
15467 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15468# else
15469 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15470 < 0)
15471# endif
15472 {
15473 if (r != NULL)
15474 EMSG(r); /* sending worked but evaluation failed */
15475 else
15476 EMSG2(_("E241: Unable to send to %s"), server_name);
15477 return;
15478 }
15479
15480 rettv->vval.v_string = r;
15481
15482 if (argvars[2].v_type != VAR_UNKNOWN)
15483 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015484 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015485 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015486 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015487
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015488 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015489 v.di_tv.v_type = VAR_STRING;
15490 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015491 idvar = get_tv_string_chk(&argvars[2]);
15492 if (idvar != NULL)
15493 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015494 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015495 }
15496}
15497#endif
15498
15499/*
15500 * "remote_expr()" function
15501 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015502 static void
15503f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015504 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015505 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015506{
15507 rettv->v_type = VAR_STRING;
15508 rettv->vval.v_string = NULL;
15509#ifdef FEAT_CLIENTSERVER
15510 remote_common(argvars, rettv, TRUE);
15511#endif
15512}
15513
15514/*
15515 * "remote_foreground()" function
15516 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015517 static void
15518f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015519 typval_T *argvars UNUSED;
15520 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015521{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015522#ifdef FEAT_CLIENTSERVER
15523# ifdef WIN32
15524 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015525 {
15526 char_u *server_name = get_tv_string_chk(&argvars[0]);
15527
15528 if (server_name != NULL)
15529 serverForeground(server_name);
15530 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015531# else
15532 /* Send a foreground() expression to the server. */
15533 argvars[1].v_type = VAR_STRING;
15534 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15535 argvars[2].v_type = VAR_UNKNOWN;
15536 remote_common(argvars, rettv, TRUE);
15537 vim_free(argvars[1].vval.v_string);
15538# endif
15539#endif
15540}
15541
Bram Moolenaar0d660222005-01-07 21:51:51 +000015542 static void
15543f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015544 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015545 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015546{
15547#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015548 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015549 char_u *s = NULL;
15550# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015551 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015552# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015553 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015554
15555 if (check_restricted() || check_secure())
15556 {
15557 rettv->vval.v_number = -1;
15558 return;
15559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015560 serverid = get_tv_string_chk(&argvars[0]);
15561 if (serverid == NULL)
15562 {
15563 rettv->vval.v_number = -1;
15564 return; /* type error; errmsg already given */
15565 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015566# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015567 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015568 if (n == 0)
15569 rettv->vval.v_number = -1;
15570 else
15571 {
15572 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15573 rettv->vval.v_number = (s != NULL);
15574 }
15575# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015576 if (check_connection() == FAIL)
15577 return;
15578
15579 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015580 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015581# endif
15582
15583 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015585 char_u *retvar;
15586
Bram Moolenaar33570922005-01-25 22:26:29 +000015587 v.di_tv.v_type = VAR_STRING;
15588 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015589 retvar = get_tv_string_chk(&argvars[1]);
15590 if (retvar != NULL)
15591 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015592 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015593 }
15594#else
15595 rettv->vval.v_number = -1;
15596#endif
15597}
15598
Bram Moolenaar0d660222005-01-07 21:51:51 +000015599 static void
15600f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015601 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015602 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015603{
15604 char_u *r = NULL;
15605
15606#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015607 char_u *serverid = get_tv_string_chk(&argvars[0]);
15608
15609 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015610 {
15611# ifdef WIN32
15612 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015613 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015614
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015615 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015616 if (n != 0)
15617 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15618 if (r == NULL)
15619# else
15620 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015621 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015622# endif
15623 EMSG(_("E277: Unable to read a server reply"));
15624 }
15625#endif
15626 rettv->v_type = VAR_STRING;
15627 rettv->vval.v_string = r;
15628}
15629
15630/*
15631 * "remote_send()" function
15632 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633 static void
15634f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015636 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637{
15638 rettv->v_type = VAR_STRING;
15639 rettv->vval.v_string = NULL;
15640#ifdef FEAT_CLIENTSERVER
15641 remote_common(argvars, rettv, FALSE);
15642#endif
15643}
15644
15645/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015646 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015647 */
15648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015649f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015650 typval_T *argvars;
15651 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015652{
Bram Moolenaar33570922005-01-25 22:26:29 +000015653 list_T *l;
15654 listitem_T *item, *item2;
15655 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015656 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015657 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015658 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015659 dict_T *d;
15660 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015661 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015662
Bram Moolenaar8c711452005-01-14 21:53:12 +000015663 if (argvars[0].v_type == VAR_DICT)
15664 {
15665 if (argvars[2].v_type != VAR_UNKNOWN)
15666 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015667 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015668 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015670 key = get_tv_string_chk(&argvars[1]);
15671 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015673 di = dict_find(d, key, -1);
15674 if (di == NULL)
15675 EMSG2(_(e_dictkey), key);
15676 else
15677 {
15678 *rettv = di->di_tv;
15679 init_tv(&di->di_tv);
15680 dictitem_remove(d, di);
15681 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015683 }
15684 }
15685 else if (argvars[0].v_type != VAR_LIST)
15686 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015687 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015688 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015690 int error = FALSE;
15691
15692 idx = get_tv_number_chk(&argvars[1], &error);
15693 if (error)
15694 ; /* type error: do nothing, errmsg already given */
15695 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015696 EMSGN(_(e_listidx), idx);
15697 else
15698 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015699 if (argvars[2].v_type == VAR_UNKNOWN)
15700 {
15701 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015702 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015703 *rettv = item->li_tv;
15704 vim_free(item);
15705 }
15706 else
15707 {
15708 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015709 end = get_tv_number_chk(&argvars[2], &error);
15710 if (error)
15711 ; /* type error: do nothing */
15712 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015713 EMSGN(_(e_listidx), end);
15714 else
15715 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015716 int cnt = 0;
15717
15718 for (li = item; li != NULL; li = li->li_next)
15719 {
15720 ++cnt;
15721 if (li == item2)
15722 break;
15723 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015724 if (li == NULL) /* didn't find "item2" after "item" */
15725 EMSG(_(e_invrange));
15726 else
15727 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015728 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015729 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015730 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015731 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015732 l->lv_first = item;
15733 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015734 item->li_prev = NULL;
15735 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015736 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015737 }
15738 }
15739 }
15740 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015741 }
15742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743}
15744
15745/*
15746 * "rename({from}, {to})" function
15747 */
15748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015749f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015750 typval_T *argvars;
15751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752{
15753 char_u buf[NUMBUFLEN];
15754
15755 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015756 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15759 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760}
15761
15762/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015763 * "repeat()" function
15764 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015765 static void
15766f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015767 typval_T *argvars;
15768 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015769{
15770 char_u *p;
15771 int n;
15772 int slen;
15773 int len;
15774 char_u *r;
15775 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015776
15777 n = get_tv_number(&argvars[1]);
15778 if (argvars[0].v_type == VAR_LIST)
15779 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015780 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015781 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015782 if (list_extend(rettv->vval.v_list,
15783 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015784 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015785 }
15786 else
15787 {
15788 p = get_tv_string(&argvars[0]);
15789 rettv->v_type = VAR_STRING;
15790 rettv->vval.v_string = NULL;
15791
15792 slen = (int)STRLEN(p);
15793 len = slen * n;
15794 if (len <= 0)
15795 return;
15796
15797 r = alloc(len + 1);
15798 if (r != NULL)
15799 {
15800 for (i = 0; i < n; i++)
15801 mch_memmove(r + i * slen, p, (size_t)slen);
15802 r[len] = NUL;
15803 }
15804
15805 rettv->vval.v_string = r;
15806 }
15807}
15808
15809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810 * "resolve()" function
15811 */
15812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015813f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015814 typval_T *argvars;
15815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816{
15817 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015818#ifdef HAVE_READLINK
15819 char_u *buf = NULL;
15820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015822 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823#ifdef FEAT_SHORTCUT
15824 {
15825 char_u *v = NULL;
15826
15827 v = mch_resolve_shortcut(p);
15828 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015829 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015831 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 }
15833#else
15834# ifdef HAVE_READLINK
15835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836 char_u *cpy;
15837 int len;
15838 char_u *remain = NULL;
15839 char_u *q;
15840 int is_relative_to_current = FALSE;
15841 int has_trailing_pathsep = FALSE;
15842 int limit = 100;
15843
15844 p = vim_strsave(p);
15845
15846 if (p[0] == '.' && (vim_ispathsep(p[1])
15847 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15848 is_relative_to_current = TRUE;
15849
15850 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015851 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015852 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015854 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856
15857 q = getnextcomp(p);
15858 if (*q != NUL)
15859 {
15860 /* Separate the first path component in "p", and keep the
15861 * remainder (beginning with the path separator). */
15862 remain = vim_strsave(q - 1);
15863 q[-1] = NUL;
15864 }
15865
Bram Moolenaard9462e32011-04-11 21:35:11 +020015866 buf = alloc(MAXPATHL + 1);
15867 if (buf == NULL)
15868 goto fail;
15869
Bram Moolenaar071d4272004-06-13 20:20:40 +000015870 for (;;)
15871 {
15872 for (;;)
15873 {
15874 len = readlink((char *)p, (char *)buf, MAXPATHL);
15875 if (len <= 0)
15876 break;
15877 buf[len] = NUL;
15878
15879 if (limit-- == 0)
15880 {
15881 vim_free(p);
15882 vim_free(remain);
15883 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015884 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 goto fail;
15886 }
15887
15888 /* Ensure that the result will have a trailing path separator
15889 * if the argument has one. */
15890 if (remain == NULL && has_trailing_pathsep)
15891 add_pathsep(buf);
15892
15893 /* Separate the first path component in the link value and
15894 * concatenate the remainders. */
15895 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15896 if (*q != NUL)
15897 {
15898 if (remain == NULL)
15899 remain = vim_strsave(q - 1);
15900 else
15901 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015902 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 if (cpy != NULL)
15904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 vim_free(remain);
15906 remain = cpy;
15907 }
15908 }
15909 q[-1] = NUL;
15910 }
15911
15912 q = gettail(p);
15913 if (q > p && *q == NUL)
15914 {
15915 /* Ignore trailing path separator. */
15916 q[-1] = NUL;
15917 q = gettail(p);
15918 }
15919 if (q > p && !mch_isFullName(buf))
15920 {
15921 /* symlink is relative to directory of argument */
15922 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15923 if (cpy != NULL)
15924 {
15925 STRCPY(cpy, p);
15926 STRCPY(gettail(cpy), buf);
15927 vim_free(p);
15928 p = cpy;
15929 }
15930 }
15931 else
15932 {
15933 vim_free(p);
15934 p = vim_strsave(buf);
15935 }
15936 }
15937
15938 if (remain == NULL)
15939 break;
15940
15941 /* Append the first path component of "remain" to "p". */
15942 q = getnextcomp(remain + 1);
15943 len = q - remain - (*q != NUL);
15944 cpy = vim_strnsave(p, STRLEN(p) + len);
15945 if (cpy != NULL)
15946 {
15947 STRNCAT(cpy, remain, len);
15948 vim_free(p);
15949 p = cpy;
15950 }
15951 /* Shorten "remain". */
15952 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015953 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954 else
15955 {
15956 vim_free(remain);
15957 remain = NULL;
15958 }
15959 }
15960
15961 /* If the result is a relative path name, make it explicitly relative to
15962 * the current directory if and only if the argument had this form. */
15963 if (!vim_ispathsep(*p))
15964 {
15965 if (is_relative_to_current
15966 && *p != NUL
15967 && !(p[0] == '.'
15968 && (p[1] == NUL
15969 || vim_ispathsep(p[1])
15970 || (p[1] == '.'
15971 && (p[2] == NUL
15972 || vim_ispathsep(p[2]))))))
15973 {
15974 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015975 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015976 if (cpy != NULL)
15977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978 vim_free(p);
15979 p = cpy;
15980 }
15981 }
15982 else if (!is_relative_to_current)
15983 {
15984 /* Strip leading "./". */
15985 q = p;
15986 while (q[0] == '.' && vim_ispathsep(q[1]))
15987 q += 2;
15988 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015989 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 }
15991 }
15992
15993 /* Ensure that the result will have no trailing path separator
15994 * if the argument had none. But keep "/" or "//". */
15995 if (!has_trailing_pathsep)
15996 {
15997 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015998 if (after_pathsep(p, q))
15999 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 }
16001
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016002 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 }
16004# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006# endif
16007#endif
16008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010
16011#ifdef HAVE_READLINK
16012fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016013 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016015 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016016}
16017
16018/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016019 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 */
16021 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016022f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 typval_T *argvars;
16024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016025{
Bram Moolenaar33570922005-01-25 22:26:29 +000016026 list_T *l;
16027 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028
Bram Moolenaar0d660222005-01-07 21:51:51 +000016029 if (argvars[0].v_type != VAR_LIST)
16030 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016031 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016032 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016033 {
16034 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016035 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016036 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016037 while (li != NULL)
16038 {
16039 ni = li->li_prev;
16040 list_append(l, li);
16041 li = ni;
16042 }
16043 rettv->vval.v_list = l;
16044 rettv->v_type = VAR_LIST;
16045 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016046 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048}
16049
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016050#define SP_NOMOVE 0x01 /* don't move cursor */
16051#define SP_REPEAT 0x02 /* repeat to find outer pair */
16052#define SP_RETCOUNT 0x04 /* return matchcount */
16053#define SP_SETPCMARK 0x08 /* set previous context mark */
16054#define SP_START 0x10 /* accept match at start position */
16055#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16056#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016057
Bram Moolenaar33570922005-01-25 22:26:29 +000016058static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016059
16060/*
16061 * Get flags for a search function.
16062 * Possibly sets "p_ws".
16063 * Returns BACKWARD, FORWARD or zero (for an error).
16064 */
16065 static int
16066get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016067 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016068 int *flagsp;
16069{
16070 int dir = FORWARD;
16071 char_u *flags;
16072 char_u nbuf[NUMBUFLEN];
16073 int mask;
16074
16075 if (varp->v_type != VAR_UNKNOWN)
16076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016077 flags = get_tv_string_buf_chk(varp, nbuf);
16078 if (flags == NULL)
16079 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016080 while (*flags != NUL)
16081 {
16082 switch (*flags)
16083 {
16084 case 'b': dir = BACKWARD; break;
16085 case 'w': p_ws = TRUE; break;
16086 case 'W': p_ws = FALSE; break;
16087 default: mask = 0;
16088 if (flagsp != NULL)
16089 switch (*flags)
16090 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016091 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016092 case 'e': mask = SP_END; break;
16093 case 'm': mask = SP_RETCOUNT; break;
16094 case 'n': mask = SP_NOMOVE; break;
16095 case 'p': mask = SP_SUBPAT; break;
16096 case 'r': mask = SP_REPEAT; break;
16097 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016098 }
16099 if (mask == 0)
16100 {
16101 EMSG2(_(e_invarg2), flags);
16102 dir = 0;
16103 }
16104 else
16105 *flagsp |= mask;
16106 }
16107 if (dir == 0)
16108 break;
16109 ++flags;
16110 }
16111 }
16112 return dir;
16113}
16114
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016116 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016118 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016119search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016120 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016121 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016122 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016124 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 char_u *pat;
16126 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016127 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 int save_p_ws = p_ws;
16129 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016130 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016131 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016132 proftime_T tm;
16133#ifdef FEAT_RELTIME
16134 long time_limit = 0;
16135#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016136 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016137 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016139 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016140 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016141 if (dir == 0)
16142 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016143 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016144 if (flags & SP_START)
16145 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016146 if (flags & SP_END)
16147 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016148
Bram Moolenaar76929292008-01-06 19:07:36 +000016149 /* Optional arguments: line number to stop searching and timeout. */
16150 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016151 {
16152 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16153 if (lnum_stop < 0)
16154 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016155#ifdef FEAT_RELTIME
16156 if (argvars[3].v_type != VAR_UNKNOWN)
16157 {
16158 time_limit = get_tv_number_chk(&argvars[3], NULL);
16159 if (time_limit < 0)
16160 goto theend;
16161 }
16162#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016163 }
16164
Bram Moolenaar76929292008-01-06 19:07:36 +000016165#ifdef FEAT_RELTIME
16166 /* Set the time limit, if there is one. */
16167 profile_setlimit(time_limit, &tm);
16168#endif
16169
Bram Moolenaar231334e2005-07-25 20:46:57 +000016170 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016171 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016172 * Check to make sure only those flags are set.
16173 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16174 * flags cannot be set. Check for that condition also.
16175 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016176 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016177 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016179 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016180 goto theend;
16181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016183 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016184 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016185 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016186 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016188 if (flags & SP_SUBPAT)
16189 retval = subpatnum;
16190 else
16191 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016192 if (flags & SP_SETPCMARK)
16193 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016195 if (match_pos != NULL)
16196 {
16197 /* Store the match cursor position */
16198 match_pos->lnum = pos.lnum;
16199 match_pos->col = pos.col + 1;
16200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 /* "/$" will put the cursor after the end of the line, may need to
16202 * correct that here */
16203 check_cursor();
16204 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016205
16206 /* If 'n' flag is used: restore cursor position. */
16207 if (flags & SP_NOMOVE)
16208 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016209 else
16210 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016211theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016213
16214 return retval;
16215}
16216
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016217#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016218
16219/*
16220 * round() is not in C90, use ceil() or floor() instead.
16221 */
16222 float_T
16223vim_round(f)
16224 float_T f;
16225{
16226 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16227}
16228
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016229/*
16230 * "round({float})" function
16231 */
16232 static void
16233f_round(argvars, rettv)
16234 typval_T *argvars;
16235 typval_T *rettv;
16236{
16237 float_T f;
16238
16239 rettv->v_type = VAR_FLOAT;
16240 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016241 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016242 else
16243 rettv->vval.v_float = 0.0;
16244}
16245#endif
16246
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016247/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016248 * "screenattr()" function
16249 */
16250 static void
16251f_screenattr(argvars, rettv)
16252 typval_T *argvars UNUSED;
16253 typval_T *rettv;
16254{
16255 int row;
16256 int col;
16257 int c;
16258
16259 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16260 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16261 if (row < 0 || row >= screen_Rows
16262 || col < 0 || col >= screen_Columns)
16263 c = -1;
16264 else
16265 c = ScreenAttrs[LineOffset[row] + col];
16266 rettv->vval.v_number = c;
16267}
16268
16269/*
16270 * "screenchar()" function
16271 */
16272 static void
16273f_screenchar(argvars, rettv)
16274 typval_T *argvars UNUSED;
16275 typval_T *rettv;
16276{
16277 int row;
16278 int col;
16279 int off;
16280 int c;
16281
16282 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16283 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16284 if (row < 0 || row >= screen_Rows
16285 || col < 0 || col >= screen_Columns)
16286 c = -1;
16287 else
16288 {
16289 off = LineOffset[row] + col;
16290#ifdef FEAT_MBYTE
16291 if (enc_utf8 && ScreenLinesUC[off] != 0)
16292 c = ScreenLinesUC[off];
16293 else
16294#endif
16295 c = ScreenLines[off];
16296 }
16297 rettv->vval.v_number = c;
16298}
16299
16300/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016301 * "screencol()" function
16302 *
16303 * First column is 1 to be consistent with virtcol().
16304 */
16305 static void
16306f_screencol(argvars, rettv)
16307 typval_T *argvars UNUSED;
16308 typval_T *rettv;
16309{
16310 rettv->vval.v_number = screen_screencol() + 1;
16311}
16312
16313/*
16314 * "screenrow()" function
16315 */
16316 static void
16317f_screenrow(argvars, rettv)
16318 typval_T *argvars UNUSED;
16319 typval_T *rettv;
16320{
16321 rettv->vval.v_number = screen_screenrow() + 1;
16322}
16323
16324/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016325 * "search()" function
16326 */
16327 static void
16328f_search(argvars, rettv)
16329 typval_T *argvars;
16330 typval_T *rettv;
16331{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016332 int flags = 0;
16333
16334 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335}
16336
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016338 * "searchdecl()" function
16339 */
16340 static void
16341f_searchdecl(argvars, rettv)
16342 typval_T *argvars;
16343 typval_T *rettv;
16344{
16345 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016346 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016347 int error = FALSE;
16348 char_u *name;
16349
16350 rettv->vval.v_number = 1; /* default: FAIL */
16351
16352 name = get_tv_string_chk(&argvars[0]);
16353 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016354 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016355 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016356 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16357 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16358 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016359 if (!error && name != NULL)
16360 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016361 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016362}
16363
16364/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016365 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016367 static int
16368searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016369 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016370 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371{
16372 char_u *spat, *mpat, *epat;
16373 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 int dir;
16376 int flags = 0;
16377 char_u nbuf1[NUMBUFLEN];
16378 char_u nbuf2[NUMBUFLEN];
16379 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016380 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016381 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016382 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383
Bram Moolenaar071d4272004-06-13 20:20:40 +000016384 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016385 spat = get_tv_string_chk(&argvars[0]);
16386 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16387 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16388 if (spat == NULL || mpat == NULL || epat == NULL)
16389 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 /* Handle the optional fourth argument: flags */
16392 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016393 if (dir == 0)
16394 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016395
16396 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016397 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16398 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016399 if ((flags & (SP_END | SP_SUBPAT)) != 0
16400 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016401 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016402 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016403 goto theend;
16404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016406 /* Using 'r' implies 'W', otherwise it doesn't work. */
16407 if (flags & SP_REPEAT)
16408 p_ws = FALSE;
16409
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016410 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016411 if (argvars[3].v_type == VAR_UNKNOWN
16412 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016413 skip = (char_u *)"";
16414 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016416 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016417 if (argvars[5].v_type != VAR_UNKNOWN)
16418 {
16419 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16420 if (lnum_stop < 0)
16421 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016422#ifdef FEAT_RELTIME
16423 if (argvars[6].v_type != VAR_UNKNOWN)
16424 {
16425 time_limit = get_tv_number_chk(&argvars[6], NULL);
16426 if (time_limit < 0)
16427 goto theend;
16428 }
16429#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016430 }
16431 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016432 if (skip == NULL)
16433 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016435 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016436 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016437
16438theend:
16439 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016440
16441 return retval;
16442}
16443
16444/*
16445 * "searchpair()" function
16446 */
16447 static void
16448f_searchpair(argvars, rettv)
16449 typval_T *argvars;
16450 typval_T *rettv;
16451{
16452 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16453}
16454
16455/*
16456 * "searchpairpos()" function
16457 */
16458 static void
16459f_searchpairpos(argvars, rettv)
16460 typval_T *argvars;
16461 typval_T *rettv;
16462{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016463 pos_T match_pos;
16464 int lnum = 0;
16465 int col = 0;
16466
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016467 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016468 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016469
16470 if (searchpair_cmn(argvars, &match_pos) > 0)
16471 {
16472 lnum = match_pos.lnum;
16473 col = match_pos.col;
16474 }
16475
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016476 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16477 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016478}
16479
16480/*
16481 * Search for a start/middle/end thing.
16482 * Used by searchpair(), see its documentation for the details.
16483 * Returns 0 or -1 for no match,
16484 */
16485 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016486do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16487 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016488 char_u *spat; /* start pattern */
16489 char_u *mpat; /* middle pattern */
16490 char_u *epat; /* end pattern */
16491 int dir; /* BACKWARD or FORWARD */
16492 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016493 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016494 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016495 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016496 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016497{
16498 char_u *save_cpo;
16499 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16500 long retval = 0;
16501 pos_T pos;
16502 pos_T firstpos;
16503 pos_T foundpos;
16504 pos_T save_cursor;
16505 pos_T save_pos;
16506 int n;
16507 int r;
16508 int nest = 1;
16509 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016510 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016511 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016512
16513 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16514 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016515 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016516
Bram Moolenaar76929292008-01-06 19:07:36 +000016517#ifdef FEAT_RELTIME
16518 /* Set the time limit, if there is one. */
16519 profile_setlimit(time_limit, &tm);
16520#endif
16521
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016522 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16523 * start/middle/end (pat3, for the top pair). */
16524 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16525 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16526 if (pat2 == NULL || pat3 == NULL)
16527 goto theend;
16528 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16529 if (*mpat == NUL)
16530 STRCPY(pat3, pat2);
16531 else
16532 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16533 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016534 if (flags & SP_START)
16535 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016536
Bram Moolenaar071d4272004-06-13 20:20:40 +000016537 save_cursor = curwin->w_cursor;
16538 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016539 clearpos(&firstpos);
16540 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541 pat = pat3;
16542 for (;;)
16543 {
16544 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016545 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016546 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16547 /* didn't find it or found the first match again: FAIL */
16548 break;
16549
16550 if (firstpos.lnum == 0)
16551 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016552 if (equalpos(pos, foundpos))
16553 {
16554 /* Found the same position again. Can happen with a pattern that
16555 * has "\zs" at the end and searching backwards. Advance one
16556 * character and try again. */
16557 if (dir == BACKWARD)
16558 decl(&pos);
16559 else
16560 incl(&pos);
16561 }
16562 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016563
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016564 /* clear the start flag to avoid getting stuck here */
16565 options &= ~SEARCH_START;
16566
Bram Moolenaar071d4272004-06-13 20:20:40 +000016567 /* If the skip pattern matches, ignore this match. */
16568 if (*skip != NUL)
16569 {
16570 save_pos = curwin->w_cursor;
16571 curwin->w_cursor = pos;
16572 r = eval_to_bool(skip, &err, NULL, FALSE);
16573 curwin->w_cursor = save_pos;
16574 if (err)
16575 {
16576 /* Evaluating {skip} caused an error, break here. */
16577 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016578 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016579 break;
16580 }
16581 if (r)
16582 continue;
16583 }
16584
16585 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16586 {
16587 /* Found end when searching backwards or start when searching
16588 * forward: nested pair. */
16589 ++nest;
16590 pat = pat2; /* nested, don't search for middle */
16591 }
16592 else
16593 {
16594 /* Found end when searching forward or start when searching
16595 * backward: end of (nested) pair; or found middle in outer pair. */
16596 if (--nest == 1)
16597 pat = pat3; /* outer level, search for middle */
16598 }
16599
16600 if (nest == 0)
16601 {
16602 /* Found the match: return matchcount or line number. */
16603 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016604 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016605 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016606 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016607 if (flags & SP_SETPCMARK)
16608 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 curwin->w_cursor = pos;
16610 if (!(flags & SP_REPEAT))
16611 break;
16612 nest = 1; /* search for next unmatched */
16613 }
16614 }
16615
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016616 if (match_pos != NULL)
16617 {
16618 /* Store the match cursor position */
16619 match_pos->lnum = curwin->w_cursor.lnum;
16620 match_pos->col = curwin->w_cursor.col + 1;
16621 }
16622
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016624 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625 curwin->w_cursor = save_cursor;
16626
16627theend:
16628 vim_free(pat2);
16629 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016630 if (p_cpo == empty_option)
16631 p_cpo = save_cpo;
16632 else
16633 /* Darn, evaluating the {skip} expression changed the value. */
16634 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016635
16636 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637}
16638
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016639/*
16640 * "searchpos()" function
16641 */
16642 static void
16643f_searchpos(argvars, rettv)
16644 typval_T *argvars;
16645 typval_T *rettv;
16646{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016647 pos_T match_pos;
16648 int lnum = 0;
16649 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016650 int n;
16651 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016652
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016653 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016654 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016655
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016656 n = search_cmn(argvars, &match_pos, &flags);
16657 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016658 {
16659 lnum = match_pos.lnum;
16660 col = match_pos.col;
16661 }
16662
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016663 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16664 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016665 if (flags & SP_SUBPAT)
16666 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016667}
16668
16669
Bram Moolenaar0d660222005-01-07 21:51:51 +000016670 static void
16671f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016672 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016675#ifdef FEAT_CLIENTSERVER
16676 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016677 char_u *server = get_tv_string_chk(&argvars[0]);
16678 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679
Bram Moolenaar0d660222005-01-07 21:51:51 +000016680 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016681 if (server == NULL || reply == NULL)
16682 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016683 if (check_restricted() || check_secure())
16684 return;
16685# ifdef FEAT_X11
16686 if (check_connection() == FAIL)
16687 return;
16688# endif
16689
16690 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016691 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016692 EMSG(_("E258: Unable to send to client"));
16693 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016695 rettv->vval.v_number = 0;
16696#else
16697 rettv->vval.v_number = -1;
16698#endif
16699}
16700
Bram Moolenaar0d660222005-01-07 21:51:51 +000016701 static void
16702f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016703 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016704 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016705{
16706 char_u *r = NULL;
16707
16708#ifdef FEAT_CLIENTSERVER
16709# ifdef WIN32
16710 r = serverGetVimNames();
16711# else
16712 make_connection();
16713 if (X_DISPLAY != NULL)
16714 r = serverGetVimNames(X_DISPLAY);
16715# endif
16716#endif
16717 rettv->v_type = VAR_STRING;
16718 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719}
16720
16721/*
16722 * "setbufvar()" function
16723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016725f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016726 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016727 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016728{
16729 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016730 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016732 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 char_u nbuf[NUMBUFLEN];
16734
16735 if (check_restricted() || check_secure())
16736 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016737 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16738 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016739 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016740 varp = &argvars[2];
16741
16742 if (buf != NULL && varname != NULL && varp != NULL)
16743 {
16744 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746
16747 if (*varname == '&')
16748 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016749 long numval;
16750 char_u *strval;
16751 int error = FALSE;
16752
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016754 numval = get_tv_number_chk(varp, &error);
16755 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016756 if (!error && strval != NULL)
16757 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016758 }
16759 else
16760 {
16761 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16762 if (bufvarname != NULL)
16763 {
16764 STRCPY(bufvarname, "b:");
16765 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016766 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 vim_free(bufvarname);
16768 }
16769 }
16770
16771 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774}
16775
16776/*
16777 * "setcmdpos()" function
16778 */
16779 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016780f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016781 typval_T *argvars;
16782 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016784 int pos = (int)get_tv_number(&argvars[0]) - 1;
16785
16786 if (pos >= 0)
16787 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788}
16789
16790/*
16791 * "setline()" function
16792 */
16793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016794f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016795 typval_T *argvars;
16796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797{
16798 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016799 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016800 list_T *l = NULL;
16801 listitem_T *li = NULL;
16802 long added = 0;
16803 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016805 lnum = get_tv_lnum(&argvars[0]);
16806 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016808 l = argvars[1].vval.v_list;
16809 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016811 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016812 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016813
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016814 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016815 for (;;)
16816 {
16817 if (l != NULL)
16818 {
16819 /* list argument, get next string */
16820 if (li == NULL)
16821 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016822 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016823 li = li->li_next;
16824 }
16825
16826 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016827 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016828 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016829
16830 /* When coming here from Insert mode, sync undo, so that this can be
16831 * undone separately from what was previously inserted. */
16832 if (u_sync_once == 2)
16833 {
16834 u_sync_once = 1; /* notify that u_sync() was called */
16835 u_sync(TRUE);
16836 }
16837
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016838 if (lnum <= curbuf->b_ml.ml_line_count)
16839 {
16840 /* existing line, replace it */
16841 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16842 {
16843 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016844 if (lnum == curwin->w_cursor.lnum)
16845 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016846 rettv->vval.v_number = 0; /* OK */
16847 }
16848 }
16849 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16850 {
16851 /* lnum is one past the last line, append the line */
16852 ++added;
16853 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16854 rettv->vval.v_number = 0; /* OK */
16855 }
16856
16857 if (l == NULL) /* only one string argument */
16858 break;
16859 ++lnum;
16860 }
16861
16862 if (added > 0)
16863 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016864}
16865
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016866static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16867
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016869 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016870 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016871 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016872set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016873 win_T *wp UNUSED;
16874 typval_T *list_arg UNUSED;
16875 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016876 typval_T *rettv;
16877{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016878#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016879 char_u *act;
16880 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016881#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016882
Bram Moolenaar2641f772005-03-25 21:58:17 +000016883 rettv->vval.v_number = -1;
16884
16885#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016886 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016887 EMSG(_(e_listreq));
16888 else
16889 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016890 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016891
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016892 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016893 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016894 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016895 if (act == NULL)
16896 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016897 if (*act == 'a' || *act == 'r')
16898 action = *act;
16899 }
16900
Bram Moolenaar81484f42012-12-05 15:16:47 +010016901 if (l != NULL && set_errorlist(wp, l, action,
16902 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016903 rettv->vval.v_number = 0;
16904 }
16905#endif
16906}
16907
16908/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016909 * "setloclist()" function
16910 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016911 static void
16912f_setloclist(argvars, rettv)
16913 typval_T *argvars;
16914 typval_T *rettv;
16915{
16916 win_T *win;
16917
16918 rettv->vval.v_number = -1;
16919
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016920 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016921 if (win != NULL)
16922 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16923}
16924
16925/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016926 * "setmatches()" function
16927 */
16928 static void
16929f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016930 typval_T *argvars UNUSED;
16931 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016932{
16933#ifdef FEAT_SEARCH_EXTRA
16934 list_T *l;
16935 listitem_T *li;
16936 dict_T *d;
16937
16938 rettv->vval.v_number = -1;
16939 if (argvars[0].v_type != VAR_LIST)
16940 {
16941 EMSG(_(e_listreq));
16942 return;
16943 }
16944 if ((l = argvars[0].vval.v_list) != NULL)
16945 {
16946
16947 /* To some extent make sure that we are dealing with a list from
16948 * "getmatches()". */
16949 li = l->lv_first;
16950 while (li != NULL)
16951 {
16952 if (li->li_tv.v_type != VAR_DICT
16953 || (d = li->li_tv.vval.v_dict) == NULL)
16954 {
16955 EMSG(_(e_invarg));
16956 return;
16957 }
16958 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16959 && dict_find(d, (char_u *)"pattern", -1) != NULL
16960 && dict_find(d, (char_u *)"priority", -1) != NULL
16961 && dict_find(d, (char_u *)"id", -1) != NULL))
16962 {
16963 EMSG(_(e_invarg));
16964 return;
16965 }
16966 li = li->li_next;
16967 }
16968
16969 clear_matches(curwin);
16970 li = l->lv_first;
16971 while (li != NULL)
16972 {
16973 d = li->li_tv.vval.v_dict;
16974 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16975 get_dict_string(d, (char_u *)"pattern", FALSE),
16976 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020016977 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016978 li = li->li_next;
16979 }
16980 rettv->vval.v_number = 0;
16981 }
16982#endif
16983}
16984
16985/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016986 * "setpos()" function
16987 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016988 static void
16989f_setpos(argvars, rettv)
16990 typval_T *argvars;
16991 typval_T *rettv;
16992{
16993 pos_T pos;
16994 int fnum;
16995 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016996 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016997
Bram Moolenaar08250432008-02-13 11:42:46 +000016998 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016999 name = get_tv_string_chk(argvars);
17000 if (name != NULL)
17001 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017002 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017003 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017004 if (--pos.col < 0)
17005 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017006 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017007 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017008 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017009 if (fnum == curbuf->b_fnum)
17010 {
17011 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017012 if (curswant >= 0)
17013 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017014 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017015 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017016 }
17017 else
17018 EMSG(_(e_invarg));
17019 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017020 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17021 {
17022 /* set mark */
17023 if (setmark_pos(name[1], &pos, fnum) == OK)
17024 rettv->vval.v_number = 0;
17025 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017026 else
17027 EMSG(_(e_invarg));
17028 }
17029 }
17030}
17031
17032/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017033 * "setqflist()" function
17034 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017035 static void
17036f_setqflist(argvars, rettv)
17037 typval_T *argvars;
17038 typval_T *rettv;
17039{
17040 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17041}
17042
17043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 * "setreg()" function
17045 */
17046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017047f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017048 typval_T *argvars;
17049 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050{
17051 int regname;
17052 char_u *strregname;
17053 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017054 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055 int append;
17056 char_u yank_type;
17057 long block_len;
17058
17059 block_len = -1;
17060 yank_type = MAUTO;
17061 append = FALSE;
17062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017063 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017064 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017066 if (strregname == NULL)
17067 return; /* type error; errmsg already given */
17068 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069 if (regname == 0 || regname == '@')
17070 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017072 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017074 stropt = get_tv_string_chk(&argvars[2]);
17075 if (stropt == NULL)
17076 return; /* type error */
17077 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 switch (*stropt)
17079 {
17080 case 'a': case 'A': /* append */
17081 append = TRUE;
17082 break;
17083 case 'v': case 'c': /* character-wise selection */
17084 yank_type = MCHAR;
17085 break;
17086 case 'V': case 'l': /* line-wise selection */
17087 yank_type = MLINE;
17088 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089 case 'b': case Ctrl_V: /* block-wise selection */
17090 yank_type = MBLOCK;
17091 if (VIM_ISDIGIT(stropt[1]))
17092 {
17093 ++stropt;
17094 block_len = getdigits(&stropt) - 1;
17095 --stropt;
17096 }
17097 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 }
17099 }
17100
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017101 if (argvars[1].v_type == VAR_LIST)
17102 {
17103 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017104 char_u **allocval;
17105 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017106 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017107 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017108 int len = argvars[1].vval.v_list->lv_len;
17109 listitem_T *li;
17110
Bram Moolenaar7d647822014-04-05 21:28:56 +020017111 /* First half: use for pointers to result lines; second half: use for
17112 * pointers to allocated copies. */
17113 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017114 if (lstval == NULL)
17115 return;
17116 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017117 allocval = lstval + len + 2;
17118 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017119
17120 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17121 li = li->li_next)
17122 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017123 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017124 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017125 goto free_lstval;
17126 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017127 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017128 /* Need to make a copy, next get_tv_string_buf_chk() will
17129 * overwrite the string. */
17130 strval = vim_strsave(buf);
17131 if (strval == NULL)
17132 goto free_lstval;
17133 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017134 }
17135 *curval++ = strval;
17136 }
17137 *curval++ = NULL;
17138
17139 write_reg_contents_lst(regname, lstval, -1,
17140 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017141free_lstval:
17142 while (curallocval > allocval)
17143 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017144 vim_free(lstval);
17145 }
17146 else
17147 {
17148 strval = get_tv_string_chk(&argvars[1]);
17149 if (strval == NULL)
17150 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017151 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017153 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017154 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155}
17156
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017157/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017158 * "settabvar()" function
17159 */
17160 static void
17161f_settabvar(argvars, rettv)
17162 typval_T *argvars;
17163 typval_T *rettv;
17164{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017165#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017166 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017167 tabpage_T *tp;
17168#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017169 char_u *varname, *tabvarname;
17170 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017171
17172 rettv->vval.v_number = 0;
17173
17174 if (check_restricted() || check_secure())
17175 return;
17176
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017177#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017178 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017179#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017180 varname = get_tv_string_chk(&argvars[1]);
17181 varp = &argvars[2];
17182
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017183 if (varname != NULL && varp != NULL
17184#ifdef FEAT_WINDOWS
17185 && tp != NULL
17186#endif
17187 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017188 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017189#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017190 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017191 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017192#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017193
17194 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17195 if (tabvarname != NULL)
17196 {
17197 STRCPY(tabvarname, "t:");
17198 STRCPY(tabvarname + 2, varname);
17199 set_var(tabvarname, varp, TRUE);
17200 vim_free(tabvarname);
17201 }
17202
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017203#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017204 /* Restore current tabpage */
17205 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017206 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017207#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017208 }
17209}
17210
17211/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017212 * "settabwinvar()" function
17213 */
17214 static void
17215f_settabwinvar(argvars, rettv)
17216 typval_T *argvars;
17217 typval_T *rettv;
17218{
17219 setwinvar(argvars, rettv, 1);
17220}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221
17222/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017223 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017226f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017227 typval_T *argvars;
17228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017229{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017230 setwinvar(argvars, rettv, 0);
17231}
17232
17233/*
17234 * "setwinvar()" and "settabwinvar()" functions
17235 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017236
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017237 static void
17238setwinvar(argvars, rettv, off)
17239 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017240 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017241 int off;
17242{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017243 win_T *win;
17244#ifdef FEAT_WINDOWS
17245 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017246 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017247#endif
17248 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017249 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017251 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017252
17253 if (check_restricted() || check_secure())
17254 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017255
17256#ifdef FEAT_WINDOWS
17257 if (off == 1)
17258 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17259 else
17260 tp = curtab;
17261#endif
17262 win = find_win_by_nr(&argvars[off], tp);
17263 varname = get_tv_string_chk(&argvars[off + 1]);
17264 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265
17266 if (win != NULL && varname != NULL && varp != NULL)
17267 {
17268#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017269 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017272 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017274 long numval;
17275 char_u *strval;
17276 int error = FALSE;
17277
17278 ++varname;
17279 numval = get_tv_number_chk(varp, &error);
17280 strval = get_tv_string_buf_chk(varp, nbuf);
17281 if (!error && strval != NULL)
17282 set_option_value(varname, numval, strval, OPT_LOCAL);
17283 }
17284 else
17285 {
17286 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17287 if (winvarname != NULL)
17288 {
17289 STRCPY(winvarname, "w:");
17290 STRCPY(winvarname + 2, varname);
17291 set_var(winvarname, varp, TRUE);
17292 vim_free(winvarname);
17293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017294 }
17295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017296#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017297 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298#endif
17299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300}
17301
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017302#ifdef FEAT_CRYPT
17303/*
17304 * "sha256({string})" function
17305 */
17306 static void
17307f_sha256(argvars, rettv)
17308 typval_T *argvars;
17309 typval_T *rettv;
17310{
17311 char_u *p;
17312
17313 p = get_tv_string(&argvars[0]);
17314 rettv->vval.v_string = vim_strsave(
17315 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17316 rettv->v_type = VAR_STRING;
17317}
17318#endif /* FEAT_CRYPT */
17319
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017321 * "shellescape({string})" function
17322 */
17323 static void
17324f_shellescape(argvars, rettv)
17325 typval_T *argvars;
17326 typval_T *rettv;
17327{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017328 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017329 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017330 rettv->v_type = VAR_STRING;
17331}
17332
17333/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017334 * shiftwidth() function
17335 */
17336 static void
17337f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017338 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017339 typval_T *rettv;
17340{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017341 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017342}
17343
17344/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017345 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 */
17347 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017348f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017349 typval_T *argvars;
17350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017352 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
Bram Moolenaar0d660222005-01-07 21:51:51 +000017354 p = get_tv_string(&argvars[0]);
17355 rettv->vval.v_string = vim_strsave(p);
17356 simplify_filename(rettv->vval.v_string); /* simplify in place */
17357 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358}
17359
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017360#ifdef FEAT_FLOAT
17361/*
17362 * "sin()" function
17363 */
17364 static void
17365f_sin(argvars, rettv)
17366 typval_T *argvars;
17367 typval_T *rettv;
17368{
17369 float_T f;
17370
17371 rettv->v_type = VAR_FLOAT;
17372 if (get_float_arg(argvars, &f) == OK)
17373 rettv->vval.v_float = sin(f);
17374 else
17375 rettv->vval.v_float = 0.0;
17376}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017377
17378/*
17379 * "sinh()" function
17380 */
17381 static void
17382f_sinh(argvars, rettv)
17383 typval_T *argvars;
17384 typval_T *rettv;
17385{
17386 float_T f;
17387
17388 rettv->v_type = VAR_FLOAT;
17389 if (get_float_arg(argvars, &f) == OK)
17390 rettv->vval.v_float = sinh(f);
17391 else
17392 rettv->vval.v_float = 0.0;
17393}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017394#endif
17395
Bram Moolenaar0d660222005-01-07 21:51:51 +000017396static int
17397#ifdef __BORLANDC__
17398 _RTLENTRYF
17399#endif
17400 item_compare __ARGS((const void *s1, const void *s2));
17401static int
17402#ifdef __BORLANDC__
17403 _RTLENTRYF
17404#endif
17405 item_compare2 __ARGS((const void *s1, const void *s2));
17406
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017407/* struct used in the array that's given to qsort() */
17408typedef struct
17409{
17410 listitem_T *item;
17411 int idx;
17412} sortItem_T;
17413
Bram Moolenaar0d660222005-01-07 21:51:51 +000017414static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017415static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017416static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017417static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017418static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017419static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017420static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017421#define ITEM_COMPARE_FAIL 999
17422
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017424 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017426 static int
17427#ifdef __BORLANDC__
17428_RTLENTRYF
17429#endif
17430item_compare(s1, s2)
17431 const void *s1;
17432 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017433{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017434 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017435 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017436 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017437 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017438 int res;
17439 char_u numbuf1[NUMBUFLEN];
17440 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017442 si1 = (sortItem_T *)s1;
17443 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017444 tv1 = &si1->item->li_tv;
17445 tv2 = &si2->item->li_tv;
17446 /* tv2string() puts quotes around a string and allocates memory. Don't do
17447 * that for string variables. Use a single quote when comparing with a
17448 * non-string to do what the docs promise. */
17449 if (tv1->v_type == VAR_STRING)
17450 {
17451 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17452 p1 = (char_u *)"'";
17453 else
17454 p1 = tv1->vval.v_string;
17455 }
17456 else
17457 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17458 if (tv2->v_type == VAR_STRING)
17459 {
17460 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17461 p2 = (char_u *)"'";
17462 else
17463 p2 = tv2->vval.v_string;
17464 }
17465 else
17466 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017467 if (p1 == NULL)
17468 p1 = (char_u *)"";
17469 if (p2 == NULL)
17470 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017471 if (!item_compare_numeric)
17472 {
17473 if (item_compare_ic)
17474 res = STRICMP(p1, p2);
17475 else
17476 res = STRCMP(p1, p2);
17477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017479 {
17480 double n1, n2;
17481 n1 = strtod((char *)p1, (char **)&p1);
17482 n2 = strtod((char *)p2, (char **)&p2);
17483 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17484 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017485
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017486 /* When the result would be zero, compare the item indexes. Makes the
17487 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017488 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017489 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017490
Bram Moolenaar0d660222005-01-07 21:51:51 +000017491 vim_free(tofree1);
17492 vim_free(tofree2);
17493 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494}
17495
17496 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017497#ifdef __BORLANDC__
17498_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017500item_compare2(s1, s2)
17501 const void *s1;
17502 const void *s2;
17503{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017504 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017505 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017506 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017507 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017508 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017510 /* shortcut after failure in previous call; compare all items equal */
17511 if (item_compare_func_err)
17512 return 0;
17513
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017514 si1 = (sortItem_T *)s1;
17515 si2 = (sortItem_T *)s2;
17516
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017517 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017518 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017519 copy_tv(&si1->item->li_tv, &argv[0]);
17520 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017521
17522 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017523 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017524 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17525 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526 clear_tv(&argv[0]);
17527 clear_tv(&argv[1]);
17528
17529 if (res == FAIL)
17530 res = ITEM_COMPARE_FAIL;
17531 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017532 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17533 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017534 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017535 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017536
17537 /* When the result would be zero, compare the pointers themselves. Makes
17538 * the sort stable. */
17539 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017540 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017541
Bram Moolenaar0d660222005-01-07 21:51:51 +000017542 return res;
17543}
17544
17545/*
17546 * "sort({list})" function
17547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017549do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017550 typval_T *argvars;
17551 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017552 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553{
Bram Moolenaar33570922005-01-25 22:26:29 +000017554 list_T *l;
17555 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017556 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017557 long len;
17558 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559
Bram Moolenaar0d660222005-01-07 21:51:51 +000017560 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017561 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562 else
17563 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017565 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017566 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 return;
17568 rettv->vval.v_list = l;
17569 rettv->v_type = VAR_LIST;
17570 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571
Bram Moolenaar0d660222005-01-07 21:51:51 +000017572 len = list_len(l);
17573 if (len <= 1)
17574 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575
Bram Moolenaar0d660222005-01-07 21:51:51 +000017576 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017577 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017578 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017579 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017580 if (argvars[1].v_type != VAR_UNKNOWN)
17581 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017582 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017583 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017584 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017585 else
17586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017587 int error = FALSE;
17588
17589 i = get_tv_number_chk(&argvars[1], &error);
17590 if (error)
17591 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017592 if (i == 1)
17593 item_compare_ic = TRUE;
17594 else
17595 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017596 if (item_compare_func != NULL)
17597 {
17598 if (STRCMP(item_compare_func, "n") == 0)
17599 {
17600 item_compare_func = NULL;
17601 item_compare_numeric = TRUE;
17602 }
17603 else if (STRCMP(item_compare_func, "i") == 0)
17604 {
17605 item_compare_func = NULL;
17606 item_compare_ic = TRUE;
17607 }
17608 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017609 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017610
17611 if (argvars[2].v_type != VAR_UNKNOWN)
17612 {
17613 /* optional third argument: {dict} */
17614 if (argvars[2].v_type != VAR_DICT)
17615 {
17616 EMSG(_(e_dictreq));
17617 return;
17618 }
17619 item_compare_selfdict = argvars[2].vval.v_dict;
17620 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622
Bram Moolenaar0d660222005-01-07 21:51:51 +000017623 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017624 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017625 if (ptrs == NULL)
17626 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017627
Bram Moolenaar327aa022014-03-25 18:24:23 +010017628 i = 0;
17629 if (sort)
17630 {
17631 /* sort(): ptrs will be the list to sort */
17632 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017633 {
17634 ptrs[i].item = li;
17635 ptrs[i].idx = i;
17636 ++i;
17637 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017638
17639 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017640 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017641 /* test the compare function */
17642 if (item_compare_func != NULL
17643 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017644 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017645 EMSG(_("E702: Sort compare function failed"));
17646 else
17647 {
17648 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017649 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017650 item_compare_func == NULL ? item_compare : item_compare2);
17651
17652 if (!item_compare_func_err)
17653 {
17654 /* Clear the List and append the items in sorted order. */
17655 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17656 l->lv_len = 0;
17657 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017658 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017659 }
17660 }
17661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017663 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017664 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17665
17666 /* f_uniq(): ptrs will be a stack of items to remove */
17667 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017668 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017669 item_compare_func_ptr = item_compare_func
17670 ? item_compare2 : item_compare;
17671
17672 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17673 li = li->li_next)
17674 {
17675 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17676 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017677 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017678 if (item_compare_func_err)
17679 {
17680 EMSG(_("E882: Uniq compare function failed"));
17681 break;
17682 }
17683 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017684
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017685 if (!item_compare_func_err)
17686 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017687 while (--i >= 0)
17688 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017689 li = ptrs[i].item->li_next;
17690 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017691 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017692 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017693 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017694 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017695 list_fix_watch(l, li);
17696 listitem_free(li);
17697 l->lv_len--;
17698 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017699 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017700 }
17701
17702 vim_free(ptrs);
17703 }
17704}
17705
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017706/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017707 * "sort({list})" function
17708 */
17709 static void
17710f_sort(argvars, rettv)
17711 typval_T *argvars;
17712 typval_T *rettv;
17713{
17714 do_sort_uniq(argvars, rettv, TRUE);
17715}
17716
17717/*
17718 * "uniq({list})" function
17719 */
17720 static void
17721f_uniq(argvars, rettv)
17722 typval_T *argvars;
17723 typval_T *rettv;
17724{
17725 do_sort_uniq(argvars, rettv, FALSE);
17726}
17727
17728/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017729 * "soundfold({word})" function
17730 */
17731 static void
17732f_soundfold(argvars, rettv)
17733 typval_T *argvars;
17734 typval_T *rettv;
17735{
17736 char_u *s;
17737
17738 rettv->v_type = VAR_STRING;
17739 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017740#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017741 rettv->vval.v_string = eval_soundfold(s);
17742#else
17743 rettv->vval.v_string = vim_strsave(s);
17744#endif
17745}
17746
17747/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017748 * "spellbadword()" function
17749 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017750 static void
17751f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017752 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017753 typval_T *rettv;
17754{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017755 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017756 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017757 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017758
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017759 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017760 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017761
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017762#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017763 if (argvars[0].v_type == VAR_UNKNOWN)
17764 {
17765 /* Find the start and length of the badly spelled word. */
17766 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17767 if (len != 0)
17768 word = ml_get_cursor();
17769 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017770 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017771 {
17772 char_u *str = get_tv_string_chk(&argvars[0]);
17773 int capcol = -1;
17774
17775 if (str != NULL)
17776 {
17777 /* Check the argument for spelling. */
17778 while (*str != NUL)
17779 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017780 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017781 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017782 {
17783 word = str;
17784 break;
17785 }
17786 str += len;
17787 }
17788 }
17789 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017790#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017791
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017792 list_append_string(rettv->vval.v_list, word, len);
17793 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017794 attr == HLF_SPB ? "bad" :
17795 attr == HLF_SPR ? "rare" :
17796 attr == HLF_SPL ? "local" :
17797 attr == HLF_SPC ? "caps" :
17798 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017799}
17800
17801/*
17802 * "spellsuggest()" function
17803 */
17804 static void
17805f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017806 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017807 typval_T *rettv;
17808{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017809#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017810 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017811 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017812 int maxcount;
17813 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017814 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017815 listitem_T *li;
17816 int need_capital = FALSE;
17817#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017818
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017819 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017820 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017821
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017822#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017823 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017824 {
17825 str = get_tv_string(&argvars[0]);
17826 if (argvars[1].v_type != VAR_UNKNOWN)
17827 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017828 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017829 if (maxcount <= 0)
17830 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017831 if (argvars[2].v_type != VAR_UNKNOWN)
17832 {
17833 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17834 if (typeerr)
17835 return;
17836 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017837 }
17838 else
17839 maxcount = 25;
17840
Bram Moolenaar4770d092006-01-12 23:22:24 +000017841 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017842
17843 for (i = 0; i < ga.ga_len; ++i)
17844 {
17845 str = ((char_u **)ga.ga_data)[i];
17846
17847 li = listitem_alloc();
17848 if (li == NULL)
17849 vim_free(str);
17850 else
17851 {
17852 li->li_tv.v_type = VAR_STRING;
17853 li->li_tv.v_lock = 0;
17854 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017855 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017856 }
17857 }
17858 ga_clear(&ga);
17859 }
17860#endif
17861}
17862
Bram Moolenaar0d660222005-01-07 21:51:51 +000017863 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017864f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017865 typval_T *argvars;
17866 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017867{
17868 char_u *str;
17869 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017870 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017871 regmatch_T regmatch;
17872 char_u patbuf[NUMBUFLEN];
17873 char_u *save_cpo;
17874 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017875 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017876 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017877 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017878
17879 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17880 save_cpo = p_cpo;
17881 p_cpo = (char_u *)"";
17882
17883 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017884 if (argvars[1].v_type != VAR_UNKNOWN)
17885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017886 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17887 if (pat == NULL)
17888 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017889 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017890 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017891 }
17892 if (pat == NULL || *pat == NUL)
17893 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017894
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017895 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017897 if (typeerr)
17898 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899
Bram Moolenaar0d660222005-01-07 21:51:51 +000017900 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17901 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017903 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017904 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017905 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017906 if (*str == NUL)
17907 match = FALSE; /* empty item at the end */
17908 else
17909 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017910 if (match)
17911 end = regmatch.startp[0];
17912 else
17913 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017914 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17915 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017916 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017917 if (list_append_string(rettv->vval.v_list, str,
17918 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017919 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017920 }
17921 if (!match)
17922 break;
17923 /* Advance to just after the match. */
17924 if (regmatch.endp[0] > str)
17925 col = 0;
17926 else
17927 {
17928 /* Don't get stuck at the same match. */
17929#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017930 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017931#else
17932 col = 1;
17933#endif
17934 }
17935 str = regmatch.endp[0];
17936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937
Bram Moolenaar473de612013-06-08 18:19:48 +020017938 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940
Bram Moolenaar0d660222005-01-07 21:51:51 +000017941 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942}
17943
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017944#ifdef FEAT_FLOAT
17945/*
17946 * "sqrt()" function
17947 */
17948 static void
17949f_sqrt(argvars, rettv)
17950 typval_T *argvars;
17951 typval_T *rettv;
17952{
17953 float_T f;
17954
17955 rettv->v_type = VAR_FLOAT;
17956 if (get_float_arg(argvars, &f) == OK)
17957 rettv->vval.v_float = sqrt(f);
17958 else
17959 rettv->vval.v_float = 0.0;
17960}
17961
17962/*
17963 * "str2float()" function
17964 */
17965 static void
17966f_str2float(argvars, rettv)
17967 typval_T *argvars;
17968 typval_T *rettv;
17969{
17970 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17971
17972 if (*p == '+')
17973 p = skipwhite(p + 1);
17974 (void)string2float(p, &rettv->vval.v_float);
17975 rettv->v_type = VAR_FLOAT;
17976}
17977#endif
17978
Bram Moolenaar2c932302006-03-18 21:42:09 +000017979/*
17980 * "str2nr()" function
17981 */
17982 static void
17983f_str2nr(argvars, rettv)
17984 typval_T *argvars;
17985 typval_T *rettv;
17986{
17987 int base = 10;
17988 char_u *p;
17989 long n;
17990
17991 if (argvars[1].v_type != VAR_UNKNOWN)
17992 {
17993 base = get_tv_number(&argvars[1]);
17994 if (base != 8 && base != 10 && base != 16)
17995 {
17996 EMSG(_(e_invarg));
17997 return;
17998 }
17999 }
18000
18001 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018002 if (*p == '+')
18003 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018004 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18005 rettv->vval.v_number = n;
18006}
18007
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008#ifdef HAVE_STRFTIME
18009/*
18010 * "strftime({format}[, {time}])" function
18011 */
18012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018013f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018014 typval_T *argvars;
18015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016{
18017 char_u result_buf[256];
18018 struct tm *curtime;
18019 time_t seconds;
18020 char_u *p;
18021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018022 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018024 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018025 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 seconds = time(NULL);
18027 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018028 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 curtime = localtime(&seconds);
18030 /* MSVC returns NULL for an invalid value of seconds. */
18031 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018032 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 else
18034 {
18035# ifdef FEAT_MBYTE
18036 vimconv_T conv;
18037 char_u *enc;
18038
18039 conv.vc_type = CONV_NONE;
18040 enc = enc_locale();
18041 convert_setup(&conv, p_enc, enc);
18042 if (conv.vc_type != CONV_NONE)
18043 p = string_convert(&conv, p, NULL);
18044# endif
18045 if (p != NULL)
18046 (void)strftime((char *)result_buf, sizeof(result_buf),
18047 (char *)p, curtime);
18048 else
18049 result_buf[0] = NUL;
18050
18051# ifdef FEAT_MBYTE
18052 if (conv.vc_type != CONV_NONE)
18053 vim_free(p);
18054 convert_setup(&conv, enc, p_enc);
18055 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018056 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057 else
18058# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060
18061# ifdef FEAT_MBYTE
18062 /* Release conversion descriptors */
18063 convert_setup(&conv, NULL, NULL);
18064 vim_free(enc);
18065# endif
18066 }
18067}
18068#endif
18069
18070/*
18071 * "stridx()" function
18072 */
18073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018074f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018075 typval_T *argvars;
18076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077{
18078 char_u buf[NUMBUFLEN];
18079 char_u *needle;
18080 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018081 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018083 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018085 needle = get_tv_string_chk(&argvars[1]);
18086 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018087 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018088 if (needle == NULL || haystack == NULL)
18089 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090
Bram Moolenaar33570922005-01-25 22:26:29 +000018091 if (argvars[2].v_type != VAR_UNKNOWN)
18092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018093 int error = FALSE;
18094
18095 start_idx = get_tv_number_chk(&argvars[2], &error);
18096 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018097 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018098 if (start_idx >= 0)
18099 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 }
18101
18102 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18103 if (pos != NULL)
18104 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105}
18106
18107/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018108 * "string()" function
18109 */
18110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018111f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018112 typval_T *argvars;
18113 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018114{
18115 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018116 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018118 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018119 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018120 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018121 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018122 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018123}
18124
18125/*
18126 * "strlen()" function
18127 */
18128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018129f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018130 typval_T *argvars;
18131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018133 rettv->vval.v_number = (varnumber_T)(STRLEN(
18134 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135}
18136
18137/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018138 * "strchars()" function
18139 */
18140 static void
18141f_strchars(argvars, rettv)
18142 typval_T *argvars;
18143 typval_T *rettv;
18144{
18145 char_u *s = get_tv_string(&argvars[0]);
18146#ifdef FEAT_MBYTE
18147 varnumber_T len = 0;
18148
18149 while (*s != NUL)
18150 {
18151 mb_cptr2char_adv(&s);
18152 ++len;
18153 }
18154 rettv->vval.v_number = len;
18155#else
18156 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18157#endif
18158}
18159
18160/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018161 * "strdisplaywidth()" function
18162 */
18163 static void
18164f_strdisplaywidth(argvars, rettv)
18165 typval_T *argvars;
18166 typval_T *rettv;
18167{
18168 char_u *s = get_tv_string(&argvars[0]);
18169 int col = 0;
18170
18171 if (argvars[1].v_type != VAR_UNKNOWN)
18172 col = get_tv_number(&argvars[1]);
18173
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018174 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018175}
18176
18177/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018178 * "strwidth()" function
18179 */
18180 static void
18181f_strwidth(argvars, rettv)
18182 typval_T *argvars;
18183 typval_T *rettv;
18184{
18185 char_u *s = get_tv_string(&argvars[0]);
18186
18187 rettv->vval.v_number = (varnumber_T)(
18188#ifdef FEAT_MBYTE
18189 mb_string2cells(s, -1)
18190#else
18191 STRLEN(s)
18192#endif
18193 );
18194}
18195
18196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197 * "strpart()" function
18198 */
18199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018201 typval_T *argvars;
18202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203{
18204 char_u *p;
18205 int n;
18206 int len;
18207 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018208 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018210 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 slen = (int)STRLEN(p);
18212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018213 n = get_tv_number_chk(&argvars[1], &error);
18214 if (error)
18215 len = 0;
18216 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018217 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 else
18219 len = slen - n; /* default len: all bytes that are available. */
18220
18221 /*
18222 * Only return the overlap between the specified part and the actual
18223 * string.
18224 */
18225 if (n < 0)
18226 {
18227 len += n;
18228 n = 0;
18229 }
18230 else if (n > slen)
18231 n = slen;
18232 if (len < 0)
18233 len = 0;
18234 else if (n + len > slen)
18235 len = slen - n;
18236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018237 rettv->v_type = VAR_STRING;
18238 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239}
18240
18241/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018242 * "strridx()" function
18243 */
18244 static void
18245f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018246 typval_T *argvars;
18247 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018248{
18249 char_u buf[NUMBUFLEN];
18250 char_u *needle;
18251 char_u *haystack;
18252 char_u *rest;
18253 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018254 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018256 needle = get_tv_string_chk(&argvars[1]);
18257 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018258
18259 rettv->vval.v_number = -1;
18260 if (needle == NULL || haystack == NULL)
18261 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018262
18263 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018264 if (argvars[2].v_type != VAR_UNKNOWN)
18265 {
18266 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018267 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018268 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018269 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018270 }
18271 else
18272 end_idx = haystack_len;
18273
Bram Moolenaar0d660222005-01-07 21:51:51 +000018274 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018275 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018276 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018277 lastmatch = haystack + end_idx;
18278 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018279 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018281 for (rest = haystack; *rest != '\0'; ++rest)
18282 {
18283 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018284 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018285 break;
18286 lastmatch = rest;
18287 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018288 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018289
18290 if (lastmatch == NULL)
18291 rettv->vval.v_number = -1;
18292 else
18293 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18294}
18295
18296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018297 * "strtrans()" function
18298 */
18299 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018300f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018301 typval_T *argvars;
18302 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018304 rettv->v_type = VAR_STRING;
18305 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306}
18307
18308/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018309 * "submatch()" function
18310 */
18311 static void
18312f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018313 typval_T *argvars;
18314 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018315{
Bram Moolenaar41571762014-04-02 19:00:58 +020018316 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018317 int no;
18318 int retList = 0;
18319
18320 no = (int)get_tv_number_chk(&argvars[0], &error);
18321 if (error)
18322 return;
18323 error = FALSE;
18324 if (argvars[1].v_type != VAR_UNKNOWN)
18325 retList = get_tv_number_chk(&argvars[1], &error);
18326 if (error)
18327 return;
18328
18329 if (retList == 0)
18330 {
18331 rettv->v_type = VAR_STRING;
18332 rettv->vval.v_string = reg_submatch(no);
18333 }
18334 else
18335 {
18336 rettv->v_type = VAR_LIST;
18337 rettv->vval.v_list = reg_submatch_list(no);
18338 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018339}
18340
18341/*
18342 * "substitute()" function
18343 */
18344 static void
18345f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018346 typval_T *argvars;
18347 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018348{
18349 char_u patbuf[NUMBUFLEN];
18350 char_u subbuf[NUMBUFLEN];
18351 char_u flagsbuf[NUMBUFLEN];
18352
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018353 char_u *str = get_tv_string_chk(&argvars[0]);
18354 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18355 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18356 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18357
Bram Moolenaar0d660222005-01-07 21:51:51 +000018358 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018359 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18360 rettv->vval.v_string = NULL;
18361 else
18362 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018363}
18364
18365/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018366 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018369f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018370 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372{
18373 int id = 0;
18374#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018375 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376 long col;
18377 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018378 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018380 lnum = get_tv_lnum(argvars); /* -1 on type error */
18381 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18382 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018384 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018385 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018386 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018387#endif
18388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018389 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390}
18391
18392/*
18393 * "synIDattr(id, what [, mode])" function
18394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018396f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399{
18400 char_u *p = NULL;
18401#ifdef FEAT_SYN_HL
18402 int id;
18403 char_u *what;
18404 char_u *mode;
18405 char_u modebuf[NUMBUFLEN];
18406 int modec;
18407
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018408 id = get_tv_number(&argvars[0]);
18409 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018410 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018412 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018414 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 modec = 0; /* replace invalid with current */
18416 }
18417 else
18418 {
18419#ifdef FEAT_GUI
18420 if (gui.in_use)
18421 modec = 'g';
18422 else
18423#endif
18424 if (t_colors > 1)
18425 modec = 'c';
18426 else
18427 modec = 't';
18428 }
18429
18430
18431 switch (TOLOWER_ASC(what[0]))
18432 {
18433 case 'b':
18434 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18435 p = highlight_color(id, what, modec);
18436 else /* bold */
18437 p = highlight_has_attr(id, HL_BOLD, modec);
18438 break;
18439
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018440 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441 p = highlight_color(id, what, modec);
18442 break;
18443
18444 case 'i':
18445 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18446 p = highlight_has_attr(id, HL_INVERSE, modec);
18447 else /* italic */
18448 p = highlight_has_attr(id, HL_ITALIC, modec);
18449 break;
18450
18451 case 'n': /* name */
18452 p = get_highlight_name(NULL, id - 1);
18453 break;
18454
18455 case 'r': /* reverse */
18456 p = highlight_has_attr(id, HL_INVERSE, modec);
18457 break;
18458
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018459 case 's':
18460 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18461 p = highlight_color(id, what, modec);
18462 else /* standout */
18463 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 break;
18465
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018466 case 'u':
18467 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18468 /* underline */
18469 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18470 else
18471 /* undercurl */
18472 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 break;
18474 }
18475
18476 if (p != NULL)
18477 p = vim_strsave(p);
18478#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018479 rettv->v_type = VAR_STRING;
18480 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481}
18482
18483/*
18484 * "synIDtrans(id)" function
18485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018487f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018488 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490{
18491 int id;
18492
18493#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018494 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495
18496 if (id > 0)
18497 id = syn_get_final_id(id);
18498 else
18499#endif
18500 id = 0;
18501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018502 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018503}
18504
18505/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018506 * "synconcealed(lnum, col)" function
18507 */
18508 static void
18509f_synconcealed(argvars, rettv)
18510 typval_T *argvars UNUSED;
18511 typval_T *rettv;
18512{
18513#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18514 long lnum;
18515 long col;
18516 int syntax_flags = 0;
18517 int cchar;
18518 int matchid = 0;
18519 char_u str[NUMBUFLEN];
18520#endif
18521
18522 rettv->v_type = VAR_LIST;
18523 rettv->vval.v_list = NULL;
18524
18525#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18526 lnum = get_tv_lnum(argvars); /* -1 on type error */
18527 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18528
18529 vim_memset(str, NUL, sizeof(str));
18530
18531 if (rettv_list_alloc(rettv) != FAIL)
18532 {
18533 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18534 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18535 && curwin->w_p_cole > 0)
18536 {
18537 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18538 syntax_flags = get_syntax_info(&matchid);
18539
18540 /* get the conceal character */
18541 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18542 {
18543 cchar = syn_get_sub_char();
18544 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18545 cchar = lcs_conceal;
18546 if (cchar != NUL)
18547 {
18548# ifdef FEAT_MBYTE
18549 if (has_mbyte)
18550 (*mb_char2bytes)(cchar, str);
18551 else
18552# endif
18553 str[0] = cchar;
18554 }
18555 }
18556 }
18557
18558 list_append_number(rettv->vval.v_list,
18559 (syntax_flags & HL_CONCEAL) != 0);
18560 /* -1 to auto-determine strlen */
18561 list_append_string(rettv->vval.v_list, str, -1);
18562 list_append_number(rettv->vval.v_list, matchid);
18563 }
18564#endif
18565}
18566
18567/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018568 * "synstack(lnum, col)" function
18569 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018570 static void
18571f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018572 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018573 typval_T *rettv;
18574{
18575#ifdef FEAT_SYN_HL
18576 long lnum;
18577 long col;
18578 int i;
18579 int id;
18580#endif
18581
18582 rettv->v_type = VAR_LIST;
18583 rettv->vval.v_list = NULL;
18584
18585#ifdef FEAT_SYN_HL
18586 lnum = get_tv_lnum(argvars); /* -1 on type error */
18587 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18588
18589 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018590 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018591 && rettv_list_alloc(rettv) != FAIL)
18592 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018593 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018594 for (i = 0; ; ++i)
18595 {
18596 id = syn_get_stack_item(i);
18597 if (id < 0)
18598 break;
18599 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18600 break;
18601 }
18602 }
18603#endif
18604}
18605
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018607get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018608 typval_T *argvars;
18609 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018610 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018612 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018614 char_u *infile = NULL;
18615 char_u buf[NUMBUFLEN];
18616 int err = FALSE;
18617 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018618 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018619 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018620
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018621 rettv->v_type = VAR_STRING;
18622 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018623 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018624 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018625
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018626 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018627 {
18628 /*
18629 * Write the string to a temp file, to be used for input of the shell
18630 * command.
18631 */
18632 if ((infile = vim_tempname('i')) == NULL)
18633 {
18634 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018635 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018636 }
18637
18638 fd = mch_fopen((char *)infile, WRITEBIN);
18639 if (fd == NULL)
18640 {
18641 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018642 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018643 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018644 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018645 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018646 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18647 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018648 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018649 else
18650 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018651 size_t len;
18652
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018653 p = get_tv_string_buf_chk(&argvars[1], buf);
18654 if (p == NULL)
18655 {
18656 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018657 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018658 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018659 len = STRLEN(p);
18660 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018661 err = TRUE;
18662 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018663 if (fclose(fd) != 0)
18664 err = TRUE;
18665 if (err)
18666 {
18667 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018668 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018669 }
18670 }
18671
Bram Moolenaar52a72462014-08-29 15:53:52 +020018672 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18673 * echoes typeahead, that messes up the display. */
18674 if (!msg_silent)
18675 flags += SHELL_COOKED;
18676
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018677 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018679 int len;
18680 listitem_T *li;
18681 char_u *s = NULL;
18682 char_u *start;
18683 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018684 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685
Bram Moolenaar52a72462014-08-29 15:53:52 +020018686 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018687 if (res == NULL)
18688 goto errret;
18689
18690 list = list_alloc();
18691 if (list == NULL)
18692 goto errret;
18693
18694 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018696 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018697 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018698 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018699 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018700
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018701 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018702 if (s == NULL)
18703 goto errret;
18704
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018705 for (p = s; start < end; ++p, ++start)
18706 *p = *start == NUL ? NL : *start;
18707 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018708
18709 li = listitem_alloc();
18710 if (li == NULL)
18711 {
18712 vim_free(s);
18713 goto errret;
18714 }
18715 li->li_tv.v_type = VAR_STRING;
18716 li->li_tv.vval.v_string = s;
18717 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018719
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018720 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018721 rettv->v_type = VAR_LIST;
18722 rettv->vval.v_list = list;
18723 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018725 else
18726 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018727 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018728#ifdef USE_CR
18729 /* translate <CR> into <NL> */
18730 if (res != NULL)
18731 {
18732 char_u *s;
18733
18734 for (s = res; *s; ++s)
18735 {
18736 if (*s == CAR)
18737 *s = NL;
18738 }
18739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740#else
18741# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018742 /* translate <CR><NL> into <NL> */
18743 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018745 char_u *s, *d;
18746
18747 d = res;
18748 for (s = res; *s; ++s)
18749 {
18750 if (s[0] == CAR && s[1] == NL)
18751 ++s;
18752 *d++ = *s;
18753 }
18754 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756# endif
18757#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018758 rettv->vval.v_string = res;
18759 res = NULL;
18760 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018761
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018762errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018763 if (infile != NULL)
18764 {
18765 mch_remove(infile);
18766 vim_free(infile);
18767 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018768 if (res != NULL)
18769 vim_free(res);
18770 if (list != NULL)
18771 list_free(list, TRUE);
18772}
18773
18774/*
18775 * "system()" function
18776 */
18777 static void
18778f_system(argvars, rettv)
18779 typval_T *argvars;
18780 typval_T *rettv;
18781{
18782 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18783}
18784
18785/*
18786 * "systemlist()" function
18787 */
18788 static void
18789f_systemlist(argvars, rettv)
18790 typval_T *argvars;
18791 typval_T *rettv;
18792{
18793 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794}
18795
18796/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018797 * "tabpagebuflist()" function
18798 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018799 static void
18800f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018801 typval_T *argvars UNUSED;
18802 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018803{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018804#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018805 tabpage_T *tp;
18806 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018807
18808 if (argvars[0].v_type == VAR_UNKNOWN)
18809 wp = firstwin;
18810 else
18811 {
18812 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18813 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018814 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018815 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018816 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018817 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018818 for (; wp != NULL; wp = wp->w_next)
18819 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018820 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018821 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018822 }
18823#endif
18824}
18825
18826
18827/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018828 * "tabpagenr()" function
18829 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018830 static void
18831f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018832 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018833 typval_T *rettv;
18834{
18835 int nr = 1;
18836#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018837 char_u *arg;
18838
18839 if (argvars[0].v_type != VAR_UNKNOWN)
18840 {
18841 arg = get_tv_string_chk(&argvars[0]);
18842 nr = 0;
18843 if (arg != NULL)
18844 {
18845 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018846 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018847 else
18848 EMSG2(_(e_invexpr2), arg);
18849 }
18850 }
18851 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018852 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018853#endif
18854 rettv->vval.v_number = nr;
18855}
18856
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018857
18858#ifdef FEAT_WINDOWS
18859static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18860
18861/*
18862 * Common code for tabpagewinnr() and winnr().
18863 */
18864 static int
18865get_winnr(tp, argvar)
18866 tabpage_T *tp;
18867 typval_T *argvar;
18868{
18869 win_T *twin;
18870 int nr = 1;
18871 win_T *wp;
18872 char_u *arg;
18873
18874 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18875 if (argvar->v_type != VAR_UNKNOWN)
18876 {
18877 arg = get_tv_string_chk(argvar);
18878 if (arg == NULL)
18879 nr = 0; /* type error; errmsg already given */
18880 else if (STRCMP(arg, "$") == 0)
18881 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18882 else if (STRCMP(arg, "#") == 0)
18883 {
18884 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18885 if (twin == NULL)
18886 nr = 0;
18887 }
18888 else
18889 {
18890 EMSG2(_(e_invexpr2), arg);
18891 nr = 0;
18892 }
18893 }
18894
18895 if (nr > 0)
18896 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18897 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018898 {
18899 if (wp == NULL)
18900 {
18901 /* didn't find it in this tabpage */
18902 nr = 0;
18903 break;
18904 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018905 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018906 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018907 return nr;
18908}
18909#endif
18910
18911/*
18912 * "tabpagewinnr()" function
18913 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018914 static void
18915f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018916 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018917 typval_T *rettv;
18918{
18919 int nr = 1;
18920#ifdef FEAT_WINDOWS
18921 tabpage_T *tp;
18922
18923 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18924 if (tp == NULL)
18925 nr = 0;
18926 else
18927 nr = get_winnr(tp, &argvars[1]);
18928#endif
18929 rettv->vval.v_number = nr;
18930}
18931
18932
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018933/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018934 * "tagfiles()" function
18935 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018936 static void
18937f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018938 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018939 typval_T *rettv;
18940{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018941 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018942 tagname_T tn;
18943 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018944
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018945 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018946 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018947 fname = alloc(MAXPATHL);
18948 if (fname == NULL)
18949 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018950
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018951 for (first = TRUE; ; first = FALSE)
18952 if (get_tagfname(&tn, first, fname) == FAIL
18953 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018954 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018955 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018956 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018957}
18958
18959/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018960 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018961 */
18962 static void
18963f_taglist(argvars, rettv)
18964 typval_T *argvars;
18965 typval_T *rettv;
18966{
18967 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018968
18969 tag_pattern = get_tv_string(&argvars[0]);
18970
18971 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018972 if (*tag_pattern == NUL)
18973 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018974
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018975 if (rettv_list_alloc(rettv) == OK)
18976 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018977}
18978
18979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980 * "tempname()" function
18981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018983f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018984 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986{
18987 static int x = 'A';
18988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018989 rettv->v_type = VAR_STRING;
18990 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991
18992 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18993 * names. Skip 'I' and 'O', they are used for shell redirection. */
18994 do
18995 {
18996 if (x == 'Z')
18997 x = '0';
18998 else if (x == '9')
18999 x = 'A';
19000 else
19001 {
19002#ifdef EBCDIC
19003 if (x == 'I')
19004 x = 'J';
19005 else if (x == 'R')
19006 x = 'S';
19007 else
19008#endif
19009 ++x;
19010 }
19011 } while (x == 'I' || x == 'O');
19012}
19013
19014/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019015 * "test(list)" function: Just checking the walls...
19016 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019017 static void
19018f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019019 typval_T *argvars UNUSED;
19020 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019021{
19022 /* Used for unit testing. Change the code below to your liking. */
19023#if 0
19024 listitem_T *li;
19025 list_T *l;
19026 char_u *bad, *good;
19027
19028 if (argvars[0].v_type != VAR_LIST)
19029 return;
19030 l = argvars[0].vval.v_list;
19031 if (l == NULL)
19032 return;
19033 li = l->lv_first;
19034 if (li == NULL)
19035 return;
19036 bad = get_tv_string(&li->li_tv);
19037 li = li->li_next;
19038 if (li == NULL)
19039 return;
19040 good = get_tv_string(&li->li_tv);
19041 rettv->vval.v_number = test_edit_score(bad, good);
19042#endif
19043}
19044
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019045#ifdef FEAT_FLOAT
19046/*
19047 * "tan()" function
19048 */
19049 static void
19050f_tan(argvars, rettv)
19051 typval_T *argvars;
19052 typval_T *rettv;
19053{
19054 float_T f;
19055
19056 rettv->v_type = VAR_FLOAT;
19057 if (get_float_arg(argvars, &f) == OK)
19058 rettv->vval.v_float = tan(f);
19059 else
19060 rettv->vval.v_float = 0.0;
19061}
19062
19063/*
19064 * "tanh()" function
19065 */
19066 static void
19067f_tanh(argvars, rettv)
19068 typval_T *argvars;
19069 typval_T *rettv;
19070{
19071 float_T f;
19072
19073 rettv->v_type = VAR_FLOAT;
19074 if (get_float_arg(argvars, &f) == OK)
19075 rettv->vval.v_float = tanh(f);
19076 else
19077 rettv->vval.v_float = 0.0;
19078}
19079#endif
19080
Bram Moolenaard52d9742005-08-21 22:20:28 +000019081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 * "tolower(string)" function
19083 */
19084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019085f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019086 typval_T *argvars;
19087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088{
19089 char_u *p;
19090
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019091 p = vim_strsave(get_tv_string(&argvars[0]));
19092 rettv->v_type = VAR_STRING;
19093 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094
19095 if (p != NULL)
19096 while (*p != NUL)
19097 {
19098#ifdef FEAT_MBYTE
19099 int l;
19100
19101 if (enc_utf8)
19102 {
19103 int c, lc;
19104
19105 c = utf_ptr2char(p);
19106 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019107 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019108 /* TODO: reallocate string when byte count changes. */
19109 if (utf_char2len(lc) == l)
19110 utf_char2bytes(lc, p);
19111 p += l;
19112 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019113 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019114 p += l; /* skip multi-byte character */
19115 else
19116#endif
19117 {
19118 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19119 ++p;
19120 }
19121 }
19122}
19123
19124/*
19125 * "toupper(string)" function
19126 */
19127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019128f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019129 typval_T *argvars;
19130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019132 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019133 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134}
19135
19136/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019137 * "tr(string, fromstr, tostr)" function
19138 */
19139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019140f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019141 typval_T *argvars;
19142 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019143{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019144 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019145 char_u *fromstr;
19146 char_u *tostr;
19147 char_u *p;
19148#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019149 int inlen;
19150 int fromlen;
19151 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019152 int idx;
19153 char_u *cpstr;
19154 int cplen;
19155 int first = TRUE;
19156#endif
19157 char_u buf[NUMBUFLEN];
19158 char_u buf2[NUMBUFLEN];
19159 garray_T ga;
19160
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019161 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019162 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19163 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019164
19165 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019166 rettv->v_type = VAR_STRING;
19167 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019168 if (fromstr == NULL || tostr == NULL)
19169 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019170 ga_init2(&ga, (int)sizeof(char), 80);
19171
19172#ifdef FEAT_MBYTE
19173 if (!has_mbyte)
19174#endif
19175 /* not multi-byte: fromstr and tostr must be the same length */
19176 if (STRLEN(fromstr) != STRLEN(tostr))
19177 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019178#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019179error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019180#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019181 EMSG2(_(e_invarg2), fromstr);
19182 ga_clear(&ga);
19183 return;
19184 }
19185
19186 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019187 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019188 {
19189#ifdef FEAT_MBYTE
19190 if (has_mbyte)
19191 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019192 inlen = (*mb_ptr2len)(in_str);
19193 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019194 cplen = inlen;
19195 idx = 0;
19196 for (p = fromstr; *p != NUL; p += fromlen)
19197 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019198 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019199 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019200 {
19201 for (p = tostr; *p != NUL; p += tolen)
19202 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019203 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019204 if (idx-- == 0)
19205 {
19206 cplen = tolen;
19207 cpstr = p;
19208 break;
19209 }
19210 }
19211 if (*p == NUL) /* tostr is shorter than fromstr */
19212 goto error;
19213 break;
19214 }
19215 ++idx;
19216 }
19217
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019218 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019219 {
19220 /* Check that fromstr and tostr have the same number of
19221 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019222 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019223 first = FALSE;
19224 for (p = tostr; *p != NUL; p += tolen)
19225 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019226 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019227 --idx;
19228 }
19229 if (idx != 0)
19230 goto error;
19231 }
19232
19233 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019234 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019235 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019236
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019237 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019238 }
19239 else
19240#endif
19241 {
19242 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019243 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019244 if (p != NULL)
19245 ga_append(&ga, tostr[p - fromstr]);
19246 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019247 ga_append(&ga, *in_str);
19248 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019249 }
19250 }
19251
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019252 /* add a terminating NUL */
19253 ga_grow(&ga, 1);
19254 ga_append(&ga, NUL);
19255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019256 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019257}
19258
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019259#ifdef FEAT_FLOAT
19260/*
19261 * "trunc({float})" function
19262 */
19263 static void
19264f_trunc(argvars, rettv)
19265 typval_T *argvars;
19266 typval_T *rettv;
19267{
19268 float_T f;
19269
19270 rettv->v_type = VAR_FLOAT;
19271 if (get_float_arg(argvars, &f) == OK)
19272 /* trunc() is not in C90, use floor() or ceil() instead. */
19273 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19274 else
19275 rettv->vval.v_float = 0.0;
19276}
19277#endif
19278
Bram Moolenaar8299df92004-07-10 09:47:34 +000019279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019280 * "type(expr)" function
19281 */
19282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019283f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019284 typval_T *argvars;
19285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019287 int n;
19288
19289 switch (argvars[0].v_type)
19290 {
19291 case VAR_NUMBER: n = 0; break;
19292 case VAR_STRING: n = 1; break;
19293 case VAR_FUNC: n = 2; break;
19294 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019295 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019296#ifdef FEAT_FLOAT
19297 case VAR_FLOAT: n = 5; break;
19298#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019299 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19300 }
19301 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302}
19303
19304/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019305 * "undofile(name)" function
19306 */
19307 static void
19308f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019309 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019310 typval_T *rettv;
19311{
19312 rettv->v_type = VAR_STRING;
19313#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019314 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019315 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019316
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019317 if (*fname == NUL)
19318 {
19319 /* If there is no file name there will be no undo file. */
19320 rettv->vval.v_string = NULL;
19321 }
19322 else
19323 {
19324 char_u *ffname = FullName_save(fname, FALSE);
19325
19326 if (ffname != NULL)
19327 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19328 vim_free(ffname);
19329 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019330 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019331#else
19332 rettv->vval.v_string = NULL;
19333#endif
19334}
19335
19336/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019337 * "undotree()" function
19338 */
19339 static void
19340f_undotree(argvars, rettv)
19341 typval_T *argvars UNUSED;
19342 typval_T *rettv;
19343{
19344 if (rettv_dict_alloc(rettv) == OK)
19345 {
19346 dict_T *dict = rettv->vval.v_dict;
19347 list_T *list;
19348
Bram Moolenaar730cde92010-06-27 05:18:54 +020019349 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019350 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019351 dict_add_nr_str(dict, "save_last",
19352 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019353 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19354 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019355 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019356
19357 list = list_alloc();
19358 if (list != NULL)
19359 {
19360 u_eval_tree(curbuf->b_u_oldhead, list);
19361 dict_add_list(dict, "entries", list);
19362 }
19363 }
19364}
19365
19366/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019367 * "values(dict)" function
19368 */
19369 static void
19370f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019371 typval_T *argvars;
19372 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019373{
19374 dict_list(argvars, rettv, 1);
19375}
19376
19377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019378 * "virtcol(string)" function
19379 */
19380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019381f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019382 typval_T *argvars;
19383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384{
19385 colnr_T vcol = 0;
19386 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019387 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019389 fp = var2fpos(&argvars[0], FALSE, &fnum);
19390 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19391 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392 {
19393 getvvcol(curwin, fp, NULL, NULL, &vcol);
19394 ++vcol;
19395 }
19396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019397 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398}
19399
19400/*
19401 * "visualmode()" function
19402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019404f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019405 typval_T *argvars UNUSED;
19406 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408 char_u str[2];
19409
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019410 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 str[0] = curbuf->b_visual_mode_eval;
19412 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019413 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414
19415 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019416 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418}
19419
19420/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019421 * "wildmenumode()" function
19422 */
19423 static void
19424f_wildmenumode(argvars, rettv)
19425 typval_T *argvars UNUSED;
19426 typval_T *rettv UNUSED;
19427{
19428#ifdef FEAT_WILDMENU
19429 if (wild_menu_showing)
19430 rettv->vval.v_number = 1;
19431#endif
19432}
19433
19434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 * "winbufnr(nr)" function
19436 */
19437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019438f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019439 typval_T *argvars;
19440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441{
19442 win_T *wp;
19443
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019444 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019446 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019448 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449}
19450
19451/*
19452 * "wincol()" function
19453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019455f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019456 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458{
19459 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019460 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461}
19462
19463/*
19464 * "winheight(nr)" function
19465 */
19466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019467f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019468 typval_T *argvars;
19469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470{
19471 win_T *wp;
19472
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019473 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019475 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019477 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478}
19479
19480/*
19481 * "winline()" function
19482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019484f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019485 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019486 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487{
19488 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019489 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490}
19491
19492/*
19493 * "winnr()" function
19494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019495 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019496f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019497 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019498 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499{
19500 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019501
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019503 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019505 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506}
19507
19508/*
19509 * "winrestcmd()" function
19510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019512f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019513 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515{
19516#ifdef FEAT_WINDOWS
19517 win_T *wp;
19518 int winnr = 1;
19519 garray_T ga;
19520 char_u buf[50];
19521
19522 ga_init2(&ga, (int)sizeof(char), 70);
19523 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19524 {
19525 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19526 ga_concat(&ga, buf);
19527# ifdef FEAT_VERTSPLIT
19528 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19529 ga_concat(&ga, buf);
19530# endif
19531 ++winnr;
19532 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019533 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019535 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540}
19541
19542/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019543 * "winrestview()" function
19544 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019545 static void
19546f_winrestview(argvars, rettv)
19547 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019548 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019549{
19550 dict_T *dict;
19551
19552 if (argvars[0].v_type != VAR_DICT
19553 || (dict = argvars[0].vval.v_dict) == NULL)
19554 EMSG(_(e_invarg));
19555 else
19556 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019557 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19558 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19559 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19560 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019561#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019562 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19563 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019564#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019565 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19566 {
19567 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19568 curwin->w_set_curswant = FALSE;
19569 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019570
Bram Moolenaar82c25852014-05-28 16:47:16 +020019571 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19572 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019573#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019574 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19575 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019576#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019577 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19578 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19579 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19580 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019581
19582 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019583 win_new_height(curwin, curwin->w_height);
19584# ifdef FEAT_VERTSPLIT
19585 win_new_width(curwin, W_WIDTH(curwin));
19586# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019587 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019588
Bram Moolenaarb851a962014-10-31 15:45:52 +010019589 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019590 curwin->w_topline = 1;
19591 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19592 curwin->w_topline = curbuf->b_ml.ml_line_count;
19593#ifdef FEAT_DIFF
19594 check_topfill(curwin, TRUE);
19595#endif
19596 }
19597}
19598
19599/*
19600 * "winsaveview()" function
19601 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019602 static void
19603f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019604 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019605 typval_T *rettv;
19606{
19607 dict_T *dict;
19608
Bram Moolenaara800b422010-06-27 01:15:55 +020019609 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019610 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019611 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019612
19613 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19614 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19615#ifdef FEAT_VIRTUALEDIT
19616 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19617#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019618 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019619 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19620
19621 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19622#ifdef FEAT_DIFF
19623 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19624#endif
19625 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19626 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19627}
19628
19629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 * "winwidth(nr)" function
19631 */
19632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019633f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019634 typval_T *argvars;
19635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636{
19637 win_T *wp;
19638
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019639 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019641 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 else
19643#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019644 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019646 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647#endif
19648}
19649
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019651 * Write list of strings to file
19652 */
19653 static int
19654write_list(fd, list, binary)
19655 FILE *fd;
19656 list_T *list;
19657 int binary;
19658{
19659 listitem_T *li;
19660 int c;
19661 int ret = OK;
19662 char_u *s;
19663
19664 for (li = list->lv_first; li != NULL; li = li->li_next)
19665 {
19666 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19667 {
19668 if (*s == '\n')
19669 c = putc(NUL, fd);
19670 else
19671 c = putc(*s, fd);
19672 if (c == EOF)
19673 {
19674 ret = FAIL;
19675 break;
19676 }
19677 }
19678 if (!binary || li->li_next != NULL)
19679 if (putc('\n', fd) == EOF)
19680 {
19681 ret = FAIL;
19682 break;
19683 }
19684 if (ret == FAIL)
19685 {
19686 EMSG(_(e_write));
19687 break;
19688 }
19689 }
19690 return ret;
19691}
19692
19693/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019694 * "writefile()" function
19695 */
19696 static void
19697f_writefile(argvars, rettv)
19698 typval_T *argvars;
19699 typval_T *rettv;
19700{
19701 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019702 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019703 char_u *fname;
19704 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019705 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019706
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019707 if (check_restricted() || check_secure())
19708 return;
19709
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019710 if (argvars[0].v_type != VAR_LIST)
19711 {
19712 EMSG2(_(e_listarg), "writefile()");
19713 return;
19714 }
19715 if (argvars[0].vval.v_list == NULL)
19716 return;
19717
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019718 if (argvars[2].v_type != VAR_UNKNOWN)
19719 {
19720 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19721 binary = TRUE;
19722 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19723 append = TRUE;
19724 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019725
19726 /* Always open the file in binary mode, library functions have a mind of
19727 * their own about CR-LF conversion. */
19728 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019729 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19730 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019731 {
19732 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19733 ret = -1;
19734 }
19735 else
19736 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019737 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19738 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019739 fclose(fd);
19740 }
19741
19742 rettv->vval.v_number = ret;
19743}
19744
19745/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019746 * "xor(expr, expr)" function
19747 */
19748 static void
19749f_xor(argvars, rettv)
19750 typval_T *argvars;
19751 typval_T *rettv;
19752{
19753 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19754 ^ get_tv_number_chk(&argvars[1], NULL);
19755}
19756
19757
19758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019760 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 */
19762 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019763var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019764 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019765 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019766 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019768 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019770 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771
Bram Moolenaara5525202006-03-02 22:52:09 +000019772 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019773 if (varp->v_type == VAR_LIST)
19774 {
19775 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019776 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019777 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019778 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019779
19780 l = varp->vval.v_list;
19781 if (l == NULL)
19782 return NULL;
19783
19784 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019785 pos.lnum = list_find_nr(l, 0L, &error);
19786 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019787 return NULL; /* invalid line number */
19788
19789 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019790 pos.col = list_find_nr(l, 1L, &error);
19791 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019792 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019793 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019794
19795 /* We accept "$" for the column number: last column. */
19796 li = list_find(l, 1L);
19797 if (li != NULL && li->li_tv.v_type == VAR_STRING
19798 && li->li_tv.vval.v_string != NULL
19799 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19800 pos.col = len + 1;
19801
Bram Moolenaara5525202006-03-02 22:52:09 +000019802 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019803 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019804 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019805 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019806
Bram Moolenaara5525202006-03-02 22:52:09 +000019807#ifdef FEAT_VIRTUALEDIT
19808 /* Get the virtual offset. Defaults to zero. */
19809 pos.coladd = list_find_nr(l, 2L, &error);
19810 if (error)
19811 pos.coladd = 0;
19812#endif
19813
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019814 return &pos;
19815 }
19816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019817 name = get_tv_string_chk(varp);
19818 if (name == NULL)
19819 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019820 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019822 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19823 {
19824 if (VIsual_active)
19825 return &VIsual;
19826 return &curwin->w_cursor;
19827 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019828 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019829 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019830 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019831 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19832 return NULL;
19833 return pp;
19834 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019835
19836#ifdef FEAT_VIRTUALEDIT
19837 pos.coladd = 0;
19838#endif
19839
Bram Moolenaar477933c2007-07-17 14:32:23 +000019840 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019841 {
19842 pos.col = 0;
19843 if (name[1] == '0') /* "w0": first visible line */
19844 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019845 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019846 pos.lnum = curwin->w_topline;
19847 return &pos;
19848 }
19849 else if (name[1] == '$') /* "w$": last visible line */
19850 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019851 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019852 pos.lnum = curwin->w_botline - 1;
19853 return &pos;
19854 }
19855 }
19856 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019858 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 {
19860 pos.lnum = curbuf->b_ml.ml_line_count;
19861 pos.col = 0;
19862 }
19863 else
19864 {
19865 pos.lnum = curwin->w_cursor.lnum;
19866 pos.col = (colnr_T)STRLEN(ml_get_curline());
19867 }
19868 return &pos;
19869 }
19870 return NULL;
19871}
19872
19873/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019874 * Convert list in "arg" into a position and optional file number.
19875 * When "fnump" is NULL there is no file number, only 3 items.
19876 * Note that the column is passed on as-is, the caller may want to decrement
19877 * it to use 1 for the first column.
19878 * Return FAIL when conversion is not possible, doesn't check the position for
19879 * validity.
19880 */
19881 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019882list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019883 typval_T *arg;
19884 pos_T *posp;
19885 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019886 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019887{
19888 list_T *l = arg->vval.v_list;
19889 long i = 0;
19890 long n;
19891
Bram Moolenaar493c1782014-05-28 14:34:46 +020019892 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
19893 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000019894 if (arg->v_type != VAR_LIST
19895 || l == NULL
19896 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020019897 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019898 return FAIL;
19899
19900 if (fnump != NULL)
19901 {
19902 n = list_find_nr(l, i++, NULL); /* fnum */
19903 if (n < 0)
19904 return FAIL;
19905 if (n == 0)
19906 n = curbuf->b_fnum; /* current buffer */
19907 *fnump = n;
19908 }
19909
19910 n = list_find_nr(l, i++, NULL); /* lnum */
19911 if (n < 0)
19912 return FAIL;
19913 posp->lnum = n;
19914
19915 n = list_find_nr(l, i++, NULL); /* col */
19916 if (n < 0)
19917 return FAIL;
19918 posp->col = n;
19919
19920#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020019921 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019922 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019923 posp->coladd = 0;
19924 else
19925 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019926#endif
19927
Bram Moolenaar493c1782014-05-28 14:34:46 +020019928 if (curswantp != NULL)
19929 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
19930
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019931 return OK;
19932}
19933
19934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 * Get the length of an environment variable name.
19936 * Advance "arg" to the first character after the name.
19937 * Return 0 for error.
19938 */
19939 static int
19940get_env_len(arg)
19941 char_u **arg;
19942{
19943 char_u *p;
19944 int len;
19945
19946 for (p = *arg; vim_isIDc(*p); ++p)
19947 ;
19948 if (p == *arg) /* no name found */
19949 return 0;
19950
19951 len = (int)(p - *arg);
19952 *arg = p;
19953 return len;
19954}
19955
19956/*
19957 * Get the length of the name of a function or internal variable.
19958 * "arg" is advanced to the first non-white character after the name.
19959 * Return 0 if something is wrong.
19960 */
19961 static int
19962get_id_len(arg)
19963 char_u **arg;
19964{
19965 char_u *p;
19966 int len;
19967
19968 /* Find the end of the name. */
19969 for (p = *arg; eval_isnamec(*p); ++p)
19970 ;
19971 if (p == *arg) /* no name found */
19972 return 0;
19973
19974 len = (int)(p - *arg);
19975 *arg = skipwhite(p);
19976
19977 return len;
19978}
19979
19980/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019981 * Get the length of the name of a variable or function.
19982 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019984 * Return -1 if curly braces expansion failed.
19985 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 * If the name contains 'magic' {}'s, expand them and return the
19987 * expanded name in an allocated string via 'alias' - caller must free.
19988 */
19989 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019990get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991 char_u **arg;
19992 char_u **alias;
19993 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019994 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995{
19996 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997 char_u *p;
19998 char_u *expr_start;
19999 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000
20001 *alias = NULL; /* default to no alias */
20002
20003 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20004 && (*arg)[2] == (int)KE_SNR)
20005 {
20006 /* hard coded <SNR>, already translated */
20007 *arg += 3;
20008 return get_id_len(arg) + 3;
20009 }
20010 len = eval_fname_script(*arg);
20011 if (len > 0)
20012 {
20013 /* literal "<SID>", "s:" or "<SNR>" */
20014 *arg += len;
20015 }
20016
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020020 p = find_name_end(*arg, &expr_start, &expr_end,
20021 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 if (expr_start != NULL)
20023 {
20024 char_u *temp_string;
20025
20026 if (!evaluate)
20027 {
20028 len += (int)(p - *arg);
20029 *arg = skipwhite(p);
20030 return len;
20031 }
20032
20033 /*
20034 * Include any <SID> etc in the expanded string:
20035 * Thus the -len here.
20036 */
20037 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20038 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020039 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 *alias = temp_string;
20041 *arg = skipwhite(p);
20042 return (int)STRLEN(temp_string);
20043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044
20045 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020046 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 EMSG2(_(e_invexpr2), *arg);
20048
20049 return len;
20050}
20051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020052/*
20053 * Find the end of a variable or function name, taking care of magic braces.
20054 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20055 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020056 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020057 * Return a pointer to just after the name. Equal to "arg" if there is no
20058 * valid name.
20059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020061find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 char_u *arg;
20063 char_u **expr_start;
20064 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020065 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020067 int mb_nest = 0;
20068 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 char_u *p;
20070
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020071 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020073 *expr_start = NULL;
20074 *expr_end = NULL;
20075 }
20076
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020077 /* Quick check for valid starting character. */
20078 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20079 return arg;
20080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020081 for (p = arg; *p != NUL
20082 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020083 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020084 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020086 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020087 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020088 if (*p == '\'')
20089 {
20090 /* skip over 'string' to avoid counting [ and ] inside it. */
20091 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20092 ;
20093 if (*p == NUL)
20094 break;
20095 }
20096 else if (*p == '"')
20097 {
20098 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20099 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20100 if (*p == '\\' && p[1] != NUL)
20101 ++p;
20102 if (*p == NUL)
20103 break;
20104 }
20105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020108 if (*p == '[')
20109 ++br_nest;
20110 else if (*p == ']')
20111 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020114 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020116 if (*p == '{')
20117 {
20118 mb_nest++;
20119 if (expr_start != NULL && *expr_start == NULL)
20120 *expr_start = p;
20121 }
20122 else if (*p == '}')
20123 {
20124 mb_nest--;
20125 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20126 *expr_end = p;
20127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 }
20130
20131 return p;
20132}
20133
20134/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020135 * Expands out the 'magic' {}'s in a variable/function name.
20136 * Note that this can call itself recursively, to deal with
20137 * constructs like foo{bar}{baz}{bam}
20138 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20139 * "in_start" ^
20140 * "expr_start" ^
20141 * "expr_end" ^
20142 * "in_end" ^
20143 *
20144 * Returns a new allocated string, which the caller must free.
20145 * Returns NULL for failure.
20146 */
20147 static char_u *
20148make_expanded_name(in_start, expr_start, expr_end, in_end)
20149 char_u *in_start;
20150 char_u *expr_start;
20151 char_u *expr_end;
20152 char_u *in_end;
20153{
20154 char_u c1;
20155 char_u *retval = NULL;
20156 char_u *temp_result;
20157 char_u *nextcmd = NULL;
20158
20159 if (expr_end == NULL || in_end == NULL)
20160 return NULL;
20161 *expr_start = NUL;
20162 *expr_end = NUL;
20163 c1 = *in_end;
20164 *in_end = NUL;
20165
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020166 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020167 if (temp_result != NULL && nextcmd == NULL)
20168 {
20169 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20170 + (in_end - expr_end) + 1));
20171 if (retval != NULL)
20172 {
20173 STRCPY(retval, in_start);
20174 STRCAT(retval, temp_result);
20175 STRCAT(retval, expr_end + 1);
20176 }
20177 }
20178 vim_free(temp_result);
20179
20180 *in_end = c1; /* put char back for error messages */
20181 *expr_start = '{';
20182 *expr_end = '}';
20183
20184 if (retval != NULL)
20185 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020186 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020187 if (expr_start != NULL)
20188 {
20189 /* Further expansion! */
20190 temp_result = make_expanded_name(retval, expr_start,
20191 expr_end, temp_result);
20192 vim_free(retval);
20193 retval = temp_result;
20194 }
20195 }
20196
20197 return retval;
20198}
20199
20200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020202 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 */
20204 static int
20205eval_isnamec(c)
20206 int c;
20207{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020208 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20209}
20210
20211/*
20212 * Return TRUE if character "c" can be used as the first character in a
20213 * variable or function name (excluding '{' and '}').
20214 */
20215 static int
20216eval_isnamec1(c)
20217 int c;
20218{
20219 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220}
20221
20222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 * Set number v: variable to "val".
20224 */
20225 void
20226set_vim_var_nr(idx, val)
20227 int idx;
20228 long val;
20229{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020230 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020231}
20232
20233/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020234 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 */
20236 long
20237get_vim_var_nr(idx)
20238 int idx;
20239{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020240 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241}
20242
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020243/*
20244 * Get string v: variable value. Uses a static buffer, can only be used once.
20245 */
20246 char_u *
20247get_vim_var_str(idx)
20248 int idx;
20249{
20250 return get_tv_string(&vimvars[idx].vv_tv);
20251}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020252
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020254 * Get List v: variable value. Caller must take care of reference count when
20255 * needed.
20256 */
20257 list_T *
20258get_vim_var_list(idx)
20259 int idx;
20260{
20261 return vimvars[idx].vv_list;
20262}
20263
20264/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020265 * Set v:char to character "c".
20266 */
20267 void
20268set_vim_var_char(c)
20269 int c;
20270{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020271 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020272
20273#ifdef FEAT_MBYTE
20274 if (has_mbyte)
20275 buf[(*mb_char2bytes)(c, buf)] = NUL;
20276 else
20277#endif
20278 {
20279 buf[0] = c;
20280 buf[1] = NUL;
20281 }
20282 set_vim_var_string(VV_CHAR, buf, -1);
20283}
20284
20285/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020286 * Set v:count to "count" and v:count1 to "count1".
20287 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020288 */
20289 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020290set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 long count;
20292 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020293 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020295 if (set_prevcount)
20296 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020297 vimvars[VV_COUNT].vv_nr = count;
20298 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299}
20300
20301/*
20302 * Set string v: variable to a copy of "val".
20303 */
20304 void
20305set_vim_var_string(idx, val, len)
20306 int idx;
20307 char_u *val;
20308 int len; /* length of "val" to use or -1 (whole string) */
20309{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020310 /* Need to do this (at least) once, since we can't initialize a union.
20311 * Will always be invoked when "v:progname" is set. */
20312 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20313
Bram Moolenaare9a41262005-01-15 22:18:47 +000020314 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020316 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020318 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020320 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321}
20322
20323/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020324 * Set List v: variable to "val".
20325 */
20326 void
20327set_vim_var_list(idx, val)
20328 int idx;
20329 list_T *val;
20330{
20331 list_unref(vimvars[idx].vv_list);
20332 vimvars[idx].vv_list = val;
20333 if (val != NULL)
20334 ++val->lv_refcount;
20335}
20336
20337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020338 * Set v:register if needed.
20339 */
20340 void
20341set_reg_var(c)
20342 int c;
20343{
20344 char_u regname;
20345
20346 if (c == 0 || c == ' ')
20347 regname = '"';
20348 else
20349 regname = c;
20350 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020351 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 set_vim_var_string(VV_REG, &regname, 1);
20353}
20354
20355/*
20356 * Get or set v:exception. If "oldval" == NULL, return the current value.
20357 * Otherwise, restore the value to "oldval" and return NULL.
20358 * Must always be called in pairs to save and restore v:exception! Does not
20359 * take care of memory allocations.
20360 */
20361 char_u *
20362v_exception(oldval)
20363 char_u *oldval;
20364{
20365 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020366 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367
Bram Moolenaare9a41262005-01-15 22:18:47 +000020368 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369 return NULL;
20370}
20371
20372/*
20373 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20374 * Otherwise, restore the value to "oldval" and return NULL.
20375 * Must always be called in pairs to save and restore v:throwpoint! Does not
20376 * take care of memory allocations.
20377 */
20378 char_u *
20379v_throwpoint(oldval)
20380 char_u *oldval;
20381{
20382 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020383 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384
Bram Moolenaare9a41262005-01-15 22:18:47 +000020385 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020386 return NULL;
20387}
20388
20389#if defined(FEAT_AUTOCMD) || defined(PROTO)
20390/*
20391 * Set v:cmdarg.
20392 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20393 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20394 * Must always be called in pairs!
20395 */
20396 char_u *
20397set_cmdarg(eap, oldarg)
20398 exarg_T *eap;
20399 char_u *oldarg;
20400{
20401 char_u *oldval;
20402 char_u *newval;
20403 unsigned len;
20404
Bram Moolenaare9a41262005-01-15 22:18:47 +000020405 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020406 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020408 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020409 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020410 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 }
20412
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020413 if (eap->force_bin == FORCE_BIN)
20414 len = 6;
20415 else if (eap->force_bin == FORCE_NOBIN)
20416 len = 8;
20417 else
20418 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020419
20420 if (eap->read_edit)
20421 len += 7;
20422
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020423 if (eap->force_ff != 0)
20424 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20425# ifdef FEAT_MBYTE
20426 if (eap->force_enc != 0)
20427 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020428 if (eap->bad_char != 0)
20429 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020430# endif
20431
20432 newval = alloc(len + 1);
20433 if (newval == NULL)
20434 return NULL;
20435
20436 if (eap->force_bin == FORCE_BIN)
20437 sprintf((char *)newval, " ++bin");
20438 else if (eap->force_bin == FORCE_NOBIN)
20439 sprintf((char *)newval, " ++nobin");
20440 else
20441 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020442
20443 if (eap->read_edit)
20444 STRCAT(newval, " ++edit");
20445
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020446 if (eap->force_ff != 0)
20447 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20448 eap->cmd + eap->force_ff);
20449# ifdef FEAT_MBYTE
20450 if (eap->force_enc != 0)
20451 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20452 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020453 if (eap->bad_char == BAD_KEEP)
20454 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20455 else if (eap->bad_char == BAD_DROP)
20456 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20457 else if (eap->bad_char != 0)
20458 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020459# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020460 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020461 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462}
20463#endif
20464
20465/*
20466 * Get the value of internal variable "name".
20467 * Return OK or FAIL.
20468 */
20469 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020470get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 char_u *name;
20472 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020473 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020474 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020475 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476{
20477 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020478 typval_T *tv = NULL;
20479 typval_T atv;
20480 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482
20483 /* truncate the name, so that we can use strcmp() */
20484 cc = name[len];
20485 name[len] = NUL;
20486
20487 /*
20488 * Check for "b:changedtick".
20489 */
20490 if (STRCMP(name, "b:changedtick") == 0)
20491 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020492 atv.v_type = VAR_NUMBER;
20493 atv.vval.v_number = curbuf->b_changedtick;
20494 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 }
20496
20497 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498 * Check for user-defined variables.
20499 */
20500 else
20501 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020502 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020504 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 }
20506
Bram Moolenaare9a41262005-01-15 22:18:47 +000020507 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020509 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020510 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020511 ret = FAIL;
20512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020513 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020514 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515
20516 name[len] = cc;
20517
20518 return ret;
20519}
20520
20521/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020522 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20523 * Also handle function call with Funcref variable: func(expr)
20524 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20525 */
20526 static int
20527handle_subscript(arg, rettv, evaluate, verbose)
20528 char_u **arg;
20529 typval_T *rettv;
20530 int evaluate; /* do more than finding the end */
20531 int verbose; /* give error messages */
20532{
20533 int ret = OK;
20534 dict_T *selfdict = NULL;
20535 char_u *s;
20536 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020537 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020538
20539 while (ret == OK
20540 && (**arg == '['
20541 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020542 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020543 && !vim_iswhite(*(*arg - 1)))
20544 {
20545 if (**arg == '(')
20546 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020547 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020548 if (evaluate)
20549 {
20550 functv = *rettv;
20551 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020552
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020553 /* Invoke the function. Recursive! */
20554 s = functv.vval.v_string;
20555 }
20556 else
20557 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020558 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020559 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20560 &len, evaluate, selfdict);
20561
20562 /* Clear the funcref afterwards, so that deleting it while
20563 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020564 if (evaluate)
20565 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020566
20567 /* Stop the expression evaluation when immediately aborting on
20568 * error, or when an interrupt occurred or an exception was thrown
20569 * but not caught. */
20570 if (aborting())
20571 {
20572 if (ret == OK)
20573 clear_tv(rettv);
20574 ret = FAIL;
20575 }
20576 dict_unref(selfdict);
20577 selfdict = NULL;
20578 }
20579 else /* **arg == '[' || **arg == '.' */
20580 {
20581 dict_unref(selfdict);
20582 if (rettv->v_type == VAR_DICT)
20583 {
20584 selfdict = rettv->vval.v_dict;
20585 if (selfdict != NULL)
20586 ++selfdict->dv_refcount;
20587 }
20588 else
20589 selfdict = NULL;
20590 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20591 {
20592 clear_tv(rettv);
20593 ret = FAIL;
20594 }
20595 }
20596 }
20597 dict_unref(selfdict);
20598 return ret;
20599}
20600
20601/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020602 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020603 * value).
20604 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020605 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020606alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020607{
Bram Moolenaar33570922005-01-25 22:26:29 +000020608 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020609}
20610
20611/*
20612 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 * The string "s" must have been allocated, it is consumed.
20614 * Return NULL for out of memory, the variable otherwise.
20615 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020616 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020617alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 char_u *s;
20619{
Bram Moolenaar33570922005-01-25 22:26:29 +000020620 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020622 rettv = alloc_tv();
20623 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020625 rettv->v_type = VAR_STRING;
20626 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 }
20628 else
20629 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020630 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631}
20632
20633/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020634 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020636 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020637free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020638 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639{
20640 if (varp != NULL)
20641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020642 switch (varp->v_type)
20643 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020644 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020645 func_unref(varp->vval.v_string);
20646 /*FALLTHROUGH*/
20647 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020648 vim_free(varp->vval.v_string);
20649 break;
20650 case VAR_LIST:
20651 list_unref(varp->vval.v_list);
20652 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020653 case VAR_DICT:
20654 dict_unref(varp->vval.v_dict);
20655 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020656 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020657#ifdef FEAT_FLOAT
20658 case VAR_FLOAT:
20659#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020660 case VAR_UNKNOWN:
20661 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020662 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020663 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020664 break;
20665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666 vim_free(varp);
20667 }
20668}
20669
20670/*
20671 * Free the memory for a variable value and set the value to NULL or 0.
20672 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020673 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020674clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020675 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676{
20677 if (varp != NULL)
20678 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020679 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020681 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020682 func_unref(varp->vval.v_string);
20683 /*FALLTHROUGH*/
20684 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020685 vim_free(varp->vval.v_string);
20686 varp->vval.v_string = NULL;
20687 break;
20688 case VAR_LIST:
20689 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020690 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020691 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020692 case VAR_DICT:
20693 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020694 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020695 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020696 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020697 varp->vval.v_number = 0;
20698 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020699#ifdef FEAT_FLOAT
20700 case VAR_FLOAT:
20701 varp->vval.v_float = 0.0;
20702 break;
20703#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020704 case VAR_UNKNOWN:
20705 break;
20706 default:
20707 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020709 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710 }
20711}
20712
20713/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020714 * Set the value of a variable to NULL without freeing items.
20715 */
20716 static void
20717init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020718 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020719{
20720 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020721 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020722}
20723
20724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 * Get the number value of a variable.
20726 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020727 * For incompatible types, return 0.
20728 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20729 * caller of incompatible types: it sets *denote to TRUE if "denote"
20730 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020731 */
20732 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020733get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020734 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020736 int error = FALSE;
20737
20738 return get_tv_number_chk(varp, &error); /* return 0L on error */
20739}
20740
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020741 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020742get_tv_number_chk(varp, denote)
20743 typval_T *varp;
20744 int *denote;
20745{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020746 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020748 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020750 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020751 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020752#ifdef FEAT_FLOAT
20753 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020754 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020755 break;
20756#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020757 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020758 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020759 break;
20760 case VAR_STRING:
20761 if (varp->vval.v_string != NULL)
20762 vim_str2nr(varp->vval.v_string, NULL, NULL,
20763 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020764 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020765 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020766 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020767 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020768 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020769 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020770 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020771 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020772 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020773 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020775 if (denote == NULL) /* useful for values that must be unsigned */
20776 n = -1;
20777 else
20778 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020779 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780}
20781
20782/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020783 * Get the lnum from the first argument.
20784 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020785 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020786 */
20787 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020788get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020789 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790{
Bram Moolenaar33570922005-01-25 22:26:29 +000020791 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 linenr_T lnum;
20793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020794 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 if (lnum == 0) /* no valid number, try using line() */
20796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020797 rettv.v_type = VAR_NUMBER;
20798 f_line(argvars, &rettv);
20799 lnum = rettv.vval.v_number;
20800 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020801 }
20802 return lnum;
20803}
20804
20805/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020806 * Get the lnum from the first argument.
20807 * Also accepts "$", then "buf" is used.
20808 * Returns 0 on error.
20809 */
20810 static linenr_T
20811get_tv_lnum_buf(argvars, buf)
20812 typval_T *argvars;
20813 buf_T *buf;
20814{
20815 if (argvars[0].v_type == VAR_STRING
20816 && argvars[0].vval.v_string != NULL
20817 && argvars[0].vval.v_string[0] == '$'
20818 && buf != NULL)
20819 return buf->b_ml.ml_line_count;
20820 return get_tv_number_chk(&argvars[0], NULL);
20821}
20822
20823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020824 * Get the string value of a variable.
20825 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020826 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20827 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 * If the String variable has never been set, return an empty string.
20829 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020830 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20831 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 */
20833 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020834get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020835 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836{
20837 static char_u mybuf[NUMBUFLEN];
20838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020839 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840}
20841
20842 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020844 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020845 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020847 char_u *res = get_tv_string_buf_chk(varp, buf);
20848
20849 return res != NULL ? res : (char_u *)"";
20850}
20851
Bram Moolenaar7d647822014-04-05 21:28:56 +020020852/*
20853 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20854 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020855 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020856get_tv_string_chk(varp)
20857 typval_T *varp;
20858{
20859 static char_u mybuf[NUMBUFLEN];
20860
20861 return get_tv_string_buf_chk(varp, mybuf);
20862}
20863
20864 static char_u *
20865get_tv_string_buf_chk(varp, buf)
20866 typval_T *varp;
20867 char_u *buf;
20868{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020869 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020870 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020871 case VAR_NUMBER:
20872 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20873 return buf;
20874 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020875 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020876 break;
20877 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020878 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020879 break;
20880 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020881 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020882 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020883#ifdef FEAT_FLOAT
20884 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020885 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020886 break;
20887#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020888 case VAR_STRING:
20889 if (varp->vval.v_string != NULL)
20890 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020891 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020892 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020893 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020894 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020896 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020897}
20898
20899/*
20900 * Find variable "name" in the list of variables.
20901 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020902 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020903 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020904 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020906 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020907find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020909 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020910 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020913 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914
Bram Moolenaara7043832005-01-21 11:56:39 +000020915 ht = find_var_ht(name, &varname);
20916 if (htp != NULL)
20917 *htp = ht;
20918 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020920 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921}
20922
20923/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020924 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020925 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020927 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020928find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020929 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020930 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020931 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020932 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020933{
Bram Moolenaar33570922005-01-25 22:26:29 +000020934 hashitem_T *hi;
20935
20936 if (*varname == NUL)
20937 {
20938 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020939 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020940 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020941 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020942 case 'g': return &globvars_var;
20943 case 'v': return &vimvars_var;
20944 case 'b': return &curbuf->b_bufvar;
20945 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020946#ifdef FEAT_WINDOWS
20947 case 't': return &curtab->tp_winvar;
20948#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020949 case 'l': return current_funccal == NULL
20950 ? NULL : &current_funccal->l_vars_var;
20951 case 'a': return current_funccal == NULL
20952 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020953 }
20954 return NULL;
20955 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020956
20957 hi = hash_find(ht, varname);
20958 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020959 {
20960 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020961 * worked find the variable again. Don't auto-load a script if it was
20962 * loaded already, otherwise it would be loaded every time when
20963 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020964 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020965 {
20966 /* Note: script_autoload() may make "hi" invalid. It must either
20967 * be obtained again or not used. */
20968 if (!script_autoload(varname, FALSE) || aborting())
20969 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020970 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020971 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020972 if (HASHITEM_EMPTY(hi))
20973 return NULL;
20974 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020975 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020976}
20977
20978/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020979 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020980 * Set "varname" to the start of name without ':'.
20981 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020982 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020983find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 char_u *name;
20985 char_u **varname;
20986{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020987 hashitem_T *hi;
20988
Bram Moolenaar071d4272004-06-13 20:20:40 +000020989 if (name[1] != ':')
20990 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020991 /* The name must not start with a colon or #. */
20992 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 return NULL;
20994 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020995
20996 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020997 hi = hash_find(&compat_hashtab, name);
20998 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020999 return &compat_hashtab;
21000
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 return &globvarht; /* global variable */
21003 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 }
21005 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021006 if (*name == 'g') /* global variable */
21007 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021008 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21009 */
21010 if (vim_strchr(name + 2, ':') != NULL
21011 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021012 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021013 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021014 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021016 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021017#ifdef FEAT_WINDOWS
21018 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021019 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021020#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021021 if (*name == 'v') /* v: variable */
21022 return &vimvarht;
21023 if (*name == 'a' && current_funccal != NULL) /* function argument */
21024 return &current_funccal->l_avars.dv_hashtab;
21025 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21026 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021027 if (*name == 's' /* script variable */
21028 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21029 return &SCRIPT_VARS(current_SID);
21030 return NULL;
21031}
21032
21033/*
21034 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021035 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 * Returns NULL when it doesn't exist.
21037 */
21038 char_u *
21039get_var_value(name)
21040 char_u *name;
21041{
Bram Moolenaar33570922005-01-25 22:26:29 +000021042 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021044 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045 if (v == NULL)
21046 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021047 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048}
21049
21050/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021051 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 * sourcing this script and when executing functions defined in the script.
21053 */
21054 void
21055new_script_vars(id)
21056 scid_T id;
21057{
Bram Moolenaara7043832005-01-21 11:56:39 +000021058 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021059 hashtab_T *ht;
21060 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021061
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21063 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021064 /* Re-allocating ga_data means that an ht_array pointing to
21065 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021066 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021067 for (i = 1; i <= ga_scripts.ga_len; ++i)
21068 {
21069 ht = &SCRIPT_VARS(i);
21070 if (ht->ht_mask == HT_INIT_SIZE - 1)
21071 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021072 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021073 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021074 }
21075
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 while (ga_scripts.ga_len < id)
21077 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021078 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021079 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021080 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021081 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 }
21083 }
21084}
21085
21086/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021087 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21088 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 */
21090 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021091init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021092 dict_T *dict;
21093 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021094 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095{
Bram Moolenaar33570922005-01-25 22:26:29 +000021096 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021097 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021098 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021099 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021100 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021101 dict_var->di_tv.vval.v_dict = dict;
21102 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021103 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021104 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21105 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106}
21107
21108/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021109 * Unreference a dictionary initialized by init_var_dict().
21110 */
21111 void
21112unref_var_dict(dict)
21113 dict_T *dict;
21114{
21115 /* Now the dict needs to be freed if no one else is using it, go back to
21116 * normal reference counting. */
21117 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21118 dict_unref(dict);
21119}
21120
21121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021123 * Frees all allocated variables and the value they contain.
21124 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 */
21126 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021127vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021128 hashtab_T *ht;
21129{
21130 vars_clear_ext(ht, TRUE);
21131}
21132
21133/*
21134 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21135 */
21136 static void
21137vars_clear_ext(ht, free_val)
21138 hashtab_T *ht;
21139 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140{
Bram Moolenaara7043832005-01-21 11:56:39 +000021141 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021142 hashitem_T *hi;
21143 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144
Bram Moolenaar33570922005-01-25 22:26:29 +000021145 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021146 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021147 for (hi = ht->ht_array; todo > 0; ++hi)
21148 {
21149 if (!HASHITEM_EMPTY(hi))
21150 {
21151 --todo;
21152
Bram Moolenaar33570922005-01-25 22:26:29 +000021153 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021154 * ht_array might change then. hash_clear() takes care of it
21155 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021156 v = HI2DI(hi);
21157 if (free_val)
21158 clear_tv(&v->di_tv);
21159 if ((v->di_flags & DI_FLAGS_FIX) == 0)
21160 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021161 }
21162 }
21163 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021164 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165}
21166
Bram Moolenaara7043832005-01-21 11:56:39 +000021167/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021168 * Delete a variable from hashtab "ht" at item "hi".
21169 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021172delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021173 hashtab_T *ht;
21174 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175{
Bram Moolenaar33570922005-01-25 22:26:29 +000021176 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021177
21178 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021179 clear_tv(&di->di_tv);
21180 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181}
21182
21183/*
21184 * List the value of one internal variable.
21185 */
21186 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021187list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021188 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021190 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021192 char_u *tofree;
21193 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021194 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021195
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021196 current_copyID += COPYID_INC;
21197 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021198 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021199 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021200 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021201}
21202
Bram Moolenaar071d4272004-06-13 20:20:40 +000021203 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021204list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205 char_u *prefix;
21206 char_u *name;
21207 int type;
21208 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021209 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021210{
Bram Moolenaar31859182007-08-14 20:41:13 +000021211 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21212 msg_start();
21213 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214 if (name != NULL) /* "a:" vars don't have a name stored */
21215 msg_puts(name);
21216 msg_putchar(' ');
21217 msg_advance(22);
21218 if (type == VAR_NUMBER)
21219 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021220 else if (type == VAR_FUNC)
21221 msg_putchar('*');
21222 else if (type == VAR_LIST)
21223 {
21224 msg_putchar('[');
21225 if (*string == '[')
21226 ++string;
21227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021228 else if (type == VAR_DICT)
21229 {
21230 msg_putchar('{');
21231 if (*string == '{')
21232 ++string;
21233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234 else
21235 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021236
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021238
21239 if (type == VAR_FUNC)
21240 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021241 if (*first)
21242 {
21243 msg_clr_eos();
21244 *first = FALSE;
21245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246}
21247
21248/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021249 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250 * If the variable already exists, the value is updated.
21251 * Otherwise the variable is created.
21252 */
21253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021254set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021256 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021257 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258{
Bram Moolenaar33570922005-01-25 22:26:29 +000021259 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021261 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021263 ht = find_var_ht(name, &varname);
21264 if (ht == NULL || *varname == NUL)
21265 {
21266 EMSG2(_(e_illvar), name);
21267 return;
21268 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021269 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021270
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021271 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21272 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021273
Bram Moolenaar33570922005-01-25 22:26:29 +000021274 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021276 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021277 if (var_check_ro(v->di_flags, name)
21278 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021279 return;
21280 if (v->di_tv.v_type != tv->v_type
21281 && !((v->di_tv.v_type == VAR_STRING
21282 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021284 || tv->v_type == VAR_NUMBER))
21285#ifdef FEAT_FLOAT
21286 && !((v->di_tv.v_type == VAR_NUMBER
21287 || v->di_tv.v_type == VAR_FLOAT)
21288 && (tv->v_type == VAR_NUMBER
21289 || tv->v_type == VAR_FLOAT))
21290#endif
21291 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021292 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021293 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021294 return;
21295 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021296
21297 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021298 * Handle setting internal v: variables separately: we don't change
21299 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021300 */
21301 if (ht == &vimvarht)
21302 {
21303 if (v->di_tv.v_type == VAR_STRING)
21304 {
21305 vim_free(v->di_tv.vval.v_string);
21306 if (copy || tv->v_type != VAR_STRING)
21307 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21308 else
21309 {
21310 /* Take over the string to avoid an extra alloc/free. */
21311 v->di_tv.vval.v_string = tv->vval.v_string;
21312 tv->vval.v_string = NULL;
21313 }
21314 }
21315 else if (v->di_tv.v_type != VAR_NUMBER)
21316 EMSG2(_(e_intern2), "set_var()");
21317 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021318 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021319 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021320 if (STRCMP(varname, "searchforward") == 0)
21321 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021322#ifdef FEAT_SEARCH_EXTRA
21323 else if (STRCMP(varname, "hlsearch") == 0)
21324 {
21325 no_hlsearch = !v->di_tv.vval.v_number;
21326 redraw_all_later(SOME_VALID);
21327 }
21328#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021329 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021330 return;
21331 }
21332
21333 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 }
21335 else /* add a new variable */
21336 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021337 /* Can't add "v:" variable. */
21338 if (ht == &vimvarht)
21339 {
21340 EMSG2(_(e_illvar), name);
21341 return;
21342 }
21343
Bram Moolenaar92124a32005-06-17 22:03:40 +000021344 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021345 if (!valid_varname(varname))
21346 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021347
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021348 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21349 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021350 if (v == NULL)
21351 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021352 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021353 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021355 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356 return;
21357 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021358 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021360
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021361 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021362 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021363 else
21364 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021365 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021366 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369}
21370
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021371/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021372 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021373 * Also give an error message.
21374 */
21375 static int
21376var_check_ro(flags, name)
21377 int flags;
21378 char_u *name;
21379{
21380 if (flags & DI_FLAGS_RO)
21381 {
21382 EMSG2(_(e_readonlyvar), name);
21383 return TRUE;
21384 }
21385 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21386 {
21387 EMSG2(_(e_readonlysbx), name);
21388 return TRUE;
21389 }
21390 return FALSE;
21391}
21392
21393/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021394 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21395 * Also give an error message.
21396 */
21397 static int
21398var_check_fixed(flags, name)
21399 int flags;
21400 char_u *name;
21401{
21402 if (flags & DI_FLAGS_FIX)
21403 {
21404 EMSG2(_("E795: Cannot delete variable %s"), name);
21405 return TRUE;
21406 }
21407 return FALSE;
21408}
21409
21410/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021411 * Check if a funcref is assigned to a valid variable name.
21412 * Return TRUE and give an error if not.
21413 */
21414 static int
21415var_check_func_name(name, new_var)
21416 char_u *name; /* points to start of variable name */
21417 int new_var; /* TRUE when creating the variable */
21418{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021419 /* Allow for w: b: s: and t:. */
21420 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021421 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21422 ? name[2] : name[0]))
21423 {
21424 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21425 name);
21426 return TRUE;
21427 }
21428 /* Don't allow hiding a function. When "v" is not NULL we might be
21429 * assigning another function to the same var, the type is checked
21430 * below. */
21431 if (new_var && function_exists(name))
21432 {
21433 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21434 name);
21435 return TRUE;
21436 }
21437 return FALSE;
21438}
21439
21440/*
21441 * Check if a variable name is valid.
21442 * Return FALSE and give an error if not.
21443 */
21444 static int
21445valid_varname(varname)
21446 char_u *varname;
21447{
21448 char_u *p;
21449
21450 for (p = varname; *p != NUL; ++p)
21451 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21452 && *p != AUTOLOAD_CHAR)
21453 {
21454 EMSG2(_(e_illvar), varname);
21455 return FALSE;
21456 }
21457 return TRUE;
21458}
21459
21460/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021461 * Return TRUE if typeval "tv" is set to be locked (immutable).
21462 * Also give an error message, using "name".
21463 */
21464 static int
21465tv_check_lock(lock, name)
21466 int lock;
21467 char_u *name;
21468{
21469 if (lock & VAR_LOCKED)
21470 {
21471 EMSG2(_("E741: Value is locked: %s"),
21472 name == NULL ? (char_u *)_("Unknown") : name);
21473 return TRUE;
21474 }
21475 if (lock & VAR_FIXED)
21476 {
21477 EMSG2(_("E742: Cannot change value of %s"),
21478 name == NULL ? (char_u *)_("Unknown") : name);
21479 return TRUE;
21480 }
21481 return FALSE;
21482}
21483
21484/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021485 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021486 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021487 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021488 * It is OK for "from" and "to" to point to the same item. This is used to
21489 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021490 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021491 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021492copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021493 typval_T *from;
21494 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021496 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021497 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021498 switch (from->v_type)
21499 {
21500 case VAR_NUMBER:
21501 to->vval.v_number = from->vval.v_number;
21502 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021503#ifdef FEAT_FLOAT
21504 case VAR_FLOAT:
21505 to->vval.v_float = from->vval.v_float;
21506 break;
21507#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021508 case VAR_STRING:
21509 case VAR_FUNC:
21510 if (from->vval.v_string == NULL)
21511 to->vval.v_string = NULL;
21512 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021513 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021514 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021515 if (from->v_type == VAR_FUNC)
21516 func_ref(to->vval.v_string);
21517 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021518 break;
21519 case VAR_LIST:
21520 if (from->vval.v_list == NULL)
21521 to->vval.v_list = NULL;
21522 else
21523 {
21524 to->vval.v_list = from->vval.v_list;
21525 ++to->vval.v_list->lv_refcount;
21526 }
21527 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021528 case VAR_DICT:
21529 if (from->vval.v_dict == NULL)
21530 to->vval.v_dict = NULL;
21531 else
21532 {
21533 to->vval.v_dict = from->vval.v_dict;
21534 ++to->vval.v_dict->dv_refcount;
21535 }
21536 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021537 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021538 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021539 break;
21540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541}
21542
21543/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021544 * Make a copy of an item.
21545 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021546 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21547 * reference to an already copied list/dict can be used.
21548 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021549 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021550 static int
21551item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021552 typval_T *from;
21553 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021554 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021555 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021556{
21557 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021558 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021559
Bram Moolenaar33570922005-01-25 22:26:29 +000021560 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021561 {
21562 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021563 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021564 }
21565 ++recurse;
21566
21567 switch (from->v_type)
21568 {
21569 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021570#ifdef FEAT_FLOAT
21571 case VAR_FLOAT:
21572#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021573 case VAR_STRING:
21574 case VAR_FUNC:
21575 copy_tv(from, to);
21576 break;
21577 case VAR_LIST:
21578 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021579 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021580 if (from->vval.v_list == NULL)
21581 to->vval.v_list = NULL;
21582 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21583 {
21584 /* use the copy made earlier */
21585 to->vval.v_list = from->vval.v_list->lv_copylist;
21586 ++to->vval.v_list->lv_refcount;
21587 }
21588 else
21589 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21590 if (to->vval.v_list == NULL)
21591 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021592 break;
21593 case VAR_DICT:
21594 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021595 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021596 if (from->vval.v_dict == NULL)
21597 to->vval.v_dict = NULL;
21598 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21599 {
21600 /* use the copy made earlier */
21601 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21602 ++to->vval.v_dict->dv_refcount;
21603 }
21604 else
21605 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21606 if (to->vval.v_dict == NULL)
21607 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021608 break;
21609 default:
21610 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021611 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021612 }
21613 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021614 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021615}
21616
21617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 * ":echo expr1 ..." print each argument separated with a space, add a
21619 * newline at the end.
21620 * ":echon expr1 ..." print each argument plain.
21621 */
21622 void
21623ex_echo(eap)
21624 exarg_T *eap;
21625{
21626 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021627 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021628 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 char_u *p;
21630 int needclr = TRUE;
21631 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021632 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633
21634 if (eap->skip)
21635 ++emsg_skip;
21636 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21637 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021638 /* If eval1() causes an error message the text from the command may
21639 * still need to be cleared. E.g., "echo 22,44". */
21640 need_clr_eos = needclr;
21641
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021643 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 {
21645 /*
21646 * Report the invalid expression unless the expression evaluation
21647 * has been cancelled due to an aborting error, an interrupt, or an
21648 * exception.
21649 */
21650 if (!aborting())
21651 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021652 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 break;
21654 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021655 need_clr_eos = FALSE;
21656
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 if (!eap->skip)
21658 {
21659 if (atstart)
21660 {
21661 atstart = FALSE;
21662 /* Call msg_start() after eval1(), evaluating the expression
21663 * may cause a message to appear. */
21664 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021665 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021666 /* Mark the saved text as finishing the line, so that what
21667 * follows is displayed on a new line when scrolling back
21668 * at the more prompt. */
21669 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 }
21673 else if (eap->cmdidx == CMD_echo)
21674 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021675 current_copyID += COPYID_INC;
21676 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021677 if (p != NULL)
21678 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021680 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021682 if (*p != TAB && needclr)
21683 {
21684 /* remove any text still there from the command */
21685 msg_clr_eos();
21686 needclr = FALSE;
21687 }
21688 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689 }
21690 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021691 {
21692#ifdef FEAT_MBYTE
21693 if (has_mbyte)
21694 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021695 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021696
21697 (void)msg_outtrans_len_attr(p, i, echo_attr);
21698 p += i - 1;
21699 }
21700 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021702 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021705 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021707 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 arg = skipwhite(arg);
21709 }
21710 eap->nextcmd = check_nextcmd(arg);
21711
21712 if (eap->skip)
21713 --emsg_skip;
21714 else
21715 {
21716 /* remove text that may still be there from the command */
21717 if (needclr)
21718 msg_clr_eos();
21719 if (eap->cmdidx == CMD_echo)
21720 msg_end();
21721 }
21722}
21723
21724/*
21725 * ":echohl {name}".
21726 */
21727 void
21728ex_echohl(eap)
21729 exarg_T *eap;
21730{
21731 int id;
21732
21733 id = syn_name2id(eap->arg);
21734 if (id == 0)
21735 echo_attr = 0;
21736 else
21737 echo_attr = syn_id2attr(id);
21738}
21739
21740/*
21741 * ":execute expr1 ..." execute the result of an expression.
21742 * ":echomsg expr1 ..." Print a message
21743 * ":echoerr expr1 ..." Print an error
21744 * Each gets spaces around each argument and a newline at the end for
21745 * echo commands
21746 */
21747 void
21748ex_execute(eap)
21749 exarg_T *eap;
21750{
21751 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021752 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 int ret = OK;
21754 char_u *p;
21755 garray_T ga;
21756 int len;
21757 int save_did_emsg;
21758
21759 ga_init2(&ga, 1, 80);
21760
21761 if (eap->skip)
21762 ++emsg_skip;
21763 while (*arg != NUL && *arg != '|' && *arg != '\n')
21764 {
21765 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021766 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 {
21768 /*
21769 * Report the invalid expression unless the expression evaluation
21770 * has been cancelled due to an aborting error, an interrupt, or an
21771 * exception.
21772 */
21773 if (!aborting())
21774 EMSG2(_(e_invexpr2), p);
21775 ret = FAIL;
21776 break;
21777 }
21778
21779 if (!eap->skip)
21780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021781 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 len = (int)STRLEN(p);
21783 if (ga_grow(&ga, len + 2) == FAIL)
21784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021785 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 ret = FAIL;
21787 break;
21788 }
21789 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792 ga.ga_len += len;
21793 }
21794
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021795 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 arg = skipwhite(arg);
21797 }
21798
21799 if (ret != FAIL && ga.ga_data != NULL)
21800 {
21801 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021802 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021804 out_flush();
21805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806 else if (eap->cmdidx == CMD_echoerr)
21807 {
21808 /* We don't want to abort following commands, restore did_emsg. */
21809 save_did_emsg = did_emsg;
21810 EMSG((char_u *)ga.ga_data);
21811 if (!force_abort)
21812 did_emsg = save_did_emsg;
21813 }
21814 else if (eap->cmdidx == CMD_execute)
21815 do_cmdline((char_u *)ga.ga_data,
21816 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21817 }
21818
21819 ga_clear(&ga);
21820
21821 if (eap->skip)
21822 --emsg_skip;
21823
21824 eap->nextcmd = check_nextcmd(arg);
21825}
21826
21827/*
21828 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21829 * "arg" points to the "&" or '+' when called, to "option" when returning.
21830 * Returns NULL when no option name found. Otherwise pointer to the char
21831 * after the option name.
21832 */
21833 static char_u *
21834find_option_end(arg, opt_flags)
21835 char_u **arg;
21836 int *opt_flags;
21837{
21838 char_u *p = *arg;
21839
21840 ++p;
21841 if (*p == 'g' && p[1] == ':')
21842 {
21843 *opt_flags = OPT_GLOBAL;
21844 p += 2;
21845 }
21846 else if (*p == 'l' && p[1] == ':')
21847 {
21848 *opt_flags = OPT_LOCAL;
21849 p += 2;
21850 }
21851 else
21852 *opt_flags = 0;
21853
21854 if (!ASCII_ISALPHA(*p))
21855 return NULL;
21856 *arg = p;
21857
21858 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21859 p += 4; /* termcap option */
21860 else
21861 while (ASCII_ISALPHA(*p))
21862 ++p;
21863 return p;
21864}
21865
21866/*
21867 * ":function"
21868 */
21869 void
21870ex_function(eap)
21871 exarg_T *eap;
21872{
21873 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021874 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021875 int j;
21876 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021878 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879 char_u *name = NULL;
21880 char_u *p;
21881 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021882 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 garray_T newargs;
21884 garray_T newlines;
21885 int varargs = FALSE;
21886 int mustend = FALSE;
21887 int flags = 0;
21888 ufunc_T *fp;
21889 int indent;
21890 int nesting;
21891 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021892 dictitem_T *v;
21893 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021894 static int func_nr = 0; /* number for nameless function */
21895 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021896 hashtab_T *ht;
21897 int todo;
21898 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021899 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900
21901 /*
21902 * ":function" without argument: list functions.
21903 */
21904 if (ends_excmd(*eap->arg))
21905 {
21906 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021907 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021908 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021909 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021910 {
21911 if (!HASHITEM_EMPTY(hi))
21912 {
21913 --todo;
21914 fp = HI2UF(hi);
21915 if (!isdigit(*fp->uf_name))
21916 list_func_head(fp, FALSE);
21917 }
21918 }
21919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 eap->nextcmd = check_nextcmd(eap->arg);
21921 return;
21922 }
21923
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021924 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021925 * ":function /pat": list functions matching pattern.
21926 */
21927 if (*eap->arg == '/')
21928 {
21929 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21930 if (!eap->skip)
21931 {
21932 regmatch_T regmatch;
21933
21934 c = *p;
21935 *p = NUL;
21936 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21937 *p = c;
21938 if (regmatch.regprog != NULL)
21939 {
21940 regmatch.rm_ic = p_ic;
21941
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021942 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021943 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21944 {
21945 if (!HASHITEM_EMPTY(hi))
21946 {
21947 --todo;
21948 fp = HI2UF(hi);
21949 if (!isdigit(*fp->uf_name)
21950 && vim_regexec(&regmatch, fp->uf_name, 0))
21951 list_func_head(fp, FALSE);
21952 }
21953 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021954 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021955 }
21956 }
21957 if (*p == '/')
21958 ++p;
21959 eap->nextcmd = check_nextcmd(p);
21960 return;
21961 }
21962
21963 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021964 * Get the function name. There are these situations:
21965 * func normal function name
21966 * "name" == func, "fudi.fd_dict" == NULL
21967 * dict.func new dictionary entry
21968 * "name" == NULL, "fudi.fd_dict" set,
21969 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21970 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021971 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021972 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21973 * dict.func existing dict entry that's not a Funcref
21974 * "name" == NULL, "fudi.fd_dict" set,
21975 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021976 * s:func script-local function name
21977 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021980 name = trans_function_name(&p, eap->skip, 0, &fudi);
21981 paren = (vim_strchr(p, '(') != NULL);
21982 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 {
21984 /*
21985 * Return on an invalid expression in braces, unless the expression
21986 * evaluation has been cancelled due to an aborting error, an
21987 * interrupt, or an exception.
21988 */
21989 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021990 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021991 if (!eap->skip && fudi.fd_newkey != NULL)
21992 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021993 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 else
21997 eap->skip = TRUE;
21998 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021999
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 /* An error in a function call during evaluation of an expression in magic
22001 * braces should not cause the function not to be defined. */
22002 saved_did_emsg = did_emsg;
22003 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004
22005 /*
22006 * ":function func" with only function name: list function.
22007 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022008 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 {
22010 if (!ends_excmd(*skipwhite(p)))
22011 {
22012 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022013 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 }
22015 eap->nextcmd = check_nextcmd(p);
22016 if (eap->nextcmd != NULL)
22017 *p = NUL;
22018 if (!eap->skip && !got_int)
22019 {
22020 fp = find_func(name);
22021 if (fp != NULL)
22022 {
22023 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022024 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022026 if (FUNCLINE(fp, j) == NULL)
22027 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 msg_putchar('\n');
22029 msg_outnum((long)(j + 1));
22030 if (j < 9)
22031 msg_putchar(' ');
22032 if (j < 99)
22033 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022034 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 out_flush(); /* show a line at a time */
22036 ui_breakcheck();
22037 }
22038 if (!got_int)
22039 {
22040 msg_putchar('\n');
22041 msg_puts((char_u *)" endfunction");
22042 }
22043 }
22044 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022045 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022047 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 }
22049
22050 /*
22051 * ":function name(arg1, arg2)" Define function.
22052 */
22053 p = skipwhite(p);
22054 if (*p != '(')
22055 {
22056 if (!eap->skip)
22057 {
22058 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022059 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 }
22061 /* attempt to continue by skipping some text */
22062 if (vim_strchr(p, '(') != NULL)
22063 p = vim_strchr(p, '(');
22064 }
22065 p = skipwhite(p + 1);
22066
22067 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22068 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22069
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022070 if (!eap->skip)
22071 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022072 /* Check the name of the function. Unless it's a dictionary function
22073 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022074 if (name != NULL)
22075 arg = name;
22076 else
22077 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022078 if (arg != NULL && (fudi.fd_di == NULL
22079 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022080 {
22081 if (*arg == K_SPECIAL)
22082 j = 3;
22083 else
22084 j = 0;
22085 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22086 : eval_isnamec(arg[j])))
22087 ++j;
22088 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022089 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022090 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022091 /* Disallow using the g: dict. */
22092 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22093 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022094 }
22095
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096 /*
22097 * Isolate the arguments: "arg1, arg2, ...)"
22098 */
22099 while (*p != ')')
22100 {
22101 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22102 {
22103 varargs = TRUE;
22104 p += 3;
22105 mustend = TRUE;
22106 }
22107 else
22108 {
22109 arg = p;
22110 while (ASCII_ISALNUM(*p) || *p == '_')
22111 ++p;
22112 if (arg == p || isdigit(*arg)
22113 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22114 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22115 {
22116 if (!eap->skip)
22117 EMSG2(_("E125: Illegal argument: %s"), arg);
22118 break;
22119 }
22120 if (ga_grow(&newargs, 1) == FAIL)
22121 goto erret;
22122 c = *p;
22123 *p = NUL;
22124 arg = vim_strsave(arg);
22125 if (arg == NULL)
22126 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022127
22128 /* Check for duplicate argument name. */
22129 for (i = 0; i < newargs.ga_len; ++i)
22130 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22131 {
22132 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022133 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022134 goto erret;
22135 }
22136
Bram Moolenaar071d4272004-06-13 20:20:40 +000022137 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22138 *p = c;
22139 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022140 if (*p == ',')
22141 ++p;
22142 else
22143 mustend = TRUE;
22144 }
22145 p = skipwhite(p);
22146 if (mustend && *p != ')')
22147 {
22148 if (!eap->skip)
22149 EMSG2(_(e_invarg2), eap->arg);
22150 break;
22151 }
22152 }
22153 ++p; /* skip the ')' */
22154
Bram Moolenaare9a41262005-01-15 22:18:47 +000022155 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 for (;;)
22157 {
22158 p = skipwhite(p);
22159 if (STRNCMP(p, "range", 5) == 0)
22160 {
22161 flags |= FC_RANGE;
22162 p += 5;
22163 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022164 else if (STRNCMP(p, "dict", 4) == 0)
22165 {
22166 flags |= FC_DICT;
22167 p += 4;
22168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 else if (STRNCMP(p, "abort", 5) == 0)
22170 {
22171 flags |= FC_ABORT;
22172 p += 5;
22173 }
22174 else
22175 break;
22176 }
22177
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022178 /* When there is a line break use what follows for the function body.
22179 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22180 if (*p == '\n')
22181 line_arg = p + 1;
22182 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022183 EMSG(_(e_trailing));
22184
22185 /*
22186 * Read the body of the function, until ":endfunction" is found.
22187 */
22188 if (KeyTyped)
22189 {
22190 /* Check if the function already exists, don't let the user type the
22191 * whole function before telling him it doesn't work! For a script we
22192 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022193 if (!eap->skip && !eap->forceit)
22194 {
22195 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22196 EMSG(_(e_funcdict));
22197 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022198 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022200
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022201 if (!eap->skip && did_emsg)
22202 goto erret;
22203
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 msg_putchar('\n'); /* don't overwrite the function name */
22205 cmdline_row = msg_row;
22206 }
22207
22208 indent = 2;
22209 nesting = 0;
22210 for (;;)
22211 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022212 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022213 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022214 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022215 saved_wait_return = FALSE;
22216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022218 sourcing_lnum_off = sourcing_lnum;
22219
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022220 if (line_arg != NULL)
22221 {
22222 /* Use eap->arg, split up in parts by line breaks. */
22223 theline = line_arg;
22224 p = vim_strchr(theline, '\n');
22225 if (p == NULL)
22226 line_arg += STRLEN(line_arg);
22227 else
22228 {
22229 *p = NUL;
22230 line_arg = p + 1;
22231 }
22232 }
22233 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 theline = getcmdline(':', 0L, indent);
22235 else
22236 theline = eap->getline(':', eap->cookie, indent);
22237 if (KeyTyped)
22238 lines_left = Rows - 1;
22239 if (theline == NULL)
22240 {
22241 EMSG(_("E126: Missing :endfunction"));
22242 goto erret;
22243 }
22244
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022245 /* Detect line continuation: sourcing_lnum increased more than one. */
22246 if (sourcing_lnum > sourcing_lnum_off + 1)
22247 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22248 else
22249 sourcing_lnum_off = 0;
22250
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251 if (skip_until != NULL)
22252 {
22253 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22254 * don't check for ":endfunc". */
22255 if (STRCMP(theline, skip_until) == 0)
22256 {
22257 vim_free(skip_until);
22258 skip_until = NULL;
22259 }
22260 }
22261 else
22262 {
22263 /* skip ':' and blanks*/
22264 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22265 ;
22266
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022267 /* Check for "endfunction". */
22268 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022270 if (line_arg == NULL)
22271 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 break;
22273 }
22274
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022275 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 * at "end". */
22277 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22278 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022279 else if (STRNCMP(p, "if", 2) == 0
22280 || STRNCMP(p, "wh", 2) == 0
22281 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 || STRNCMP(p, "try", 3) == 0)
22283 indent += 2;
22284
22285 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022286 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022287 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022288 if (*p == '!')
22289 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022291 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22292 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022294 ++nesting;
22295 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 }
22297 }
22298
22299 /* Check for ":append" or ":insert". */
22300 p = skip_range(p, NULL);
22301 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22302 || (p[0] == 'i'
22303 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22304 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22305 skip_until = vim_strsave((char_u *)".");
22306
22307 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22308 arg = skipwhite(skiptowhite(p));
22309 if (arg[0] == '<' && arg[1] =='<'
22310 && ((p[0] == 'p' && p[1] == 'y'
22311 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22312 || (p[0] == 'p' && p[1] == 'e'
22313 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22314 || (p[0] == 't' && p[1] == 'c'
22315 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022316 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22317 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22319 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022320 || (p[0] == 'm' && p[1] == 'z'
22321 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 ))
22323 {
22324 /* ":python <<" continues until a dot, like ":append" */
22325 p = skipwhite(arg + 2);
22326 if (*p == NUL)
22327 skip_until = vim_strsave((char_u *)".");
22328 else
22329 skip_until = vim_strsave(p);
22330 }
22331 }
22332
22333 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022334 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022335 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022336 if (line_arg == NULL)
22337 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022339 }
22340
22341 /* Copy the line to newly allocated memory. get_one_sourceline()
22342 * allocates 250 bytes per line, this saves 80% on average. The cost
22343 * is an extra alloc/free. */
22344 p = vim_strsave(theline);
22345 if (p != NULL)
22346 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022347 if (line_arg == NULL)
22348 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022349 theline = p;
22350 }
22351
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022352 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22353
22354 /* Add NULL lines for continuation lines, so that the line count is
22355 * equal to the index in the growarray. */
22356 while (sourcing_lnum_off-- > 0)
22357 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022358
22359 /* Check for end of eap->arg. */
22360 if (line_arg != NULL && *line_arg == NUL)
22361 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 }
22363
22364 /* Don't define the function when skipping commands or when an error was
22365 * detected. */
22366 if (eap->skip || did_emsg)
22367 goto erret;
22368
22369 /*
22370 * If there are no errors, add the function
22371 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022372 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022373 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022374 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022375 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022376 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022377 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022378 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022379 goto erret;
22380 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022381
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022382 fp = find_func(name);
22383 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022385 if (!eap->forceit)
22386 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022387 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022388 goto erret;
22389 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022390 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022391 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022392 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022393 name);
22394 goto erret;
22395 }
22396 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022397 ga_clear_strings(&(fp->uf_args));
22398 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022399 vim_free(name);
22400 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022402 }
22403 else
22404 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022405 char numbuf[20];
22406
22407 fp = NULL;
22408 if (fudi.fd_newkey == NULL && !eap->forceit)
22409 {
22410 EMSG(_(e_funcdict));
22411 goto erret;
22412 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022413 if (fudi.fd_di == NULL)
22414 {
22415 /* Can't add a function to a locked dictionary */
22416 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22417 goto erret;
22418 }
22419 /* Can't change an existing function if it is locked */
22420 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22421 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022422
22423 /* Give the function a sequential number. Can only be used with a
22424 * Funcref! */
22425 vim_free(name);
22426 sprintf(numbuf, "%d", ++func_nr);
22427 name = vim_strsave((char_u *)numbuf);
22428 if (name == NULL)
22429 goto erret;
22430 }
22431
22432 if (fp == NULL)
22433 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022434 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022435 {
22436 int slen, plen;
22437 char_u *scriptname;
22438
22439 /* Check that the autoload name matches the script name. */
22440 j = FAIL;
22441 if (sourcing_name != NULL)
22442 {
22443 scriptname = autoload_name(name);
22444 if (scriptname != NULL)
22445 {
22446 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022447 plen = (int)STRLEN(p);
22448 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022449 if (slen > plen && fnamecmp(p,
22450 sourcing_name + slen - plen) == 0)
22451 j = OK;
22452 vim_free(scriptname);
22453 }
22454 }
22455 if (j == FAIL)
22456 {
22457 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22458 goto erret;
22459 }
22460 }
22461
22462 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 if (fp == NULL)
22464 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022465
22466 if (fudi.fd_dict != NULL)
22467 {
22468 if (fudi.fd_di == NULL)
22469 {
22470 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022471 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022472 if (fudi.fd_di == NULL)
22473 {
22474 vim_free(fp);
22475 goto erret;
22476 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022477 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22478 {
22479 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022480 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022481 goto erret;
22482 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022483 }
22484 else
22485 /* overwrite existing dict entry */
22486 clear_tv(&fudi.fd_di->di_tv);
22487 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022488 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022489 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022490 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022491
22492 /* behave like "dict" was used */
22493 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022494 }
22495
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022497 STRCPY(fp->uf_name, name);
22498 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022500 fp->uf_args = newargs;
22501 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022502#ifdef FEAT_PROFILE
22503 fp->uf_tml_count = NULL;
22504 fp->uf_tml_total = NULL;
22505 fp->uf_tml_self = NULL;
22506 fp->uf_profiling = FALSE;
22507 if (prof_def_func())
22508 func_do_profile(fp);
22509#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022510 fp->uf_varargs = varargs;
22511 fp->uf_flags = flags;
22512 fp->uf_calls = 0;
22513 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022514 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515
22516erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 ga_clear_strings(&newargs);
22518 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022519ret_free:
22520 vim_free(skip_until);
22521 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022524 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525}
22526
22527/*
22528 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022529 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022531 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022532 * TFN_INT: internal function name OK
22533 * TFN_QUIET: be quiet
22534 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 * Advances "pp" to just after the function name (if no error).
22536 */
22537 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022538trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 char_u **pp;
22540 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022541 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022542 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022544 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 char_u *start;
22546 char_u *end;
22547 int lead;
22548 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022550 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022551
22552 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022553 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022555
22556 /* Check for hard coded <SNR>: already translated function ID (from a user
22557 * command). */
22558 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22559 && (*pp)[2] == (int)KE_SNR)
22560 {
22561 *pp += 3;
22562 len = get_id_len(pp) + 3;
22563 return vim_strnsave(start, len);
22564 }
22565
22566 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22567 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022569 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022571
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022572 /* Note that TFN_ flags use the same values as GLV_ flags. */
22573 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022574 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022575 if (end == start)
22576 {
22577 if (!skip)
22578 EMSG(_("E129: Function name required"));
22579 goto theend;
22580 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022581 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022582 {
22583 /*
22584 * Report an invalid expression in braces, unless the expression
22585 * evaluation has been cancelled due to an aborting error, an
22586 * interrupt, or an exception.
22587 */
22588 if (!aborting())
22589 {
22590 if (end != NULL)
22591 EMSG2(_(e_invarg2), start);
22592 }
22593 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022594 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022595 goto theend;
22596 }
22597
22598 if (lv.ll_tv != NULL)
22599 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022600 if (fdp != NULL)
22601 {
22602 fdp->fd_dict = lv.ll_dict;
22603 fdp->fd_newkey = lv.ll_newkey;
22604 lv.ll_newkey = NULL;
22605 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022606 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022607 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22608 {
22609 name = vim_strsave(lv.ll_tv->vval.v_string);
22610 *pp = end;
22611 }
22612 else
22613 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022614 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22615 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022616 EMSG(_(e_funcref));
22617 else
22618 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022619 name = NULL;
22620 }
22621 goto theend;
22622 }
22623
22624 if (lv.ll_name == NULL)
22625 {
22626 /* Error found, but continue after the function name. */
22627 *pp = end;
22628 goto theend;
22629 }
22630
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022631 /* Check if the name is a Funcref. If so, use the value. */
22632 if (lv.ll_exp_name != NULL)
22633 {
22634 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022635 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022636 if (name == lv.ll_exp_name)
22637 name = NULL;
22638 }
22639 else
22640 {
22641 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022642 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022643 if (name == *pp)
22644 name = NULL;
22645 }
22646 if (name != NULL)
22647 {
22648 name = vim_strsave(name);
22649 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022650 if (STRNCMP(name, "<SNR>", 5) == 0)
22651 {
22652 /* Change "<SNR>" to the byte sequence. */
22653 name[0] = K_SPECIAL;
22654 name[1] = KS_EXTRA;
22655 name[2] = (int)KE_SNR;
22656 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22657 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022658 goto theend;
22659 }
22660
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022661 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022662 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022663 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022664 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22665 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22666 {
22667 /* When there was "s:" already or the name expanded to get a
22668 * leading "s:" then remove it. */
22669 lv.ll_name += 2;
22670 len -= 2;
22671 lead = 2;
22672 }
22673 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022674 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022675 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022676 /* skip over "s:" and "g:" */
22677 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022678 lv.ll_name += 2;
22679 len = (int)(end - lv.ll_name);
22680 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022681
22682 /*
22683 * Copy the function name to allocated memory.
22684 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22685 * Accept <SNR>123_name() outside a script.
22686 */
22687 if (skip)
22688 lead = 0; /* do nothing */
22689 else if (lead > 0)
22690 {
22691 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022692 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22693 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022694 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022695 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022696 if (current_SID <= 0)
22697 {
22698 EMSG(_(e_usingsid));
22699 goto theend;
22700 }
22701 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22702 lead += (int)STRLEN(sid_buf);
22703 }
22704 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022705 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022706 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022707 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022708 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022709 goto theend;
22710 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022711 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022712 {
22713 char_u *cp = vim_strchr(lv.ll_name, ':');
22714
22715 if (cp != NULL && cp < end)
22716 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022717 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022718 goto theend;
22719 }
22720 }
22721
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022722 name = alloc((unsigned)(len + lead + 1));
22723 if (name != NULL)
22724 {
22725 if (lead > 0)
22726 {
22727 name[0] = K_SPECIAL;
22728 name[1] = KS_EXTRA;
22729 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022730 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022731 STRCPY(name + 3, sid_buf);
22732 }
22733 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022734 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022735 }
22736 *pp = end;
22737
22738theend:
22739 clear_lval(&lv);
22740 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741}
22742
22743/*
22744 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22745 * Return 2 if "p" starts with "s:".
22746 * Return 0 otherwise.
22747 */
22748 static int
22749eval_fname_script(p)
22750 char_u *p;
22751{
22752 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22753 || STRNICMP(p + 1, "SNR>", 4) == 0))
22754 return 5;
22755 if (p[0] == 's' && p[1] == ':')
22756 return 2;
22757 return 0;
22758}
22759
22760/*
22761 * Return TRUE if "p" starts with "<SID>" or "s:".
22762 * Only works if eval_fname_script() returned non-zero for "p"!
22763 */
22764 static int
22765eval_fname_sid(p)
22766 char_u *p;
22767{
22768 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22769}
22770
22771/*
22772 * List the head of the function: "name(arg1, arg2)".
22773 */
22774 static void
22775list_func_head(fp, indent)
22776 ufunc_T *fp;
22777 int indent;
22778{
22779 int j;
22780
22781 msg_start();
22782 if (indent)
22783 MSG_PUTS(" ");
22784 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022785 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 {
22787 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022788 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 }
22790 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022791 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022792 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022793 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794 {
22795 if (j)
22796 MSG_PUTS(", ");
22797 msg_puts(FUNCARG(fp, j));
22798 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022799 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800 {
22801 if (j)
22802 MSG_PUTS(", ");
22803 MSG_PUTS("...");
22804 }
22805 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022806 if (fp->uf_flags & FC_ABORT)
22807 MSG_PUTS(" abort");
22808 if (fp->uf_flags & FC_RANGE)
22809 MSG_PUTS(" range");
22810 if (fp->uf_flags & FC_DICT)
22811 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022812 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022813 if (p_verbose > 0)
22814 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815}
22816
22817/*
22818 * Find a function by name, return pointer to it in ufuncs.
22819 * Return NULL for unknown function.
22820 */
22821 static ufunc_T *
22822find_func(name)
22823 char_u *name;
22824{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022825 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022827 hi = hash_find(&func_hashtab, name);
22828 if (!HASHITEM_EMPTY(hi))
22829 return HI2UF(hi);
22830 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831}
22832
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022833#if defined(EXITFREE) || defined(PROTO)
22834 void
22835free_all_functions()
22836{
22837 hashitem_T *hi;
22838
22839 /* Need to start all over every time, because func_free() may change the
22840 * hash table. */
22841 while (func_hashtab.ht_used > 0)
22842 for (hi = func_hashtab.ht_array; ; ++hi)
22843 if (!HASHITEM_EMPTY(hi))
22844 {
22845 func_free(HI2UF(hi));
22846 break;
22847 }
22848}
22849#endif
22850
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022851 int
22852translated_function_exists(name)
22853 char_u *name;
22854{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022855 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022856 return find_internal_func(name) >= 0;
22857 return find_func(name) != NULL;
22858}
22859
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022860/*
22861 * Return TRUE if a function "name" exists.
22862 */
22863 static int
22864function_exists(name)
22865 char_u *name;
22866{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022867 char_u *nm = name;
22868 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022869 int n = FALSE;
22870
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022871 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22872 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022873 nm = skipwhite(nm);
22874
22875 /* Only accept "funcname", "funcname ", "funcname (..." and
22876 * "funcname(...", not "funcname!...". */
22877 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022878 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022879 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022880 return n;
22881}
22882
Bram Moolenaara1544c02013-05-30 12:35:52 +020022883 char_u *
22884get_expanded_name(name, check)
22885 char_u *name;
22886 int check;
22887{
22888 char_u *nm = name;
22889 char_u *p;
22890
22891 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22892
22893 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022894 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022895 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022896
Bram Moolenaara1544c02013-05-30 12:35:52 +020022897 vim_free(p);
22898 return NULL;
22899}
22900
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022901/*
22902 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022903 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22904 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022905 */
22906 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022907builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022908 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022909 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022910{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022911 char_u *p;
22912
22913 if (!ASCII_ISLOWER(name[0]))
22914 return FALSE;
22915 p = vim_strchr(name, AUTOLOAD_CHAR);
22916 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022917}
22918
Bram Moolenaar05159a02005-02-26 23:04:13 +000022919#if defined(FEAT_PROFILE) || defined(PROTO)
22920/*
22921 * Start profiling function "fp".
22922 */
22923 static void
22924func_do_profile(fp)
22925 ufunc_T *fp;
22926{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022927 int len = fp->uf_lines.ga_len;
22928
22929 if (len == 0)
22930 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022931 fp->uf_tm_count = 0;
22932 profile_zero(&fp->uf_tm_self);
22933 profile_zero(&fp->uf_tm_total);
22934 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022935 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022936 if (fp->uf_tml_total == NULL)
22937 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022938 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022939 if (fp->uf_tml_self == NULL)
22940 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022941 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022942 fp->uf_tml_idx = -1;
22943 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22944 || fp->uf_tml_self == NULL)
22945 return; /* out of memory */
22946
22947 fp->uf_profiling = TRUE;
22948}
22949
22950/*
22951 * Dump the profiling results for all functions in file "fd".
22952 */
22953 void
22954func_dump_profile(fd)
22955 FILE *fd;
22956{
22957 hashitem_T *hi;
22958 int todo;
22959 ufunc_T *fp;
22960 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022961 ufunc_T **sorttab;
22962 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022963
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022964 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022965 if (todo == 0)
22966 return; /* nothing to dump */
22967
Bram Moolenaar73830342005-02-28 22:48:19 +000022968 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22969
Bram Moolenaar05159a02005-02-26 23:04:13 +000022970 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22971 {
22972 if (!HASHITEM_EMPTY(hi))
22973 {
22974 --todo;
22975 fp = HI2UF(hi);
22976 if (fp->uf_profiling)
22977 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022978 if (sorttab != NULL)
22979 sorttab[st_len++] = fp;
22980
Bram Moolenaar05159a02005-02-26 23:04:13 +000022981 if (fp->uf_name[0] == K_SPECIAL)
22982 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22983 else
22984 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22985 if (fp->uf_tm_count == 1)
22986 fprintf(fd, "Called 1 time\n");
22987 else
22988 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22989 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22990 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22991 fprintf(fd, "\n");
22992 fprintf(fd, "count total (s) self (s)\n");
22993
22994 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22995 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022996 if (FUNCLINE(fp, i) == NULL)
22997 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022998 prof_func_line(fd, fp->uf_tml_count[i],
22999 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023000 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23001 }
23002 fprintf(fd, "\n");
23003 }
23004 }
23005 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023006
23007 if (sorttab != NULL && st_len > 0)
23008 {
23009 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23010 prof_total_cmp);
23011 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23012 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23013 prof_self_cmp);
23014 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23015 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023016
23017 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023018}
Bram Moolenaar73830342005-02-28 22:48:19 +000023019
23020 static void
23021prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23022 FILE *fd;
23023 ufunc_T **sorttab;
23024 int st_len;
23025 char *title;
23026 int prefer_self; /* when equal print only self time */
23027{
23028 int i;
23029 ufunc_T *fp;
23030
23031 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23032 fprintf(fd, "count total (s) self (s) function\n");
23033 for (i = 0; i < 20 && i < st_len; ++i)
23034 {
23035 fp = sorttab[i];
23036 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23037 prefer_self);
23038 if (fp->uf_name[0] == K_SPECIAL)
23039 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23040 else
23041 fprintf(fd, " %s()\n", fp->uf_name);
23042 }
23043 fprintf(fd, "\n");
23044}
23045
23046/*
23047 * Print the count and times for one function or function line.
23048 */
23049 static void
23050prof_func_line(fd, count, total, self, prefer_self)
23051 FILE *fd;
23052 int count;
23053 proftime_T *total;
23054 proftime_T *self;
23055 int prefer_self; /* when equal print only self time */
23056{
23057 if (count > 0)
23058 {
23059 fprintf(fd, "%5d ", count);
23060 if (prefer_self && profile_equal(total, self))
23061 fprintf(fd, " ");
23062 else
23063 fprintf(fd, "%s ", profile_msg(total));
23064 if (!prefer_self && profile_equal(total, self))
23065 fprintf(fd, " ");
23066 else
23067 fprintf(fd, "%s ", profile_msg(self));
23068 }
23069 else
23070 fprintf(fd, " ");
23071}
23072
23073/*
23074 * Compare function for total time sorting.
23075 */
23076 static int
23077#ifdef __BORLANDC__
23078_RTLENTRYF
23079#endif
23080prof_total_cmp(s1, s2)
23081 const void *s1;
23082 const void *s2;
23083{
23084 ufunc_T *p1, *p2;
23085
23086 p1 = *(ufunc_T **)s1;
23087 p2 = *(ufunc_T **)s2;
23088 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23089}
23090
23091/*
23092 * Compare function for self time sorting.
23093 */
23094 static int
23095#ifdef __BORLANDC__
23096_RTLENTRYF
23097#endif
23098prof_self_cmp(s1, s2)
23099 const void *s1;
23100 const void *s2;
23101{
23102 ufunc_T *p1, *p2;
23103
23104 p1 = *(ufunc_T **)s1;
23105 p2 = *(ufunc_T **)s2;
23106 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23107}
23108
Bram Moolenaar05159a02005-02-26 23:04:13 +000023109#endif
23110
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023111/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023112 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023113 * Return TRUE if a package was loaded.
23114 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023115 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023116script_autoload(name, reload)
23117 char_u *name;
23118 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023119{
23120 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023121 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023122 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023123 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023124
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023125 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023126 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023127 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023128 return FALSE;
23129
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023130 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023131
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023132 /* Find the name in the list of previously loaded package names. Skip
23133 * "autoload/", it's always the same. */
23134 for (i = 0; i < ga_loaded.ga_len; ++i)
23135 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23136 break;
23137 if (!reload && i < ga_loaded.ga_len)
23138 ret = FALSE; /* was loaded already */
23139 else
23140 {
23141 /* Remember the name if it wasn't loaded already. */
23142 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23143 {
23144 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23145 tofree = NULL;
23146 }
23147
23148 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023149 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023150 ret = TRUE;
23151 }
23152
23153 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023154 return ret;
23155}
23156
23157/*
23158 * Return the autoload script name for a function or variable name.
23159 * Returns NULL when out of memory.
23160 */
23161 static char_u *
23162autoload_name(name)
23163 char_u *name;
23164{
23165 char_u *p;
23166 char_u *scriptname;
23167
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023168 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023169 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23170 if (scriptname == NULL)
23171 return FALSE;
23172 STRCPY(scriptname, "autoload/");
23173 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023174 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023175 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023176 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023177 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023178 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023179}
23180
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23182
23183/*
23184 * Function given to ExpandGeneric() to obtain the list of user defined
23185 * function names.
23186 */
23187 char_u *
23188get_user_func_name(xp, idx)
23189 expand_T *xp;
23190 int idx;
23191{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023192 static long_u done;
23193 static hashitem_T *hi;
23194 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195
23196 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023198 done = 0;
23199 hi = func_hashtab.ht_array;
23200 }
23201 if (done < func_hashtab.ht_used)
23202 {
23203 if (done++ > 0)
23204 ++hi;
23205 while (HASHITEM_EMPTY(hi))
23206 ++hi;
23207 fp = HI2UF(hi);
23208
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023209 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023210 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023211
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023212 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23213 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214
23215 cat_func_name(IObuff, fp);
23216 if (xp->xp_context != EXPAND_USER_FUNC)
23217 {
23218 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023219 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 STRCAT(IObuff, ")");
23221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222 return IObuff;
23223 }
23224 return NULL;
23225}
23226
23227#endif /* FEAT_CMDL_COMPL */
23228
23229/*
23230 * Copy the function name of "fp" to buffer "buf".
23231 * "buf" must be able to hold the function name plus three bytes.
23232 * Takes care of script-local function names.
23233 */
23234 static void
23235cat_func_name(buf, fp)
23236 char_u *buf;
23237 ufunc_T *fp;
23238{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023239 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 {
23241 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023242 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023243 }
23244 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023245 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246}
23247
23248/*
23249 * ":delfunction {name}"
23250 */
23251 void
23252ex_delfunction(eap)
23253 exarg_T *eap;
23254{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023255 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 char_u *p;
23257 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023258 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259
23260 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023261 name = trans_function_name(&p, eap->skip, 0, &fudi);
23262 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023264 {
23265 if (fudi.fd_dict != NULL && !eap->skip)
23266 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269 if (!ends_excmd(*skipwhite(p)))
23270 {
23271 vim_free(name);
23272 EMSG(_(e_trailing));
23273 return;
23274 }
23275 eap->nextcmd = check_nextcmd(p);
23276 if (eap->nextcmd != NULL)
23277 *p = NUL;
23278
23279 if (!eap->skip)
23280 fp = find_func(name);
23281 vim_free(name);
23282
23283 if (!eap->skip)
23284 {
23285 if (fp == NULL)
23286 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023287 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288 return;
23289 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023290 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 {
23292 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23293 return;
23294 }
23295
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023296 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023298 /* Delete the dict item that refers to the function, it will
23299 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023300 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023302 else
23303 func_free(fp);
23304 }
23305}
23306
23307/*
23308 * Free a function and remove it from the list of functions.
23309 */
23310 static void
23311func_free(fp)
23312 ufunc_T *fp;
23313{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023314 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023315
23316 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023317 ga_clear_strings(&(fp->uf_args));
23318 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023319#ifdef FEAT_PROFILE
23320 vim_free(fp->uf_tml_count);
23321 vim_free(fp->uf_tml_total);
23322 vim_free(fp->uf_tml_self);
23323#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023324
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023325 /* remove the function from the function hashtable */
23326 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23327 if (HASHITEM_EMPTY(hi))
23328 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023329 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023330 hash_remove(&func_hashtab, hi);
23331
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023332 vim_free(fp);
23333}
23334
23335/*
23336 * Unreference a Function: decrement the reference count and free it when it
23337 * becomes zero. Only for numbered functions.
23338 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023339 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023340func_unref(name)
23341 char_u *name;
23342{
23343 ufunc_T *fp;
23344
23345 if (name != NULL && isdigit(*name))
23346 {
23347 fp = find_func(name);
23348 if (fp == NULL)
23349 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023350 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023351 {
23352 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023353 * when "uf_calls" becomes zero. */
23354 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023355 func_free(fp);
23356 }
23357 }
23358}
23359
23360/*
23361 * Count a reference to a Function.
23362 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023363 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023364func_ref(name)
23365 char_u *name;
23366{
23367 ufunc_T *fp;
23368
23369 if (name != NULL && isdigit(*name))
23370 {
23371 fp = find_func(name);
23372 if (fp == NULL)
23373 EMSG2(_(e_intern2), "func_ref()");
23374 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023375 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023376 }
23377}
23378
23379/*
23380 * Call a user function.
23381 */
23382 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023383call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384 ufunc_T *fp; /* pointer to function */
23385 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023386 typval_T *argvars; /* arguments */
23387 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 linenr_T firstline; /* first line of range */
23389 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023390 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391{
Bram Moolenaar33570922005-01-25 22:26:29 +000023392 char_u *save_sourcing_name;
23393 linenr_T save_sourcing_lnum;
23394 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023395 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023396 int save_did_emsg;
23397 static int depth = 0;
23398 dictitem_T *v;
23399 int fixvar_idx = 0; /* index in fixvar[] */
23400 int i;
23401 int ai;
23402 char_u numbuf[NUMBUFLEN];
23403 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023404#ifdef FEAT_PROFILE
23405 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023406 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408
23409 /* If depth of calling is getting too high, don't execute the function */
23410 if (depth >= p_mfd)
23411 {
23412 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023413 rettv->v_type = VAR_NUMBER;
23414 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415 return;
23416 }
23417 ++depth;
23418
23419 line_breakcheck(); /* check for CTRL-C hit */
23420
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023421 fc = (funccall_T *)alloc(sizeof(funccall_T));
23422 fc->caller = current_funccal;
23423 current_funccal = fc;
23424 fc->func = fp;
23425 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023426 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023427 fc->linenr = 0;
23428 fc->returned = FALSE;
23429 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023431 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23432 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023433
Bram Moolenaar33570922005-01-25 22:26:29 +000023434 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023435 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023436 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23437 * each argument variable and saves a lot of time.
23438 */
23439 /*
23440 * Init l: variables.
23441 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023442 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023443 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023444 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023445 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23446 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023447 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023448 name = v->di_key;
23449 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023450 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023451 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023452 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023453 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023454 v->di_tv.vval.v_dict = selfdict;
23455 ++selfdict->dv_refcount;
23456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023457
Bram Moolenaar33570922005-01-25 22:26:29 +000023458 /*
23459 * Init a: variables.
23460 * Set a:0 to "argcount".
23461 * Set a:000 to a list with room for the "..." arguments.
23462 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023463 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023464 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023465 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023466 /* Use "name" to avoid a warning from some compiler that checks the
23467 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023468 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023469 name = v->di_key;
23470 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023471 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023472 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023473 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023474 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023475 v->di_tv.vval.v_list = &fc->l_varlist;
23476 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23477 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23478 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023479
23480 /*
23481 * Set a:firstline to "firstline" and a:lastline to "lastline".
23482 * Set a:name to named arguments.
23483 * Set a:N to the "..." arguments.
23484 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023485 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023486 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023487 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023488 (varnumber_T)lastline);
23489 for (i = 0; i < argcount; ++i)
23490 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023491 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023492 if (ai < 0)
23493 /* named argument a:name */
23494 name = FUNCARG(fp, i);
23495 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023496 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023497 /* "..." argument a:1, a:2, etc. */
23498 sprintf((char *)numbuf, "%d", ai + 1);
23499 name = numbuf;
23500 }
23501 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23502 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023503 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023504 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23505 }
23506 else
23507 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023508 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23509 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023510 if (v == NULL)
23511 break;
23512 v->di_flags = DI_FLAGS_RO;
23513 }
23514 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023515 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023516
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023517 /* Note: the values are copied directly to avoid alloc/free.
23518 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023519 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023520 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023521
23522 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23523 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023524 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23525 fc->l_listitems[ai].li_tv = argvars[i];
23526 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023527 }
23528 }
23529
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 /* Don't redraw while executing the function. */
23531 ++RedrawingDisabled;
23532 save_sourcing_name = sourcing_name;
23533 save_sourcing_lnum = sourcing_lnum;
23534 sourcing_lnum = 1;
23535 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023536 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023537 if (sourcing_name != NULL)
23538 {
23539 if (save_sourcing_name != NULL
23540 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23541 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23542 else
23543 STRCPY(sourcing_name, "function ");
23544 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23545
23546 if (p_verbose >= 12)
23547 {
23548 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023549 verbose_enter_scroll();
23550
Bram Moolenaar555b2802005-05-19 21:08:39 +000023551 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 if (p_verbose >= 14)
23553 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023555 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023556 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023557 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558
23559 msg_puts((char_u *)"(");
23560 for (i = 0; i < argcount; ++i)
23561 {
23562 if (i > 0)
23563 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023564 if (argvars[i].v_type == VAR_NUMBER)
23565 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 else
23567 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023568 /* Do not want errors such as E724 here. */
23569 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023570 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023571 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023572 if (s != NULL)
23573 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023574 if (vim_strsize(s) > MSG_BUF_CLEN)
23575 {
23576 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23577 s = buf;
23578 }
23579 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023580 vim_free(tofree);
23581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 }
23583 }
23584 msg_puts((char_u *)")");
23585 }
23586 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023587
23588 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 --no_wait_return;
23590 }
23591 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023592#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023593 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023594 {
23595 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23596 func_do_profile(fp);
23597 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023598 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023599 {
23600 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023601 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023602 profile_zero(&fp->uf_tm_children);
23603 }
23604 script_prof_save(&wait_start);
23605 }
23606#endif
23607
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023609 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 save_did_emsg = did_emsg;
23611 did_emsg = FALSE;
23612
23613 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023614 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23616
23617 --RedrawingDisabled;
23618
23619 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023620 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023622 clear_tv(rettv);
23623 rettv->v_type = VAR_NUMBER;
23624 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 }
23626
Bram Moolenaar05159a02005-02-26 23:04:13 +000023627#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023628 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023629 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023630 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023631 profile_end(&call_start);
23632 profile_sub_wait(&wait_start, &call_start);
23633 profile_add(&fp->uf_tm_total, &call_start);
23634 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023635 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023636 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023637 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23638 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023639 }
23640 }
23641#endif
23642
Bram Moolenaar071d4272004-06-13 20:20:40 +000023643 /* when being verbose, mention the return value */
23644 if (p_verbose >= 12)
23645 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023647 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023650 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023651 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023652 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023653 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023654 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023656 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023657 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023658 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023659 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023660
Bram Moolenaar555b2802005-05-19 21:08:39 +000023661 /* The value may be very long. Skip the middle part, so that we
23662 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023663 * truncate it at the end. Don't want errors such as E724 here. */
23664 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023665 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023666 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023667 if (s != NULL)
23668 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023669 if (vim_strsize(s) > MSG_BUF_CLEN)
23670 {
23671 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23672 s = buf;
23673 }
23674 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023675 vim_free(tofree);
23676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023677 }
23678 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023679
23680 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 --no_wait_return;
23682 }
23683
23684 vim_free(sourcing_name);
23685 sourcing_name = save_sourcing_name;
23686 sourcing_lnum = save_sourcing_lnum;
23687 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023688#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023689 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023690 script_prof_restore(&wait_start);
23691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692
23693 if (p_verbose >= 12 && sourcing_name != NULL)
23694 {
23695 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023696 verbose_enter_scroll();
23697
Bram Moolenaar555b2802005-05-19 21:08:39 +000023698 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023700
23701 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 --no_wait_return;
23703 }
23704
23705 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023706 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023708
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023709 /* If the a:000 list and the l: and a: dicts are not referenced we can
23710 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023711 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23712 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23713 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23714 {
23715 free_funccal(fc, FALSE);
23716 }
23717 else
23718 {
23719 hashitem_T *hi;
23720 listitem_T *li;
23721 int todo;
23722
23723 /* "fc" is still in use. This can happen when returning "a:000" or
23724 * assigning "l:" to a global variable.
23725 * Link "fc" in the list for garbage collection later. */
23726 fc->caller = previous_funccal;
23727 previous_funccal = fc;
23728
23729 /* Make a copy of the a: variables, since we didn't do that above. */
23730 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23731 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23732 {
23733 if (!HASHITEM_EMPTY(hi))
23734 {
23735 --todo;
23736 v = HI2DI(hi);
23737 copy_tv(&v->di_tv, &v->di_tv);
23738 }
23739 }
23740
23741 /* Make a copy of the a:000 items, since we didn't do that above. */
23742 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23743 copy_tv(&li->li_tv, &li->li_tv);
23744 }
23745}
23746
23747/*
23748 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023749 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023750 */
23751 static int
23752can_free_funccal(fc, copyID)
23753 funccall_T *fc;
23754 int copyID;
23755{
23756 return (fc->l_varlist.lv_copyID != copyID
23757 && fc->l_vars.dv_copyID != copyID
23758 && fc->l_avars.dv_copyID != copyID);
23759}
23760
23761/*
23762 * Free "fc" and what it contains.
23763 */
23764 static void
23765free_funccal(fc, free_val)
23766 funccall_T *fc;
23767 int free_val; /* a: vars were allocated */
23768{
23769 listitem_T *li;
23770
23771 /* The a: variables typevals may not have been allocated, only free the
23772 * allocated variables. */
23773 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23774
23775 /* free all l: variables */
23776 vars_clear(&fc->l_vars.dv_hashtab);
23777
23778 /* Free the a:000 variables if they were allocated. */
23779 if (free_val)
23780 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23781 clear_tv(&li->li_tv);
23782
23783 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784}
23785
23786/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023787 * Add a number variable "name" to dict "dp" with value "nr".
23788 */
23789 static void
23790add_nr_var(dp, v, name, nr)
23791 dict_T *dp;
23792 dictitem_T *v;
23793 char *name;
23794 varnumber_T nr;
23795{
23796 STRCPY(v->di_key, name);
23797 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23798 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23799 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023800 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023801 v->di_tv.vval.v_number = nr;
23802}
23803
23804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 * ":return [expr]"
23806 */
23807 void
23808ex_return(eap)
23809 exarg_T *eap;
23810{
23811 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023812 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813 int returning = FALSE;
23814
23815 if (current_funccal == NULL)
23816 {
23817 EMSG(_("E133: :return not inside a function"));
23818 return;
23819 }
23820
23821 if (eap->skip)
23822 ++emsg_skip;
23823
23824 eap->nextcmd = NULL;
23825 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023826 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 {
23828 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023829 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023831 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832 }
23833 /* It's safer to return also on error. */
23834 else if (!eap->skip)
23835 {
23836 /*
23837 * Return unless the expression evaluation has been cancelled due to an
23838 * aborting error, an interrupt, or an exception.
23839 */
23840 if (!aborting())
23841 returning = do_return(eap, FALSE, TRUE, NULL);
23842 }
23843
23844 /* When skipping or the return gets pending, advance to the next command
23845 * in this line (!returning). Otherwise, ignore the rest of the line.
23846 * Following lines will be ignored by get_func_line(). */
23847 if (returning)
23848 eap->nextcmd = NULL;
23849 else if (eap->nextcmd == NULL) /* no argument */
23850 eap->nextcmd = check_nextcmd(arg);
23851
23852 if (eap->skip)
23853 --emsg_skip;
23854}
23855
23856/*
23857 * Return from a function. Possibly makes the return pending. Also called
23858 * for a pending return at the ":endtry" or after returning from an extra
23859 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023860 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023861 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862 * FALSE when the return gets pending.
23863 */
23864 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023865do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 exarg_T *eap;
23867 int reanimate;
23868 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023869 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870{
23871 int idx;
23872 struct condstack *cstack = eap->cstack;
23873
23874 if (reanimate)
23875 /* Undo the return. */
23876 current_funccal->returned = FALSE;
23877
23878 /*
23879 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23880 * not in its finally clause (which then is to be executed next) is found.
23881 * In this case, make the ":return" pending for execution at the ":endtry".
23882 * Otherwise, return normally.
23883 */
23884 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23885 if (idx >= 0)
23886 {
23887 cstack->cs_pending[idx] = CSTP_RETURN;
23888
23889 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023890 /* A pending return again gets pending. "rettv" points to an
23891 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023893 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 else
23895 {
23896 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023897 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023899 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023901 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 {
23903 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023904 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023905 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 else
23907 EMSG(_(e_outofmem));
23908 }
23909 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023910 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023911
23912 if (reanimate)
23913 {
23914 /* The pending return value could be overwritten by a ":return"
23915 * without argument in a finally clause; reset the default
23916 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023917 current_funccal->rettv->v_type = VAR_NUMBER;
23918 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 }
23920 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023921 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 }
23923 else
23924 {
23925 current_funccal->returned = TRUE;
23926
23927 /* If the return is carried out now, store the return value. For
23928 * a return immediately after reanimation, the value is already
23929 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023930 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023932 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023933 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023934 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023935 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936 }
23937 }
23938
23939 return idx < 0;
23940}
23941
23942/*
23943 * Free the variable with a pending return value.
23944 */
23945 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023946discard_pending_return(rettv)
23947 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948{
Bram Moolenaar33570922005-01-25 22:26:29 +000023949 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950}
23951
23952/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023953 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 * is an allocated string. Used by report_pending() for verbose messages.
23955 */
23956 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023957get_return_cmd(rettv)
23958 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023960 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023961 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023962 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023964 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023965 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023966 if (s == NULL)
23967 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023968
23969 STRCPY(IObuff, ":return ");
23970 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23971 if (STRLEN(s) + 8 >= IOSIZE)
23972 STRCPY(IObuff + IOSIZE - 4, "...");
23973 vim_free(tofree);
23974 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975}
23976
23977/*
23978 * Get next function line.
23979 * Called by do_cmdline() to get the next line.
23980 * Returns allocated string, or NULL for end of function.
23981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982 char_u *
23983get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023984 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023986 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987{
Bram Moolenaar33570922005-01-25 22:26:29 +000023988 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023989 ufunc_T *fp = fcp->func;
23990 char_u *retval;
23991 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992
23993 /* If breakpoints have been added/deleted need to check for it. */
23994 if (fcp->dbg_tick != debug_tick)
23995 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023996 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 sourcing_lnum);
23998 fcp->dbg_tick = debug_tick;
23999 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024000#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024001 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024002 func_line_end(cookie);
24003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004
Bram Moolenaar05159a02005-02-26 23:04:13 +000024005 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024006 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24007 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 retval = NULL;
24009 else
24010 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024011 /* Skip NULL lines (continuation lines). */
24012 while (fcp->linenr < gap->ga_len
24013 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24014 ++fcp->linenr;
24015 if (fcp->linenr >= gap->ga_len)
24016 retval = NULL;
24017 else
24018 {
24019 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24020 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024021#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024022 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024023 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024024#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024026 }
24027
24028 /* Did we encounter a breakpoint? */
24029 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24030 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024031 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024033 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024034 sourcing_lnum);
24035 fcp->dbg_tick = debug_tick;
24036 }
24037
24038 return retval;
24039}
24040
Bram Moolenaar05159a02005-02-26 23:04:13 +000024041#if defined(FEAT_PROFILE) || defined(PROTO)
24042/*
24043 * Called when starting to read a function line.
24044 * "sourcing_lnum" must be correct!
24045 * When skipping lines it may not actually be executed, but we won't find out
24046 * until later and we need to store the time now.
24047 */
24048 void
24049func_line_start(cookie)
24050 void *cookie;
24051{
24052 funccall_T *fcp = (funccall_T *)cookie;
24053 ufunc_T *fp = fcp->func;
24054
24055 if (fp->uf_profiling && sourcing_lnum >= 1
24056 && sourcing_lnum <= fp->uf_lines.ga_len)
24057 {
24058 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024059 /* Skip continuation lines. */
24060 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24061 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024062 fp->uf_tml_execed = FALSE;
24063 profile_start(&fp->uf_tml_start);
24064 profile_zero(&fp->uf_tml_children);
24065 profile_get_wait(&fp->uf_tml_wait);
24066 }
24067}
24068
24069/*
24070 * Called when actually executing a function line.
24071 */
24072 void
24073func_line_exec(cookie)
24074 void *cookie;
24075{
24076 funccall_T *fcp = (funccall_T *)cookie;
24077 ufunc_T *fp = fcp->func;
24078
24079 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24080 fp->uf_tml_execed = TRUE;
24081}
24082
24083/*
24084 * Called when done with a function line.
24085 */
24086 void
24087func_line_end(cookie)
24088 void *cookie;
24089{
24090 funccall_T *fcp = (funccall_T *)cookie;
24091 ufunc_T *fp = fcp->func;
24092
24093 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24094 {
24095 if (fp->uf_tml_execed)
24096 {
24097 ++fp->uf_tml_count[fp->uf_tml_idx];
24098 profile_end(&fp->uf_tml_start);
24099 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024100 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024101 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24102 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024103 }
24104 fp->uf_tml_idx = -1;
24105 }
24106}
24107#endif
24108
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109/*
24110 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024111 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 */
24113 int
24114func_has_ended(cookie)
24115 void *cookie;
24116{
Bram Moolenaar33570922005-01-25 22:26:29 +000024117 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118
24119 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24120 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024121 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 || fcp->returned);
24123}
24124
24125/*
24126 * return TRUE if cookie indicates a function which "abort"s on errors.
24127 */
24128 int
24129func_has_abort(cookie)
24130 void *cookie;
24131{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024132 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133}
24134
24135#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24136typedef enum
24137{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024138 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24139 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24140 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141} var_flavour_T;
24142
24143static var_flavour_T var_flavour __ARGS((char_u *varname));
24144
24145 static var_flavour_T
24146var_flavour(varname)
24147 char_u *varname;
24148{
24149 char_u *p = varname;
24150
24151 if (ASCII_ISUPPER(*p))
24152 {
24153 while (*(++p))
24154 if (ASCII_ISLOWER(*p))
24155 return VAR_FLAVOUR_SESSION;
24156 return VAR_FLAVOUR_VIMINFO;
24157 }
24158 else
24159 return VAR_FLAVOUR_DEFAULT;
24160}
24161#endif
24162
24163#if defined(FEAT_VIMINFO) || defined(PROTO)
24164/*
24165 * Restore global vars that start with a capital from the viminfo file
24166 */
24167 int
24168read_viminfo_varlist(virp, writing)
24169 vir_T *virp;
24170 int writing;
24171{
24172 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024173 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024174 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175
24176 if (!writing && (find_viminfo_parameter('!') != NULL))
24177 {
24178 tab = vim_strchr(virp->vir_line + 1, '\t');
24179 if (tab != NULL)
24180 {
24181 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024182 switch (*tab)
24183 {
24184 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024185#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024186 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024187#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024188 case 'D': type = VAR_DICT; break;
24189 case 'L': type = VAR_LIST; break;
24190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191
24192 tab = vim_strchr(tab, '\t');
24193 if (tab != NULL)
24194 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024195 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024196 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024197 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024199#ifdef FEAT_FLOAT
24200 else if (type == VAR_FLOAT)
24201 (void)string2float(tab + 1, &tv.vval.v_float);
24202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024204 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024205 if (type == VAR_DICT || type == VAR_LIST)
24206 {
24207 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24208
24209 if (etv == NULL)
24210 /* Failed to parse back the dict or list, use it as a
24211 * string. */
24212 tv.v_type = VAR_STRING;
24213 else
24214 {
24215 vim_free(tv.vval.v_string);
24216 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024217 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024218 }
24219 }
24220
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024221 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024222
24223 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024224 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024225 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24226 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024227 }
24228 }
24229 }
24230
24231 return viminfo_readline(virp);
24232}
24233
24234/*
24235 * Write global vars that start with a capital to the viminfo file
24236 */
24237 void
24238write_viminfo_varlist(fp)
24239 FILE *fp;
24240{
Bram Moolenaar33570922005-01-25 22:26:29 +000024241 hashitem_T *hi;
24242 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024243 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024244 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024245 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024246 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024247 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024248
24249 if (find_viminfo_parameter('!') == NULL)
24250 return;
24251
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024252 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024253
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024254 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024255 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024256 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024257 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024259 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024260 this_var = HI2DI(hi);
24261 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024262 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024263 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024264 {
24265 case VAR_STRING: s = "STR"; break;
24266 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024267#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024268 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024269#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024270 case VAR_DICT: s = "DIC"; break;
24271 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024272 default: continue;
24273 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024274 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024275 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024276 if (p != NULL)
24277 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024278 vim_free(tofree);
24279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024280 }
24281 }
24282}
24283#endif
24284
24285#if defined(FEAT_SESSION) || defined(PROTO)
24286 int
24287store_session_globals(fd)
24288 FILE *fd;
24289{
Bram Moolenaar33570922005-01-25 22:26:29 +000024290 hashitem_T *hi;
24291 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024292 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 char_u *p, *t;
24294
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024295 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024296 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024298 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024300 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024301 this_var = HI2DI(hi);
24302 if ((this_var->di_tv.v_type == VAR_NUMBER
24303 || this_var->di_tv.v_type == VAR_STRING)
24304 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024305 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024306 /* Escape special characters with a backslash. Turn a LF and
24307 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024308 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024309 (char_u *)"\\\"\n\r");
24310 if (p == NULL) /* out of memory */
24311 break;
24312 for (t = p; *t != NUL; ++t)
24313 if (*t == '\n')
24314 *t = 'n';
24315 else if (*t == '\r')
24316 *t = 'r';
24317 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024318 this_var->di_key,
24319 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24320 : ' ',
24321 p,
24322 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24323 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024324 || put_eol(fd) == FAIL)
24325 {
24326 vim_free(p);
24327 return FAIL;
24328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024329 vim_free(p);
24330 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024331#ifdef FEAT_FLOAT
24332 else if (this_var->di_tv.v_type == VAR_FLOAT
24333 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24334 {
24335 float_T f = this_var->di_tv.vval.v_float;
24336 int sign = ' ';
24337
24338 if (f < 0)
24339 {
24340 f = -f;
24341 sign = '-';
24342 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024343 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024344 this_var->di_key, sign, f) < 0)
24345 || put_eol(fd) == FAIL)
24346 return FAIL;
24347 }
24348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 }
24350 }
24351 return OK;
24352}
24353#endif
24354
Bram Moolenaar661b1822005-07-28 22:36:45 +000024355/*
24356 * Display script name where an item was last set.
24357 * Should only be invoked when 'verbose' is non-zero.
24358 */
24359 void
24360last_set_msg(scriptID)
24361 scid_T scriptID;
24362{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024363 char_u *p;
24364
Bram Moolenaar661b1822005-07-28 22:36:45 +000024365 if (scriptID != 0)
24366 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024367 p = home_replace_save(NULL, get_scriptname(scriptID));
24368 if (p != NULL)
24369 {
24370 verbose_enter();
24371 MSG_PUTS(_("\n\tLast set from "));
24372 MSG_PUTS(p);
24373 vim_free(p);
24374 verbose_leave();
24375 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024376 }
24377}
24378
Bram Moolenaard812df62008-11-09 12:46:09 +000024379/*
24380 * List v:oldfiles in a nice way.
24381 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024382 void
24383ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024384 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024385{
24386 list_T *l = vimvars[VV_OLDFILES].vv_list;
24387 listitem_T *li;
24388 int nr = 0;
24389
24390 if (l == NULL)
24391 msg((char_u *)_("No old files"));
24392 else
24393 {
24394 msg_start();
24395 msg_scroll = TRUE;
24396 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24397 {
24398 msg_outnum((long)++nr);
24399 MSG_PUTS(": ");
24400 msg_outtrans(get_tv_string(&li->li_tv));
24401 msg_putchar('\n');
24402 out_flush(); /* output one line at a time */
24403 ui_breakcheck();
24404 }
24405 /* Assume "got_int" was set to truncate the listing. */
24406 got_int = FALSE;
24407
24408#ifdef FEAT_BROWSE_CMD
24409 if (cmdmod.browse)
24410 {
24411 quit_more = FALSE;
24412 nr = prompt_for_number(FALSE);
24413 msg_starthere();
24414 if (nr > 0)
24415 {
24416 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24417 (long)nr);
24418
24419 if (p != NULL)
24420 {
24421 p = expand_env_save(p);
24422 eap->arg = p;
24423 eap->cmdidx = CMD_edit;
24424 cmdmod.browse = FALSE;
24425 do_exedit(eap, NULL);
24426 vim_free(p);
24427 }
24428 }
24429 }
24430#endif
24431 }
24432}
24433
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434#endif /* FEAT_EVAL */
24435
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024437#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024438
24439#ifdef WIN3264
24440/*
24441 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24442 */
24443static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24444static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24445static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24446
24447/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024448 * Get the short path (8.3) for the filename in "fnamep".
24449 * Only works for a valid file name.
24450 * When the path gets longer "fnamep" is changed and the allocated buffer
24451 * is put in "bufp".
24452 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24453 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024454 */
24455 static int
24456get_short_pathname(fnamep, bufp, fnamelen)
24457 char_u **fnamep;
24458 char_u **bufp;
24459 int *fnamelen;
24460{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024461 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024462 char_u *newbuf;
24463
24464 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024465 l = GetShortPathName(*fnamep, *fnamep, len);
24466 if (l > len - 1)
24467 {
24468 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024469 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470 newbuf = vim_strnsave(*fnamep, l);
24471 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024472 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473
24474 vim_free(*bufp);
24475 *fnamep = *bufp = newbuf;
24476
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024477 /* Really should always succeed, as the buffer is big enough. */
24478 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024479 }
24480
24481 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024482 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483}
24484
24485/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024486 * Get the short path (8.3) for the filename in "fname". The converted
24487 * path is returned in "bufp".
24488 *
24489 * Some of the directories specified in "fname" may not exist. This function
24490 * will shorten the existing directories at the beginning of the path and then
24491 * append the remaining non-existing path.
24492 *
24493 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024494 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024495 * bufp - Pointer to an allocated buffer for the filename.
24496 * fnamelen - Length of the filename pointed to by fname
24497 *
24498 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499 */
24500 static int
24501shortpath_for_invalid_fname(fname, bufp, fnamelen)
24502 char_u **fname;
24503 char_u **bufp;
24504 int *fnamelen;
24505{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024506 char_u *short_fname, *save_fname, *pbuf_unused;
24507 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024508 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024509 int old_len, len;
24510 int new_len, sfx_len;
24511 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512
24513 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024514 old_len = *fnamelen;
24515 save_fname = vim_strnsave(*fname, old_len);
24516 pbuf_unused = NULL;
24517 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024519 endp = save_fname + old_len - 1; /* Find the end of the copy */
24520 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024521
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024522 /*
24523 * Try shortening the supplied path till it succeeds by removing one
24524 * directory at a time from the tail of the path.
24525 */
24526 len = 0;
24527 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024529 /* go back one path-separator */
24530 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24531 --endp;
24532 if (endp <= save_fname)
24533 break; /* processed the complete path */
24534
24535 /*
24536 * Replace the path separator with a NUL and try to shorten the
24537 * resulting path.
24538 */
24539 ch = *endp;
24540 *endp = 0;
24541 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024542 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024543 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24544 {
24545 retval = FAIL;
24546 goto theend;
24547 }
24548 *endp = ch; /* preserve the string */
24549
24550 if (len > 0)
24551 break; /* successfully shortened the path */
24552
24553 /* failed to shorten the path. Skip the path separator */
24554 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024555 }
24556
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024557 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024558 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024559 /*
24560 * Succeeded in shortening the path. Now concatenate the shortened
24561 * path with the remaining path at the tail.
24562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024563
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024564 /* Compute the length of the new path. */
24565 sfx_len = (int)(save_endp - endp) + 1;
24566 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024568 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024569 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024570 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024572 /* There is not enough space in the currently allocated string,
24573 * copy it to a buffer big enough. */
24574 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024576 {
24577 retval = FAIL;
24578 goto theend;
24579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024580 }
24581 else
24582 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024583 /* Transfer short_fname to the main buffer (it's big enough),
24584 * unless get_short_pathname() did its work in-place. */
24585 *fname = *bufp = save_fname;
24586 if (short_fname != save_fname)
24587 vim_strncpy(save_fname, short_fname, len);
24588 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024589 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024590
24591 /* concat the not-shortened part of the path */
24592 vim_strncpy(*fname + len, endp, sfx_len);
24593 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024594 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024595
24596theend:
24597 vim_free(pbuf_unused);
24598 vim_free(save_fname);
24599
24600 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601}
24602
24603/*
24604 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024605 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024606 */
24607 static int
24608shortpath_for_partial(fnamep, bufp, fnamelen)
24609 char_u **fnamep;
24610 char_u **bufp;
24611 int *fnamelen;
24612{
24613 int sepcount, len, tflen;
24614 char_u *p;
24615 char_u *pbuf, *tfname;
24616 int hasTilde;
24617
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024618 /* Count up the path separators from the RHS.. so we know which part
24619 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024621 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622 if (vim_ispathsep(*p))
24623 ++sepcount;
24624
24625 /* Need full path first (use expand_env() to remove a "~/") */
24626 hasTilde = (**fnamep == '~');
24627 if (hasTilde)
24628 pbuf = tfname = expand_env_save(*fnamep);
24629 else
24630 pbuf = tfname = FullName_save(*fnamep, FALSE);
24631
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024632 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024633
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024634 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636
24637 if (len == 0)
24638 {
24639 /* Don't have a valid filename, so shorten the rest of the
24640 * path if we can. This CAN give us invalid 8.3 filenames, but
24641 * there's not a lot of point in guessing what it might be.
24642 */
24643 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024644 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24645 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024646 }
24647
24648 /* Count the paths backward to find the beginning of the desired string. */
24649 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024650 {
24651#ifdef FEAT_MBYTE
24652 if (has_mbyte)
24653 p -= mb_head_off(tfname, p);
24654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 if (vim_ispathsep(*p))
24656 {
24657 if (sepcount == 0 || (hasTilde && sepcount == 1))
24658 break;
24659 else
24660 sepcount --;
24661 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024663 if (hasTilde)
24664 {
24665 --p;
24666 if (p >= tfname)
24667 *p = '~';
24668 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024669 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 }
24671 else
24672 ++p;
24673
24674 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24675 vim_free(*bufp);
24676 *fnamelen = (int)STRLEN(p);
24677 *bufp = pbuf;
24678 *fnamep = p;
24679
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024680 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024681}
24682#endif /* WIN3264 */
24683
24684/*
24685 * Adjust a filename, according to a string of modifiers.
24686 * *fnamep must be NUL terminated when called. When returning, the length is
24687 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024688 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 * When there is an error, *fnamep is set to NULL.
24690 */
24691 int
24692modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24693 char_u *src; /* string with modifiers */
24694 int *usedlen; /* characters after src that are used */
24695 char_u **fnamep; /* file name so far */
24696 char_u **bufp; /* buffer for allocated file name or NULL */
24697 int *fnamelen; /* length of fnamep */
24698{
24699 int valid = 0;
24700 char_u *tail;
24701 char_u *s, *p, *pbuf;
24702 char_u dirname[MAXPATHL];
24703 int c;
24704 int has_fullname = 0;
24705#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024706 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 int has_shortname = 0;
24708#endif
24709
24710repeat:
24711 /* ":p" - full path/file_name */
24712 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24713 {
24714 has_fullname = 1;
24715
24716 valid |= VALID_PATH;
24717 *usedlen += 2;
24718
24719 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24720 if ((*fnamep)[0] == '~'
24721#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24722 && ((*fnamep)[1] == '/'
24723# ifdef BACKSLASH_IN_FILENAME
24724 || (*fnamep)[1] == '\\'
24725# endif
24726 || (*fnamep)[1] == NUL)
24727
24728#endif
24729 )
24730 {
24731 *fnamep = expand_env_save(*fnamep);
24732 vim_free(*bufp); /* free any allocated file name */
24733 *bufp = *fnamep;
24734 if (*fnamep == NULL)
24735 return -1;
24736 }
24737
24738 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024739 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024740 {
24741 if (vim_ispathsep(*p)
24742 && p[1] == '.'
24743 && (p[2] == NUL
24744 || vim_ispathsep(p[2])
24745 || (p[2] == '.'
24746 && (p[3] == NUL || vim_ispathsep(p[3])))))
24747 break;
24748 }
24749
24750 /* FullName_save() is slow, don't use it when not needed. */
24751 if (*p != NUL || !vim_isAbsName(*fnamep))
24752 {
24753 *fnamep = FullName_save(*fnamep, *p != NUL);
24754 vim_free(*bufp); /* free any allocated file name */
24755 *bufp = *fnamep;
24756 if (*fnamep == NULL)
24757 return -1;
24758 }
24759
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024760#ifdef WIN3264
24761# if _WIN32_WINNT >= 0x0500
24762 if (vim_strchr(*fnamep, '~') != NULL)
24763 {
24764 /* Expand 8.3 filename to full path. Needed to make sure the same
24765 * file does not have two different names.
24766 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24767 p = alloc(_MAX_PATH + 1);
24768 if (p != NULL)
24769 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010024770 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024771 {
24772 vim_free(*bufp);
24773 *bufp = *fnamep = p;
24774 }
24775 else
24776 vim_free(p);
24777 }
24778 }
24779# endif
24780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781 /* Append a path separator to a directory. */
24782 if (mch_isdir(*fnamep))
24783 {
24784 /* Make room for one or two extra characters. */
24785 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24786 vim_free(*bufp); /* free any allocated file name */
24787 *bufp = *fnamep;
24788 if (*fnamep == NULL)
24789 return -1;
24790 add_pathsep(*fnamep);
24791 }
24792 }
24793
24794 /* ":." - path relative to the current directory */
24795 /* ":~" - path relative to the home directory */
24796 /* ":8" - shortname path - postponed till after */
24797 while (src[*usedlen] == ':'
24798 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24799 {
24800 *usedlen += 2;
24801 if (c == '8')
24802 {
24803#ifdef WIN3264
24804 has_shortname = 1; /* Postpone this. */
24805#endif
24806 continue;
24807 }
24808 pbuf = NULL;
24809 /* Need full path first (use expand_env() to remove a "~/") */
24810 if (!has_fullname)
24811 {
24812 if (c == '.' && **fnamep == '~')
24813 p = pbuf = expand_env_save(*fnamep);
24814 else
24815 p = pbuf = FullName_save(*fnamep, FALSE);
24816 }
24817 else
24818 p = *fnamep;
24819
24820 has_fullname = 0;
24821
24822 if (p != NULL)
24823 {
24824 if (c == '.')
24825 {
24826 mch_dirname(dirname, MAXPATHL);
24827 s = shorten_fname(p, dirname);
24828 if (s != NULL)
24829 {
24830 *fnamep = s;
24831 if (pbuf != NULL)
24832 {
24833 vim_free(*bufp); /* free any allocated file name */
24834 *bufp = pbuf;
24835 pbuf = NULL;
24836 }
24837 }
24838 }
24839 else
24840 {
24841 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24842 /* Only replace it when it starts with '~' */
24843 if (*dirname == '~')
24844 {
24845 s = vim_strsave(dirname);
24846 if (s != NULL)
24847 {
24848 *fnamep = s;
24849 vim_free(*bufp);
24850 *bufp = s;
24851 }
24852 }
24853 }
24854 vim_free(pbuf);
24855 }
24856 }
24857
24858 tail = gettail(*fnamep);
24859 *fnamelen = (int)STRLEN(*fnamep);
24860
24861 /* ":h" - head, remove "/file_name", can be repeated */
24862 /* Don't remove the first "/" or "c:\" */
24863 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24864 {
24865 valid |= VALID_HEAD;
24866 *usedlen += 2;
24867 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024868 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024869 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870 *fnamelen = (int)(tail - *fnamep);
24871#ifdef VMS
24872 if (*fnamelen > 0)
24873 *fnamelen += 1; /* the path separator is part of the path */
24874#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024875 if (*fnamelen == 0)
24876 {
24877 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24878 p = vim_strsave((char_u *)".");
24879 if (p == NULL)
24880 return -1;
24881 vim_free(*bufp);
24882 *bufp = *fnamep = tail = p;
24883 *fnamelen = 1;
24884 }
24885 else
24886 {
24887 while (tail > s && !after_pathsep(s, tail))
24888 mb_ptr_back(*fnamep, tail);
24889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024890 }
24891
24892 /* ":8" - shortname */
24893 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24894 {
24895 *usedlen += 2;
24896#ifdef WIN3264
24897 has_shortname = 1;
24898#endif
24899 }
24900
24901#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024902 /*
24903 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024904 */
24905 if (has_shortname)
24906 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024907 /* Copy the string if it is shortened by :h and when it wasn't copied
24908 * yet, because we are going to change it in place. Avoids changing
24909 * the buffer name for "%:8". */
24910 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024911 {
24912 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024913 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024914 return -1;
24915 vim_free(*bufp);
24916 *bufp = *fnamep = p;
24917 }
24918
24919 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024920 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024921 if (!has_fullname && !vim_isAbsName(*fnamep))
24922 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024923 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924 return -1;
24925 }
24926 else
24927 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024928 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929
Bram Moolenaardc935552011-08-17 15:23:23 +020024930 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024932 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933 return -1;
24934
24935 if (l == 0)
24936 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024937 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024939 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024940 return -1;
24941 }
24942 *fnamelen = l;
24943 }
24944 }
24945#endif /* WIN3264 */
24946
24947 /* ":t" - tail, just the basename */
24948 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24949 {
24950 *usedlen += 2;
24951 *fnamelen -= (int)(tail - *fnamep);
24952 *fnamep = tail;
24953 }
24954
24955 /* ":e" - extension, can be repeated */
24956 /* ":r" - root, without extension, can be repeated */
24957 while (src[*usedlen] == ':'
24958 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24959 {
24960 /* find a '.' in the tail:
24961 * - for second :e: before the current fname
24962 * - otherwise: The last '.'
24963 */
24964 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24965 s = *fnamep - 2;
24966 else
24967 s = *fnamep + *fnamelen - 1;
24968 for ( ; s > tail; --s)
24969 if (s[0] == '.')
24970 break;
24971 if (src[*usedlen + 1] == 'e') /* :e */
24972 {
24973 if (s > tail)
24974 {
24975 *fnamelen += (int)(*fnamep - (s + 1));
24976 *fnamep = s + 1;
24977#ifdef VMS
24978 /* cut version from the extension */
24979 s = *fnamep + *fnamelen - 1;
24980 for ( ; s > *fnamep; --s)
24981 if (s[0] == ';')
24982 break;
24983 if (s > *fnamep)
24984 *fnamelen = s - *fnamep;
24985#endif
24986 }
24987 else if (*fnamep <= tail)
24988 *fnamelen = 0;
24989 }
24990 else /* :r */
24991 {
24992 if (s > tail) /* remove one extension */
24993 *fnamelen = (int)(s - *fnamep);
24994 }
24995 *usedlen += 2;
24996 }
24997
24998 /* ":s?pat?foo?" - substitute */
24999 /* ":gs?pat?foo?" - global substitute */
25000 if (src[*usedlen] == ':'
25001 && (src[*usedlen + 1] == 's'
25002 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25003 {
25004 char_u *str;
25005 char_u *pat;
25006 char_u *sub;
25007 int sep;
25008 char_u *flags;
25009 int didit = FALSE;
25010
25011 flags = (char_u *)"";
25012 s = src + *usedlen + 2;
25013 if (src[*usedlen + 1] == 'g')
25014 {
25015 flags = (char_u *)"g";
25016 ++s;
25017 }
25018
25019 sep = *s++;
25020 if (sep)
25021 {
25022 /* find end of pattern */
25023 p = vim_strchr(s, sep);
25024 if (p != NULL)
25025 {
25026 pat = vim_strnsave(s, (int)(p - s));
25027 if (pat != NULL)
25028 {
25029 s = p + 1;
25030 /* find end of substitution */
25031 p = vim_strchr(s, sep);
25032 if (p != NULL)
25033 {
25034 sub = vim_strnsave(s, (int)(p - s));
25035 str = vim_strnsave(*fnamep, *fnamelen);
25036 if (sub != NULL && str != NULL)
25037 {
25038 *usedlen = (int)(p + 1 - src);
25039 s = do_string_sub(str, pat, sub, flags);
25040 if (s != NULL)
25041 {
25042 *fnamep = s;
25043 *fnamelen = (int)STRLEN(s);
25044 vim_free(*bufp);
25045 *bufp = s;
25046 didit = TRUE;
25047 }
25048 }
25049 vim_free(sub);
25050 vim_free(str);
25051 }
25052 vim_free(pat);
25053 }
25054 }
25055 /* after using ":s", repeat all the modifiers */
25056 if (didit)
25057 goto repeat;
25058 }
25059 }
25060
Bram Moolenaar26df0922014-02-23 23:39:13 +010025061 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25062 {
25063 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25064 if (p == NULL)
25065 return -1;
25066 vim_free(*bufp);
25067 *bufp = *fnamep = p;
25068 *fnamelen = (int)STRLEN(p);
25069 *usedlen += 2;
25070 }
25071
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072 return valid;
25073}
25074
25075/*
25076 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25077 * "flags" can be "g" to do a global substitute.
25078 * Returns an allocated string, NULL for error.
25079 */
25080 char_u *
25081do_string_sub(str, pat, sub, flags)
25082 char_u *str;
25083 char_u *pat;
25084 char_u *sub;
25085 char_u *flags;
25086{
25087 int sublen;
25088 regmatch_T regmatch;
25089 int i;
25090 int do_all;
25091 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025092 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 garray_T ga;
25094 char_u *ret;
25095 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025096 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097
25098 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25099 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025100 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101
25102 ga_init2(&ga, 1, 200);
25103
25104 do_all = (flags[0] == 'g');
25105
25106 regmatch.rm_ic = p_ic;
25107 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25108 if (regmatch.regprog != NULL)
25109 {
25110 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025111 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25113 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025114 /* Skip empty match except for first match. */
25115 if (regmatch.startp[0] == regmatch.endp[0])
25116 {
25117 if (zero_width == regmatch.startp[0])
25118 {
25119 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025120 i = MB_PTR2LEN(tail);
25121 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25122 (size_t)i);
25123 ga.ga_len += i;
25124 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025125 continue;
25126 }
25127 zero_width = regmatch.startp[0];
25128 }
25129
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 /*
25131 * Get some space for a temporary buffer to do the substitution
25132 * into. It will contain:
25133 * - The text up to where the match is.
25134 * - The substituted text.
25135 * - The text after the match.
25136 */
25137 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025138 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25140 {
25141 ga_clear(&ga);
25142 break;
25143 }
25144
25145 /* copy the text up to where the match is */
25146 i = (int)(regmatch.startp[0] - tail);
25147 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25148 /* add the substituted text */
25149 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25150 + ga.ga_len + i, TRUE, TRUE, FALSE);
25151 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025152 tail = regmatch.endp[0];
25153 if (*tail == NUL)
25154 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155 if (!do_all)
25156 break;
25157 }
25158
25159 if (ga.ga_data != NULL)
25160 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25161
Bram Moolenaar473de612013-06-08 18:19:48 +020025162 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025163 }
25164
25165 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25166 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025167 if (p_cpo == empty_option)
25168 p_cpo = save_cpo;
25169 else
25170 /* Darn, evaluating {sub} expression changed the value. */
25171 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025172
25173 return ret;
25174}
25175
25176#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */