blob: c9c179a051d6ca9a274d1eeecc6335b4f7bba20e [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.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006010 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006012 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013listitem_alloc()
6014{
Bram Moolenaar33570922005-01-25 22:26:29 +00006015 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016}
6017
6018/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006019 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006021 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006025 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 vim_free(item);
6027}
6028
6029/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006030 * Remove a list item from a List and free it. Also clears the value.
6031 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006032 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006033listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006034 list_T *l;
6035 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006036{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006037 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006038 listitem_free(item);
6039}
6040
6041/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 * Get the number of items in a list.
6043 */
6044 static long
6045list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 if (l == NULL)
6049 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006050 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051}
6052
6053/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006054 * Return TRUE when two lists have exactly the same values.
6055 */
6056 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006057list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 list_T *l1;
6059 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006060 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006061 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006062{
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006064
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006065 if (l1 == NULL || l2 == NULL)
6066 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006067 if (l1 == l2)
6068 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006069 if (list_len(l1) != list_len(l2))
6070 return FALSE;
6071
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006072 for (item1 = l1->lv_first, item2 = l2->lv_first;
6073 item1 != NULL && item2 != NULL;
6074 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006075 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076 return FALSE;
6077 return item1 == NULL && item2 == NULL;
6078}
6079
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006080#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6081 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006082/*
6083 * Return the dictitem that an entry in a hashtable points to.
6084 */
6085 dictitem_T *
6086dict_lookup(hi)
6087 hashitem_T *hi;
6088{
6089 return HI2DI(hi);
6090}
6091#endif
6092
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006094 * Return TRUE when two dictionaries have exactly the same key/values.
6095 */
6096 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006097dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 dict_T *d1;
6099 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006100 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006101 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006102{
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 hashitem_T *hi;
6104 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006105 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006106
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006107 if (d1 == NULL || d2 == NULL)
6108 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006109 if (d1 == d2)
6110 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006111 if (dict_len(d1) != dict_len(d2))
6112 return FALSE;
6113
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006114 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006115 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006116 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006117 if (!HASHITEM_EMPTY(hi))
6118 {
6119 item2 = dict_find(d2, hi->hi_key, -1);
6120 if (item2 == NULL)
6121 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006122 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006123 return FALSE;
6124 --todo;
6125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006126 }
6127 return TRUE;
6128}
6129
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006130static int tv_equal_recurse_limit;
6131
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006132/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006134 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006135 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136 */
6137 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006138tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006139 typval_T *tv1;
6140 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006141 int ic; /* ignore case */
6142 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143{
6144 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006145 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006147 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006148
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006149 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006150 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006151
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006152 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006153 * recursiveness to a limit. We guess they are equal then.
6154 * A fixed limit has the problem of still taking an awful long time.
6155 * Reduce the limit every time running into it. That should work fine for
6156 * deeply linked structures that are not recursively linked and catch
6157 * recursiveness quickly. */
6158 if (!recursive)
6159 tv_equal_recurse_limit = 1000;
6160 if (recursive_cnt >= tv_equal_recurse_limit)
6161 {
6162 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006163 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006164 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006165
6166 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006168 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006169 ++recursive_cnt;
6170 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6171 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006172 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006173
6174 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006175 ++recursive_cnt;
6176 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6177 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006178 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006179
6180 case VAR_FUNC:
6181 return (tv1->vval.v_string != NULL
6182 && tv2->vval.v_string != NULL
6183 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6184
6185 case VAR_NUMBER:
6186 return tv1->vval.v_number == tv2->vval.v_number;
6187
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006188#ifdef FEAT_FLOAT
6189 case VAR_FLOAT:
6190 return tv1->vval.v_float == tv2->vval.v_float;
6191#endif
6192
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193 case VAR_STRING:
6194 s1 = get_tv_string_buf(tv1, buf1);
6195 s2 = get_tv_string_buf(tv2, buf2);
6196 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006198
6199 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006200 return TRUE;
6201}
6202
6203/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006204 * Locate item with index "n" in list "l" and return it.
6205 * A negative index is counted from the end; -1 is the last item.
6206 * Returns NULL when "n" is out of range.
6207 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006208 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006209list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006210 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006211 long n;
6212{
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006214 long idx;
6215
6216 if (l == NULL)
6217 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006218
6219 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006221 n = l->lv_len + n;
6222
6223 /* Check for index out of range. */
6224 if (n < 0 || n >= l->lv_len)
6225 return NULL;
6226
6227 /* When there is a cached index may start search from there. */
6228 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006229 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006230 if (n < l->lv_idx / 2)
6231 {
6232 /* closest to the start of the list */
6233 item = l->lv_first;
6234 idx = 0;
6235 }
6236 else if (n > (l->lv_idx + l->lv_len) / 2)
6237 {
6238 /* closest to the end of the list */
6239 item = l->lv_last;
6240 idx = l->lv_len - 1;
6241 }
6242 else
6243 {
6244 /* closest to the cached index */
6245 item = l->lv_idx_item;
6246 idx = l->lv_idx;
6247 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006248 }
6249 else
6250 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006251 if (n < l->lv_len / 2)
6252 {
6253 /* closest to the start of the list */
6254 item = l->lv_first;
6255 idx = 0;
6256 }
6257 else
6258 {
6259 /* closest to the end of the list */
6260 item = l->lv_last;
6261 idx = l->lv_len - 1;
6262 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006264
6265 while (n > idx)
6266 {
6267 /* search forward */
6268 item = item->li_next;
6269 ++idx;
6270 }
6271 while (n < idx)
6272 {
6273 /* search backward */
6274 item = item->li_prev;
6275 --idx;
6276 }
6277
6278 /* cache the used index */
6279 l->lv_idx = idx;
6280 l->lv_idx_item = item;
6281
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006282 return item;
6283}
6284
6285/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006286 * Get list item "l[idx]" as a number.
6287 */
6288 static long
6289list_find_nr(l, idx, errorp)
6290 list_T *l;
6291 long idx;
6292 int *errorp; /* set to TRUE when something wrong */
6293{
6294 listitem_T *li;
6295
6296 li = list_find(l, idx);
6297 if (li == NULL)
6298 {
6299 if (errorp != NULL)
6300 *errorp = TRUE;
6301 return -1L;
6302 }
6303 return get_tv_number_chk(&li->li_tv, errorp);
6304}
6305
6306/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006307 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6308 */
6309 char_u *
6310list_find_str(l, idx)
6311 list_T *l;
6312 long idx;
6313{
6314 listitem_T *li;
6315
6316 li = list_find(l, idx - 1);
6317 if (li == NULL)
6318 {
6319 EMSGN(_(e_listidx), idx);
6320 return NULL;
6321 }
6322 return get_tv_string(&li->li_tv);
6323}
6324
6325/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006326 * Locate "item" list "l" and return its index.
6327 * Returns -1 when "item" is not in the list.
6328 */
6329 static long
6330list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 list_T *l;
6332 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006333{
6334 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006335 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006336
6337 if (l == NULL)
6338 return -1;
6339 idx = 0;
6340 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6341 ++idx;
6342 if (li == NULL)
6343 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006344 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006345}
6346
6347/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 * Append item "item" to the end of list "l".
6349 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006350 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 list_T *l;
6353 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354{
6355 if (l->lv_last == NULL)
6356 {
6357 /* empty list */
6358 l->lv_first = item;
6359 l->lv_last = item;
6360 item->li_prev = NULL;
6361 }
6362 else
6363 {
6364 l->lv_last->li_next = item;
6365 item->li_prev = l->lv_last;
6366 l->lv_last = item;
6367 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006368 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006369 item->li_next = NULL;
6370}
6371
6372/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006373 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006374 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006376 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006378 list_T *l;
6379 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006380{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006381 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382
Bram Moolenaar05159a02005-02-26 23:04:13 +00006383 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006384 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006385 copy_tv(tv, &li->li_tv);
6386 list_append(l, li);
6387 return OK;
6388}
6389
6390/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006391 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006392 * Return FAIL when out of memory.
6393 */
6394 int
6395list_append_dict(list, dict)
6396 list_T *list;
6397 dict_T *dict;
6398{
6399 listitem_T *li = listitem_alloc();
6400
6401 if (li == NULL)
6402 return FAIL;
6403 li->li_tv.v_type = VAR_DICT;
6404 li->li_tv.v_lock = 0;
6405 li->li_tv.vval.v_dict = dict;
6406 list_append(list, li);
6407 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408 return OK;
6409}
6410
6411/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006412 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006413 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006414 * Returns FAIL when out of memory.
6415 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006416 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006417list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006418 list_T *l;
6419 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006420 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006421{
6422 listitem_T *li = listitem_alloc();
6423
6424 if (li == NULL)
6425 return FAIL;
6426 list_append(l, li);
6427 li->li_tv.v_type = VAR_STRING;
6428 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006429 if (str == NULL)
6430 li->li_tv.vval.v_string = NULL;
6431 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006432 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006433 return FAIL;
6434 return OK;
6435}
6436
6437/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006438 * Append "n" to list "l".
6439 * Returns FAIL when out of memory.
6440 */
6441 static int
6442list_append_number(l, n)
6443 list_T *l;
6444 varnumber_T n;
6445{
6446 listitem_T *li;
6447
6448 li = listitem_alloc();
6449 if (li == NULL)
6450 return FAIL;
6451 li->li_tv.v_type = VAR_NUMBER;
6452 li->li_tv.v_lock = 0;
6453 li->li_tv.vval.v_number = n;
6454 list_append(l, li);
6455 return OK;
6456}
6457
6458/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 * If "item" is NULL append at the end.
6461 * Return FAIL when out of memory.
6462 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006463 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 list_T *l;
6466 typval_T *tv;
6467 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468{
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470
6471 if (ni == NULL)
6472 return FAIL;
6473 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006474 list_insert(l, ni, item);
6475 return OK;
6476}
6477
6478 void
6479list_insert(l, ni, item)
6480 list_T *l;
6481 listitem_T *ni;
6482 listitem_T *item;
6483{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006484 if (item == NULL)
6485 /* Append new item at end of list. */
6486 list_append(l, ni);
6487 else
6488 {
6489 /* Insert new item before existing item. */
6490 ni->li_prev = item->li_prev;
6491 ni->li_next = item;
6492 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006493 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006495 ++l->lv_idx;
6496 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006497 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006498 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006500 l->lv_idx_item = NULL;
6501 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006503 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006504 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505}
6506
6507/*
6508 * Extend "l1" with "l2".
6509 * If "bef" is NULL append at the end, otherwise insert before this item.
6510 * Returns FAIL when out of memory.
6511 */
6512 static int
6513list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 list_T *l1;
6515 list_T *l2;
6516 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006517{
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006519 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006521 /* We also quit the loop when we have inserted the original item count of
6522 * the list, avoid a hang when we extend a list with itself. */
6523 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6525 return FAIL;
6526 return OK;
6527}
6528
6529/*
6530 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6531 * Return FAIL when out of memory.
6532 */
6533 static int
6534list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006535 list_T *l1;
6536 list_T *l2;
6537 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538{
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006541 if (l1 == NULL || l2 == NULL)
6542 return FAIL;
6543
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 if (l == NULL)
6547 return FAIL;
6548 tv->v_type = VAR_LIST;
6549 tv->vval.v_list = l;
6550
6551 /* append all items from the second list */
6552 return list_extend(l, l2, NULL);
6553}
6554
6555/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006556 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006558 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006559 * Returns NULL when out of memory.
6560 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006562list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 list_T *copy;
6568 listitem_T *item;
6569 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570
6571 if (orig == NULL)
6572 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006573
6574 copy = list_alloc();
6575 if (copy != NULL)
6576 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006577 if (copyID != 0)
6578 {
6579 /* Do this before adding the items, because one of the items may
6580 * refer back to this list. */
6581 orig->lv_copyID = copyID;
6582 orig->lv_copylist = copy;
6583 }
6584 for (item = orig->lv_first; item != NULL && !got_int;
6585 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 {
6587 ni = listitem_alloc();
6588 if (ni == NULL)
6589 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006590 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 {
6592 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6593 {
6594 vim_free(ni);
6595 break;
6596 }
6597 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006599 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600 list_append(copy, ni);
6601 }
6602 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006603 if (item != NULL)
6604 {
6605 list_unref(copy);
6606 copy = NULL;
6607 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006608 }
6609
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006610 return copy;
6611}
6612
6613/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006614 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006615 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006616 * This used to be called list_remove, but that conflicts with a Sun header
6617 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006619 void
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006620vimlist_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006621 list_T *l;
6622 listitem_T *item;
6623 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624{
Bram Moolenaar33570922005-01-25 22:26:29 +00006625 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006627 /* notify watchers */
6628 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006630 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 list_fix_watch(l, ip);
6632 if (ip == item2)
6633 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006634 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635
6636 if (item2->li_next == NULL)
6637 l->lv_last = item->li_prev;
6638 else
6639 item2->li_next->li_prev = item->li_prev;
6640 if (item->li_prev == NULL)
6641 l->lv_first = item2->li_next;
6642 else
6643 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006644 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645}
6646
6647/*
6648 * Return an allocated string with the string representation of a list.
6649 * May return NULL.
6650 */
6651 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006652list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006653 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006654 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006655{
6656 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657
6658 if (tv->vval.v_list == NULL)
6659 return NULL;
6660 ga_init2(&ga, (int)sizeof(char), 80);
6661 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006662 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006663 {
6664 vim_free(ga.ga_data);
6665 return NULL;
6666 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 ga_append(&ga, ']');
6668 ga_append(&ga, NUL);
6669 return (char_u *)ga.ga_data;
6670}
6671
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006672typedef struct join_S {
6673 char_u *s;
6674 char_u *tofree;
6675} join_T;
6676
6677 static int
6678list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6679 garray_T *gap; /* to store the result in */
6680 list_T *l;
6681 char_u *sep;
6682 int echo_style;
6683 int copyID;
6684 garray_T *join_gap; /* to keep each list item string */
6685{
6686 int i;
6687 join_T *p;
6688 int len;
6689 int sumlen = 0;
6690 int first = TRUE;
6691 char_u *tofree;
6692 char_u numbuf[NUMBUFLEN];
6693 listitem_T *item;
6694 char_u *s;
6695
6696 /* Stringify each item in the list. */
6697 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6698 {
6699 if (echo_style)
6700 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6701 else
6702 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6703 if (s == NULL)
6704 return FAIL;
6705
6706 len = (int)STRLEN(s);
6707 sumlen += len;
6708
6709 ga_grow(join_gap, 1);
6710 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6711 if (tofree != NULL || s != numbuf)
6712 {
6713 p->s = s;
6714 p->tofree = tofree;
6715 }
6716 else
6717 {
6718 p->s = vim_strnsave(s, len);
6719 p->tofree = p->s;
6720 }
6721
6722 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006723 if (did_echo_string_emsg) /* recursion error, bail out */
6724 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006725 }
6726
6727 /* Allocate result buffer with its total size, avoid re-allocation and
6728 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6729 if (join_gap->ga_len >= 2)
6730 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6731 if (ga_grow(gap, sumlen + 2) == FAIL)
6732 return FAIL;
6733
6734 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6735 {
6736 if (first)
6737 first = FALSE;
6738 else
6739 ga_concat(gap, sep);
6740 p = ((join_T *)join_gap->ga_data) + i;
6741
6742 if (p->s != NULL)
6743 ga_concat(gap, p->s);
6744 line_breakcheck();
6745 }
6746
6747 return OK;
6748}
6749
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006750/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006751 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006752 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006753 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006754 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006755 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006756list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006757 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006758 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006759 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006760 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006761 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006762{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006763 garray_T join_ga;
6764 int retval;
6765 join_T *p;
6766 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006767
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006768 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6769 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6770
6771 /* Dispose each item in join_ga. */
6772 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006774 p = (join_T *)join_ga.ga_data;
6775 for (i = 0; i < join_ga.ga_len; ++i)
6776 {
6777 vim_free(p->tofree);
6778 ++p;
6779 }
6780 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006782
6783 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784}
6785
6786/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787 * Garbage collection for lists and dictionaries.
6788 *
6789 * We use reference counts to be able to free most items right away when they
6790 * are no longer used. But for composite items it's possible that it becomes
6791 * unused while the reference count is > 0: When there is a recursive
6792 * reference. Example:
6793 * :let l = [1, 2, 3]
6794 * :let d = {9: l}
6795 * :let l[1] = d
6796 *
6797 * Since this is quite unusual we handle this with garbage collection: every
6798 * once in a while find out which lists and dicts are not referenced from any
6799 * variable.
6800 *
6801 * Here is a good reference text about garbage collection (refers to Python
6802 * but it applies to all reference-counting mechanisms):
6803 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006804 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006805
6806/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807 * Do garbage collection for lists and dicts.
6808 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006809 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006810 int
6811garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006812{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006813 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006814 buf_T *buf;
6815 win_T *wp;
6816 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006817 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006818 int did_free;
6819 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006820#ifdef FEAT_WINDOWS
6821 tabpage_T *tp;
6822#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006823
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006824 /* Only do this once. */
6825 want_garbage_collect = FALSE;
6826 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006827 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006828
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006829 /* We advance by two because we add one for items referenced through
6830 * previous_funccal. */
6831 current_copyID += COPYID_INC;
6832 copyID = current_copyID;
6833
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 /*
6835 * 1. Go through all accessible variables and mark all lists and dicts
6836 * with copyID.
6837 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006838
6839 /* Don't free variables in the previous_funccal list unless they are only
6840 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006841 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006842 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6843 {
6844 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6845 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6846 }
6847
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848 /* script-local variables */
6849 for (i = 1; i <= ga_scripts.ga_len; ++i)
6850 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6851
6852 /* buffer-local variables */
6853 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006854 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006855
6856 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006857 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006858 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006859#ifdef FEAT_AUTOCMD
6860 if (aucmd_win != NULL)
6861 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6862#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006863
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006864#ifdef FEAT_WINDOWS
6865 /* tabpage-local variables */
6866 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006867 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006868#endif
6869
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870 /* global variables */
6871 set_ref_in_ht(&globvarht, copyID);
6872
6873 /* function-local variables */
6874 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6875 {
6876 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6877 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6878 }
6879
Bram Moolenaard812df62008-11-09 12:46:09 +00006880 /* v: vars */
6881 set_ref_in_ht(&vimvarht, copyID);
6882
Bram Moolenaar1dced572012-04-05 16:54:08 +02006883#ifdef FEAT_LUA
6884 set_ref_in_lua(copyID);
6885#endif
6886
Bram Moolenaardb913952012-06-29 12:54:53 +02006887#ifdef FEAT_PYTHON
6888 set_ref_in_python(copyID);
6889#endif
6890
6891#ifdef FEAT_PYTHON3
6892 set_ref_in_python3(copyID);
6893#endif
6894
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006895 /*
6896 * 2. Free lists and dictionaries that are not referenced.
6897 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006898 did_free = free_unref_items(copyID);
6899
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006900 /*
6901 * 3. Check if any funccal can be freed now.
6902 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006903 for (pfc = &previous_funccal; *pfc != NULL; )
6904 {
6905 if (can_free_funccal(*pfc, copyID))
6906 {
6907 fc = *pfc;
6908 *pfc = fc->caller;
6909 free_funccal(fc, TRUE);
6910 did_free = TRUE;
6911 did_free_funccal = TRUE;
6912 }
6913 else
6914 pfc = &(*pfc)->caller;
6915 }
6916 if (did_free_funccal)
6917 /* When a funccal was freed some more items might be garbage
6918 * collected, so run again. */
6919 (void)garbage_collect();
6920
6921 return did_free;
6922}
6923
6924/*
6925 * Free lists and dictionaries that are no longer referenced.
6926 */
6927 static int
6928free_unref_items(copyID)
6929 int copyID;
6930{
6931 dict_T *dd;
6932 list_T *ll;
6933 int did_free = FALSE;
6934
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006936 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937 */
6938 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006939 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006941 /* Free the Dictionary and ordinary items it contains, but don't
6942 * recurse into Lists and Dictionaries, they will be in the list
6943 * of dicts or list of lists. */
6944 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 did_free = TRUE;
6946
6947 /* restart, next dict may also have been freed */
6948 dd = first_dict;
6949 }
6950 else
6951 dd = dd->dv_used_next;
6952
6953 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 * Go through the list of lists and free items without the copyID.
6955 * But don't free a list that has a watcher (used in a for loop), these
6956 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 */
6958 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6960 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006962 /* Free the List and ordinary items it contains, but don't recurse
6963 * into Lists and Dictionaries, they will be in the list of dicts
6964 * or list of lists. */
6965 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006966 did_free = TRUE;
6967
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006968 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 ll = first_list;
6970 }
6971 else
6972 ll = ll->lv_used_next;
6973
6974 return did_free;
6975}
6976
6977/*
6978 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6979 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006980 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006981set_ref_in_ht(ht, copyID)
6982 hashtab_T *ht;
6983 int copyID;
6984{
6985 int todo;
6986 hashitem_T *hi;
6987
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006988 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 for (hi = ht->ht_array; todo > 0; ++hi)
6990 if (!HASHITEM_EMPTY(hi))
6991 {
6992 --todo;
6993 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6994 }
6995}
6996
6997/*
6998 * Mark all lists and dicts referenced through list "l" with "copyID".
6999 */
Bram Moolenaardb913952012-06-29 12:54:53 +02007000 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001set_ref_in_list(l, copyID)
7002 list_T *l;
7003 int copyID;
7004{
7005 listitem_T *li;
7006
7007 for (li = l->lv_first; li != NULL; li = li->li_next)
7008 set_ref_in_item(&li->li_tv, copyID);
7009}
7010
7011/*
7012 * Mark all lists and dicts referenced through typval "tv" with "copyID".
7013 */
Bram Moolenaardb913952012-06-29 12:54:53 +02007014 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015set_ref_in_item(tv, copyID)
7016 typval_T *tv;
7017 int copyID;
7018{
7019 dict_T *dd;
7020 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007021
7022 switch (tv->v_type)
7023 {
7024 case VAR_DICT:
7025 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00007026 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007027 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028 /* Didn't see this dict yet. */
7029 dd->dv_copyID = copyID;
7030 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007031 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007033
7034 case VAR_LIST:
7035 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00007036 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007037 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 /* Didn't see this list yet. */
7039 ll->lv_copyID = copyID;
7040 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007041 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007042 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007043 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007045}
7046
7047/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007048 * Allocate an empty header for a dictionary.
7049 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007050 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051dict_alloc()
7052{
Bram Moolenaar33570922005-01-25 22:26:29 +00007053 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007054
Bram Moolenaar33570922005-01-25 22:26:29 +00007055 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007056 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007057 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007058 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007059 if (first_dict != NULL)
7060 first_dict->dv_used_prev = d;
7061 d->dv_used_next = first_dict;
7062 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007063 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064
Bram Moolenaar33570922005-01-25 22:26:29 +00007065 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007066 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007067 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007068 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007069 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007070 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007072}
7073
7074/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007075 * Allocate an empty dict for a return value.
7076 * Returns OK or FAIL.
7077 */
7078 static int
7079rettv_dict_alloc(rettv)
7080 typval_T *rettv;
7081{
7082 dict_T *d = dict_alloc();
7083
7084 if (d == NULL)
7085 return FAIL;
7086
7087 rettv->vval.v_dict = d;
7088 rettv->v_type = VAR_DICT;
7089 ++d->dv_refcount;
7090 return OK;
7091}
7092
7093
7094/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007095 * Unreference a Dictionary: decrement the reference count and free it when it
7096 * becomes zero.
7097 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007098 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007099dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007100 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007101{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007102 if (d != NULL && --d->dv_refcount <= 0)
7103 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104}
7105
7106/*
7107 * Free a Dictionary, including all items it contains.
7108 * Ignores the reference count.
7109 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007110 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007111dict_free(d, recurse)
7112 dict_T *d;
7113 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007114{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007116 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007117 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119 /* Remove the dict from the list of dicts for garbage collection. */
7120 if (d->dv_used_prev == NULL)
7121 first_dict = d->dv_used_next;
7122 else
7123 d->dv_used_prev->dv_used_next = d->dv_used_next;
7124 if (d->dv_used_next != NULL)
7125 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7126
7127 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007128 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007129 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 if (!HASHITEM_EMPTY(hi))
7133 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007134 /* Remove the item before deleting it, just in case there is
7135 * something recursive causing trouble. */
7136 di = HI2DI(hi);
7137 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007138 if (recurse || (di->di_tv.v_type != VAR_LIST
7139 && di->di_tv.v_type != VAR_DICT))
7140 clear_tv(&di->di_tv);
7141 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007142 --todo;
7143 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146 vim_free(d);
7147}
7148
7149/*
7150 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 * The "key" is copied to the new item.
7152 * Note that the value of the item "di_tv" still needs to be initialized!
7153 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007155 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007156dictitem_alloc(key)
7157 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007158{
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007160
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007161 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007163 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007164 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007165 di->di_flags = 0;
7166 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007168}
7169
7170/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007171 * Make a copy of a Dictionary item.
7172 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007173 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007174dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007176{
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007179 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7180 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 if (di != NULL)
7182 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007184 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185 copy_tv(&org->di_tv, &di->di_tv);
7186 }
7187 return di;
7188}
7189
7190/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007191 * Remove item "item" from Dictionary "dict" and free it.
7192 */
7193 static void
7194dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007195 dict_T *dict;
7196 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007197{
Bram Moolenaar33570922005-01-25 22:26:29 +00007198 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007201 if (HASHITEM_EMPTY(hi))
7202 EMSG2(_(e_intern2), "dictitem_remove()");
7203 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007204 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205 dictitem_free(item);
7206}
7207
7208/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209 * Free a dict item. Also clears the value.
7210 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007211 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 clear_tv(&item->di_tv);
7216 vim_free(item);
7217}
7218
7219/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7221 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007222 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 * Returns NULL when out of memory.
7224 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007226dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007227 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007228 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007229 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007230{
Bram Moolenaar33570922005-01-25 22:26:29 +00007231 dict_T *copy;
7232 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007233 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007234 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007235
7236 if (orig == NULL)
7237 return NULL;
7238
7239 copy = dict_alloc();
7240 if (copy != NULL)
7241 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007242 if (copyID != 0)
7243 {
7244 orig->dv_copyID = copyID;
7245 orig->dv_copydict = copy;
7246 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007247 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007248 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007249 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007250 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007251 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007252 --todo;
7253
7254 di = dictitem_alloc(hi->hi_key);
7255 if (di == NULL)
7256 break;
7257 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007258 {
7259 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7260 copyID) == FAIL)
7261 {
7262 vim_free(di);
7263 break;
7264 }
7265 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 else
7267 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7268 if (dict_add(copy, di) == FAIL)
7269 {
7270 dictitem_free(di);
7271 break;
7272 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007273 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007274 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007275
Bram Moolenaare9a41262005-01-15 22:18:47 +00007276 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007277 if (todo > 0)
7278 {
7279 dict_unref(copy);
7280 copy = NULL;
7281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282 }
7283
7284 return copy;
7285}
7286
7287/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007289 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007291 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007292dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007293 dict_T *d;
7294 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007295{
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297}
7298
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007300 * Add a number or string entry to dictionary "d".
7301 * When "str" is NULL use number "nr", otherwise use "str".
7302 * Returns FAIL when out of memory and when key already exists.
7303 */
7304 int
7305dict_add_nr_str(d, key, nr, str)
7306 dict_T *d;
7307 char *key;
7308 long nr;
7309 char_u *str;
7310{
7311 dictitem_T *item;
7312
7313 item = dictitem_alloc((char_u *)key);
7314 if (item == NULL)
7315 return FAIL;
7316 item->di_tv.v_lock = 0;
7317 if (str == NULL)
7318 {
7319 item->di_tv.v_type = VAR_NUMBER;
7320 item->di_tv.vval.v_number = nr;
7321 }
7322 else
7323 {
7324 item->di_tv.v_type = VAR_STRING;
7325 item->di_tv.vval.v_string = vim_strsave(str);
7326 }
7327 if (dict_add(d, item) == FAIL)
7328 {
7329 dictitem_free(item);
7330 return FAIL;
7331 }
7332 return OK;
7333}
7334
7335/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007336 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007337 * Returns FAIL when out of memory and when key already exists.
7338 */
7339 int
7340dict_add_list(d, key, list)
7341 dict_T *d;
7342 char *key;
7343 list_T *list;
7344{
7345 dictitem_T *item;
7346
7347 item = dictitem_alloc((char_u *)key);
7348 if (item == NULL)
7349 return FAIL;
7350 item->di_tv.v_lock = 0;
7351 item->di_tv.v_type = VAR_LIST;
7352 item->di_tv.vval.v_list = list;
7353 if (dict_add(d, item) == FAIL)
7354 {
7355 dictitem_free(item);
7356 return FAIL;
7357 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007358 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007359 return OK;
7360}
7361
7362/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007363 * Get the number of items in a Dictionary.
7364 */
7365 static long
7366dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007369 if (d == NULL)
7370 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007371 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372}
7373
7374/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007375 * Find item "key[len]" in Dictionary "d".
7376 * If "len" is negative use strlen(key).
7377 * Returns NULL when not found.
7378 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007379 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007380dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007382 char_u *key;
7383 int len;
7384{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385#define AKEYLEN 200
7386 char_u buf[AKEYLEN];
7387 char_u *akey;
7388 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 if (len < 0)
7392 akey = key;
7393 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007394 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007395 tofree = akey = vim_strnsave(key, len);
7396 if (akey == NULL)
7397 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007398 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 else
7400 {
7401 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007402 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 akey = buf;
7404 }
7405
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007407 vim_free(tofree);
7408 if (HASHITEM_EMPTY(hi))
7409 return NULL;
7410 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411}
7412
7413/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007414 * Get a string item from a dictionary.
7415 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007416 * Returns NULL if the entry doesn't exist or out of memory.
7417 */
7418 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007419get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007420 dict_T *d;
7421 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007422 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007423{
7424 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007425 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007426
7427 di = dict_find(d, key, -1);
7428 if (di == NULL)
7429 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007430 s = get_tv_string(&di->di_tv);
7431 if (save && s != NULL)
7432 s = vim_strsave(s);
7433 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007434}
7435
7436/*
7437 * Get a number item from a dictionary.
7438 * Returns 0 if the entry doesn't exist or out of memory.
7439 */
7440 long
7441get_dict_number(d, key)
7442 dict_T *d;
7443 char_u *key;
7444{
7445 dictitem_T *di;
7446
7447 di = dict_find(d, key, -1);
7448 if (di == NULL)
7449 return 0;
7450 return get_tv_number(&di->di_tv);
7451}
7452
7453/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 * Return an allocated string with the string representation of a Dictionary.
7455 * May return NULL.
7456 */
7457 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007458dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007459 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007460 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461{
7462 garray_T ga;
7463 int first = TRUE;
7464 char_u *tofree;
7465 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007466 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007468 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007469 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007471 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472 return NULL;
7473 ga_init2(&ga, (int)sizeof(char), 80);
7474 ga_append(&ga, '{');
7475
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007476 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007477 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007479 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007481 --todo;
7482
7483 if (first)
7484 first = FALSE;
7485 else
7486 ga_concat(&ga, (char_u *)", ");
7487
7488 tofree = string_quote(hi->hi_key, FALSE);
7489 if (tofree != NULL)
7490 {
7491 ga_concat(&ga, tofree);
7492 vim_free(tofree);
7493 }
7494 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007495 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 if (s != NULL)
7497 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007499 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007500 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007501 line_breakcheck();
7502
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007505 if (todo > 0)
7506 {
7507 vim_free(ga.ga_data);
7508 return NULL;
7509 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510
7511 ga_append(&ga, '}');
7512 ga_append(&ga, NUL);
7513 return (char_u *)ga.ga_data;
7514}
7515
7516/*
7517 * Allocate a variable for a Dictionary and fill it from "*arg".
7518 * Return OK or FAIL. Returns NOTDONE for {expr}.
7519 */
7520 static int
7521get_dict_tv(arg, rettv, evaluate)
7522 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524 int evaluate;
7525{
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 dict_T *d = NULL;
7527 typval_T tvkey;
7528 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007529 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533
7534 /*
7535 * First check if it's not a curly-braces thing: {expr}.
7536 * Must do this without evaluating, otherwise a function may be called
7537 * twice. Unfortunately this means we need to call eval1() twice for the
7538 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007539 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007541 if (*start != '}')
7542 {
7543 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7544 return FAIL;
7545 if (*start == '}')
7546 return NOTDONE;
7547 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548
7549 if (evaluate)
7550 {
7551 d = dict_alloc();
7552 if (d == NULL)
7553 return FAIL;
7554 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007555 tvkey.v_type = VAR_UNKNOWN;
7556 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007557
7558 *arg = skipwhite(*arg + 1);
7559 while (**arg != '}' && **arg != NUL)
7560 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007562 goto failret;
7563 if (**arg != ':')
7564 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007565 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 goto failret;
7568 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007569 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007571 key = get_tv_string_buf_chk(&tvkey, buf);
7572 if (key == NULL || *key == NUL)
7573 {
7574 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7575 if (key != NULL)
7576 EMSG(_(e_emptykey));
7577 clear_tv(&tvkey);
7578 goto failret;
7579 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007580 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581
7582 *arg = skipwhite(*arg + 1);
7583 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7584 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007585 if (evaluate)
7586 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 goto failret;
7588 }
7589 if (evaluate)
7590 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007591 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 if (item != NULL)
7593 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007594 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007595 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 clear_tv(&tv);
7597 goto failret;
7598 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 item = dictitem_alloc(key);
7600 clear_tv(&tvkey);
7601 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007602 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007604 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007605 if (dict_add(d, item) == FAIL)
7606 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 }
7608 }
7609
7610 if (**arg == '}')
7611 break;
7612 if (**arg != ',')
7613 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007614 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 goto failret;
7616 }
7617 *arg = skipwhite(*arg + 1);
7618 }
7619
7620 if (**arg != '}')
7621 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007622 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007623failret:
7624 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007625 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 return FAIL;
7627 }
7628
7629 *arg = skipwhite(*arg + 1);
7630 if (evaluate)
7631 {
7632 rettv->v_type = VAR_DICT;
7633 rettv->vval.v_dict = d;
7634 ++d->dv_refcount;
7635 }
7636
7637 return OK;
7638}
7639
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007641 * Return a string with the string representation of a variable.
7642 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007643 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007644 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007645 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007646 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007647 */
7648 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007649echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007650 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007651 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007652 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007653 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007654{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007655 static int recurse = 0;
7656 char_u *r = NULL;
7657
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007659 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007660 if (!did_echo_string_emsg)
7661 {
7662 /* Only give this message once for a recursive call to avoid
7663 * flooding the user with errors. And stop iterating over lists
7664 * and dicts. */
7665 did_echo_string_emsg = TRUE;
7666 EMSG(_("E724: variable nested too deep for displaying"));
7667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007668 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007669 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007670 }
7671 ++recurse;
7672
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007673 switch (tv->v_type)
7674 {
7675 case VAR_FUNC:
7676 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677 r = tv->vval.v_string;
7678 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007679
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007680 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007681 if (tv->vval.v_list == NULL)
7682 {
7683 *tofree = NULL;
7684 r = NULL;
7685 }
7686 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7687 {
7688 *tofree = NULL;
7689 r = (char_u *)"[...]";
7690 }
7691 else
7692 {
7693 tv->vval.v_list->lv_copyID = copyID;
7694 *tofree = list2string(tv, copyID);
7695 r = *tofree;
7696 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007698
Bram Moolenaar8c711452005-01-14 21:53:12 +00007699 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007700 if (tv->vval.v_dict == NULL)
7701 {
7702 *tofree = NULL;
7703 r = NULL;
7704 }
7705 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7706 {
7707 *tofree = NULL;
7708 r = (char_u *)"{...}";
7709 }
7710 else
7711 {
7712 tv->vval.v_dict->dv_copyID = copyID;
7713 *tofree = dict2string(tv, copyID);
7714 r = *tofree;
7715 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007717
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 case VAR_STRING:
7719 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007720 *tofree = NULL;
7721 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007723
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007724#ifdef FEAT_FLOAT
7725 case VAR_FLOAT:
7726 *tofree = NULL;
7727 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7728 r = numbuf;
7729 break;
7730#endif
7731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007732 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007733 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007734 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007735 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007736
Bram Moolenaar8502c702014-06-17 12:51:16 +02007737 if (--recurse == 0)
7738 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007740}
7741
7742/*
7743 * Return a string with the string representation of a variable.
7744 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7745 * "numbuf" is used for a number.
7746 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007747 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007748 */
7749 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007750tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007752 char_u **tofree;
7753 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007754 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007755{
7756 switch (tv->v_type)
7757 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007758 case VAR_FUNC:
7759 *tofree = string_quote(tv->vval.v_string, TRUE);
7760 return *tofree;
7761 case VAR_STRING:
7762 *tofree = string_quote(tv->vval.v_string, FALSE);
7763 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007764#ifdef FEAT_FLOAT
7765 case VAR_FLOAT:
7766 *tofree = NULL;
7767 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7768 return numbuf;
7769#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007770 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007771 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007772 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007773 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007774 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007775 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007776 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007777 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007778}
7779
7780/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 * Return string "str" in ' quotes, doubling ' characters.
7782 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007783 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007784 */
7785 static char_u *
7786string_quote(str, function)
7787 char_u *str;
7788 int function;
7789{
Bram Moolenaar33570922005-01-25 22:26:29 +00007790 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007791 char_u *p, *r, *s;
7792
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 len = (function ? 13 : 3);
7794 if (str != NULL)
7795 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007796 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007797 for (p = str; *p != NUL; mb_ptr_adv(p))
7798 if (*p == '\'')
7799 ++len;
7800 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007801 s = r = alloc(len);
7802 if (r != NULL)
7803 {
7804 if (function)
7805 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007806 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007807 r += 10;
7808 }
7809 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007810 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007811 if (str != NULL)
7812 for (p = str; *p != NUL; )
7813 {
7814 if (*p == '\'')
7815 *r++ = '\'';
7816 MB_COPY_CHAR(p, r);
7817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007818 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007819 if (function)
7820 *r++ = ')';
7821 *r++ = NUL;
7822 }
7823 return s;
7824}
7825
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007826#ifdef FEAT_FLOAT
7827/*
7828 * Convert the string "text" to a floating point number.
7829 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7830 * this always uses a decimal point.
7831 * Returns the length of the text that was consumed.
7832 */
7833 static int
7834string2float(text, value)
7835 char_u *text;
7836 float_T *value; /* result stored here */
7837{
7838 char *s = (char *)text;
7839 float_T f;
7840
7841 f = strtod(s, &s);
7842 *value = f;
7843 return (int)((char_u *)s - text);
7844}
7845#endif
7846
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007847/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 * Get the value of an environment variable.
7849 * "arg" is pointing to the '$'. It is advanced to after the name.
7850 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007851 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 */
7853 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007854get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 int evaluate;
7858{
7859 char_u *string = NULL;
7860 int len;
7861 int cc;
7862 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007863 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864
7865 ++*arg;
7866 name = *arg;
7867 len = get_env_len(arg);
7868 if (evaluate)
7869 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007870 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01007871 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007872
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007873 cc = name[len];
7874 name[len] = NUL;
7875 /* first try vim_getenv(), fast for normal environment vars */
7876 string = vim_getenv(name, &mustfree);
7877 if (string != NULL && *string != NUL)
7878 {
7879 if (!mustfree)
7880 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02007882 else
7883 {
7884 if (mustfree)
7885 vim_free(string);
7886
7887 /* next try expanding things like $VIM and ${HOME} */
7888 string = expand_env_save(name - 1);
7889 if (string != NULL && *string == '$')
7890 {
7891 vim_free(string);
7892 string = NULL;
7893 }
7894 }
7895 name[len] = cc;
7896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 rettv->v_type = VAR_STRING;
7898 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 }
7900
7901 return OK;
7902}
7903
7904/*
7905 * Array with names and number of arguments of all internal functions
7906 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7907 */
7908static struct fst
7909{
7910 char *f_name; /* function name */
7911 char f_min_argc; /* minimal number of arguments */
7912 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007913 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007914 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915} functions[] =
7916{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917#ifdef FEAT_FLOAT
7918 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007919 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007920#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007921 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007922 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 {"append", 2, 2, f_append},
7924 {"argc", 0, 0, f_argc},
7925 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02007926 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007927 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007928#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007929 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007930 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007931 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007934 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"bufexists", 1, 1, f_bufexists},
7936 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7937 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7938 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7939 {"buflisted", 1, 1, f_buflisted},
7940 {"bufloaded", 1, 1, f_bufloaded},
7941 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007942 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"bufwinnr", 1, 1, f_bufwinnr},
7944 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007945 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007946 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007947 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007948#ifdef FEAT_FLOAT
7949 {"ceil", 1, 1, f_ceil},
7950#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007951 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007952 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007954 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007956#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007957 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007958 {"complete_add", 1, 1, f_complete_add},
7959 {"complete_check", 0, 0, f_complete_check},
7960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007962 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007963#ifdef FEAT_FLOAT
7964 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007965 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007966#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007967 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007969 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007970 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"delete", 1, 1, f_delete},
7972 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007973 {"diff_filler", 1, 1, f_diff_filler},
7974 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007975 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007977 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"eventhandler", 0, 0, f_eventhandler},
7979 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02007980 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007982#ifdef FEAT_FLOAT
7983 {"exp", 1, 1, f_exp},
7984#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007985 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007986 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007987 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7989 {"filereadable", 1, 1, f_filereadable},
7990 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007991 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007992 {"finddir", 1, 3, f_finddir},
7993 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007994#ifdef FEAT_FLOAT
7995 {"float2nr", 1, 1, f_float2nr},
7996 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007997 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007999 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {"fnamemodify", 2, 2, f_fnamemodify},
8001 {"foldclosed", 1, 1, f_foldclosed},
8002 {"foldclosedend", 1, 1, f_foldclosedend},
8003 {"foldlevel", 1, 1, f_foldlevel},
8004 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008005 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008008 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008009 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008010 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008011 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 {"getchar", 0, 1, f_getchar},
8013 {"getcharmod", 0, 0, f_getcharmod},
8014 {"getcmdline", 0, 0, f_getcmdline},
8015 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008016 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008017 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008018 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008020 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008021 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {"getfsize", 1, 1, f_getfsize},
8023 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008024 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008025 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008026 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008027 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008028 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008029 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008030 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008031 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008033 {"gettabvar", 2, 3, f_gettabvar},
8034 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 {"getwinposx", 0, 0, f_getwinposx},
8036 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008037 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008038 {"glob", 1, 3, f_glob},
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02008039 {"globpath", 2, 4, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008041 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00008042 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008043 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8045 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8046 {"histadd", 2, 2, f_histadd},
8047 {"histdel", 1, 2, f_histdel},
8048 {"histget", 1, 2, f_histget},
8049 {"histnr", 1, 1, f_histnr},
8050 {"hlID", 1, 1, f_hlID},
8051 {"hlexists", 1, 1, f_hlexists},
8052 {"hostname", 0, 0, f_hostname},
8053 {"iconv", 3, 3, f_iconv},
8054 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008055 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008056 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008058 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"inputrestore", 0, 0, f_inputrestore},
8060 {"inputsave", 0, 0, f_inputsave},
8061 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008062 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008063 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008065 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008066 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008067 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008070 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"libcall", 3, 3, f_libcall},
8072 {"libcallnr", 3, 3, f_libcallnr},
8073 {"line", 1, 1, f_line},
8074 {"line2byte", 1, 1, f_line2byte},
8075 {"lispindent", 1, 1, f_lispindent},
8076 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008078 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008079 {"log10", 1, 1, f_log10},
8080#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008081#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008082 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008083#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008084 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008085 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008086 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008087 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008088 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaarb3414592014-06-17 17:48:32 +02008089 {"matchaddpos", 2, 4, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008090 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008091 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008092 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008093 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008094 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008095 {"max", 1, 1, f_max},
8096 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008097#ifdef vim_mkdir
8098 {"mkdir", 1, 3, f_mkdir},
8099#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008100 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008101#ifdef FEAT_MZSCHEME
8102 {"mzeval", 1, 1, f_mzeval},
8103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008105 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008106 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008107 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#ifdef FEAT_FLOAT
8109 {"pow", 2, 2, f_pow},
8110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008112 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008113 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008114#ifdef FEAT_PYTHON3
8115 {"py3eval", 1, 1, f_py3eval},
8116#endif
8117#ifdef FEAT_PYTHON
8118 {"pyeval", 1, 1, f_pyeval},
8119#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008120 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008121 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008122 {"reltime", 0, 2, f_reltime},
8123 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"remote_expr", 2, 3, f_remote_expr},
8125 {"remote_foreground", 1, 1, f_remote_foreground},
8126 {"remote_peek", 1, 2, f_remote_peek},
8127 {"remote_read", 1, 1, f_remote_read},
8128 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008129 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008131 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008133 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008134#ifdef FEAT_FLOAT
8135 {"round", 1, 1, f_round},
8136#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008137 {"screenattr", 2, 2, f_screenattr},
8138 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008139 {"screencol", 0, 0, f_screencol},
8140 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008141 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008142 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008143 {"searchpair", 3, 7, f_searchpair},
8144 {"searchpairpos", 3, 7, f_searchpairpos},
8145 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 {"server2client", 2, 2, f_server2client},
8147 {"serverlist", 0, 0, f_serverlist},
8148 {"setbufvar", 3, 3, f_setbufvar},
8149 {"setcmdpos", 1, 1, f_setcmdpos},
8150 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008151 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008152 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008153 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008154 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008156 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008157 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008159#ifdef FEAT_CRYPT
8160 {"sha256", 1, 1, f_sha256},
8161#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008162 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008163 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#ifdef FEAT_FLOAT
8166 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008167 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008169 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008170 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008171 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008172 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008173 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008174#ifdef FEAT_FLOAT
8175 {"sqrt", 1, 1, f_sqrt},
8176 {"str2float", 1, 1, f_str2float},
8177#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008178 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008179 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008180 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181#ifdef HAVE_STRFTIME
8182 {"strftime", 1, 2, f_strftime},
8183#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008184 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008185 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"strlen", 1, 1, f_strlen},
8187 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008188 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008190 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008191 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"substitute", 4, 4, f_substitute},
8193 {"synID", 3, 3, f_synID},
8194 {"synIDattr", 2, 3, f_synIDattr},
8195 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008196 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008197 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008198 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008199 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008200 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008201 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008202 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008203 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008204 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008205#ifdef FEAT_FLOAT
8206 {"tan", 1, 1, f_tan},
8207 {"tanh", 1, 1, f_tanh},
8208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008210 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 {"tolower", 1, 1, f_tolower},
8212 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008213 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008214#ifdef FEAT_FLOAT
8215 {"trunc", 1, 1, f_trunc},
8216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008218 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008219 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008220 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008221 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 {"virtcol", 1, 1, f_virtcol},
8223 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008224 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"winbufnr", 1, 1, f_winbufnr},
8226 {"wincol", 0, 0, f_wincol},
8227 {"winheight", 1, 1, f_winheight},
8228 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008229 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008231 {"winrestview", 1, 1, f_winrestview},
8232 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008234 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008235 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236};
8237
8238#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8239
8240/*
8241 * Function given to ExpandGeneric() to obtain the list of internal
8242 * or user defined function names.
8243 */
8244 char_u *
8245get_function_name(xp, idx)
8246 expand_T *xp;
8247 int idx;
8248{
8249 static int intidx = -1;
8250 char_u *name;
8251
8252 if (idx == 0)
8253 intidx = -1;
8254 if (intidx < 0)
8255 {
8256 name = get_user_func_name(xp, idx);
8257 if (name != NULL)
8258 return name;
8259 }
8260 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8261 {
8262 STRCPY(IObuff, functions[intidx].f_name);
8263 STRCAT(IObuff, "(");
8264 if (functions[intidx].f_max_argc == 0)
8265 STRCAT(IObuff, ")");
8266 return IObuff;
8267 }
8268
8269 return NULL;
8270}
8271
8272/*
8273 * Function given to ExpandGeneric() to obtain the list of internal or
8274 * user defined variable or function names.
8275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 char_u *
8277get_expr_name(xp, idx)
8278 expand_T *xp;
8279 int idx;
8280{
8281 static int intidx = -1;
8282 char_u *name;
8283
8284 if (idx == 0)
8285 intidx = -1;
8286 if (intidx < 0)
8287 {
8288 name = get_function_name(xp, idx);
8289 if (name != NULL)
8290 return name;
8291 }
8292 return get_user_var_name(xp, ++intidx);
8293}
8294
8295#endif /* FEAT_CMDL_COMPL */
8296
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008297#if defined(EBCDIC) || defined(PROTO)
8298/*
8299 * Compare struct fst by function name.
8300 */
8301 static int
8302compare_func_name(s1, s2)
8303 const void *s1;
8304 const void *s2;
8305{
8306 struct fst *p1 = (struct fst *)s1;
8307 struct fst *p2 = (struct fst *)s2;
8308
8309 return STRCMP(p1->f_name, p2->f_name);
8310}
8311
8312/*
8313 * Sort the function table by function name.
8314 * The sorting of the table above is ASCII dependant.
8315 * On machines using EBCDIC we have to sort it.
8316 */
8317 static void
8318sortFunctions()
8319{
8320 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8321
8322 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8323}
8324#endif
8325
8326
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327/*
8328 * Find internal function in table above.
8329 * Return index, or -1 if not found
8330 */
8331 static int
8332find_internal_func(name)
8333 char_u *name; /* name of the function */
8334{
8335 int first = 0;
8336 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8337 int cmp;
8338 int x;
8339
8340 /*
8341 * Find the function name in the table. Binary search.
8342 */
8343 while (first <= last)
8344 {
8345 x = first + ((unsigned)(last - first) >> 1);
8346 cmp = STRCMP(name, functions[x].f_name);
8347 if (cmp < 0)
8348 last = x - 1;
8349 else if (cmp > 0)
8350 first = x + 1;
8351 else
8352 return x;
8353 }
8354 return -1;
8355}
8356
8357/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008358 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8359 * name it contains, otherwise return "name".
8360 */
8361 static char_u *
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008362deref_func_name(name, lenp, no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363 char_u *name;
8364 int *lenp;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008365 int no_autoload;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008366{
Bram Moolenaar33570922005-01-25 22:26:29 +00008367 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008368 int cc;
8369
8370 cc = name[*lenp];
8371 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008372 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008373 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008374 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008375 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008376 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008377 {
8378 *lenp = 0;
8379 return (char_u *)""; /* just in case */
8380 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008381 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008382 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008383 }
8384
8385 return name;
8386}
8387
8388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 * Allocate a variable for the result of a function.
8390 * Return OK or FAIL.
8391 */
8392 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008393get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8394 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 char_u *name; /* name of the function */
8396 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 char_u **arg; /* argument, pointing to the '(' */
8399 linenr_T firstline; /* first line of range */
8400 linenr_T lastline; /* last line of range */
8401 int *doesrange; /* return: function handled range */
8402 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008403 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404{
8405 char_u *argp;
8406 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008407 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 int argcount = 0; /* number of arguments found */
8409
8410 /*
8411 * Get the arguments.
8412 */
8413 argp = *arg;
8414 while (argcount < MAX_FUNC_ARGS)
8415 {
8416 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8417 if (*argp == ')' || *argp == ',' || *argp == NUL)
8418 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8420 {
8421 ret = FAIL;
8422 break;
8423 }
8424 ++argcount;
8425 if (*argp != ',')
8426 break;
8427 }
8428 if (*argp == ')')
8429 ++argp;
8430 else
8431 ret = FAIL;
8432
8433 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008434 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008435 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008437 {
8438 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008439 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008440 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008441 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443
8444 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008445 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
8447 *arg = skipwhite(argp);
8448 return ret;
8449}
8450
8451
8452/*
8453 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008454 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008455 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 */
8457 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008458call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008459 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008460 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008462 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008464 typval_T *argvars; /* vars for arguments, must have "argcount"
8465 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 linenr_T firstline; /* first line of range */
8467 linenr_T lastline; /* last line of range */
8468 int *doesrange; /* return: function handled range */
8469 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008470 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471{
8472 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473#define ERROR_UNKNOWN 0
8474#define ERROR_TOOMANY 1
8475#define ERROR_TOOFEW 2
8476#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008477#define ERROR_DICT 4
8478#define ERROR_NONE 5
8479#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 int error = ERROR_NONE;
8481 int i;
8482 int llen;
8483 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484#define FLEN_FIXED 40
8485 char_u fname_buf[FLEN_FIXED + 1];
8486 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008487 char_u *name;
8488
8489 /* Make a copy of the name, if it comes from a funcref variable it could
8490 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008491 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008492 if (name == NULL)
8493 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494
8495 /*
8496 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8497 * Change <SNR>123_name() to K_SNR 123_name().
8498 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 llen = eval_fname_script(name);
8501 if (llen > 0)
8502 {
8503 fname_buf[0] = K_SPECIAL;
8504 fname_buf[1] = KS_EXTRA;
8505 fname_buf[2] = (int)KE_SNR;
8506 i = 3;
8507 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8508 {
8509 if (current_SID <= 0)
8510 error = ERROR_SCRIPT;
8511 else
8512 {
8513 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8514 i = (int)STRLEN(fname_buf);
8515 }
8516 }
8517 if (i + STRLEN(name + llen) < FLEN_FIXED)
8518 {
8519 STRCPY(fname_buf + i, name + llen);
8520 fname = fname_buf;
8521 }
8522 else
8523 {
8524 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8525 if (fname == NULL)
8526 error = ERROR_OTHER;
8527 else
8528 {
8529 mch_memmove(fname, fname_buf, (size_t)i);
8530 STRCPY(fname + i, name + llen);
8531 }
8532 }
8533 }
8534 else
8535 fname = name;
8536
8537 *doesrange = FALSE;
8538
8539
8540 /* execute the function if no errors detected and executing */
8541 if (evaluate && error == ERROR_NONE)
8542 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008543 char_u *rfname = fname;
8544
8545 /* Ignore "g:" before a function name. */
8546 if (fname[0] == 'g' && fname[1] == ':')
8547 rfname = fname + 2;
8548
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008549 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8550 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 error = ERROR_UNKNOWN;
8552
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008553 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 {
8555 /*
8556 * User defined function.
8557 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008558 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008559
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008561 /* Trigger FuncUndefined event, may load the function. */
8562 if (fp == NULL
8563 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008564 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008565 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008567 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008568 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 }
8570#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008571 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008572 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008573 {
8574 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008575 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008576 }
8577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 if (fp != NULL)
8579 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008580 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008582 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008584 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008586 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008587 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 else
8589 {
8590 /*
8591 * Call the user function.
8592 * Save and restore search patterns, script variables and
8593 * redo buffer.
8594 */
8595 save_search_patterns();
8596 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008597 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008599 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008600 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8601 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8602 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008603 /* Function was unreferenced while being used, free it
8604 * now. */
8605 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 restoreRedobuff();
8607 restore_search_patterns();
8608 error = ERROR_NONE;
8609 }
8610 }
8611 }
8612 else
8613 {
8614 /*
8615 * Find the function name in the table, call its implementation.
8616 */
8617 i = find_internal_func(fname);
8618 if (i >= 0)
8619 {
8620 if (argcount < functions[i].f_min_argc)
8621 error = ERROR_TOOFEW;
8622 else if (argcount > functions[i].f_max_argc)
8623 error = ERROR_TOOMANY;
8624 else
8625 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008626 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008627 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 error = ERROR_NONE;
8629 }
8630 }
8631 }
8632 /*
8633 * The function call (or "FuncUndefined" autocommand sequence) might
8634 * have been aborted by an error, an interrupt, or an explicitly thrown
8635 * exception that has not been caught so far. This situation can be
8636 * tested for by calling aborting(). For an error in an internal
8637 * function or for the "E132" error in call_user_func(), however, the
8638 * throw point at which the "force_abort" flag (temporarily reset by
8639 * emsg()) is normally updated has not been reached yet. We need to
8640 * update that flag first to make aborting() reliable.
8641 */
8642 update_force_abort();
8643 }
8644 if (error == ERROR_NONE)
8645 ret = OK;
8646
8647 /*
8648 * Report an error unless the argument evaluation or function call has been
8649 * cancelled due to an aborting error, an interrupt, or an exception.
8650 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008651 if (!aborting())
8652 {
8653 switch (error)
8654 {
8655 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008656 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008657 break;
8658 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008659 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008660 break;
8661 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008662 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008663 name);
8664 break;
8665 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008666 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008667 name);
8668 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008669 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008670 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008671 name);
8672 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008673 }
8674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 if (fname != name && fname != fname_buf)
8677 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008678 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679
8680 return ret;
8681}
8682
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008683/*
8684 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008685 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008686 */
8687 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008688emsg_funcname(ermsg, name)
8689 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008690 char_u *name;
8691{
8692 char_u *p;
8693
8694 if (*name == K_SPECIAL)
8695 p = concat_str((char_u *)"<SNR>", name + 3);
8696 else
8697 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008698 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008699 if (p != name)
8700 vim_free(p);
8701}
8702
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008703/*
8704 * Return TRUE for a non-zero Number and a non-empty String.
8705 */
8706 static int
8707non_zero_arg(argvars)
8708 typval_T *argvars;
8709{
8710 return ((argvars[0].v_type == VAR_NUMBER
8711 && argvars[0].vval.v_number != 0)
8712 || (argvars[0].v_type == VAR_STRING
8713 && argvars[0].vval.v_string != NULL
8714 && *argvars[0].vval.v_string != NUL));
8715}
8716
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717/*********************************************
8718 * Implementation of the built-in functions
8719 */
8720
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008721#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008722static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8723
8724/*
8725 * Get the float value of "argvars[0]" into "f".
8726 * Returns FAIL when the argument is not a Number or Float.
8727 */
8728 static int
8729get_float_arg(argvars, f)
8730 typval_T *argvars;
8731 float_T *f;
8732{
8733 if (argvars[0].v_type == VAR_FLOAT)
8734 {
8735 *f = argvars[0].vval.v_float;
8736 return OK;
8737 }
8738 if (argvars[0].v_type == VAR_NUMBER)
8739 {
8740 *f = (float_T)argvars[0].vval.v_number;
8741 return OK;
8742 }
8743 EMSG(_("E808: Number or Float required"));
8744 return FAIL;
8745}
8746
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008747/*
8748 * "abs(expr)" function
8749 */
8750 static void
8751f_abs(argvars, rettv)
8752 typval_T *argvars;
8753 typval_T *rettv;
8754{
8755 if (argvars[0].v_type == VAR_FLOAT)
8756 {
8757 rettv->v_type = VAR_FLOAT;
8758 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8759 }
8760 else
8761 {
8762 varnumber_T n;
8763 int error = FALSE;
8764
8765 n = get_tv_number_chk(&argvars[0], &error);
8766 if (error)
8767 rettv->vval.v_number = -1;
8768 else if (n > 0)
8769 rettv->vval.v_number = n;
8770 else
8771 rettv->vval.v_number = -n;
8772 }
8773}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008774
8775/*
8776 * "acos()" function
8777 */
8778 static void
8779f_acos(argvars, rettv)
8780 typval_T *argvars;
8781 typval_T *rettv;
8782{
8783 float_T f;
8784
8785 rettv->v_type = VAR_FLOAT;
8786 if (get_float_arg(argvars, &f) == OK)
8787 rettv->vval.v_float = acos(f);
8788 else
8789 rettv->vval.v_float = 0.0;
8790}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008791#endif
8792
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008794 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 */
8796 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008797f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008798 typval_T *argvars;
8799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
Bram Moolenaar33570922005-01-25 22:26:29 +00008801 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008804 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008806 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008807 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008808 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008809 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008810 }
8811 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008812 EMSG(_(e_listreq));
8813}
8814
8815/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008816 * "and(expr, expr)" function
8817 */
8818 static void
8819f_and(argvars, rettv)
8820 typval_T *argvars;
8821 typval_T *rettv;
8822{
8823 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8824 & get_tv_number_chk(&argvars[1], NULL);
8825}
8826
8827/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008828 * "append(lnum, string/list)" function
8829 */
8830 static void
8831f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008832 typval_T *argvars;
8833 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008834{
8835 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008836 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008837 list_T *l = NULL;
8838 listitem_T *li = NULL;
8839 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008840 long added = 0;
8841
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008842 /* When coming here from Insert mode, sync undo, so that this can be
8843 * undone separately from what was previously inserted. */
8844 if (u_sync_once == 2)
8845 {
8846 u_sync_once = 1; /* notify that u_sync() was called */
8847 u_sync(TRUE);
8848 }
8849
Bram Moolenaar0d660222005-01-07 21:51:51 +00008850 lnum = get_tv_lnum(argvars);
8851 if (lnum >= 0
8852 && lnum <= curbuf->b_ml.ml_line_count
8853 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008854 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008855 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008856 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008857 l = argvars[1].vval.v_list;
8858 if (l == NULL)
8859 return;
8860 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008861 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008862 for (;;)
8863 {
8864 if (l == NULL)
8865 tv = &argvars[1]; /* append a string */
8866 else if (li == NULL)
8867 break; /* end of list */
8868 else
8869 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008870 line = get_tv_string_chk(tv);
8871 if (line == NULL) /* type error */
8872 {
8873 rettv->vval.v_number = 1; /* Failed */
8874 break;
8875 }
8876 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008877 ++added;
8878 if (l == NULL)
8879 break;
8880 li = li->li_next;
8881 }
8882
8883 appended_lines_mark(lnum, added);
8884 if (curwin->w_cursor.lnum > lnum)
8885 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008887 else
8888 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889}
8890
8891/*
8892 * "argc()" function
8893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008896 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008897 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900}
8901
8902/*
8903 * "argidx()" function
8904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008906f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008907 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008908 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008910 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911}
8912
8913/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008914 * "arglistid()" function
8915 */
8916 static void
8917f_arglistid(argvars, rettv)
8918 typval_T *argvars UNUSED;
8919 typval_T *rettv;
8920{
8921 win_T *wp;
8922 tabpage_T *tp = NULL;
8923 long n;
8924
8925 rettv->vval.v_number = -1;
8926 if (argvars[0].v_type != VAR_UNKNOWN)
8927 {
8928 if (argvars[1].v_type != VAR_UNKNOWN)
8929 {
8930 n = get_tv_number(&argvars[1]);
8931 if (n >= 0)
8932 tp = find_tabpage(n);
8933 }
8934 else
8935 tp = curtab;
8936
8937 if (tp != NULL)
8938 {
8939 wp = find_win_by_nr(&argvars[0], tp);
8940 if (wp != NULL)
8941 rettv->vval.v_number = wp->w_alist->id;
8942 }
8943 }
8944 else
8945 rettv->vval.v_number = curwin->w_alist->id;
8946}
8947
8948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 * "argv(nr)" function
8950 */
8951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 typval_T *argvars;
8954 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956 int idx;
8957
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008958 if (argvars[0].v_type != VAR_UNKNOWN)
8959 {
8960 idx = get_tv_number_chk(&argvars[0], NULL);
8961 if (idx >= 0 && idx < ARGCOUNT)
8962 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8963 else
8964 rettv->vval.v_string = NULL;
8965 rettv->v_type = VAR_STRING;
8966 }
8967 else if (rettv_list_alloc(rettv) == OK)
8968 for (idx = 0; idx < ARGCOUNT; ++idx)
8969 list_append_string(rettv->vval.v_list,
8970 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971}
8972
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008973#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008974/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008975 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008976 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008977 static void
8978f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008979 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008980 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008981{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008982 float_T f;
8983
8984 rettv->v_type = VAR_FLOAT;
8985 if (get_float_arg(argvars, &f) == OK)
8986 rettv->vval.v_float = asin(f);
8987 else
8988 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008989}
8990
8991/*
8992 * "atan()" function
8993 */
8994 static void
8995f_atan(argvars, rettv)
8996 typval_T *argvars;
8997 typval_T *rettv;
8998{
8999 float_T f;
9000
9001 rettv->v_type = VAR_FLOAT;
9002 if (get_float_arg(argvars, &f) == OK)
9003 rettv->vval.v_float = atan(f);
9004 else
9005 rettv->vval.v_float = 0.0;
9006}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009007
9008/*
9009 * "atan2()" function
9010 */
9011 static void
9012f_atan2(argvars, rettv)
9013 typval_T *argvars;
9014 typval_T *rettv;
9015{
9016 float_T fx, fy;
9017
9018 rettv->v_type = VAR_FLOAT;
9019 if (get_float_arg(argvars, &fx) == OK
9020 && get_float_arg(&argvars[1], &fy) == OK)
9021 rettv->vval.v_float = atan2(fx, fy);
9022 else
9023 rettv->vval.v_float = 0.0;
9024}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009025#endif
9026
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027/*
9028 * "browse(save, title, initdir, default)" function
9029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009032 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034{
9035#ifdef FEAT_BROWSE
9036 int save;
9037 char_u *title;
9038 char_u *initdir;
9039 char_u *defname;
9040 char_u buf[NUMBUFLEN];
9041 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009042 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009044 save = get_tv_number_chk(&argvars[0], &error);
9045 title = get_tv_string_chk(&argvars[1]);
9046 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9047 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049 if (error || title == NULL || initdir == NULL || defname == NULL)
9050 rettv->vval.v_string = NULL;
9051 else
9052 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009053 do_browse(save ? BROWSE_SAVE : 0,
9054 title, defname, NULL, initdir, NULL, curbuf);
9055#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009056 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009057#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009058 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009059}
9060
9061/*
9062 * "browsedir(title, initdir)" function
9063 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009066 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009067 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009068{
9069#ifdef FEAT_BROWSE
9070 char_u *title;
9071 char_u *initdir;
9072 char_u buf[NUMBUFLEN];
9073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009074 title = get_tv_string_chk(&argvars[0]);
9075 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009076
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009077 if (title == NULL || initdir == NULL)
9078 rettv->vval.v_string = NULL;
9079 else
9080 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009081 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086}
9087
Bram Moolenaar33570922005-01-25 22:26:29 +00009088static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009089
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090/*
9091 * Find a buffer by number or exact name.
9092 */
9093 static buf_T *
9094find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00009095 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
9097 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009099 if (avar->v_type == VAR_NUMBER)
9100 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009101 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009103 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009104 if (buf == NULL)
9105 {
9106 /* No full path name match, try a match with a URL or a "nofile"
9107 * buffer, these don't use the full path. */
9108 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9109 if (buf->b_fname != NULL
9110 && (path_with_url(buf->b_fname)
9111#ifdef FEAT_QUICKFIX
9112 || bt_nofile(buf)
9113#endif
9114 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009115 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009116 break;
9117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 }
9119 return buf;
9120}
9121
9122/*
9123 * "bufexists(expr)" function
9124 */
9125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009126f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009127 typval_T *argvars;
9128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131}
9132
9133/*
9134 * "buflisted(expr)" function
9135 */
9136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009138 typval_T *argvars;
9139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140{
9141 buf_T *buf;
9142
9143 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145}
9146
9147/*
9148 * "bufloaded(expr)" function
9149 */
9150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009152 typval_T *argvars;
9153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
9155 buf_T *buf;
9156
9157 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159}
9160
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009161static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009162
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163/*
9164 * Get buffer by number or pattern.
9165 */
9166 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009167get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009168 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009169 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009171 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 int save_magic;
9173 char_u *save_cpo;
9174 buf_T *buf;
9175
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176 if (tv->v_type == VAR_NUMBER)
9177 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009178 if (tv->v_type != VAR_STRING)
9179 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 if (name == NULL || *name == NUL)
9181 return curbuf;
9182 if (name[0] == '$' && name[1] == NUL)
9183 return lastbuf;
9184
9185 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9186 save_magic = p_magic;
9187 p_magic = TRUE;
9188 save_cpo = p_cpo;
9189 p_cpo = (char_u *)"";
9190
9191 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009192 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193
9194 p_magic = save_magic;
9195 p_cpo = save_cpo;
9196
9197 /* If not found, try expanding the name, like done for bufexists(). */
9198 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200
9201 return buf;
9202}
9203
9204/*
9205 * "bufname(expr)" function
9206 */
9207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009209 typval_T *argvars;
9210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211{
9212 buf_T *buf;
9213
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009214 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009216 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009217 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009221 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 --emsg_off;
9223}
9224
9225/*
9226 * "bufnr(expr)" function
9227 */
9228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009230 typval_T *argvars;
9231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232{
9233 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009234 int error = FALSE;
9235 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009237 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009239 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009240 --emsg_off;
9241
9242 /* If the buffer isn't found and the second argument is not zero create a
9243 * new buffer. */
9244 if (buf == NULL
9245 && argvars[1].v_type != VAR_UNKNOWN
9246 && get_tv_number_chk(&argvars[1], &error) != 0
9247 && !error
9248 && (name = get_tv_string_chk(&argvars[0])) != NULL
9249 && !error)
9250 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9251
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009255 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256}
9257
9258/*
9259 * "bufwinnr(nr)" function
9260 */
9261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009262f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009263 typval_T *argvars;
9264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265{
9266#ifdef FEAT_WINDOWS
9267 win_T *wp;
9268 int winnr = 0;
9269#endif
9270 buf_T *buf;
9271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009272 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009274 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275#ifdef FEAT_WINDOWS
9276 for (wp = firstwin; wp; wp = wp->w_next)
9277 {
9278 ++winnr;
9279 if (wp->w_buffer == buf)
9280 break;
9281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009284 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285#endif
9286 --emsg_off;
9287}
9288
9289/*
9290 * "byte2line(byte)" function
9291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009293f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009294 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
9297#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009298 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299#else
9300 long boff = 0;
9301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009302 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009306 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 (linenr_T)0, &boff);
9308#endif
9309}
9310
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009311 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009312byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009313 typval_T *argvars;
9314 typval_T *rettv;
Bram Moolenaarf30caaf2014-02-23 22:54:58 +01009315 int comp UNUSED;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009316{
9317#ifdef FEAT_MBYTE
9318 char_u *t;
9319#endif
9320 char_u *str;
9321 long idx;
9322
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009323 str = get_tv_string_chk(&argvars[0]);
9324 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009326 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009327 return;
9328
9329#ifdef FEAT_MBYTE
9330 t = str;
9331 for ( ; idx > 0; idx--)
9332 {
9333 if (*t == NUL) /* EOL reached */
9334 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009335 if (enc_utf8 && comp)
9336 t += utf_ptr2len(t);
9337 else
9338 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009339 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009340 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009341#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009342 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009343 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009344#endif
9345}
9346
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009347/*
9348 * "byteidx()" function
9349 */
9350 static void
9351f_byteidx(argvars, rettv)
9352 typval_T *argvars;
9353 typval_T *rettv;
9354{
9355 byteidx(argvars, rettv, FALSE);
9356}
9357
9358/*
9359 * "byteidxcomp()" function
9360 */
9361 static void
9362f_byteidxcomp(argvars, rettv)
9363 typval_T *argvars;
9364 typval_T *rettv;
9365{
9366 byteidx(argvars, rettv, TRUE);
9367}
9368
Bram Moolenaardb913952012-06-29 12:54:53 +02009369 int
9370func_call(name, args, selfdict, rettv)
9371 char_u *name;
9372 typval_T *args;
9373 dict_T *selfdict;
9374 typval_T *rettv;
9375{
9376 listitem_T *item;
9377 typval_T argv[MAX_FUNC_ARGS + 1];
9378 int argc = 0;
9379 int dummy;
9380 int r = 0;
9381
9382 for (item = args->vval.v_list->lv_first; item != NULL;
9383 item = item->li_next)
9384 {
9385 if (argc == MAX_FUNC_ARGS)
9386 {
9387 EMSG(_("E699: Too many arguments"));
9388 break;
9389 }
9390 /* Make a copy of each argument. This is needed to be able to set
9391 * v_lock to VAR_FIXED in the copy without changing the original list.
9392 */
9393 copy_tv(&item->li_tv, &argv[argc++]);
9394 }
9395
9396 if (item == NULL)
9397 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9398 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9399 &dummy, TRUE, selfdict);
9400
9401 /* Free the arguments. */
9402 while (argc > 0)
9403 clear_tv(&argv[--argc]);
9404
9405 return r;
9406}
9407
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009408/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009409 * "call(func, arglist)" function
9410 */
9411 static void
9412f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009413 typval_T *argvars;
9414 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009415{
9416 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009417 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009418
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009419 if (argvars[1].v_type != VAR_LIST)
9420 {
9421 EMSG(_(e_listreq));
9422 return;
9423 }
9424 if (argvars[1].vval.v_list == NULL)
9425 return;
9426
9427 if (argvars[0].v_type == VAR_FUNC)
9428 func = argvars[0].vval.v_string;
9429 else
9430 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009431 if (*func == NUL)
9432 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009433
Bram Moolenaare9a41262005-01-15 22:18:47 +00009434 if (argvars[2].v_type != VAR_UNKNOWN)
9435 {
9436 if (argvars[2].v_type != VAR_DICT)
9437 {
9438 EMSG(_(e_dictreq));
9439 return;
9440 }
9441 selfdict = argvars[2].vval.v_dict;
9442 }
9443
Bram Moolenaardb913952012-06-29 12:54:53 +02009444 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009445}
9446
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009447#ifdef FEAT_FLOAT
9448/*
9449 * "ceil({float})" function
9450 */
9451 static void
9452f_ceil(argvars, rettv)
9453 typval_T *argvars;
9454 typval_T *rettv;
9455{
9456 float_T f;
9457
9458 rettv->v_type = VAR_FLOAT;
9459 if (get_float_arg(argvars, &f) == OK)
9460 rettv->vval.v_float = ceil(f);
9461 else
9462 rettv->vval.v_float = 0.0;
9463}
9464#endif
9465
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009466/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009467 * "changenr()" function
9468 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009469 static void
9470f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009471 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009472 typval_T *rettv;
9473{
9474 rettv->vval.v_number = curbuf->b_u_seq_cur;
9475}
9476
9477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 * "char2nr(string)" function
9479 */
9480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009481f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009482 typval_T *argvars;
9483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484{
9485#ifdef FEAT_MBYTE
9486 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009487 {
9488 int utf8 = 0;
9489
9490 if (argvars[1].v_type != VAR_UNKNOWN)
9491 utf8 = get_tv_number_chk(&argvars[1], NULL);
9492
9493 if (utf8)
9494 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9495 else
9496 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 else
9499#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009500 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501}
9502
9503/*
9504 * "cindent(lnum)" function
9505 */
9506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009508 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
9511#ifdef FEAT_CINDENT
9512 pos_T pos;
9513 linenr_T lnum;
9514
9515 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9518 {
9519 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 curwin->w_cursor = pos;
9522 }
9523 else
9524#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526}
9527
9528/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009529 * "clearmatches()" function
9530 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009531 static void
9532f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009533 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009534 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009535{
9536#ifdef FEAT_SEARCH_EXTRA
9537 clear_matches(curwin);
9538#endif
9539}
9540
9541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 * "col(string)" function
9543 */
9544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009546 typval_T *argvars;
9547 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548{
9549 colnr_T col = 0;
9550 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009551 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009553 fp = var2fpos(&argvars[0], FALSE, &fnum);
9554 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 {
9556 if (fp->col == MAXCOL)
9557 {
9558 /* '> can be MAXCOL, get the length of the line then */
9559 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009560 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 else
9562 col = MAXCOL;
9563 }
9564 else
9565 {
9566 col = fp->col + 1;
9567#ifdef FEAT_VIRTUALEDIT
9568 /* col(".") when the cursor is on the NUL at the end of the line
9569 * because of "coladd" can be seen as an extra column. */
9570 if (virtual_active() && fp == &curwin->w_cursor)
9571 {
9572 char_u *p = ml_get_cursor();
9573
9574 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9575 curwin->w_virtcol - curwin->w_cursor.coladd))
9576 {
9577# ifdef FEAT_MBYTE
9578 int l;
9579
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009580 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 col += l;
9582# else
9583 if (*p != NUL && p[1] == NUL)
9584 ++col;
9585# endif
9586 }
9587 }
9588#endif
9589 }
9590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592}
9593
Bram Moolenaar572cb562005-08-05 21:35:02 +00009594#if defined(FEAT_INS_EXPAND)
9595/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009596 * "complete()" function
9597 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009598 static void
9599f_complete(argvars, rettv)
9600 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009601 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009602{
9603 int startcol;
9604
9605 if ((State & INSERT) == 0)
9606 {
9607 EMSG(_("E785: complete() can only be used in Insert mode"));
9608 return;
9609 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009610
9611 /* Check for undo allowed here, because if something was already inserted
9612 * the line was already saved for undo and this check isn't done. */
9613 if (!undo_allowed())
9614 return;
9615
Bram Moolenaarade00832006-03-10 21:46:58 +00009616 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9617 {
9618 EMSG(_(e_invarg));
9619 return;
9620 }
9621
9622 startcol = get_tv_number_chk(&argvars[0], NULL);
9623 if (startcol <= 0)
9624 return;
9625
9626 set_completion(startcol - 1, argvars[1].vval.v_list);
9627}
9628
9629/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009630 * "complete_add()" function
9631 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009632 static void
9633f_complete_add(argvars, rettv)
9634 typval_T *argvars;
9635 typval_T *rettv;
9636{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009637 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009638}
9639
9640/*
9641 * "complete_check()" function
9642 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009643 static void
9644f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009645 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009646 typval_T *rettv;
9647{
9648 int saved = RedrawingDisabled;
9649
9650 RedrawingDisabled = 0;
9651 ins_compl_check_keys(0);
9652 rettv->vval.v_number = compl_interrupted;
9653 RedrawingDisabled = saved;
9654}
9655#endif
9656
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657/*
9658 * "confirm(message, buttons[, default [, type]])" function
9659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009662 typval_T *argvars UNUSED;
9663 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9666 char_u *message;
9667 char_u *buttons = NULL;
9668 char_u buf[NUMBUFLEN];
9669 char_u buf2[NUMBUFLEN];
9670 int def = 1;
9671 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009672 char_u *typestr;
9673 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009675 message = get_tv_string_chk(&argvars[0]);
9676 if (message == NULL)
9677 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009678 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009680 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9681 if (buttons == NULL)
9682 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009683 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009685 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009686 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9689 if (typestr == NULL)
9690 error = TRUE;
9691 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009693 switch (TOUPPER_ASC(*typestr))
9694 {
9695 case 'E': type = VIM_ERROR; break;
9696 case 'Q': type = VIM_QUESTION; break;
9697 case 'I': type = VIM_INFO; break;
9698 case 'W': type = VIM_WARNING; break;
9699 case 'G': type = VIM_GENERIC; break;
9700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 }
9702 }
9703 }
9704 }
9705
9706 if (buttons == NULL || *buttons == NUL)
9707 buttons = (char_u *)_("&Ok");
9708
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009709 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009710 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009711 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712#endif
9713}
9714
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009715/*
9716 * "copy()" function
9717 */
9718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009720 typval_T *argvars;
9721 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009722{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009723 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009724}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009726#ifdef FEAT_FLOAT
9727/*
9728 * "cos()" function
9729 */
9730 static void
9731f_cos(argvars, rettv)
9732 typval_T *argvars;
9733 typval_T *rettv;
9734{
9735 float_T f;
9736
9737 rettv->v_type = VAR_FLOAT;
9738 if (get_float_arg(argvars, &f) == OK)
9739 rettv->vval.v_float = cos(f);
9740 else
9741 rettv->vval.v_float = 0.0;
9742}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009743
9744/*
9745 * "cosh()" function
9746 */
9747 static void
9748f_cosh(argvars, rettv)
9749 typval_T *argvars;
9750 typval_T *rettv;
9751{
9752 float_T f;
9753
9754 rettv->v_type = VAR_FLOAT;
9755 if (get_float_arg(argvars, &f) == OK)
9756 rettv->vval.v_float = cosh(f);
9757 else
9758 rettv->vval.v_float = 0.0;
9759}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009760#endif
9761
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009763 * "count()" function
9764 */
9765 static void
9766f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009767 typval_T *argvars;
9768 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009769{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009770 long n = 0;
9771 int ic = FALSE;
9772
Bram Moolenaare9a41262005-01-15 22:18:47 +00009773 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009774 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009775 listitem_T *li;
9776 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009777 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009778
Bram Moolenaare9a41262005-01-15 22:18:47 +00009779 if ((l = argvars[0].vval.v_list) != NULL)
9780 {
9781 li = l->lv_first;
9782 if (argvars[2].v_type != VAR_UNKNOWN)
9783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 int error = FALSE;
9785
9786 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009787 if (argvars[3].v_type != VAR_UNKNOWN)
9788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 idx = get_tv_number_chk(&argvars[3], &error);
9790 if (!error)
9791 {
9792 li = list_find(l, idx);
9793 if (li == NULL)
9794 EMSGN(_(e_listidx), idx);
9795 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009796 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009797 if (error)
9798 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009799 }
9800
9801 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009802 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009803 ++n;
9804 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009805 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009806 else if (argvars[0].v_type == VAR_DICT)
9807 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009808 int todo;
9809 dict_T *d;
9810 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009811
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009812 if ((d = argvars[0].vval.v_dict) != NULL)
9813 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009814 int error = FALSE;
9815
Bram Moolenaare9a41262005-01-15 22:18:47 +00009816 if (argvars[2].v_type != VAR_UNKNOWN)
9817 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009818 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009819 if (argvars[3].v_type != VAR_UNKNOWN)
9820 EMSG(_(e_invarg));
9821 }
9822
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009823 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009824 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009825 {
9826 if (!HASHITEM_EMPTY(hi))
9827 {
9828 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009829 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009830 ++n;
9831 }
9832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009833 }
9834 }
9835 else
9836 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009837 rettv->vval.v_number = n;
9838}
9839
9840/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9842 *
9843 * Checks the existence of a cscope connection.
9844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009846f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009847 typval_T *argvars UNUSED;
9848 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849{
9850#ifdef FEAT_CSCOPE
9851 int num = 0;
9852 char_u *dbpath = NULL;
9853 char_u *prepend = NULL;
9854 char_u buf[NUMBUFLEN];
9855
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009856 if (argvars[0].v_type != VAR_UNKNOWN
9857 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859 num = (int)get_tv_number(&argvars[0]);
9860 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009861 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 }
9864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866#endif
9867}
9868
9869/*
9870 * "cursor(lnum, col)" function
9871 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009872 * Moves the cursor to the specified line and column.
9873 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009877 typval_T *argvars;
9878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879{
9880 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009881#ifdef FEAT_VIRTUALEDIT
9882 long coladd = 0;
9883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009884
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009885 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009886 if (argvars[1].v_type == VAR_UNKNOWN)
9887 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009888 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +02009889 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009890
Bram Moolenaar493c1782014-05-28 14:34:46 +02009891 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009892 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009893 line = pos.lnum;
9894 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009895#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009896 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009897#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +02009898 if (curswant >= 0)
9899 curwin->w_curswant = curswant - 1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009900 }
9901 else
9902 {
9903 line = get_tv_lnum(argvars);
9904 col = get_tv_number_chk(&argvars[1], NULL);
9905#ifdef FEAT_VIRTUALEDIT
9906 if (argvars[2].v_type != VAR_UNKNOWN)
9907 coladd = get_tv_number_chk(&argvars[2], NULL);
9908#endif
9909 }
9910 if (line < 0 || col < 0
9911#ifdef FEAT_VIRTUALEDIT
9912 || coladd < 0
9913#endif
9914 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009915 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 if (line > 0)
9917 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 if (col > 0)
9919 curwin->w_cursor.col = col - 1;
9920#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009921 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922#endif
9923
9924 /* Make sure the cursor is in a valid position. */
9925 check_cursor();
9926#ifdef FEAT_MBYTE
9927 /* Correct cursor for multi-byte character. */
9928 if (has_mbyte)
9929 mb_adjust_cursor();
9930#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009931
9932 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009933 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934}
9935
9936/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009937 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 */
9939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009940f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009941 typval_T *argvars;
9942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009944 int noref = 0;
9945
9946 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009948 if (noref < 0 || noref > 1)
9949 EMSG(_(e_invarg));
9950 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009951 {
9952 current_copyID += COPYID_INC;
9953 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955}
9956
9957/*
9958 * "delete()" function
9959 */
9960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009961f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009962 typval_T *argvars;
9963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964{
9965 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009966 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009968 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969}
9970
9971/*
9972 * "did_filetype()" function
9973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009975f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009976 typval_T *argvars UNUSED;
9977 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
9979#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981#endif
9982}
9983
9984/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009985 * "diff_filler()" function
9986 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009988f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009989 typval_T *argvars UNUSED;
9990 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009991{
9992#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009993 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009994#endif
9995}
9996
9997/*
9998 * "diff_hlID()" function
9999 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010002 typval_T *argvars UNUSED;
10003 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010004{
10005#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010006 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010007 static linenr_T prev_lnum = 0;
10008 static int changedtick = 0;
10009 static int fnum = 0;
10010 static int change_start = 0;
10011 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010012 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010013 int filler_lines;
10014 int col;
10015
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010016 if (lnum < 0) /* ignore type error in {lnum} arg */
10017 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010018 if (lnum != prev_lnum
10019 || changedtick != curbuf->b_changedtick
10020 || fnum != curbuf->b_fnum)
10021 {
10022 /* New line, buffer, change: need to get the values. */
10023 filler_lines = diff_check(curwin, lnum);
10024 if (filler_lines < 0)
10025 {
10026 if (filler_lines == -1)
10027 {
10028 change_start = MAXCOL;
10029 change_end = -1;
10030 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10031 hlID = HLF_ADD; /* added line */
10032 else
10033 hlID = HLF_CHD; /* changed line */
10034 }
10035 else
10036 hlID = HLF_ADD; /* added line */
10037 }
10038 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010039 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010040 prev_lnum = lnum;
10041 changedtick = curbuf->b_changedtick;
10042 fnum = curbuf->b_fnum;
10043 }
10044
10045 if (hlID == HLF_CHD || hlID == HLF_TXD)
10046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010047 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010048 if (col >= change_start && col <= change_end)
10049 hlID = HLF_TXD; /* changed text */
10050 else
10051 hlID = HLF_CHD; /* changed line */
10052 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010053 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010054#endif
10055}
10056
10057/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010058 * "empty({expr})" function
10059 */
10060 static void
10061f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010064{
10065 int n;
10066
10067 switch (argvars[0].v_type)
10068 {
10069 case VAR_STRING:
10070 case VAR_FUNC:
10071 n = argvars[0].vval.v_string == NULL
10072 || *argvars[0].vval.v_string == NUL;
10073 break;
10074 case VAR_NUMBER:
10075 n = argvars[0].vval.v_number == 0;
10076 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010077#ifdef FEAT_FLOAT
10078 case VAR_FLOAT:
10079 n = argvars[0].vval.v_float == 0.0;
10080 break;
10081#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010082 case VAR_LIST:
10083 n = argvars[0].vval.v_list == NULL
10084 || argvars[0].vval.v_list->lv_first == NULL;
10085 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010086 case VAR_DICT:
10087 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010088 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010089 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010090 default:
10091 EMSG2(_(e_intern2), "f_empty()");
10092 n = 0;
10093 }
10094
10095 rettv->vval.v_number = n;
10096}
10097
10098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 * "escape({string}, {chars})" function
10100 */
10101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010103 typval_T *argvars;
10104 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
10106 char_u buf[NUMBUFLEN];
10107
Bram Moolenaar758711c2005-02-02 23:11:38 +000010108 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10109 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010110 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111}
10112
10113/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010114 * "eval()" function
10115 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010116 static void
10117f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010118 typval_T *argvars;
10119 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010120{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010121 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010122
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 s = get_tv_string_chk(&argvars[0]);
10124 if (s != NULL)
10125 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010126
Bram Moolenaar615b9972015-01-14 17:15:05 +010010127 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10129 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010130 if (p != NULL && !aborting())
10131 EMSG2(_(e_invexpr2), p);
10132 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010134 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010136 else if (*s != NUL)
10137 EMSG(_(e_trailing));
10138}
10139
10140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 * "eventhandler()" function
10142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010144f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010145 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149}
10150
10151/*
10152 * "executable()" function
10153 */
10154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010156 typval_T *argvars;
10157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158{
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010159 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]), NULL);
10160}
10161
10162/*
10163 * "exepath()" function
10164 */
10165 static void
10166f_exepath(argvars, rettv)
10167 typval_T *argvars;
10168 typval_T *rettv;
10169{
10170 char_u *p = NULL;
10171
10172 (void)mch_can_exe(get_tv_string(&argvars[0]), &p);
10173 rettv->v_type = VAR_STRING;
10174 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175}
10176
10177/*
10178 * "exists()" function
10179 */
10180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010182 typval_T *argvars;
10183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184{
10185 char_u *p;
10186 char_u *name;
10187 int n = FALSE;
10188 int len = 0;
10189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010190 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010191 if (*p == '$') /* environment variable */
10192 {
10193 /* first try "normal" environment variables (fast) */
10194 if (mch_getenv(p + 1) != NULL)
10195 n = TRUE;
10196 else
10197 {
10198 /* try expanding things like $VIM and ${HOME} */
10199 p = expand_env_save(p);
10200 if (p != NULL && *p != '$')
10201 n = TRUE;
10202 vim_free(p);
10203 }
10204 }
10205 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010208 if (*skipwhite(p) != NUL)
10209 n = FALSE; /* trailing garbage */
10210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 else if (*p == '*') /* internal or user defined function */
10212 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010213 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 }
10215 else if (*p == ':')
10216 {
10217 n = cmd_exists(p + 1);
10218 }
10219 else if (*p == '#')
10220 {
10221#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010222 if (p[1] == '#')
10223 n = autocmd_supported(p + 2);
10224 else
10225 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226#endif
10227 }
10228 else /* internal variable */
10229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010230 char_u *tofree;
10231 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010233 /* get_name_len() takes care of expanding curly braces */
10234 name = p;
10235 len = get_name_len(&p, &tofree, TRUE, FALSE);
10236 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010238 if (tofree != NULL)
10239 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010240 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010241 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010243 /* handle d.key, l[idx], f(expr) */
10244 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10245 if (n)
10246 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 }
10248 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010249 if (*p != NUL)
10250 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010252 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 }
10254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256}
10257
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010258#ifdef FEAT_FLOAT
10259/*
10260 * "exp()" function
10261 */
10262 static void
10263f_exp(argvars, rettv)
10264 typval_T *argvars;
10265 typval_T *rettv;
10266{
10267 float_T f;
10268
10269 rettv->v_type = VAR_FLOAT;
10270 if (get_float_arg(argvars, &f) == OK)
10271 rettv->vval.v_float = exp(f);
10272 else
10273 rettv->vval.v_float = 0.0;
10274}
10275#endif
10276
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277/*
10278 * "expand()" function
10279 */
10280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010281f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010282 typval_T *argvars;
10283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284{
10285 char_u *s;
10286 int len;
10287 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010288 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010291 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010293 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010294 if (argvars[1].v_type != VAR_UNKNOWN
10295 && argvars[2].v_type != VAR_UNKNOWN
10296 && get_tv_number_chk(&argvars[2], &error)
10297 && !error)
10298 {
10299 rettv->v_type = VAR_LIST;
10300 rettv->vval.v_list = NULL;
10301 }
10302
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304 if (*s == '%' || *s == '#' || *s == '<')
10305 {
10306 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010307 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010309 if (rettv->v_type == VAR_LIST)
10310 {
10311 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10312 list_append_string(rettv->vval.v_list, result, -1);
10313 else
10314 vim_free(result);
10315 }
10316 else
10317 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 }
10319 else
10320 {
10321 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010322 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010323 if (argvars[1].v_type != VAR_UNKNOWN
10324 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010325 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010326 if (!error)
10327 {
10328 ExpandInit(&xpc);
10329 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010330 if (p_wic)
10331 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010332 if (rettv->v_type == VAR_STRING)
10333 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10334 options, WILD_ALL);
10335 else if (rettv_list_alloc(rettv) != FAIL)
10336 {
10337 int i;
10338
10339 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10340 for (i = 0; i < xpc.xp_numfiles; i++)
10341 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10342 ExpandCleanup(&xpc);
10343 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 }
10345 else
10346 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 }
10348}
10349
10350/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010351 * Go over all entries in "d2" and add them to "d1".
10352 * When "action" is "error" then a duplicate key is an error.
10353 * When "action" is "force" then a duplicate key is overwritten.
10354 * Otherwise duplicate keys are ignored ("action" is "keep").
10355 */
10356 void
10357dict_extend(d1, d2, action)
10358 dict_T *d1;
10359 dict_T *d2;
10360 char_u *action;
10361{
10362 dictitem_T *di1;
10363 hashitem_T *hi2;
10364 int todo;
10365
10366 todo = (int)d2->dv_hashtab.ht_used;
10367 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10368 {
10369 if (!HASHITEM_EMPTY(hi2))
10370 {
10371 --todo;
10372 di1 = dict_find(d1, hi2->hi_key, -1);
10373 if (d1->dv_scope != 0)
10374 {
10375 /* Disallow replacing a builtin function in l: and g:.
10376 * Check the key to be valid when adding to any
10377 * scope. */
10378 if (d1->dv_scope == VAR_DEF_SCOPE
10379 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10380 && var_check_func_name(hi2->hi_key,
10381 di1 == NULL))
10382 break;
10383 if (!valid_varname(hi2->hi_key))
10384 break;
10385 }
10386 if (di1 == NULL)
10387 {
10388 di1 = dictitem_copy(HI2DI(hi2));
10389 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10390 dictitem_free(di1);
10391 }
10392 else if (*action == 'e')
10393 {
10394 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10395 break;
10396 }
10397 else if (*action == 'f' && HI2DI(hi2) != di1)
10398 {
10399 clear_tv(&di1->di_tv);
10400 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10401 }
10402 }
10403 }
10404}
10405
10406/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010407 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010408 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010409 */
10410 static void
10411f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010412 typval_T *argvars;
10413 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010414{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010415 char *arg_errmsg = N_("extend() argument");
10416
Bram Moolenaare9a41262005-01-15 22:18:47 +000010417 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010418 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010419 list_T *l1, *l2;
10420 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010421 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010422 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010423
Bram Moolenaare9a41262005-01-15 22:18:47 +000010424 l1 = argvars[0].vval.v_list;
10425 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010426 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010427 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010428 {
10429 if (argvars[2].v_type != VAR_UNKNOWN)
10430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010431 before = get_tv_number_chk(&argvars[2], &error);
10432 if (error)
10433 return; /* type error; errmsg already given */
10434
Bram Moolenaar758711c2005-02-02 23:11:38 +000010435 if (before == l1->lv_len)
10436 item = NULL;
10437 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010438 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010439 item = list_find(l1, before);
10440 if (item == NULL)
10441 {
10442 EMSGN(_(e_listidx), before);
10443 return;
10444 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010445 }
10446 }
10447 else
10448 item = NULL;
10449 list_extend(l1, l2, item);
10450
Bram Moolenaare9a41262005-01-15 22:18:47 +000010451 copy_tv(&argvars[0], rettv);
10452 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010453 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010454 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10455 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010456 dict_T *d1, *d2;
10457 char_u *action;
10458 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010459
10460 d1 = argvars[0].vval.v_dict;
10461 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010462 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010463 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010464 {
10465 /* Check the third argument. */
10466 if (argvars[2].v_type != VAR_UNKNOWN)
10467 {
10468 static char *(av[]) = {"keep", "force", "error"};
10469
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010470 action = get_tv_string_chk(&argvars[2]);
10471 if (action == NULL)
10472 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010473 for (i = 0; i < 3; ++i)
10474 if (STRCMP(action, av[i]) == 0)
10475 break;
10476 if (i == 3)
10477 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010478 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010479 return;
10480 }
10481 }
10482 else
10483 action = (char_u *)"force";
10484
Bram Moolenaara9922d62013-05-30 13:01:18 +020010485 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486
Bram Moolenaare9a41262005-01-15 22:18:47 +000010487 copy_tv(&argvars[0], rettv);
10488 }
10489 }
10490 else
10491 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010492}
10493
10494/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010495 * "feedkeys()" function
10496 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010497 static void
10498f_feedkeys(argvars, rettv)
10499 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010500 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010501{
10502 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010503 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010504 char_u *keys, *flags;
10505 char_u nbuf[NUMBUFLEN];
10506 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010507 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010508
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010509 /* This is not allowed in the sandbox. If the commands would still be
10510 * executed in the sandbox it would be OK, but it probably happens later,
10511 * when "sandbox" is no longer set. */
10512 if (check_secure())
10513 return;
10514
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010515 keys = get_tv_string(&argvars[0]);
10516 if (*keys != NUL)
10517 {
10518 if (argvars[1].v_type != VAR_UNKNOWN)
10519 {
10520 flags = get_tv_string_buf(&argvars[1], nbuf);
10521 for ( ; *flags != NUL; ++flags)
10522 {
10523 switch (*flags)
10524 {
10525 case 'n': remap = FALSE; break;
10526 case 'm': remap = TRUE; break;
10527 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010528 case 'i': insert = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010529 }
10530 }
10531 }
10532
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010533 /* Need to escape K_SPECIAL and CSI before putting the string in the
10534 * typeahead buffer. */
10535 keys_esc = vim_strsave_escape_csi(keys);
10536 if (keys_esc != NULL)
10537 {
10538 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010010539 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010540 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010541 if (vgetc_busy)
10542 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010543 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010544 }
10545}
10546
10547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 * "filereadable()" function
10549 */
10550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010551f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010552 typval_T *argvars;
10553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010555 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 char_u *p;
10557 int n;
10558
Bram Moolenaarc236c162008-07-13 17:41:49 +000010559#ifndef O_NONBLOCK
10560# define O_NONBLOCK 0
10561#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010562 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010563 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10564 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 {
10566 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010567 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568 }
10569 else
10570 n = FALSE;
10571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010572 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573}
10574
10575/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010576 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 * rights to write into.
10578 */
10579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010580f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010581 typval_T *argvars;
10582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010584 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585}
10586
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010587static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010588
10589 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010590findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010591 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010592 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010593 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010594{
10595#ifdef FEAT_SEARCHPATH
10596 char_u *fname;
10597 char_u *fresult = NULL;
10598 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10599 char_u *p;
10600 char_u pathbuf[NUMBUFLEN];
10601 int count = 1;
10602 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010603 int error = FALSE;
10604#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010605
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010606 rettv->vval.v_string = NULL;
10607 rettv->v_type = VAR_STRING;
10608
10609#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010610 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010611
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010612 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10615 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010616 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010617 else
10618 {
10619 if (*p != NUL)
10620 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010622 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010623 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010624 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010625 }
10626
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010627 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10628 error = TRUE;
10629
10630 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 do
10633 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010634 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010635 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010636 fresult = find_file_in_path_option(first ? fname : NULL,
10637 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010638 0, first, path,
10639 find_what,
10640 curbuf->b_ffname,
10641 find_what == FINDFILE_DIR
10642 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010643 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010644
10645 if (fresult != NULL && rettv->v_type == VAR_LIST)
10646 list_append_string(rettv->vval.v_list, fresult, -1);
10647
10648 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010649 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010650
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010651 if (rettv->v_type == VAR_STRING)
10652 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010653#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010654}
10655
Bram Moolenaar33570922005-01-25 22:26:29 +000010656static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10657static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010658
10659/*
10660 * Implementation of map() and filter().
10661 */
10662 static void
10663filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010664 typval_T *argvars;
10665 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010666 int map;
10667{
10668 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010669 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010670 listitem_T *li, *nli;
10671 list_T *l = NULL;
10672 dictitem_T *di;
10673 hashtab_T *ht;
10674 hashitem_T *hi;
10675 dict_T *d = NULL;
10676 typval_T save_val;
10677 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010678 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010679 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010680 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10681 char *arg_errmsg = (map ? N_("map() argument")
10682 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010683 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010684 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010685
Bram Moolenaare9a41262005-01-15 22:18:47 +000010686 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010687 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010688 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010689 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010690 return;
10691 }
10692 else if (argvars[0].v_type == VAR_DICT)
10693 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010694 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010695 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010696 return;
10697 }
10698 else
10699 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010700 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010701 return;
10702 }
10703
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010704 expr = get_tv_string_buf_chk(&argvars[1], buf);
10705 /* On type errors, the preceding call has already displayed an error
10706 * message. Avoid a misleading error message for an empty string that
10707 * was not passed as argument. */
10708 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 prepare_vimvar(VV_VAL, &save_val);
10711 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010712
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010713 /* We reset "did_emsg" to be able to detect whether an error
10714 * occurred during evaluation of the expression. */
10715 save_did_emsg = did_emsg;
10716 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010717
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010718 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010719 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 vimvars[VV_KEY].vv_type = VAR_STRING;
10722
10723 ht = &d->dv_hashtab;
10724 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010725 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010726 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010727 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010728 if (!HASHITEM_EMPTY(hi))
10729 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010730 int r;
10731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 --todo;
10733 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010734 if (tv_check_lock(di->di_tv.v_lock,
10735 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010736 break;
10737 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010738 r = filter_map_one(&di->di_tv, expr, map, &rem);
10739 clear_tv(&vimvars[VV_KEY].vv_tv);
10740 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 break;
10742 if (!map && rem)
10743 dictitem_remove(d, di);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010744 }
10745 }
10746 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010747 }
10748 else
10749 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010750 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10751
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010752 for (li = l->lv_first; li != NULL; li = nli)
10753 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010754 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010755 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010756 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010757 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010758 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010759 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010760 break;
10761 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010762 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010763 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010764 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010765 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010766
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010767 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010768 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010769
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010770 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010771 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010772
10773 copy_tv(&argvars[0], rettv);
10774}
10775
10776 static int
10777filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010778 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010779 char_u *expr;
10780 int map;
10781 int *remp;
10782{
Bram Moolenaar33570922005-01-25 22:26:29 +000010783 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010784 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010785 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010786
Bram Moolenaar33570922005-01-25 22:26:29 +000010787 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010788 s = expr;
10789 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010790 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010791 if (*s != NUL) /* check for trailing chars after expr */
10792 {
10793 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010010794 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010795 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010796 }
10797 if (map)
10798 {
10799 /* map(): replace the list item value */
10800 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010801 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010802 *tv = rettv;
10803 }
10804 else
10805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010806 int error = FALSE;
10807
Bram Moolenaare9a41262005-01-15 22:18:47 +000010808 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010810 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 /* On type error, nothing has been removed; return FAIL to stop the
10812 * loop. The error message was given by get_tv_number_chk(). */
10813 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010814 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010815 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010816 retval = OK;
10817theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010818 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010819 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010820}
10821
10822/*
10823 * "filter()" function
10824 */
10825 static void
10826f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010827 typval_T *argvars;
10828 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010829{
10830 filter_map(argvars, rettv, FALSE);
10831}
10832
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010833/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010834 * "finddir({fname}[, {path}[, {count}]])" function
10835 */
10836 static void
10837f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010838 typval_T *argvars;
10839 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010840{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010841 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010842}
10843
10844/*
10845 * "findfile({fname}[, {path}[, {count}]])" function
10846 */
10847 static void
10848f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 typval_T *argvars;
10850 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010851{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010852 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010853}
10854
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010855#ifdef FEAT_FLOAT
10856/*
10857 * "float2nr({float})" function
10858 */
10859 static void
10860f_float2nr(argvars, rettv)
10861 typval_T *argvars;
10862 typval_T *rettv;
10863{
10864 float_T f;
10865
10866 if (get_float_arg(argvars, &f) == OK)
10867 {
10868 if (f < -0x7fffffff)
10869 rettv->vval.v_number = -0x7fffffff;
10870 else if (f > 0x7fffffff)
10871 rettv->vval.v_number = 0x7fffffff;
10872 else
10873 rettv->vval.v_number = (varnumber_T)f;
10874 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010875}
10876
10877/*
10878 * "floor({float})" function
10879 */
10880 static void
10881f_floor(argvars, rettv)
10882 typval_T *argvars;
10883 typval_T *rettv;
10884{
10885 float_T f;
10886
10887 rettv->v_type = VAR_FLOAT;
10888 if (get_float_arg(argvars, &f) == OK)
10889 rettv->vval.v_float = floor(f);
10890 else
10891 rettv->vval.v_float = 0.0;
10892}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010893
10894/*
10895 * "fmod()" function
10896 */
10897 static void
10898f_fmod(argvars, rettv)
10899 typval_T *argvars;
10900 typval_T *rettv;
10901{
10902 float_T fx, fy;
10903
10904 rettv->v_type = VAR_FLOAT;
10905 if (get_float_arg(argvars, &fx) == OK
10906 && get_float_arg(&argvars[1], &fy) == OK)
10907 rettv->vval.v_float = fmod(fx, fy);
10908 else
10909 rettv->vval.v_float = 0.0;
10910}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010911#endif
10912
Bram Moolenaar0d660222005-01-07 21:51:51 +000010913/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010914 * "fnameescape({string})" function
10915 */
10916 static void
10917f_fnameescape(argvars, rettv)
10918 typval_T *argvars;
10919 typval_T *rettv;
10920{
10921 rettv->vval.v_string = vim_strsave_fnameescape(
10922 get_tv_string(&argvars[0]), FALSE);
10923 rettv->v_type = VAR_STRING;
10924}
10925
10926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 * "fnamemodify({fname}, {mods})" function
10928 */
10929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010931 typval_T *argvars;
10932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933{
10934 char_u *fname;
10935 char_u *mods;
10936 int usedlen = 0;
10937 int len;
10938 char_u *fbuf = NULL;
10939 char_u buf[NUMBUFLEN];
10940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010941 fname = get_tv_string_chk(&argvars[0]);
10942 mods = get_tv_string_buf_chk(&argvars[1], buf);
10943 if (fname == NULL || mods == NULL)
10944 fname = NULL;
10945 else
10946 {
10947 len = (int)STRLEN(fname);
10948 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010951 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 vim_free(fbuf);
10957}
10958
Bram Moolenaar33570922005-01-25 22:26:29 +000010959static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960
10961/*
10962 * "foldclosed()" function
10963 */
10964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010966 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010967 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010968 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969{
10970#ifdef FEAT_FOLDING
10971 linenr_T lnum;
10972 linenr_T first, last;
10973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10976 {
10977 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10978 {
10979 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010980 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 return;
10984 }
10985 }
10986#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988}
10989
10990/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010991 * "foldclosed()" function
10992 */
10993 static void
10994f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010995 typval_T *argvars;
10996 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010997{
10998 foldclosed_both(argvars, rettv, FALSE);
10999}
11000
11001/*
11002 * "foldclosedend()" function
11003 */
11004 static void
11005f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011006 typval_T *argvars;
11007 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011008{
11009 foldclosed_both(argvars, rettv, TRUE);
11010}
11011
11012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 * "foldlevel()" function
11014 */
11015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011017 typval_T *argvars UNUSED;
11018 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019{
11020#ifdef FEAT_FOLDING
11021 linenr_T lnum;
11022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027}
11028
11029/*
11030 * "foldtext()" function
11031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011034 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036{
11037#ifdef FEAT_FOLDING
11038 linenr_T lnum;
11039 char_u *s;
11040 char_u *r;
11041 int len;
11042 char *txt;
11043#endif
11044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->v_type = VAR_STRING;
11046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011048 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11049 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11050 <= curbuf->b_ml.ml_line_count
11051 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 {
11053 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011054 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11055 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 {
11057 if (!linewhite(lnum))
11058 break;
11059 ++lnum;
11060 }
11061
11062 /* Find interesting text in this line. */
11063 s = skipwhite(ml_get(lnum));
11064 /* skip C comment-start */
11065 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011068 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011069 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011070 {
11071 s = skipwhite(ml_get(lnum + 1));
11072 if (*s == '*')
11073 s = skipwhite(s + 1);
11074 }
11075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011076 txt = _("+-%s%3ld lines: ");
11077 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011078 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 + 20 /* for %3ld */
11080 + STRLEN(s))); /* concatenated */
11081 if (r != NULL)
11082 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011083 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11084 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11085 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011086 len = (int)STRLEN(r);
11087 STRCAT(r, s);
11088 /* remove 'foldmarker' and 'commentstring' */
11089 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011090 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091 }
11092 }
11093#endif
11094}
11095
11096/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011097 * "foldtextresult(lnum)" function
11098 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011101 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011103{
11104#ifdef FEAT_FOLDING
11105 linenr_T lnum;
11106 char_u *text;
11107 char_u buf[51];
11108 foldinfo_T foldinfo;
11109 int fold_count;
11110#endif
11111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->v_type = VAR_STRING;
11113 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011114#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011116 /* treat illegal types and illegal string values for {lnum} the same */
11117 if (lnum < 0)
11118 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011119 fold_count = foldedCount(curwin, lnum, &foldinfo);
11120 if (fold_count > 0)
11121 {
11122 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11123 &foldinfo, buf);
11124 if (text == buf)
11125 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011127 }
11128#endif
11129}
11130
11131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132 * "foreground()" function
11133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011136 typval_T *argvars UNUSED;
11137 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139#ifdef FEAT_GUI
11140 if (gui.in_use)
11141 gui_mch_set_foreground();
11142#else
11143# ifdef WIN32
11144 win32_set_foreground();
11145# endif
11146#endif
11147}
11148
11149/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011150 * "function()" function
11151 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011153f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 typval_T *argvars;
11155 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011156{
11157 char_u *s;
11158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011159 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011160 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011161 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011162 /* Don't check an autoload name for existence here. */
11163 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011164 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011165 else
11166 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011167 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011168 {
11169 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011170 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011171
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011172 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11173 * also be called from another script. Using trans_function_name()
11174 * would also work, but some plugins depend on the name being
11175 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011176 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011177 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011178 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011179 if (rettv->vval.v_string != NULL)
11180 {
11181 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011182 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011183 }
11184 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011185 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011186 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011187 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011188 }
11189}
11190
11191/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011192 * "garbagecollect()" function
11193 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011194 static void
11195f_garbagecollect(argvars, rettv)
11196 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011197 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011198{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011199 /* This is postponed until we are back at the toplevel, because we may be
11200 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11201 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011202
11203 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11204 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011205}
11206
11207/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011208 * "get()" function
11209 */
11210 static void
11211f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011212 typval_T *argvars;
11213 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011214{
Bram Moolenaar33570922005-01-25 22:26:29 +000011215 listitem_T *li;
11216 list_T *l;
11217 dictitem_T *di;
11218 dict_T *d;
11219 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011220
Bram Moolenaare9a41262005-01-15 22:18:47 +000011221 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011222 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011223 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011225 int error = FALSE;
11226
11227 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11228 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011229 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011230 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011232 else if (argvars[0].v_type == VAR_DICT)
11233 {
11234 if ((d = argvars[0].vval.v_dict) != NULL)
11235 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011236 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011237 if (di != NULL)
11238 tv = &di->di_tv;
11239 }
11240 }
11241 else
11242 EMSG2(_(e_listdictarg), "get()");
11243
11244 if (tv == NULL)
11245 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011246 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011247 copy_tv(&argvars[2], rettv);
11248 }
11249 else
11250 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011251}
11252
Bram Moolenaar342337a2005-07-21 21:11:17 +000011253static 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 +000011254
11255/*
11256 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011257 * Return a range (from start to end) of lines in rettv from the specified
11258 * buffer.
11259 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011260 */
11261 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011262get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011263 buf_T *buf;
11264 linenr_T start;
11265 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011266 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011267 typval_T *rettv;
11268{
11269 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011270
Bram Moolenaar959a1432013-12-14 12:17:38 +010011271 rettv->v_type = VAR_STRING;
11272 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011273 if (retlist && rettv_list_alloc(rettv) == FAIL)
11274 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011275
11276 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11277 return;
11278
11279 if (!retlist)
11280 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011281 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11282 p = ml_get_buf(buf, start, FALSE);
11283 else
11284 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011285 rettv->vval.v_string = vim_strsave(p);
11286 }
11287 else
11288 {
11289 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011290 return;
11291
11292 if (start < 1)
11293 start = 1;
11294 if (end > buf->b_ml.ml_line_count)
11295 end = buf->b_ml.ml_line_count;
11296 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011297 if (list_append_string(rettv->vval.v_list,
11298 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011299 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011300 }
11301}
11302
11303/*
11304 * "getbufline()" function
11305 */
11306 static void
11307f_getbufline(argvars, rettv)
11308 typval_T *argvars;
11309 typval_T *rettv;
11310{
11311 linenr_T lnum;
11312 linenr_T end;
11313 buf_T *buf;
11314
11315 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11316 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011317 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011318 --emsg_off;
11319
Bram Moolenaar661b1822005-07-28 22:36:45 +000011320 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011321 if (argvars[2].v_type == VAR_UNKNOWN)
11322 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011323 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011324 end = get_tv_lnum_buf(&argvars[2], buf);
11325
Bram Moolenaar342337a2005-07-21 21:11:17 +000011326 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011327}
11328
Bram Moolenaar0d660222005-01-07 21:51:51 +000011329/*
11330 * "getbufvar()" function
11331 */
11332 static void
11333f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011334 typval_T *argvars;
11335 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011336{
11337 buf_T *buf;
11338 buf_T *save_curbuf;
11339 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011340 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011341 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011343 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11344 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011345 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011346 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011347
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011348 rettv->v_type = VAR_STRING;
11349 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011350
11351 if (buf != NULL && varname != NULL)
11352 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011353 /* set curbuf to be our buf, temporarily */
11354 save_curbuf = curbuf;
11355 curbuf = buf;
11356
Bram Moolenaar0d660222005-01-07 21:51:51 +000011357 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011358 {
11359 if (get_option_tv(&varname, rettv, TRUE) == OK)
11360 done = TRUE;
11361 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011362 else if (STRCMP(varname, "changedtick") == 0)
11363 {
11364 rettv->v_type = VAR_NUMBER;
11365 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011366 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011367 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011368 else
11369 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011370 /* Look up the variable. */
11371 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11372 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11373 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011374 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011375 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011376 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011377 done = TRUE;
11378 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011379 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011380
11381 /* restore previous notion of curbuf */
11382 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011383 }
11384
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011385 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11386 /* use the default value */
11387 copy_tv(&argvars[2], rettv);
11388
Bram Moolenaar0d660222005-01-07 21:51:51 +000011389 --emsg_off;
11390}
11391
11392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 * "getchar()" function
11394 */
11395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *argvars;
11398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
11400 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011401 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011403 /* Position the cursor. Needed after a message that ends in a space. */
11404 windgoto(msg_row, msg_col);
11405
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 ++no_mapping;
11407 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011408 for (;;)
11409 {
11410 if (argvars[0].v_type == VAR_UNKNOWN)
11411 /* getchar(): blocking wait. */
11412 n = safe_vgetc();
11413 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11414 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011415 n = vpeekc_any();
11416 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011417 /* illegal argument or getchar(0) and no char avail: return zero */
11418 n = 0;
11419 else
11420 /* getchar(0) and char avail: return char */
11421 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020011422
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011423 if (n == K_IGNORE)
11424 continue;
11425 break;
11426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011427 --no_mapping;
11428 --allow_keys;
11429
Bram Moolenaar219b8702006-11-01 14:32:36 +000011430 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11431 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11432 vimvars[VV_MOUSE_COL].vv_nr = 0;
11433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011434 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 if (IS_SPECIAL(n) || mod_mask != 0)
11436 {
11437 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11438 int i = 0;
11439
11440 /* Turn a special key into three bytes, plus modifier. */
11441 if (mod_mask != 0)
11442 {
11443 temp[i++] = K_SPECIAL;
11444 temp[i++] = KS_MODIFIER;
11445 temp[i++] = mod_mask;
11446 }
11447 if (IS_SPECIAL(n))
11448 {
11449 temp[i++] = K_SPECIAL;
11450 temp[i++] = K_SECOND(n);
11451 temp[i++] = K_THIRD(n);
11452 }
11453#ifdef FEAT_MBYTE
11454 else if (has_mbyte)
11455 i += (*mb_char2bytes)(n, temp + i);
11456#endif
11457 else
11458 temp[i++] = n;
11459 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->v_type = VAR_STRING;
11461 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011462
11463#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011464 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011465 {
11466 int row = mouse_row;
11467 int col = mouse_col;
11468 win_T *win;
11469 linenr_T lnum;
11470# ifdef FEAT_WINDOWS
11471 win_T *wp;
11472# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011473 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011474
11475 if (row >= 0 && col >= 0)
11476 {
11477 /* Find the window at the mouse coordinates and compute the
11478 * text position. */
11479 win = mouse_find_win(&row, &col);
11480 (void)mouse_comp_pos(win, &row, &col, &lnum);
11481# ifdef FEAT_WINDOWS
11482 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011483 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011484# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011485 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011486 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11487 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11488 }
11489 }
11490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 }
11492}
11493
11494/*
11495 * "getcharmod()" function
11496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011498f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011499 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011500 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503}
11504
11505/*
11506 * "getcmdline()" function
11507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011510 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 rettv->v_type = VAR_STRING;
11514 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515}
11516
11517/*
11518 * "getcmdpos()" function
11519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011522 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011525 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526}
11527
11528/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011529 * "getcmdtype()" function
11530 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011531 static void
11532f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011533 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011534 typval_T *rettv;
11535{
11536 rettv->v_type = VAR_STRING;
11537 rettv->vval.v_string = alloc(2);
11538 if (rettv->vval.v_string != NULL)
11539 {
11540 rettv->vval.v_string[0] = get_cmdline_type();
11541 rettv->vval.v_string[1] = NUL;
11542 }
11543}
11544
11545/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020011546 * "getcmdwintype()" function
11547 */
11548 static void
11549f_getcmdwintype(argvars, rettv)
11550 typval_T *argvars UNUSED;
11551 typval_T *rettv;
11552{
11553 rettv->v_type = VAR_STRING;
11554 rettv->vval.v_string = NULL;
11555#ifdef FEAT_CMDWIN
11556 rettv->vval.v_string = alloc(2);
11557 if (rettv->vval.v_string != NULL)
11558 {
11559 rettv->vval.v_string[0] = cmdwin_type;
11560 rettv->vval.v_string[1] = NUL;
11561 }
11562#endif
11563}
11564
11565/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 * "getcwd()" function
11567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011570 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011572{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011573 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011575 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011576 rettv->vval.v_string = NULL;
11577 cwd = alloc(MAXPATHL);
11578 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011580 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11581 {
11582 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011583#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011584 if (rettv->vval.v_string != NULL)
11585 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011587 }
11588 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 }
11590}
11591
11592/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011593 * "getfontname()" function
11594 */
11595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011596f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011597 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011598 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011599{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011600 rettv->v_type = VAR_STRING;
11601 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011602#ifdef FEAT_GUI
11603 if (gui.in_use)
11604 {
11605 GuiFont font;
11606 char_u *name = NULL;
11607
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011608 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011609 {
11610 /* Get the "Normal" font. Either the name saved by
11611 * hl_set_font_name() or from the font ID. */
11612 font = gui.norm_font;
11613 name = hl_get_font_name();
11614 }
11615 else
11616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011617 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011618 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11619 return;
11620 font = gui_mch_get_font(name, FALSE);
11621 if (font == NOFONT)
11622 return; /* Invalid font name, return empty string. */
11623 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011625 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011626 gui_mch_free_font(font);
11627 }
11628#endif
11629}
11630
11631/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011632 * "getfperm({fname})" function
11633 */
11634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011636 typval_T *argvars;
11637 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011638{
11639 char_u *fname;
11640 struct stat st;
11641 char_u *perm = NULL;
11642 char_u flags[] = "rwx";
11643 int i;
11644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011647 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011648 if (mch_stat((char *)fname, &st) >= 0)
11649 {
11650 perm = vim_strsave((char_u *)"---------");
11651 if (perm != NULL)
11652 {
11653 for (i = 0; i < 9; i++)
11654 {
11655 if (st.st_mode & (1 << (8 - i)))
11656 perm[i] = flags[i % 3];
11657 }
11658 }
11659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011660 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011661}
11662
11663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 * "getfsize({fname})" function
11665 */
11666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011668 typval_T *argvars;
11669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670{
11671 char_u *fname;
11672 struct stat st;
11673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011674 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677
11678 if (mch_stat((char *)fname, &st) >= 0)
11679 {
11680 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011685
11686 /* non-perfect check for overflow */
11687 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11688 rettv->vval.v_number = -2;
11689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690 }
11691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011692 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693}
11694
11695/*
11696 * "getftime({fname})" function
11697 */
11698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011700 typval_T *argvars;
11701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702{
11703 char_u *fname;
11704 struct stat st;
11705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011706 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011707
11708 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011711 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712}
11713
11714/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011715 * "getftype({fname})" function
11716 */
11717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011718f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011719 typval_T *argvars;
11720 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011721{
11722 char_u *fname;
11723 struct stat st;
11724 char_u *type = NULL;
11725 char *t;
11726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011727 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011729 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011730 if (mch_lstat((char *)fname, &st) >= 0)
11731 {
11732#ifdef S_ISREG
11733 if (S_ISREG(st.st_mode))
11734 t = "file";
11735 else if (S_ISDIR(st.st_mode))
11736 t = "dir";
11737# ifdef S_ISLNK
11738 else if (S_ISLNK(st.st_mode))
11739 t = "link";
11740# endif
11741# ifdef S_ISBLK
11742 else if (S_ISBLK(st.st_mode))
11743 t = "bdev";
11744# endif
11745# ifdef S_ISCHR
11746 else if (S_ISCHR(st.st_mode))
11747 t = "cdev";
11748# endif
11749# ifdef S_ISFIFO
11750 else if (S_ISFIFO(st.st_mode))
11751 t = "fifo";
11752# endif
11753# ifdef S_ISSOCK
11754 else if (S_ISSOCK(st.st_mode))
11755 t = "fifo";
11756# endif
11757 else
11758 t = "other";
11759#else
11760# ifdef S_IFMT
11761 switch (st.st_mode & S_IFMT)
11762 {
11763 case S_IFREG: t = "file"; break;
11764 case S_IFDIR: t = "dir"; break;
11765# ifdef S_IFLNK
11766 case S_IFLNK: t = "link"; break;
11767# endif
11768# ifdef S_IFBLK
11769 case S_IFBLK: t = "bdev"; break;
11770# endif
11771# ifdef S_IFCHR
11772 case S_IFCHR: t = "cdev"; break;
11773# endif
11774# ifdef S_IFIFO
11775 case S_IFIFO: t = "fifo"; break;
11776# endif
11777# ifdef S_IFSOCK
11778 case S_IFSOCK: t = "socket"; break;
11779# endif
11780 default: t = "other";
11781 }
11782# else
11783 if (mch_isdir(fname))
11784 t = "dir";
11785 else
11786 t = "file";
11787# endif
11788#endif
11789 type = vim_strsave((char_u *)t);
11790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011791 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011792}
11793
11794/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011795 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011796 */
11797 static void
11798f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011799 typval_T *argvars;
11800 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011801{
11802 linenr_T lnum;
11803 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011804 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011805
11806 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011807 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011808 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011809 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011810 retlist = FALSE;
11811 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011812 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011813 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011814 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011815 retlist = TRUE;
11816 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011817
Bram Moolenaar342337a2005-07-21 21:11:17 +000011818 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011819}
11820
11821/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011822 * "getmatches()" function
11823 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011824 static void
11825f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011826 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011827 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011828{
11829#ifdef FEAT_SEARCH_EXTRA
11830 dict_T *dict;
11831 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011832 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011833
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011834 if (rettv_list_alloc(rettv) == OK)
11835 {
11836 while (cur != NULL)
11837 {
11838 dict = dict_alloc();
11839 if (dict == NULL)
11840 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020011841 if (cur->match.regprog == NULL)
11842 {
11843 /* match added with matchaddpos() */
11844 for (i = 0; i < MAXPOSMATCH; ++i)
11845 {
11846 llpos_T *llpos;
11847 char buf[6];
11848 list_T *l;
11849
11850 llpos = &cur->pos.pos[i];
11851 if (llpos->lnum == 0)
11852 break;
11853 l = list_alloc();
11854 if (l == NULL)
11855 break;
11856 list_append_number(l, (varnumber_T)llpos->lnum);
11857 if (llpos->col > 0)
11858 {
11859 list_append_number(l, (varnumber_T)llpos->col);
11860 list_append_number(l, (varnumber_T)llpos->len);
11861 }
11862 sprintf(buf, "pos%d", i + 1);
11863 dict_add_list(dict, buf, l);
11864 }
11865 }
11866 else
11867 {
11868 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11869 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011870 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011871 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11872 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11873 list_append_dict(rettv->vval.v_list, dict);
11874 cur = cur->next;
11875 }
11876 }
11877#endif
11878}
11879
11880/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011881 * "getpid()" function
11882 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011883 static void
11884f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011885 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011886 typval_T *rettv;
11887{
11888 rettv->vval.v_number = mch_get_pid();
11889}
11890
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011891static void getpos_both __ARGS((typval_T *argvars, typval_T *rettv, int getcurpos));
11892
11893/*
11894 * "getcurpos()" function
11895 */
11896 static void
11897f_getcurpos(argvars, rettv)
11898 typval_T *argvars;
11899 typval_T *rettv;
11900{
11901 getpos_both(argvars, rettv, TRUE);
11902}
11903
Bram Moolenaar18081e32008-02-20 19:11:07 +000011904/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011905 * "getpos(string)" function
11906 */
11907 static void
11908f_getpos(argvars, rettv)
11909 typval_T *argvars;
11910 typval_T *rettv;
11911{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011912 getpos_both(argvars, rettv, FALSE);
11913}
11914
11915 static void
11916getpos_both(argvars, rettv, getcurpos)
11917 typval_T *argvars;
11918 typval_T *rettv;
11919 int getcurpos;
11920{
Bram Moolenaara5525202006-03-02 22:52:09 +000011921 pos_T *fp;
11922 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011923 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011924
11925 if (rettv_list_alloc(rettv) == OK)
11926 {
11927 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011928 if (getcurpos)
11929 fp = &curwin->w_cursor;
11930 else
11931 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011932 if (fnum != -1)
11933 list_append_number(l, (varnumber_T)fnum);
11934 else
11935 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011936 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11937 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011938 list_append_number(l, (fp != NULL)
11939 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011940 : (varnumber_T)0);
11941 list_append_number(l,
11942#ifdef FEAT_VIRTUALEDIT
11943 (fp != NULL) ? (varnumber_T)fp->coladd :
11944#endif
11945 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020011946 if (getcurpos)
Bram Moolenaar084abae2015-01-14 19:00:38 +010011947 list_append_number(l, curwin->w_curswant == MAXCOL ?
11948 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaara5525202006-03-02 22:52:09 +000011949 }
11950 else
11951 rettv->vval.v_number = FALSE;
11952}
11953
11954/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011955 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011956 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011957 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011958f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011959 typval_T *argvars UNUSED;
11960 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011961{
11962#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011963 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011964#endif
11965
Bram Moolenaar2641f772005-03-25 21:58:17 +000011966#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011967 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011968 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011969 wp = NULL;
11970 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11971 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011972 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011973 if (wp == NULL)
11974 return;
11975 }
11976
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011977 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011978 }
11979#endif
11980}
11981
11982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 * "getreg()" function
11984 */
11985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011987 typval_T *argvars;
11988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989{
11990 char_u *strregname;
11991 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011992 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020011993 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011994 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011996 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011998 strregname = get_tv_string_chk(&argvars[0]);
11999 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012000 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012001 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012002 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012003 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12004 return_list = get_tv_number_chk(&argvars[2], &error);
12005 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012008 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012009
12010 if (error)
12011 return;
12012
Bram Moolenaar071d4272004-06-13 20:20:40 +000012013 regname = (strregname == NULL ? '"' : *strregname);
12014 if (regname == 0)
12015 regname = '"';
12016
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012017 if (return_list)
12018 {
12019 rettv->v_type = VAR_LIST;
12020 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12021 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012022 if (rettv->vval.v_list != NULL)
12023 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012024 }
12025 else
12026 {
12027 rettv->v_type = VAR_STRING;
12028 rettv->vval.v_string = get_reg_contents(regname,
12029 arg2 ? GREG_EXPR_SRC : 0);
12030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031}
12032
12033/*
12034 * "getregtype()" function
12035 */
12036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012038 typval_T *argvars;
12039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040{
12041 char_u *strregname;
12042 int regname;
12043 char_u buf[NUMBUFLEN + 2];
12044 long reglen = 0;
12045
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012046 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012047 {
12048 strregname = get_tv_string_chk(&argvars[0]);
12049 if (strregname == NULL) /* type error; errmsg already given */
12050 {
12051 rettv->v_type = VAR_STRING;
12052 rettv->vval.v_string = NULL;
12053 return;
12054 }
12055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 else
12057 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012058 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059
12060 regname = (strregname == NULL ? '"' : *strregname);
12061 if (regname == 0)
12062 regname = '"';
12063
12064 buf[0] = NUL;
12065 buf[1] = NUL;
12066 switch (get_reg_type(regname, &reglen))
12067 {
12068 case MLINE: buf[0] = 'V'; break;
12069 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 case MBLOCK:
12071 buf[0] = Ctrl_V;
12072 sprintf((char *)buf + 1, "%ld", reglen + 1);
12073 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012075 rettv->v_type = VAR_STRING;
12076 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077}
12078
12079/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012080 * "gettabvar()" function
12081 */
12082 static void
12083f_gettabvar(argvars, rettv)
12084 typval_T *argvars;
12085 typval_T *rettv;
12086{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012087 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012088 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012089 dictitem_T *v;
12090 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012091 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012092
12093 rettv->v_type = VAR_STRING;
12094 rettv->vval.v_string = NULL;
12095
12096 varname = get_tv_string_chk(&argvars[1]);
12097 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12098 if (tp != NULL && varname != NULL)
12099 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012100 /* Set tp to be our tabpage, temporarily. Also set the window to the
12101 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012102 if (switch_win(&oldcurwin, &oldtabpage, tp->tp_firstwin, tp, TRUE)
12103 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012104 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012105 /* look up the variable */
12106 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12107 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12108 if (v != NULL)
12109 {
12110 copy_tv(&v->di_tv, rettv);
12111 done = TRUE;
12112 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012113 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012114
12115 /* restore previous notion of curwin */
12116 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012117 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012118
12119 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012120 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012121}
12122
12123/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012124 * "gettabwinvar()" function
12125 */
12126 static void
12127f_gettabwinvar(argvars, rettv)
12128 typval_T *argvars;
12129 typval_T *rettv;
12130{
12131 getwinvar(argvars, rettv, 1);
12132}
12133
12134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 * "getwinposx()" function
12136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012139 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012140 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012142 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012143#ifdef FEAT_GUI
12144 if (gui.in_use)
12145 {
12146 int x, y;
12147
12148 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012149 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012150 }
12151#endif
12152}
12153
12154/*
12155 * "getwinposy()" function
12156 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012158f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012159 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012162 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163#ifdef FEAT_GUI
12164 if (gui.in_use)
12165 {
12166 int x, y;
12167
12168 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012169 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012170 }
12171#endif
12172}
12173
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012174/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012175 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012176 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012177 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012178find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012179 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012180 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000012181{
12182#ifdef FEAT_WINDOWS
12183 win_T *wp;
12184#endif
12185 int nr;
12186
12187 nr = get_tv_number_chk(vp, NULL);
12188
12189#ifdef FEAT_WINDOWS
12190 if (nr < 0)
12191 return NULL;
12192 if (nr == 0)
12193 return curwin;
12194
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012195 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
12196 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000012197 if (--nr <= 0)
12198 break;
12199 return wp;
12200#else
12201 if (nr == 0 || nr == 1)
12202 return curwin;
12203 return NULL;
12204#endif
12205}
12206
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207/*
12208 * "getwinvar()" function
12209 */
12210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012211f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012212 typval_T *argvars;
12213 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012214{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012215 getwinvar(argvars, rettv, 0);
12216}
12217
12218/*
12219 * getwinvar() and gettabwinvar()
12220 */
12221 static void
12222getwinvar(argvars, rettv, off)
12223 typval_T *argvars;
12224 typval_T *rettv;
12225 int off; /* 1 for gettabwinvar() */
12226{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 win_T *win, *oldcurwin;
12228 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012229 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020012230 tabpage_T *tp = NULL;
12231 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012232 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012234#ifdef FEAT_WINDOWS
12235 if (off == 1)
12236 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12237 else
12238 tp = curtab;
12239#endif
12240 win = find_win_by_nr(&argvars[off], tp);
12241 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012242 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012244 rettv->v_type = VAR_STRING;
12245 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246
12247 if (win != NULL && varname != NULL)
12248 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012249 /* Set curwin to be our win, temporarily. Also set the tabpage,
12250 * otherwise the window is not valid. */
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012251 if (switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012252 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012253 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012254 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012255 if (get_option_tv(&varname, rettv, 1) == OK)
12256 done = TRUE;
12257 }
12258 else
12259 {
12260 /* Look up the variable. */
12261 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12262 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
12263 varname, FALSE);
12264 if (v != NULL)
12265 {
12266 copy_tv(&v->di_tv, rettv);
12267 done = TRUE;
12268 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012271
12272 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012273 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274 }
12275
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012276 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12277 /* use the default return value */
12278 copy_tv(&argvars[off + 2], rettv);
12279
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 --emsg_off;
12281}
12282
12283/*
12284 * "glob()" function
12285 */
12286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012287f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012288 typval_T *argvars;
12289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012291 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012293 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012295 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012296 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012297 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012298 if (argvars[1].v_type != VAR_UNKNOWN)
12299 {
12300 if (get_tv_number_chk(&argvars[1], &error))
12301 options |= WILD_KEEP_ALL;
12302 if (argvars[2].v_type != VAR_UNKNOWN
12303 && get_tv_number_chk(&argvars[2], &error))
12304 {
12305 rettv->v_type = VAR_LIST;
12306 rettv->vval.v_list = NULL;
12307 }
12308 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012309 if (!error)
12310 {
12311 ExpandInit(&xpc);
12312 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012313 if (p_wic)
12314 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012315 if (rettv->v_type == VAR_STRING)
12316 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012317 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012318 else if (rettv_list_alloc(rettv) != FAIL)
12319 {
12320 int i;
12321
12322 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12323 NULL, options, WILD_ALL_KEEP);
12324 for (i = 0; i < xpc.xp_numfiles; i++)
12325 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12326
12327 ExpandCleanup(&xpc);
12328 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012329 }
12330 else
12331 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332}
12333
12334/*
12335 * "globpath()" function
12336 */
12337 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012339 typval_T *argvars;
12340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012342 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012344 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012345 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012346 garray_T ga;
12347 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012349 /* When the optional second argument is non-zero, don't remove matches
12350 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012351 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012352 if (argvars[2].v_type != VAR_UNKNOWN)
12353 {
12354 if (get_tv_number_chk(&argvars[2], &error))
12355 flags |= WILD_KEEP_ALL;
12356 if (argvars[3].v_type != VAR_UNKNOWN
12357 && get_tv_number_chk(&argvars[3], &error))
12358 {
12359 rettv->v_type = VAR_LIST;
12360 rettv->vval.v_list = NULL;
12361 }
12362 }
12363 if (file != NULL && !error)
12364 {
12365 ga_init2(&ga, (int)sizeof(char_u *), 10);
12366 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
12367 if (rettv->v_type == VAR_STRING)
12368 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
12369 else if (rettv_list_alloc(rettv) != FAIL)
12370 for (i = 0; i < ga.ga_len; ++i)
12371 list_append_string(rettv->vval.v_list,
12372 ((char_u **)(ga.ga_data))[i], -1);
12373 ga_clear_strings(&ga);
12374 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012375 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020012376 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377}
12378
12379/*
12380 * "has()" function
12381 */
12382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012384 typval_T *argvars;
12385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386{
12387 int i;
12388 char_u *name;
12389 int n = FALSE;
12390 static char *(has_list[]) =
12391 {
12392#ifdef AMIGA
12393 "amiga",
12394# ifdef FEAT_ARP
12395 "arp",
12396# endif
12397#endif
12398#ifdef __BEOS__
12399 "beos",
12400#endif
12401#ifdef MSDOS
12402# ifdef DJGPP
12403 "dos32",
12404# else
12405 "dos16",
12406# endif
12407#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012408#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 "mac",
12410#endif
12411#if defined(MACOS_X_UNIX)
12412 "macunix",
12413#endif
12414#ifdef OS2
12415 "os2",
12416#endif
12417#ifdef __QNX__
12418 "qnx",
12419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420#ifdef UNIX
12421 "unix",
12422#endif
12423#ifdef VMS
12424 "vms",
12425#endif
12426#ifdef WIN16
12427 "win16",
12428#endif
12429#ifdef WIN32
12430 "win32",
12431#endif
12432#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12433 "win32unix",
12434#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012435#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 "win64",
12437#endif
12438#ifdef EBCDIC
12439 "ebcdic",
12440#endif
12441#ifndef CASE_INSENSITIVE_FILENAME
12442 "fname_case",
12443#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012444#ifdef HAVE_ACL
12445 "acl",
12446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447#ifdef FEAT_ARABIC
12448 "arabic",
12449#endif
12450#ifdef FEAT_AUTOCMD
12451 "autocmd",
12452#endif
12453#ifdef FEAT_BEVAL
12454 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012455# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12456 "balloon_multiline",
12457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012458#endif
12459#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12460 "builtin_terms",
12461# ifdef ALL_BUILTIN_TCAPS
12462 "all_builtin_terms",
12463# endif
12464#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012465#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12466 || defined(FEAT_GUI_W32) \
12467 || defined(FEAT_GUI_MOTIF))
12468 "browsefilter",
12469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470#ifdef FEAT_BYTEOFF
12471 "byte_offset",
12472#endif
12473#ifdef FEAT_CINDENT
12474 "cindent",
12475#endif
12476#ifdef FEAT_CLIENTSERVER
12477 "clientserver",
12478#endif
12479#ifdef FEAT_CLIPBOARD
12480 "clipboard",
12481#endif
12482#ifdef FEAT_CMDL_COMPL
12483 "cmdline_compl",
12484#endif
12485#ifdef FEAT_CMDHIST
12486 "cmdline_hist",
12487#endif
12488#ifdef FEAT_COMMENTS
12489 "comments",
12490#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012491#ifdef FEAT_CONCEAL
12492 "conceal",
12493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494#ifdef FEAT_CRYPT
12495 "cryptv",
12496#endif
12497#ifdef FEAT_CSCOPE
12498 "cscope",
12499#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012500#ifdef FEAT_CURSORBIND
12501 "cursorbind",
12502#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012503#ifdef CURSOR_SHAPE
12504 "cursorshape",
12505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506#ifdef DEBUG
12507 "debug",
12508#endif
12509#ifdef FEAT_CON_DIALOG
12510 "dialog_con",
12511#endif
12512#ifdef FEAT_GUI_DIALOG
12513 "dialog_gui",
12514#endif
12515#ifdef FEAT_DIFF
12516 "diff",
12517#endif
12518#ifdef FEAT_DIGRAPHS
12519 "digraphs",
12520#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020012521#ifdef FEAT_DIRECTX
12522 "directx",
12523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524#ifdef FEAT_DND
12525 "dnd",
12526#endif
12527#ifdef FEAT_EMACS_TAGS
12528 "emacs_tags",
12529#endif
12530 "eval", /* always present, of course! */
12531#ifdef FEAT_EX_EXTRA
12532 "ex_extra",
12533#endif
12534#ifdef FEAT_SEARCH_EXTRA
12535 "extra_search",
12536#endif
12537#ifdef FEAT_FKMAP
12538 "farsi",
12539#endif
12540#ifdef FEAT_SEARCHPATH
12541 "file_in_path",
12542#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012543#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012544 "filterpipe",
12545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546#ifdef FEAT_FIND_ID
12547 "find_in_path",
12548#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012549#ifdef FEAT_FLOAT
12550 "float",
12551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552#ifdef FEAT_FOLDING
12553 "folding",
12554#endif
12555#ifdef FEAT_FOOTER
12556 "footer",
12557#endif
12558#if !defined(USE_SYSTEM) && defined(UNIX)
12559 "fork",
12560#endif
12561#ifdef FEAT_GETTEXT
12562 "gettext",
12563#endif
12564#ifdef FEAT_GUI
12565 "gui",
12566#endif
12567#ifdef FEAT_GUI_ATHENA
12568# ifdef FEAT_GUI_NEXTAW
12569 "gui_neXtaw",
12570# else
12571 "gui_athena",
12572# endif
12573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574#ifdef FEAT_GUI_GTK
12575 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012577#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012578#ifdef FEAT_GUI_GNOME
12579 "gui_gnome",
12580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581#ifdef FEAT_GUI_MAC
12582 "gui_mac",
12583#endif
12584#ifdef FEAT_GUI_MOTIF
12585 "gui_motif",
12586#endif
12587#ifdef FEAT_GUI_PHOTON
12588 "gui_photon",
12589#endif
12590#ifdef FEAT_GUI_W16
12591 "gui_win16",
12592#endif
12593#ifdef FEAT_GUI_W32
12594 "gui_win32",
12595#endif
12596#ifdef FEAT_HANGULIN
12597 "hangul_input",
12598#endif
12599#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12600 "iconv",
12601#endif
12602#ifdef FEAT_INS_EXPAND
12603 "insert_expand",
12604#endif
12605#ifdef FEAT_JUMPLIST
12606 "jumplist",
12607#endif
12608#ifdef FEAT_KEYMAP
12609 "keymap",
12610#endif
12611#ifdef FEAT_LANGMAP
12612 "langmap",
12613#endif
12614#ifdef FEAT_LIBCALL
12615 "libcall",
12616#endif
12617#ifdef FEAT_LINEBREAK
12618 "linebreak",
12619#endif
12620#ifdef FEAT_LISP
12621 "lispindent",
12622#endif
12623#ifdef FEAT_LISTCMDS
12624 "listcmds",
12625#endif
12626#ifdef FEAT_LOCALMAP
12627 "localmap",
12628#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012629#ifdef FEAT_LUA
12630# ifndef DYNAMIC_LUA
12631 "lua",
12632# endif
12633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634#ifdef FEAT_MENU
12635 "menu",
12636#endif
12637#ifdef FEAT_SESSION
12638 "mksession",
12639#endif
12640#ifdef FEAT_MODIFY_FNAME
12641 "modify_fname",
12642#endif
12643#ifdef FEAT_MOUSE
12644 "mouse",
12645#endif
12646#ifdef FEAT_MOUSESHAPE
12647 "mouseshape",
12648#endif
12649#if defined(UNIX) || defined(VMS)
12650# ifdef FEAT_MOUSE_DEC
12651 "mouse_dec",
12652# endif
12653# ifdef FEAT_MOUSE_GPM
12654 "mouse_gpm",
12655# endif
12656# ifdef FEAT_MOUSE_JSB
12657 "mouse_jsbterm",
12658# endif
12659# ifdef FEAT_MOUSE_NET
12660 "mouse_netterm",
12661# endif
12662# ifdef FEAT_MOUSE_PTERM
12663 "mouse_pterm",
12664# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012665# ifdef FEAT_MOUSE_SGR
12666 "mouse_sgr",
12667# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012668# ifdef FEAT_SYSMOUSE
12669 "mouse_sysmouse",
12670# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012671# ifdef FEAT_MOUSE_URXVT
12672 "mouse_urxvt",
12673# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674# ifdef FEAT_MOUSE_XTERM
12675 "mouse_xterm",
12676# endif
12677#endif
12678#ifdef FEAT_MBYTE
12679 "multi_byte",
12680#endif
12681#ifdef FEAT_MBYTE_IME
12682 "multi_byte_ime",
12683#endif
12684#ifdef FEAT_MULTI_LANG
12685 "multi_lang",
12686#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012687#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012688#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012689 "mzscheme",
12690#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692#ifdef FEAT_OLE
12693 "ole",
12694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695#ifdef FEAT_PATH_EXTRA
12696 "path_extra",
12697#endif
12698#ifdef FEAT_PERL
12699#ifndef DYNAMIC_PERL
12700 "perl",
12701#endif
12702#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012703#ifdef FEAT_PERSISTENT_UNDO
12704 "persistent_undo",
12705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706#ifdef FEAT_PYTHON
12707#ifndef DYNAMIC_PYTHON
12708 "python",
12709#endif
12710#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012711#ifdef FEAT_PYTHON3
12712#ifndef DYNAMIC_PYTHON3
12713 "python3",
12714#endif
12715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012716#ifdef FEAT_POSTSCRIPT
12717 "postscript",
12718#endif
12719#ifdef FEAT_PRINTER
12720 "printer",
12721#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012722#ifdef FEAT_PROFILE
12723 "profile",
12724#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012725#ifdef FEAT_RELTIME
12726 "reltime",
12727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728#ifdef FEAT_QUICKFIX
12729 "quickfix",
12730#endif
12731#ifdef FEAT_RIGHTLEFT
12732 "rightleft",
12733#endif
12734#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12735 "ruby",
12736#endif
12737#ifdef FEAT_SCROLLBIND
12738 "scrollbind",
12739#endif
12740#ifdef FEAT_CMDL_INFO
12741 "showcmd",
12742 "cmdline_info",
12743#endif
12744#ifdef FEAT_SIGNS
12745 "signs",
12746#endif
12747#ifdef FEAT_SMARTINDENT
12748 "smartindent",
12749#endif
12750#ifdef FEAT_SNIFF
12751 "sniff",
12752#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012753#ifdef STARTUPTIME
12754 "startuptime",
12755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012756#ifdef FEAT_STL_OPT
12757 "statusline",
12758#endif
12759#ifdef FEAT_SUN_WORKSHOP
12760 "sun_workshop",
12761#endif
12762#ifdef FEAT_NETBEANS_INTG
12763 "netbeans_intg",
12764#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012765#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012766 "spell",
12767#endif
12768#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 "syntax",
12770#endif
12771#if defined(USE_SYSTEM) || !defined(UNIX)
12772 "system",
12773#endif
12774#ifdef FEAT_TAG_BINS
12775 "tag_binary",
12776#endif
12777#ifdef FEAT_TAG_OLDSTATIC
12778 "tag_old_static",
12779#endif
12780#ifdef FEAT_TAG_ANYWHITE
12781 "tag_any_white",
12782#endif
12783#ifdef FEAT_TCL
12784# ifndef DYNAMIC_TCL
12785 "tcl",
12786# endif
12787#endif
12788#ifdef TERMINFO
12789 "terminfo",
12790#endif
12791#ifdef FEAT_TERMRESPONSE
12792 "termresponse",
12793#endif
12794#ifdef FEAT_TEXTOBJ
12795 "textobjects",
12796#endif
12797#ifdef HAVE_TGETENT
12798 "tgetent",
12799#endif
12800#ifdef FEAT_TITLE
12801 "title",
12802#endif
12803#ifdef FEAT_TOOLBAR
12804 "toolbar",
12805#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012806#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12807 "unnamedplus",
12808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809#ifdef FEAT_USR_CMDS
12810 "user-commands", /* was accidentally included in 5.4 */
12811 "user_commands",
12812#endif
12813#ifdef FEAT_VIMINFO
12814 "viminfo",
12815#endif
12816#ifdef FEAT_VERTSPLIT
12817 "vertsplit",
12818#endif
12819#ifdef FEAT_VIRTUALEDIT
12820 "virtualedit",
12821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823#ifdef FEAT_VISUALEXTRA
12824 "visualextra",
12825#endif
12826#ifdef FEAT_VREPLACE
12827 "vreplace",
12828#endif
12829#ifdef FEAT_WILDIGN
12830 "wildignore",
12831#endif
12832#ifdef FEAT_WILDMENU
12833 "wildmenu",
12834#endif
12835#ifdef FEAT_WINDOWS
12836 "windows",
12837#endif
12838#ifdef FEAT_WAK
12839 "winaltkeys",
12840#endif
12841#ifdef FEAT_WRITEBACKUP
12842 "writebackup",
12843#endif
12844#ifdef FEAT_XIM
12845 "xim",
12846#endif
12847#ifdef FEAT_XFONTSET
12848 "xfontset",
12849#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012850#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012851 "xpm",
12852 "xpm_w32", /* for backward compatibility */
12853#else
12854# if defined(HAVE_XPM)
12855 "xpm",
12856# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858#ifdef USE_XSMP
12859 "xsmp",
12860#endif
12861#ifdef USE_XSMP_INTERACT
12862 "xsmp_interact",
12863#endif
12864#ifdef FEAT_XCLIPBOARD
12865 "xterm_clipboard",
12866#endif
12867#ifdef FEAT_XTERM_SAVE
12868 "xterm_save",
12869#endif
12870#if defined(UNIX) && defined(FEAT_X11)
12871 "X11",
12872#endif
12873 NULL
12874 };
12875
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 for (i = 0; has_list[i] != NULL; ++i)
12878 if (STRICMP(name, has_list[i]) == 0)
12879 {
12880 n = TRUE;
12881 break;
12882 }
12883
12884 if (n == FALSE)
12885 {
12886 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012887 {
12888 if (name[5] == '-'
12889 && STRLEN(name) > 11
12890 && vim_isdigit(name[6])
12891 && vim_isdigit(name[8])
12892 && vim_isdigit(name[10]))
12893 {
12894 int major = atoi((char *)name + 6);
12895 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012896
12897 /* Expect "patch-9.9.01234". */
12898 n = (major < VIM_VERSION_MAJOR
12899 || (major == VIM_VERSION_MAJOR
12900 && (minor < VIM_VERSION_MINOR
12901 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020012902 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020012903 }
12904 else
12905 n = has_patch(atoi((char *)name + 5));
12906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907 else if (STRICMP(name, "vim_starting") == 0)
12908 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012909#ifdef FEAT_MBYTE
12910 else if (STRICMP(name, "multi_byte_encoding") == 0)
12911 n = has_mbyte;
12912#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012913#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12914 else if (STRICMP(name, "balloon_multiline") == 0)
12915 n = multiline_balloon_available();
12916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917#ifdef DYNAMIC_TCL
12918 else if (STRICMP(name, "tcl") == 0)
12919 n = tcl_enabled(FALSE);
12920#endif
12921#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12922 else if (STRICMP(name, "iconv") == 0)
12923 n = iconv_enabled(FALSE);
12924#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012925#ifdef DYNAMIC_LUA
12926 else if (STRICMP(name, "lua") == 0)
12927 n = lua_enabled(FALSE);
12928#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012929#ifdef DYNAMIC_MZSCHEME
12930 else if (STRICMP(name, "mzscheme") == 0)
12931 n = mzscheme_enabled(FALSE);
12932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933#ifdef DYNAMIC_RUBY
12934 else if (STRICMP(name, "ruby") == 0)
12935 n = ruby_enabled(FALSE);
12936#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012937#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938#ifdef DYNAMIC_PYTHON
12939 else if (STRICMP(name, "python") == 0)
12940 n = python_enabled(FALSE);
12941#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012942#endif
12943#ifdef FEAT_PYTHON3
12944#ifdef DYNAMIC_PYTHON3
12945 else if (STRICMP(name, "python3") == 0)
12946 n = python3_enabled(FALSE);
12947#endif
12948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949#ifdef DYNAMIC_PERL
12950 else if (STRICMP(name, "perl") == 0)
12951 n = perl_enabled(FALSE);
12952#endif
12953#ifdef FEAT_GUI
12954 else if (STRICMP(name, "gui_running") == 0)
12955 n = (gui.in_use || gui.starting);
12956# ifdef FEAT_GUI_W32
12957 else if (STRICMP(name, "gui_win32s") == 0)
12958 n = gui_is_win32s();
12959# endif
12960# ifdef FEAT_BROWSE
12961 else if (STRICMP(name, "browse") == 0)
12962 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12963# endif
12964#endif
12965#ifdef FEAT_SYN_HL
12966 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012967 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968#endif
12969#if defined(WIN3264)
12970 else if (STRICMP(name, "win95") == 0)
12971 n = mch_windows95();
12972#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012973#ifdef FEAT_NETBEANS_INTG
12974 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012975 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977 }
12978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980}
12981
12982/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012983 * "has_key()" function
12984 */
12985 static void
12986f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012987 typval_T *argvars;
12988 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012989{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012990 if (argvars[0].v_type != VAR_DICT)
12991 {
12992 EMSG(_(e_dictreq));
12993 return;
12994 }
12995 if (argvars[0].vval.v_dict == NULL)
12996 return;
12997
12998 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012999 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013000}
13001
13002/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013003 * "haslocaldir()" function
13004 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013005 static void
13006f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013007 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013008 typval_T *rettv;
13009{
13010 rettv->vval.v_number = (curwin->w_localdir != NULL);
13011}
13012
13013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 * "hasmapto()" function
13015 */
13016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013017f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013018 typval_T *argvars;
13019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013020{
13021 char_u *name;
13022 char_u *mode;
13023 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013024 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013027 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028 mode = (char_u *)"nvo";
13029 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013031 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013032 if (argvars[2].v_type != VAR_UNKNOWN)
13033 abbr = get_tv_number(&argvars[2]);
13034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013035
Bram Moolenaar2c932302006-03-18 21:42:09 +000013036 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013037 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013038 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013039 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040}
13041
13042/*
13043 * "histadd()" function
13044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013046f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013047 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013049{
13050#ifdef FEAT_CMDHIST
13051 int histype;
13052 char_u *str;
13053 char_u buf[NUMBUFLEN];
13054#endif
13055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057 if (check_restricted() || check_secure())
13058 return;
13059#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013060 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13061 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013062 if (histype >= 0)
13063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013064 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065 if (*str != NUL)
13066 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013067 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013069 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 return;
13071 }
13072 }
13073#endif
13074}
13075
13076/*
13077 * "histdel()" function
13078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013080f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013081 typval_T *argvars UNUSED;
13082 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083{
13084#ifdef FEAT_CMDHIST
13085 int n;
13086 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013087 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013089 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13090 if (str == NULL)
13091 n = 0;
13092 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013094 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013095 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013097 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013098 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 else
13100 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013101 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013102 get_tv_string_buf(&argvars[1], buf));
13103 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104#endif
13105}
13106
13107/*
13108 * "histget()" function
13109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013112 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114{
13115#ifdef FEAT_CMDHIST
13116 int type;
13117 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013118 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013120 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13121 if (str == NULL)
13122 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013124 {
13125 type = get_histtype(str);
13126 if (argvars[1].v_type == VAR_UNKNOWN)
13127 idx = get_history_idx(type);
13128 else
13129 idx = (int)get_tv_number_chk(&argvars[1], NULL);
13130 /* -1 on type error */
13131 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
13132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013134 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013135#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013136 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137}
13138
13139/*
13140 * "histnr()" function
13141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013143f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013144 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146{
13147 int i;
13148
13149#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013150 char_u *history = get_tv_string_chk(&argvars[0]);
13151
13152 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013153 if (i >= HIST_CMD && i < HIST_COUNT)
13154 i = get_history_idx(i);
13155 else
13156#endif
13157 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159}
13160
13161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 * "highlightID(name)" function
13163 */
13164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013165f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013166 typval_T *argvars;
13167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170}
13171
13172/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013173 * "highlight_exists()" function
13174 */
13175 static void
13176f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *argvars;
13178 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013179{
13180 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
13181}
13182
13183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 * "hostname()" function
13185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013187f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013188 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190{
13191 char_u hostname[256];
13192
13193 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194 rettv->v_type = VAR_STRING;
13195 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
13198/*
13199 * iconv() function
13200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013203 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
13206#ifdef FEAT_MBYTE
13207 char_u buf1[NUMBUFLEN];
13208 char_u buf2[NUMBUFLEN];
13209 char_u *from, *to, *str;
13210 vimconv_T vimconv;
13211#endif
13212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->v_type = VAR_STRING;
13214 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215
13216#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217 str = get_tv_string(&argvars[0]);
13218 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
13219 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 vimconv.vc_type = CONV_NONE;
13221 convert_setup(&vimconv, from, to);
13222
13223 /* If the encodings are equal, no conversion needed. */
13224 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228
13229 convert_setup(&vimconv, NULL, NULL);
13230 vim_free(from);
13231 vim_free(to);
13232#endif
13233}
13234
13235/*
13236 * "indent()" function
13237 */
13238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013239f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013240 typval_T *argvars;
13241 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242{
13243 linenr_T lnum;
13244
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013245 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013247 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250}
13251
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013252/*
13253 * "index()" function
13254 */
13255 static void
13256f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013257 typval_T *argvars;
13258 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013259{
Bram Moolenaar33570922005-01-25 22:26:29 +000013260 list_T *l;
13261 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013262 long idx = 0;
13263 int ic = FALSE;
13264
13265 rettv->vval.v_number = -1;
13266 if (argvars[0].v_type != VAR_LIST)
13267 {
13268 EMSG(_(e_listreq));
13269 return;
13270 }
13271 l = argvars[0].vval.v_list;
13272 if (l != NULL)
13273 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013274 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013277 int error = FALSE;
13278
Bram Moolenaar758711c2005-02-02 23:11:38 +000013279 /* Start at specified item. Use the cached index that list_find()
13280 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013281 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000013282 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013283 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013284 ic = get_tv_number_chk(&argvars[3], &error);
13285 if (error)
13286 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013287 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013288
Bram Moolenaar758711c2005-02-02 23:11:38 +000013289 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013290 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013291 {
13292 rettv->vval.v_number = idx;
13293 break;
13294 }
13295 }
13296}
13297
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298static int inputsecret_flag = 0;
13299
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013300static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13301
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013303 * This function is used by f_input() and f_inputdialog() functions. The third
13304 * argument to f_input() specifies the type of completion to use at the
13305 * prompt. The third argument to f_inputdialog() specifies the value to return
13306 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 */
13308 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013309get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013310 typval_T *argvars;
13311 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013312 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013314 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 char_u *p = NULL;
13316 int c;
13317 char_u buf[NUMBUFLEN];
13318 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013319 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013320 int xp_type = EXPAND_NOTHING;
13321 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013323 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013324 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325
13326#ifdef NO_CONSOLE_INPUT
13327 /* While starting up, there is no place to enter text. */
13328 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330#endif
13331
13332 cmd_silent = FALSE; /* Want to see the prompt. */
13333 if (prompt != NULL)
13334 {
13335 /* Only the part of the message after the last NL is considered as
13336 * prompt for the command line */
13337 p = vim_strrchr(prompt, '\n');
13338 if (p == NULL)
13339 p = prompt;
13340 else
13341 {
13342 ++p;
13343 c = *p;
13344 *p = NUL;
13345 msg_start();
13346 msg_clr_eos();
13347 msg_puts_attr(prompt, echo_attr);
13348 msg_didout = FALSE;
13349 msg_starthere();
13350 *p = c;
13351 }
13352 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013354 if (argvars[1].v_type != VAR_UNKNOWN)
13355 {
13356 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13357 if (defstr != NULL)
13358 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013360 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013361 {
13362 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013363 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013364 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013365
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013366 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013367 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013368
Bram Moolenaar4463f292005-09-25 22:20:24 +000013369 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13370 if (xp_name == NULL)
13371 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013372
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013373 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013374
Bram Moolenaar4463f292005-09-25 22:20:24 +000013375 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13376 &xp_arg) == FAIL)
13377 return;
13378 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013379 }
13380
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013381 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013382 {
13383# ifdef FEAT_EX_EXTRA
13384 int save_ex_normal_busy = ex_normal_busy;
13385 ex_normal_busy = 0;
13386# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013387 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013388 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13389 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013390# ifdef FEAT_EX_EXTRA
13391 ex_normal_busy = save_ex_normal_busy;
13392# endif
13393 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013394 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013395 && argvars[1].v_type != VAR_UNKNOWN
13396 && argvars[2].v_type != VAR_UNKNOWN)
13397 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13398 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013399
13400 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013402 /* since the user typed this, no need to wait for return */
13403 need_wait_return = FALSE;
13404 msg_didout = FALSE;
13405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 cmd_silent = cmd_silent_save;
13407}
13408
13409/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013410 * "input()" function
13411 * Also handles inputsecret() when inputsecret is set.
13412 */
13413 static void
13414f_input(argvars, rettv)
13415 typval_T *argvars;
13416 typval_T *rettv;
13417{
13418 get_user_input(argvars, rettv, FALSE);
13419}
13420
13421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422 * "inputdialog()" function
13423 */
13424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013425f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013426 typval_T *argvars;
13427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428{
13429#if defined(FEAT_GUI_TEXTDIALOG)
13430 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13431 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13432 {
13433 char_u *message;
13434 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013435 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013436
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013437 message = get_tv_string_chk(&argvars[0]);
13438 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013439 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013440 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 else
13442 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013443 if (message != NULL && defstr != NULL
13444 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013445 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013446 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 else
13448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013449 if (message != NULL && defstr != NULL
13450 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013451 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013452 rettv->vval.v_string = vim_strsave(
13453 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013457 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 }
13459 else
13460#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013461 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462}
13463
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013464/*
13465 * "inputlist()" function
13466 */
13467 static void
13468f_inputlist(argvars, rettv)
13469 typval_T *argvars;
13470 typval_T *rettv;
13471{
13472 listitem_T *li;
13473 int selected;
13474 int mouse_used;
13475
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013476#ifdef NO_CONSOLE_INPUT
13477 /* While starting up, there is no place to enter text. */
13478 if (no_console_input())
13479 return;
13480#endif
13481 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13482 {
13483 EMSG2(_(e_listarg), "inputlist()");
13484 return;
13485 }
13486
13487 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013488 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013489 lines_left = Rows; /* avoid more prompt */
13490 msg_scroll = TRUE;
13491 msg_clr_eos();
13492
13493 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13494 {
13495 msg_puts(get_tv_string(&li->li_tv));
13496 msg_putchar('\n');
13497 }
13498
13499 /* Ask for choice. */
13500 selected = prompt_for_number(&mouse_used);
13501 if (mouse_used)
13502 selected -= lines_left;
13503
13504 rettv->vval.v_number = selected;
13505}
13506
13507
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13509
13510/*
13511 * "inputrestore()" function
13512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013514f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013515 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013516 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517{
13518 if (ga_userinput.ga_len > 0)
13519 {
13520 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13522 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013523 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 }
13525 else if (p_verbose > 1)
13526 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013527 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013528 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 }
13530}
13531
13532/*
13533 * "inputsave()" function
13534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013536f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013537 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013540 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013541 if (ga_grow(&ga_userinput, 1) == OK)
13542 {
13543 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13544 + ga_userinput.ga_len);
13545 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013546 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547 }
13548 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550}
13551
13552/*
13553 * "inputsecret()" function
13554 */
13555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013556f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013557 typval_T *argvars;
13558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559{
13560 ++cmdline_star;
13561 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013562 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013563 --cmdline_star;
13564 --inputsecret_flag;
13565}
13566
13567/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013568 * "insert()" function
13569 */
13570 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013571f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013572 typval_T *argvars;
13573 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013574{
13575 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013576 listitem_T *item;
13577 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013578 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013579
13580 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013581 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013582 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013583 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013584 {
13585 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013586 before = get_tv_number_chk(&argvars[2], &error);
13587 if (error)
13588 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013589
Bram Moolenaar758711c2005-02-02 23:11:38 +000013590 if (before == l->lv_len)
13591 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013592 else
13593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013594 item = list_find(l, before);
13595 if (item == NULL)
13596 {
13597 EMSGN(_(e_listidx), before);
13598 l = NULL;
13599 }
13600 }
13601 if (l != NULL)
13602 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013603 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013604 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013605 }
13606 }
13607}
13608
13609/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013610 * "invert(expr)" function
13611 */
13612 static void
13613f_invert(argvars, rettv)
13614 typval_T *argvars;
13615 typval_T *rettv;
13616{
13617 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13618}
13619
13620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 * "isdirectory()" function
13622 */
13623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013625 typval_T *argvars;
13626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013628 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629}
13630
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013631/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013632 * "islocked()" function
13633 */
13634 static void
13635f_islocked(argvars, rettv)
13636 typval_T *argvars;
13637 typval_T *rettv;
13638{
13639 lval_T lv;
13640 char_u *end;
13641 dictitem_T *di;
13642
13643 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013644 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13645 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013646 if (end != NULL && lv.ll_name != NULL)
13647 {
13648 if (*end != NUL)
13649 EMSG(_(e_trailing));
13650 else
13651 {
13652 if (lv.ll_tv == NULL)
13653 {
13654 if (check_changedtick(lv.ll_name))
13655 rettv->vval.v_number = 1; /* always locked */
13656 else
13657 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013658 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013659 if (di != NULL)
13660 {
13661 /* Consider a variable locked when:
13662 * 1. the variable itself is locked
13663 * 2. the value of the variable is locked.
13664 * 3. the List or Dict value is locked.
13665 */
13666 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13667 || tv_islocked(&di->di_tv));
13668 }
13669 }
13670 }
13671 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013672 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013673 else if (lv.ll_newkey != NULL)
13674 EMSG2(_(e_dictkey), lv.ll_newkey);
13675 else if (lv.ll_list != NULL)
13676 /* List item. */
13677 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13678 else
13679 /* Dictionary item. */
13680 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13681 }
13682 }
13683
13684 clear_lval(&lv);
13685}
13686
Bram Moolenaar33570922005-01-25 22:26:29 +000013687static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013688
13689/*
13690 * Turn a dict into a list:
13691 * "what" == 0: list of keys
13692 * "what" == 1: list of values
13693 * "what" == 2: list of items
13694 */
13695 static void
13696dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013697 typval_T *argvars;
13698 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013699 int what;
13700{
Bram Moolenaar33570922005-01-25 22:26:29 +000013701 list_T *l2;
13702 dictitem_T *di;
13703 hashitem_T *hi;
13704 listitem_T *li;
13705 listitem_T *li2;
13706 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013707 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013708
Bram Moolenaar8c711452005-01-14 21:53:12 +000013709 if (argvars[0].v_type != VAR_DICT)
13710 {
13711 EMSG(_(e_dictreq));
13712 return;
13713 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013714 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013715 return;
13716
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013717 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013718 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013719
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013720 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013721 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013722 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013723 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013724 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013725 --todo;
13726 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013727
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013728 li = listitem_alloc();
13729 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013730 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013731 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013732
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013733 if (what == 0)
13734 {
13735 /* keys() */
13736 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013737 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013738 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13739 }
13740 else if (what == 1)
13741 {
13742 /* values() */
13743 copy_tv(&di->di_tv, &li->li_tv);
13744 }
13745 else
13746 {
13747 /* items() */
13748 l2 = list_alloc();
13749 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013750 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013751 li->li_tv.vval.v_list = l2;
13752 if (l2 == NULL)
13753 break;
13754 ++l2->lv_refcount;
13755
13756 li2 = listitem_alloc();
13757 if (li2 == NULL)
13758 break;
13759 list_append(l2, li2);
13760 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013761 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013762 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13763
13764 li2 = listitem_alloc();
13765 if (li2 == NULL)
13766 break;
13767 list_append(l2, li2);
13768 copy_tv(&di->di_tv, &li2->li_tv);
13769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013770 }
13771 }
13772}
13773
13774/*
13775 * "items(dict)" function
13776 */
13777 static void
13778f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013779 typval_T *argvars;
13780 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013781{
13782 dict_list(argvars, rettv, 2);
13783}
13784
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013786 * "join()" function
13787 */
13788 static void
13789f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013790 typval_T *argvars;
13791 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013792{
13793 garray_T ga;
13794 char_u *sep;
13795
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013796 if (argvars[0].v_type != VAR_LIST)
13797 {
13798 EMSG(_(e_listreq));
13799 return;
13800 }
13801 if (argvars[0].vval.v_list == NULL)
13802 return;
13803 if (argvars[1].v_type == VAR_UNKNOWN)
13804 sep = (char_u *)" ";
13805 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013806 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013807
13808 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013809
13810 if (sep != NULL)
13811 {
13812 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013813 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013814 ga_append(&ga, NUL);
13815 rettv->vval.v_string = (char_u *)ga.ga_data;
13816 }
13817 else
13818 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013819}
13820
13821/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013822 * "keys()" function
13823 */
13824 static void
13825f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013826 typval_T *argvars;
13827 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013828{
13829 dict_list(argvars, rettv, 0);
13830}
13831
13832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833 * "last_buffer_nr()" function.
13834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013836f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013837 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839{
13840 int n = 0;
13841 buf_T *buf;
13842
13843 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13844 if (n < buf->b_fnum)
13845 n = buf->b_fnum;
13846
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013847 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013848}
13849
13850/*
13851 * "len()" function
13852 */
13853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013855 typval_T *argvars;
13856 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013857{
13858 switch (argvars[0].v_type)
13859 {
13860 case VAR_STRING:
13861 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013862 rettv->vval.v_number = (varnumber_T)STRLEN(
13863 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013864 break;
13865 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013866 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013867 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013868 case VAR_DICT:
13869 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13870 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013871 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013872 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013873 break;
13874 }
13875}
13876
Bram Moolenaar33570922005-01-25 22:26:29 +000013877static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013878
13879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013880libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013881 typval_T *argvars;
13882 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013883 int type;
13884{
13885#ifdef FEAT_LIBCALL
13886 char_u *string_in;
13887 char_u **string_result;
13888 int nr_result;
13889#endif
13890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013891 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013892 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013893 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013894
13895 if (check_restricted() || check_secure())
13896 return;
13897
13898#ifdef FEAT_LIBCALL
13899 /* The first two args must be strings, otherwise its meaningless */
13900 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13901 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013902 string_in = NULL;
13903 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013904 string_in = argvars[2].vval.v_string;
13905 if (type == VAR_NUMBER)
13906 string_result = NULL;
13907 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013908 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013909 if (mch_libcall(argvars[0].vval.v_string,
13910 argvars[1].vval.v_string,
13911 string_in,
13912 argvars[2].vval.v_number,
13913 string_result,
13914 &nr_result) == OK
13915 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013916 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013917 }
13918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919}
13920
13921/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013922 * "libcall()" function
13923 */
13924 static void
13925f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013926 typval_T *argvars;
13927 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013928{
13929 libcall_common(argvars, rettv, VAR_STRING);
13930}
13931
13932/*
13933 * "libcallnr()" function
13934 */
13935 static void
13936f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013937 typval_T *argvars;
13938 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013939{
13940 libcall_common(argvars, rettv, VAR_NUMBER);
13941}
13942
13943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944 * "line(string)" function
13945 */
13946 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013947f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013948 typval_T *argvars;
13949 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950{
13951 linenr_T lnum = 0;
13952 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013953 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013955 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 if (fp != NULL)
13957 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013958 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013959}
13960
13961/*
13962 * "line2byte(lnum)" function
13963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013965f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013966 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013967 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968{
13969#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971#else
13972 linenr_T lnum;
13973
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013974 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013978 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13979 if (rettv->vval.v_number >= 0)
13980 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981#endif
13982}
13983
13984/*
13985 * "lispindent(lnum)" function
13986 */
13987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013988f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013989 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991{
13992#ifdef FEAT_LISP
13993 pos_T pos;
13994 linenr_T lnum;
13995
13996 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013997 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13999 {
14000 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014001 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 curwin->w_cursor = pos;
14003 }
14004 else
14005#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014006 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007}
14008
14009/*
14010 * "localtime()" function
14011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014013f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014014 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018}
14019
Bram Moolenaar33570922005-01-25 22:26:29 +000014020static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021
14022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000014024 typval_T *argvars;
14025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 int exact;
14027{
14028 char_u *keys;
14029 char_u *which;
14030 char_u buf[NUMBUFLEN];
14031 char_u *keys_buf = NULL;
14032 char_u *rhs;
14033 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000014034 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010014035 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020014036 mapblock_T *mp;
14037 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038
14039 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014040 rettv->v_type = VAR_STRING;
14041 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 if (*keys == NUL)
14045 return;
14046
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014047 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000014048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014050 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020014051 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000014052 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014053 if (argvars[3].v_type != VAR_UNKNOWN)
14054 get_dict = get_tv_number(&argvars[3]);
14055 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000014056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 else
14058 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014059 if (which == NULL)
14060 return;
14061
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 mode = get_map_mode(&which, 0);
14063
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000014064 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014065 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014067
14068 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020014070 /* Return a string. */
14071 if (rhs != NULL)
14072 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073
Bram Moolenaarbd743252010-10-20 21:23:33 +020014074 }
14075 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
14076 {
14077 /* Return a dictionary. */
14078 char_u *lhs = str2special_save(mp->m_keys, TRUE);
14079 char_u *mapmode = map_mode_to_chars(mp->m_mode);
14080 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081
Bram Moolenaarbd743252010-10-20 21:23:33 +020014082 dict_add_nr_str(dict, "lhs", 0L, lhs);
14083 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
14084 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
14085 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
14086 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
14087 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
14088 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020014089 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020014090 dict_add_nr_str(dict, "mode", 0L, mapmode);
14091
14092 vim_free(lhs);
14093 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 }
14095}
14096
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014097#ifdef FEAT_FLOAT
14098/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020014099 * "log()" function
14100 */
14101 static void
14102f_log(argvars, rettv)
14103 typval_T *argvars;
14104 typval_T *rettv;
14105{
14106 float_T f;
14107
14108 rettv->v_type = VAR_FLOAT;
14109 if (get_float_arg(argvars, &f) == OK)
14110 rettv->vval.v_float = log(f);
14111 else
14112 rettv->vval.v_float = 0.0;
14113}
14114
14115/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014116 * "log10()" function
14117 */
14118 static void
14119f_log10(argvars, rettv)
14120 typval_T *argvars;
14121 typval_T *rettv;
14122{
14123 float_T f;
14124
14125 rettv->v_type = VAR_FLOAT;
14126 if (get_float_arg(argvars, &f) == OK)
14127 rettv->vval.v_float = log10(f);
14128 else
14129 rettv->vval.v_float = 0.0;
14130}
14131#endif
14132
Bram Moolenaar1dced572012-04-05 16:54:08 +020014133#ifdef FEAT_LUA
14134/*
14135 * "luaeval()" function
14136 */
14137 static void
14138f_luaeval(argvars, rettv)
14139 typval_T *argvars;
14140 typval_T *rettv;
14141{
14142 char_u *str;
14143 char_u buf[NUMBUFLEN];
14144
14145 str = get_tv_string_buf(&argvars[0], buf);
14146 do_luaeval(str, argvars + 1, rettv);
14147}
14148#endif
14149
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014151 * "map()" function
14152 */
14153 static void
14154f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014155 typval_T *argvars;
14156 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014157{
14158 filter_map(argvars, rettv, TRUE);
14159}
14160
14161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163 */
14164 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 typval_T *argvars;
14167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014169 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170}
14171
14172/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174 */
14175 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000014176f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014177 typval_T *argvars;
14178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014180 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181}
14182
Bram Moolenaar33570922005-01-25 22:26:29 +000014183static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184
14185 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014186find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000014187 typval_T *argvars;
14188 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189 int type;
14190{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014191 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014192 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014193 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 char_u *pat;
14195 regmatch_T regmatch;
14196 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014197 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 char_u *save_cpo;
14199 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014200 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014201 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014202 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014203 list_T *l = NULL;
14204 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014205 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014206 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207
14208 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14209 save_cpo = p_cpo;
14210 p_cpo = (char_u *)"";
14211
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014212 rettv->vval.v_number = -1;
14213 if (type == 3)
14214 {
14215 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014216 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014217 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014218 }
14219 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014221 rettv->v_type = VAR_STRING;
14222 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014225 if (argvars[0].v_type == VAR_LIST)
14226 {
14227 if ((l = argvars[0].vval.v_list) == NULL)
14228 goto theend;
14229 li = l->lv_first;
14230 }
14231 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014232 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014233 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014234 len = (long)STRLEN(str);
14235 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014236
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014237 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14238 if (pat == NULL)
14239 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014240
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014241 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014243 int error = FALSE;
14244
14245 start = get_tv_number_chk(&argvars[2], &error);
14246 if (error)
14247 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014248 if (l != NULL)
14249 {
14250 li = list_find(l, start);
14251 if (li == NULL)
14252 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014253 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014254 }
14255 else
14256 {
14257 if (start < 0)
14258 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014259 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014260 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014261 /* When "count" argument is there ignore matches before "start",
14262 * otherwise skip part of the string. Differs when pattern is "^"
14263 * or "\<". */
14264 if (argvars[3].v_type != VAR_UNKNOWN)
14265 startcol = start;
14266 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014267 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014268 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014269 len -= start;
14270 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014271 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014272
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014273 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 nth = get_tv_number_chk(&argvars[3], &error);
14275 if (error)
14276 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 }
14278
14279 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14280 if (regmatch.regprog != NULL)
14281 {
14282 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014283
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014284 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014285 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014286 if (l != NULL)
14287 {
14288 if (li == NULL)
14289 {
14290 match = FALSE;
14291 break;
14292 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014293 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014294 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014295 if (str == NULL)
14296 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014297 }
14298
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014299 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014300
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014301 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014302 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014303 if (l == NULL && !match)
14304 break;
14305
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014306 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014307 if (l != NULL)
14308 {
14309 li = li->li_next;
14310 ++idx;
14311 }
14312 else
14313 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014314#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014315 startcol = (colnr_T)(regmatch.startp[0]
14316 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014317#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014318 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014319#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010014320 if (startcol > (colnr_T)len
14321 || str + startcol <= regmatch.startp[0])
14322 {
14323 match = FALSE;
14324 break;
14325 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014326 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014327 }
14328
14329 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014331 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014332 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014333 int i;
14334
14335 /* return list with matched string and submatches */
14336 for (i = 0; i < NSUBEXP; ++i)
14337 {
14338 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014339 {
14340 if (list_append_string(rettv->vval.v_list,
14341 (char_u *)"", 0) == FAIL)
14342 break;
14343 }
14344 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014345 regmatch.startp[i],
14346 (int)(regmatch.endp[i] - regmatch.startp[i]))
14347 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014348 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014349 }
14350 }
14351 else if (type == 2)
14352 {
14353 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014354 if (l != NULL)
14355 copy_tv(&li->li_tv, rettv);
14356 else
14357 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014359 }
14360 else if (l != NULL)
14361 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 else
14363 {
14364 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 (varnumber_T)(regmatch.startp[0] - str);
14367 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014368 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014370 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 }
14372 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014373 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 }
14375
14376theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014377 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 p_cpo = save_cpo;
14379}
14380
14381/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014382 * "match()" function
14383 */
14384 static void
14385f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014386 typval_T *argvars;
14387 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014388{
14389 find_some_match(argvars, rettv, 1);
14390}
14391
14392/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014393 * "matchadd()" function
14394 */
14395 static void
14396f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014397 typval_T *argvars UNUSED;
14398 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014399{
14400#ifdef FEAT_SEARCH_EXTRA
14401 char_u buf[NUMBUFLEN];
14402 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14403 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14404 int prio = 10; /* default priority */
14405 int id = -1;
14406 int error = FALSE;
14407
14408 rettv->vval.v_number = -1;
14409
14410 if (grp == NULL || pat == NULL)
14411 return;
14412 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014413 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014414 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014415 if (argvars[3].v_type != VAR_UNKNOWN)
14416 id = get_tv_number_chk(&argvars[3], &error);
14417 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014418 if (error == TRUE)
14419 return;
14420 if (id >= 1 && id <= 3)
14421 {
14422 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14423 return;
14424 }
14425
Bram Moolenaarb3414592014-06-17 17:48:32 +020014426 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL);
14427#endif
14428}
14429
14430/*
14431 * "matchaddpos()" function
14432 */
14433 static void
14434f_matchaddpos(argvars, rettv)
14435 typval_T *argvars UNUSED;
14436 typval_T *rettv UNUSED;
14437{
14438#ifdef FEAT_SEARCH_EXTRA
14439 char_u buf[NUMBUFLEN];
14440 char_u *group;
14441 int prio = 10;
14442 int id = -1;
14443 int error = FALSE;
14444 list_T *l;
14445
14446 rettv->vval.v_number = -1;
14447
14448 group = get_tv_string_buf_chk(&argvars[0], buf);
14449 if (group == NULL)
14450 return;
14451
14452 if (argvars[1].v_type != VAR_LIST)
14453 {
14454 EMSG2(_(e_listarg), "matchaddpos()");
14455 return;
14456 }
14457 l = argvars[1].vval.v_list;
14458 if (l == NULL)
14459 return;
14460
14461 if (argvars[2].v_type != VAR_UNKNOWN)
14462 {
14463 prio = get_tv_number_chk(&argvars[2], &error);
14464 if (argvars[3].v_type != VAR_UNKNOWN)
14465 id = get_tv_number_chk(&argvars[3], &error);
14466 }
14467 if (error == TRUE)
14468 return;
14469
14470 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
14471 if (id == 1 || id == 2)
14472 {
14473 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14474 return;
14475 }
14476
14477 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014478#endif
14479}
14480
14481/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014482 * "matcharg()" function
14483 */
14484 static void
14485f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014486 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014487 typval_T *rettv;
14488{
14489 if (rettv_list_alloc(rettv) == OK)
14490 {
14491#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014492 int id = get_tv_number(&argvars[0]);
14493 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014494
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014495 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014496 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014497 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14498 {
14499 list_append_string(rettv->vval.v_list,
14500 syn_id2name(m->hlg_id), -1);
14501 list_append_string(rettv->vval.v_list, m->pattern, -1);
14502 }
14503 else
14504 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014505 list_append_string(rettv->vval.v_list, NULL, -1);
14506 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014507 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014508 }
14509#endif
14510 }
14511}
14512
14513/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014514 * "matchdelete()" function
14515 */
14516 static void
14517f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014518 typval_T *argvars UNUSED;
14519 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014520{
14521#ifdef FEAT_SEARCH_EXTRA
14522 rettv->vval.v_number = match_delete(curwin,
14523 (int)get_tv_number(&argvars[0]), TRUE);
14524#endif
14525}
14526
14527/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014528 * "matchend()" function
14529 */
14530 static void
14531f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014532 typval_T *argvars;
14533 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014534{
14535 find_some_match(argvars, rettv, 0);
14536}
14537
14538/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014539 * "matchlist()" function
14540 */
14541 static void
14542f_matchlist(argvars, rettv)
14543 typval_T *argvars;
14544 typval_T *rettv;
14545{
14546 find_some_match(argvars, rettv, 3);
14547}
14548
14549/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014550 * "matchstr()" function
14551 */
14552 static void
14553f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014554 typval_T *argvars;
14555 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556{
14557 find_some_match(argvars, rettv, 2);
14558}
14559
Bram Moolenaar33570922005-01-25 22:26:29 +000014560static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014561
14562 static void
14563max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014564 typval_T *argvars;
14565 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014566 int domax;
14567{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014568 long n = 0;
14569 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014570 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014571
14572 if (argvars[0].v_type == VAR_LIST)
14573 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014574 list_T *l;
14575 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014576
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014577 l = argvars[0].vval.v_list;
14578 if (l != NULL)
14579 {
14580 li = l->lv_first;
14581 if (li != NULL)
14582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014583 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014584 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014585 {
14586 li = li->li_next;
14587 if (li == NULL)
14588 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014589 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014590 if (domax ? i > n : i < n)
14591 n = i;
14592 }
14593 }
14594 }
14595 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014596 else if (argvars[0].v_type == VAR_DICT)
14597 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014598 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014599 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014600 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014601 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014602
14603 d = argvars[0].vval.v_dict;
14604 if (d != NULL)
14605 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014606 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014607 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014608 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014609 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014610 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014611 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014612 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014613 if (first)
14614 {
14615 n = i;
14616 first = FALSE;
14617 }
14618 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014619 n = i;
14620 }
14621 }
14622 }
14623 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014624 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014625 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014626 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014627}
14628
14629/*
14630 * "max()" function
14631 */
14632 static void
14633f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014634 typval_T *argvars;
14635 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014636{
14637 max_min(argvars, rettv, TRUE);
14638}
14639
14640/*
14641 * "min()" function
14642 */
14643 static void
14644f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014645 typval_T *argvars;
14646 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014647{
14648 max_min(argvars, rettv, FALSE);
14649}
14650
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014651static int mkdir_recurse __ARGS((char_u *dir, int prot));
14652
14653/*
14654 * Create the directory in which "dir" is located, and higher levels when
14655 * needed.
14656 */
14657 static int
14658mkdir_recurse(dir, prot)
14659 char_u *dir;
14660 int prot;
14661{
14662 char_u *p;
14663 char_u *updir;
14664 int r = FAIL;
14665
14666 /* Get end of directory name in "dir".
14667 * We're done when it's "/" or "c:/". */
14668 p = gettail_sep(dir);
14669 if (p <= get_past_head(dir))
14670 return OK;
14671
14672 /* If the directory exists we're done. Otherwise: create it.*/
14673 updir = vim_strnsave(dir, (int)(p - dir));
14674 if (updir == NULL)
14675 return FAIL;
14676 if (mch_isdir(updir))
14677 r = OK;
14678 else if (mkdir_recurse(updir, prot) == OK)
14679 r = vim_mkdir_emsg(updir, prot);
14680 vim_free(updir);
14681 return r;
14682}
14683
14684#ifdef vim_mkdir
14685/*
14686 * "mkdir()" function
14687 */
14688 static void
14689f_mkdir(argvars, rettv)
14690 typval_T *argvars;
14691 typval_T *rettv;
14692{
14693 char_u *dir;
14694 char_u buf[NUMBUFLEN];
14695 int prot = 0755;
14696
14697 rettv->vval.v_number = FAIL;
14698 if (check_restricted() || check_secure())
14699 return;
14700
14701 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014702 if (*dir == NUL)
14703 rettv->vval.v_number = FAIL;
14704 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014705 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014706 if (*gettail(dir) == NUL)
14707 /* remove trailing slashes */
14708 *gettail_sep(dir) = NUL;
14709
14710 if (argvars[1].v_type != VAR_UNKNOWN)
14711 {
14712 if (argvars[2].v_type != VAR_UNKNOWN)
14713 prot = get_tv_number_chk(&argvars[2], NULL);
14714 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14715 mkdir_recurse(dir, prot);
14716 }
14717 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014718 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014719}
14720#endif
14721
Bram Moolenaar0d660222005-01-07 21:51:51 +000014722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 * "mode()" function
14724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014726f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014727 typval_T *argvars;
14728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014729{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014730 char_u buf[3];
14731
14732 buf[1] = NUL;
14733 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735 if (VIsual_active)
14736 {
14737 if (VIsual_select)
14738 buf[0] = VIsual_mode + 's' - 'v';
14739 else
14740 buf[0] = VIsual_mode;
14741 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010014742 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014743 || State == CONFIRM)
14744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014745 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014746 if (State == ASKMORE)
14747 buf[1] = 'm';
14748 else if (State == CONFIRM)
14749 buf[1] = '?';
14750 }
14751 else if (State == EXTERNCMD)
14752 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 else if (State & INSERT)
14754 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014755#ifdef FEAT_VREPLACE
14756 if (State & VREPLACE_FLAG)
14757 {
14758 buf[0] = 'R';
14759 buf[1] = 'v';
14760 }
14761 else
14762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763 if (State & REPLACE_FLAG)
14764 buf[0] = 'R';
14765 else
14766 buf[0] = 'i';
14767 }
14768 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014771 if (exmode_active)
14772 buf[1] = 'v';
14773 }
14774 else if (exmode_active)
14775 {
14776 buf[0] = 'c';
14777 buf[1] = 'e';
14778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014779 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014782 if (finish_op)
14783 buf[1] = 'o';
14784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014786 /* Clear out the minor mode when the argument is not a non-zero number or
14787 * non-empty string. */
14788 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014789 buf[1] = NUL;
14790
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791 rettv->vval.v_string = vim_strsave(buf);
14792 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793}
14794
Bram Moolenaar429fa852013-04-15 12:27:36 +020014795#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014796/*
14797 * "mzeval()" function
14798 */
14799 static void
14800f_mzeval(argvars, rettv)
14801 typval_T *argvars;
14802 typval_T *rettv;
14803{
14804 char_u *str;
14805 char_u buf[NUMBUFLEN];
14806
14807 str = get_tv_string_buf(&argvars[0], buf);
14808 do_mzeval(str, rettv);
14809}
Bram Moolenaar75676462013-01-30 14:55:42 +010014810
14811 void
14812mzscheme_call_vim(name, args, rettv)
14813 char_u *name;
14814 typval_T *args;
14815 typval_T *rettv;
14816{
14817 typval_T argvars[3];
14818
14819 argvars[0].v_type = VAR_STRING;
14820 argvars[0].vval.v_string = name;
14821 copy_tv(args, &argvars[1]);
14822 argvars[2].v_type = VAR_UNKNOWN;
14823 f_call(argvars, rettv);
14824 clear_tv(&argvars[1]);
14825}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014826#endif
14827
Bram Moolenaar071d4272004-06-13 20:20:40 +000014828/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014829 * "nextnonblank()" function
14830 */
14831 static void
14832f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014833 typval_T *argvars;
14834 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014835{
14836 linenr_T lnum;
14837
14838 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14839 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014840 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014841 {
14842 lnum = 0;
14843 break;
14844 }
14845 if (*skipwhite(ml_get(lnum)) != NUL)
14846 break;
14847 }
14848 rettv->vval.v_number = lnum;
14849}
14850
14851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 * "nr2char()" function
14853 */
14854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014855f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014856 typval_T *argvars;
14857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858{
14859 char_u buf[NUMBUFLEN];
14860
14861#ifdef FEAT_MBYTE
14862 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014863 {
14864 int utf8 = 0;
14865
14866 if (argvars[1].v_type != VAR_UNKNOWN)
14867 utf8 = get_tv_number_chk(&argvars[1], NULL);
14868 if (utf8)
14869 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14870 else
14871 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014873 else
14874#endif
14875 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014876 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014877 buf[1] = NUL;
14878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014879 rettv->v_type = VAR_STRING;
14880 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014881}
14882
14883/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014884 * "or(expr, expr)" function
14885 */
14886 static void
14887f_or(argvars, rettv)
14888 typval_T *argvars;
14889 typval_T *rettv;
14890{
14891 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14892 | get_tv_number_chk(&argvars[1], NULL);
14893}
14894
14895/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014896 * "pathshorten()" function
14897 */
14898 static void
14899f_pathshorten(argvars, rettv)
14900 typval_T *argvars;
14901 typval_T *rettv;
14902{
14903 char_u *p;
14904
14905 rettv->v_type = VAR_STRING;
14906 p = get_tv_string_chk(&argvars[0]);
14907 if (p == NULL)
14908 rettv->vval.v_string = NULL;
14909 else
14910 {
14911 p = vim_strsave(p);
14912 rettv->vval.v_string = p;
14913 if (p != NULL)
14914 shorten_dir(p);
14915 }
14916}
14917
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014918#ifdef FEAT_FLOAT
14919/*
14920 * "pow()" function
14921 */
14922 static void
14923f_pow(argvars, rettv)
14924 typval_T *argvars;
14925 typval_T *rettv;
14926{
14927 float_T fx, fy;
14928
14929 rettv->v_type = VAR_FLOAT;
14930 if (get_float_arg(argvars, &fx) == OK
14931 && get_float_arg(&argvars[1], &fy) == OK)
14932 rettv->vval.v_float = pow(fx, fy);
14933 else
14934 rettv->vval.v_float = 0.0;
14935}
14936#endif
14937
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014939 * "prevnonblank()" function
14940 */
14941 static void
14942f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014943 typval_T *argvars;
14944 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014945{
14946 linenr_T lnum;
14947
14948 lnum = get_tv_lnum(argvars);
14949 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14950 lnum = 0;
14951 else
14952 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14953 --lnum;
14954 rettv->vval.v_number = lnum;
14955}
14956
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014957#ifdef HAVE_STDARG_H
14958/* This dummy va_list is here because:
14959 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14960 * - locally in the function results in a "used before set" warning
14961 * - using va_start() to initialize it gives "function with fixed args" error */
14962static va_list ap;
14963#endif
14964
Bram Moolenaar8c711452005-01-14 21:53:12 +000014965/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014966 * "printf()" function
14967 */
14968 static void
14969f_printf(argvars, rettv)
14970 typval_T *argvars;
14971 typval_T *rettv;
14972{
14973 rettv->v_type = VAR_STRING;
14974 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014975#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014976 {
14977 char_u buf[NUMBUFLEN];
14978 int len;
14979 char_u *s;
14980 int saved_did_emsg = did_emsg;
14981 char *fmt;
14982
14983 /* Get the required length, allocate the buffer and do it for real. */
14984 did_emsg = FALSE;
14985 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014986 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014987 if (!did_emsg)
14988 {
14989 s = alloc(len + 1);
14990 if (s != NULL)
14991 {
14992 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014993 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014994 }
14995 }
14996 did_emsg |= saved_did_emsg;
14997 }
14998#endif
14999}
15000
15001/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015002 * "pumvisible()" function
15003 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015004 static void
15005f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015006 typval_T *argvars UNUSED;
15007 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015008{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015009#ifdef FEAT_INS_EXPAND
15010 if (pum_visible())
15011 rettv->vval.v_number = 1;
15012#endif
15013}
15014
Bram Moolenaardb913952012-06-29 12:54:53 +020015015#ifdef FEAT_PYTHON3
15016/*
15017 * "py3eval()" function
15018 */
15019 static void
15020f_py3eval(argvars, rettv)
15021 typval_T *argvars;
15022 typval_T *rettv;
15023{
15024 char_u *str;
15025 char_u buf[NUMBUFLEN];
15026
15027 str = get_tv_string_buf(&argvars[0], buf);
15028 do_py3eval(str, rettv);
15029}
15030#endif
15031
15032#ifdef FEAT_PYTHON
15033/*
15034 * "pyeval()" function
15035 */
15036 static void
15037f_pyeval(argvars, rettv)
15038 typval_T *argvars;
15039 typval_T *rettv;
15040{
15041 char_u *str;
15042 char_u buf[NUMBUFLEN];
15043
15044 str = get_tv_string_buf(&argvars[0], buf);
15045 do_pyeval(str, rettv);
15046}
15047#endif
15048
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015049/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015050 * "range()" function
15051 */
15052 static void
15053f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015054 typval_T *argvars;
15055 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015056{
15057 long start;
15058 long end;
15059 long stride = 1;
15060 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015061 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015063 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015064 if (argvars[1].v_type == VAR_UNKNOWN)
15065 {
15066 end = start - 1;
15067 start = 0;
15068 }
15069 else
15070 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015071 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015072 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015073 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015074 }
15075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076 if (error)
15077 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000015078 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015079 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000015080 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015081 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015082 else
15083 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015084 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015085 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015086 if (list_append_number(rettv->vval.v_list,
15087 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015088 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015089 }
15090}
15091
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015092/*
15093 * "readfile()" function
15094 */
15095 static void
15096f_readfile(argvars, rettv)
15097 typval_T *argvars;
15098 typval_T *rettv;
15099{
15100 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015101 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015102 char_u *fname;
15103 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015104 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
15105 int io_size = sizeof(buf);
15106 int readlen; /* size of last fread() */
15107 char_u *prev = NULL; /* previously read bytes, if any */
15108 long prevlen = 0; /* length of data in prev */
15109 long prevsize = 0; /* size of prev buffer */
15110 long maxline = MAXLNUM;
15111 long cnt = 0;
15112 char_u *p; /* position in buf */
15113 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015114
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015115 if (argvars[1].v_type != VAR_UNKNOWN)
15116 {
15117 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
15118 binary = TRUE;
15119 if (argvars[2].v_type != VAR_UNKNOWN)
15120 maxline = get_tv_number(&argvars[2]);
15121 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015122
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015123 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015124 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015125
15126 /* Always open the file in binary mode, library functions have a mind of
15127 * their own about CR-LF conversion. */
15128 fname = get_tv_string(&argvars[0]);
15129 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
15130 {
15131 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
15132 return;
15133 }
15134
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015135 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015136 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015137 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015138
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015139 /* This for loop processes what was read, but is also entered at end
15140 * of file so that either:
15141 * - an incomplete line gets written
15142 * - a "binary" file gets an empty line at the end if it ends in a
15143 * newline. */
15144 for (p = buf, start = buf;
15145 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
15146 ++p)
15147 {
15148 if (*p == '\n' || readlen <= 0)
15149 {
15150 listitem_T *li;
15151 char_u *s = NULL;
15152 long_u len = p - start;
15153
15154 /* Finished a line. Remove CRs before NL. */
15155 if (readlen > 0 && !binary)
15156 {
15157 while (len > 0 && start[len - 1] == '\r')
15158 --len;
15159 /* removal may cross back to the "prev" string */
15160 if (len == 0)
15161 while (prevlen > 0 && prev[prevlen - 1] == '\r')
15162 --prevlen;
15163 }
15164 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015165 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015166 else
15167 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015168 /* Change "prev" buffer to be the right size. This way
15169 * the bytes are only copied once, and very long lines are
15170 * allocated only once. */
15171 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015172 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015173 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015174 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015175 prev = NULL; /* the list will own the string */
15176 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015177 }
15178 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015179 if (s == NULL)
15180 {
15181 do_outofmem_msg((long_u) prevlen + len + 1);
15182 failed = TRUE;
15183 break;
15184 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015185
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015186 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015187 {
15188 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015189 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015190 break;
15191 }
15192 li->li_tv.v_type = VAR_STRING;
15193 li->li_tv.v_lock = 0;
15194 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015195 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015196
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015197 start = p + 1; /* step over newline */
15198 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015199 break;
15200 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015201 else if (*p == NUL)
15202 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020015203#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015204 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
15205 * when finding the BF and check the previous two bytes. */
15206 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020015207 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015208 /* Find the two bytes before the 0xbf. If p is at buf, or buf
15209 * + 1, these may be in the "prev" string. */
15210 char_u back1 = p >= buf + 1 ? p[-1]
15211 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
15212 char_u back2 = p >= buf + 2 ? p[-2]
15213 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
15214 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015215
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015216 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015217 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015218 char_u *dest = p - 2;
15219
15220 /* Usually a BOM is at the beginning of a file, and so at
15221 * the beginning of a line; then we can just step over it.
15222 */
15223 if (start == dest)
15224 start = p + 1;
15225 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020015226 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015227 /* have to shuffle buf to close gap */
15228 int adjust_prevlen = 0;
15229
15230 if (dest < buf)
15231 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015232 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015233 dest = buf;
15234 }
15235 if (readlen > p - buf + 1)
15236 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
15237 readlen -= 3 - adjust_prevlen;
15238 prevlen -= adjust_prevlen;
15239 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020015240 }
15241 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015242 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015243#endif
15244 } /* for */
15245
15246 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
15247 break;
15248 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015249 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015250 /* There's part of a line in buf, store it in "prev". */
15251 if (p - start + prevlen >= prevsize)
15252 {
15253 /* need bigger "prev" buffer */
15254 char_u *newprev;
15255
15256 /* A common use case is ordinary text files and "prev" gets a
15257 * fragment of a line, so the first allocation is made
15258 * small, to avoid repeatedly 'allocing' large and
15259 * 'reallocing' small. */
15260 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015261 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015262 else
15263 {
15264 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015265 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015266 prevsize = grow50pc > growmin ? grow50pc : growmin;
15267 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020015268 newprev = prev == NULL ? alloc(prevsize)
15269 : vim_realloc(prev, prevsize);
15270 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015271 {
15272 do_outofmem_msg((long_u)prevsize);
15273 failed = TRUE;
15274 break;
15275 }
15276 prev = newprev;
15277 }
15278 /* Add the line part to end of "prev". */
15279 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010015280 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015281 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015282 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015283
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015284 /*
15285 * For a negative line count use only the lines at the end of the file,
15286 * free the rest.
15287 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015288 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015289 while (cnt > -maxline)
15290 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015291 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000015292 --cnt;
15293 }
15294
Bram Moolenaara489e1d2012-02-05 00:39:18 +010015295 if (failed)
15296 {
15297 list_free(rettv->vval.v_list, TRUE);
15298 /* readfile doc says an empty list is returned on error */
15299 rettv->vval.v_list = list_alloc();
15300 }
15301
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015302 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015303 fclose(fd);
15304}
15305
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015306#if defined(FEAT_RELTIME)
15307static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
15308
15309/*
15310 * Convert a List to proftime_T.
15311 * Return FAIL when there is something wrong.
15312 */
15313 static int
15314list2proftime(arg, tm)
15315 typval_T *arg;
15316 proftime_T *tm;
15317{
15318 long n1, n2;
15319 int error = FALSE;
15320
15321 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
15322 || arg->vval.v_list->lv_len != 2)
15323 return FAIL;
15324 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
15325 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
15326# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015327 tm->HighPart = n1;
15328 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015329# else
15330 tm->tv_sec = n1;
15331 tm->tv_usec = n2;
15332# endif
15333 return error ? FAIL : OK;
15334}
15335#endif /* FEAT_RELTIME */
15336
15337/*
15338 * "reltime()" function
15339 */
15340 static void
15341f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015342 typval_T *argvars UNUSED;
15343 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015344{
15345#ifdef FEAT_RELTIME
15346 proftime_T res;
15347 proftime_T start;
15348
15349 if (argvars[0].v_type == VAR_UNKNOWN)
15350 {
15351 /* No arguments: get current time. */
15352 profile_start(&res);
15353 }
15354 else if (argvars[1].v_type == VAR_UNKNOWN)
15355 {
15356 if (list2proftime(&argvars[0], &res) == FAIL)
15357 return;
15358 profile_end(&res);
15359 }
15360 else
15361 {
15362 /* Two arguments: compute the difference. */
15363 if (list2proftime(&argvars[0], &start) == FAIL
15364 || list2proftime(&argvars[1], &res) == FAIL)
15365 return;
15366 profile_sub(&res, &start);
15367 }
15368
15369 if (rettv_list_alloc(rettv) == OK)
15370 {
15371 long n1, n2;
15372
15373# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015374 n1 = res.HighPart;
15375 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015376# else
15377 n1 = res.tv_sec;
15378 n2 = res.tv_usec;
15379# endif
15380 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15381 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15382 }
15383#endif
15384}
15385
15386/*
15387 * "reltimestr()" function
15388 */
15389 static void
15390f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015391 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015392 typval_T *rettv;
15393{
15394#ifdef FEAT_RELTIME
15395 proftime_T tm;
15396#endif
15397
15398 rettv->v_type = VAR_STRING;
15399 rettv->vval.v_string = NULL;
15400#ifdef FEAT_RELTIME
15401 if (list2proftime(&argvars[0], &tm) == OK)
15402 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15403#endif
15404}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015405
Bram Moolenaar0d660222005-01-07 21:51:51 +000015406#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15407static void make_connection __ARGS((void));
15408static int check_connection __ARGS((void));
15409
15410 static void
15411make_connection()
15412{
15413 if (X_DISPLAY == NULL
15414# ifdef FEAT_GUI
15415 && !gui.in_use
15416# endif
15417 )
15418 {
15419 x_force_connect = TRUE;
15420 setup_term_clip();
15421 x_force_connect = FALSE;
15422 }
15423}
15424
15425 static int
15426check_connection()
15427{
15428 make_connection();
15429 if (X_DISPLAY == NULL)
15430 {
15431 EMSG(_("E240: No connection to Vim server"));
15432 return FAIL;
15433 }
15434 return OK;
15435}
15436#endif
15437
15438#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015439static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015440
15441 static void
15442remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015443 typval_T *argvars;
15444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015445 int expr;
15446{
15447 char_u *server_name;
15448 char_u *keys;
15449 char_u *r = NULL;
15450 char_u buf[NUMBUFLEN];
15451# ifdef WIN32
15452 HWND w;
15453# else
15454 Window w;
15455# endif
15456
15457 if (check_restricted() || check_secure())
15458 return;
15459
15460# ifdef FEAT_X11
15461 if (check_connection() == FAIL)
15462 return;
15463# endif
15464
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015465 server_name = get_tv_string_chk(&argvars[0]);
15466 if (server_name == NULL)
15467 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015468 keys = get_tv_string_buf(&argvars[1], buf);
15469# ifdef WIN32
15470 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15471# else
15472 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15473 < 0)
15474# endif
15475 {
15476 if (r != NULL)
15477 EMSG(r); /* sending worked but evaluation failed */
15478 else
15479 EMSG2(_("E241: Unable to send to %s"), server_name);
15480 return;
15481 }
15482
15483 rettv->vval.v_string = r;
15484
15485 if (argvars[2].v_type != VAR_UNKNOWN)
15486 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015487 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015488 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015489 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015490
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015491 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015492 v.di_tv.v_type = VAR_STRING;
15493 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015494 idvar = get_tv_string_chk(&argvars[2]);
15495 if (idvar != NULL)
15496 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015497 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015498 }
15499}
15500#endif
15501
15502/*
15503 * "remote_expr()" function
15504 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015505 static void
15506f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015508 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015509{
15510 rettv->v_type = VAR_STRING;
15511 rettv->vval.v_string = NULL;
15512#ifdef FEAT_CLIENTSERVER
15513 remote_common(argvars, rettv, TRUE);
15514#endif
15515}
15516
15517/*
15518 * "remote_foreground()" function
15519 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015520 static void
15521f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015522 typval_T *argvars UNUSED;
15523 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015524{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015525#ifdef FEAT_CLIENTSERVER
15526# ifdef WIN32
15527 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015528 {
15529 char_u *server_name = get_tv_string_chk(&argvars[0]);
15530
15531 if (server_name != NULL)
15532 serverForeground(server_name);
15533 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015534# else
15535 /* Send a foreground() expression to the server. */
15536 argvars[1].v_type = VAR_STRING;
15537 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15538 argvars[2].v_type = VAR_UNKNOWN;
15539 remote_common(argvars, rettv, TRUE);
15540 vim_free(argvars[1].vval.v_string);
15541# endif
15542#endif
15543}
15544
Bram Moolenaar0d660222005-01-07 21:51:51 +000015545 static void
15546f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015547 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015548 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015549{
15550#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015551 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015552 char_u *s = NULL;
15553# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015554 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015555# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015556 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015557
15558 if (check_restricted() || check_secure())
15559 {
15560 rettv->vval.v_number = -1;
15561 return;
15562 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015563 serverid = get_tv_string_chk(&argvars[0]);
15564 if (serverid == NULL)
15565 {
15566 rettv->vval.v_number = -1;
15567 return; /* type error; errmsg already given */
15568 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015569# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015570 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015571 if (n == 0)
15572 rettv->vval.v_number = -1;
15573 else
15574 {
15575 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15576 rettv->vval.v_number = (s != NULL);
15577 }
15578# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015579 if (check_connection() == FAIL)
15580 return;
15581
15582 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015584# endif
15585
15586 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15587 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015588 char_u *retvar;
15589
Bram Moolenaar33570922005-01-25 22:26:29 +000015590 v.di_tv.v_type = VAR_STRING;
15591 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015592 retvar = get_tv_string_chk(&argvars[1]);
15593 if (retvar != NULL)
15594 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015595 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596 }
15597#else
15598 rettv->vval.v_number = -1;
15599#endif
15600}
15601
Bram Moolenaar0d660222005-01-07 21:51:51 +000015602 static void
15603f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015604 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015605 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606{
15607 char_u *r = NULL;
15608
15609#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015610 char_u *serverid = get_tv_string_chk(&argvars[0]);
15611
15612 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015613 {
15614# ifdef WIN32
15615 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015616 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015617
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015618 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015619 if (n != 0)
15620 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15621 if (r == NULL)
15622# else
15623 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015624 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015625# endif
15626 EMSG(_("E277: Unable to read a server reply"));
15627 }
15628#endif
15629 rettv->v_type = VAR_STRING;
15630 rettv->vval.v_string = r;
15631}
15632
15633/*
15634 * "remote_send()" function
15635 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015636 static void
15637f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015638 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640{
15641 rettv->v_type = VAR_STRING;
15642 rettv->vval.v_string = NULL;
15643#ifdef FEAT_CLIENTSERVER
15644 remote_common(argvars, rettv, FALSE);
15645#endif
15646}
15647
15648/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015649 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015650 */
15651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015652f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015653 typval_T *argvars;
15654 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015655{
Bram Moolenaar33570922005-01-25 22:26:29 +000015656 list_T *l;
15657 listitem_T *item, *item2;
15658 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015659 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015660 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015661 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015662 dict_T *d;
15663 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015664 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015665
Bram Moolenaar8c711452005-01-14 21:53:12 +000015666 if (argvars[0].v_type == VAR_DICT)
15667 {
15668 if (argvars[2].v_type != VAR_UNKNOWN)
15669 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015670 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015671 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015673 key = get_tv_string_chk(&argvars[1]);
15674 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015676 di = dict_find(d, key, -1);
15677 if (di == NULL)
15678 EMSG2(_(e_dictkey), key);
15679 else
15680 {
15681 *rettv = di->di_tv;
15682 init_tv(&di->di_tv);
15683 dictitem_remove(d, di);
15684 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015686 }
15687 }
15688 else if (argvars[0].v_type != VAR_LIST)
15689 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015690 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015691 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015692 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015693 int error = FALSE;
15694
15695 idx = get_tv_number_chk(&argvars[1], &error);
15696 if (error)
15697 ; /* type error: do nothing, errmsg already given */
15698 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015699 EMSGN(_(e_listidx), idx);
15700 else
15701 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015702 if (argvars[2].v_type == VAR_UNKNOWN)
15703 {
15704 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015705 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015706 *rettv = item->li_tv;
15707 vim_free(item);
15708 }
15709 else
15710 {
15711 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015712 end = get_tv_number_chk(&argvars[2], &error);
15713 if (error)
15714 ; /* type error: do nothing */
15715 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015716 EMSGN(_(e_listidx), end);
15717 else
15718 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015719 int cnt = 0;
15720
15721 for (li = item; li != NULL; li = li->li_next)
15722 {
15723 ++cnt;
15724 if (li == item2)
15725 break;
15726 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015727 if (li == NULL) /* didn't find "item2" after "item" */
15728 EMSG(_(e_invrange));
15729 else
15730 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020015731 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015732 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015733 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015734 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015735 l->lv_first = item;
15736 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015737 item->li_prev = NULL;
15738 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015739 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015740 }
15741 }
15742 }
15743 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015744 }
15745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746}
15747
15748/*
15749 * "rename({from}, {to})" function
15750 */
15751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015752f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015753 typval_T *argvars;
15754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755{
15756 char_u buf[NUMBUFLEN];
15757
15758 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015761 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15762 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763}
15764
15765/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015766 * "repeat()" function
15767 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015768 static void
15769f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015770 typval_T *argvars;
15771 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015772{
15773 char_u *p;
15774 int n;
15775 int slen;
15776 int len;
15777 char_u *r;
15778 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015779
15780 n = get_tv_number(&argvars[1]);
15781 if (argvars[0].v_type == VAR_LIST)
15782 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015783 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015784 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015785 if (list_extend(rettv->vval.v_list,
15786 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015787 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015788 }
15789 else
15790 {
15791 p = get_tv_string(&argvars[0]);
15792 rettv->v_type = VAR_STRING;
15793 rettv->vval.v_string = NULL;
15794
15795 slen = (int)STRLEN(p);
15796 len = slen * n;
15797 if (len <= 0)
15798 return;
15799
15800 r = alloc(len + 1);
15801 if (r != NULL)
15802 {
15803 for (i = 0; i < n; i++)
15804 mch_memmove(r + i * slen, p, (size_t)slen);
15805 r[len] = NUL;
15806 }
15807
15808 rettv->vval.v_string = r;
15809 }
15810}
15811
15812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813 * "resolve()" function
15814 */
15815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015816f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015817 typval_T *argvars;
15818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819{
15820 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015821#ifdef HAVE_READLINK
15822 char_u *buf = NULL;
15823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015825 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015826#ifdef FEAT_SHORTCUT
15827 {
15828 char_u *v = NULL;
15829
15830 v = mch_resolve_shortcut(p);
15831 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015832 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015834 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 }
15836#else
15837# ifdef HAVE_READLINK
15838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 char_u *cpy;
15840 int len;
15841 char_u *remain = NULL;
15842 char_u *q;
15843 int is_relative_to_current = FALSE;
15844 int has_trailing_pathsep = FALSE;
15845 int limit = 100;
15846
15847 p = vim_strsave(p);
15848
15849 if (p[0] == '.' && (vim_ispathsep(p[1])
15850 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15851 is_relative_to_current = TRUE;
15852
15853 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015854 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015857 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015859
15860 q = getnextcomp(p);
15861 if (*q != NUL)
15862 {
15863 /* Separate the first path component in "p", and keep the
15864 * remainder (beginning with the path separator). */
15865 remain = vim_strsave(q - 1);
15866 q[-1] = NUL;
15867 }
15868
Bram Moolenaard9462e32011-04-11 21:35:11 +020015869 buf = alloc(MAXPATHL + 1);
15870 if (buf == NULL)
15871 goto fail;
15872
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873 for (;;)
15874 {
15875 for (;;)
15876 {
15877 len = readlink((char *)p, (char *)buf, MAXPATHL);
15878 if (len <= 0)
15879 break;
15880 buf[len] = NUL;
15881
15882 if (limit-- == 0)
15883 {
15884 vim_free(p);
15885 vim_free(remain);
15886 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015887 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 goto fail;
15889 }
15890
15891 /* Ensure that the result will have a trailing path separator
15892 * if the argument has one. */
15893 if (remain == NULL && has_trailing_pathsep)
15894 add_pathsep(buf);
15895
15896 /* Separate the first path component in the link value and
15897 * concatenate the remainders. */
15898 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15899 if (*q != NUL)
15900 {
15901 if (remain == NULL)
15902 remain = vim_strsave(q - 1);
15903 else
15904 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015905 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906 if (cpy != NULL)
15907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908 vim_free(remain);
15909 remain = cpy;
15910 }
15911 }
15912 q[-1] = NUL;
15913 }
15914
15915 q = gettail(p);
15916 if (q > p && *q == NUL)
15917 {
15918 /* Ignore trailing path separator. */
15919 q[-1] = NUL;
15920 q = gettail(p);
15921 }
15922 if (q > p && !mch_isFullName(buf))
15923 {
15924 /* symlink is relative to directory of argument */
15925 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15926 if (cpy != NULL)
15927 {
15928 STRCPY(cpy, p);
15929 STRCPY(gettail(cpy), buf);
15930 vim_free(p);
15931 p = cpy;
15932 }
15933 }
15934 else
15935 {
15936 vim_free(p);
15937 p = vim_strsave(buf);
15938 }
15939 }
15940
15941 if (remain == NULL)
15942 break;
15943
15944 /* Append the first path component of "remain" to "p". */
15945 q = getnextcomp(remain + 1);
15946 len = q - remain - (*q != NUL);
15947 cpy = vim_strnsave(p, STRLEN(p) + len);
15948 if (cpy != NULL)
15949 {
15950 STRNCAT(cpy, remain, len);
15951 vim_free(p);
15952 p = cpy;
15953 }
15954 /* Shorten "remain". */
15955 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015956 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015957 else
15958 {
15959 vim_free(remain);
15960 remain = NULL;
15961 }
15962 }
15963
15964 /* If the result is a relative path name, make it explicitly relative to
15965 * the current directory if and only if the argument had this form. */
15966 if (!vim_ispathsep(*p))
15967 {
15968 if (is_relative_to_current
15969 && *p != NUL
15970 && !(p[0] == '.'
15971 && (p[1] == NUL
15972 || vim_ispathsep(p[1])
15973 || (p[1] == '.'
15974 && (p[2] == NUL
15975 || vim_ispathsep(p[2]))))))
15976 {
15977 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015978 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979 if (cpy != NULL)
15980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 vim_free(p);
15982 p = cpy;
15983 }
15984 }
15985 else if (!is_relative_to_current)
15986 {
15987 /* Strip leading "./". */
15988 q = p;
15989 while (q[0] == '.' && vim_ispathsep(q[1]))
15990 q += 2;
15991 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015992 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 }
15994 }
15995
15996 /* Ensure that the result will have no trailing path separator
15997 * if the argument had none. But keep "/" or "//". */
15998 if (!has_trailing_pathsep)
15999 {
16000 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016001 if (after_pathsep(p, q))
16002 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 }
16004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016005 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 }
16007# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016008 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009# endif
16010#endif
16011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013
16014#ifdef HAVE_READLINK
16015fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016016 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016018 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016019}
16020
16021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016022 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023 */
16024 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016025f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016026 typval_T *argvars;
16027 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028{
Bram Moolenaar33570922005-01-25 22:26:29 +000016029 list_T *l;
16030 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031
Bram Moolenaar0d660222005-01-07 21:51:51 +000016032 if (argvars[0].v_type != VAR_LIST)
16033 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016034 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020016035 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016036 {
16037 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016038 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016039 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016040 while (li != NULL)
16041 {
16042 ni = li->li_prev;
16043 list_append(l, li);
16044 li = ni;
16045 }
16046 rettv->vval.v_list = l;
16047 rettv->v_type = VAR_LIST;
16048 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000016049 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051}
16052
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016053#define SP_NOMOVE 0x01 /* don't move cursor */
16054#define SP_REPEAT 0x02 /* repeat to find outer pair */
16055#define SP_RETCOUNT 0x04 /* return matchcount */
16056#define SP_SETPCMARK 0x08 /* set previous context mark */
16057#define SP_START 0x10 /* accept match at start position */
16058#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
16059#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016060
Bram Moolenaar33570922005-01-25 22:26:29 +000016061static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016062
16063/*
16064 * Get flags for a search function.
16065 * Possibly sets "p_ws".
16066 * Returns BACKWARD, FORWARD or zero (for an error).
16067 */
16068 static int
16069get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016070 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016071 int *flagsp;
16072{
16073 int dir = FORWARD;
16074 char_u *flags;
16075 char_u nbuf[NUMBUFLEN];
16076 int mask;
16077
16078 if (varp->v_type != VAR_UNKNOWN)
16079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016080 flags = get_tv_string_buf_chk(varp, nbuf);
16081 if (flags == NULL)
16082 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016083 while (*flags != NUL)
16084 {
16085 switch (*flags)
16086 {
16087 case 'b': dir = BACKWARD; break;
16088 case 'w': p_ws = TRUE; break;
16089 case 'W': p_ws = FALSE; break;
16090 default: mask = 0;
16091 if (flagsp != NULL)
16092 switch (*flags)
16093 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016094 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016095 case 'e': mask = SP_END; break;
16096 case 'm': mask = SP_RETCOUNT; break;
16097 case 'n': mask = SP_NOMOVE; break;
16098 case 'p': mask = SP_SUBPAT; break;
16099 case 'r': mask = SP_REPEAT; break;
16100 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016101 }
16102 if (mask == 0)
16103 {
16104 EMSG2(_(e_invarg2), flags);
16105 dir = 0;
16106 }
16107 else
16108 *flagsp |= mask;
16109 }
16110 if (dir == 0)
16111 break;
16112 ++flags;
16113 }
16114 }
16115 return dir;
16116}
16117
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016119 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016121 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016122search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000016123 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016124 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016125 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016127 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 char_u *pat;
16129 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016130 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 int save_p_ws = p_ws;
16132 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016133 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016134 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016135 proftime_T tm;
16136#ifdef FEAT_RELTIME
16137 long time_limit = 0;
16138#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016139 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016140 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016142 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016143 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016144 if (dir == 0)
16145 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016146 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016147 if (flags & SP_START)
16148 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016149 if (flags & SP_END)
16150 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016151
Bram Moolenaar76929292008-01-06 19:07:36 +000016152 /* Optional arguments: line number to stop searching and timeout. */
16153 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016154 {
16155 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
16156 if (lnum_stop < 0)
16157 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016158#ifdef FEAT_RELTIME
16159 if (argvars[3].v_type != VAR_UNKNOWN)
16160 {
16161 time_limit = get_tv_number_chk(&argvars[3], NULL);
16162 if (time_limit < 0)
16163 goto theend;
16164 }
16165#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016166 }
16167
Bram Moolenaar76929292008-01-06 19:07:36 +000016168#ifdef FEAT_RELTIME
16169 /* Set the time limit, if there is one. */
16170 profile_setlimit(time_limit, &tm);
16171#endif
16172
Bram Moolenaar231334e2005-07-25 20:46:57 +000016173 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016174 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016175 * Check to make sure only those flags are set.
16176 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
16177 * flags cannot be set. Check for that condition also.
16178 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016179 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016180 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016183 goto theend;
16184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016186 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016187 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016188 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016189 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016191 if (flags & SP_SUBPAT)
16192 retval = subpatnum;
16193 else
16194 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016195 if (flags & SP_SETPCMARK)
16196 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016198 if (match_pos != NULL)
16199 {
16200 /* Store the match cursor position */
16201 match_pos->lnum = pos.lnum;
16202 match_pos->col = pos.col + 1;
16203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 /* "/$" will put the cursor after the end of the line, may need to
16205 * correct that here */
16206 check_cursor();
16207 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016208
16209 /* If 'n' flag is used: restore cursor position. */
16210 if (flags & SP_NOMOVE)
16211 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000016212 else
16213 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016214theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016216
16217 return retval;
16218}
16219
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016220#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016221
16222/*
16223 * round() is not in C90, use ceil() or floor() instead.
16224 */
16225 float_T
16226vim_round(f)
16227 float_T f;
16228{
16229 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
16230}
16231
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016232/*
16233 * "round({float})" function
16234 */
16235 static void
16236f_round(argvars, rettv)
16237 typval_T *argvars;
16238 typval_T *rettv;
16239{
16240 float_T f;
16241
16242 rettv->v_type = VAR_FLOAT;
16243 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020016244 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016245 else
16246 rettv->vval.v_float = 0.0;
16247}
16248#endif
16249
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016250/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020016251 * "screenattr()" function
16252 */
16253 static void
16254f_screenattr(argvars, rettv)
16255 typval_T *argvars UNUSED;
16256 typval_T *rettv;
16257{
16258 int row;
16259 int col;
16260 int c;
16261
16262 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16263 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16264 if (row < 0 || row >= screen_Rows
16265 || col < 0 || col >= screen_Columns)
16266 c = -1;
16267 else
16268 c = ScreenAttrs[LineOffset[row] + col];
16269 rettv->vval.v_number = c;
16270}
16271
16272/*
16273 * "screenchar()" function
16274 */
16275 static void
16276f_screenchar(argvars, rettv)
16277 typval_T *argvars UNUSED;
16278 typval_T *rettv;
16279{
16280 int row;
16281 int col;
16282 int off;
16283 int c;
16284
16285 row = get_tv_number_chk(&argvars[0], NULL) - 1;
16286 col = get_tv_number_chk(&argvars[1], NULL) - 1;
16287 if (row < 0 || row >= screen_Rows
16288 || col < 0 || col >= screen_Columns)
16289 c = -1;
16290 else
16291 {
16292 off = LineOffset[row] + col;
16293#ifdef FEAT_MBYTE
16294 if (enc_utf8 && ScreenLinesUC[off] != 0)
16295 c = ScreenLinesUC[off];
16296 else
16297#endif
16298 c = ScreenLines[off];
16299 }
16300 rettv->vval.v_number = c;
16301}
16302
16303/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010016304 * "screencol()" function
16305 *
16306 * First column is 1 to be consistent with virtcol().
16307 */
16308 static void
16309f_screencol(argvars, rettv)
16310 typval_T *argvars UNUSED;
16311 typval_T *rettv;
16312{
16313 rettv->vval.v_number = screen_screencol() + 1;
16314}
16315
16316/*
16317 * "screenrow()" function
16318 */
16319 static void
16320f_screenrow(argvars, rettv)
16321 typval_T *argvars UNUSED;
16322 typval_T *rettv;
16323{
16324 rettv->vval.v_number = screen_screenrow() + 1;
16325}
16326
16327/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016328 * "search()" function
16329 */
16330 static void
16331f_search(argvars, rettv)
16332 typval_T *argvars;
16333 typval_T *rettv;
16334{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016335 int flags = 0;
16336
16337 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338}
16339
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016341 * "searchdecl()" function
16342 */
16343 static void
16344f_searchdecl(argvars, rettv)
16345 typval_T *argvars;
16346 typval_T *rettv;
16347{
16348 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016349 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016350 int error = FALSE;
16351 char_u *name;
16352
16353 rettv->vval.v_number = 1; /* default: FAIL */
16354
16355 name = get_tv_string_chk(&argvars[0]);
16356 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016357 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016358 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016359 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16360 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16361 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016362 if (!error && name != NULL)
16363 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016364 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016365}
16366
16367/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016368 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016370 static int
16371searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016372 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016373 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374{
16375 char_u *spat, *mpat, *epat;
16376 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016377 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 int dir;
16379 int flags = 0;
16380 char_u nbuf1[NUMBUFLEN];
16381 char_u nbuf2[NUMBUFLEN];
16382 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016383 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016384 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016385 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016388 spat = get_tv_string_chk(&argvars[0]);
16389 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16390 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16391 if (spat == NULL || mpat == NULL || epat == NULL)
16392 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394 /* Handle the optional fourth argument: flags */
16395 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016396 if (dir == 0)
16397 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016398
16399 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016400 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16401 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016402 if ((flags & (SP_END | SP_SUBPAT)) != 0
16403 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016404 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016405 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016406 goto theend;
16407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016408
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016409 /* Using 'r' implies 'W', otherwise it doesn't work. */
16410 if (flags & SP_REPEAT)
16411 p_ws = FALSE;
16412
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016413 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016414 if (argvars[3].v_type == VAR_UNKNOWN
16415 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 skip = (char_u *)"";
16417 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016419 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016420 if (argvars[5].v_type != VAR_UNKNOWN)
16421 {
16422 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16423 if (lnum_stop < 0)
16424 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016425#ifdef FEAT_RELTIME
16426 if (argvars[6].v_type != VAR_UNKNOWN)
16427 {
16428 time_limit = get_tv_number_chk(&argvars[6], NULL);
16429 if (time_limit < 0)
16430 goto theend;
16431 }
16432#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016433 }
16434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016435 if (skip == NULL)
16436 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016437
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016438 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016439 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016440
16441theend:
16442 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016443
16444 return retval;
16445}
16446
16447/*
16448 * "searchpair()" function
16449 */
16450 static void
16451f_searchpair(argvars, rettv)
16452 typval_T *argvars;
16453 typval_T *rettv;
16454{
16455 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16456}
16457
16458/*
16459 * "searchpairpos()" function
16460 */
16461 static void
16462f_searchpairpos(argvars, rettv)
16463 typval_T *argvars;
16464 typval_T *rettv;
16465{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016466 pos_T match_pos;
16467 int lnum = 0;
16468 int col = 0;
16469
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016470 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016471 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016472
16473 if (searchpair_cmn(argvars, &match_pos) > 0)
16474 {
16475 lnum = match_pos.lnum;
16476 col = match_pos.col;
16477 }
16478
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016479 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16480 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016481}
16482
16483/*
16484 * Search for a start/middle/end thing.
16485 * Used by searchpair(), see its documentation for the details.
16486 * Returns 0 or -1 for no match,
16487 */
16488 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016489do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16490 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016491 char_u *spat; /* start pattern */
16492 char_u *mpat; /* middle pattern */
16493 char_u *epat; /* end pattern */
16494 int dir; /* BACKWARD or FORWARD */
16495 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016496 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016497 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016498 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016499 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016500{
16501 char_u *save_cpo;
16502 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16503 long retval = 0;
16504 pos_T pos;
16505 pos_T firstpos;
16506 pos_T foundpos;
16507 pos_T save_cursor;
16508 pos_T save_pos;
16509 int n;
16510 int r;
16511 int nest = 1;
16512 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016513 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016514 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016515
16516 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16517 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016518 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016519
Bram Moolenaar76929292008-01-06 19:07:36 +000016520#ifdef FEAT_RELTIME
16521 /* Set the time limit, if there is one. */
16522 profile_setlimit(time_limit, &tm);
16523#endif
16524
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016525 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16526 * start/middle/end (pat3, for the top pair). */
16527 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16528 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16529 if (pat2 == NULL || pat3 == NULL)
16530 goto theend;
16531 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16532 if (*mpat == NUL)
16533 STRCPY(pat3, pat2);
16534 else
16535 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16536 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016537 if (flags & SP_START)
16538 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016539
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 save_cursor = curwin->w_cursor;
16541 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016542 clearpos(&firstpos);
16543 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016544 pat = pat3;
16545 for (;;)
16546 {
16547 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016548 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016549 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16550 /* didn't find it or found the first match again: FAIL */
16551 break;
16552
16553 if (firstpos.lnum == 0)
16554 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016555 if (equalpos(pos, foundpos))
16556 {
16557 /* Found the same position again. Can happen with a pattern that
16558 * has "\zs" at the end and searching backwards. Advance one
16559 * character and try again. */
16560 if (dir == BACKWARD)
16561 decl(&pos);
16562 else
16563 incl(&pos);
16564 }
16565 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016567 /* clear the start flag to avoid getting stuck here */
16568 options &= ~SEARCH_START;
16569
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 /* If the skip pattern matches, ignore this match. */
16571 if (*skip != NUL)
16572 {
16573 save_pos = curwin->w_cursor;
16574 curwin->w_cursor = pos;
16575 r = eval_to_bool(skip, &err, NULL, FALSE);
16576 curwin->w_cursor = save_pos;
16577 if (err)
16578 {
16579 /* Evaluating {skip} caused an error, break here. */
16580 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016581 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016582 break;
16583 }
16584 if (r)
16585 continue;
16586 }
16587
16588 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16589 {
16590 /* Found end when searching backwards or start when searching
16591 * forward: nested pair. */
16592 ++nest;
16593 pat = pat2; /* nested, don't search for middle */
16594 }
16595 else
16596 {
16597 /* Found end when searching forward or start when searching
16598 * backward: end of (nested) pair; or found middle in outer pair. */
16599 if (--nest == 1)
16600 pat = pat3; /* outer level, search for middle */
16601 }
16602
16603 if (nest == 0)
16604 {
16605 /* Found the match: return matchcount or line number. */
16606 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016607 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016608 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016609 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016610 if (flags & SP_SETPCMARK)
16611 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016612 curwin->w_cursor = pos;
16613 if (!(flags & SP_REPEAT))
16614 break;
16615 nest = 1; /* search for next unmatched */
16616 }
16617 }
16618
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016619 if (match_pos != NULL)
16620 {
16621 /* Store the match cursor position */
16622 match_pos->lnum = curwin->w_cursor.lnum;
16623 match_pos->col = curwin->w_cursor.col + 1;
16624 }
16625
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016627 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 curwin->w_cursor = save_cursor;
16629
16630theend:
16631 vim_free(pat2);
16632 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016633 if (p_cpo == empty_option)
16634 p_cpo = save_cpo;
16635 else
16636 /* Darn, evaluating the {skip} expression changed the value. */
16637 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016638
16639 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016640}
16641
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016642/*
16643 * "searchpos()" function
16644 */
16645 static void
16646f_searchpos(argvars, rettv)
16647 typval_T *argvars;
16648 typval_T *rettv;
16649{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016650 pos_T match_pos;
16651 int lnum = 0;
16652 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016653 int n;
16654 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016655
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016656 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016657 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016658
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016659 n = search_cmn(argvars, &match_pos, &flags);
16660 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016661 {
16662 lnum = match_pos.lnum;
16663 col = match_pos.col;
16664 }
16665
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016666 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16667 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016668 if (flags & SP_SUBPAT)
16669 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016670}
16671
16672
Bram Moolenaar0d660222005-01-07 21:51:51 +000016673 static void
16674f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016675 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016676 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016678#ifdef FEAT_CLIENTSERVER
16679 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016680 char_u *server = get_tv_string_chk(&argvars[0]);
16681 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016682
Bram Moolenaar0d660222005-01-07 21:51:51 +000016683 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016684 if (server == NULL || reply == NULL)
16685 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016686 if (check_restricted() || check_secure())
16687 return;
16688# ifdef FEAT_X11
16689 if (check_connection() == FAIL)
16690 return;
16691# endif
16692
16693 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016694 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016695 EMSG(_("E258: Unable to send to client"));
16696 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016698 rettv->vval.v_number = 0;
16699#else
16700 rettv->vval.v_number = -1;
16701#endif
16702}
16703
Bram Moolenaar0d660222005-01-07 21:51:51 +000016704 static void
16705f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016706 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016707 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016708{
16709 char_u *r = NULL;
16710
16711#ifdef FEAT_CLIENTSERVER
16712# ifdef WIN32
16713 r = serverGetVimNames();
16714# else
16715 make_connection();
16716 if (X_DISPLAY != NULL)
16717 r = serverGetVimNames(X_DISPLAY);
16718# endif
16719#endif
16720 rettv->v_type = VAR_STRING;
16721 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722}
16723
16724/*
16725 * "setbufvar()" function
16726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016728f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016729 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016730 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731{
16732 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016735 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 char_u nbuf[NUMBUFLEN];
16737
16738 if (check_restricted() || check_secure())
16739 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016740 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16741 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016742 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 varp = &argvars[2];
16744
16745 if (buf != NULL && varname != NULL && varp != NULL)
16746 {
16747 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749
16750 if (*varname == '&')
16751 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016752 long numval;
16753 char_u *strval;
16754 int error = FALSE;
16755
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016757 numval = get_tv_number_chk(varp, &error);
16758 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016759 if (!error && strval != NULL)
16760 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016761 }
16762 else
16763 {
16764 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16765 if (bufvarname != NULL)
16766 {
16767 STRCPY(bufvarname, "b:");
16768 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016769 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 vim_free(bufvarname);
16771 }
16772 }
16773
16774 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016775 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777}
16778
16779/*
16780 * "setcmdpos()" function
16781 */
16782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016783f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016784 typval_T *argvars;
16785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016786{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016787 int pos = (int)get_tv_number(&argvars[0]) - 1;
16788
16789 if (pos >= 0)
16790 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016791}
16792
16793/*
16794 * "setline()" function
16795 */
16796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016797f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016798 typval_T *argvars;
16799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800{
16801 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016802 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016803 list_T *l = NULL;
16804 listitem_T *li = NULL;
16805 long added = 0;
16806 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016808 lnum = get_tv_lnum(&argvars[0]);
16809 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016811 l = argvars[1].vval.v_list;
16812 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016814 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016815 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016816
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016817 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016818 for (;;)
16819 {
16820 if (l != NULL)
16821 {
16822 /* list argument, get next string */
16823 if (li == NULL)
16824 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016825 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016826 li = li->li_next;
16827 }
16828
16829 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016830 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016831 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016832
16833 /* When coming here from Insert mode, sync undo, so that this can be
16834 * undone separately from what was previously inserted. */
16835 if (u_sync_once == 2)
16836 {
16837 u_sync_once = 1; /* notify that u_sync() was called */
16838 u_sync(TRUE);
16839 }
16840
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016841 if (lnum <= curbuf->b_ml.ml_line_count)
16842 {
16843 /* existing line, replace it */
16844 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16845 {
16846 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016847 if (lnum == curwin->w_cursor.lnum)
16848 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016849 rettv->vval.v_number = 0; /* OK */
16850 }
16851 }
16852 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16853 {
16854 /* lnum is one past the last line, append the line */
16855 ++added;
16856 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16857 rettv->vval.v_number = 0; /* OK */
16858 }
16859
16860 if (l == NULL) /* only one string argument */
16861 break;
16862 ++lnum;
16863 }
16864
16865 if (added > 0)
16866 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016867}
16868
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016869static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16870
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016872 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016873 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016874 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016875set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016876 win_T *wp UNUSED;
16877 typval_T *list_arg UNUSED;
16878 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016879 typval_T *rettv;
16880{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016881#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016882 char_u *act;
16883 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016884#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016885
Bram Moolenaar2641f772005-03-25 21:58:17 +000016886 rettv->vval.v_number = -1;
16887
16888#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016889 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016890 EMSG(_(e_listreq));
16891 else
16892 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016893 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016894
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016895 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016896 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016897 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016898 if (act == NULL)
16899 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016900 if (*act == 'a' || *act == 'r')
16901 action = *act;
16902 }
16903
Bram Moolenaar81484f42012-12-05 15:16:47 +010016904 if (l != NULL && set_errorlist(wp, l, action,
16905 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016906 rettv->vval.v_number = 0;
16907 }
16908#endif
16909}
16910
16911/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016912 * "setloclist()" function
16913 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016914 static void
16915f_setloclist(argvars, rettv)
16916 typval_T *argvars;
16917 typval_T *rettv;
16918{
16919 win_T *win;
16920
16921 rettv->vval.v_number = -1;
16922
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016923 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016924 if (win != NULL)
16925 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16926}
16927
16928/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016929 * "setmatches()" function
16930 */
16931 static void
16932f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016933 typval_T *argvars UNUSED;
16934 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016935{
16936#ifdef FEAT_SEARCH_EXTRA
16937 list_T *l;
16938 listitem_T *li;
16939 dict_T *d;
16940
16941 rettv->vval.v_number = -1;
16942 if (argvars[0].v_type != VAR_LIST)
16943 {
16944 EMSG(_(e_listreq));
16945 return;
16946 }
16947 if ((l = argvars[0].vval.v_list) != NULL)
16948 {
16949
16950 /* To some extent make sure that we are dealing with a list from
16951 * "getmatches()". */
16952 li = l->lv_first;
16953 while (li != NULL)
16954 {
16955 if (li->li_tv.v_type != VAR_DICT
16956 || (d = li->li_tv.vval.v_dict) == NULL)
16957 {
16958 EMSG(_(e_invarg));
16959 return;
16960 }
16961 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16962 && dict_find(d, (char_u *)"pattern", -1) != NULL
16963 && dict_find(d, (char_u *)"priority", -1) != NULL
16964 && dict_find(d, (char_u *)"id", -1) != NULL))
16965 {
16966 EMSG(_(e_invarg));
16967 return;
16968 }
16969 li = li->li_next;
16970 }
16971
16972 clear_matches(curwin);
16973 li = l->lv_first;
16974 while (li != NULL)
16975 {
16976 d = li->li_tv.vval.v_dict;
16977 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16978 get_dict_string(d, (char_u *)"pattern", FALSE),
16979 (int)get_dict_number(d, (char_u *)"priority"),
Bram Moolenaarb3414592014-06-17 17:48:32 +020016980 (int)get_dict_number(d, (char_u *)"id"), NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016981 li = li->li_next;
16982 }
16983 rettv->vval.v_number = 0;
16984 }
16985#endif
16986}
16987
16988/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016989 * "setpos()" function
16990 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016991 static void
16992f_setpos(argvars, rettv)
16993 typval_T *argvars;
16994 typval_T *rettv;
16995{
16996 pos_T pos;
16997 int fnum;
16998 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020016999 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017000
Bram Moolenaar08250432008-02-13 11:42:46 +000017001 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017002 name = get_tv_string_chk(argvars);
17003 if (name != NULL)
17004 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020017005 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017006 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000017007 if (--pos.col < 0)
17008 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000017009 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017010 {
Bram Moolenaar08250432008-02-13 11:42:46 +000017011 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017012 if (fnum == curbuf->b_fnum)
17013 {
17014 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020017015 if (curswant >= 0)
17016 curwin->w_curswant = curswant - 1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017017 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000017018 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017019 }
17020 else
17021 EMSG(_(e_invarg));
17022 }
Bram Moolenaar08250432008-02-13 11:42:46 +000017023 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
17024 {
17025 /* set mark */
17026 if (setmark_pos(name[1], &pos, fnum) == OK)
17027 rettv->vval.v_number = 0;
17028 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017029 else
17030 EMSG(_(e_invarg));
17031 }
17032 }
17033}
17034
17035/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017036 * "setqflist()" function
17037 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017038 static void
17039f_setqflist(argvars, rettv)
17040 typval_T *argvars;
17041 typval_T *rettv;
17042{
17043 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
17044}
17045
17046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 * "setreg()" function
17048 */
17049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017050f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017051 typval_T *argvars;
17052 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053{
17054 int regname;
17055 char_u *strregname;
17056 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058 int append;
17059 char_u yank_type;
17060 long block_len;
17061
17062 block_len = -1;
17063 yank_type = MAUTO;
17064 append = FALSE;
17065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017066 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017067 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017069 if (strregname == NULL)
17070 return; /* type error; errmsg already given */
17071 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072 if (regname == 0 || regname == '@')
17073 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017075 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017077 stropt = get_tv_string_chk(&argvars[2]);
17078 if (stropt == NULL)
17079 return; /* type error */
17080 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 switch (*stropt)
17082 {
17083 case 'a': case 'A': /* append */
17084 append = TRUE;
17085 break;
17086 case 'v': case 'c': /* character-wise selection */
17087 yank_type = MCHAR;
17088 break;
17089 case 'V': case 'l': /* line-wise selection */
17090 yank_type = MLINE;
17091 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092 case 'b': case Ctrl_V: /* block-wise selection */
17093 yank_type = MBLOCK;
17094 if (VIM_ISDIGIT(stropt[1]))
17095 {
17096 ++stropt;
17097 block_len = getdigits(&stropt) - 1;
17098 --stropt;
17099 }
17100 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101 }
17102 }
17103
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017104 if (argvars[1].v_type == VAR_LIST)
17105 {
17106 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017107 char_u **allocval;
17108 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017109 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017110 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017111 int len = argvars[1].vval.v_list->lv_len;
17112 listitem_T *li;
17113
Bram Moolenaar7d647822014-04-05 21:28:56 +020017114 /* First half: use for pointers to result lines; second half: use for
17115 * pointers to allocated copies. */
17116 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017117 if (lstval == NULL)
17118 return;
17119 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020017120 allocval = lstval + len + 2;
17121 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017122
17123 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
17124 li = li->li_next)
17125 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017126 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017127 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020017128 goto free_lstval;
17129 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017130 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020017131 /* Need to make a copy, next get_tv_string_buf_chk() will
17132 * overwrite the string. */
17133 strval = vim_strsave(buf);
17134 if (strval == NULL)
17135 goto free_lstval;
17136 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017137 }
17138 *curval++ = strval;
17139 }
17140 *curval++ = NULL;
17141
17142 write_reg_contents_lst(regname, lstval, -1,
17143 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020017144free_lstval:
17145 while (curallocval > allocval)
17146 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017147 vim_free(lstval);
17148 }
17149 else
17150 {
17151 strval = get_tv_string_chk(&argvars[1]);
17152 if (strval == NULL)
17153 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017154 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020017156 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017157 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158}
17159
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017160/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017161 * "settabvar()" function
17162 */
17163 static void
17164f_settabvar(argvars, rettv)
17165 typval_T *argvars;
17166 typval_T *rettv;
17167{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017168#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017169 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017170 tabpage_T *tp;
17171#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017172 char_u *varname, *tabvarname;
17173 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017174
17175 rettv->vval.v_number = 0;
17176
17177 if (check_restricted() || check_secure())
17178 return;
17179
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017180#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017181 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017182#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017183 varname = get_tv_string_chk(&argvars[1]);
17184 varp = &argvars[2];
17185
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017186 if (varname != NULL && varp != NULL
17187#ifdef FEAT_WINDOWS
17188 && tp != NULL
17189#endif
17190 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017191 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017192#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017193 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017194 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017195#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017196
17197 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
17198 if (tabvarname != NULL)
17199 {
17200 STRCPY(tabvarname, "t:");
17201 STRCPY(tabvarname + 2, varname);
17202 set_var(tabvarname, varp, TRUE);
17203 vim_free(tabvarname);
17204 }
17205
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017206#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017207 /* Restore current tabpage */
17208 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020017209 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017210#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020017211 }
17212}
17213
17214/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017215 * "settabwinvar()" function
17216 */
17217 static void
17218f_settabwinvar(argvars, rettv)
17219 typval_T *argvars;
17220 typval_T *rettv;
17221{
17222 setwinvar(argvars, rettv, 1);
17223}
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224
17225/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017226 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017229f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017230 typval_T *argvars;
17231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017233 setwinvar(argvars, rettv, 0);
17234}
17235
17236/*
17237 * "setwinvar()" and "settabwinvar()" functions
17238 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020017239
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017240 static void
17241setwinvar(argvars, rettv, off)
17242 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017243 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017244 int off;
17245{
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246 win_T *win;
17247#ifdef FEAT_WINDOWS
17248 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017249 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250#endif
17251 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017252 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020017254 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017255
17256 if (check_restricted() || check_secure())
17257 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017258
17259#ifdef FEAT_WINDOWS
17260 if (off == 1)
17261 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
17262 else
17263 tp = curtab;
17264#endif
17265 win = find_win_by_nr(&argvars[off], tp);
17266 varname = get_tv_string_chk(&argvars[off + 1]);
17267 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268
17269 if (win != NULL && varname != NULL && varp != NULL)
17270 {
17271#ifdef FEAT_WINDOWS
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017272 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017275 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020017277 long numval;
17278 char_u *strval;
17279 int error = FALSE;
17280
17281 ++varname;
17282 numval = get_tv_number_chk(varp, &error);
17283 strval = get_tv_string_buf_chk(varp, nbuf);
17284 if (!error && strval != NULL)
17285 set_option_value(varname, numval, strval, OPT_LOCAL);
17286 }
17287 else
17288 {
17289 winvarname = alloc((unsigned)STRLEN(varname) + 3);
17290 if (winvarname != NULL)
17291 {
17292 STRCPY(winvarname, "w:");
17293 STRCPY(winvarname + 2, varname);
17294 set_var(winvarname, varp, TRUE);
17295 vim_free(winvarname);
17296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 }
17298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020017300 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301#endif
17302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017303}
17304
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010017305#ifdef FEAT_CRYPT
17306/*
17307 * "sha256({string})" function
17308 */
17309 static void
17310f_sha256(argvars, rettv)
17311 typval_T *argvars;
17312 typval_T *rettv;
17313{
17314 char_u *p;
17315
17316 p = get_tv_string(&argvars[0]);
17317 rettv->vval.v_string = vim_strsave(
17318 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
17319 rettv->v_type = VAR_STRING;
17320}
17321#endif /* FEAT_CRYPT */
17322
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017324 * "shellescape({string})" function
17325 */
17326 static void
17327f_shellescape(argvars, rettv)
17328 typval_T *argvars;
17329 typval_T *rettv;
17330{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017331 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010017332 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000017333 rettv->v_type = VAR_STRING;
17334}
17335
17336/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017337 * shiftwidth() function
17338 */
17339 static void
17340f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020017341 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017342 typval_T *rettv;
17343{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010017344 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020017345}
17346
17347/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017348 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017349 */
17350 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017351f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017352 typval_T *argvars;
17353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017355 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356
Bram Moolenaar0d660222005-01-07 21:51:51 +000017357 p = get_tv_string(&argvars[0]);
17358 rettv->vval.v_string = vim_strsave(p);
17359 simplify_filename(rettv->vval.v_string); /* simplify in place */
17360 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017361}
17362
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017363#ifdef FEAT_FLOAT
17364/*
17365 * "sin()" function
17366 */
17367 static void
17368f_sin(argvars, rettv)
17369 typval_T *argvars;
17370 typval_T *rettv;
17371{
17372 float_T f;
17373
17374 rettv->v_type = VAR_FLOAT;
17375 if (get_float_arg(argvars, &f) == OK)
17376 rettv->vval.v_float = sin(f);
17377 else
17378 rettv->vval.v_float = 0.0;
17379}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017380
17381/*
17382 * "sinh()" function
17383 */
17384 static void
17385f_sinh(argvars, rettv)
17386 typval_T *argvars;
17387 typval_T *rettv;
17388{
17389 float_T f;
17390
17391 rettv->v_type = VAR_FLOAT;
17392 if (get_float_arg(argvars, &f) == OK)
17393 rettv->vval.v_float = sinh(f);
17394 else
17395 rettv->vval.v_float = 0.0;
17396}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017397#endif
17398
Bram Moolenaar0d660222005-01-07 21:51:51 +000017399static int
17400#ifdef __BORLANDC__
17401 _RTLENTRYF
17402#endif
17403 item_compare __ARGS((const void *s1, const void *s2));
17404static int
17405#ifdef __BORLANDC__
17406 _RTLENTRYF
17407#endif
17408 item_compare2 __ARGS((const void *s1, const void *s2));
17409
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017410/* struct used in the array that's given to qsort() */
17411typedef struct
17412{
17413 listitem_T *item;
17414 int idx;
17415} sortItem_T;
17416
Bram Moolenaar0d660222005-01-07 21:51:51 +000017417static int item_compare_ic;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017418static int item_compare_numeric;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017419static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017420static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017421static int item_compare_func_err;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017422static int item_compare_keep_zero;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017423static void do_sort_uniq __ARGS((typval_T *argvars, typval_T *rettv, int sort));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017424#define ITEM_COMPARE_FAIL 999
17425
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017427 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017429 static int
17430#ifdef __BORLANDC__
17431_RTLENTRYF
17432#endif
17433item_compare(s1, s2)
17434 const void *s1;
17435 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017437 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017438 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017439 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017440 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017441 int res;
17442 char_u numbuf1[NUMBUFLEN];
17443 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017445 si1 = (sortItem_T *)s1;
17446 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017447 tv1 = &si1->item->li_tv;
17448 tv2 = &si2->item->li_tv;
17449 /* tv2string() puts quotes around a string and allocates memory. Don't do
17450 * that for string variables. Use a single quote when comparing with a
17451 * non-string to do what the docs promise. */
17452 if (tv1->v_type == VAR_STRING)
17453 {
17454 if (tv2->v_type != VAR_STRING || item_compare_numeric)
17455 p1 = (char_u *)"'";
17456 else
17457 p1 = tv1->vval.v_string;
17458 }
17459 else
17460 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
17461 if (tv2->v_type == VAR_STRING)
17462 {
17463 if (tv1->v_type != VAR_STRING || item_compare_numeric)
17464 p2 = (char_u *)"'";
17465 else
17466 p2 = tv2->vval.v_string;
17467 }
17468 else
17469 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017470 if (p1 == NULL)
17471 p1 = (char_u *)"";
17472 if (p2 == NULL)
17473 p2 = (char_u *)"";
Bram Moolenaare8a34922014-06-25 17:31:09 +020017474 if (!item_compare_numeric)
17475 {
17476 if (item_compare_ic)
17477 res = STRICMP(p1, p2);
17478 else
17479 res = STRCMP(p1, p2);
17480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020017482 {
17483 double n1, n2;
17484 n1 = strtod((char *)p1, (char **)&p1);
17485 n2 = strtod((char *)p2, (char **)&p2);
17486 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
17487 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017488
Bram Moolenaar1b338d22014-08-22 13:13:27 +020017489 /* When the result would be zero, compare the item indexes. Makes the
17490 * sort stable. */
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017491 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017492 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017493
Bram Moolenaar0d660222005-01-07 21:51:51 +000017494 vim_free(tofree1);
17495 vim_free(tofree2);
17496 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497}
17498
17499 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017500#ifdef __BORLANDC__
17501_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017503item_compare2(s1, s2)
17504 const void *s1;
17505 const void *s2;
17506{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017507 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017508 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017509 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017510 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017511 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017513 /* shortcut after failure in previous call; compare all items equal */
17514 if (item_compare_func_err)
17515 return 0;
17516
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017517 si1 = (sortItem_T *)s1;
17518 si2 = (sortItem_T *)s2;
17519
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017520 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017521 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017522 copy_tv(&si1->item->li_tv, &argv[0]);
17523 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017524
17525 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017526 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017527 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17528 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017529 clear_tv(&argv[0]);
17530 clear_tv(&argv[1]);
17531
17532 if (res == FAIL)
17533 res = ITEM_COMPARE_FAIL;
17534 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017535 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17536 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017537 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017538 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017539
17540 /* When the result would be zero, compare the pointers themselves. Makes
17541 * the sort stable. */
17542 if (res == 0 && !item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017543 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017544
Bram Moolenaar0d660222005-01-07 21:51:51 +000017545 return res;
17546}
17547
17548/*
17549 * "sort({list})" function
17550 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551 static void
Bram Moolenaar327aa022014-03-25 18:24:23 +010017552do_sort_uniq(argvars, rettv, sort)
Bram Moolenaar33570922005-01-25 22:26:29 +000017553 typval_T *argvars;
17554 typval_T *rettv;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017555 int sort;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556{
Bram Moolenaar33570922005-01-25 22:26:29 +000017557 list_T *l;
17558 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017559 sortItem_T *ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017560 long len;
17561 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562
Bram Moolenaar0d660222005-01-07 21:51:51 +000017563 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017564 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565 else
17566 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017567 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017568 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar327aa022014-03-25 18:24:23 +010017569 (char_u *)(sort ? _("sort() argument") : _("uniq() argument"))))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017570 return;
17571 rettv->vval.v_list = l;
17572 rettv->v_type = VAR_LIST;
17573 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017574
Bram Moolenaar0d660222005-01-07 21:51:51 +000017575 len = list_len(l);
17576 if (len <= 1)
17577 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578
Bram Moolenaar0d660222005-01-07 21:51:51 +000017579 item_compare_ic = FALSE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020017580 item_compare_numeric = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017581 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017582 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017583 if (argvars[1].v_type != VAR_UNKNOWN)
17584 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017585 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017586 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017587 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017588 else
17589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017590 int error = FALSE;
17591
17592 i = get_tv_number_chk(&argvars[1], &error);
17593 if (error)
17594 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595 if (i == 1)
17596 item_compare_ic = TRUE;
17597 else
17598 item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaare8a34922014-06-25 17:31:09 +020017599 if (item_compare_func != NULL)
17600 {
17601 if (STRCMP(item_compare_func, "n") == 0)
17602 {
17603 item_compare_func = NULL;
17604 item_compare_numeric = TRUE;
17605 }
17606 else if (STRCMP(item_compare_func, "i") == 0)
17607 {
17608 item_compare_func = NULL;
17609 item_compare_ic = TRUE;
17610 }
17611 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017612 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017613
17614 if (argvars[2].v_type != VAR_UNKNOWN)
17615 {
17616 /* optional third argument: {dict} */
17617 if (argvars[2].v_type != VAR_DICT)
17618 {
17619 EMSG(_(e_dictreq));
17620 return;
17621 }
17622 item_compare_selfdict = argvars[2].vval.v_dict;
17623 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625
Bram Moolenaar0d660222005-01-07 21:51:51 +000017626 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017627 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017628 if (ptrs == NULL)
17629 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630
Bram Moolenaar327aa022014-03-25 18:24:23 +010017631 i = 0;
17632 if (sort)
17633 {
17634 /* sort(): ptrs will be the list to sort */
17635 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017636 {
17637 ptrs[i].item = li;
17638 ptrs[i].idx = i;
17639 ++i;
17640 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010017641
17642 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017643 item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017644 /* test the compare function */
17645 if (item_compare_func != NULL
17646 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000017647 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010017648 EMSG(_("E702: Sort compare function failed"));
17649 else
17650 {
17651 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017652 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar327aa022014-03-25 18:24:23 +010017653 item_compare_func == NULL ? item_compare : item_compare2);
17654
17655 if (!item_compare_func_err)
17656 {
17657 /* Clear the List and append the items in sorted order. */
17658 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
17659 l->lv_len = 0;
17660 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017661 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010017662 }
17663 }
17664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017666 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017667 int (*item_compare_func_ptr)__ARGS((const void *, const void *));
17668
17669 /* f_uniq(): ptrs will be a stack of items to remove */
17670 item_compare_func_err = FALSE;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020017671 item_compare_keep_zero = TRUE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017672 item_compare_func_ptr = item_compare_func
17673 ? item_compare2 : item_compare;
17674
17675 for (li = l->lv_first; li != NULL && li->li_next != NULL;
17676 li = li->li_next)
17677 {
17678 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
17679 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017680 ptrs[i++].item = li;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017681 if (item_compare_func_err)
17682 {
17683 EMSG(_("E882: Uniq compare function failed"));
17684 break;
17685 }
17686 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017688 if (!item_compare_func_err)
17689 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010017690 while (--i >= 0)
17691 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017692 li = ptrs[i].item->li_next;
17693 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017694 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017695 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017696 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020017697 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010017698 list_fix_watch(l, li);
17699 listitem_free(li);
17700 l->lv_len--;
17701 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017702 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017703 }
17704
17705 vim_free(ptrs);
17706 }
17707}
17708
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017709/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010017710 * "sort({list})" function
17711 */
17712 static void
17713f_sort(argvars, rettv)
17714 typval_T *argvars;
17715 typval_T *rettv;
17716{
17717 do_sort_uniq(argvars, rettv, TRUE);
17718}
17719
17720/*
17721 * "uniq({list})" function
17722 */
17723 static void
17724f_uniq(argvars, rettv)
17725 typval_T *argvars;
17726 typval_T *rettv;
17727{
17728 do_sort_uniq(argvars, rettv, FALSE);
17729}
17730
17731/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017732 * "soundfold({word})" function
17733 */
17734 static void
17735f_soundfold(argvars, rettv)
17736 typval_T *argvars;
17737 typval_T *rettv;
17738{
17739 char_u *s;
17740
17741 rettv->v_type = VAR_STRING;
17742 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017743#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017744 rettv->vval.v_string = eval_soundfold(s);
17745#else
17746 rettv->vval.v_string = vim_strsave(s);
17747#endif
17748}
17749
17750/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017751 * "spellbadword()" function
17752 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017753 static void
17754f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017755 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017756 typval_T *rettv;
17757{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017758 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017759 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017760 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017761
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017762 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017763 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017764
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017765#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017766 if (argvars[0].v_type == VAR_UNKNOWN)
17767 {
17768 /* Find the start and length of the badly spelled word. */
17769 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17770 if (len != 0)
17771 word = ml_get_cursor();
17772 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017773 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017774 {
17775 char_u *str = get_tv_string_chk(&argvars[0]);
17776 int capcol = -1;
17777
17778 if (str != NULL)
17779 {
17780 /* Check the argument for spelling. */
17781 while (*str != NUL)
17782 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017783 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017784 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017785 {
17786 word = str;
17787 break;
17788 }
17789 str += len;
17790 }
17791 }
17792 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017793#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017794
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017795 list_append_string(rettv->vval.v_list, word, len);
17796 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017797 attr == HLF_SPB ? "bad" :
17798 attr == HLF_SPR ? "rare" :
17799 attr == HLF_SPL ? "local" :
17800 attr == HLF_SPC ? "caps" :
17801 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017802}
17803
17804/*
17805 * "spellsuggest()" function
17806 */
17807 static void
17808f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017809 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017810 typval_T *rettv;
17811{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017812#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017813 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017814 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017815 int maxcount;
17816 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017817 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017818 listitem_T *li;
17819 int need_capital = FALSE;
17820#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017821
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017822 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017823 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017824
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017825#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017826 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017827 {
17828 str = get_tv_string(&argvars[0]);
17829 if (argvars[1].v_type != VAR_UNKNOWN)
17830 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017831 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017832 if (maxcount <= 0)
17833 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017834 if (argvars[2].v_type != VAR_UNKNOWN)
17835 {
17836 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17837 if (typeerr)
17838 return;
17839 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017840 }
17841 else
17842 maxcount = 25;
17843
Bram Moolenaar4770d092006-01-12 23:22:24 +000017844 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017845
17846 for (i = 0; i < ga.ga_len; ++i)
17847 {
17848 str = ((char_u **)ga.ga_data)[i];
17849
17850 li = listitem_alloc();
17851 if (li == NULL)
17852 vim_free(str);
17853 else
17854 {
17855 li->li_tv.v_type = VAR_STRING;
17856 li->li_tv.v_lock = 0;
17857 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017858 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017859 }
17860 }
17861 ga_clear(&ga);
17862 }
17863#endif
17864}
17865
Bram Moolenaar0d660222005-01-07 21:51:51 +000017866 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017867f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017868 typval_T *argvars;
17869 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017870{
17871 char_u *str;
17872 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017873 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017874 regmatch_T regmatch;
17875 char_u patbuf[NUMBUFLEN];
17876 char_u *save_cpo;
17877 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017878 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017879 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017880 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017881
17882 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17883 save_cpo = p_cpo;
17884 p_cpo = (char_u *)"";
17885
17886 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017887 if (argvars[1].v_type != VAR_UNKNOWN)
17888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017889 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17890 if (pat == NULL)
17891 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017892 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017893 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017894 }
17895 if (pat == NULL || *pat == NUL)
17896 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017897
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017898 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017900 if (typeerr)
17901 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902
Bram Moolenaar0d660222005-01-07 21:51:51 +000017903 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17904 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017906 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017907 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017908 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017909 if (*str == NUL)
17910 match = FALSE; /* empty item at the end */
17911 else
17912 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017913 if (match)
17914 end = regmatch.startp[0];
17915 else
17916 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017917 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17918 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017919 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017920 if (list_append_string(rettv->vval.v_list, str,
17921 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017922 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017923 }
17924 if (!match)
17925 break;
17926 /* Advance to just after the match. */
17927 if (regmatch.endp[0] > str)
17928 col = 0;
17929 else
17930 {
17931 /* Don't get stuck at the same match. */
17932#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017933 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017934#else
17935 col = 1;
17936#endif
17937 }
17938 str = regmatch.endp[0];
17939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940
Bram Moolenaar473de612013-06-08 18:19:48 +020017941 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943
Bram Moolenaar0d660222005-01-07 21:51:51 +000017944 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945}
17946
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017947#ifdef FEAT_FLOAT
17948/*
17949 * "sqrt()" function
17950 */
17951 static void
17952f_sqrt(argvars, rettv)
17953 typval_T *argvars;
17954 typval_T *rettv;
17955{
17956 float_T f;
17957
17958 rettv->v_type = VAR_FLOAT;
17959 if (get_float_arg(argvars, &f) == OK)
17960 rettv->vval.v_float = sqrt(f);
17961 else
17962 rettv->vval.v_float = 0.0;
17963}
17964
17965/*
17966 * "str2float()" function
17967 */
17968 static void
17969f_str2float(argvars, rettv)
17970 typval_T *argvars;
17971 typval_T *rettv;
17972{
17973 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17974
17975 if (*p == '+')
17976 p = skipwhite(p + 1);
17977 (void)string2float(p, &rettv->vval.v_float);
17978 rettv->v_type = VAR_FLOAT;
17979}
17980#endif
17981
Bram Moolenaar2c932302006-03-18 21:42:09 +000017982/*
17983 * "str2nr()" function
17984 */
17985 static void
17986f_str2nr(argvars, rettv)
17987 typval_T *argvars;
17988 typval_T *rettv;
17989{
17990 int base = 10;
17991 char_u *p;
17992 long n;
17993
17994 if (argvars[1].v_type != VAR_UNKNOWN)
17995 {
17996 base = get_tv_number(&argvars[1]);
17997 if (base != 8 && base != 10 && base != 16)
17998 {
17999 EMSG(_(e_invarg));
18000 return;
18001 }
18002 }
18003
18004 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018005 if (*p == '+')
18006 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000018007 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
18008 rettv->vval.v_number = n;
18009}
18010
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011#ifdef HAVE_STRFTIME
18012/*
18013 * "strftime({format}[, {time}])" function
18014 */
18015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018016f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018017 typval_T *argvars;
18018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019{
18020 char_u result_buf[256];
18021 struct tm *curtime;
18022 time_t seconds;
18023 char_u *p;
18024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018025 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018027 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018028 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 seconds = time(NULL);
18030 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018031 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 curtime = localtime(&seconds);
18033 /* MSVC returns NULL for an invalid value of seconds. */
18034 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018035 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 else
18037 {
18038# ifdef FEAT_MBYTE
18039 vimconv_T conv;
18040 char_u *enc;
18041
18042 conv.vc_type = CONV_NONE;
18043 enc = enc_locale();
18044 convert_setup(&conv, p_enc, enc);
18045 if (conv.vc_type != CONV_NONE)
18046 p = string_convert(&conv, p, NULL);
18047# endif
18048 if (p != NULL)
18049 (void)strftime((char *)result_buf, sizeof(result_buf),
18050 (char *)p, curtime);
18051 else
18052 result_buf[0] = NUL;
18053
18054# ifdef FEAT_MBYTE
18055 if (conv.vc_type != CONV_NONE)
18056 vim_free(p);
18057 convert_setup(&conv, enc, p_enc);
18058 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018059 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 else
18061# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018062 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063
18064# ifdef FEAT_MBYTE
18065 /* Release conversion descriptors */
18066 convert_setup(&conv, NULL, NULL);
18067 vim_free(enc);
18068# endif
18069 }
18070}
18071#endif
18072
18073/*
18074 * "stridx()" function
18075 */
18076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018077f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018078 typval_T *argvars;
18079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080{
18081 char_u buf[NUMBUFLEN];
18082 char_u *needle;
18083 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000018084 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000018086 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018088 needle = get_tv_string_chk(&argvars[1]);
18089 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000018090 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018091 if (needle == NULL || haystack == NULL)
18092 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093
Bram Moolenaar33570922005-01-25 22:26:29 +000018094 if (argvars[2].v_type != VAR_UNKNOWN)
18095 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018096 int error = FALSE;
18097
18098 start_idx = get_tv_number_chk(&argvars[2], &error);
18099 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000018100 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018101 if (start_idx >= 0)
18102 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000018103 }
18104
18105 pos = (char_u *)strstr((char *)haystack, (char *)needle);
18106 if (pos != NULL)
18107 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108}
18109
18110/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018111 * "string()" function
18112 */
18113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018114f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018115 typval_T *argvars;
18116 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018117{
18118 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018119 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018121 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000018122 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018123 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018124 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018125 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126}
18127
18128/*
18129 * "strlen()" function
18130 */
18131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018133 typval_T *argvars;
18134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018136 rettv->vval.v_number = (varnumber_T)(STRLEN(
18137 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138}
18139
18140/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018141 * "strchars()" function
18142 */
18143 static void
18144f_strchars(argvars, rettv)
18145 typval_T *argvars;
18146 typval_T *rettv;
18147{
18148 char_u *s = get_tv_string(&argvars[0]);
18149#ifdef FEAT_MBYTE
18150 varnumber_T len = 0;
18151
18152 while (*s != NUL)
18153 {
18154 mb_cptr2char_adv(&s);
18155 ++len;
18156 }
18157 rettv->vval.v_number = len;
18158#else
18159 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
18160#endif
18161}
18162
18163/*
Bram Moolenaardc536092010-07-18 15:45:49 +020018164 * "strdisplaywidth()" function
18165 */
18166 static void
18167f_strdisplaywidth(argvars, rettv)
18168 typval_T *argvars;
18169 typval_T *rettv;
18170{
18171 char_u *s = get_tv_string(&argvars[0]);
18172 int col = 0;
18173
18174 if (argvars[1].v_type != VAR_UNKNOWN)
18175 col = get_tv_number(&argvars[1]);
18176
Bram Moolenaar8a09b982010-07-22 22:20:57 +020018177 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020018178}
18179
18180/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020018181 * "strwidth()" function
18182 */
18183 static void
18184f_strwidth(argvars, rettv)
18185 typval_T *argvars;
18186 typval_T *rettv;
18187{
18188 char_u *s = get_tv_string(&argvars[0]);
18189
18190 rettv->vval.v_number = (varnumber_T)(
18191#ifdef FEAT_MBYTE
18192 mb_string2cells(s, -1)
18193#else
18194 STRLEN(s)
18195#endif
18196 );
18197}
18198
18199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 * "strpart()" function
18201 */
18202 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018203f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018204 typval_T *argvars;
18205 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206{
18207 char_u *p;
18208 int n;
18209 int len;
18210 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018211 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018213 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 slen = (int)STRLEN(p);
18215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018216 n = get_tv_number_chk(&argvars[1], &error);
18217 if (error)
18218 len = 0;
18219 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018220 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 else
18222 len = slen - n; /* default len: all bytes that are available. */
18223
18224 /*
18225 * Only return the overlap between the specified part and the actual
18226 * string.
18227 */
18228 if (n < 0)
18229 {
18230 len += n;
18231 n = 0;
18232 }
18233 else if (n > slen)
18234 n = slen;
18235 if (len < 0)
18236 len = 0;
18237 else if (n + len > slen)
18238 len = slen - n;
18239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018240 rettv->v_type = VAR_STRING;
18241 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242}
18243
18244/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018245 * "strridx()" function
18246 */
18247 static void
18248f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018249 typval_T *argvars;
18250 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018251{
18252 char_u buf[NUMBUFLEN];
18253 char_u *needle;
18254 char_u *haystack;
18255 char_u *rest;
18256 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000018257 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018258
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018259 needle = get_tv_string_chk(&argvars[1]);
18260 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018261
18262 rettv->vval.v_number = -1;
18263 if (needle == NULL || haystack == NULL)
18264 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018265
18266 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018267 if (argvars[2].v_type != VAR_UNKNOWN)
18268 {
18269 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018270 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018271 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018272 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018273 }
18274 else
18275 end_idx = haystack_len;
18276
Bram Moolenaar0d660222005-01-07 21:51:51 +000018277 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018278 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018279 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018280 lastmatch = haystack + end_idx;
18281 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018282 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000018283 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018284 for (rest = haystack; *rest != '\0'; ++rest)
18285 {
18286 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000018287 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018288 break;
18289 lastmatch = rest;
18290 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000018291 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018292
18293 if (lastmatch == NULL)
18294 rettv->vval.v_number = -1;
18295 else
18296 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
18297}
18298
18299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018300 * "strtrans()" function
18301 */
18302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018303f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018304 typval_T *argvars;
18305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018307 rettv->v_type = VAR_STRING;
18308 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309}
18310
18311/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018312 * "submatch()" function
18313 */
18314 static void
18315f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018316 typval_T *argvars;
18317 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018318{
Bram Moolenaar41571762014-04-02 19:00:58 +020018319 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020018320 int no;
18321 int retList = 0;
18322
18323 no = (int)get_tv_number_chk(&argvars[0], &error);
18324 if (error)
18325 return;
18326 error = FALSE;
18327 if (argvars[1].v_type != VAR_UNKNOWN)
18328 retList = get_tv_number_chk(&argvars[1], &error);
18329 if (error)
18330 return;
18331
18332 if (retList == 0)
18333 {
18334 rettv->v_type = VAR_STRING;
18335 rettv->vval.v_string = reg_submatch(no);
18336 }
18337 else
18338 {
18339 rettv->v_type = VAR_LIST;
18340 rettv->vval.v_list = reg_submatch_list(no);
18341 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018342}
18343
18344/*
18345 * "substitute()" function
18346 */
18347 static void
18348f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018349 typval_T *argvars;
18350 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018351{
18352 char_u patbuf[NUMBUFLEN];
18353 char_u subbuf[NUMBUFLEN];
18354 char_u flagsbuf[NUMBUFLEN];
18355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018356 char_u *str = get_tv_string_chk(&argvars[0]);
18357 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18358 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
18359 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
18360
Bram Moolenaar0d660222005-01-07 21:51:51 +000018361 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018362 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
18363 rettv->vval.v_string = NULL;
18364 else
18365 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018366}
18367
18368/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018369 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018372f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018373 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375{
18376 int id = 0;
18377#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018378 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 long col;
18380 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000018381 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018383 lnum = get_tv_lnum(argvars); /* -1 on type error */
18384 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18385 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018387 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000018388 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018389 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390#endif
18391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018392 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393}
18394
18395/*
18396 * "synIDattr(id, what [, mode])" function
18397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018399f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018400 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018401 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018402{
18403 char_u *p = NULL;
18404#ifdef FEAT_SYN_HL
18405 int id;
18406 char_u *what;
18407 char_u *mode;
18408 char_u modebuf[NUMBUFLEN];
18409 int modec;
18410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018411 id = get_tv_number(&argvars[0]);
18412 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018413 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018415 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020018417 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418 modec = 0; /* replace invalid with current */
18419 }
18420 else
18421 {
18422#ifdef FEAT_GUI
18423 if (gui.in_use)
18424 modec = 'g';
18425 else
18426#endif
18427 if (t_colors > 1)
18428 modec = 'c';
18429 else
18430 modec = 't';
18431 }
18432
18433
18434 switch (TOLOWER_ASC(what[0]))
18435 {
18436 case 'b':
18437 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
18438 p = highlight_color(id, what, modec);
18439 else /* bold */
18440 p = highlight_has_attr(id, HL_BOLD, modec);
18441 break;
18442
Bram Moolenaar12682fd2010-03-10 13:43:49 +010018443 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444 p = highlight_color(id, what, modec);
18445 break;
18446
18447 case 'i':
18448 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
18449 p = highlight_has_attr(id, HL_INVERSE, modec);
18450 else /* italic */
18451 p = highlight_has_attr(id, HL_ITALIC, modec);
18452 break;
18453
18454 case 'n': /* name */
18455 p = get_highlight_name(NULL, id - 1);
18456 break;
18457
18458 case 'r': /* reverse */
18459 p = highlight_has_attr(id, HL_INVERSE, modec);
18460 break;
18461
Bram Moolenaar6f507d62008-11-28 10:16:05 +000018462 case 's':
18463 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
18464 p = highlight_color(id, what, modec);
18465 else /* standout */
18466 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467 break;
18468
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000018469 case 'u':
18470 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
18471 /* underline */
18472 p = highlight_has_attr(id, HL_UNDERLINE, modec);
18473 else
18474 /* undercurl */
18475 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 break;
18477 }
18478
18479 if (p != NULL)
18480 p = vim_strsave(p);
18481#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018482 rettv->v_type = VAR_STRING;
18483 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484}
18485
18486/*
18487 * "synIDtrans(id)" function
18488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018490f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018491 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493{
18494 int id;
18495
18496#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018497 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498
18499 if (id > 0)
18500 id = syn_get_final_id(id);
18501 else
18502#endif
18503 id = 0;
18504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018505 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506}
18507
18508/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020018509 * "synconcealed(lnum, col)" function
18510 */
18511 static void
18512f_synconcealed(argvars, rettv)
18513 typval_T *argvars UNUSED;
18514 typval_T *rettv;
18515{
18516#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18517 long lnum;
18518 long col;
18519 int syntax_flags = 0;
18520 int cchar;
18521 int matchid = 0;
18522 char_u str[NUMBUFLEN];
18523#endif
18524
18525 rettv->v_type = VAR_LIST;
18526 rettv->vval.v_list = NULL;
18527
18528#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
18529 lnum = get_tv_lnum(argvars); /* -1 on type error */
18530 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18531
18532 vim_memset(str, NUL, sizeof(str));
18533
18534 if (rettv_list_alloc(rettv) != FAIL)
18535 {
18536 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
18537 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
18538 && curwin->w_p_cole > 0)
18539 {
18540 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
18541 syntax_flags = get_syntax_info(&matchid);
18542
18543 /* get the conceal character */
18544 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
18545 {
18546 cchar = syn_get_sub_char();
18547 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
18548 cchar = lcs_conceal;
18549 if (cchar != NUL)
18550 {
18551# ifdef FEAT_MBYTE
18552 if (has_mbyte)
18553 (*mb_char2bytes)(cchar, str);
18554 else
18555# endif
18556 str[0] = cchar;
18557 }
18558 }
18559 }
18560
18561 list_append_number(rettv->vval.v_list,
18562 (syntax_flags & HL_CONCEAL) != 0);
18563 /* -1 to auto-determine strlen */
18564 list_append_string(rettv->vval.v_list, str, -1);
18565 list_append_number(rettv->vval.v_list, matchid);
18566 }
18567#endif
18568}
18569
18570/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018571 * "synstack(lnum, col)" function
18572 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018573 static void
18574f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018575 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018576 typval_T *rettv;
18577{
18578#ifdef FEAT_SYN_HL
18579 long lnum;
18580 long col;
18581 int i;
18582 int id;
18583#endif
18584
18585 rettv->v_type = VAR_LIST;
18586 rettv->vval.v_list = NULL;
18587
18588#ifdef FEAT_SYN_HL
18589 lnum = get_tv_lnum(argvars); /* -1 on type error */
18590 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18591
18592 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018593 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018594 && rettv_list_alloc(rettv) != FAIL)
18595 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018596 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018597 for (i = 0; ; ++i)
18598 {
18599 id = syn_get_stack_item(i);
18600 if (id < 0)
18601 break;
18602 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18603 break;
18604 }
18605 }
18606#endif
18607}
18608
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 static void
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018610get_cmd_output_as_rettv(argvars, rettv, retlist)
Bram Moolenaar33570922005-01-25 22:26:29 +000018611 typval_T *argvars;
18612 typval_T *rettv;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018613 int retlist;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018615 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018617 char_u *infile = NULL;
18618 char_u buf[NUMBUFLEN];
18619 int err = FALSE;
18620 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018621 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020018622 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018624 rettv->v_type = VAR_STRING;
18625 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018626 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018627 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018628
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018629 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018630 {
18631 /*
18632 * Write the string to a temp file, to be used for input of the shell
18633 * command.
18634 */
18635 if ((infile = vim_tempname('i')) == NULL)
18636 {
18637 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018638 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018639 }
18640
18641 fd = mch_fopen((char *)infile, WRITEBIN);
18642 if (fd == NULL)
18643 {
18644 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018645 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018646 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018647 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018648 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018649 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
18650 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018651 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018652 else
18653 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018654 size_t len;
18655
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018656 p = get_tv_string_buf_chk(&argvars[1], buf);
18657 if (p == NULL)
18658 {
18659 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018660 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018661 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020018662 len = STRLEN(p);
18663 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020018664 err = TRUE;
18665 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018666 if (fclose(fd) != 0)
18667 err = TRUE;
18668 if (err)
18669 {
18670 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018671 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018672 }
18673 }
18674
Bram Moolenaar52a72462014-08-29 15:53:52 +020018675 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
18676 * echoes typeahead, that messes up the display. */
18677 if (!msg_silent)
18678 flags += SHELL_COOKED;
18679
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018680 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018682 int len;
18683 listitem_T *li;
18684 char_u *s = NULL;
18685 char_u *start;
18686 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018687 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688
Bram Moolenaar52a72462014-08-29 15:53:52 +020018689 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018690 if (res == NULL)
18691 goto errret;
18692
18693 list = list_alloc();
18694 if (list == NULL)
18695 goto errret;
18696
18697 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018699 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018700 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018701 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018702 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018703
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018704 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018705 if (s == NULL)
18706 goto errret;
18707
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018708 for (p = s; start < end; ++p, ++start)
18709 *p = *start == NUL ? NL : *start;
18710 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018711
18712 li = listitem_alloc();
18713 if (li == NULL)
18714 {
18715 vim_free(s);
18716 goto errret;
18717 }
18718 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010018719 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018720 li->li_tv.vval.v_string = s;
18721 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018723
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020018724 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018725 rettv->v_type = VAR_LIST;
18726 rettv->vval.v_list = list;
18727 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018729 else
18730 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020018731 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018732#ifdef USE_CR
18733 /* translate <CR> into <NL> */
18734 if (res != NULL)
18735 {
18736 char_u *s;
18737
18738 for (s = res; *s; ++s)
18739 {
18740 if (*s == CAR)
18741 *s = NL;
18742 }
18743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744#else
18745# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018746 /* translate <CR><NL> into <NL> */
18747 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018749 char_u *s, *d;
18750
18751 d = res;
18752 for (s = res; *s; ++s)
18753 {
18754 if (s[0] == CAR && s[1] == NL)
18755 ++s;
18756 *d++ = *s;
18757 }
18758 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760# endif
18761#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018762 rettv->vval.v_string = res;
18763 res = NULL;
18764 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018765
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018766errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018767 if (infile != NULL)
18768 {
18769 mch_remove(infile);
18770 vim_free(infile);
18771 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020018772 if (res != NULL)
18773 vim_free(res);
18774 if (list != NULL)
18775 list_free(list, TRUE);
18776}
18777
18778/*
18779 * "system()" function
18780 */
18781 static void
18782f_system(argvars, rettv)
18783 typval_T *argvars;
18784 typval_T *rettv;
18785{
18786 get_cmd_output_as_rettv(argvars, rettv, FALSE);
18787}
18788
18789/*
18790 * "systemlist()" function
18791 */
18792 static void
18793f_systemlist(argvars, rettv)
18794 typval_T *argvars;
18795 typval_T *rettv;
18796{
18797 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798}
18799
18800/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018801 * "tabpagebuflist()" function
18802 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018803 static void
18804f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018805 typval_T *argvars UNUSED;
18806 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018807{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018808#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018809 tabpage_T *tp;
18810 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018811
18812 if (argvars[0].v_type == VAR_UNKNOWN)
18813 wp = firstwin;
18814 else
18815 {
18816 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18817 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018818 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018819 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018820 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018821 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018822 for (; wp != NULL; wp = wp->w_next)
18823 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018824 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018825 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018826 }
18827#endif
18828}
18829
18830
18831/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018832 * "tabpagenr()" function
18833 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018834 static void
18835f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018836 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018837 typval_T *rettv;
18838{
18839 int nr = 1;
18840#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018841 char_u *arg;
18842
18843 if (argvars[0].v_type != VAR_UNKNOWN)
18844 {
18845 arg = get_tv_string_chk(&argvars[0]);
18846 nr = 0;
18847 if (arg != NULL)
18848 {
18849 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018850 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018851 else
18852 EMSG2(_(e_invexpr2), arg);
18853 }
18854 }
18855 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018856 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018857#endif
18858 rettv->vval.v_number = nr;
18859}
18860
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018861
18862#ifdef FEAT_WINDOWS
18863static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18864
18865/*
18866 * Common code for tabpagewinnr() and winnr().
18867 */
18868 static int
18869get_winnr(tp, argvar)
18870 tabpage_T *tp;
18871 typval_T *argvar;
18872{
18873 win_T *twin;
18874 int nr = 1;
18875 win_T *wp;
18876 char_u *arg;
18877
18878 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18879 if (argvar->v_type != VAR_UNKNOWN)
18880 {
18881 arg = get_tv_string_chk(argvar);
18882 if (arg == NULL)
18883 nr = 0; /* type error; errmsg already given */
18884 else if (STRCMP(arg, "$") == 0)
18885 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18886 else if (STRCMP(arg, "#") == 0)
18887 {
18888 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18889 if (twin == NULL)
18890 nr = 0;
18891 }
18892 else
18893 {
18894 EMSG2(_(e_invexpr2), arg);
18895 nr = 0;
18896 }
18897 }
18898
18899 if (nr > 0)
18900 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18901 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018902 {
18903 if (wp == NULL)
18904 {
18905 /* didn't find it in this tabpage */
18906 nr = 0;
18907 break;
18908 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018909 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018910 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018911 return nr;
18912}
18913#endif
18914
18915/*
18916 * "tabpagewinnr()" function
18917 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018918 static void
18919f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018920 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018921 typval_T *rettv;
18922{
18923 int nr = 1;
18924#ifdef FEAT_WINDOWS
18925 tabpage_T *tp;
18926
18927 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18928 if (tp == NULL)
18929 nr = 0;
18930 else
18931 nr = get_winnr(tp, &argvars[1]);
18932#endif
18933 rettv->vval.v_number = nr;
18934}
18935
18936
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018937/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018938 * "tagfiles()" function
18939 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018940 static void
18941f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018942 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018943 typval_T *rettv;
18944{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018945 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018946 tagname_T tn;
18947 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018948
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018949 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018950 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018951 fname = alloc(MAXPATHL);
18952 if (fname == NULL)
18953 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018954
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018955 for (first = TRUE; ; first = FALSE)
18956 if (get_tagfname(&tn, first, fname) == FAIL
18957 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018958 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018959 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018960 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018961}
18962
18963/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018964 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018965 */
18966 static void
18967f_taglist(argvars, rettv)
18968 typval_T *argvars;
18969 typval_T *rettv;
18970{
18971 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018972
18973 tag_pattern = get_tv_string(&argvars[0]);
18974
18975 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018976 if (*tag_pattern == NUL)
18977 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018978
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018979 if (rettv_list_alloc(rettv) == OK)
18980 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018981}
18982
18983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 * "tempname()" function
18985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018987f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018988 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990{
18991 static int x = 'A';
18992
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018993 rettv->v_type = VAR_STRING;
18994 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995
18996 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18997 * names. Skip 'I' and 'O', they are used for shell redirection. */
18998 do
18999 {
19000 if (x == 'Z')
19001 x = '0';
19002 else if (x == '9')
19003 x = 'A';
19004 else
19005 {
19006#ifdef EBCDIC
19007 if (x == 'I')
19008 x = 'J';
19009 else if (x == 'R')
19010 x = 'S';
19011 else
19012#endif
19013 ++x;
19014 }
19015 } while (x == 'I' || x == 'O');
19016}
19017
19018/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000019019 * "test(list)" function: Just checking the walls...
19020 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000019021 static void
19022f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019023 typval_T *argvars UNUSED;
19024 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000019025{
19026 /* Used for unit testing. Change the code below to your liking. */
19027#if 0
19028 listitem_T *li;
19029 list_T *l;
19030 char_u *bad, *good;
19031
19032 if (argvars[0].v_type != VAR_LIST)
19033 return;
19034 l = argvars[0].vval.v_list;
19035 if (l == NULL)
19036 return;
19037 li = l->lv_first;
19038 if (li == NULL)
19039 return;
19040 bad = get_tv_string(&li->li_tv);
19041 li = li->li_next;
19042 if (li == NULL)
19043 return;
19044 good = get_tv_string(&li->li_tv);
19045 rettv->vval.v_number = test_edit_score(bad, good);
19046#endif
19047}
19048
Bram Moolenaardb7c6862010-05-21 16:33:48 +020019049#ifdef FEAT_FLOAT
19050/*
19051 * "tan()" function
19052 */
19053 static void
19054f_tan(argvars, rettv)
19055 typval_T *argvars;
19056 typval_T *rettv;
19057{
19058 float_T f;
19059
19060 rettv->v_type = VAR_FLOAT;
19061 if (get_float_arg(argvars, &f) == OK)
19062 rettv->vval.v_float = tan(f);
19063 else
19064 rettv->vval.v_float = 0.0;
19065}
19066
19067/*
19068 * "tanh()" function
19069 */
19070 static void
19071f_tanh(argvars, rettv)
19072 typval_T *argvars;
19073 typval_T *rettv;
19074{
19075 float_T f;
19076
19077 rettv->v_type = VAR_FLOAT;
19078 if (get_float_arg(argvars, &f) == OK)
19079 rettv->vval.v_float = tanh(f);
19080 else
19081 rettv->vval.v_float = 0.0;
19082}
19083#endif
19084
Bram Moolenaard52d9742005-08-21 22:20:28 +000019085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086 * "tolower(string)" function
19087 */
19088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019089f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019090 typval_T *argvars;
19091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092{
19093 char_u *p;
19094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019095 p = vim_strsave(get_tv_string(&argvars[0]));
19096 rettv->v_type = VAR_STRING;
19097 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019098
19099 if (p != NULL)
19100 while (*p != NUL)
19101 {
19102#ifdef FEAT_MBYTE
19103 int l;
19104
19105 if (enc_utf8)
19106 {
19107 int c, lc;
19108
19109 c = utf_ptr2char(p);
19110 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019111 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 /* TODO: reallocate string when byte count changes. */
19113 if (utf_char2len(lc) == l)
19114 utf_char2bytes(lc, p);
19115 p += l;
19116 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019117 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 p += l; /* skip multi-byte character */
19119 else
19120#endif
19121 {
19122 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
19123 ++p;
19124 }
19125 }
19126}
19127
19128/*
19129 * "toupper(string)" function
19130 */
19131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019132f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019133 typval_T *argvars;
19134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019137 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138}
19139
19140/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000019141 * "tr(string, fromstr, tostr)" function
19142 */
19143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019144f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019145 typval_T *argvars;
19146 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019147{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019148 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019149 char_u *fromstr;
19150 char_u *tostr;
19151 char_u *p;
19152#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000019153 int inlen;
19154 int fromlen;
19155 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019156 int idx;
19157 char_u *cpstr;
19158 int cplen;
19159 int first = TRUE;
19160#endif
19161 char_u buf[NUMBUFLEN];
19162 char_u buf2[NUMBUFLEN];
19163 garray_T ga;
19164
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019165 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019166 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
19167 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019168
19169 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170 rettv->v_type = VAR_STRING;
19171 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019172 if (fromstr == NULL || tostr == NULL)
19173 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019174 ga_init2(&ga, (int)sizeof(char), 80);
19175
19176#ifdef FEAT_MBYTE
19177 if (!has_mbyte)
19178#endif
19179 /* not multi-byte: fromstr and tostr must be the same length */
19180 if (STRLEN(fromstr) != STRLEN(tostr))
19181 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019182#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000019183error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000019184#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000019185 EMSG2(_(e_invarg2), fromstr);
19186 ga_clear(&ga);
19187 return;
19188 }
19189
19190 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019191 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019192 {
19193#ifdef FEAT_MBYTE
19194 if (has_mbyte)
19195 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019196 inlen = (*mb_ptr2len)(in_str);
19197 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019198 cplen = inlen;
19199 idx = 0;
19200 for (p = fromstr; *p != NUL; p += fromlen)
19201 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019202 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019203 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019204 {
19205 for (p = tostr; *p != NUL; p += tolen)
19206 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019207 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019208 if (idx-- == 0)
19209 {
19210 cplen = tolen;
19211 cpstr = p;
19212 break;
19213 }
19214 }
19215 if (*p == NUL) /* tostr is shorter than fromstr */
19216 goto error;
19217 break;
19218 }
19219 ++idx;
19220 }
19221
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019222 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000019223 {
19224 /* Check that fromstr and tostr have the same number of
19225 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019226 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000019227 first = FALSE;
19228 for (p = tostr; *p != NUL; p += tolen)
19229 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019230 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019231 --idx;
19232 }
19233 if (idx != 0)
19234 goto error;
19235 }
19236
19237 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000019238 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019239 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019240
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019241 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019242 }
19243 else
19244#endif
19245 {
19246 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019247 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000019248 if (p != NULL)
19249 ga_append(&ga, tostr[p - fromstr]);
19250 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010019251 ga_append(&ga, *in_str);
19252 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019253 }
19254 }
19255
Bram Moolenaar61b974b2006-12-05 09:32:29 +000019256 /* add a terminating NUL */
19257 ga_grow(&ga, 1);
19258 ga_append(&ga, NUL);
19259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000019261}
19262
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019263#ifdef FEAT_FLOAT
19264/*
19265 * "trunc({float})" function
19266 */
19267 static void
19268f_trunc(argvars, rettv)
19269 typval_T *argvars;
19270 typval_T *rettv;
19271{
19272 float_T f;
19273
19274 rettv->v_type = VAR_FLOAT;
19275 if (get_float_arg(argvars, &f) == OK)
19276 /* trunc() is not in C90, use floor() or ceil() instead. */
19277 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
19278 else
19279 rettv->vval.v_float = 0.0;
19280}
19281#endif
19282
Bram Moolenaar8299df92004-07-10 09:47:34 +000019283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284 * "type(expr)" function
19285 */
19286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019287f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019288 typval_T *argvars;
19289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019291 int n;
19292
19293 switch (argvars[0].v_type)
19294 {
19295 case VAR_NUMBER: n = 0; break;
19296 case VAR_STRING: n = 1; break;
19297 case VAR_FUNC: n = 2; break;
19298 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019299 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019300#ifdef FEAT_FLOAT
19301 case VAR_FLOAT: n = 5; break;
19302#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000019303 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
19304 }
19305 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306}
19307
19308/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019309 * "undofile(name)" function
19310 */
19311 static void
19312f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010019313 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019314 typval_T *rettv;
19315{
19316 rettv->v_type = VAR_STRING;
19317#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019318 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019319 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019320
Bram Moolenaarb41d9682012-04-30 17:35:48 +020019321 if (*fname == NUL)
19322 {
19323 /* If there is no file name there will be no undo file. */
19324 rettv->vval.v_string = NULL;
19325 }
19326 else
19327 {
19328 char_u *ffname = FullName_save(fname, FALSE);
19329
19330 if (ffname != NULL)
19331 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
19332 vim_free(ffname);
19333 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020019334 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020019335#else
19336 rettv->vval.v_string = NULL;
19337#endif
19338}
19339
19340/*
Bram Moolenaara800b422010-06-27 01:15:55 +020019341 * "undotree()" function
19342 */
19343 static void
19344f_undotree(argvars, rettv)
19345 typval_T *argvars UNUSED;
19346 typval_T *rettv;
19347{
19348 if (rettv_dict_alloc(rettv) == OK)
19349 {
19350 dict_T *dict = rettv->vval.v_dict;
19351 list_T *list;
19352
Bram Moolenaar730cde92010-06-27 05:18:54 +020019353 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019354 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019355 dict_add_nr_str(dict, "save_last",
19356 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019357 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
19358 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020019359 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020019360
19361 list = list_alloc();
19362 if (list != NULL)
19363 {
19364 u_eval_tree(curbuf->b_u_oldhead, list);
19365 dict_add_list(dict, "entries", list);
19366 }
19367 }
19368}
19369
19370/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000019371 * "values(dict)" function
19372 */
19373 static void
19374f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019375 typval_T *argvars;
19376 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019377{
19378 dict_list(argvars, rettv, 1);
19379}
19380
19381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 * "virtcol(string)" function
19383 */
19384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019385f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019386 typval_T *argvars;
19387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388{
19389 colnr_T vcol = 0;
19390 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019391 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019393 fp = var2fpos(&argvars[0], FALSE, &fnum);
19394 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
19395 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019396 {
19397 getvvcol(curwin, fp, NULL, NULL, &vcol);
19398 ++vcol;
19399 }
19400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019401 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402}
19403
19404/*
19405 * "visualmode()" function
19406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019408f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019409 typval_T *argvars UNUSED;
19410 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 char_u str[2];
19413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019414 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415 str[0] = curbuf->b_visual_mode_eval;
19416 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019417 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019418
19419 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000019420 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019422}
19423
19424/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010019425 * "wildmenumode()" function
19426 */
19427 static void
19428f_wildmenumode(argvars, rettv)
19429 typval_T *argvars UNUSED;
19430 typval_T *rettv UNUSED;
19431{
19432#ifdef FEAT_WILDMENU
19433 if (wild_menu_showing)
19434 rettv->vval.v_number = 1;
19435#endif
19436}
19437
19438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 * "winbufnr(nr)" function
19440 */
19441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019442f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019443 typval_T *argvars;
19444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445{
19446 win_T *wp;
19447
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019448 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019450 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019452 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453}
19454
19455/*
19456 * "wincol()" function
19457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019459f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019460 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462{
19463 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019464 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465}
19466
19467/*
19468 * "winheight(nr)" function
19469 */
19470 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019471f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019472 typval_T *argvars;
19473 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474{
19475 win_T *wp;
19476
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019477 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019479 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019481 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482}
19483
19484/*
19485 * "winline()" function
19486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019488f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019489 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491{
19492 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019493 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494}
19495
19496/*
19497 * "winnr()" function
19498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019500f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019501 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503{
19504 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019505
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019507 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019509 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510}
19511
19512/*
19513 * "winrestcmd()" function
19514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019516f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019517 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519{
19520#ifdef FEAT_WINDOWS
19521 win_T *wp;
19522 int winnr = 1;
19523 garray_T ga;
19524 char_u buf[50];
19525
19526 ga_init2(&ga, (int)sizeof(char), 70);
19527 for (wp = firstwin; wp != NULL; wp = wp->w_next)
19528 {
19529 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
19530 ga_concat(&ga, buf);
19531# ifdef FEAT_VERTSPLIT
19532 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
19533 ga_concat(&ga, buf);
19534# endif
19535 ++winnr;
19536 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000019537 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019543 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544}
19545
19546/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019547 * "winrestview()" function
19548 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019549 static void
19550f_winrestview(argvars, rettv)
19551 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019552 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019553{
19554 dict_T *dict;
19555
19556 if (argvars[0].v_type != VAR_DICT
19557 || (dict = argvars[0].vval.v_dict) == NULL)
19558 EMSG(_(e_invarg));
19559 else
19560 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020019561 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
19562 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
19563 if (dict_find(dict, (char_u *)"col", -1) != NULL)
19564 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019565#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020019566 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
19567 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019568#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019569 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
19570 {
19571 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
19572 curwin->w_set_curswant = FALSE;
19573 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019574
Bram Moolenaar82c25852014-05-28 16:47:16 +020019575 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
19576 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019577#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020019578 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
19579 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019580#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020019581 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
19582 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
19583 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
19584 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019585
19586 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020019587 win_new_height(curwin, curwin->w_height);
19588# ifdef FEAT_VERTSPLIT
19589 win_new_width(curwin, W_WIDTH(curwin));
19590# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020019591 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019592
Bram Moolenaarb851a962014-10-31 15:45:52 +010019593 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019594 curwin->w_topline = 1;
19595 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
19596 curwin->w_topline = curbuf->b_ml.ml_line_count;
19597#ifdef FEAT_DIFF
19598 check_topfill(curwin, TRUE);
19599#endif
19600 }
19601}
19602
19603/*
19604 * "winsaveview()" function
19605 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019606 static void
19607f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000019608 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019609 typval_T *rettv;
19610{
19611 dict_T *dict;
19612
Bram Moolenaara800b422010-06-27 01:15:55 +020019613 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019614 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020019615 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019616
19617 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
19618 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
19619#ifdef FEAT_VIRTUALEDIT
19620 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
19621#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000019622 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000019623 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
19624
19625 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
19626#ifdef FEAT_DIFF
19627 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
19628#endif
19629 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
19630 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
19631}
19632
19633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 * "winwidth(nr)" function
19635 */
19636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019637f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000019638 typval_T *argvars;
19639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640{
19641 win_T *wp;
19642
Bram Moolenaar99ebf042006-04-15 20:28:54 +000019643 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019645 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 else
19647#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019650 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651#endif
19652}
19653
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019655 * Write list of strings to file
19656 */
19657 static int
19658write_list(fd, list, binary)
19659 FILE *fd;
19660 list_T *list;
19661 int binary;
19662{
19663 listitem_T *li;
19664 int c;
19665 int ret = OK;
19666 char_u *s;
19667
19668 for (li = list->lv_first; li != NULL; li = li->li_next)
19669 {
19670 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19671 {
19672 if (*s == '\n')
19673 c = putc(NUL, fd);
19674 else
19675 c = putc(*s, fd);
19676 if (c == EOF)
19677 {
19678 ret = FAIL;
19679 break;
19680 }
19681 }
19682 if (!binary || li->li_next != NULL)
19683 if (putc('\n', fd) == EOF)
19684 {
19685 ret = FAIL;
19686 break;
19687 }
19688 if (ret == FAIL)
19689 {
19690 EMSG(_(e_write));
19691 break;
19692 }
19693 }
19694 return ret;
19695}
19696
19697/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019698 * "writefile()" function
19699 */
19700 static void
19701f_writefile(argvars, rettv)
19702 typval_T *argvars;
19703 typval_T *rettv;
19704{
19705 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019706 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019707 char_u *fname;
19708 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019709 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019710
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019711 if (check_restricted() || check_secure())
19712 return;
19713
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019714 if (argvars[0].v_type != VAR_LIST)
19715 {
19716 EMSG2(_(e_listarg), "writefile()");
19717 return;
19718 }
19719 if (argvars[0].vval.v_list == NULL)
19720 return;
19721
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019722 if (argvars[2].v_type != VAR_UNKNOWN)
19723 {
19724 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
19725 binary = TRUE;
19726 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
19727 append = TRUE;
19728 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019729
19730 /* Always open the file in binary mode, library functions have a mind of
19731 * their own about CR-LF conversion. */
19732 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010019733 if (*fname == NUL || (fd = mch_fopen((char *)fname,
19734 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019735 {
19736 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19737 ret = -1;
19738 }
19739 else
19740 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019741 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
19742 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019743 fclose(fd);
19744 }
19745
19746 rettv->vval.v_number = ret;
19747}
19748
19749/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019750 * "xor(expr, expr)" function
19751 */
19752 static void
19753f_xor(argvars, rettv)
19754 typval_T *argvars;
19755 typval_T *rettv;
19756{
19757 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19758 ^ get_tv_number_chk(&argvars[1], NULL);
19759}
19760
19761
19762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019764 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765 */
19766 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019767var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019768 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019769 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019770 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019772 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019774 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019775
Bram Moolenaara5525202006-03-02 22:52:09 +000019776 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019777 if (varp->v_type == VAR_LIST)
19778 {
19779 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019780 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019781 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019782 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019783
19784 l = varp->vval.v_list;
19785 if (l == NULL)
19786 return NULL;
19787
19788 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019789 pos.lnum = list_find_nr(l, 0L, &error);
19790 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019791 return NULL; /* invalid line number */
19792
19793 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019794 pos.col = list_find_nr(l, 1L, &error);
19795 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019796 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019797 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019798
19799 /* We accept "$" for the column number: last column. */
19800 li = list_find(l, 1L);
19801 if (li != NULL && li->li_tv.v_type == VAR_STRING
19802 && li->li_tv.vval.v_string != NULL
19803 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19804 pos.col = len + 1;
19805
Bram Moolenaara5525202006-03-02 22:52:09 +000019806 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019807 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019808 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019809 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019810
Bram Moolenaara5525202006-03-02 22:52:09 +000019811#ifdef FEAT_VIRTUALEDIT
19812 /* Get the virtual offset. Defaults to zero. */
19813 pos.coladd = list_find_nr(l, 2L, &error);
19814 if (error)
19815 pos.coladd = 0;
19816#endif
19817
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019818 return &pos;
19819 }
19820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019821 name = get_tv_string_chk(varp);
19822 if (name == NULL)
19823 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019824 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019826 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19827 {
19828 if (VIsual_active)
19829 return &VIsual;
19830 return &curwin->w_cursor;
19831 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019832 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019834 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19836 return NULL;
19837 return pp;
19838 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019839
19840#ifdef FEAT_VIRTUALEDIT
19841 pos.coladd = 0;
19842#endif
19843
Bram Moolenaar477933c2007-07-17 14:32:23 +000019844 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019845 {
19846 pos.col = 0;
19847 if (name[1] == '0') /* "w0": first visible line */
19848 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019849 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019850 pos.lnum = curwin->w_topline;
19851 return &pos;
19852 }
19853 else if (name[1] == '$') /* "w$": last visible line */
19854 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019855 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019856 pos.lnum = curwin->w_botline - 1;
19857 return &pos;
19858 }
19859 }
19860 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019862 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 {
19864 pos.lnum = curbuf->b_ml.ml_line_count;
19865 pos.col = 0;
19866 }
19867 else
19868 {
19869 pos.lnum = curwin->w_cursor.lnum;
19870 pos.col = (colnr_T)STRLEN(ml_get_curline());
19871 }
19872 return &pos;
19873 }
19874 return NULL;
19875}
19876
19877/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019878 * Convert list in "arg" into a position and optional file number.
19879 * When "fnump" is NULL there is no file number, only 3 items.
19880 * Note that the column is passed on as-is, the caller may want to decrement
19881 * it to use 1 for the first column.
19882 * Return FAIL when conversion is not possible, doesn't check the position for
19883 * validity.
19884 */
19885 static int
Bram Moolenaar493c1782014-05-28 14:34:46 +020019886list2fpos(arg, posp, fnump, curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019887 typval_T *arg;
19888 pos_T *posp;
19889 int *fnump;
Bram Moolenaar493c1782014-05-28 14:34:46 +020019890 colnr_T *curswantp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019891{
19892 list_T *l = arg->vval.v_list;
19893 long i = 0;
19894 long n;
19895
Bram Moolenaar493c1782014-05-28 14:34:46 +020019896 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
19897 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000019898 if (arg->v_type != VAR_LIST
19899 || l == NULL
19900 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020019901 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019902 return FAIL;
19903
19904 if (fnump != NULL)
19905 {
19906 n = list_find_nr(l, i++, NULL); /* fnum */
19907 if (n < 0)
19908 return FAIL;
19909 if (n == 0)
19910 n = curbuf->b_fnum; /* current buffer */
19911 *fnump = n;
19912 }
19913
19914 n = list_find_nr(l, i++, NULL); /* lnum */
19915 if (n < 0)
19916 return FAIL;
19917 posp->lnum = n;
19918
19919 n = list_find_nr(l, i++, NULL); /* col */
19920 if (n < 0)
19921 return FAIL;
19922 posp->col = n;
19923
19924#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020019925 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019926 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019927 posp->coladd = 0;
19928 else
19929 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019930#endif
19931
Bram Moolenaar493c1782014-05-28 14:34:46 +020019932 if (curswantp != NULL)
19933 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
19934
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019935 return OK;
19936}
19937
19938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939 * Get the length of an environment variable name.
19940 * Advance "arg" to the first character after the name.
19941 * Return 0 for error.
19942 */
19943 static int
19944get_env_len(arg)
19945 char_u **arg;
19946{
19947 char_u *p;
19948 int len;
19949
19950 for (p = *arg; vim_isIDc(*p); ++p)
19951 ;
19952 if (p == *arg) /* no name found */
19953 return 0;
19954
19955 len = (int)(p - *arg);
19956 *arg = p;
19957 return len;
19958}
19959
19960/*
19961 * Get the length of the name of a function or internal variable.
19962 * "arg" is advanced to the first non-white character after the name.
19963 * Return 0 if something is wrong.
19964 */
19965 static int
19966get_id_len(arg)
19967 char_u **arg;
19968{
19969 char_u *p;
19970 int len;
19971
19972 /* Find the end of the name. */
19973 for (p = *arg; eval_isnamec(*p); ++p)
19974 ;
19975 if (p == *arg) /* no name found */
19976 return 0;
19977
19978 len = (int)(p - *arg);
19979 *arg = skipwhite(p);
19980
19981 return len;
19982}
19983
19984/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019985 * Get the length of the name of a variable or function.
19986 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019988 * Return -1 if curly braces expansion failed.
19989 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 * If the name contains 'magic' {}'s, expand them and return the
19991 * expanded name in an allocated string via 'alias' - caller must free.
19992 */
19993 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019994get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995 char_u **arg;
19996 char_u **alias;
19997 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019998 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999{
20000 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 char_u *p;
20002 char_u *expr_start;
20003 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004
20005 *alias = NULL; /* default to no alias */
20006
20007 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
20008 && (*arg)[2] == (int)KE_SNR)
20009 {
20010 /* hard coded <SNR>, already translated */
20011 *arg += 3;
20012 return get_id_len(arg) + 3;
20013 }
20014 len = eval_fname_script(*arg);
20015 if (len > 0)
20016 {
20017 /* literal "<SID>", "s:" or "<SNR>" */
20018 *arg += len;
20019 }
20020
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020022 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020024 p = find_name_end(*arg, &expr_start, &expr_end,
20025 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020026 if (expr_start != NULL)
20027 {
20028 char_u *temp_string;
20029
20030 if (!evaluate)
20031 {
20032 len += (int)(p - *arg);
20033 *arg = skipwhite(p);
20034 return len;
20035 }
20036
20037 /*
20038 * Include any <SID> etc in the expanded string:
20039 * Thus the -len here.
20040 */
20041 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
20042 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020043 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 *alias = temp_string;
20045 *arg = skipwhite(p);
20046 return (int)STRLEN(temp_string);
20047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048
20049 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020050 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 EMSG2(_(e_invexpr2), *arg);
20052
20053 return len;
20054}
20055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020056/*
20057 * Find the end of a variable or function name, taking care of magic braces.
20058 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
20059 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020060 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061 * Return a pointer to just after the name. Equal to "arg" if there is no
20062 * valid name.
20063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020065find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 char_u *arg;
20067 char_u **expr_start;
20068 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020069 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020071 int mb_nest = 0;
20072 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 char_u *p;
20074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020077 *expr_start = NULL;
20078 *expr_end = NULL;
20079 }
20080
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020081 /* Quick check for valid starting character. */
20082 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
20083 return arg;
20084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020085 for (p = arg; *p != NUL
20086 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020087 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020088 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020089 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000020090 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020091 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000020092 if (*p == '\'')
20093 {
20094 /* skip over 'string' to avoid counting [ and ] inside it. */
20095 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
20096 ;
20097 if (*p == NUL)
20098 break;
20099 }
20100 else if (*p == '"')
20101 {
20102 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
20103 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
20104 if (*p == '\\' && p[1] != NUL)
20105 ++p;
20106 if (*p == NUL)
20107 break;
20108 }
20109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020110 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112 if (*p == '[')
20113 ++br_nest;
20114 else if (*p == ']')
20115 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020116 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000020117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020118 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020120 if (*p == '{')
20121 {
20122 mb_nest++;
20123 if (expr_start != NULL && *expr_start == NULL)
20124 *expr_start = p;
20125 }
20126 else if (*p == '}')
20127 {
20128 mb_nest--;
20129 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
20130 *expr_end = p;
20131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 }
20134
20135 return p;
20136}
20137
20138/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020139 * Expands out the 'magic' {}'s in a variable/function name.
20140 * Note that this can call itself recursively, to deal with
20141 * constructs like foo{bar}{baz}{bam}
20142 * The four pointer arguments point to "foo{expre}ss{ion}bar"
20143 * "in_start" ^
20144 * "expr_start" ^
20145 * "expr_end" ^
20146 * "in_end" ^
20147 *
20148 * Returns a new allocated string, which the caller must free.
20149 * Returns NULL for failure.
20150 */
20151 static char_u *
20152make_expanded_name(in_start, expr_start, expr_end, in_end)
20153 char_u *in_start;
20154 char_u *expr_start;
20155 char_u *expr_end;
20156 char_u *in_end;
20157{
20158 char_u c1;
20159 char_u *retval = NULL;
20160 char_u *temp_result;
20161 char_u *nextcmd = NULL;
20162
20163 if (expr_end == NULL || in_end == NULL)
20164 return NULL;
20165 *expr_start = NUL;
20166 *expr_end = NUL;
20167 c1 = *in_end;
20168 *in_end = NUL;
20169
Bram Moolenaar362e1a32006-03-06 23:29:24 +000020170 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020171 if (temp_result != NULL && nextcmd == NULL)
20172 {
20173 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
20174 + (in_end - expr_end) + 1));
20175 if (retval != NULL)
20176 {
20177 STRCPY(retval, in_start);
20178 STRCAT(retval, temp_result);
20179 STRCAT(retval, expr_end + 1);
20180 }
20181 }
20182 vim_free(temp_result);
20183
20184 *in_end = c1; /* put char back for error messages */
20185 *expr_start = '{';
20186 *expr_end = '}';
20187
20188 if (retval != NULL)
20189 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020190 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020191 if (expr_start != NULL)
20192 {
20193 /* Further expansion! */
20194 temp_result = make_expanded_name(retval, expr_start,
20195 expr_end, temp_result);
20196 vim_free(retval);
20197 retval = temp_result;
20198 }
20199 }
20200
20201 return retval;
20202}
20203
20204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020206 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 */
20208 static int
20209eval_isnamec(c)
20210 int c;
20211{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020212 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
20213}
20214
20215/*
20216 * Return TRUE if character "c" can be used as the first character in a
20217 * variable or function name (excluding '{' and '}').
20218 */
20219 static int
20220eval_isnamec1(c)
20221 int c;
20222{
20223 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224}
20225
20226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 * Set number v: variable to "val".
20228 */
20229 void
20230set_vim_var_nr(idx, val)
20231 int idx;
20232 long val;
20233{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020234 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235}
20236
20237/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020238 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 */
20240 long
20241get_vim_var_nr(idx)
20242 int idx;
20243{
Bram Moolenaare9a41262005-01-15 22:18:47 +000020244 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245}
20246
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020247/*
20248 * Get string v: variable value. Uses a static buffer, can only be used once.
20249 */
20250 char_u *
20251get_vim_var_str(idx)
20252 int idx;
20253{
20254 return get_tv_string(&vimvars[idx].vv_tv);
20255}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020256
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020258 * Get List v: variable value. Caller must take care of reference count when
20259 * needed.
20260 */
20261 list_T *
20262get_vim_var_list(idx)
20263 int idx;
20264{
20265 return vimvars[idx].vv_list;
20266}
20267
20268/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020269 * Set v:char to character "c".
20270 */
20271 void
20272set_vim_var_char(c)
20273 int c;
20274{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020020275 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000020276
20277#ifdef FEAT_MBYTE
20278 if (has_mbyte)
20279 buf[(*mb_char2bytes)(c, buf)] = NUL;
20280 else
20281#endif
20282 {
20283 buf[0] = c;
20284 buf[1] = NUL;
20285 }
20286 set_vim_var_string(VV_CHAR, buf, -1);
20287}
20288
20289/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020290 * Set v:count to "count" and v:count1 to "count1".
20291 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292 */
20293 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020294set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 long count;
20296 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020297 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000020299 if (set_prevcount)
20300 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020301 vimvars[VV_COUNT].vv_nr = count;
20302 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303}
20304
20305/*
20306 * Set string v: variable to a copy of "val".
20307 */
20308 void
20309set_vim_var_string(idx, val, len)
20310 int idx;
20311 char_u *val;
20312 int len; /* length of "val" to use or -1 (whole string) */
20313{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020314 /* Need to do this (at least) once, since we can't initialize a union.
20315 * Will always be invoked when "v:progname" is set. */
20316 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
20317
Bram Moolenaare9a41262005-01-15 22:18:47 +000020318 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020319 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020320 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020322 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000020324 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325}
20326
20327/*
Bram Moolenaard812df62008-11-09 12:46:09 +000020328 * Set List v: variable to "val".
20329 */
20330 void
20331set_vim_var_list(idx, val)
20332 int idx;
20333 list_T *val;
20334{
20335 list_unref(vimvars[idx].vv_list);
20336 vimvars[idx].vv_list = val;
20337 if (val != NULL)
20338 ++val->lv_refcount;
20339}
20340
20341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 * Set v:register if needed.
20343 */
20344 void
20345set_reg_var(c)
20346 int c;
20347{
20348 char_u regname;
20349
20350 if (c == 0 || c == ' ')
20351 regname = '"';
20352 else
20353 regname = c;
20354 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000020355 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 set_vim_var_string(VV_REG, &regname, 1);
20357}
20358
20359/*
20360 * Get or set v:exception. If "oldval" == NULL, return the current value.
20361 * Otherwise, restore the value to "oldval" and return NULL.
20362 * Must always be called in pairs to save and restore v:exception! Does not
20363 * take care of memory allocations.
20364 */
20365 char_u *
20366v_exception(oldval)
20367 char_u *oldval;
20368{
20369 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020370 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371
Bram Moolenaare9a41262005-01-15 22:18:47 +000020372 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020373 return NULL;
20374}
20375
20376/*
20377 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
20378 * Otherwise, restore the value to "oldval" and return NULL.
20379 * Must always be called in pairs to save and restore v:throwpoint! Does not
20380 * take care of memory allocations.
20381 */
20382 char_u *
20383v_throwpoint(oldval)
20384 char_u *oldval;
20385{
20386 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020387 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388
Bram Moolenaare9a41262005-01-15 22:18:47 +000020389 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 return NULL;
20391}
20392
20393#if defined(FEAT_AUTOCMD) || defined(PROTO)
20394/*
20395 * Set v:cmdarg.
20396 * If "eap" != NULL, use "eap" to generate the value and return the old value.
20397 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
20398 * Must always be called in pairs!
20399 */
20400 char_u *
20401set_cmdarg(eap, oldarg)
20402 exarg_T *eap;
20403 char_u *oldarg;
20404{
20405 char_u *oldval;
20406 char_u *newval;
20407 unsigned len;
20408
Bram Moolenaare9a41262005-01-15 22:18:47 +000020409 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020410 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020412 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000020413 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020414 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020415 }
20416
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020417 if (eap->force_bin == FORCE_BIN)
20418 len = 6;
20419 else if (eap->force_bin == FORCE_NOBIN)
20420 len = 8;
20421 else
20422 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020423
20424 if (eap->read_edit)
20425 len += 7;
20426
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020427 if (eap->force_ff != 0)
20428 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
20429# ifdef FEAT_MBYTE
20430 if (eap->force_enc != 0)
20431 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020432 if (eap->bad_char != 0)
20433 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020434# endif
20435
20436 newval = alloc(len + 1);
20437 if (newval == NULL)
20438 return NULL;
20439
20440 if (eap->force_bin == FORCE_BIN)
20441 sprintf((char *)newval, " ++bin");
20442 else if (eap->force_bin == FORCE_NOBIN)
20443 sprintf((char *)newval, " ++nobin");
20444 else
20445 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020446
20447 if (eap->read_edit)
20448 STRCAT(newval, " ++edit");
20449
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020450 if (eap->force_ff != 0)
20451 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
20452 eap->cmd + eap->force_ff);
20453# ifdef FEAT_MBYTE
20454 if (eap->force_enc != 0)
20455 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
20456 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020020457 if (eap->bad_char == BAD_KEEP)
20458 STRCPY(newval + STRLEN(newval), " ++bad=keep");
20459 else if (eap->bad_char == BAD_DROP)
20460 STRCPY(newval + STRLEN(newval), " ++bad=drop");
20461 else if (eap->bad_char != 0)
20462 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020463# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020464 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000020465 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466}
20467#endif
20468
20469/*
20470 * Get the value of internal variable "name".
20471 * Return OK or FAIL.
20472 */
20473 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020474get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 char_u *name;
20476 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000020477 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020478 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020479 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020480{
20481 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 typval_T *tv = NULL;
20483 typval_T atv;
20484 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020486
20487 /* truncate the name, so that we can use strcmp() */
20488 cc = name[len];
20489 name[len] = NUL;
20490
20491 /*
20492 * Check for "b:changedtick".
20493 */
20494 if (STRCMP(name, "b:changedtick") == 0)
20495 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000020496 atv.v_type = VAR_NUMBER;
20497 atv.vval.v_number = curbuf->b_changedtick;
20498 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 }
20500
20501 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 * Check for user-defined variables.
20503 */
20504 else
20505 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020506 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020508 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509 }
20510
Bram Moolenaare9a41262005-01-15 22:18:47 +000020511 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020512 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020513 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020514 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 ret = FAIL;
20516 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020517 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020518 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519
20520 name[len] = cc;
20521
20522 return ret;
20523}
20524
20525/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020526 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
20527 * Also handle function call with Funcref variable: func(expr)
20528 * Can all be combined: dict.func(expr)[idx]['func'](expr)
20529 */
20530 static int
20531handle_subscript(arg, rettv, evaluate, verbose)
20532 char_u **arg;
20533 typval_T *rettv;
20534 int evaluate; /* do more than finding the end */
20535 int verbose; /* give error messages */
20536{
20537 int ret = OK;
20538 dict_T *selfdict = NULL;
20539 char_u *s;
20540 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000020541 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020542
20543 while (ret == OK
20544 && (**arg == '['
20545 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020546 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020547 && !vim_iswhite(*(*arg - 1)))
20548 {
20549 if (**arg == '(')
20550 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000020551 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020552 if (evaluate)
20553 {
20554 functv = *rettv;
20555 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020556
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020557 /* Invoke the function. Recursive! */
20558 s = functv.vval.v_string;
20559 }
20560 else
20561 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020562 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000020563 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
20564 &len, evaluate, selfdict);
20565
20566 /* Clear the funcref afterwards, so that deleting it while
20567 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010020568 if (evaluate)
20569 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020570
20571 /* Stop the expression evaluation when immediately aborting on
20572 * error, or when an interrupt occurred or an exception was thrown
20573 * but not caught. */
20574 if (aborting())
20575 {
20576 if (ret == OK)
20577 clear_tv(rettv);
20578 ret = FAIL;
20579 }
20580 dict_unref(selfdict);
20581 selfdict = NULL;
20582 }
20583 else /* **arg == '[' || **arg == '.' */
20584 {
20585 dict_unref(selfdict);
20586 if (rettv->v_type == VAR_DICT)
20587 {
20588 selfdict = rettv->vval.v_dict;
20589 if (selfdict != NULL)
20590 ++selfdict->dv_refcount;
20591 }
20592 else
20593 selfdict = NULL;
20594 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
20595 {
20596 clear_tv(rettv);
20597 ret = FAIL;
20598 }
20599 }
20600 }
20601 dict_unref(selfdict);
20602 return ret;
20603}
20604
20605/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020606 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020607 * value).
20608 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020609 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020610alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020611{
Bram Moolenaar33570922005-01-25 22:26:29 +000020612 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020613}
20614
20615/*
20616 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020617 * The string "s" must have been allocated, it is consumed.
20618 * Return NULL for out of memory, the variable otherwise.
20619 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020620 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020621alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622 char_u *s;
20623{
Bram Moolenaar33570922005-01-25 22:26:29 +000020624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020626 rettv = alloc_tv();
20627 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020629 rettv->v_type = VAR_STRING;
20630 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 }
20632 else
20633 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635}
20636
20637/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020638 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000020640 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020641free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020642 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643{
20644 if (varp != NULL)
20645 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020646 switch (varp->v_type)
20647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020648 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020649 func_unref(varp->vval.v_string);
20650 /*FALLTHROUGH*/
20651 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020652 vim_free(varp->vval.v_string);
20653 break;
20654 case VAR_LIST:
20655 list_unref(varp->vval.v_list);
20656 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020657 case VAR_DICT:
20658 dict_unref(varp->vval.v_dict);
20659 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020660 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020661#ifdef FEAT_FLOAT
20662 case VAR_FLOAT:
20663#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000020664 case VAR_UNKNOWN:
20665 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020666 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000020667 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020668 break;
20669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670 vim_free(varp);
20671 }
20672}
20673
20674/*
20675 * Free the memory for a variable value and set the value to NULL or 0.
20676 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020677 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020678clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020679 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680{
20681 if (varp != NULL)
20682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020683 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020685 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020686 func_unref(varp->vval.v_string);
20687 /*FALLTHROUGH*/
20688 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020689 vim_free(varp->vval.v_string);
20690 varp->vval.v_string = NULL;
20691 break;
20692 case VAR_LIST:
20693 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020694 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020695 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020696 case VAR_DICT:
20697 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020698 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020699 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020700 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020701 varp->vval.v_number = 0;
20702 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020703#ifdef FEAT_FLOAT
20704 case VAR_FLOAT:
20705 varp->vval.v_float = 0.0;
20706 break;
20707#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020708 case VAR_UNKNOWN:
20709 break;
20710 default:
20711 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020713 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 }
20715}
20716
20717/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020718 * Set the value of a variable to NULL without freeing items.
20719 */
20720 static void
20721init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020722 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020723{
20724 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020725 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020726}
20727
20728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 * Get the number value of a variable.
20730 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020731 * For incompatible types, return 0.
20732 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20733 * caller of incompatible types: it sets *denote to TRUE if "denote"
20734 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 */
20736 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020737get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020738 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020740 int error = FALSE;
20741
20742 return get_tv_number_chk(varp, &error); /* return 0L on error */
20743}
20744
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020745 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020746get_tv_number_chk(varp, denote)
20747 typval_T *varp;
20748 int *denote;
20749{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020750 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020752 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020754 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020755 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020756#ifdef FEAT_FLOAT
20757 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020758 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020759 break;
20760#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020761 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020762 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020763 break;
20764 case VAR_STRING:
20765 if (varp->vval.v_string != NULL)
20766 vim_str2nr(varp->vval.v_string, NULL, NULL,
20767 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020768 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020769 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020770 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020771 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020772 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020773 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020774 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020775 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020776 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020777 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020778 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020779 if (denote == NULL) /* useful for values that must be unsigned */
20780 n = -1;
20781 else
20782 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020783 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784}
20785
20786/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020787 * Get the lnum from the first argument.
20788 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020789 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 */
20791 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020792get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020793 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020794{
Bram Moolenaar33570922005-01-25 22:26:29 +000020795 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 linenr_T lnum;
20797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020798 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799 if (lnum == 0) /* no valid number, try using line() */
20800 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020801 rettv.v_type = VAR_NUMBER;
20802 f_line(argvars, &rettv);
20803 lnum = rettv.vval.v_number;
20804 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020805 }
20806 return lnum;
20807}
20808
20809/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020810 * Get the lnum from the first argument.
20811 * Also accepts "$", then "buf" is used.
20812 * Returns 0 on error.
20813 */
20814 static linenr_T
20815get_tv_lnum_buf(argvars, buf)
20816 typval_T *argvars;
20817 buf_T *buf;
20818{
20819 if (argvars[0].v_type == VAR_STRING
20820 && argvars[0].vval.v_string != NULL
20821 && argvars[0].vval.v_string[0] == '$'
20822 && buf != NULL)
20823 return buf->b_ml.ml_line_count;
20824 return get_tv_number_chk(&argvars[0], NULL);
20825}
20826
20827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020828 * Get the string value of a variable.
20829 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020830 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20831 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832 * If the String variable has never been set, return an empty string.
20833 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020834 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20835 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 */
20837 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020838get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020839 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840{
20841 static char_u mybuf[NUMBUFLEN];
20842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844}
20845
20846 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020847get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020848 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020849 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020851 char_u *res = get_tv_string_buf_chk(varp, buf);
20852
20853 return res != NULL ? res : (char_u *)"";
20854}
20855
Bram Moolenaar7d647822014-04-05 21:28:56 +020020856/*
20857 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20858 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020859 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020860get_tv_string_chk(varp)
20861 typval_T *varp;
20862{
20863 static char_u mybuf[NUMBUFLEN];
20864
20865 return get_tv_string_buf_chk(varp, mybuf);
20866}
20867
20868 static char_u *
20869get_tv_string_buf_chk(varp, buf)
20870 typval_T *varp;
20871 char_u *buf;
20872{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020873 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020875 case VAR_NUMBER:
20876 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20877 return buf;
20878 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020879 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020880 break;
20881 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020882 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020883 break;
20884 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020885 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020886 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020887#ifdef FEAT_FLOAT
20888 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020889 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020890 break;
20891#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020892 case VAR_STRING:
20893 if (varp->vval.v_string != NULL)
20894 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020895 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020896 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020897 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020898 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020899 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020900 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901}
20902
20903/*
20904 * Find variable "name" in the list of variables.
20905 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020906 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020907 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020908 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020910 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020911find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020913 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020914 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020917 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918
Bram Moolenaara7043832005-01-21 11:56:39 +000020919 ht = find_var_ht(name, &varname);
20920 if (htp != NULL)
20921 *htp = ht;
20922 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020924 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020925}
20926
20927/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020928 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020929 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020931 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020932find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020933 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020934 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020935 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020936 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020937{
Bram Moolenaar33570922005-01-25 22:26:29 +000020938 hashitem_T *hi;
20939
20940 if (*varname == NUL)
20941 {
20942 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020943 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020944 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020945 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020946 case 'g': return &globvars_var;
20947 case 'v': return &vimvars_var;
20948 case 'b': return &curbuf->b_bufvar;
20949 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020950#ifdef FEAT_WINDOWS
20951 case 't': return &curtab->tp_winvar;
20952#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020953 case 'l': return current_funccal == NULL
20954 ? NULL : &current_funccal->l_vars_var;
20955 case 'a': return current_funccal == NULL
20956 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020957 }
20958 return NULL;
20959 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020960
20961 hi = hash_find(ht, varname);
20962 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020963 {
20964 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020965 * worked find the variable again. Don't auto-load a script if it was
20966 * loaded already, otherwise it would be loaded every time when
20967 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020968 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020969 {
20970 /* Note: script_autoload() may make "hi" invalid. It must either
20971 * be obtained again or not used. */
20972 if (!script_autoload(varname, FALSE) || aborting())
20973 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020974 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020975 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020976 if (HASHITEM_EMPTY(hi))
20977 return NULL;
20978 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020979 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020980}
20981
20982/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020983 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020984 * Set "varname" to the start of name without ':'.
20985 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020986 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020987find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 char_u *name;
20989 char_u **varname;
20990{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020991 hashitem_T *hi;
20992
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993 if (name[1] != ':')
20994 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020995 /* The name must not start with a colon or #. */
20996 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 return NULL;
20998 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020999
21000 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000021001 hi = hash_find(&compat_hashtab, name);
21002 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000021003 return &compat_hashtab;
21004
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021006 return &globvarht; /* global variable */
21007 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008 }
21009 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021010 if (*name == 'g') /* global variable */
21011 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021012 /* There must be no ':' or '#' in the rest of the name, unless g: is used
21013 */
21014 if (vim_strchr(name + 2, ':') != NULL
21015 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021016 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021018 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021020 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021021#ifdef FEAT_WINDOWS
21022 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020021023 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021024#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000021025 if (*name == 'v') /* v: variable */
21026 return &vimvarht;
21027 if (*name == 'a' && current_funccal != NULL) /* function argument */
21028 return &current_funccal->l_avars.dv_hashtab;
21029 if (*name == 'l' && current_funccal != NULL) /* local function variable */
21030 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021031 if (*name == 's' /* script variable */
21032 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
21033 return &SCRIPT_VARS(current_SID);
21034 return NULL;
21035}
21036
21037/*
21038 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020021039 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 * Returns NULL when it doesn't exist.
21041 */
21042 char_u *
21043get_var_value(name)
21044 char_u *name;
21045{
Bram Moolenaar33570922005-01-25 22:26:29 +000021046 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021048 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049 if (v == NULL)
21050 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021051 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052}
21053
21054/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021055 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056 * sourcing this script and when executing functions defined in the script.
21057 */
21058 void
21059new_script_vars(id)
21060 scid_T id;
21061{
Bram Moolenaara7043832005-01-21 11:56:39 +000021062 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000021063 hashtab_T *ht;
21064 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000021065
Bram Moolenaar071d4272004-06-13 20:20:40 +000021066 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
21067 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021068 /* Re-allocating ga_data means that an ht_array pointing to
21069 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000021070 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000021071 for (i = 1; i <= ga_scripts.ga_len; ++i)
21072 {
21073 ht = &SCRIPT_VARS(i);
21074 if (ht->ht_mask == HT_INIT_SIZE - 1)
21075 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021076 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000021077 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000021078 }
21079
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 while (ga_scripts.ga_len < id)
21081 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020021082 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020021083 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021084 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 }
21087 }
21088}
21089
21090/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021091 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
21092 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 */
21094 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021095init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000021096 dict_T *dict;
21097 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021098 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021099{
Bram Moolenaar33570922005-01-25 22:26:29 +000021100 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020021101 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020021102 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021103 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000021104 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021105 dict_var->di_tv.vval.v_dict = dict;
21106 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021107 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021108 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21109 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110}
21111
21112/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020021113 * Unreference a dictionary initialized by init_var_dict().
21114 */
21115 void
21116unref_var_dict(dict)
21117 dict_T *dict;
21118{
21119 /* Now the dict needs to be freed if no one else is using it, go back to
21120 * normal reference counting. */
21121 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
21122 dict_unref(dict);
21123}
21124
21125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000021127 * Frees all allocated variables and the value they contain.
21128 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 */
21130 void
Bram Moolenaara7043832005-01-21 11:56:39 +000021131vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000021132 hashtab_T *ht;
21133{
21134 vars_clear_ext(ht, TRUE);
21135}
21136
21137/*
21138 * Like vars_clear(), but only free the value if "free_val" is TRUE.
21139 */
21140 static void
21141vars_clear_ext(ht, free_val)
21142 hashtab_T *ht;
21143 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144{
Bram Moolenaara7043832005-01-21 11:56:39 +000021145 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000021146 hashitem_T *hi;
21147 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148
Bram Moolenaar33570922005-01-25 22:26:29 +000021149 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021150 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000021151 for (hi = ht->ht_array; todo > 0; ++hi)
21152 {
21153 if (!HASHITEM_EMPTY(hi))
21154 {
21155 --todo;
21156
Bram Moolenaar33570922005-01-25 22:26:29 +000021157 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000021158 * ht_array might change then. hash_clear() takes care of it
21159 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021160 v = HI2DI(hi);
21161 if (free_val)
21162 clear_tv(&v->di_tv);
21163 if ((v->di_flags & DI_FLAGS_FIX) == 0)
21164 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000021165 }
21166 }
21167 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000021168 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169}
21170
Bram Moolenaara7043832005-01-21 11:56:39 +000021171/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021172 * Delete a variable from hashtab "ht" at item "hi".
21173 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000021174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000021176delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000021177 hashtab_T *ht;
21178 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179{
Bram Moolenaar33570922005-01-25 22:26:29 +000021180 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000021181
21182 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000021183 clear_tv(&di->di_tv);
21184 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021185}
21186
21187/*
21188 * List the value of one internal variable.
21189 */
21190 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021191list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000021192 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021193 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021194 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021196 char_u *tofree;
21197 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021198 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021199
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021200 current_copyID += COPYID_INC;
21201 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000021202 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021203 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021204 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021205}
21206
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021208list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 char_u *prefix;
21210 char_u *name;
21211 int type;
21212 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021213 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214{
Bram Moolenaar31859182007-08-14 20:41:13 +000021215 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
21216 msg_start();
21217 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218 if (name != NULL) /* "a:" vars don't have a name stored */
21219 msg_puts(name);
21220 msg_putchar(' ');
21221 msg_advance(22);
21222 if (type == VAR_NUMBER)
21223 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021224 else if (type == VAR_FUNC)
21225 msg_putchar('*');
21226 else if (type == VAR_LIST)
21227 {
21228 msg_putchar('[');
21229 if (*string == '[')
21230 ++string;
21231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000021232 else if (type == VAR_DICT)
21233 {
21234 msg_putchar('{');
21235 if (*string == '{')
21236 ++string;
21237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021238 else
21239 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021240
Bram Moolenaar071d4272004-06-13 20:20:40 +000021241 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021242
21243 if (type == VAR_FUNC)
21244 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000021245 if (*first)
21246 {
21247 msg_clr_eos();
21248 *first = FALSE;
21249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250}
21251
21252/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021253 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021254 * If the variable already exists, the value is updated.
21255 * Otherwise the variable is created.
21256 */
21257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021258set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021259 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021260 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021261 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262{
Bram Moolenaar33570922005-01-25 22:26:29 +000021263 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021264 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000021265 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021266
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021267 ht = find_var_ht(name, &varname);
21268 if (ht == NULL || *varname == NUL)
21269 {
21270 EMSG2(_(e_illvar), name);
21271 return;
21272 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020021273 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010021274
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021275 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
21276 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021277
Bram Moolenaar33570922005-01-25 22:26:29 +000021278 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021280 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021281 if (var_check_ro(v->di_flags, name)
21282 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000021283 return;
21284 if (v->di_tv.v_type != tv->v_type
21285 && !((v->di_tv.v_type == VAR_STRING
21286 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021287 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021288 || tv->v_type == VAR_NUMBER))
21289#ifdef FEAT_FLOAT
21290 && !((v->di_tv.v_type == VAR_NUMBER
21291 || v->di_tv.v_type == VAR_FLOAT)
21292 && (tv->v_type == VAR_NUMBER
21293 || tv->v_type == VAR_FLOAT))
21294#endif
21295 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021296 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000021297 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021298 return;
21299 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021300
21301 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000021302 * Handle setting internal v: variables separately: we don't change
21303 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000021304 */
21305 if (ht == &vimvarht)
21306 {
21307 if (v->di_tv.v_type == VAR_STRING)
21308 {
21309 vim_free(v->di_tv.vval.v_string);
21310 if (copy || tv->v_type != VAR_STRING)
21311 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
21312 else
21313 {
21314 /* Take over the string to avoid an extra alloc/free. */
21315 v->di_tv.vval.v_string = tv->vval.v_string;
21316 tv->vval.v_string = NULL;
21317 }
21318 }
21319 else if (v->di_tv.v_type != VAR_NUMBER)
21320 EMSG2(_(e_intern2), "set_var()");
21321 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021322 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021323 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021324 if (STRCMP(varname, "searchforward") == 0)
21325 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010021326#ifdef FEAT_SEARCH_EXTRA
21327 else if (STRCMP(varname, "hlsearch") == 0)
21328 {
21329 no_hlsearch = !v->di_tv.vval.v_number;
21330 redraw_all_later(SOME_VALID);
21331 }
21332#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021333 }
Bram Moolenaar33570922005-01-25 22:26:29 +000021334 return;
21335 }
21336
21337 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 }
21339 else /* add a new variable */
21340 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000021341 /* Can't add "v:" variable. */
21342 if (ht == &vimvarht)
21343 {
21344 EMSG2(_(e_illvar), name);
21345 return;
21346 }
21347
Bram Moolenaar92124a32005-06-17 22:03:40 +000021348 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021349 if (!valid_varname(varname))
21350 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000021351
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021352 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21353 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000021354 if (v == NULL)
21355 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000021356 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000021357 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 {
Bram Moolenaara7043832005-01-21 11:56:39 +000021359 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021360 return;
21361 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021362 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021364
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021365 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000021366 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021367 else
21368 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021369 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021370 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021371 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000021372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373}
21374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021375/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021376 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000021377 * Also give an error message.
21378 */
21379 static int
21380var_check_ro(flags, name)
21381 int flags;
21382 char_u *name;
21383{
21384 if (flags & DI_FLAGS_RO)
21385 {
21386 EMSG2(_(e_readonlyvar), name);
21387 return TRUE;
21388 }
21389 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
21390 {
21391 EMSG2(_(e_readonlysbx), name);
21392 return TRUE;
21393 }
21394 return FALSE;
21395}
21396
21397/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000021398 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
21399 * Also give an error message.
21400 */
21401 static int
21402var_check_fixed(flags, name)
21403 int flags;
21404 char_u *name;
21405{
21406 if (flags & DI_FLAGS_FIX)
21407 {
21408 EMSG2(_("E795: Cannot delete variable %s"), name);
21409 return TRUE;
21410 }
21411 return FALSE;
21412}
21413
21414/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021415 * Check if a funcref is assigned to a valid variable name.
21416 * Return TRUE and give an error if not.
21417 */
21418 static int
21419var_check_func_name(name, new_var)
21420 char_u *name; /* points to start of variable name */
21421 int new_var; /* TRUE when creating the variable */
21422{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020021423 /* Allow for w: b: s: and t:. */
21424 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020021425 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
21426 ? name[2] : name[0]))
21427 {
21428 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
21429 name);
21430 return TRUE;
21431 }
21432 /* Don't allow hiding a function. When "v" is not NULL we might be
21433 * assigning another function to the same var, the type is checked
21434 * below. */
21435 if (new_var && function_exists(name))
21436 {
21437 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
21438 name);
21439 return TRUE;
21440 }
21441 return FALSE;
21442}
21443
21444/*
21445 * Check if a variable name is valid.
21446 * Return FALSE and give an error if not.
21447 */
21448 static int
21449valid_varname(varname)
21450 char_u *varname;
21451{
21452 char_u *p;
21453
21454 for (p = varname; *p != NUL; ++p)
21455 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
21456 && *p != AUTOLOAD_CHAR)
21457 {
21458 EMSG2(_(e_illvar), varname);
21459 return FALSE;
21460 }
21461 return TRUE;
21462}
21463
21464/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021465 * Return TRUE if typeval "tv" is set to be locked (immutable).
21466 * Also give an error message, using "name".
21467 */
21468 static int
21469tv_check_lock(lock, name)
21470 int lock;
21471 char_u *name;
21472{
21473 if (lock & VAR_LOCKED)
21474 {
21475 EMSG2(_("E741: Value is locked: %s"),
21476 name == NULL ? (char_u *)_("Unknown") : name);
21477 return TRUE;
21478 }
21479 if (lock & VAR_FIXED)
21480 {
21481 EMSG2(_("E742: Cannot change value of %s"),
21482 name == NULL ? (char_u *)_("Unknown") : name);
21483 return TRUE;
21484 }
21485 return FALSE;
21486}
21487
21488/*
Bram Moolenaar33570922005-01-25 22:26:29 +000021489 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021490 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021491 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021492 * It is OK for "from" and "to" to point to the same item. This is used to
21493 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021494 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010021495 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021496copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000021497 typval_T *from;
21498 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021500 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021501 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021502 switch (from->v_type)
21503 {
21504 case VAR_NUMBER:
21505 to->vval.v_number = from->vval.v_number;
21506 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021507#ifdef FEAT_FLOAT
21508 case VAR_FLOAT:
21509 to->vval.v_float = from->vval.v_float;
21510 break;
21511#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021512 case VAR_STRING:
21513 case VAR_FUNC:
21514 if (from->vval.v_string == NULL)
21515 to->vval.v_string = NULL;
21516 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021517 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021518 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021519 if (from->v_type == VAR_FUNC)
21520 func_ref(to->vval.v_string);
21521 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021522 break;
21523 case VAR_LIST:
21524 if (from->vval.v_list == NULL)
21525 to->vval.v_list = NULL;
21526 else
21527 {
21528 to->vval.v_list = from->vval.v_list;
21529 ++to->vval.v_list->lv_refcount;
21530 }
21531 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021532 case VAR_DICT:
21533 if (from->vval.v_dict == NULL)
21534 to->vval.v_dict = NULL;
21535 else
21536 {
21537 to->vval.v_dict = from->vval.v_dict;
21538 ++to->vval.v_dict->dv_refcount;
21539 }
21540 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021541 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021542 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021543 break;
21544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545}
21546
21547/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000021548 * Make a copy of an item.
21549 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021550 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
21551 * reference to an already copied list/dict can be used.
21552 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021553 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021554 static int
21555item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000021556 typval_T *from;
21557 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021558 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021559 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021560{
21561 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021562 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021563
Bram Moolenaar33570922005-01-25 22:26:29 +000021564 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021565 {
21566 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021567 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021568 }
21569 ++recurse;
21570
21571 switch (from->v_type)
21572 {
21573 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021574#ifdef FEAT_FLOAT
21575 case VAR_FLOAT:
21576#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021577 case VAR_STRING:
21578 case VAR_FUNC:
21579 copy_tv(from, to);
21580 break;
21581 case VAR_LIST:
21582 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021583 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021584 if (from->vval.v_list == NULL)
21585 to->vval.v_list = NULL;
21586 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
21587 {
21588 /* use the copy made earlier */
21589 to->vval.v_list = from->vval.v_list->lv_copylist;
21590 ++to->vval.v_list->lv_refcount;
21591 }
21592 else
21593 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
21594 if (to->vval.v_list == NULL)
21595 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021596 break;
21597 case VAR_DICT:
21598 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021599 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021600 if (from->vval.v_dict == NULL)
21601 to->vval.v_dict = NULL;
21602 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
21603 {
21604 /* use the copy made earlier */
21605 to->vval.v_dict = from->vval.v_dict->dv_copydict;
21606 ++to->vval.v_dict->dv_refcount;
21607 }
21608 else
21609 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
21610 if (to->vval.v_dict == NULL)
21611 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021612 break;
21613 default:
21614 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021615 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021616 }
21617 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021618 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021619}
21620
21621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 * ":echo expr1 ..." print each argument separated with a space, add a
21623 * newline at the end.
21624 * ":echon expr1 ..." print each argument plain.
21625 */
21626 void
21627ex_echo(eap)
21628 exarg_T *eap;
21629{
21630 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021631 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021632 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 char_u *p;
21634 int needclr = TRUE;
21635 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000021636 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637
21638 if (eap->skip)
21639 ++emsg_skip;
21640 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
21641 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021642 /* If eval1() causes an error message the text from the command may
21643 * still need to be cleared. E.g., "echo 22,44". */
21644 need_clr_eos = needclr;
21645
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021647 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 {
21649 /*
21650 * Report the invalid expression unless the expression evaluation
21651 * has been cancelled due to an aborting error, an interrupt, or an
21652 * exception.
21653 */
21654 if (!aborting())
21655 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021656 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 break;
21658 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021659 need_clr_eos = FALSE;
21660
Bram Moolenaar071d4272004-06-13 20:20:40 +000021661 if (!eap->skip)
21662 {
21663 if (atstart)
21664 {
21665 atstart = FALSE;
21666 /* Call msg_start() after eval1(), evaluating the expression
21667 * may cause a message to appear. */
21668 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010021669 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020021670 /* Mark the saved text as finishing the line, so that what
21671 * follows is displayed on a new line when scrolling back
21672 * at the more prompt. */
21673 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010021675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 }
21677 else if (eap->cmdidx == CMD_echo)
21678 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000021679 current_copyID += COPYID_INC;
21680 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021681 if (p != NULL)
21682 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021684 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021686 if (*p != TAB && needclr)
21687 {
21688 /* remove any text still there from the command */
21689 msg_clr_eos();
21690 needclr = FALSE;
21691 }
21692 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 }
21694 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021695 {
21696#ifdef FEAT_MBYTE
21697 if (has_mbyte)
21698 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021699 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021700
21701 (void)msg_outtrans_len_attr(p, i, echo_attr);
21702 p += i - 1;
21703 }
21704 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021706 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021709 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712 arg = skipwhite(arg);
21713 }
21714 eap->nextcmd = check_nextcmd(arg);
21715
21716 if (eap->skip)
21717 --emsg_skip;
21718 else
21719 {
21720 /* remove text that may still be there from the command */
21721 if (needclr)
21722 msg_clr_eos();
21723 if (eap->cmdidx == CMD_echo)
21724 msg_end();
21725 }
21726}
21727
21728/*
21729 * ":echohl {name}".
21730 */
21731 void
21732ex_echohl(eap)
21733 exarg_T *eap;
21734{
21735 int id;
21736
21737 id = syn_name2id(eap->arg);
21738 if (id == 0)
21739 echo_attr = 0;
21740 else
21741 echo_attr = syn_id2attr(id);
21742}
21743
21744/*
21745 * ":execute expr1 ..." execute the result of an expression.
21746 * ":echomsg expr1 ..." Print a message
21747 * ":echoerr expr1 ..." Print an error
21748 * Each gets spaces around each argument and a newline at the end for
21749 * echo commands
21750 */
21751 void
21752ex_execute(eap)
21753 exarg_T *eap;
21754{
21755 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021756 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 int ret = OK;
21758 char_u *p;
21759 garray_T ga;
21760 int len;
21761 int save_did_emsg;
21762
21763 ga_init2(&ga, 1, 80);
21764
21765 if (eap->skip)
21766 ++emsg_skip;
21767 while (*arg != NUL && *arg != '|' && *arg != '\n')
21768 {
21769 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021770 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771 {
21772 /*
21773 * Report the invalid expression unless the expression evaluation
21774 * has been cancelled due to an aborting error, an interrupt, or an
21775 * exception.
21776 */
21777 if (!aborting())
21778 EMSG2(_(e_invexpr2), p);
21779 ret = FAIL;
21780 break;
21781 }
21782
21783 if (!eap->skip)
21784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021785 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 len = (int)STRLEN(p);
21787 if (ga_grow(&ga, len + 2) == FAIL)
21788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021789 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 ret = FAIL;
21791 break;
21792 }
21793 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 ga.ga_len += len;
21797 }
21798
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021799 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021800 arg = skipwhite(arg);
21801 }
21802
21803 if (ret != FAIL && ga.ga_data != NULL)
21804 {
21805 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021806 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021807 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021808 out_flush();
21809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021810 else if (eap->cmdidx == CMD_echoerr)
21811 {
21812 /* We don't want to abort following commands, restore did_emsg. */
21813 save_did_emsg = did_emsg;
21814 EMSG((char_u *)ga.ga_data);
21815 if (!force_abort)
21816 did_emsg = save_did_emsg;
21817 }
21818 else if (eap->cmdidx == CMD_execute)
21819 do_cmdline((char_u *)ga.ga_data,
21820 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21821 }
21822
21823 ga_clear(&ga);
21824
21825 if (eap->skip)
21826 --emsg_skip;
21827
21828 eap->nextcmd = check_nextcmd(arg);
21829}
21830
21831/*
21832 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21833 * "arg" points to the "&" or '+' when called, to "option" when returning.
21834 * Returns NULL when no option name found. Otherwise pointer to the char
21835 * after the option name.
21836 */
21837 static char_u *
21838find_option_end(arg, opt_flags)
21839 char_u **arg;
21840 int *opt_flags;
21841{
21842 char_u *p = *arg;
21843
21844 ++p;
21845 if (*p == 'g' && p[1] == ':')
21846 {
21847 *opt_flags = OPT_GLOBAL;
21848 p += 2;
21849 }
21850 else if (*p == 'l' && p[1] == ':')
21851 {
21852 *opt_flags = OPT_LOCAL;
21853 p += 2;
21854 }
21855 else
21856 *opt_flags = 0;
21857
21858 if (!ASCII_ISALPHA(*p))
21859 return NULL;
21860 *arg = p;
21861
21862 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21863 p += 4; /* termcap option */
21864 else
21865 while (ASCII_ISALPHA(*p))
21866 ++p;
21867 return p;
21868}
21869
21870/*
21871 * ":function"
21872 */
21873 void
21874ex_function(eap)
21875 exarg_T *eap;
21876{
21877 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021878 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879 int j;
21880 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021882 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 char_u *name = NULL;
21884 char_u *p;
21885 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021886 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887 garray_T newargs;
21888 garray_T newlines;
21889 int varargs = FALSE;
21890 int mustend = FALSE;
21891 int flags = 0;
21892 ufunc_T *fp;
21893 int indent;
21894 int nesting;
21895 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021896 dictitem_T *v;
21897 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021898 static int func_nr = 0; /* number for nameless function */
21899 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021900 hashtab_T *ht;
21901 int todo;
21902 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021903 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904
21905 /*
21906 * ":function" without argument: list functions.
21907 */
21908 if (ends_excmd(*eap->arg))
21909 {
21910 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021911 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021912 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021913 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021914 {
21915 if (!HASHITEM_EMPTY(hi))
21916 {
21917 --todo;
21918 fp = HI2UF(hi);
21919 if (!isdigit(*fp->uf_name))
21920 list_func_head(fp, FALSE);
21921 }
21922 }
21923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021924 eap->nextcmd = check_nextcmd(eap->arg);
21925 return;
21926 }
21927
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021928 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021929 * ":function /pat": list functions matching pattern.
21930 */
21931 if (*eap->arg == '/')
21932 {
21933 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21934 if (!eap->skip)
21935 {
21936 regmatch_T regmatch;
21937
21938 c = *p;
21939 *p = NUL;
21940 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21941 *p = c;
21942 if (regmatch.regprog != NULL)
21943 {
21944 regmatch.rm_ic = p_ic;
21945
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021946 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021947 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21948 {
21949 if (!HASHITEM_EMPTY(hi))
21950 {
21951 --todo;
21952 fp = HI2UF(hi);
21953 if (!isdigit(*fp->uf_name)
21954 && vim_regexec(&regmatch, fp->uf_name, 0))
21955 list_func_head(fp, FALSE);
21956 }
21957 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021958 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021959 }
21960 }
21961 if (*p == '/')
21962 ++p;
21963 eap->nextcmd = check_nextcmd(p);
21964 return;
21965 }
21966
21967 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021968 * Get the function name. There are these situations:
21969 * func normal function name
21970 * "name" == func, "fudi.fd_dict" == NULL
21971 * dict.func new dictionary entry
21972 * "name" == NULL, "fudi.fd_dict" set,
21973 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21974 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021975 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021976 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21977 * dict.func existing dict entry that's not a Funcref
21978 * "name" == NULL, "fudi.fd_dict" set,
21979 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020021980 * s:func script-local function name
21981 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021984 name = trans_function_name(&p, eap->skip, 0, &fudi);
21985 paren = (vim_strchr(p, '(') != NULL);
21986 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 {
21988 /*
21989 * Return on an invalid expression in braces, unless the expression
21990 * evaluation has been cancelled due to an aborting error, an
21991 * interrupt, or an exception.
21992 */
21993 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021994 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021995 if (!eap->skip && fudi.fd_newkey != NULL)
21996 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021997 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 else
22001 eap->skip = TRUE;
22002 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000022003
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 /* An error in a function call during evaluation of an expression in magic
22005 * braces should not cause the function not to be defined. */
22006 saved_did_emsg = did_emsg;
22007 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022008
22009 /*
22010 * ":function func" with only function name: list function.
22011 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022012 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 {
22014 if (!ends_excmd(*skipwhite(p)))
22015 {
22016 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022017 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018 }
22019 eap->nextcmd = check_nextcmd(p);
22020 if (eap->nextcmd != NULL)
22021 *p = NUL;
22022 if (!eap->skip && !got_int)
22023 {
22024 fp = find_func(name);
22025 if (fp != NULL)
22026 {
22027 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022028 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022030 if (FUNCLINE(fp, j) == NULL)
22031 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 msg_putchar('\n');
22033 msg_outnum((long)(j + 1));
22034 if (j < 9)
22035 msg_putchar(' ');
22036 if (j < 99)
22037 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022038 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 out_flush(); /* show a line at a time */
22040 ui_breakcheck();
22041 }
22042 if (!got_int)
22043 {
22044 msg_putchar('\n');
22045 msg_puts((char_u *)" endfunction");
22046 }
22047 }
22048 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022049 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022051 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 }
22053
22054 /*
22055 * ":function name(arg1, arg2)" Define function.
22056 */
22057 p = skipwhite(p);
22058 if (*p != '(')
22059 {
22060 if (!eap->skip)
22061 {
22062 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022063 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 }
22065 /* attempt to continue by skipping some text */
22066 if (vim_strchr(p, '(') != NULL)
22067 p = vim_strchr(p, '(');
22068 }
22069 p = skipwhite(p + 1);
22070
22071 ga_init2(&newargs, (int)sizeof(char_u *), 3);
22072 ga_init2(&newlines, (int)sizeof(char_u *), 3);
22073
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022074 if (!eap->skip)
22075 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022076 /* Check the name of the function. Unless it's a dictionary function
22077 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022078 if (name != NULL)
22079 arg = name;
22080 else
22081 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000022082 if (arg != NULL && (fudi.fd_di == NULL
22083 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022084 {
22085 if (*arg == K_SPECIAL)
22086 j = 3;
22087 else
22088 j = 0;
22089 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
22090 : eval_isnamec(arg[j])))
22091 ++j;
22092 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000022093 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022094 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010022095 /* Disallow using the g: dict. */
22096 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
22097 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022098 }
22099
Bram Moolenaar071d4272004-06-13 20:20:40 +000022100 /*
22101 * Isolate the arguments: "arg1, arg2, ...)"
22102 */
22103 while (*p != ')')
22104 {
22105 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
22106 {
22107 varargs = TRUE;
22108 p += 3;
22109 mustend = TRUE;
22110 }
22111 else
22112 {
22113 arg = p;
22114 while (ASCII_ISALNUM(*p) || *p == '_')
22115 ++p;
22116 if (arg == p || isdigit(*arg)
22117 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
22118 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
22119 {
22120 if (!eap->skip)
22121 EMSG2(_("E125: Illegal argument: %s"), arg);
22122 break;
22123 }
22124 if (ga_grow(&newargs, 1) == FAIL)
22125 goto erret;
22126 c = *p;
22127 *p = NUL;
22128 arg = vim_strsave(arg);
22129 if (arg == NULL)
22130 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022131
22132 /* Check for duplicate argument name. */
22133 for (i = 0; i < newargs.ga_len; ++i)
22134 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
22135 {
22136 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010022137 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020022138 goto erret;
22139 }
22140
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
22142 *p = c;
22143 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 if (*p == ',')
22145 ++p;
22146 else
22147 mustend = TRUE;
22148 }
22149 p = skipwhite(p);
22150 if (mustend && *p != ')')
22151 {
22152 if (!eap->skip)
22153 EMSG2(_(e_invarg2), eap->arg);
22154 break;
22155 }
22156 }
22157 ++p; /* skip the ')' */
22158
Bram Moolenaare9a41262005-01-15 22:18:47 +000022159 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 for (;;)
22161 {
22162 p = skipwhite(p);
22163 if (STRNCMP(p, "range", 5) == 0)
22164 {
22165 flags |= FC_RANGE;
22166 p += 5;
22167 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022168 else if (STRNCMP(p, "dict", 4) == 0)
22169 {
22170 flags |= FC_DICT;
22171 p += 4;
22172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 else if (STRNCMP(p, "abort", 5) == 0)
22174 {
22175 flags |= FC_ABORT;
22176 p += 5;
22177 }
22178 else
22179 break;
22180 }
22181
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022182 /* When there is a line break use what follows for the function body.
22183 * Makes 'exe "func Test()\n...\nendfunc"' work. */
22184 if (*p == '\n')
22185 line_arg = p + 1;
22186 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022187 EMSG(_(e_trailing));
22188
22189 /*
22190 * Read the body of the function, until ":endfunction" is found.
22191 */
22192 if (KeyTyped)
22193 {
22194 /* Check if the function already exists, don't let the user type the
22195 * whole function before telling him it doesn't work! For a script we
22196 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022197 if (!eap->skip && !eap->forceit)
22198 {
22199 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
22200 EMSG(_(e_funcdict));
22201 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022202 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204
Bram Moolenaard857f0e2005-06-21 22:37:39 +000022205 if (!eap->skip && did_emsg)
22206 goto erret;
22207
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208 msg_putchar('\n'); /* don't overwrite the function name */
22209 cmdline_row = msg_row;
22210 }
22211
22212 indent = 2;
22213 nesting = 0;
22214 for (;;)
22215 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022216 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022217 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020022218 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022219 saved_wait_return = FALSE;
22220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022222 sourcing_lnum_off = sourcing_lnum;
22223
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022224 if (line_arg != NULL)
22225 {
22226 /* Use eap->arg, split up in parts by line breaks. */
22227 theline = line_arg;
22228 p = vim_strchr(theline, '\n');
22229 if (p == NULL)
22230 line_arg += STRLEN(line_arg);
22231 else
22232 {
22233 *p = NUL;
22234 line_arg = p + 1;
22235 }
22236 }
22237 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 theline = getcmdline(':', 0L, indent);
22239 else
22240 theline = eap->getline(':', eap->cookie, indent);
22241 if (KeyTyped)
22242 lines_left = Rows - 1;
22243 if (theline == NULL)
22244 {
22245 EMSG(_("E126: Missing :endfunction"));
22246 goto erret;
22247 }
22248
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022249 /* Detect line continuation: sourcing_lnum increased more than one. */
22250 if (sourcing_lnum > sourcing_lnum_off + 1)
22251 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
22252 else
22253 sourcing_lnum_off = 0;
22254
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 if (skip_until != NULL)
22256 {
22257 /* between ":append" and "." and between ":python <<EOF" and "EOF"
22258 * don't check for ":endfunc". */
22259 if (STRCMP(theline, skip_until) == 0)
22260 {
22261 vim_free(skip_until);
22262 skip_until = NULL;
22263 }
22264 }
22265 else
22266 {
22267 /* skip ':' and blanks*/
22268 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
22269 ;
22270
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022271 /* Check for "endfunction". */
22272 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022274 if (line_arg == NULL)
22275 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 break;
22277 }
22278
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022279 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 * at "end". */
22281 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
22282 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022283 else if (STRNCMP(p, "if", 2) == 0
22284 || STRNCMP(p, "wh", 2) == 0
22285 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 || STRNCMP(p, "try", 3) == 0)
22287 indent += 2;
22288
22289 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022290 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022292 if (*p == '!')
22293 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010022295 vim_free(trans_function_name(&p, TRUE, 0, NULL));
22296 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000022297 {
Bram Moolenaaref923902014-12-13 21:00:55 +010022298 ++nesting;
22299 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 }
22301 }
22302
22303 /* Check for ":append" or ":insert". */
22304 p = skip_range(p, NULL);
22305 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
22306 || (p[0] == 'i'
22307 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
22308 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
22309 skip_until = vim_strsave((char_u *)".");
22310
22311 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
22312 arg = skipwhite(skiptowhite(p));
22313 if (arg[0] == '<' && arg[1] =='<'
22314 && ((p[0] == 'p' && p[1] == 'y'
22315 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
22316 || (p[0] == 'p' && p[1] == 'e'
22317 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
22318 || (p[0] == 't' && p[1] == 'c'
22319 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020022320 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
22321 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
22323 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000022324 || (p[0] == 'm' && p[1] == 'z'
22325 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 ))
22327 {
22328 /* ":python <<" continues until a dot, like ":append" */
22329 p = skipwhite(arg + 2);
22330 if (*p == NUL)
22331 skip_until = vim_strsave((char_u *)".");
22332 else
22333 skip_until = vim_strsave(p);
22334 }
22335 }
22336
22337 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022338 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022339 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022340 if (line_arg == NULL)
22341 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022343 }
22344
22345 /* Copy the line to newly allocated memory. get_one_sourceline()
22346 * allocates 250 bytes per line, this saves 80% on average. The cost
22347 * is an extra alloc/free. */
22348 p = vim_strsave(theline);
22349 if (p != NULL)
22350 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022351 if (line_arg == NULL)
22352 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022353 theline = p;
22354 }
22355
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022356 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
22357
22358 /* Add NULL lines for continuation lines, so that the line count is
22359 * equal to the index in the growarray. */
22360 while (sourcing_lnum_off-- > 0)
22361 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000022362
22363 /* Check for end of eap->arg. */
22364 if (line_arg != NULL && *line_arg == NUL)
22365 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 }
22367
22368 /* Don't define the function when skipping commands or when an error was
22369 * detected. */
22370 if (eap->skip || did_emsg)
22371 goto erret;
22372
22373 /*
22374 * If there are no errors, add the function
22375 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022376 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022377 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022378 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000022379 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022380 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022381 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022382 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022383 goto erret;
22384 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022385
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022386 fp = find_func(name);
22387 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022389 if (!eap->forceit)
22390 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022391 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022392 goto erret;
22393 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022394 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022395 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000022396 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022397 name);
22398 goto erret;
22399 }
22400 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022401 ga_clear_strings(&(fp->uf_args));
22402 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022403 vim_free(name);
22404 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 }
22407 else
22408 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022409 char numbuf[20];
22410
22411 fp = NULL;
22412 if (fudi.fd_newkey == NULL && !eap->forceit)
22413 {
22414 EMSG(_(e_funcdict));
22415 goto erret;
22416 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000022417 if (fudi.fd_di == NULL)
22418 {
22419 /* Can't add a function to a locked dictionary */
22420 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
22421 goto erret;
22422 }
22423 /* Can't change an existing function if it is locked */
22424 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
22425 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022426
22427 /* Give the function a sequential number. Can only be used with a
22428 * Funcref! */
22429 vim_free(name);
22430 sprintf(numbuf, "%d", ++func_nr);
22431 name = vim_strsave((char_u *)numbuf);
22432 if (name == NULL)
22433 goto erret;
22434 }
22435
22436 if (fp == NULL)
22437 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022438 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022439 {
22440 int slen, plen;
22441 char_u *scriptname;
22442
22443 /* Check that the autoload name matches the script name. */
22444 j = FAIL;
22445 if (sourcing_name != NULL)
22446 {
22447 scriptname = autoload_name(name);
22448 if (scriptname != NULL)
22449 {
22450 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022451 plen = (int)STRLEN(p);
22452 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022453 if (slen > plen && fnamecmp(p,
22454 sourcing_name + slen - plen) == 0)
22455 j = OK;
22456 vim_free(scriptname);
22457 }
22458 }
22459 if (j == FAIL)
22460 {
22461 EMSG2(_("E746: Function name does not match script file name: %s"), name);
22462 goto erret;
22463 }
22464 }
22465
22466 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 if (fp == NULL)
22468 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022469
22470 if (fudi.fd_dict != NULL)
22471 {
22472 if (fudi.fd_di == NULL)
22473 {
22474 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022475 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022476 if (fudi.fd_di == NULL)
22477 {
22478 vim_free(fp);
22479 goto erret;
22480 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022481 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
22482 {
22483 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000022484 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022485 goto erret;
22486 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022487 }
22488 else
22489 /* overwrite existing dict entry */
22490 clear_tv(&fudi.fd_di->di_tv);
22491 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022492 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022493 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022494 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022495
22496 /* behave like "dict" was used */
22497 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022498 }
22499
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022501 STRCPY(fp->uf_name, name);
22502 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022504 fp->uf_args = newargs;
22505 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022506#ifdef FEAT_PROFILE
22507 fp->uf_tml_count = NULL;
22508 fp->uf_tml_total = NULL;
22509 fp->uf_tml_self = NULL;
22510 fp->uf_profiling = FALSE;
22511 if (prof_def_func())
22512 func_do_profile(fp);
22513#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022514 fp->uf_varargs = varargs;
22515 fp->uf_flags = flags;
22516 fp->uf_calls = 0;
22517 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022518 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519
22520erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 ga_clear_strings(&newargs);
22522 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022523ret_free:
22524 vim_free(skip_until);
22525 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020022528 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529}
22530
22531/*
22532 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000022533 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022535 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022536 * TFN_INT: internal function name OK
22537 * TFN_QUIET: be quiet
22538 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539 * Advances "pp" to just after the function name (if no error).
22540 */
22541 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022542trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 char_u **pp;
22544 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022545 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000022546 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022548 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022549 char_u *start;
22550 char_u *end;
22551 int lead;
22552 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022553 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022554 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022555
22556 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000022559
22560 /* Check for hard coded <SNR>: already translated function ID (from a user
22561 * command). */
22562 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
22563 && (*pp)[2] == (int)KE_SNR)
22564 {
22565 *pp += 3;
22566 len = get_id_len(pp) + 3;
22567 return vim_strnsave(start, len);
22568 }
22569
22570 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
22571 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000022573 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022575
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022576 /* Note that TFN_ flags use the same values as GLV_ flags. */
22577 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022578 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022579 if (end == start)
22580 {
22581 if (!skip)
22582 EMSG(_("E129: Function name required"));
22583 goto theend;
22584 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022585 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022586 {
22587 /*
22588 * Report an invalid expression in braces, unless the expression
22589 * evaluation has been cancelled due to an aborting error, an
22590 * interrupt, or an exception.
22591 */
22592 if (!aborting())
22593 {
22594 if (end != NULL)
22595 EMSG2(_(e_invarg2), start);
22596 }
22597 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022598 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022599 goto theend;
22600 }
22601
22602 if (lv.ll_tv != NULL)
22603 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022604 if (fdp != NULL)
22605 {
22606 fdp->fd_dict = lv.ll_dict;
22607 fdp->fd_newkey = lv.ll_newkey;
22608 lv.ll_newkey = NULL;
22609 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022610 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022611 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
22612 {
22613 name = vim_strsave(lv.ll_tv->vval.v_string);
22614 *pp = end;
22615 }
22616 else
22617 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022618 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
22619 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022620 EMSG(_(e_funcref));
22621 else
22622 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022623 name = NULL;
22624 }
22625 goto theend;
22626 }
22627
22628 if (lv.ll_name == NULL)
22629 {
22630 /* Error found, but continue after the function name. */
22631 *pp = end;
22632 goto theend;
22633 }
22634
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022635 /* Check if the name is a Funcref. If so, use the value. */
22636 if (lv.ll_exp_name != NULL)
22637 {
22638 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022639 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022640 if (name == lv.ll_exp_name)
22641 name = NULL;
22642 }
22643 else
22644 {
22645 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010022646 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022647 if (name == *pp)
22648 name = NULL;
22649 }
22650 if (name != NULL)
22651 {
22652 name = vim_strsave(name);
22653 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020022654 if (STRNCMP(name, "<SNR>", 5) == 0)
22655 {
22656 /* Change "<SNR>" to the byte sequence. */
22657 name[0] = K_SPECIAL;
22658 name[1] = KS_EXTRA;
22659 name[2] = (int)KE_SNR;
22660 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
22661 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000022662 goto theend;
22663 }
22664
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022665 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022666 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022667 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000022668 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
22669 && STRNCMP(lv.ll_name, "s:", 2) == 0)
22670 {
22671 /* When there was "s:" already or the name expanded to get a
22672 * leading "s:" then remove it. */
22673 lv.ll_name += 2;
22674 len -= 2;
22675 lead = 2;
22676 }
22677 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022678 else
Bram Moolenaara7043832005-01-21 11:56:39 +000022679 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022680 /* skip over "s:" and "g:" */
22681 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000022682 lv.ll_name += 2;
22683 len = (int)(end - lv.ll_name);
22684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022685
22686 /*
22687 * Copy the function name to allocated memory.
22688 * Accept <SID>name() inside a script, translate into <SNR>123_name().
22689 * Accept <SNR>123_name() outside a script.
22690 */
22691 if (skip)
22692 lead = 0; /* do nothing */
22693 else if (lead > 0)
22694 {
22695 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000022696 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
22697 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022698 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022699 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022700 if (current_SID <= 0)
22701 {
22702 EMSG(_(e_usingsid));
22703 goto theend;
22704 }
22705 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22706 lead += (int)STRLEN(sid_buf);
22707 }
22708 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022709 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022710 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022711 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022712 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022713 goto theend;
22714 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022715 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022716 {
22717 char_u *cp = vim_strchr(lv.ll_name, ':');
22718
22719 if (cp != NULL && cp < end)
22720 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020022721 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022722 goto theend;
22723 }
22724 }
22725
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022726 name = alloc((unsigned)(len + lead + 1));
22727 if (name != NULL)
22728 {
22729 if (lead > 0)
22730 {
22731 name[0] = K_SPECIAL;
22732 name[1] = KS_EXTRA;
22733 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022734 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022735 STRCPY(name + 3, sid_buf);
22736 }
22737 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022738 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022739 }
22740 *pp = end;
22741
22742theend:
22743 clear_lval(&lv);
22744 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745}
22746
22747/*
22748 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22749 * Return 2 if "p" starts with "s:".
22750 * Return 0 otherwise.
22751 */
22752 static int
22753eval_fname_script(p)
22754 char_u *p;
22755{
22756 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22757 || STRNICMP(p + 1, "SNR>", 4) == 0))
22758 return 5;
22759 if (p[0] == 's' && p[1] == ':')
22760 return 2;
22761 return 0;
22762}
22763
22764/*
22765 * Return TRUE if "p" starts with "<SID>" or "s:".
22766 * Only works if eval_fname_script() returned non-zero for "p"!
22767 */
22768 static int
22769eval_fname_sid(p)
22770 char_u *p;
22771{
22772 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22773}
22774
22775/*
22776 * List the head of the function: "name(arg1, arg2)".
22777 */
22778 static void
22779list_func_head(fp, indent)
22780 ufunc_T *fp;
22781 int indent;
22782{
22783 int j;
22784
22785 msg_start();
22786 if (indent)
22787 MSG_PUTS(" ");
22788 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022789 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 {
22791 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022792 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793 }
22794 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022795 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022797 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 {
22799 if (j)
22800 MSG_PUTS(", ");
22801 msg_puts(FUNCARG(fp, j));
22802 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022803 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 {
22805 if (j)
22806 MSG_PUTS(", ");
22807 MSG_PUTS("...");
22808 }
22809 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022810 if (fp->uf_flags & FC_ABORT)
22811 MSG_PUTS(" abort");
22812 if (fp->uf_flags & FC_RANGE)
22813 MSG_PUTS(" range");
22814 if (fp->uf_flags & FC_DICT)
22815 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022816 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022817 if (p_verbose > 0)
22818 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819}
22820
22821/*
22822 * Find a function by name, return pointer to it in ufuncs.
22823 * Return NULL for unknown function.
22824 */
22825 static ufunc_T *
22826find_func(name)
22827 char_u *name;
22828{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022829 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022831 hi = hash_find(&func_hashtab, name);
22832 if (!HASHITEM_EMPTY(hi))
22833 return HI2UF(hi);
22834 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835}
22836
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022837#if defined(EXITFREE) || defined(PROTO)
22838 void
22839free_all_functions()
22840{
22841 hashitem_T *hi;
22842
22843 /* Need to start all over every time, because func_free() may change the
22844 * hash table. */
22845 while (func_hashtab.ht_used > 0)
22846 for (hi = func_hashtab.ht_array; ; ++hi)
22847 if (!HASHITEM_EMPTY(hi))
22848 {
22849 func_free(HI2UF(hi));
22850 break;
22851 }
22852}
22853#endif
22854
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022855 int
22856translated_function_exists(name)
22857 char_u *name;
22858{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022859 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022860 return find_internal_func(name) >= 0;
22861 return find_func(name) != NULL;
22862}
22863
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022864/*
22865 * Return TRUE if a function "name" exists.
22866 */
22867 static int
22868function_exists(name)
22869 char_u *name;
22870{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022871 char_u *nm = name;
22872 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022873 int n = FALSE;
22874
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022875 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22876 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022877 nm = skipwhite(nm);
22878
22879 /* Only accept "funcname", "funcname ", "funcname (..." and
22880 * "funcname(...", not "funcname!...". */
22881 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022882 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022883 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022884 return n;
22885}
22886
Bram Moolenaara1544c02013-05-30 12:35:52 +020022887 char_u *
22888get_expanded_name(name, check)
22889 char_u *name;
22890 int check;
22891{
22892 char_u *nm = name;
22893 char_u *p;
22894
22895 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22896
22897 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022898 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022899 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022900
Bram Moolenaara1544c02013-05-30 12:35:52 +020022901 vim_free(p);
22902 return NULL;
22903}
22904
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022905/*
22906 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022907 * lower case letter and doesn't contain AUTOLOAD_CHAR.
22908 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022909 */
22910 static int
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022911builtin_function(name, len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022912 char_u *name;
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022913 int len;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022914{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020022915 char_u *p;
22916
22917 if (!ASCII_ISLOWER(name[0]))
22918 return FALSE;
22919 p = vim_strchr(name, AUTOLOAD_CHAR);
22920 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022921}
22922
Bram Moolenaar05159a02005-02-26 23:04:13 +000022923#if defined(FEAT_PROFILE) || defined(PROTO)
22924/*
22925 * Start profiling function "fp".
22926 */
22927 static void
22928func_do_profile(fp)
22929 ufunc_T *fp;
22930{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022931 int len = fp->uf_lines.ga_len;
22932
22933 if (len == 0)
22934 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022935 fp->uf_tm_count = 0;
22936 profile_zero(&fp->uf_tm_self);
22937 profile_zero(&fp->uf_tm_total);
22938 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022939 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022940 if (fp->uf_tml_total == NULL)
22941 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022942 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022943 if (fp->uf_tml_self == NULL)
22944 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022945 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022946 fp->uf_tml_idx = -1;
22947 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22948 || fp->uf_tml_self == NULL)
22949 return; /* out of memory */
22950
22951 fp->uf_profiling = TRUE;
22952}
22953
22954/*
22955 * Dump the profiling results for all functions in file "fd".
22956 */
22957 void
22958func_dump_profile(fd)
22959 FILE *fd;
22960{
22961 hashitem_T *hi;
22962 int todo;
22963 ufunc_T *fp;
22964 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022965 ufunc_T **sorttab;
22966 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022967
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022968 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022969 if (todo == 0)
22970 return; /* nothing to dump */
22971
Bram Moolenaar73830342005-02-28 22:48:19 +000022972 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22973
Bram Moolenaar05159a02005-02-26 23:04:13 +000022974 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22975 {
22976 if (!HASHITEM_EMPTY(hi))
22977 {
22978 --todo;
22979 fp = HI2UF(hi);
22980 if (fp->uf_profiling)
22981 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022982 if (sorttab != NULL)
22983 sorttab[st_len++] = fp;
22984
Bram Moolenaar05159a02005-02-26 23:04:13 +000022985 if (fp->uf_name[0] == K_SPECIAL)
22986 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22987 else
22988 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22989 if (fp->uf_tm_count == 1)
22990 fprintf(fd, "Called 1 time\n");
22991 else
22992 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22993 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22994 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22995 fprintf(fd, "\n");
22996 fprintf(fd, "count total (s) self (s)\n");
22997
22998 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22999 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023000 if (FUNCLINE(fp, i) == NULL)
23001 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000023002 prof_func_line(fd, fp->uf_tml_count[i],
23003 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023004 fprintf(fd, "%s\n", FUNCLINE(fp, i));
23005 }
23006 fprintf(fd, "\n");
23007 }
23008 }
23009 }
Bram Moolenaar73830342005-02-28 22:48:19 +000023010
23011 if (sorttab != NULL && st_len > 0)
23012 {
23013 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23014 prof_total_cmp);
23015 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
23016 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
23017 prof_self_cmp);
23018 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
23019 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000023020
23021 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023022}
Bram Moolenaar73830342005-02-28 22:48:19 +000023023
23024 static void
23025prof_sort_list(fd, sorttab, st_len, title, prefer_self)
23026 FILE *fd;
23027 ufunc_T **sorttab;
23028 int st_len;
23029 char *title;
23030 int prefer_self; /* when equal print only self time */
23031{
23032 int i;
23033 ufunc_T *fp;
23034
23035 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
23036 fprintf(fd, "count total (s) self (s) function\n");
23037 for (i = 0; i < 20 && i < st_len; ++i)
23038 {
23039 fp = sorttab[i];
23040 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
23041 prefer_self);
23042 if (fp->uf_name[0] == K_SPECIAL)
23043 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
23044 else
23045 fprintf(fd, " %s()\n", fp->uf_name);
23046 }
23047 fprintf(fd, "\n");
23048}
23049
23050/*
23051 * Print the count and times for one function or function line.
23052 */
23053 static void
23054prof_func_line(fd, count, total, self, prefer_self)
23055 FILE *fd;
23056 int count;
23057 proftime_T *total;
23058 proftime_T *self;
23059 int prefer_self; /* when equal print only self time */
23060{
23061 if (count > 0)
23062 {
23063 fprintf(fd, "%5d ", count);
23064 if (prefer_self && profile_equal(total, self))
23065 fprintf(fd, " ");
23066 else
23067 fprintf(fd, "%s ", profile_msg(total));
23068 if (!prefer_self && profile_equal(total, self))
23069 fprintf(fd, " ");
23070 else
23071 fprintf(fd, "%s ", profile_msg(self));
23072 }
23073 else
23074 fprintf(fd, " ");
23075}
23076
23077/*
23078 * Compare function for total time sorting.
23079 */
23080 static int
23081#ifdef __BORLANDC__
23082_RTLENTRYF
23083#endif
23084prof_total_cmp(s1, s2)
23085 const void *s1;
23086 const void *s2;
23087{
23088 ufunc_T *p1, *p2;
23089
23090 p1 = *(ufunc_T **)s1;
23091 p2 = *(ufunc_T **)s2;
23092 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
23093}
23094
23095/*
23096 * Compare function for self time sorting.
23097 */
23098 static int
23099#ifdef __BORLANDC__
23100_RTLENTRYF
23101#endif
23102prof_self_cmp(s1, s2)
23103 const void *s1;
23104 const void *s2;
23105{
23106 ufunc_T *p1, *p2;
23107
23108 p1 = *(ufunc_T **)s1;
23109 p2 = *(ufunc_T **)s2;
23110 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
23111}
23112
Bram Moolenaar05159a02005-02-26 23:04:13 +000023113#endif
23114
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023115/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023116 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023117 * Return TRUE if a package was loaded.
23118 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020023119 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023120script_autoload(name, reload)
23121 char_u *name;
23122 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023123{
23124 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023125 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023126 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023127 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023128
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023129 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023130 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023131 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023132 return FALSE;
23133
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023134 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023135
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023136 /* Find the name in the list of previously loaded package names. Skip
23137 * "autoload/", it's always the same. */
23138 for (i = 0; i < ga_loaded.ga_len; ++i)
23139 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
23140 break;
23141 if (!reload && i < ga_loaded.ga_len)
23142 ret = FALSE; /* was loaded already */
23143 else
23144 {
23145 /* Remember the name if it wasn't loaded already. */
23146 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
23147 {
23148 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
23149 tofree = NULL;
23150 }
23151
23152 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000023153 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023154 ret = TRUE;
23155 }
23156
23157 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023158 return ret;
23159}
23160
23161/*
23162 * Return the autoload script name for a function or variable name.
23163 * Returns NULL when out of memory.
23164 */
23165 static char_u *
23166autoload_name(name)
23167 char_u *name;
23168{
23169 char_u *p;
23170 char_u *scriptname;
23171
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023172 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023173 scriptname = alloc((unsigned)(STRLEN(name) + 14));
23174 if (scriptname == NULL)
23175 return FALSE;
23176 STRCPY(scriptname, "autoload/");
23177 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023178 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023179 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023180 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023181 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023182 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000023183}
23184
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
23186
23187/*
23188 * Function given to ExpandGeneric() to obtain the list of user defined
23189 * function names.
23190 */
23191 char_u *
23192get_user_func_name(xp, idx)
23193 expand_T *xp;
23194 int idx;
23195{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023196 static long_u done;
23197 static hashitem_T *hi;
23198 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199
23200 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023202 done = 0;
23203 hi = func_hashtab.ht_array;
23204 }
23205 if (done < func_hashtab.ht_used)
23206 {
23207 if (done++ > 0)
23208 ++hi;
23209 while (HASHITEM_EMPTY(hi))
23210 ++hi;
23211 fp = HI2UF(hi);
23212
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023213 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010023214 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010023215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023216 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
23217 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218
23219 cat_func_name(IObuff, fp);
23220 if (xp->xp_context != EXPAND_USER_FUNC)
23221 {
23222 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023223 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 STRCAT(IObuff, ")");
23225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 return IObuff;
23227 }
23228 return NULL;
23229}
23230
23231#endif /* FEAT_CMDL_COMPL */
23232
23233/*
23234 * Copy the function name of "fp" to buffer "buf".
23235 * "buf" must be able to hold the function name plus three bytes.
23236 * Takes care of script-local function names.
23237 */
23238 static void
23239cat_func_name(buf, fp)
23240 char_u *buf;
23241 ufunc_T *fp;
23242{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023243 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023244 {
23245 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023246 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247 }
23248 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023249 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250}
23251
23252/*
23253 * ":delfunction {name}"
23254 */
23255 void
23256ex_delfunction(eap)
23257 exarg_T *eap;
23258{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023259 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 char_u *p;
23261 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000023262 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263
23264 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023265 name = trans_function_name(&p, eap->skip, 0, &fudi);
23266 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023267 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023268 {
23269 if (fudi.fd_dict != NULL && !eap->skip)
23270 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 if (!ends_excmd(*skipwhite(p)))
23274 {
23275 vim_free(name);
23276 EMSG(_(e_trailing));
23277 return;
23278 }
23279 eap->nextcmd = check_nextcmd(p);
23280 if (eap->nextcmd != NULL)
23281 *p = NUL;
23282
23283 if (!eap->skip)
23284 fp = find_func(name);
23285 vim_free(name);
23286
23287 if (!eap->skip)
23288 {
23289 if (fp == NULL)
23290 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023291 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 return;
23293 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023294 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 {
23296 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
23297 return;
23298 }
23299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023300 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023302 /* Delete the dict item that refers to the function, it will
23303 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023304 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023306 else
23307 func_free(fp);
23308 }
23309}
23310
23311/*
23312 * Free a function and remove it from the list of functions.
23313 */
23314 static void
23315func_free(fp)
23316 ufunc_T *fp;
23317{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023318 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023319
23320 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023321 ga_clear_strings(&(fp->uf_args));
23322 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000023323#ifdef FEAT_PROFILE
23324 vim_free(fp->uf_tml_count);
23325 vim_free(fp->uf_tml_total);
23326 vim_free(fp->uf_tml_self);
23327#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023328
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023329 /* remove the function from the function hashtable */
23330 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
23331 if (HASHITEM_EMPTY(hi))
23332 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023333 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023334 hash_remove(&func_hashtab, hi);
23335
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023336 vim_free(fp);
23337}
23338
23339/*
23340 * Unreference a Function: decrement the reference count and free it when it
23341 * becomes zero. Only for numbered functions.
23342 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023343 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023344func_unref(name)
23345 char_u *name;
23346{
23347 ufunc_T *fp;
23348
23349 if (name != NULL && isdigit(*name))
23350 {
23351 fp = find_func(name);
23352 if (fp == NULL)
23353 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023354 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023355 {
23356 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023357 * when "uf_calls" becomes zero. */
23358 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023359 func_free(fp);
23360 }
23361 }
23362}
23363
23364/*
23365 * Count a reference to a Function.
23366 */
Bram Moolenaardb913952012-06-29 12:54:53 +020023367 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023368func_ref(name)
23369 char_u *name;
23370{
23371 ufunc_T *fp;
23372
23373 if (name != NULL && isdigit(*name))
23374 {
23375 fp = find_func(name);
23376 if (fp == NULL)
23377 EMSG2(_(e_intern2), "func_ref()");
23378 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023379 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023380 }
23381}
23382
23383/*
23384 * Call a user function.
23385 */
23386 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000023387call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 ufunc_T *fp; /* pointer to function */
23389 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000023390 typval_T *argvars; /* arguments */
23391 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 linenr_T firstline; /* first line of range */
23393 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000023394 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395{
Bram Moolenaar33570922005-01-25 22:26:29 +000023396 char_u *save_sourcing_name;
23397 linenr_T save_sourcing_lnum;
23398 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023399 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000023400 int save_did_emsg;
23401 static int depth = 0;
23402 dictitem_T *v;
23403 int fixvar_idx = 0; /* index in fixvar[] */
23404 int i;
23405 int ai;
23406 char_u numbuf[NUMBUFLEN];
23407 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023408#ifdef FEAT_PROFILE
23409 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023410 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023412
23413 /* If depth of calling is getting too high, don't execute the function */
23414 if (depth >= p_mfd)
23415 {
23416 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023417 rettv->v_type = VAR_NUMBER;
23418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 return;
23420 }
23421 ++depth;
23422
23423 line_breakcheck(); /* check for CTRL-C hit */
23424
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023425 fc = (funccall_T *)alloc(sizeof(funccall_T));
23426 fc->caller = current_funccal;
23427 current_funccal = fc;
23428 fc->func = fp;
23429 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023430 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023431 fc->linenr = 0;
23432 fc->returned = FALSE;
23433 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023435 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
23436 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437
Bram Moolenaar33570922005-01-25 22:26:29 +000023438 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023439 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000023440 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
23441 * each argument variable and saves a lot of time.
23442 */
23443 /*
23444 * Init l: variables.
23445 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023446 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000023447 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023448 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023449 /* Set l:self to "selfdict". Use "name" to avoid a warning from
23450 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023451 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000023452 name = v->di_key;
23453 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000023454 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023455 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023456 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023457 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023458 v->di_tv.vval.v_dict = selfdict;
23459 ++selfdict->dv_refcount;
23460 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023461
Bram Moolenaar33570922005-01-25 22:26:29 +000023462 /*
23463 * Init a: variables.
23464 * Set a:0 to "argcount".
23465 * Set a:000 to a list with room for the "..." arguments.
23466 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023467 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023468 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023469 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023470 /* Use "name" to avoid a warning from some compiler that checks the
23471 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023472 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000023473 name = v->di_key;
23474 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000023475 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023476 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023477 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023478 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023479 v->di_tv.vval.v_list = &fc->l_varlist;
23480 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
23481 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
23482 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023483
23484 /*
23485 * Set a:firstline to "firstline" and a:lastline to "lastline".
23486 * Set a:name to named arguments.
23487 * Set a:N to the "..." arguments.
23488 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023489 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023490 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023491 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000023492 (varnumber_T)lastline);
23493 for (i = 0; i < argcount; ++i)
23494 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023495 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023496 if (ai < 0)
23497 /* named argument a:name */
23498 name = FUNCARG(fp, i);
23499 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000023500 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023501 /* "..." argument a:1, a:2, etc. */
23502 sprintf((char *)numbuf, "%d", ai + 1);
23503 name = numbuf;
23504 }
23505 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
23506 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023507 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023508 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23509 }
23510 else
23511 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023512 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23513 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000023514 if (v == NULL)
23515 break;
23516 v->di_flags = DI_FLAGS_RO;
23517 }
23518 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023519 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000023520
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023521 /* Note: the values are copied directly to avoid alloc/free.
23522 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023523 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023524 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023525
23526 if (ai >= 0 && ai < MAX_FUNC_ARGS)
23527 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023528 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
23529 fc->l_listitems[ai].li_tv = argvars[i];
23530 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023531 }
23532 }
23533
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534 /* Don't redraw while executing the function. */
23535 ++RedrawingDisabled;
23536 save_sourcing_name = sourcing_name;
23537 save_sourcing_lnum = sourcing_lnum;
23538 sourcing_lnum = 1;
23539 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023540 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 if (sourcing_name != NULL)
23542 {
23543 if (save_sourcing_name != NULL
23544 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
23545 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
23546 else
23547 STRCPY(sourcing_name, "function ");
23548 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
23549
23550 if (p_verbose >= 12)
23551 {
23552 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023553 verbose_enter_scroll();
23554
Bram Moolenaar555b2802005-05-19 21:08:39 +000023555 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556 if (p_verbose >= 14)
23557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023559 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023560 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023561 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023562
23563 msg_puts((char_u *)"(");
23564 for (i = 0; i < argcount; ++i)
23565 {
23566 if (i > 0)
23567 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023568 if (argvars[i].v_type == VAR_NUMBER)
23569 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570 else
23571 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020023572 /* Do not want errors such as E724 here. */
23573 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023574 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023575 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023576 if (s != NULL)
23577 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023578 if (vim_strsize(s) > MSG_BUF_CLEN)
23579 {
23580 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23581 s = buf;
23582 }
23583 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023584 vim_free(tofree);
23585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 }
23587 }
23588 msg_puts((char_u *)")");
23589 }
23590 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023591
23592 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 --no_wait_return;
23594 }
23595 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023596#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023597 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023598 {
23599 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
23600 func_do_profile(fp);
23601 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023602 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023603 {
23604 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023605 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023606 profile_zero(&fp->uf_tm_children);
23607 }
23608 script_prof_save(&wait_start);
23609 }
23610#endif
23611
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023613 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 save_did_emsg = did_emsg;
23615 did_emsg = FALSE;
23616
23617 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023618 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
23620
23621 --RedrawingDisabled;
23622
23623 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023624 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023626 clear_tv(rettv);
23627 rettv->v_type = VAR_NUMBER;
23628 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023629 }
23630
Bram Moolenaar05159a02005-02-26 23:04:13 +000023631#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023632 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023633 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000023634 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000023635 profile_end(&call_start);
23636 profile_sub_wait(&wait_start, &call_start);
23637 profile_add(&fp->uf_tm_total, &call_start);
23638 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023639 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023640 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023641 profile_add(&fc->caller->func->uf_tm_children, &call_start);
23642 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023643 }
23644 }
23645#endif
23646
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 /* when being verbose, mention the return value */
23648 if (p_verbose >= 12)
23649 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023651 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000023654 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023655 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000023656 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023657 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000023658 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000023660 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000023661 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000023662 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023663 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000023664
Bram Moolenaar555b2802005-05-19 21:08:39 +000023665 /* The value may be very long. Skip the middle part, so that we
23666 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020023667 * truncate it at the end. Don't want errors such as E724 here. */
23668 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023669 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020023670 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023671 if (s != NULL)
23672 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010023673 if (vim_strsize(s) > MSG_BUF_CLEN)
23674 {
23675 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
23676 s = buf;
23677 }
23678 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000023679 vim_free(tofree);
23680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 }
23682 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023683
23684 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 --no_wait_return;
23686 }
23687
23688 vim_free(sourcing_name);
23689 sourcing_name = save_sourcing_name;
23690 sourcing_lnum = save_sourcing_lnum;
23691 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023692#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023693 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023694 script_prof_restore(&wait_start);
23695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696
23697 if (p_verbose >= 12 && sourcing_name != NULL)
23698 {
23699 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023700 verbose_enter_scroll();
23701
Bram Moolenaar555b2802005-05-19 21:08:39 +000023702 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000023704
23705 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706 --no_wait_return;
23707 }
23708
23709 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023710 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023711 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023712
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023713 /* If the a:000 list and the l: and a: dicts are not referenced we can
23714 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023715 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
23716 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
23717 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
23718 {
23719 free_funccal(fc, FALSE);
23720 }
23721 else
23722 {
23723 hashitem_T *hi;
23724 listitem_T *li;
23725 int todo;
23726
23727 /* "fc" is still in use. This can happen when returning "a:000" or
23728 * assigning "l:" to a global variable.
23729 * Link "fc" in the list for garbage collection later. */
23730 fc->caller = previous_funccal;
23731 previous_funccal = fc;
23732
23733 /* Make a copy of the a: variables, since we didn't do that above. */
23734 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23735 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23736 {
23737 if (!HASHITEM_EMPTY(hi))
23738 {
23739 --todo;
23740 v = HI2DI(hi);
23741 copy_tv(&v->di_tv, &v->di_tv);
23742 }
23743 }
23744
23745 /* Make a copy of the a:000 items, since we didn't do that above. */
23746 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23747 copy_tv(&li->li_tv, &li->li_tv);
23748 }
23749}
23750
23751/*
23752 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023753 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023754 */
23755 static int
23756can_free_funccal(fc, copyID)
23757 funccall_T *fc;
23758 int copyID;
23759{
23760 return (fc->l_varlist.lv_copyID != copyID
23761 && fc->l_vars.dv_copyID != copyID
23762 && fc->l_avars.dv_copyID != copyID);
23763}
23764
23765/*
23766 * Free "fc" and what it contains.
23767 */
23768 static void
23769free_funccal(fc, free_val)
23770 funccall_T *fc;
23771 int free_val; /* a: vars were allocated */
23772{
23773 listitem_T *li;
23774
23775 /* The a: variables typevals may not have been allocated, only free the
23776 * allocated variables. */
23777 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23778
23779 /* free all l: variables */
23780 vars_clear(&fc->l_vars.dv_hashtab);
23781
23782 /* Free the a:000 variables if they were allocated. */
23783 if (free_val)
23784 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23785 clear_tv(&li->li_tv);
23786
23787 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788}
23789
23790/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023791 * Add a number variable "name" to dict "dp" with value "nr".
23792 */
23793 static void
23794add_nr_var(dp, v, name, nr)
23795 dict_T *dp;
23796 dictitem_T *v;
23797 char *name;
23798 varnumber_T nr;
23799{
23800 STRCPY(v->di_key, name);
23801 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23802 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23803 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023804 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023805 v->di_tv.vval.v_number = nr;
23806}
23807
23808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 * ":return [expr]"
23810 */
23811 void
23812ex_return(eap)
23813 exarg_T *eap;
23814{
23815 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023816 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 int returning = FALSE;
23818
23819 if (current_funccal == NULL)
23820 {
23821 EMSG(_("E133: :return not inside a function"));
23822 return;
23823 }
23824
23825 if (eap->skip)
23826 ++emsg_skip;
23827
23828 eap->nextcmd = NULL;
23829 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023830 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023831 {
23832 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023833 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023835 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836 }
23837 /* It's safer to return also on error. */
23838 else if (!eap->skip)
23839 {
23840 /*
23841 * Return unless the expression evaluation has been cancelled due to an
23842 * aborting error, an interrupt, or an exception.
23843 */
23844 if (!aborting())
23845 returning = do_return(eap, FALSE, TRUE, NULL);
23846 }
23847
23848 /* When skipping or the return gets pending, advance to the next command
23849 * in this line (!returning). Otherwise, ignore the rest of the line.
23850 * Following lines will be ignored by get_func_line(). */
23851 if (returning)
23852 eap->nextcmd = NULL;
23853 else if (eap->nextcmd == NULL) /* no argument */
23854 eap->nextcmd = check_nextcmd(arg);
23855
23856 if (eap->skip)
23857 --emsg_skip;
23858}
23859
23860/*
23861 * Return from a function. Possibly makes the return pending. Also called
23862 * for a pending return at the ":endtry" or after returning from an extra
23863 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023864 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023865 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 * FALSE when the return gets pending.
23867 */
23868 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023869do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870 exarg_T *eap;
23871 int reanimate;
23872 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023873 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874{
23875 int idx;
23876 struct condstack *cstack = eap->cstack;
23877
23878 if (reanimate)
23879 /* Undo the return. */
23880 current_funccal->returned = FALSE;
23881
23882 /*
23883 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23884 * not in its finally clause (which then is to be executed next) is found.
23885 * In this case, make the ":return" pending for execution at the ":endtry".
23886 * Otherwise, return normally.
23887 */
23888 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23889 if (idx >= 0)
23890 {
23891 cstack->cs_pending[idx] = CSTP_RETURN;
23892
23893 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023894 /* A pending return again gets pending. "rettv" points to an
23895 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023897 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 else
23899 {
23900 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023901 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023903 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023905 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 {
23907 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023908 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023909 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 else
23911 EMSG(_(e_outofmem));
23912 }
23913 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023914 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023915
23916 if (reanimate)
23917 {
23918 /* The pending return value could be overwritten by a ":return"
23919 * without argument in a finally clause; reset the default
23920 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023921 current_funccal->rettv->v_type = VAR_NUMBER;
23922 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 }
23924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023925 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 }
23927 else
23928 {
23929 current_funccal->returned = TRUE;
23930
23931 /* If the return is carried out now, store the return value. For
23932 * a return immediately after reanimation, the value is already
23933 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023934 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023936 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023937 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023939 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023940 }
23941 }
23942
23943 return idx < 0;
23944}
23945
23946/*
23947 * Free the variable with a pending return value.
23948 */
23949 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023950discard_pending_return(rettv)
23951 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952{
Bram Moolenaar33570922005-01-25 22:26:29 +000023953 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954}
23955
23956/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023957 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 * is an allocated string. Used by report_pending() for verbose messages.
23959 */
23960 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023961get_return_cmd(rettv)
23962 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023964 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023965 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023966 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023967
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023968 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023969 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023970 if (s == NULL)
23971 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023972
23973 STRCPY(IObuff, ":return ");
23974 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23975 if (STRLEN(s) + 8 >= IOSIZE)
23976 STRCPY(IObuff + IOSIZE - 4, "...");
23977 vim_free(tofree);
23978 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979}
23980
23981/*
23982 * Get next function line.
23983 * Called by do_cmdline() to get the next line.
23984 * Returns allocated string, or NULL for end of function.
23985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023986 char_u *
23987get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023988 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023989 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023990 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023991{
Bram Moolenaar33570922005-01-25 22:26:29 +000023992 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023993 ufunc_T *fp = fcp->func;
23994 char_u *retval;
23995 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023996
23997 /* If breakpoints have been added/deleted need to check for it. */
23998 if (fcp->dbg_tick != debug_tick)
23999 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024000 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 sourcing_lnum);
24002 fcp->dbg_tick = debug_tick;
24003 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024004#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024005 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024006 func_line_end(cookie);
24007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008
Bram Moolenaar05159a02005-02-26 23:04:13 +000024009 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024010 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
24011 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024012 retval = NULL;
24013 else
24014 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024015 /* Skip NULL lines (continuation lines). */
24016 while (fcp->linenr < gap->ga_len
24017 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
24018 ++fcp->linenr;
24019 if (fcp->linenr >= gap->ga_len)
24020 retval = NULL;
24021 else
24022 {
24023 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
24024 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024025#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024026 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024027 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024028#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024030 }
24031
24032 /* Did we encounter a breakpoint? */
24033 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
24034 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024035 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024037 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024038 sourcing_lnum);
24039 fcp->dbg_tick = debug_tick;
24040 }
24041
24042 return retval;
24043}
24044
Bram Moolenaar05159a02005-02-26 23:04:13 +000024045#if defined(FEAT_PROFILE) || defined(PROTO)
24046/*
24047 * Called when starting to read a function line.
24048 * "sourcing_lnum" must be correct!
24049 * When skipping lines it may not actually be executed, but we won't find out
24050 * until later and we need to store the time now.
24051 */
24052 void
24053func_line_start(cookie)
24054 void *cookie;
24055{
24056 funccall_T *fcp = (funccall_T *)cookie;
24057 ufunc_T *fp = fcp->func;
24058
24059 if (fp->uf_profiling && sourcing_lnum >= 1
24060 && sourcing_lnum <= fp->uf_lines.ga_len)
24061 {
24062 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024063 /* Skip continuation lines. */
24064 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
24065 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024066 fp->uf_tml_execed = FALSE;
24067 profile_start(&fp->uf_tml_start);
24068 profile_zero(&fp->uf_tml_children);
24069 profile_get_wait(&fp->uf_tml_wait);
24070 }
24071}
24072
24073/*
24074 * Called when actually executing a function line.
24075 */
24076 void
24077func_line_exec(cookie)
24078 void *cookie;
24079{
24080 funccall_T *fcp = (funccall_T *)cookie;
24081 ufunc_T *fp = fcp->func;
24082
24083 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24084 fp->uf_tml_execed = TRUE;
24085}
24086
24087/*
24088 * Called when done with a function line.
24089 */
24090 void
24091func_line_end(cookie)
24092 void *cookie;
24093{
24094 funccall_T *fcp = (funccall_T *)cookie;
24095 ufunc_T *fp = fcp->func;
24096
24097 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
24098 {
24099 if (fp->uf_tml_execed)
24100 {
24101 ++fp->uf_tml_count[fp->uf_tml_idx];
24102 profile_end(&fp->uf_tml_start);
24103 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024104 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000024105 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
24106 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024107 }
24108 fp->uf_tml_idx = -1;
24109 }
24110}
24111#endif
24112
Bram Moolenaar071d4272004-06-13 20:20:40 +000024113/*
24114 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024115 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116 */
24117 int
24118func_has_ended(cookie)
24119 void *cookie;
24120{
Bram Moolenaar33570922005-01-25 22:26:29 +000024121 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122
24123 /* Ignore the "abort" flag if the abortion behavior has been changed due to
24124 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024125 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126 || fcp->returned);
24127}
24128
24129/*
24130 * return TRUE if cookie indicates a function which "abort"s on errors.
24131 */
24132 int
24133func_has_abort(cookie)
24134 void *cookie;
24135{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024136 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137}
24138
24139#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
24140typedef enum
24141{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024142 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
24143 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
24144 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145} var_flavour_T;
24146
24147static var_flavour_T var_flavour __ARGS((char_u *varname));
24148
24149 static var_flavour_T
24150var_flavour(varname)
24151 char_u *varname;
24152{
24153 char_u *p = varname;
24154
24155 if (ASCII_ISUPPER(*p))
24156 {
24157 while (*(++p))
24158 if (ASCII_ISLOWER(*p))
24159 return VAR_FLAVOUR_SESSION;
24160 return VAR_FLAVOUR_VIMINFO;
24161 }
24162 else
24163 return VAR_FLAVOUR_DEFAULT;
24164}
24165#endif
24166
24167#if defined(FEAT_VIMINFO) || defined(PROTO)
24168/*
24169 * Restore global vars that start with a capital from the viminfo file
24170 */
24171 int
24172read_viminfo_varlist(virp, writing)
24173 vir_T *virp;
24174 int writing;
24175{
24176 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024177 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000024178 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179
24180 if (!writing && (find_viminfo_parameter('!') != NULL))
24181 {
24182 tab = vim_strchr(virp->vir_line + 1, '\t');
24183 if (tab != NULL)
24184 {
24185 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024186 switch (*tab)
24187 {
24188 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024189#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024190 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024191#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024192 case 'D': type = VAR_DICT; break;
24193 case 'L': type = VAR_LIST; break;
24194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195
24196 tab = vim_strchr(tab, '\t');
24197 if (tab != NULL)
24198 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024199 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024200 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024201 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024202 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024203#ifdef FEAT_FLOAT
24204 else if (type == VAR_FLOAT)
24205 (void)string2float(tab + 1, &tv.vval.v_float);
24206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024208 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024209 if (type == VAR_DICT || type == VAR_LIST)
24210 {
24211 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
24212
24213 if (etv == NULL)
24214 /* Failed to parse back the dict or list, use it as a
24215 * string. */
24216 tv.v_type = VAR_STRING;
24217 else
24218 {
24219 vim_free(tv.vval.v_string);
24220 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010024221 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024222 }
24223 }
24224
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024225 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024226
24227 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024228 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024229 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
24230 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231 }
24232 }
24233 }
24234
24235 return viminfo_readline(virp);
24236}
24237
24238/*
24239 * Write global vars that start with a capital to the viminfo file
24240 */
24241 void
24242write_viminfo_varlist(fp)
24243 FILE *fp;
24244{
Bram Moolenaar33570922005-01-25 22:26:29 +000024245 hashitem_T *hi;
24246 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024247 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024248 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024249 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024250 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000024251 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252
24253 if (find_viminfo_parameter('!') == NULL)
24254 return;
24255
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020024256 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000024257
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024258 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024259 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024261 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024263 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024264 this_var = HI2DI(hi);
24265 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024266 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024267 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000024268 {
24269 case VAR_STRING: s = "STR"; break;
24270 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024271#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024272 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024273#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020024274 case VAR_DICT: s = "DIC"; break;
24275 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000024276 default: continue;
24277 }
Bram Moolenaar33570922005-01-25 22:26:29 +000024278 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000024279 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024280 if (p != NULL)
24281 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000024282 vim_free(tofree);
24283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024284 }
24285 }
24286}
24287#endif
24288
24289#if defined(FEAT_SESSION) || defined(PROTO)
24290 int
24291store_session_globals(fd)
24292 FILE *fd;
24293{
Bram Moolenaar33570922005-01-25 22:26:29 +000024294 hashitem_T *hi;
24295 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000024296 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024297 char_u *p, *t;
24298
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024299 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000024300 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024302 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024304 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000024305 this_var = HI2DI(hi);
24306 if ((this_var->di_tv.v_type == VAR_NUMBER
24307 || this_var->di_tv.v_type == VAR_STRING)
24308 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000024309 {
Bram Moolenaara7043832005-01-21 11:56:39 +000024310 /* Escape special characters with a backslash. Turn a LF and
24311 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024312 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000024313 (char_u *)"\\\"\n\r");
24314 if (p == NULL) /* out of memory */
24315 break;
24316 for (t = p; *t != NUL; ++t)
24317 if (*t == '\n')
24318 *t = 'n';
24319 else if (*t == '\r')
24320 *t = 'r';
24321 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000024322 this_var->di_key,
24323 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24324 : ' ',
24325 p,
24326 (this_var->di_tv.v_type == VAR_STRING) ? '"'
24327 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000024328 || put_eol(fd) == FAIL)
24329 {
24330 vim_free(p);
24331 return FAIL;
24332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333 vim_free(p);
24334 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024335#ifdef FEAT_FLOAT
24336 else if (this_var->di_tv.v_type == VAR_FLOAT
24337 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
24338 {
24339 float_T f = this_var->di_tv.vval.v_float;
24340 int sign = ' ';
24341
24342 if (f < 0)
24343 {
24344 f = -f;
24345 sign = '-';
24346 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010024347 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024348 this_var->di_key, sign, f) < 0)
24349 || put_eol(fd) == FAIL)
24350 return FAIL;
24351 }
24352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353 }
24354 }
24355 return OK;
24356}
24357#endif
24358
Bram Moolenaar661b1822005-07-28 22:36:45 +000024359/*
24360 * Display script name where an item was last set.
24361 * Should only be invoked when 'verbose' is non-zero.
24362 */
24363 void
24364last_set_msg(scriptID)
24365 scid_T scriptID;
24366{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024367 char_u *p;
24368
Bram Moolenaar661b1822005-07-28 22:36:45 +000024369 if (scriptID != 0)
24370 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024371 p = home_replace_save(NULL, get_scriptname(scriptID));
24372 if (p != NULL)
24373 {
24374 verbose_enter();
24375 MSG_PUTS(_("\n\tLast set from "));
24376 MSG_PUTS(p);
24377 vim_free(p);
24378 verbose_leave();
24379 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000024380 }
24381}
24382
Bram Moolenaard812df62008-11-09 12:46:09 +000024383/*
24384 * List v:oldfiles in a nice way.
24385 */
Bram Moolenaard812df62008-11-09 12:46:09 +000024386 void
24387ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000024388 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000024389{
24390 list_T *l = vimvars[VV_OLDFILES].vv_list;
24391 listitem_T *li;
24392 int nr = 0;
24393
24394 if (l == NULL)
24395 msg((char_u *)_("No old files"));
24396 else
24397 {
24398 msg_start();
24399 msg_scroll = TRUE;
24400 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
24401 {
24402 msg_outnum((long)++nr);
24403 MSG_PUTS(": ");
24404 msg_outtrans(get_tv_string(&li->li_tv));
24405 msg_putchar('\n');
24406 out_flush(); /* output one line at a time */
24407 ui_breakcheck();
24408 }
24409 /* Assume "got_int" was set to truncate the listing. */
24410 got_int = FALSE;
24411
24412#ifdef FEAT_BROWSE_CMD
24413 if (cmdmod.browse)
24414 {
24415 quit_more = FALSE;
24416 nr = prompt_for_number(FALSE);
24417 msg_starthere();
24418 if (nr > 0)
24419 {
24420 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
24421 (long)nr);
24422
24423 if (p != NULL)
24424 {
24425 p = expand_env_save(p);
24426 eap->arg = p;
24427 eap->cmdidx = CMD_edit;
24428 cmdmod.browse = FALSE;
24429 do_exedit(eap, NULL);
24430 vim_free(p);
24431 }
24432 }
24433 }
24434#endif
24435 }
24436}
24437
Bram Moolenaar071d4272004-06-13 20:20:40 +000024438#endif /* FEAT_EVAL */
24439
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024441#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442
24443#ifdef WIN3264
24444/*
24445 * Functions for ":8" filename modifier: get 8.3 version of a filename.
24446 */
24447static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24448static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
24449static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
24450
24451/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024452 * Get the short path (8.3) for the filename in "fnamep".
24453 * Only works for a valid file name.
24454 * When the path gets longer "fnamep" is changed and the allocated buffer
24455 * is put in "bufp".
24456 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
24457 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024458 */
24459 static int
24460get_short_pathname(fnamep, bufp, fnamelen)
24461 char_u **fnamep;
24462 char_u **bufp;
24463 int *fnamelen;
24464{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024465 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 char_u *newbuf;
24467
24468 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024469 l = GetShortPathName(*fnamep, *fnamep, len);
24470 if (l > len - 1)
24471 {
24472 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024473 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 newbuf = vim_strnsave(*fnamep, l);
24475 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024476 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477
24478 vim_free(*bufp);
24479 *fnamep = *bufp = newbuf;
24480
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024481 /* Really should always succeed, as the buffer is big enough. */
24482 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483 }
24484
24485 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024486 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487}
24488
24489/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024490 * Get the short path (8.3) for the filename in "fname". The converted
24491 * path is returned in "bufp".
24492 *
24493 * Some of the directories specified in "fname" may not exist. This function
24494 * will shorten the existing directories at the beginning of the path and then
24495 * append the remaining non-existing path.
24496 *
24497 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020024498 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024499 * bufp - Pointer to an allocated buffer for the filename.
24500 * fnamelen - Length of the filename pointed to by fname
24501 *
24502 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503 */
24504 static int
24505shortpath_for_invalid_fname(fname, bufp, fnamelen)
24506 char_u **fname;
24507 char_u **bufp;
24508 int *fnamelen;
24509{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024510 char_u *short_fname, *save_fname, *pbuf_unused;
24511 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024513 int old_len, len;
24514 int new_len, sfx_len;
24515 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024516
24517 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024518 old_len = *fnamelen;
24519 save_fname = vim_strnsave(*fname, old_len);
24520 pbuf_unused = NULL;
24521 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024522
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024523 endp = save_fname + old_len - 1; /* Find the end of the copy */
24524 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024525
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024526 /*
24527 * Try shortening the supplied path till it succeeds by removing one
24528 * directory at a time from the tail of the path.
24529 */
24530 len = 0;
24531 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024532 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024533 /* go back one path-separator */
24534 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
24535 --endp;
24536 if (endp <= save_fname)
24537 break; /* processed the complete path */
24538
24539 /*
24540 * Replace the path separator with a NUL and try to shorten the
24541 * resulting path.
24542 */
24543 ch = *endp;
24544 *endp = 0;
24545 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000024546 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024547 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
24548 {
24549 retval = FAIL;
24550 goto theend;
24551 }
24552 *endp = ch; /* preserve the string */
24553
24554 if (len > 0)
24555 break; /* successfully shortened the path */
24556
24557 /* failed to shorten the path. Skip the path separator */
24558 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024559 }
24560
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024561 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024563 /*
24564 * Succeeded in shortening the path. Now concatenate the shortened
24565 * path with the remaining path at the tail.
24566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024567
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024568 /* Compute the length of the new path. */
24569 sfx_len = (int)(save_endp - endp) + 1;
24570 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024571
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024572 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024573 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024574 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024576 /* There is not enough space in the currently allocated string,
24577 * copy it to a buffer big enough. */
24578 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024579 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024580 {
24581 retval = FAIL;
24582 goto theend;
24583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 }
24585 else
24586 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024587 /* Transfer short_fname to the main buffer (it's big enough),
24588 * unless get_short_pathname() did its work in-place. */
24589 *fname = *bufp = save_fname;
24590 if (short_fname != save_fname)
24591 vim_strncpy(save_fname, short_fname, len);
24592 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024594
24595 /* concat the not-shortened part of the path */
24596 vim_strncpy(*fname + len, endp, sfx_len);
24597 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024598 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024599
24600theend:
24601 vim_free(pbuf_unused);
24602 vim_free(save_fname);
24603
24604 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024605}
24606
24607/*
24608 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024609 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024610 */
24611 static int
24612shortpath_for_partial(fnamep, bufp, fnamelen)
24613 char_u **fnamep;
24614 char_u **bufp;
24615 int *fnamelen;
24616{
24617 int sepcount, len, tflen;
24618 char_u *p;
24619 char_u *pbuf, *tfname;
24620 int hasTilde;
24621
Bram Moolenaar8c8de832008-06-24 22:58:06 +000024622 /* Count up the path separators from the RHS.. so we know which part
24623 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024625 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 if (vim_ispathsep(*p))
24627 ++sepcount;
24628
24629 /* Need full path first (use expand_env() to remove a "~/") */
24630 hasTilde = (**fnamep == '~');
24631 if (hasTilde)
24632 pbuf = tfname = expand_env_save(*fnamep);
24633 else
24634 pbuf = tfname = FullName_save(*fnamep, FALSE);
24635
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024636 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024637
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024638 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
24639 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024640
24641 if (len == 0)
24642 {
24643 /* Don't have a valid filename, so shorten the rest of the
24644 * path if we can. This CAN give us invalid 8.3 filenames, but
24645 * there's not a lot of point in guessing what it might be.
24646 */
24647 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024648 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
24649 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 }
24651
24652 /* Count the paths backward to find the beginning of the desired string. */
24653 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024654 {
24655#ifdef FEAT_MBYTE
24656 if (has_mbyte)
24657 p -= mb_head_off(tfname, p);
24658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024659 if (vim_ispathsep(*p))
24660 {
24661 if (sepcount == 0 || (hasTilde && sepcount == 1))
24662 break;
24663 else
24664 sepcount --;
24665 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 if (hasTilde)
24668 {
24669 --p;
24670 if (p >= tfname)
24671 *p = '~';
24672 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024673 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024674 }
24675 else
24676 ++p;
24677
24678 /* Copy in the string - p indexes into tfname - allocated at pbuf */
24679 vim_free(*bufp);
24680 *fnamelen = (int)STRLEN(p);
24681 *bufp = pbuf;
24682 *fnamep = p;
24683
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024684 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685}
24686#endif /* WIN3264 */
24687
24688/*
24689 * Adjust a filename, according to a string of modifiers.
24690 * *fnamep must be NUL terminated when called. When returning, the length is
24691 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024692 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 * When there is an error, *fnamep is set to NULL.
24694 */
24695 int
24696modify_fname(src, usedlen, fnamep, bufp, fnamelen)
24697 char_u *src; /* string with modifiers */
24698 int *usedlen; /* characters after src that are used */
24699 char_u **fnamep; /* file name so far */
24700 char_u **bufp; /* buffer for allocated file name or NULL */
24701 int *fnamelen; /* length of fnamep */
24702{
24703 int valid = 0;
24704 char_u *tail;
24705 char_u *s, *p, *pbuf;
24706 char_u dirname[MAXPATHL];
24707 int c;
24708 int has_fullname = 0;
24709#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024710 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024711 int has_shortname = 0;
24712#endif
24713
24714repeat:
24715 /* ":p" - full path/file_name */
24716 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
24717 {
24718 has_fullname = 1;
24719
24720 valid |= VALID_PATH;
24721 *usedlen += 2;
24722
24723 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24724 if ((*fnamep)[0] == '~'
24725#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24726 && ((*fnamep)[1] == '/'
24727# ifdef BACKSLASH_IN_FILENAME
24728 || (*fnamep)[1] == '\\'
24729# endif
24730 || (*fnamep)[1] == NUL)
24731
24732#endif
24733 )
24734 {
24735 *fnamep = expand_env_save(*fnamep);
24736 vim_free(*bufp); /* free any allocated file name */
24737 *bufp = *fnamep;
24738 if (*fnamep == NULL)
24739 return -1;
24740 }
24741
24742 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024743 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024744 {
24745 if (vim_ispathsep(*p)
24746 && p[1] == '.'
24747 && (p[2] == NUL
24748 || vim_ispathsep(p[2])
24749 || (p[2] == '.'
24750 && (p[3] == NUL || vim_ispathsep(p[3])))))
24751 break;
24752 }
24753
24754 /* FullName_save() is slow, don't use it when not needed. */
24755 if (*p != NUL || !vim_isAbsName(*fnamep))
24756 {
24757 *fnamep = FullName_save(*fnamep, *p != NUL);
24758 vim_free(*bufp); /* free any allocated file name */
24759 *bufp = *fnamep;
24760 if (*fnamep == NULL)
24761 return -1;
24762 }
24763
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024764#ifdef WIN3264
24765# if _WIN32_WINNT >= 0x0500
24766 if (vim_strchr(*fnamep, '~') != NULL)
24767 {
24768 /* Expand 8.3 filename to full path. Needed to make sure the same
24769 * file does not have two different names.
24770 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24771 p = alloc(_MAX_PATH + 1);
24772 if (p != NULL)
24773 {
Bram Moolenaar4c7b2f52014-11-19 18:03:28 +010024774 if (GetLongPathName(*fnamep, p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024775 {
24776 vim_free(*bufp);
24777 *bufp = *fnamep = p;
24778 }
24779 else
24780 vim_free(p);
24781 }
24782 }
24783# endif
24784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785 /* Append a path separator to a directory. */
24786 if (mch_isdir(*fnamep))
24787 {
24788 /* Make room for one or two extra characters. */
24789 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24790 vim_free(*bufp); /* free any allocated file name */
24791 *bufp = *fnamep;
24792 if (*fnamep == NULL)
24793 return -1;
24794 add_pathsep(*fnamep);
24795 }
24796 }
24797
24798 /* ":." - path relative to the current directory */
24799 /* ":~" - path relative to the home directory */
24800 /* ":8" - shortname path - postponed till after */
24801 while (src[*usedlen] == ':'
24802 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24803 {
24804 *usedlen += 2;
24805 if (c == '8')
24806 {
24807#ifdef WIN3264
24808 has_shortname = 1; /* Postpone this. */
24809#endif
24810 continue;
24811 }
24812 pbuf = NULL;
24813 /* Need full path first (use expand_env() to remove a "~/") */
24814 if (!has_fullname)
24815 {
24816 if (c == '.' && **fnamep == '~')
24817 p = pbuf = expand_env_save(*fnamep);
24818 else
24819 p = pbuf = FullName_save(*fnamep, FALSE);
24820 }
24821 else
24822 p = *fnamep;
24823
24824 has_fullname = 0;
24825
24826 if (p != NULL)
24827 {
24828 if (c == '.')
24829 {
24830 mch_dirname(dirname, MAXPATHL);
24831 s = shorten_fname(p, dirname);
24832 if (s != NULL)
24833 {
24834 *fnamep = s;
24835 if (pbuf != NULL)
24836 {
24837 vim_free(*bufp); /* free any allocated file name */
24838 *bufp = pbuf;
24839 pbuf = NULL;
24840 }
24841 }
24842 }
24843 else
24844 {
24845 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24846 /* Only replace it when it starts with '~' */
24847 if (*dirname == '~')
24848 {
24849 s = vim_strsave(dirname);
24850 if (s != NULL)
24851 {
24852 *fnamep = s;
24853 vim_free(*bufp);
24854 *bufp = s;
24855 }
24856 }
24857 }
24858 vim_free(pbuf);
24859 }
24860 }
24861
24862 tail = gettail(*fnamep);
24863 *fnamelen = (int)STRLEN(*fnamep);
24864
24865 /* ":h" - head, remove "/file_name", can be repeated */
24866 /* Don't remove the first "/" or "c:\" */
24867 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24868 {
24869 valid |= VALID_HEAD;
24870 *usedlen += 2;
24871 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024872 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024873 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024874 *fnamelen = (int)(tail - *fnamep);
24875#ifdef VMS
24876 if (*fnamelen > 0)
24877 *fnamelen += 1; /* the path separator is part of the path */
24878#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024879 if (*fnamelen == 0)
24880 {
24881 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24882 p = vim_strsave((char_u *)".");
24883 if (p == NULL)
24884 return -1;
24885 vim_free(*bufp);
24886 *bufp = *fnamep = tail = p;
24887 *fnamelen = 1;
24888 }
24889 else
24890 {
24891 while (tail > s && !after_pathsep(s, tail))
24892 mb_ptr_back(*fnamep, tail);
24893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 }
24895
24896 /* ":8" - shortname */
24897 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24898 {
24899 *usedlen += 2;
24900#ifdef WIN3264
24901 has_shortname = 1;
24902#endif
24903 }
24904
24905#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024906 /*
24907 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908 */
24909 if (has_shortname)
24910 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024911 /* Copy the string if it is shortened by :h and when it wasn't copied
24912 * yet, because we are going to change it in place. Avoids changing
24913 * the buffer name for "%:8". */
24914 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024915 {
24916 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024917 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024918 return -1;
24919 vim_free(*bufp);
24920 *bufp = *fnamep = p;
24921 }
24922
24923 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024924 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 if (!has_fullname && !vim_isAbsName(*fnamep))
24926 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024927 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 return -1;
24929 }
24930 else
24931 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024932 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024933
Bram Moolenaardc935552011-08-17 15:23:23 +020024934 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024936 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 return -1;
24938
24939 if (l == 0)
24940 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024941 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024942 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024943 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024944 return -1;
24945 }
24946 *fnamelen = l;
24947 }
24948 }
24949#endif /* WIN3264 */
24950
24951 /* ":t" - tail, just the basename */
24952 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24953 {
24954 *usedlen += 2;
24955 *fnamelen -= (int)(tail - *fnamep);
24956 *fnamep = tail;
24957 }
24958
24959 /* ":e" - extension, can be repeated */
24960 /* ":r" - root, without extension, can be repeated */
24961 while (src[*usedlen] == ':'
24962 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24963 {
24964 /* find a '.' in the tail:
24965 * - for second :e: before the current fname
24966 * - otherwise: The last '.'
24967 */
24968 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24969 s = *fnamep - 2;
24970 else
24971 s = *fnamep + *fnamelen - 1;
24972 for ( ; s > tail; --s)
24973 if (s[0] == '.')
24974 break;
24975 if (src[*usedlen + 1] == 'e') /* :e */
24976 {
24977 if (s > tail)
24978 {
24979 *fnamelen += (int)(*fnamep - (s + 1));
24980 *fnamep = s + 1;
24981#ifdef VMS
24982 /* cut version from the extension */
24983 s = *fnamep + *fnamelen - 1;
24984 for ( ; s > *fnamep; --s)
24985 if (s[0] == ';')
24986 break;
24987 if (s > *fnamep)
24988 *fnamelen = s - *fnamep;
24989#endif
24990 }
24991 else if (*fnamep <= tail)
24992 *fnamelen = 0;
24993 }
24994 else /* :r */
24995 {
24996 if (s > tail) /* remove one extension */
24997 *fnamelen = (int)(s - *fnamep);
24998 }
24999 *usedlen += 2;
25000 }
25001
25002 /* ":s?pat?foo?" - substitute */
25003 /* ":gs?pat?foo?" - global substitute */
25004 if (src[*usedlen] == ':'
25005 && (src[*usedlen + 1] == 's'
25006 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
25007 {
25008 char_u *str;
25009 char_u *pat;
25010 char_u *sub;
25011 int sep;
25012 char_u *flags;
25013 int didit = FALSE;
25014
25015 flags = (char_u *)"";
25016 s = src + *usedlen + 2;
25017 if (src[*usedlen + 1] == 'g')
25018 {
25019 flags = (char_u *)"g";
25020 ++s;
25021 }
25022
25023 sep = *s++;
25024 if (sep)
25025 {
25026 /* find end of pattern */
25027 p = vim_strchr(s, sep);
25028 if (p != NULL)
25029 {
25030 pat = vim_strnsave(s, (int)(p - s));
25031 if (pat != NULL)
25032 {
25033 s = p + 1;
25034 /* find end of substitution */
25035 p = vim_strchr(s, sep);
25036 if (p != NULL)
25037 {
25038 sub = vim_strnsave(s, (int)(p - s));
25039 str = vim_strnsave(*fnamep, *fnamelen);
25040 if (sub != NULL && str != NULL)
25041 {
25042 *usedlen = (int)(p + 1 - src);
25043 s = do_string_sub(str, pat, sub, flags);
25044 if (s != NULL)
25045 {
25046 *fnamep = s;
25047 *fnamelen = (int)STRLEN(s);
25048 vim_free(*bufp);
25049 *bufp = s;
25050 didit = TRUE;
25051 }
25052 }
25053 vim_free(sub);
25054 vim_free(str);
25055 }
25056 vim_free(pat);
25057 }
25058 }
25059 /* after using ":s", repeat all the modifiers */
25060 if (didit)
25061 goto repeat;
25062 }
25063 }
25064
Bram Moolenaar26df0922014-02-23 23:39:13 +010025065 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
25066 {
25067 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
25068 if (p == NULL)
25069 return -1;
25070 vim_free(*bufp);
25071 *bufp = *fnamep = p;
25072 *fnamelen = (int)STRLEN(p);
25073 *usedlen += 2;
25074 }
25075
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 return valid;
25077}
25078
25079/*
25080 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
25081 * "flags" can be "g" to do a global substitute.
25082 * Returns an allocated string, NULL for error.
25083 */
25084 char_u *
25085do_string_sub(str, pat, sub, flags)
25086 char_u *str;
25087 char_u *pat;
25088 char_u *sub;
25089 char_u *flags;
25090{
25091 int sublen;
25092 regmatch_T regmatch;
25093 int i;
25094 int do_all;
25095 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025096 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097 garray_T ga;
25098 char_u *ret;
25099 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025100 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101
25102 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
25103 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025104 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025105
25106 ga_init2(&ga, 1, 200);
25107
25108 do_all = (flags[0] == 'g');
25109
25110 regmatch.rm_ic = p_ic;
25111 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
25112 if (regmatch.regprog != NULL)
25113 {
25114 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010025115 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
25117 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010025118 /* Skip empty match except for first match. */
25119 if (regmatch.startp[0] == regmatch.endp[0])
25120 {
25121 if (zero_width == regmatch.startp[0])
25122 {
25123 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020025124 i = MB_PTR2LEN(tail);
25125 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
25126 (size_t)i);
25127 ga.ga_len += i;
25128 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010025129 continue;
25130 }
25131 zero_width = regmatch.startp[0];
25132 }
25133
Bram Moolenaar071d4272004-06-13 20:20:40 +000025134 /*
25135 * Get some space for a temporary buffer to do the substitution
25136 * into. It will contain:
25137 * - The text up to where the match is.
25138 * - The substituted text.
25139 * - The text after the match.
25140 */
25141 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010025142 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000025143 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
25144 {
25145 ga_clear(&ga);
25146 break;
25147 }
25148
25149 /* copy the text up to where the match is */
25150 i = (int)(regmatch.startp[0] - tail);
25151 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
25152 /* add the substituted text */
25153 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
25154 + ga.ga_len + i, TRUE, TRUE, FALSE);
25155 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020025156 tail = regmatch.endp[0];
25157 if (*tail == NUL)
25158 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159 if (!do_all)
25160 break;
25161 }
25162
25163 if (ga.ga_data != NULL)
25164 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
25165
Bram Moolenaar473de612013-06-08 18:19:48 +020025166 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025167 }
25168
25169 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
25170 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000025171 if (p_cpo == empty_option)
25172 p_cpo = save_cpo;
25173 else
25174 /* Darn, evaluating {sub} expression changed the value. */
25175 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025176
25177 return ret;
25178}
25179
25180#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */