blob: 65f4b1138c5553dd2a8dc1a49578e72782170c0b [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
137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000364};
365
366/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000367#define vv_type vv_di.di_tv.v_type
368#define vv_nr vv_di.di_tv.vval.v_number
369#define vv_float vv_di.di_tv.vval.v_float
370#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000371#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000372#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000373
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200374static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000375#define vimvarht vimvardict.dv_hashtab
376
Bram Moolenaara40058a2005-07-11 22:42:07 +0000377static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
378static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000379static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
380static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
381static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000382static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
383static void list_glob_vars __ARGS((int *first));
384static void list_buf_vars __ARGS((int *first));
385static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000386#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000387static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000388#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000389static void list_vim_vars __ARGS((int *first));
390static void list_script_vars __ARGS((int *first));
391static void list_func_vars __ARGS((int *first));
392static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
394static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100395static 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 +0000396static void clear_lval __ARGS((lval_T *lp));
397static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
398static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000399static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
400static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
401static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
402static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
403static void item_lock __ARGS((typval_T *tv, int deep, int lock));
404static int tv_islocked __ARGS((typval_T *tv));
405
Bram Moolenaar33570922005-01-25 22:26:29 +0000406static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
407static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
409static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
410static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
411static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000412static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
413static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000414
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000415static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
417static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
418static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000420static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000421static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100422static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
423static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
424static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000425static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000426static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000427static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
429static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000430static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000431static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100432static 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 +0000433static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000434static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200435static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
437static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000438static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000440static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000442static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
443static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000445#ifdef FEAT_FLOAT
446static int string2float __ARGS((char_u *text, float_T *value));
447#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
449static int find_internal_func __ARGS((char_u *name));
450static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
451static 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 +0200452static 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 +0000453static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000454static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000455
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#ifdef FEAT_FLOAT
457static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200458static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000459#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000460static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100461static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000466#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200467static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200469static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100480static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000481static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100482static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#ifdef FEAT_FLOAT
485static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
486#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000487static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000490static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000491static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000492#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000493static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000494static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
496#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000497static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#ifdef FEAT_FLOAT
500static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200501static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000502#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000503static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
506static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200517#ifdef FEAT_FLOAT
518static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
519#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000522static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000528#ifdef FEAT_FLOAT
529static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200531static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000533static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000542static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000544static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000545static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000550static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000551static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000558static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000559static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000560static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000561static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200564static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000565static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000573static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000587static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100592static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000594static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000595static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000606#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200607static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000608static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
609#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200610#ifdef FEAT_LUA
611static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
612#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000617static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000618static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000619static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000621static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000622static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000625#ifdef vim_mkdir
626static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100629#ifdef FEAT_MZSCHEME
630static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
631#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000632static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100634static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000635static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000636#ifdef FEAT_FLOAT
637static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
638#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000639static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000640static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000641static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200642#ifdef FEAT_PYTHON3
643static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
645#ifdef FEAT_PYTHON
646static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000649static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000650static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000662#ifdef FEAT_FLOAT
663static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
664#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200665static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100667static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000670static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000672static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
676static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000679static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000680static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000681static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000682static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200684static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000685static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000686static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100687#ifdef FEAT_CRYPT
688static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
689#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000690static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200691static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#ifdef FEAT_FLOAT
694static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200695static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000698static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000699static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000706static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200707static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708#ifdef HAVE_STRFTIME
709static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
710#endif
711static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200717static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200718static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000724static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200725static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000728static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000729static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000730static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000731static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000733static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200734#ifdef FEAT_FLOAT
735static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
737#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000741#ifdef FEAT_FLOAT
742static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
743#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200745static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200746static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100750static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100761static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000763static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000764static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000765static int get_env_len __ARGS((char_u **arg));
766static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000767static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000768static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
769#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
770#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
771 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000772static 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 +0000773static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000774static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100775static 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 +0000776static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static typval_T *alloc_tv __ARGS((void));
778static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000779static void init_tv __ARGS((typval_T *varp));
780static long get_tv_number __ARGS((typval_T *varp));
781static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000782static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static char_u *get_tv_string __ARGS((typval_T *varp));
784static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000785static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100786static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp, int no_autoload));
787static 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 +0000788static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
789static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
790static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000791static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
792static 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 +0000793static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
794static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000795static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200796static int var_check_func_name __ARGS((char_u *name, int new_var));
797static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000798static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000799static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
801static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
802static int eval_fname_script __ARGS((char_u *p));
803static int eval_fname_sid __ARGS((char_u *p));
804static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000805static ufunc_T *find_func __ARGS((char_u *name));
806static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000807static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
809static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000810static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
811static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_total_cmp __ARGS((const void *s1, const void *s2));
817static int
818# ifdef __BORLANDC__
819 _RTLENTRYF
820# endif
821 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000822#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200823static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000824static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000825static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000826static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static 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 +0000828static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
829static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000830static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000831static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
832static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000833static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000834static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000835static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000836
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200837
838#ifdef EBCDIC
839static int compare_func_name __ARGS((const void *s1, const void *s2));
840static void sortFunctions __ARGS(());
841#endif
842
Bram Moolenaar33570922005-01-25 22:26:29 +0000843/*
844 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000845 */
846 void
847eval_init()
848{
Bram Moolenaar33570922005-01-25 22:26:29 +0000849 int i;
850 struct vimvar *p;
851
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200852 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
853 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200854 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000855 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000856 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000857
858 for (i = 0; i < VV_LEN; ++i)
859 {
860 p = &vimvars[i];
861 STRCPY(p->vv_di.di_key, p->vv_name);
862 if (p->vv_flags & VV_RO)
863 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
864 else if (p->vv_flags & VV_RO_SBX)
865 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
866 else
867 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000868
869 /* add to v: scope dict, unless the value is not always available */
870 if (p->vv_type != VAR_UNKNOWN)
871 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000873 /* add to compat scope dict */
874 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000876 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100877 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200878 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100882 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100914# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200915 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100916# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000917
918 /* global variables */
919 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000920
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000921 /* autoloaded script names */
922 ga_clear_strings(&ga_loaded);
923
Bram Moolenaarcca74132013-09-25 21:00:28 +0200924 /* Script-local variables. First clear all the variables and in a second
925 * loop free the scriptvar_T, because a variable in one script might hold
926 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200927 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200928 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200929 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200930 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200931 ga_clear(&ga_scripts);
932
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000933 /* unreferenced lists and dicts */
934 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000935
936 /* functions */
937 free_all_functions();
938 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000939}
940#endif
941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 * Return the name of the executed function.
944 */
945 char_u *
946func_name(cookie)
947 void *cookie;
948{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000949 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950}
951
952/*
953 * Return the address holding the next breakpoint line for a funccall cookie.
954 */
955 linenr_T *
956func_breakpoint(cookie)
957 void *cookie;
958{
Bram Moolenaar33570922005-01-25 22:26:29 +0000959 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960}
961
962/*
963 * Return the address holding the debug tick for a funccall cookie.
964 */
965 int *
966func_dbg_tick(cookie)
967 void *cookie;
968{
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970}
971
972/*
973 * Return the nesting level for a funccall cookie.
974 */
975 int
976func_level(cookie)
977 void *cookie;
978{
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980}
981
982/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000983funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000985/* pointer to list of previously used funccal, still around because some
986 * item in it is still being used. */
987funccall_T *previous_funccal = NULL;
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989/*
990 * Return TRUE when a function was ended by a ":return" command.
991 */
992 int
993current_func_returned()
994{
995 return current_funccal->returned;
996}
997
998
999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 * Set an internal variable to a string value. Creates the variable if it does
1001 * not already exist.
1002 */
1003 void
1004set_internal_string_var(name, value)
1005 char_u *name;
1006 char_u *value;
1007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001009 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011 val = vim_strsave(value);
1012 if (val != NULL)
1013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001015 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001017 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001018 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020 }
1021}
1022
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001024static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025static char_u *redir_endp = NULL;
1026static char_u *redir_varname = NULL;
1027
1028/*
1029 * Start recording command output to a variable
1030 * Returns OK if successfully completed the setup. FAIL otherwise.
1031 */
1032 int
1033var_redir_start(name, append)
1034 char_u *name;
1035 int append; /* append to an existing variable */
1036{
1037 int save_emsg;
1038 int err;
1039 typval_T tv;
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001042 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 {
1044 EMSG(_(e_invarg));
1045 return FAIL;
1046 }
1047
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001048 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001049 redir_varname = vim_strsave(name);
1050 if (redir_varname == NULL)
1051 return FAIL;
1052
1053 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1054 if (redir_lval == NULL)
1055 {
1056 var_redir_stop();
1057 return FAIL;
1058 }
1059
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001060 /* The output is stored in growarray "redir_ga" until redirection ends. */
1061 ga_init2(&redir_ga, (int)sizeof(char), 500);
1062
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001064 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001065 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001066 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1067 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001068 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 if (redir_endp != NULL && *redir_endp != NUL)
1070 /* Trailing characters are present after the variable name */
1071 EMSG(_(e_trailing));
1072 else
1073 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001074 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 var_redir_stop();
1076 return FAIL;
1077 }
1078
1079 /* check if we can write to the variable: set it to or append an empty
1080 * string */
1081 save_emsg = did_emsg;
1082 did_emsg = FALSE;
1083 tv.v_type = VAR_STRING;
1084 tv.vval.v_string = (char_u *)"";
1085 if (append)
1086 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1087 else
1088 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001089 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001091 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092 if (err)
1093 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001094 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095 var_redir_stop();
1096 return FAIL;
1097 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098
1099 return OK;
1100}
1101
1102/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103 * Append "value[value_len]" to the variable set by var_redir_start().
1104 * The actual appending is postponed until redirection ends, because the value
1105 * appended may in fact be the string we write to, changing it may cause freed
1106 * memory to be used:
1107 * :redir => foo
1108 * :let foo
1109 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 */
1111 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001112var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117
1118 if (redir_lval == NULL)
1119 return;
1120
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001126 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001127 {
1128 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001129 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001130 }
1131 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133}
1134
1135/*
1136 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001137 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 */
1139 void
1140var_redir_stop()
1141{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001142 typval_T tv;
1143
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_lval != NULL)
1145 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001146 /* If there was no error: assign the text to the variable. */
1147 if (redir_endp != NULL)
1148 {
1149 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1150 tv.v_type = VAR_STRING;
1151 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001152 /* Call get_lval() again, if it's inside a Dict or List it may
1153 * have changed. */
1154 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001155 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001156 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1157 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1158 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001159 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001161 /* free the collected output */
1162 vim_free(redir_ga.ga_data);
1163 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001164
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001165 vim_free(redir_lval);
1166 redir_lval = NULL;
1167 }
1168 vim_free(redir_varname);
1169 redir_varname = NULL;
1170}
1171
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172# if defined(FEAT_MBYTE) || defined(PROTO)
1173 int
1174eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1175 char_u *enc_from;
1176 char_u *enc_to;
1177 char_u *fname_from;
1178 char_u *fname_to;
1179{
1180 int err = FALSE;
1181
1182 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1183 set_vim_var_string(VV_CC_TO, enc_to, -1);
1184 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1185 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1186 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1187 err = TRUE;
1188 set_vim_var_string(VV_CC_FROM, NULL, -1);
1189 set_vim_var_string(VV_CC_TO, NULL, -1);
1190 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1191 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1192
1193 if (err)
1194 return FAIL;
1195 return OK;
1196}
1197# endif
1198
1199# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1200 int
1201eval_printexpr(fname, args)
1202 char_u *fname;
1203 char_u *args;
1204{
1205 int err = FALSE;
1206
1207 set_vim_var_string(VV_FNAME_IN, fname, -1);
1208 set_vim_var_string(VV_CMDARG, args, -1);
1209 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1210 err = TRUE;
1211 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1212 set_vim_var_string(VV_CMDARG, NULL, -1);
1213
1214 if (err)
1215 {
1216 mch_remove(fname);
1217 return FAIL;
1218 }
1219 return OK;
1220}
1221# endif
1222
1223# if defined(FEAT_DIFF) || defined(PROTO)
1224 void
1225eval_diff(origfile, newfile, outfile)
1226 char_u *origfile;
1227 char_u *newfile;
1228 char_u *outfile;
1229{
1230 int err = FALSE;
1231
1232 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1233 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1234 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1235 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1236 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1237 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1238 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1239}
1240
1241 void
1242eval_patch(origfile, difffile, outfile)
1243 char_u *origfile;
1244 char_u *difffile;
1245 char_u *outfile;
1246{
1247 int err;
1248
1249 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1251 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1252 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1253 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1254 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1255 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1256}
1257# endif
1258
1259/*
1260 * Top level evaluation function, returning a boolean.
1261 * Sets "error" to TRUE if there was an error.
1262 * Return TRUE or FALSE.
1263 */
1264 int
1265eval_to_bool(arg, error, nextcmd, skip)
1266 char_u *arg;
1267 int *error;
1268 char_u **nextcmd;
1269 int skip; /* only parse, don't execute */
1270{
Bram Moolenaar33570922005-01-25 22:26:29 +00001271 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 int retval = FALSE;
1273
1274 if (skip)
1275 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 else
1279 {
1280 *error = FALSE;
1281 if (!skip)
1282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001283 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001284 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
1286 }
1287 if (skip)
1288 --emsg_skip;
1289
1290 return retval;
1291}
1292
1293/*
1294 * Top level evaluation function, returning a string. If "skip" is TRUE,
1295 * only parsing to "nextcmd" is done, without reporting errors. Return
1296 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1297 */
1298 char_u *
1299eval_to_string_skip(arg, nextcmd, skip)
1300 char_u *arg;
1301 char_u **nextcmd;
1302 int skip; /* only parse, don't execute */
1303{
Bram Moolenaar33570922005-01-25 22:26:29 +00001304 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 char_u *retval;
1306
1307 if (skip)
1308 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 retval = NULL;
1311 else
1312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001313 retval = vim_strsave(get_tv_string(&tv));
1314 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 if (skip)
1317 --emsg_skip;
1318
1319 return retval;
1320}
1321
1322/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001323 * Skip over an expression at "*pp".
1324 * Return FAIL for an error, OK otherwise.
1325 */
1326 int
1327skip_expr(pp)
1328 char_u **pp;
1329{
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001331
1332 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001334}
1335
1336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338 * When "convert" is TRUE convert a List into a sequence of lines and convert
1339 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 * Return pointer to allocated memory, or NULL for failure.
1341 */
1342 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001343eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *arg;
1345 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347{
Bram Moolenaar33570922005-01-25 22:26:29 +00001348 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001350 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001351#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001355 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 retval = NULL;
1357 else
1358 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001359 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 {
1361 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001362 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001363 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001364 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001365 if (tv.vval.v_list->lv_len > 0)
1366 ga_append(&ga, NL);
1367 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001368 ga_append(&ga, NUL);
1369 retval = (char_u *)ga.ga_data;
1370 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001371#ifdef FEAT_FLOAT
1372 else if (convert && tv.v_type == VAR_FLOAT)
1373 {
1374 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1375 retval = vim_strsave(numbuf);
1376 }
1377#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001378 else
1379 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 }
1382
1383 return retval;
1384}
1385
1386/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 * Call eval_to_string() without using current local variables and using
1388 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 */
1390 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 char_u *arg;
1393 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395{
1396 char_u *retval;
1397 void *save_funccalp;
1398
1399 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 if (use_sandbox)
1401 ++sandbox;
1402 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001403 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001404 if (use_sandbox)
1405 --sandbox;
1406 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 restore_funccal(save_funccalp);
1408 return retval;
1409}
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411/*
1412 * Top level evaluation function, returning a number.
1413 * Evaluates "expr" silently.
1414 * Returns -1 for an error.
1415 */
1416 int
1417eval_to_number(expr)
1418 char_u *expr;
1419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001422 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
1424 ++emsg_off;
1425
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 retval = -1;
1428 else
1429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001430 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001431 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433 --emsg_off;
1434
1435 return retval;
1436}
1437
Bram Moolenaara40058a2005-07-11 22:42:07 +00001438/*
1439 * Prepare v: variable "idx" to be used.
1440 * Save the current typeval in "save_tv".
1441 * When not used yet add the variable to the v: hashtable.
1442 */
1443 static void
1444prepare_vimvar(idx, save_tv)
1445 int idx;
1446 typval_T *save_tv;
1447{
1448 *save_tv = vimvars[idx].vv_tv;
1449 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1450 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1451}
1452
1453/*
1454 * Restore v: variable "idx" to typeval "save_tv".
1455 * When no longer defined, remove the variable from the v: hashtable.
1456 */
1457 static void
1458restore_vimvar(idx, save_tv)
1459 int idx;
1460 typval_T *save_tv;
1461{
1462 hashitem_T *hi;
1463
Bram Moolenaara40058a2005-07-11 22:42:07 +00001464 vimvars[idx].vv_tv = *save_tv;
1465 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1466 {
1467 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1468 if (HASHITEM_EMPTY(hi))
1469 EMSG2(_(e_intern2), "restore_vimvar()");
1470 else
1471 hash_remove(&vimvarht, hi);
1472 }
1473}
1474
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001475#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001476/*
1477 * Evaluate an expression to a list with suggestions.
1478 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001479 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001480 */
1481 list_T *
1482eval_spell_expr(badword, expr)
1483 char_u *badword;
1484 char_u *expr;
1485{
1486 typval_T save_val;
1487 typval_T rettv;
1488 list_T *list = NULL;
1489 char_u *p = skipwhite(expr);
1490
1491 /* Set "v:val" to the bad word. */
1492 prepare_vimvar(VV_VAL, &save_val);
1493 vimvars[VV_VAL].vv_type = VAR_STRING;
1494 vimvars[VV_VAL].vv_str = badword;
1495 if (p_verbose == 0)
1496 ++emsg_off;
1497
1498 if (eval1(&p, &rettv, TRUE) == OK)
1499 {
1500 if (rettv.v_type != VAR_LIST)
1501 clear_tv(&rettv);
1502 else
1503 list = rettv.vval.v_list;
1504 }
1505
1506 if (p_verbose == 0)
1507 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001508 restore_vimvar(VV_VAL, &save_val);
1509
1510 return list;
1511}
1512
1513/*
1514 * "list" is supposed to contain two items: a word and a number. Return the
1515 * word in "pp" and the number as the return value.
1516 * Return -1 if anything isn't right.
1517 * Used to get the good word and score from the eval_spell_expr() result.
1518 */
1519 int
1520get_spellword(list, pp)
1521 list_T *list;
1522 char_u **pp;
1523{
1524 listitem_T *li;
1525
1526 li = list->lv_first;
1527 if (li == NULL)
1528 return -1;
1529 *pp = get_tv_string(&li->li_tv);
1530
1531 li = li->li_next;
1532 if (li == NULL)
1533 return -1;
1534 return get_tv_number(&li->li_tv);
1535}
1536#endif
1537
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001539 * Top level evaluation function.
1540 * Returns an allocated typval_T with the result.
1541 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001542 */
1543 typval_T *
1544eval_expr(arg, nextcmd)
1545 char_u *arg;
1546 char_u **nextcmd;
1547{
1548 typval_T *tv;
1549
1550 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001551 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001552 {
1553 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001554 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001555 }
1556
1557 return tv;
1558}
1559
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001562 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563 * Uses argv[argc] for the function arguments. Only Number and String
1564 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001565 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001567 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001568call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 char_u *func;
1570 int argc;
1571 char_u **argv;
1572 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001573 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
Bram Moolenaar33570922005-01-25 22:26:29 +00001576 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 long n;
1578 int len;
1579 int i;
1580 int doesrange;
1581 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001584 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587
1588 for (i = 0; i < argc; i++)
1589 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001590 /* Pass a NULL or empty argument as an empty string */
1591 if (argv[i] == NULL || *argv[i] == NUL)
1592 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001593 argvars[i].v_type = VAR_STRING;
1594 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001595 continue;
1596 }
1597
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001598 if (str_arg_only)
1599 len = 0;
1600 else
1601 /* Recognize a number argument, the others must be strings. */
1602 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (len != 0 && len == (int)STRLEN(argv[i]))
1604 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001605 argvars[i].v_type = VAR_NUMBER;
1606 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 }
1608 else
1609 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001610 argvars[i].v_type = VAR_STRING;
1611 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 }
1613 }
1614
1615 if (safe)
1616 {
1617 save_funccalp = save_funccal();
1618 ++sandbox;
1619 }
1620
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001621 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1622 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (safe)
1626 {
1627 --sandbox;
1628 restore_funccal(save_funccalp);
1629 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 vim_free(argvars);
1631
1632 if (ret == FAIL)
1633 clear_tv(rettv);
1634
1635 return ret;
1636}
1637
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001638/*
1639 * Call vimL function "func" and return the result as a number.
1640 * Returns -1 when calling the function fails.
1641 * Uses argv[argc] for the function arguments.
1642 */
1643 long
1644call_func_retnr(func, argc, argv, safe)
1645 char_u *func;
1646 int argc;
1647 char_u **argv;
1648 int safe; /* use the sandbox */
1649{
1650 typval_T rettv;
1651 long retval;
1652
1653 /* All arguments are passed as strings, no conversion to number. */
1654 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1655 return -1;
1656
1657 retval = get_tv_number_chk(&rettv, NULL);
1658 clear_tv(&rettv);
1659 return retval;
1660}
1661
1662#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1663 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1664
Bram Moolenaar4f688582007-07-24 12:34:30 +00001665# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001666/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001667 * Call vimL function "func" and return the result as a string.
1668 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001669 * Uses argv[argc] for the function arguments.
1670 */
1671 void *
1672call_func_retstr(func, argc, argv, safe)
1673 char_u *func;
1674 int argc;
1675 char_u **argv;
1676 int safe; /* use the sandbox */
1677{
1678 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001679 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001681 /* All arguments are passed as strings, no conversion to number. */
1682 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 return NULL;
1684
1685 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 return retval;
1688}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001689# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001690
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001691/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001692 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001694 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001695 */
1696 void *
1697call_func_retlist(func, argc, argv, safe)
1698 char_u *func;
1699 int argc;
1700 char_u **argv;
1701 int safe; /* use the sandbox */
1702{
1703 typval_T rettv;
1704
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001705 /* All arguments are passed as strings, no conversion to number. */
1706 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001707 return NULL;
1708
1709 if (rettv.v_type != VAR_LIST)
1710 {
1711 clear_tv(&rettv);
1712 return NULL;
1713 }
1714
1715 return rettv.vval.v_list;
1716}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717#endif
1718
1719/*
1720 * Save the current function call pointer, and set it to NULL.
1721 * Used when executing autocommands and for ":source".
1722 */
1723 void *
1724save_funccal()
1725{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 current_funccal = NULL;
1729 return (void *)fc;
1730}
1731
1732 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001733restore_funccal(vfc)
1734 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001736 funccall_T *fc = (funccall_T *)vfc;
1737
1738 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739}
1740
Bram Moolenaar05159a02005-02-26 23:04:13 +00001741#if defined(FEAT_PROFILE) || defined(PROTO)
1742/*
1743 * Prepare profiling for entering a child or something else that is not
1744 * counted for the script/function itself.
1745 * Should always be called in pair with prof_child_exit().
1746 */
1747 void
1748prof_child_enter(tm)
1749 proftime_T *tm; /* place to store waittime */
1750{
1751 funccall_T *fc = current_funccal;
1752
1753 if (fc != NULL && fc->func->uf_profiling)
1754 profile_start(&fc->prof_child);
1755 script_prof_save(tm);
1756}
1757
1758/*
1759 * Take care of time spent in a child.
1760 * Should always be called after prof_child_enter().
1761 */
1762 void
1763prof_child_exit(tm)
1764 proftime_T *tm; /* where waittime was stored */
1765{
1766 funccall_T *fc = current_funccal;
1767
1768 if (fc != NULL && fc->func->uf_profiling)
1769 {
1770 profile_end(&fc->prof_child);
1771 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1772 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1773 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1774 }
1775 script_prof_restore(tm);
1776}
1777#endif
1778
1779
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#ifdef FEAT_FOLDING
1781/*
1782 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1783 * it in "*cp". Doesn't give error messages.
1784 */
1785 int
1786eval_foldexpr(arg, cp)
1787 char_u *arg;
1788 int *cp;
1789{
Bram Moolenaar33570922005-01-25 22:26:29 +00001790 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 int retval;
1792 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001793 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1794 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
1796 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001797 if (use_sandbox)
1798 ++sandbox;
1799 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 if (tv.v_type == VAR_NUMBER)
1807 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001808 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 retval = 0;
1810 else
1811 {
1812 /* If the result is a string, check if there is a non-digit before
1813 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001814 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (!VIM_ISDIGIT(*s) && *s != '-')
1816 *cp = *s++;
1817 retval = atol((char *)s);
1818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 }
1821 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001822 if (use_sandbox)
1823 --sandbox;
1824 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 return retval;
1827}
1828#endif
1829
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 * ":let" list all variable values
1832 * ":let var1 var2" list variable values
1833 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001834 * ":let var += expr" assignment command.
1835 * ":let var -= expr" assignment command.
1836 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 */
1839 void
1840ex_let(eap)
1841 exarg_T *eap;
1842{
1843 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001844 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847 int var_count = 0;
1848 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001849 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001850 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001851 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
Bram Moolenaardb552d602006-03-23 22:59:57 +00001853 argend = skip_var_list(arg, &var_count, &semicolon);
1854 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001856 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1857 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001858 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001859 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001861 /*
1862 * ":let" without "=": list variables
1863 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 if (*arg == '[')
1865 EMSG(_(e_invarg));
1866 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_glob_vars(&first);
1873 list_buf_vars(&first);
1874 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001875#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001876 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001877#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001878 list_script_vars(&first);
1879 list_func_vars(&first);
1880 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 eap->nextcmd = check_nextcmd(arg);
1883 }
1884 else
1885 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 op[0] = '=';
1887 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001888 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 {
1890 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1891 op[0] = expr[-1]; /* +=, -= or .= */
1892 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 if (eap->skip)
1896 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001897 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 if (eap->skip)
1899 {
1900 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 --emsg_skip;
1903 }
1904 else if (i != FAIL)
1905 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 }
1910 }
1911}
1912
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913/*
1914 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1915 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001916 * When "nextchars" is not NULL it points to a string with characters that
1917 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1918 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 * Returns OK or FAIL;
1920 */
1921 static int
1922ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1923 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int copy; /* copy values from "tv", don't move */
1926 int semicolon; /* from skip_var_list() */
1927 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929{
1930 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001933 listitem_T *item;
1934 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935
1936 if (*arg != '[')
1937 {
1938 /*
1939 * ":let var = expr" or ":for var in list"
1940 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 return FAIL;
1943 return OK;
1944 }
1945
1946 /*
1947 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1948 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001949 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 {
1951 EMSG(_(e_listreq));
1952 return FAIL;
1953 }
1954
1955 i = list_len(l);
1956 if (semicolon == 0 && var_count < i)
1957 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001958 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 return FAIL;
1960 }
1961 if (var_count - semicolon > i)
1962 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001963 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 return FAIL;
1965 }
1966
1967 item = l->lv_first;
1968 while (*arg != ']')
1969 {
1970 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001972 item = item->li_next;
1973 if (arg == NULL)
1974 return FAIL;
1975
1976 arg = skipwhite(arg);
1977 if (*arg == ';')
1978 {
1979 /* Put the rest of the list (may be empty) in the var after ';'.
1980 * Create a new list for this. */
1981 l = list_alloc();
1982 if (l == NULL)
1983 return FAIL;
1984 while (item != NULL)
1985 {
1986 list_append_tv(l, &item->li_tv);
1987 item = item->li_next;
1988 }
1989
1990 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001991 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 ltv.vval.v_list = l;
1993 l->lv_refcount = 1;
1994
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1996 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997 clear_tv(&ltv);
1998 if (arg == NULL)
1999 return FAIL;
2000 break;
2001 }
2002 else if (*arg != ',' && *arg != ']')
2003 {
2004 EMSG2(_(e_intern2), "ex_let_vars()");
2005 return FAIL;
2006 }
2007 }
2008
2009 return OK;
2010}
2011
2012/*
2013 * Skip over assignable variable "var" or list of variables "[var, var]".
2014 * Used for ":let varvar = expr" and ":for varvar in expr".
2015 * For "[var, var]" increment "*var_count" for each variable.
2016 * for "[var, var; var]" set "semicolon".
2017 * Return NULL for an error.
2018 */
2019 static char_u *
2020skip_var_list(arg, var_count, semicolon)
2021 char_u *arg;
2022 int *var_count;
2023 int *semicolon;
2024{
2025 char_u *p, *s;
2026
2027 if (*arg == '[')
2028 {
2029 /* "[var, var]": find the matching ']'. */
2030 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002031 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 {
2033 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2034 s = skip_var_one(p);
2035 if (s == p)
2036 {
2037 EMSG2(_(e_invarg2), p);
2038 return NULL;
2039 }
2040 ++*var_count;
2041
2042 p = skipwhite(s);
2043 if (*p == ']')
2044 break;
2045 else if (*p == ';')
2046 {
2047 if (*semicolon == 1)
2048 {
2049 EMSG(_("Double ; in list of variables"));
2050 return NULL;
2051 }
2052 *semicolon = 1;
2053 }
2054 else if (*p != ',')
2055 {
2056 EMSG2(_(e_invarg2), p);
2057 return NULL;
2058 }
2059 }
2060 return p + 1;
2061 }
2062 else
2063 return skip_var_one(arg);
2064}
2065
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002066/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002067 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002068 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002069 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002070 static char_u *
2071skip_var_one(arg)
2072 char_u *arg;
2073{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002074 if (*arg == '@' && arg[1] != NUL)
2075 return arg + 2;
2076 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2077 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078}
2079
Bram Moolenaara7043832005-01-21 11:56:39 +00002080/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 * List variables for hashtab "ht" with prefix "prefix".
2082 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002084 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002086 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002089 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090{
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 hashitem_T *hi;
2092 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002093 int todo;
2094
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002095 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002096 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2097 {
2098 if (!HASHITEM_EMPTY(hi))
2099 {
2100 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 di = HI2DI(hi);
2102 if (empty || di->di_tv.v_type != VAR_STRING
2103 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002104 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002105 }
2106 }
2107}
2108
2109/*
2110 * List global variables.
2111 */
2112 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002113list_glob_vars(first)
2114 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002115{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002117}
2118
2119/*
2120 * List buffer variables.
2121 */
2122 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123list_buf_vars(first)
2124 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002125{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 char_u numbuf[NUMBUFLEN];
2127
Bram Moolenaar429fa852013-04-15 12:27:36 +02002128 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130
2131 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002132 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2133 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002134}
2135
2136/*
2137 * List window variables.
2138 */
2139 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140list_win_vars(first)
2141 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002142{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002143 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002144 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002145}
2146
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147#ifdef FEAT_WINDOWS
2148/*
2149 * List tab page variables.
2150 */
2151 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152list_tab_vars(first)
2153 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002154{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002155 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002156 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002157}
2158#endif
2159
Bram Moolenaara7043832005-01-21 11:56:39 +00002160/*
2161 * List Vim variables.
2162 */
2163 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164list_vim_vars(first)
2165 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002166{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168}
2169
2170/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002171 * List script-local variables, if there is a script.
2172 */
2173 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174list_script_vars(first)
2175 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176{
2177 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2179 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002180}
2181
2182/*
2183 * List function variables, if there is a function.
2184 */
2185 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186list_func_vars(first)
2187 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002188{
2189 if (current_funccal != NULL)
2190 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002192}
2193
2194/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 * List variables in "arg".
2196 */
2197 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002198list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 exarg_T *eap;
2200 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002202{
2203 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002204 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002205 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 char_u *name_start;
2207 char_u *arg_subsc;
2208 char_u *tofree;
2209 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210
2211 while (!ends_excmd(*arg) && !got_int)
2212 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002215 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2217 {
2218 emsg_severe = TRUE;
2219 EMSG(_(e_trailing));
2220 break;
2221 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002225 /* get_name_len() takes care of expanding curly braces */
2226 name_start = name = arg;
2227 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2228 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 /* This is mainly to keep test 49 working: when expanding
2231 * curly braces fails overrule the exception error message. */
2232 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 emsg_severe = TRUE;
2235 EMSG2(_(e_invarg2), arg);
2236 break;
2237 }
2238 error = TRUE;
2239 }
2240 else
2241 {
2242 if (tofree != NULL)
2243 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002244 if (get_var_tv(name, len, &tv, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 else
2247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 /* handle d.key, l[idx], f(expr) */
2249 arg_subsc = arg;
2250 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 'g': list_glob_vars(first); break;
2259 case 'b': list_buf_vars(first); break;
2260 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002261#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002262 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002263#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002264 case 'v': list_vim_vars(first); break;
2265 case 's': list_script_vars(first); break;
2266 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 default:
2268 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002269 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002270 }
2271 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 {
2273 char_u numbuf[NUMBUFLEN];
2274 char_u *tf;
2275 int c;
2276 char_u *s;
2277
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002278 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 c = *arg;
2280 *arg = NUL;
2281 list_one_var_a((char_u *)"",
2282 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002283 tv.v_type,
2284 s == NULL ? (char_u *)"" : s,
2285 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 *arg = c;
2287 vim_free(tf);
2288 }
2289 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002293
2294 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002296
2297 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 }
2299
2300 return arg;
2301}
2302
2303/*
2304 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2305 * Returns a pointer to the char just after the var name.
2306 * Returns NULL if there is an error.
2307 */
2308 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002313 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002314 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315{
2316 int c1;
2317 char_u *name;
2318 char_u *p;
2319 char_u *arg_end = NULL;
2320 int len;
2321 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002322 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323
2324 /*
2325 * ":let $VAR = expr": Set environment variable.
2326 */
2327 if (*arg == '$')
2328 {
2329 /* Find the end of the name. */
2330 ++arg;
2331 name = arg;
2332 len = get_env_len(&arg);
2333 if (len == 0)
2334 EMSG2(_(e_invarg2), name - 1);
2335 else
2336 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002337 if (op != NULL && (*op == '+' || *op == '-'))
2338 EMSG2(_(e_letwrong), op);
2339 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002340 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002342 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 {
2344 c1 = name[len];
2345 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 p = get_tv_string_chk(tv);
2347 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 {
2349 int mustfree = FALSE;
2350 char_u *s = vim_getenv(name, &mustfree);
2351
2352 if (s != NULL)
2353 {
2354 p = tofree = concat_str(s, p);
2355 if (mustfree)
2356 vim_free(s);
2357 }
2358 }
2359 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002361 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002362 if (STRICMP(name, "HOME") == 0)
2363 init_homedir();
2364 else if (didset_vim && STRICMP(name, "VIM") == 0)
2365 didset_vim = FALSE;
2366 else if (didset_vimruntime
2367 && STRICMP(name, "VIMRUNTIME") == 0)
2368 didset_vimruntime = FALSE;
2369 arg_end = arg;
2370 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
2374 }
2375 }
2376
2377 /*
2378 * ":let &option = expr": Set option value.
2379 * ":let &l:option = expr": Set local option value.
2380 * ":let &g:option = expr": Set global option value.
2381 */
2382 else if (*arg == '&')
2383 {
2384 /* Find the end of the name. */
2385 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 if (p == NULL || (endchars != NULL
2387 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002388 EMSG(_(e_letunexp));
2389 else
2390 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 long n;
2392 int opt_type;
2393 long numval;
2394 char_u *stringval = NULL;
2395 char_u *s;
2396
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 c1 = *p;
2398 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399
2400 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 s = get_tv_string_chk(tv); /* != NULL if number or string */
2402 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002403 {
2404 opt_type = get_option_value(arg, &numval,
2405 &stringval, opt_flags);
2406 if ((opt_type == 1 && *op == '.')
2407 || (opt_type == 0 && *op != '.'))
2408 EMSG2(_(e_letwrong), op);
2409 else
2410 {
2411 if (opt_type == 1) /* number */
2412 {
2413 if (*op == '+')
2414 n = numval + n;
2415 else
2416 n = numval - n;
2417 }
2418 else if (opt_type == 0 && stringval != NULL) /* string */
2419 {
2420 s = concat_str(stringval, s);
2421 vim_free(stringval);
2422 stringval = s;
2423 }
2424 }
2425 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 if (s != NULL)
2427 {
2428 set_option_value(arg, n, s, opt_flags);
2429 arg_end = p;
2430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002432 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 }
2434 }
2435
2436 /*
2437 * ":let @r = expr": Set register contents.
2438 */
2439 else if (*arg == '@')
2440 {
2441 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 if (op != NULL && (*op == '+' || *op == '-'))
2443 EMSG2(_(e_letwrong), op);
2444 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002445 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 EMSG(_(e_letunexp));
2447 else
2448 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002449 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 char_u *s;
2451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 p = get_tv_string_chk(tv);
2453 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002455 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002456 if (s != NULL)
2457 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002458 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 vim_free(s);
2460 }
2461 }
2462 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002463 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002464 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002465 arg_end = arg + 1;
2466 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002467 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 }
2469 }
2470
2471 /*
2472 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002475 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002477 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002479 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002482 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2483 EMSG(_(e_letunexp));
2484 else
2485 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002486 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002487 arg_end = p;
2488 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 }
2492
2493 else
2494 EMSG2(_(e_invarg2), arg);
2495
2496 return arg_end;
2497}
2498
2499/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2501 */
2502 static int
2503check_changedtick(arg)
2504 char_u *arg;
2505{
2506 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2507 {
2508 EMSG2(_(e_readonlyvar), arg);
2509 return TRUE;
2510 }
2511 return FALSE;
2512}
2513
2514/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 * Get an lval: variable, Dict item or List item that can be assigned a value
2516 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2517 * "name.key", "name.key[expr]" etc.
2518 * Indexing only works if "name" is an existing List or Dictionary.
2519 * "name" points to the start of the name.
2520 * If "rettv" is not NULL it points to the value to be assigned.
2521 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2522 * wrong; must end in space or cmd separator.
2523 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002524 * flags:
2525 * GLV_QUIET: do not give error messages
2526 * GLV_NO_AUTOLOAD: do not use script autoloading
2527 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002529 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 */
2532 static char_u *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002533get_lval(name, rettv, lp, unlet, skip, flags, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 typval_T *rettv;
2536 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int unlet;
2538 int skip;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002539 int flags; /* GLV_ values */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002540 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 char_u *expr_start, *expr_end;
2544 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 dictitem_T *v;
2546 typval_T var1;
2547 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002549 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002553 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557
2558 if (skip)
2559 {
2560 /* When skipping just find the end of the name. */
2561 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002562 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 }
2564
2565 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002566 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 if (expr_start != NULL)
2568 {
2569 /* Don't expand the name when we already know there is an error. */
2570 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2571 && *p != '[' && *p != '.')
2572 {
2573 EMSG(_(e_trailing));
2574 return NULL;
2575 }
2576
2577 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2578 if (lp->ll_exp_name == NULL)
2579 {
2580 /* Report an invalid expression in braces, unless the
2581 * expression evaluation has been cancelled due to an
2582 * aborting error, an interrupt, or an exception. */
2583 if (!aborting() && !quiet)
2584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002585 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002586 EMSG2(_(e_invarg2), name);
2587 return NULL;
2588 }
2589 }
2590 lp->ll_name = lp->ll_exp_name;
2591 }
2592 else
2593 lp->ll_name = name;
2594
2595 /* Without [idx] or .key we are done. */
2596 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2597 return p;
2598
2599 cc = *p;
2600 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (v == NULL && !quiet)
2603 EMSG2(_(e_undefvar), lp->ll_name);
2604 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 if (v == NULL)
2606 return NULL;
2607
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 /*
2609 * Loop until no more [idx] or .key is following.
2610 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002611 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002614 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2615 && !(lp->ll_tv->v_type == VAR_DICT
2616 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_("E689: Can only index a List or Dictionary"));
2620 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002621 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002624 if (!quiet)
2625 EMSG(_("E708: [:] must come last"));
2626 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002627 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 len = -1;
2630 if (*p == '.')
2631 {
2632 key = p + 1;
2633 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2634 ;
2635 if (len == 0)
2636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002637 if (!quiet)
2638 EMSG(_(e_emptykey));
2639 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 }
2641 p = key + len;
2642 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002643 else
2644 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002645 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002646 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 if (*p == ':')
2648 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 else
2650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 empty1 = FALSE;
2652 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002654 if (get_tv_string_chk(&var1) == NULL)
2655 {
2656 /* not a number or string */
2657 clear_tv(&var1);
2658 return NULL;
2659 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 }
2661
2662 /* Optionally get the second index [ :expr]. */
2663 if (*p == ':')
2664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002668 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002672 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (rettv != NULL && (rettv->v_type != VAR_LIST
2674 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 if (!quiet)
2677 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
2682 p = skipwhite(p + 1);
2683 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 else
2686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (!empty1)
2691 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002694 if (get_tv_string_chk(&var2) == NULL)
2695 {
2696 /* not a number or string */
2697 if (!empty1)
2698 clear_tv(&var1);
2699 clear_tv(&var2);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002706 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002707
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (!quiet)
2711 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 if (!empty1)
2713 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718
2719 /* Skip to past ']'. */
2720 ++p;
2721 }
2722
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 {
2725 if (len == -1)
2726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002728 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (*key == NUL)
2730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
2736 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002737 lp->ll_list = NULL;
2738 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002739 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002740
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002741 /* When assigning to a scope dictionary check that a function and
2742 * variable name is valid (only variable name unless it is l: or
2743 * g: dictionary). Disallow overwriting a builtin function. */
2744 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002745 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002746 int prevval;
2747 int wrong;
2748
2749 if (len != -1)
2750 {
2751 prevval = key[len];
2752 key[len] = NUL;
2753 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002754 else
2755 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002756 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2757 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002758 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002759 || !valid_varname(key);
2760 if (len != -1)
2761 key[len] = prevval;
2762 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002763 return NULL;
2764 }
2765
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002768 /* Can't add "v:" variable. */
2769 if (lp->ll_dict == &vimvardict)
2770 {
2771 EMSG2(_(e_illvar), name);
2772 return NULL;
2773 }
2774
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002775 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (len == -1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002785 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 if (len == -1)
2789 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002791 p = NULL;
2792 break;
2793 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 /* existing variable, need to check if it can be changed */
2795 else if (var_check_ro(lp->ll_di->di_flags, name))
2796 return NULL;
2797
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 if (len == -1)
2799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 else
2803 {
2804 /*
2805 * Get the number and item for the only or first index of the List.
2806 */
2807 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 else
2810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002811 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 clear_tv(&var1);
2813 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002814 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 lp->ll_list = lp->ll_tv->vval.v_list;
2816 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2817 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002818 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002819 if (lp->ll_n1 < 0)
2820 {
2821 lp->ll_n1 = 0;
2822 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2823 }
2824 }
2825 if (lp->ll_li == NULL)
2826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002829 if (!quiet)
2830 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 }
2833
2834 /*
2835 * May need to find the item or absolute index for the second
2836 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 * When no index given: "lp->ll_empty2" is TRUE.
2838 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002842 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002846 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 {
2849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2857 if (lp->ll_n1 < 0)
2858 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2859 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002860 {
2861 if (!quiet)
2862 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002864 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002865 }
2866
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002869 }
2870
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871 return p;
2872}
2873
2874/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 */
2877 static void
2878clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002879 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880{
2881 vim_free(lp->ll_exp_name);
2882 vim_free(lp->ll_newkey);
2883}
2884
2885/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002886 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002888 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 */
2890 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002891set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002892 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002894 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897{
2898 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 listitem_T *ri;
2900 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901
2902 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002903 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002905 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 cc = *endp;
2907 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 if (op != NULL && *op != '=')
2909 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002910 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002911
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002912 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002913 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002914 &tv, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002915 {
2916 if (tv_op(&tv, rettv, op) == OK)
2917 set_var(lp->ll_name, &tv, FALSE);
2918 clear_tv(&tv);
2919 }
2920 }
2921 else
2922 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002926 else if (tv_check_lock(lp->ll_newkey == NULL
2927 ? lp->ll_tv->v_lock
2928 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2929 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 else if (lp->ll_range)
2931 {
2932 /*
2933 * Assign the List values to the list items.
2934 */
2935 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002937 if (op != NULL && *op != '=')
2938 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2939 else
2940 {
2941 clear_tv(&lp->ll_li->li_tv);
2942 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 ri = ri->li_next;
2945 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2946 break;
2947 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002948 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002950 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 ri = NULL;
2953 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002954 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002956 lp->ll_li = lp->ll_li->li_next;
2957 ++lp->ll_n1;
2958 }
2959 if (ri != NULL)
2960 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 else if (lp->ll_empty2
2962 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 : lp->ll_n1 != lp->ll_n2)
2964 EMSG(_("E711: List value has not enough items"));
2965 }
2966 else
2967 {
2968 /*
2969 * Assign to a List or Dictionary item.
2970 */
2971 if (lp->ll_newkey != NULL)
2972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 {
2975 EMSG2(_(e_letwrong), op);
2976 return;
2977 }
2978
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002980 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 if (di == NULL)
2982 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002983 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2984 {
2985 vim_free(di);
2986 return;
2987 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002988 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002989 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 else if (op != NULL && *op != '=')
2991 {
2992 tv_op(lp->ll_tv, rettv, op);
2993 return;
2994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002995 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002997
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /*
2999 * Assign the value to the variable or list item.
3000 */
3001 if (copy)
3002 copy_tv(rettv, lp->ll_tv);
3003 else
3004 {
3005 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003006 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003008 }
3009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010}
3011
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003012/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3014 * Returns OK or FAIL.
3015 */
3016 static int
3017tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 typval_T *tv1;
3019 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003020 char_u *op;
3021{
3022 long n;
3023 char_u numbuf[NUMBUFLEN];
3024 char_u *s;
3025
3026 /* Can't do anything with a Funcref or a Dict on the right. */
3027 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3028 {
3029 switch (tv1->v_type)
3030 {
3031 case VAR_DICT:
3032 case VAR_FUNC:
3033 break;
3034
3035 case VAR_LIST:
3036 if (*op != '+' || tv2->v_type != VAR_LIST)
3037 break;
3038 /* List += List */
3039 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3040 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3041 return OK;
3042
3043 case VAR_NUMBER:
3044 case VAR_STRING:
3045 if (tv2->v_type == VAR_LIST)
3046 break;
3047 if (*op == '+' || *op == '-')
3048 {
3049 /* nr += nr or nr -= nr*/
3050 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003051#ifdef FEAT_FLOAT
3052 if (tv2->v_type == VAR_FLOAT)
3053 {
3054 float_T f = n;
3055
3056 if (*op == '+')
3057 f += tv2->vval.v_float;
3058 else
3059 f -= tv2->vval.v_float;
3060 clear_tv(tv1);
3061 tv1->v_type = VAR_FLOAT;
3062 tv1->vval.v_float = f;
3063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003065#endif
3066 {
3067 if (*op == '+')
3068 n += get_tv_number(tv2);
3069 else
3070 n -= get_tv_number(tv2);
3071 clear_tv(tv1);
3072 tv1->v_type = VAR_NUMBER;
3073 tv1->vval.v_number = n;
3074 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003075 }
3076 else
3077 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078 if (tv2->v_type == VAR_FLOAT)
3079 break;
3080
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081 /* str .= str */
3082 s = get_tv_string(tv1);
3083 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3084 clear_tv(tv1);
3085 tv1->v_type = VAR_STRING;
3086 tv1->vval.v_string = s;
3087 }
3088 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003089
3090#ifdef FEAT_FLOAT
3091 case VAR_FLOAT:
3092 {
3093 float_T f;
3094
3095 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3096 && tv2->v_type != VAR_NUMBER
3097 && tv2->v_type != VAR_STRING))
3098 break;
3099 if (tv2->v_type == VAR_FLOAT)
3100 f = tv2->vval.v_float;
3101 else
3102 f = get_tv_number(tv2);
3103 if (*op == '+')
3104 tv1->vval.v_float += f;
3105 else
3106 tv1->vval.v_float -= f;
3107 }
3108 return OK;
3109#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 }
3111 }
3112
3113 EMSG2(_(e_letwrong), op);
3114 return FAIL;
3115}
3116
3117/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003118 * Add a watcher to a list.
3119 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003120 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003121list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 list_T *l;
3123 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124{
3125 lw->lw_next = l->lv_watch;
3126 l->lv_watch = lw;
3127}
3128
3129/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003130 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003131 * No warning when it isn't found...
3132 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003133 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003135 list_T *l;
3136 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003137{
Bram Moolenaar33570922005-01-25 22:26:29 +00003138 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003139
3140 lwp = &l->lv_watch;
3141 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3142 {
3143 if (lw == lwrem)
3144 {
3145 *lwp = lw->lw_next;
3146 break;
3147 }
3148 lwp = &lw->lw_next;
3149 }
3150}
3151
3152/*
3153 * Just before removing an item from a list: advance watchers to the next
3154 * item.
3155 */
3156 static void
3157list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003158 list_T *l;
3159 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003160{
Bram Moolenaar33570922005-01-25 22:26:29 +00003161 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003162
3163 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3164 if (lw->lw_item == item)
3165 lw->lw_item = item->li_next;
3166}
3167
3168/*
3169 * Evaluate the expression used in a ":for var in expr" command.
3170 * "arg" points to "var".
3171 * Set "*errp" to TRUE for an error, FALSE otherwise;
3172 * Return a pointer that holds the info. Null when there is an error.
3173 */
3174 void *
3175eval_for_line(arg, errp, nextcmdp, skip)
3176 char_u *arg;
3177 int *errp;
3178 char_u **nextcmdp;
3179 int skip;
3180{
Bram Moolenaar33570922005-01-25 22:26:29 +00003181 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003182 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003183 typval_T tv;
3184 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003185
3186 *errp = TRUE; /* default: there is an error */
3187
Bram Moolenaar33570922005-01-25 22:26:29 +00003188 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 if (fi == NULL)
3190 return NULL;
3191
3192 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3193 if (expr == NULL)
3194 return fi;
3195
3196 expr = skipwhite(expr);
3197 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3198 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003199 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 return fi;
3201 }
3202
3203 if (skip)
3204 ++emsg_skip;
3205 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3206 {
3207 *errp = FALSE;
3208 if (!skip)
3209 {
3210 l = tv.vval.v_list;
3211 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003212 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003213 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003214 clear_tv(&tv);
3215 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003216 else
3217 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003218 /* No need to increment the refcount, it's already set for the
3219 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 fi->fi_list = l;
3221 list_add_watch(l, &fi->fi_lw);
3222 fi->fi_lw.lw_item = l->lv_first;
3223 }
3224 }
3225 }
3226 if (skip)
3227 --emsg_skip;
3228
3229 return fi;
3230}
3231
3232/*
3233 * Use the first item in a ":for" list. Advance to the next.
3234 * Assign the values to the variable (list). "arg" points to the first one.
3235 * Return TRUE when a valid item was found, FALSE when at end of list or
3236 * something wrong.
3237 */
3238 int
3239next_for_item(fi_void, arg)
3240 void *fi_void;
3241 char_u *arg;
3242{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003243 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246
3247 item = fi->fi_lw.lw_item;
3248 if (item == NULL)
3249 result = FALSE;
3250 else
3251 {
3252 fi->fi_lw.lw_item = item->li_next;
3253 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3254 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3255 }
3256 return result;
3257}
3258
3259/*
3260 * Free the structure used to store info used by ":for".
3261 */
3262 void
3263free_for_info(fi_void)
3264 void *fi_void;
3265{
Bram Moolenaar33570922005-01-25 22:26:29 +00003266 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003268 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003270 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 list_unref(fi->fi_list);
3272 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 vim_free(fi);
3274}
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3277
3278 void
3279set_context_for_expression(xp, arg, cmdidx)
3280 expand_T *xp;
3281 char_u *arg;
3282 cmdidx_T cmdidx;
3283{
3284 int got_eq = FALSE;
3285 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003288 if (cmdidx == CMD_let)
3289 {
3290 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003291 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003292 {
3293 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003294 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003295 {
3296 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003297 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003298 if (vim_iswhite(*p))
3299 break;
3300 }
3301 return;
3302 }
3303 }
3304 else
3305 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3306 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 while ((xp->xp_pattern = vim_strpbrk(arg,
3308 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3309 {
3310 c = *xp->xp_pattern;
3311 if (c == '&')
3312 {
3313 c = xp->xp_pattern[1];
3314 if (c == '&')
3315 {
3316 ++xp->xp_pattern;
3317 xp->xp_context = cmdidx != CMD_let || got_eq
3318 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3319 }
3320 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003323 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3324 xp->xp_pattern += 2;
3325
3326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 else if (c == '$')
3329 {
3330 /* environment variable */
3331 xp->xp_context = EXPAND_ENV_VARS;
3332 }
3333 else if (c == '=')
3334 {
3335 got_eq = TRUE;
3336 xp->xp_context = EXPAND_EXPRESSION;
3337 }
3338 else if (c == '<'
3339 && xp->xp_context == EXPAND_FUNCTIONS
3340 && vim_strchr(xp->xp_pattern, '(') == NULL)
3341 {
3342 /* Function name can start with "<SNR>" */
3343 break;
3344 }
3345 else if (cmdidx != CMD_let || got_eq)
3346 {
3347 if (c == '"') /* string */
3348 {
3349 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3350 if (c == '\\' && xp->xp_pattern[1] != NUL)
3351 ++xp->xp_pattern;
3352 xp->xp_context = EXPAND_NOTHING;
3353 }
3354 else if (c == '\'') /* literal string */
3355 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003356 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3358 /* skip */ ;
3359 xp->xp_context = EXPAND_NOTHING;
3360 }
3361 else if (c == '|')
3362 {
3363 if (xp->xp_pattern[1] == '|')
3364 {
3365 ++xp->xp_pattern;
3366 xp->xp_context = EXPAND_EXPRESSION;
3367 }
3368 else
3369 xp->xp_context = EXPAND_COMMANDS;
3370 }
3371 else
3372 xp->xp_context = EXPAND_EXPRESSION;
3373 }
3374 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003375 /* Doesn't look like something valid, expand as an expression
3376 * anyway. */
3377 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 arg = xp->xp_pattern;
3379 if (*arg != NUL)
3380 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3381 /* skip */ ;
3382 }
3383 xp->xp_pattern = arg;
3384}
3385
3386#endif /* FEAT_CMDL_COMPL */
3387
3388/*
3389 * ":1,25call func(arg1, arg2)" function call.
3390 */
3391 void
3392ex_call(eap)
3393 exarg_T *eap;
3394{
3395 char_u *arg = eap->arg;
3396 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003398 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003400 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 linenr_T lnum;
3402 int doesrange;
3403 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003404 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003406 if (eap->skip)
3407 {
3408 /* trans_function_name() doesn't work well when skipping, use eval0()
3409 * instead to skip to any following command, e.g. for:
3410 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003411 ++emsg_skip;
3412 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3413 clear_tv(&rettv);
3414 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003415 return;
3416 }
3417
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003418 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003419 if (fudi.fd_newkey != NULL)
3420 {
3421 /* Still need to give an error message for missing key. */
3422 EMSG2(_(e_dictkey), fudi.fd_newkey);
3423 vim_free(fudi.fd_newkey);
3424 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003425 if (tofree == NULL)
3426 return;
3427
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003428 /* Increase refcount on dictionary, it could get deleted when evaluating
3429 * the arguments. */
3430 if (fudi.fd_dict != NULL)
3431 ++fudi.fd_dict->dv_refcount;
3432
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003433 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003434 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003435 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaar532c7802005-01-27 14:44:31 +00003437 /* Skip white space to allow ":call func ()". Not good, but required for
3438 * backward compatibility. */
3439 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003440 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441
3442 if (*startarg != '(')
3443 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003444 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 goto end;
3446 }
3447
3448 /*
3449 * When skipping, evaluate the function once, to find the end of the
3450 * arguments.
3451 * When the function takes a range, this is discovered after the first
3452 * call, and the loop is broken.
3453 */
3454 if (eap->skip)
3455 {
3456 ++emsg_skip;
3457 lnum = eap->line2; /* do it once, also with an invalid range */
3458 }
3459 else
3460 lnum = eap->line1;
3461 for ( ; lnum <= eap->line2; ++lnum)
3462 {
3463 if (!eap->skip && eap->addr_count > 0)
3464 {
3465 curwin->w_cursor.lnum = lnum;
3466 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003467#ifdef FEAT_VIRTUALEDIT
3468 curwin->w_cursor.coladd = 0;
3469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
3471 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003472 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 eap->line1, eap->line2, &doesrange,
3474 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 failed = TRUE;
3477 break;
3478 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003479
3480 /* Handle a function returning a Funcref, Dictionary or List. */
3481 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3482 {
3483 failed = TRUE;
3484 break;
3485 }
3486
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 if (doesrange || eap->skip)
3489 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003492 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003493 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003494 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (aborting())
3496 break;
3497 }
3498 if (eap->skip)
3499 --emsg_skip;
3500
3501 if (!failed)
3502 {
3503 /* Check for trailing illegal characters and a following command. */
3504 if (!ends_excmd(*arg))
3505 {
3506 emsg_severe = TRUE;
3507 EMSG(_(e_trailing));
3508 }
3509 else
3510 eap->nextcmd = check_nextcmd(arg);
3511 }
3512
3513end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003514 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003515 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516}
3517
3518/*
3519 * ":unlet[!] var1 ... " command.
3520 */
3521 void
3522ex_unlet(eap)
3523 exarg_T *eap;
3524{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003525 ex_unletlock(eap, eap->arg, 0);
3526}
3527
3528/*
3529 * ":lockvar" and ":unlockvar" commands
3530 */
3531 void
3532ex_lockvar(eap)
3533 exarg_T *eap;
3534{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003536 int deep = 2;
3537
3538 if (eap->forceit)
3539 deep = -1;
3540 else if (vim_isdigit(*arg))
3541 {
3542 deep = getdigits(&arg);
3543 arg = skipwhite(arg);
3544 }
3545
3546 ex_unletlock(eap, arg, deep);
3547}
3548
3549/*
3550 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3551 */
3552 static void
3553ex_unletlock(eap, argstart, deep)
3554 exarg_T *eap;
3555 char_u *argstart;
3556 int deep;
3557{
3558 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003561 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 do
3564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003566 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003567 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 if (lv.ll_name == NULL)
3569 error = TRUE; /* error but continue parsing */
3570 if (name_end == NULL || (!vim_iswhite(*name_end)
3571 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003573 if (name_end != NULL)
3574 {
3575 emsg_severe = TRUE;
3576 EMSG(_(e_trailing));
3577 }
3578 if (!(eap->skip || error))
3579 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 break;
3581 }
3582
3583 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003584 {
3585 if (eap->cmdidx == CMD_unlet)
3586 {
3587 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3588 error = TRUE;
3589 }
3590 else
3591 {
3592 if (do_lock_var(&lv, name_end, deep,
3593 eap->cmdidx == CMD_lockvar) == FAIL)
3594 error = TRUE;
3595 }
3596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 if (!eap->skip)
3599 clear_lval(&lv);
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 arg = skipwhite(name_end);
3602 } while (!ends_excmd(*arg));
3603
3604 eap->nextcmd = check_nextcmd(arg);
3605}
3606
Bram Moolenaar8c711452005-01-14 21:53:12 +00003607 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003608do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003609 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003610 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003611 int forceit;
3612{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 int ret = OK;
3614 int cc;
3615
3616 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 cc = *name_end;
3619 *name_end = NUL;
3620
3621 /* Normal name or expanded name. */
3622 if (check_changedtick(lp->ll_name))
3623 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003625 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3629 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003630 else if (lp->ll_range)
3631 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003632 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633
3634 /* Delete a range of List items. */
3635 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3636 {
3637 li = lp->ll_li->li_next;
3638 listitem_remove(lp->ll_list, lp->ll_li);
3639 lp->ll_li = li;
3640 ++lp->ll_n1;
3641 }
3642 }
3643 else
3644 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003646 /* unlet a List item. */
3647 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003648 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003649 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003650 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 }
3652
3653 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003654}
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656/*
3657 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003658 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 */
3660 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664{
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 hashtab_T *ht;
3666 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003667 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003668 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
Bram Moolenaar33570922005-01-25 22:26:29 +00003670 ht = find_var_ht(name, &varname);
3671 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003673 hi = hash_find(ht, varname);
3674 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003675 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003676 di = HI2DI(hi);
3677 if (var_check_fixed(di->di_flags, name)
3678 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679 return FAIL;
3680 delete_var(ht, hi);
3681 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003684 if (forceit)
3685 return OK;
3686 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 return FAIL;
3688}
3689
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003690/*
3691 * Lock or unlock variable indicated by "lp".
3692 * "deep" is the levels to go (-1 for unlimited);
3693 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3694 */
3695 static int
3696do_lock_var(lp, name_end, deep, lock)
3697 lval_T *lp;
3698 char_u *name_end;
3699 int deep;
3700 int lock;
3701{
3702 int ret = OK;
3703 int cc;
3704 dictitem_T *di;
3705
3706 if (deep == 0) /* nothing to do */
3707 return OK;
3708
3709 if (lp->ll_tv == NULL)
3710 {
3711 cc = *name_end;
3712 *name_end = NUL;
3713
3714 /* Normal name or expanded name. */
3715 if (check_changedtick(lp->ll_name))
3716 ret = FAIL;
3717 else
3718 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003719 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 if (di == NULL)
3721 ret = FAIL;
3722 else
3723 {
3724 if (lock)
3725 di->di_flags |= DI_FLAGS_LOCK;
3726 else
3727 di->di_flags &= ~DI_FLAGS_LOCK;
3728 item_lock(&di->di_tv, deep, lock);
3729 }
3730 }
3731 *name_end = cc;
3732 }
3733 else if (lp->ll_range)
3734 {
3735 listitem_T *li = lp->ll_li;
3736
3737 /* (un)lock a range of List items. */
3738 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3739 {
3740 item_lock(&li->li_tv, deep, lock);
3741 li = li->li_next;
3742 ++lp->ll_n1;
3743 }
3744 }
3745 else if (lp->ll_list != NULL)
3746 /* (un)lock a List item. */
3747 item_lock(&lp->ll_li->li_tv, deep, lock);
3748 else
3749 /* un(lock) a Dictionary item. */
3750 item_lock(&lp->ll_di->di_tv, deep, lock);
3751
3752 return ret;
3753}
3754
3755/*
3756 * Lock or unlock an item. "deep" is nr of levels to go.
3757 */
3758 static void
3759item_lock(tv, deep, lock)
3760 typval_T *tv;
3761 int deep;
3762 int lock;
3763{
3764 static int recurse = 0;
3765 list_T *l;
3766 listitem_T *li;
3767 dict_T *d;
3768 hashitem_T *hi;
3769 int todo;
3770
3771 if (recurse >= DICT_MAXNEST)
3772 {
3773 EMSG(_("E743: variable nested too deep for (un)lock"));
3774 return;
3775 }
3776 if (deep == 0)
3777 return;
3778 ++recurse;
3779
3780 /* lock/unlock the item itself */
3781 if (lock)
3782 tv->v_lock |= VAR_LOCKED;
3783 else
3784 tv->v_lock &= ~VAR_LOCKED;
3785
3786 switch (tv->v_type)
3787 {
3788 case VAR_LIST:
3789 if ((l = tv->vval.v_list) != NULL)
3790 {
3791 if (lock)
3792 l->lv_lock |= VAR_LOCKED;
3793 else
3794 l->lv_lock &= ~VAR_LOCKED;
3795 if (deep < 0 || deep > 1)
3796 /* recursive: lock/unlock the items the List contains */
3797 for (li = l->lv_first; li != NULL; li = li->li_next)
3798 item_lock(&li->li_tv, deep - 1, lock);
3799 }
3800 break;
3801 case VAR_DICT:
3802 if ((d = tv->vval.v_dict) != NULL)
3803 {
3804 if (lock)
3805 d->dv_lock |= VAR_LOCKED;
3806 else
3807 d->dv_lock &= ~VAR_LOCKED;
3808 if (deep < 0 || deep > 1)
3809 {
3810 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003811 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003812 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3813 {
3814 if (!HASHITEM_EMPTY(hi))
3815 {
3816 --todo;
3817 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3818 }
3819 }
3820 }
3821 }
3822 }
3823 --recurse;
3824}
3825
Bram Moolenaara40058a2005-07-11 22:42:07 +00003826/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003827 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3828 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003829 */
3830 static int
3831tv_islocked(tv)
3832 typval_T *tv;
3833{
3834 return (tv->v_lock & VAR_LOCKED)
3835 || (tv->v_type == VAR_LIST
3836 && tv->vval.v_list != NULL
3837 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3838 || (tv->v_type == VAR_DICT
3839 && tv->vval.v_dict != NULL
3840 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3841}
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3844/*
3845 * Delete all "menutrans_" variables.
3846 */
3847 void
3848del_menutrans_vars()
3849{
Bram Moolenaar33570922005-01-25 22:26:29 +00003850 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003854 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003856 {
3857 if (!HASHITEM_EMPTY(hi))
3858 {
3859 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3861 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003862 }
3863 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003864 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865}
3866#endif
3867
3868#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3869
3870/*
3871 * Local string buffer for the next two functions to store a variable name
3872 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3873 * get_user_var_name().
3874 */
3875
3876static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3877
3878static char_u *varnamebuf = NULL;
3879static int varnamebuflen = 0;
3880
3881/*
3882 * Function to concatenate a prefix and a variable name.
3883 */
3884 static char_u *
3885cat_prefix_varname(prefix, name)
3886 int prefix;
3887 char_u *name;
3888{
3889 int len;
3890
3891 len = (int)STRLEN(name) + 3;
3892 if (len > varnamebuflen)
3893 {
3894 vim_free(varnamebuf);
3895 len += 10; /* some additional space */
3896 varnamebuf = alloc(len);
3897 if (varnamebuf == NULL)
3898 {
3899 varnamebuflen = 0;
3900 return NULL;
3901 }
3902 varnamebuflen = len;
3903 }
3904 *varnamebuf = prefix;
3905 varnamebuf[1] = ':';
3906 STRCPY(varnamebuf + 2, name);
3907 return varnamebuf;
3908}
3909
3910/*
3911 * Function given to ExpandGeneric() to obtain the list of user defined
3912 * (global/buffer/window/built-in) variable names.
3913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 char_u *
3915get_user_var_name(xp, idx)
3916 expand_T *xp;
3917 int idx;
3918{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003919 static long_u gdone;
3920 static long_u bdone;
3921 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003922#ifdef FEAT_WINDOWS
3923 static long_u tdone;
3924#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003925 static int vidx;
3926 static hashitem_T *hi;
3927 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003930 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003932#ifdef FEAT_WINDOWS
3933 tdone = 0;
3934#endif
3935 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003936
3937 /* Global variables */
3938 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003940 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003942 else
3943 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 while (HASHITEM_EMPTY(hi))
3945 ++hi;
3946 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3947 return cat_prefix_varname('g', hi->hi_key);
3948 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003950
3951 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003952 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003955 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003957 else
3958 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 while (HASHITEM_EMPTY(hi))
3960 ++hi;
3961 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 return (char_u *)"b:changedtick";
3967 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003968
3969 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003970 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003973 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003975 else
3976 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003977 while (HASHITEM_EMPTY(hi))
3978 ++hi;
3979 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003981
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003982#ifdef FEAT_WINDOWS
3983 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003984 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003985 if (tdone < ht->ht_used)
3986 {
3987 if (tdone++ == 0)
3988 hi = ht->ht_array;
3989 else
3990 ++hi;
3991 while (HASHITEM_EMPTY(hi))
3992 ++hi;
3993 return cat_prefix_varname('t', hi->hi_key);
3994 }
3995#endif
3996
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 /* v: variables */
3998 if (vidx < VV_LEN)
3999 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000
4001 vim_free(varnamebuf);
4002 varnamebuf = NULL;
4003 varnamebuflen = 0;
4004 return NULL;
4005}
4006
4007#endif /* FEAT_CMDL_COMPL */
4008
4009/*
4010 * types for expressions.
4011 */
4012typedef enum
4013{
4014 TYPE_UNKNOWN = 0
4015 , TYPE_EQUAL /* == */
4016 , TYPE_NEQUAL /* != */
4017 , TYPE_GREATER /* > */
4018 , TYPE_GEQUAL /* >= */
4019 , TYPE_SMALLER /* < */
4020 , TYPE_SEQUAL /* <= */
4021 , TYPE_MATCH /* =~ */
4022 , TYPE_NOMATCH /* !~ */
4023} exptype_T;
4024
4025/*
4026 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4029 */
4030
4031/*
4032 * Handle zero level expression.
4033 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004034 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004035 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 * Return OK or FAIL.
4037 */
4038 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004039eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 char_u **nextcmd;
4043 int evaluate;
4044{
4045 int ret;
4046 char_u *p;
4047
4048 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004049 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (ret == FAIL || !ends_excmd(*p))
4051 {
4052 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 /*
4055 * Report the invalid expression unless the expression evaluation has
4056 * been cancelled due to an aborting error, an interrupt, or an
4057 * exception.
4058 */
4059 if (!aborting())
4060 EMSG2(_(e_invexpr2), arg);
4061 ret = FAIL;
4062 }
4063 if (nextcmd != NULL)
4064 *nextcmd = check_nextcmd(p);
4065
4066 return ret;
4067}
4068
4069/*
4070 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004071 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 *
4073 * "arg" must point to the first non-white of the expression.
4074 * "arg" is advanced to the next non-white after the recognized expression.
4075 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004076 * Note: "rettv.v_lock" is not set.
4077 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 * Return OK or FAIL.
4079 */
4080 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 int evaluate;
4085{
4086 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004087 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
4089 /*
4090 * Get the first variable.
4091 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 return FAIL;
4094
4095 if ((*arg)[0] == '?')
4096 {
4097 result = FALSE;
4098 if (evaluate)
4099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004100 int error = FALSE;
4101
4102 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004105 if (error)
4106 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108
4109 /*
4110 * Get the second variable.
4111 */
4112 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004113 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 return FAIL;
4115
4116 /*
4117 * Check for the ":".
4118 */
4119 if ((*arg)[0] != ':')
4120 {
4121 EMSG(_("E109: Missing ':' after '?'"));
4122 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return FAIL;
4125 }
4126
4127 /*
4128 * Get the third variable.
4129 */
4130 *arg = skipwhite(*arg + 1);
4131 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4132 {
4133 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 return FAIL;
4136 }
4137 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004138 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140
4141 return OK;
4142}
4143
4144/*
4145 * Handle first level expression:
4146 * expr2 || expr2 || expr2 logical OR
4147 *
4148 * "arg" must point to the first non-white of the expression.
4149 * "arg" is advanced to the next non-white after the recognized expression.
4150 *
4151 * Return OK or FAIL.
4152 */
4153 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004154eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 int evaluate;
4158{
Bram Moolenaar33570922005-01-25 22:26:29 +00004159 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 long result;
4161 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004162 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163
4164 /*
4165 * Get the first variable.
4166 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004167 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 return FAIL;
4169
4170 /*
4171 * Repeat until there is no following "||".
4172 */
4173 first = TRUE;
4174 result = FALSE;
4175 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4176 {
4177 if (evaluate && first)
4178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004179 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004181 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004182 if (error)
4183 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 first = FALSE;
4185 }
4186
4187 /*
4188 * Get the second variable.
4189 */
4190 *arg = skipwhite(*arg + 2);
4191 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4192 return FAIL;
4193
4194 /*
4195 * Compute the result.
4196 */
4197 if (evaluate && !result)
4198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004202 if (error)
4203 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 if (evaluate)
4206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 rettv->v_type = VAR_NUMBER;
4208 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
4210 }
4211
4212 return OK;
4213}
4214
4215/*
4216 * Handle second level expression:
4217 * expr3 && expr3 && expr3 logical AND
4218 *
4219 * "arg" must point to the first non-white of the expression.
4220 * "arg" is advanced to the next non-white after the recognized expression.
4221 *
4222 * Return OK or FAIL.
4223 */
4224 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int evaluate;
4229{
Bram Moolenaar33570922005-01-25 22:26:29 +00004230 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 long result;
4232 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
4235 /*
4236 * Get the first variable.
4237 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 return FAIL;
4240
4241 /*
4242 * Repeat until there is no following "&&".
4243 */
4244 first = TRUE;
4245 result = TRUE;
4246 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4247 {
4248 if (evaluate && first)
4249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004253 if (error)
4254 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 first = FALSE;
4256 }
4257
4258 /*
4259 * Get the second variable.
4260 */
4261 *arg = skipwhite(*arg + 2);
4262 if (eval4(arg, &var2, evaluate && result) == FAIL)
4263 return FAIL;
4264
4265 /*
4266 * Compute the result.
4267 */
4268 if (evaluate && result)
4269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004270 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004273 if (error)
4274 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 if (evaluate)
4277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004278 rettv->v_type = VAR_NUMBER;
4279 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 }
4282
4283 return OK;
4284}
4285
4286/*
4287 * Handle third level expression:
4288 * var1 == var2
4289 * var1 =~ var2
4290 * var1 != var2
4291 * var1 !~ var2
4292 * var1 > var2
4293 * var1 >= var2
4294 * var1 < var2
4295 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004296 * var1 is var2
4297 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 *
4299 * "arg" must point to the first non-white of the expression.
4300 * "arg" is advanced to the next non-white after the recognized expression.
4301 *
4302 * Return OK or FAIL.
4303 */
4304 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004305eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 int evaluate;
4309{
Bram Moolenaar33570922005-01-25 22:26:29 +00004310 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 char_u *p;
4312 int i;
4313 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004314 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 int len = 2;
4316 long n1, n2;
4317 char_u *s1, *s2;
4318 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4319 regmatch_T regmatch;
4320 int ic;
4321 char_u *save_cpo;
4322
4323 /*
4324 * Get the first variable.
4325 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 return FAIL;
4328
4329 p = *arg;
4330 switch (p[0])
4331 {
4332 case '=': if (p[1] == '=')
4333 type = TYPE_EQUAL;
4334 else if (p[1] == '~')
4335 type = TYPE_MATCH;
4336 break;
4337 case '!': if (p[1] == '=')
4338 type = TYPE_NEQUAL;
4339 else if (p[1] == '~')
4340 type = TYPE_NOMATCH;
4341 break;
4342 case '>': if (p[1] != '=')
4343 {
4344 type = TYPE_GREATER;
4345 len = 1;
4346 }
4347 else
4348 type = TYPE_GEQUAL;
4349 break;
4350 case '<': if (p[1] != '=')
4351 {
4352 type = TYPE_SMALLER;
4353 len = 1;
4354 }
4355 else
4356 type = TYPE_SEQUAL;
4357 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004358 case 'i': if (p[1] == 's')
4359 {
4360 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4361 len = 5;
4362 if (!vim_isIDc(p[len]))
4363 {
4364 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4365 type_is = TRUE;
4366 }
4367 }
4368 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370
4371 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004372 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 */
4374 if (type != TYPE_UNKNOWN)
4375 {
4376 /* extra question mark appended: ignore case */
4377 if (p[len] == '?')
4378 {
4379 ic = TRUE;
4380 ++len;
4381 }
4382 /* extra '#' appended: match case */
4383 else if (p[len] == '#')
4384 {
4385 ic = FALSE;
4386 ++len;
4387 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004388 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 else
4390 ic = p_ic;
4391
4392 /*
4393 * Get the second variable.
4394 */
4395 *arg = skipwhite(p + len);
4396 if (eval5(arg, &var2, evaluate) == FAIL)
4397 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004398 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 return FAIL;
4400 }
4401
4402 if (evaluate)
4403 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 if (type_is && rettv->v_type != var2.v_type)
4405 {
4406 /* For "is" a different type always means FALSE, for "notis"
4407 * it means TRUE. */
4408 n1 = (type == TYPE_NEQUAL);
4409 }
4410 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4411 {
4412 if (type_is)
4413 {
4414 n1 = (rettv->v_type == var2.v_type
4415 && rettv->vval.v_list == var2.vval.v_list);
4416 if (type == TYPE_NEQUAL)
4417 n1 = !n1;
4418 }
4419 else if (rettv->v_type != var2.v_type
4420 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4421 {
4422 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004423 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004425 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 clear_tv(rettv);
4427 clear_tv(&var2);
4428 return FAIL;
4429 }
4430 else
4431 {
4432 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004433 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4434 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 if (type == TYPE_NEQUAL)
4436 n1 = !n1;
4437 }
4438 }
4439
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004440 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4441 {
4442 if (type_is)
4443 {
4444 n1 = (rettv->v_type == var2.v_type
4445 && rettv->vval.v_dict == var2.vval.v_dict);
4446 if (type == TYPE_NEQUAL)
4447 n1 = !n1;
4448 }
4449 else if (rettv->v_type != var2.v_type
4450 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4451 {
4452 if (rettv->v_type != var2.v_type)
4453 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4454 else
4455 EMSG(_("E736: Invalid operation for Dictionary"));
4456 clear_tv(rettv);
4457 clear_tv(&var2);
4458 return FAIL;
4459 }
4460 else
4461 {
4462 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004463 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4464 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004465 if (type == TYPE_NEQUAL)
4466 n1 = !n1;
4467 }
4468 }
4469
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4471 {
4472 if (rettv->v_type != var2.v_type
4473 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4474 {
4475 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004476 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004477 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004478 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004479 clear_tv(rettv);
4480 clear_tv(&var2);
4481 return FAIL;
4482 }
4483 else
4484 {
4485 /* Compare two Funcrefs for being equal or unequal. */
4486 if (rettv->vval.v_string == NULL
4487 || var2.vval.v_string == NULL)
4488 n1 = FALSE;
4489 else
4490 n1 = STRCMP(rettv->vval.v_string,
4491 var2.vval.v_string) == 0;
4492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 }
4496
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004497#ifdef FEAT_FLOAT
4498 /*
4499 * If one of the two variables is a float, compare as a float.
4500 * When using "=~" or "!~", always compare as string.
4501 */
4502 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4503 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4504 {
4505 float_T f1, f2;
4506
4507 if (rettv->v_type == VAR_FLOAT)
4508 f1 = rettv->vval.v_float;
4509 else
4510 f1 = get_tv_number(rettv);
4511 if (var2.v_type == VAR_FLOAT)
4512 f2 = var2.vval.v_float;
4513 else
4514 f2 = get_tv_number(&var2);
4515 n1 = FALSE;
4516 switch (type)
4517 {
4518 case TYPE_EQUAL: n1 = (f1 == f2); break;
4519 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4520 case TYPE_GREATER: n1 = (f1 > f2); break;
4521 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4522 case TYPE_SMALLER: n1 = (f1 < f2); break;
4523 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4524 case TYPE_UNKNOWN:
4525 case TYPE_MATCH:
4526 case TYPE_NOMATCH: break; /* avoid gcc warning */
4527 }
4528 }
4529#endif
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /*
4532 * If one of the two variables is a number, compare as a number.
4533 * When using "=~" or "!~", always compare as string.
4534 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004535 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 n1 = get_tv_number(rettv);
4539 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 switch (type)
4541 {
4542 case TYPE_EQUAL: n1 = (n1 == n2); break;
4543 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4544 case TYPE_GREATER: n1 = (n1 > n2); break;
4545 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4546 case TYPE_SMALLER: n1 = (n1 < n2); break;
4547 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4548 case TYPE_UNKNOWN:
4549 case TYPE_MATCH:
4550 case TYPE_NOMATCH: break; /* avoid gcc warning */
4551 }
4552 }
4553 else
4554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004555 s1 = get_tv_string_buf(rettv, buf1);
4556 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4558 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4559 else
4560 i = 0;
4561 n1 = FALSE;
4562 switch (type)
4563 {
4564 case TYPE_EQUAL: n1 = (i == 0); break;
4565 case TYPE_NEQUAL: n1 = (i != 0); break;
4566 case TYPE_GREATER: n1 = (i > 0); break;
4567 case TYPE_GEQUAL: n1 = (i >= 0); break;
4568 case TYPE_SMALLER: n1 = (i < 0); break;
4569 case TYPE_SEQUAL: n1 = (i <= 0); break;
4570
4571 case TYPE_MATCH:
4572 case TYPE_NOMATCH:
4573 /* avoid 'l' flag in 'cpoptions' */
4574 save_cpo = p_cpo;
4575 p_cpo = (char_u *)"";
4576 regmatch.regprog = vim_regcomp(s2,
4577 RE_MAGIC + RE_STRING);
4578 regmatch.rm_ic = ic;
4579 if (regmatch.regprog != NULL)
4580 {
4581 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004582 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (type == TYPE_NOMATCH)
4584 n1 = !n1;
4585 }
4586 p_cpo = save_cpo;
4587 break;
4588
4589 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4590 }
4591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004592 clear_tv(rettv);
4593 clear_tv(&var2);
4594 rettv->v_type = VAR_NUMBER;
4595 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 }
4598
4599 return OK;
4600}
4601
4602/*
4603 * Handle fourth level expression:
4604 * + number addition
4605 * - number subtraction
4606 * . string concatenation
4607 *
4608 * "arg" must point to the first non-white of the expression.
4609 * "arg" is advanced to the next non-white after the recognized expression.
4610 *
4611 * Return OK or FAIL.
4612 */
4613 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 int evaluate;
4618{
Bram Moolenaar33570922005-01-25 22:26:29 +00004619 typval_T var2;
4620 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 int op;
4622 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004623#ifdef FEAT_FLOAT
4624 float_T f1 = 0, f2 = 0;
4625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 char_u *s1, *s2;
4627 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4628 char_u *p;
4629
4630 /*
4631 * Get the first variable.
4632 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004633 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 return FAIL;
4635
4636 /*
4637 * Repeat computing, until no '+', '-' or '.' is following.
4638 */
4639 for (;;)
4640 {
4641 op = **arg;
4642 if (op != '+' && op != '-' && op != '.')
4643 break;
4644
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004645 if ((op != '+' || rettv->v_type != VAR_LIST)
4646#ifdef FEAT_FLOAT
4647 && (op == '.' || rettv->v_type != VAR_FLOAT)
4648#endif
4649 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004650 {
4651 /* For "list + ...", an illegal use of the first operand as
4652 * a number cannot be determined before evaluating the 2nd
4653 * operand: if this is also a list, all is ok.
4654 * For "something . ...", "something - ..." or "non-list + ...",
4655 * we know that the first operand needs to be a string or number
4656 * without evaluating the 2nd operand. So check before to avoid
4657 * side effects after an error. */
4658 if (evaluate && get_tv_string_chk(rettv) == NULL)
4659 {
4660 clear_tv(rettv);
4661 return FAIL;
4662 }
4663 }
4664
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 /*
4666 * Get the second variable.
4667 */
4668 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004669 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 return FAIL;
4673 }
4674
4675 if (evaluate)
4676 {
4677 /*
4678 * Compute the result.
4679 */
4680 if (op == '.')
4681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004682 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4683 s2 = get_tv_string_buf_chk(&var2, buf2);
4684 if (s2 == NULL) /* type error ? */
4685 {
4686 clear_tv(rettv);
4687 clear_tv(&var2);
4688 return FAIL;
4689 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004690 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004691 clear_tv(rettv);
4692 rettv->v_type = VAR_STRING;
4693 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004695 else if (op == '+' && rettv->v_type == VAR_LIST
4696 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004697 {
4698 /* concatenate Lists */
4699 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4700 &var3) == FAIL)
4701 {
4702 clear_tv(rettv);
4703 clear_tv(&var2);
4704 return FAIL;
4705 }
4706 clear_tv(rettv);
4707 *rettv = var3;
4708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 else
4710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004711 int error = FALSE;
4712
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713#ifdef FEAT_FLOAT
4714 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004715 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004716 f1 = rettv->vval.v_float;
4717 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004719 else
4720#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004721 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004722 n1 = get_tv_number_chk(rettv, &error);
4723 if (error)
4724 {
4725 /* This can only happen for "list + non-list". For
4726 * "non-list + ..." or "something - ...", we returned
4727 * before evaluating the 2nd operand. */
4728 clear_tv(rettv);
4729 return FAIL;
4730 }
4731#ifdef FEAT_FLOAT
4732 if (var2.v_type == VAR_FLOAT)
4733 f1 = n1;
4734#endif
4735 }
4736#ifdef FEAT_FLOAT
4737 if (var2.v_type == VAR_FLOAT)
4738 {
4739 f2 = var2.vval.v_float;
4740 n2 = 0;
4741 }
4742 else
4743#endif
4744 {
4745 n2 = get_tv_number_chk(&var2, &error);
4746 if (error)
4747 {
4748 clear_tv(rettv);
4749 clear_tv(&var2);
4750 return FAIL;
4751 }
4752#ifdef FEAT_FLOAT
4753 if (rettv->v_type == VAR_FLOAT)
4754 f2 = n2;
4755#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004757 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004758
4759#ifdef FEAT_FLOAT
4760 /* If there is a float on either side the result is a float. */
4761 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4762 {
4763 if (op == '+')
4764 f1 = f1 + f2;
4765 else
4766 f1 = f1 - f2;
4767 rettv->v_type = VAR_FLOAT;
4768 rettv->vval.v_float = f1;
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004771#endif
4772 {
4773 if (op == '+')
4774 n1 = n1 + n2;
4775 else
4776 n1 = n1 - n2;
4777 rettv->v_type = VAR_NUMBER;
4778 rettv->vval.v_number = n1;
4779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 }
4784 return OK;
4785}
4786
4787/*
4788 * Handle fifth level expression:
4789 * * number multiplication
4790 * / number division
4791 * % number modulo
4792 *
4793 * "arg" must point to the first non-white of the expression.
4794 * "arg" is advanced to the next non-white after the recognized expression.
4795 *
4796 * Return OK or FAIL.
4797 */
4798 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004799eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004803 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804{
Bram Moolenaar33570922005-01-25 22:26:29 +00004805 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 int op;
4807 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#ifdef FEAT_FLOAT
4809 int use_float = FALSE;
4810 float_T f1 = 0, f2;
4811#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004812 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
4814 /*
4815 * Get the first variable.
4816 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004817 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return FAIL;
4819
4820 /*
4821 * Repeat computing, until no '*', '/' or '%' is following.
4822 */
4823 for (;;)
4824 {
4825 op = **arg;
4826 if (op != '*' && op != '/' && op != '%')
4827 break;
4828
4829 if (evaluate)
4830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831#ifdef FEAT_FLOAT
4832 if (rettv->v_type == VAR_FLOAT)
4833 {
4834 f1 = rettv->vval.v_float;
4835 use_float = TRUE;
4836 n1 = 0;
4837 }
4838 else
4839#endif
4840 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004841 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004842 if (error)
4843 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 }
4845 else
4846 n1 = 0;
4847
4848 /*
4849 * Get the second variable.
4850 */
4851 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004852 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 return FAIL;
4854
4855 if (evaluate)
4856 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857#ifdef FEAT_FLOAT
4858 if (var2.v_type == VAR_FLOAT)
4859 {
4860 if (!use_float)
4861 {
4862 f1 = n1;
4863 use_float = TRUE;
4864 }
4865 f2 = var2.vval.v_float;
4866 n2 = 0;
4867 }
4868 else
4869#endif
4870 {
4871 n2 = get_tv_number_chk(&var2, &error);
4872 clear_tv(&var2);
4873 if (error)
4874 return FAIL;
4875#ifdef FEAT_FLOAT
4876 if (use_float)
4877 f2 = n2;
4878#endif
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880
4881 /*
4882 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004883 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885#ifdef FEAT_FLOAT
4886 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004888 if (op == '*')
4889 f1 = f1 * f2;
4890 else if (op == '/')
4891 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004892# ifdef VMS
4893 /* VMS crashes on divide by zero, work around it */
4894 if (f2 == 0.0)
4895 {
4896 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004897 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004899 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004900 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004901 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004902 }
4903 else
4904 f1 = f1 / f2;
4905# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906 /* We rely on the floating point library to handle divide
4907 * by zero to result in "inf" and not a crash. */
4908 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004909# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004913 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914 return FAIL;
4915 }
4916 rettv->v_type = VAR_FLOAT;
4917 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
4919 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922 if (op == '*')
4923 n1 = n1 * n2;
4924 else if (op == '/')
4925 {
4926 if (n2 == 0) /* give an error message? */
4927 {
4928 if (n1 == 0)
4929 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4930 else if (n1 < 0)
4931 n1 = -0x7fffffffL;
4932 else
4933 n1 = 0x7fffffffL;
4934 }
4935 else
4936 n1 = n1 / n2;
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004939 {
4940 if (n2 == 0) /* give an error message? */
4941 n1 = 0;
4942 else
4943 n1 = n1 % n2;
4944 }
4945 rettv->v_type = VAR_NUMBER;
4946 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 }
4950
4951 return OK;
4952}
4953
4954/*
4955 * Handle sixth level expression:
4956 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004957 * "string" string constant
4958 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * &option-name option value
4960 * @r register contents
4961 * identifier variable value
4962 * function() function call
4963 * $VAR environment variable
4964 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004965 * [expr, expr] List
4966 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 *
4968 * Also handle:
4969 * ! in front logical NOT
4970 * - in front unary minus
4971 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004972 * trailing [] subscript in String or List
4973 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 *
4975 * "arg" must point to the first non-white of the expression.
4976 * "arg" is advanced to the next non-white after the recognized expression.
4977 *
4978 * Return OK or FAIL.
4979 */
4980 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004981eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004985 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 long n;
4988 int len;
4989 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 char_u *start_leader, *end_leader;
4991 int ret = OK;
4992 char_u *alias;
4993
4994 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004996 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000 /*
5001 * Skip '!' and '-' characters. They are handled later.
5002 */
5003 start_leader = *arg;
5004 while (**arg == '!' || **arg == '-' || **arg == '+')
5005 *arg = skipwhite(*arg + 1);
5006 end_leader = *arg;
5007
5008 switch (**arg)
5009 {
5010 /*
5011 * Number constant.
5012 */
5013 case '0':
5014 case '1':
5015 case '2':
5016 case '3':
5017 case '4':
5018 case '5':
5019 case '6':
5020 case '7':
5021 case '8':
5022 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 {
5024#ifdef FEAT_FLOAT
5025 char_u *p = skipdigits(*arg + 1);
5026 int get_float = FALSE;
5027
5028 /* We accept a float when the format matches
5029 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005030 * strict to avoid backwards compatibility problems.
5031 * Don't look for a float after the "." operator, so that
5032 * ":let vers = 1.2.3" doesn't fail. */
5033 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 get_float = TRUE;
5036 p = skipdigits(p + 2);
5037 if (*p == 'e' || *p == 'E')
5038 {
5039 ++p;
5040 if (*p == '-' || *p == '+')
5041 ++p;
5042 if (!vim_isdigit(*p))
5043 get_float = FALSE;
5044 else
5045 p = skipdigits(p + 1);
5046 }
5047 if (ASCII_ISALPHA(*p) || *p == '.')
5048 get_float = FALSE;
5049 }
5050 if (get_float)
5051 {
5052 float_T f;
5053
5054 *arg += string2float(*arg, &f);
5055 if (evaluate)
5056 {
5057 rettv->v_type = VAR_FLOAT;
5058 rettv->vval.v_float = f;
5059 }
5060 }
5061 else
5062#endif
5063 {
5064 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5065 *arg += len;
5066 if (evaluate)
5067 {
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n;
5070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 /*
5076 * String constant: "string".
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 break;
5086
5087 /*
5088 * List: [expr, expr]
5089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 * Dictionary: {key: val, key: val}
5095 */
5096 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5097 break;
5098
5099 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005100 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005102 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 break;
5104
5105 /*
5106 * Environment variable: $VAR.
5107 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 break;
5110
5111 /*
5112 * Register contents: @r.
5113 */
5114 case '@': ++*arg;
5115 if (evaluate)
5116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005118 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120 if (**arg != NUL)
5121 ++*arg;
5122 break;
5123
5124 /*
5125 * nested expression: (expression).
5126 */
5127 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005128 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (**arg == ')')
5130 ++*arg;
5131 else if (ret == OK)
5132 {
5133 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005134 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 ret = FAIL;
5136 }
5137 break;
5138
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 break;
5141 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142
5143 if (ret == NOTDONE)
5144 {
5145 /*
5146 * Must be a variable or function name.
5147 * Can also be a curly-braces kind of name: {expr}.
5148 */
5149 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005150 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151 if (alias != NULL)
5152 s = alias;
5153
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005154 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 ret = FAIL;
5156 else
5157 {
5158 if (**arg == '(') /* recursive! */
5159 {
5160 /* If "s" is the name of a variable of type VAR_FUNC
5161 * use its contents. */
5162 s = deref_func_name(s, &len);
5163
5164 /* Invoke the function. */
5165 ret = get_func_tv(s, len, rettv, arg,
5166 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005167 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005168
5169 /* If evaluate is FALSE rettv->v_type was not set in
5170 * get_func_tv, but it's needed in handle_subscript() to parse
5171 * what follows. So set it here. */
5172 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5173 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005174 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005175 rettv->v_type = VAR_FUNC;
5176 }
5177
Bram Moolenaar8c711452005-01-14 21:53:12 +00005178 /* Stop the expression evaluation when immediately
5179 * aborting on error, or when an interrupt occurred or
5180 * an exception was thrown but not caught. */
5181 if (aborting())
5182 {
5183 if (ret == OK)
5184 clear_tv(rettv);
5185 ret = FAIL;
5186 }
5187 }
5188 else if (evaluate)
Bram Moolenaar6d977d62014-01-14 15:24:39 +01005189 ret = get_var_tv(s, len, rettv, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005190 else
5191 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005193 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 }
5195
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 *arg = skipwhite(*arg);
5197
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005198 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5199 * expr(expr). */
5200 if (ret == OK)
5201 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202
5203 /*
5204 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5205 */
5206 if (ret == OK && evaluate && end_leader > start_leader)
5207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005209 int val = 0;
5210#ifdef FEAT_FLOAT
5211 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005213 if (rettv->v_type == VAR_FLOAT)
5214 f = rettv->vval.v_float;
5215 else
5216#endif
5217 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005218 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005220 clear_tv(rettv);
5221 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005223 else
5224 {
5225 while (end_leader > start_leader)
5226 {
5227 --end_leader;
5228 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005229 {
5230#ifdef FEAT_FLOAT
5231 if (rettv->v_type == VAR_FLOAT)
5232 f = !f;
5233 else
5234#endif
5235 val = !val;
5236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005238 {
5239#ifdef FEAT_FLOAT
5240 if (rettv->v_type == VAR_FLOAT)
5241 f = -f;
5242 else
5243#endif
5244 val = -val;
5245 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005246 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005247#ifdef FEAT_FLOAT
5248 if (rettv->v_type == VAR_FLOAT)
5249 {
5250 clear_tv(rettv);
5251 rettv->vval.v_float = f;
5252 }
5253 else
5254#endif
5255 {
5256 clear_tv(rettv);
5257 rettv->v_type = VAR_NUMBER;
5258 rettv->vval.v_number = val;
5259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262
5263 return ret;
5264}
5265
5266/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005267 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5268 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5270 */
5271 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005277{
5278 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005281 long len = -1;
5282 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005284 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005286 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005288 if (verbose)
5289 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 return FAIL;
5291 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005292#ifdef FEAT_FLOAT
5293 else if (rettv->v_type == VAR_FLOAT)
5294 {
5295 if (verbose)
5296 EMSG(_(e_float_as_string));
5297 return FAIL;
5298 }
5299#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300
Bram Moolenaar8c711452005-01-14 21:53:12 +00005301 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 /*
5304 * dict.name
5305 */
5306 key = *arg + 1;
5307 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5308 ;
5309 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 }
5313 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005314 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005315 /*
5316 * something[idx]
5317 *
5318 * Get the (first) variable from inside the [].
5319 */
5320 *arg = skipwhite(*arg + 1);
5321 if (**arg == ':')
5322 empty1 = TRUE;
5323 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5324 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5326 {
5327 /* not a number or string */
5328 clear_tv(&var1);
5329 return FAIL;
5330 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331
5332 /*
5333 * Get the second variable from inside the [:].
5334 */
5335 if (**arg == ':')
5336 {
5337 range = TRUE;
5338 *arg = skipwhite(*arg + 1);
5339 if (**arg == ']')
5340 empty2 = TRUE;
5341 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005343 if (!empty1)
5344 clear_tv(&var1);
5345 return FAIL;
5346 }
5347 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5348 {
5349 /* not a number or string */
5350 if (!empty1)
5351 clear_tv(&var1);
5352 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 return FAIL;
5354 }
5355 }
5356
5357 /* Check for the ']'. */
5358 if (**arg != ']')
5359 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005360 if (verbose)
5361 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005362 clear_tv(&var1);
5363 if (range)
5364 clear_tv(&var2);
5365 return FAIL;
5366 }
5367 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 }
5369
5370 if (evaluate)
5371 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005372 n1 = 0;
5373 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 n1 = get_tv_number(&var1);
5376 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 }
5378 if (range)
5379 {
5380 if (empty2)
5381 n2 = -1;
5382 else
5383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 n2 = get_tv_number(&var2);
5385 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 }
5387 }
5388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005389 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 {
5391 case VAR_NUMBER:
5392 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005393 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394 len = (long)STRLEN(s);
5395 if (range)
5396 {
5397 /* The resulting variable is a substring. If the indexes
5398 * are out of range the result is empty. */
5399 if (n1 < 0)
5400 {
5401 n1 = len + n1;
5402 if (n1 < 0)
5403 n1 = 0;
5404 }
5405 if (n2 < 0)
5406 n2 = len + n2;
5407 else if (n2 >= len)
5408 n2 = len;
5409 if (n1 >= len || n2 < 0 || n1 > n2)
5410 s = NULL;
5411 else
5412 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5413 }
5414 else
5415 {
5416 /* The resulting variable is a string of a single
5417 * character. If the index is too big or negative the
5418 * result is empty. */
5419 if (n1 >= len || n1 < 0)
5420 s = NULL;
5421 else
5422 s = vim_strnsave(s + n1, 1);
5423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005424 clear_tv(rettv);
5425 rettv->v_type = VAR_STRING;
5426 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 break;
5428
5429 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 if (n1 < 0)
5432 n1 = len + n1;
5433 if (!empty1 && (n1 < 0 || n1 >= len))
5434 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005435 /* For a range we allow invalid values and return an empty
5436 * list. A list index out of range is an error. */
5437 if (!range)
5438 {
5439 if (verbose)
5440 EMSGN(_(e_listidx), n1);
5441 return FAIL;
5442 }
5443 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 }
5445 if (range)
5446 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005447 list_T *l;
5448 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449
5450 if (n2 < 0)
5451 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005452 else if (n2 >= len)
5453 n2 = len - 1;
5454 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005455 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 l = list_alloc();
5457 if (l == NULL)
5458 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 n1 <= n2; ++n1)
5461 {
5462 if (list_append_tv(l, &item->li_tv) == FAIL)
5463 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005464 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 return FAIL;
5466 }
5467 item = item->li_next;
5468 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 clear_tv(rettv);
5470 rettv->v_type = VAR_LIST;
5471 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005472 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 }
5474 else
5475 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005476 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 clear_tv(rettv);
5478 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005479 }
5480 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481
5482 case VAR_DICT:
5483 if (range)
5484 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005485 if (verbose)
5486 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005487 if (len == -1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005492 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005493
5494 if (len == -1)
5495 {
5496 key = get_tv_string(&var1);
5497 if (*key == NUL)
5498 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005499 if (verbose)
5500 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 clear_tv(&var1);
5502 return FAIL;
5503 }
5504 }
5505
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005506 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005508 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005509 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005510 if (len == -1)
5511 clear_tv(&var1);
5512 if (item == NULL)
5513 return FAIL;
5514
5515 copy_tv(&item->di_tv, &var1);
5516 clear_tv(rettv);
5517 *rettv = var1;
5518 }
5519 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 }
5521 }
5522
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005523 return OK;
5524}
5525
5526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 * Get an option value.
5528 * "arg" points to the '&' or '+' before the option name.
5529 * "arg" is advanced to character after the option name.
5530 * Return OK or FAIL.
5531 */
5532 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005535 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 int evaluate;
5537{
5538 char_u *option_end;
5539 long numval;
5540 char_u *stringval;
5541 int opt_type;
5542 int c;
5543 int working = (**arg == '+'); /* has("+option") */
5544 int ret = OK;
5545 int opt_flags;
5546
5547 /*
5548 * Isolate the option name and find its value.
5549 */
5550 option_end = find_option_end(arg, &opt_flags);
5551 if (option_end == NULL)
5552 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 EMSG2(_("E112: Option name missing: %s"), *arg);
5555 return FAIL;
5556 }
5557
5558 if (!evaluate)
5559 {
5560 *arg = option_end;
5561 return OK;
5562 }
5563
5564 c = *option_end;
5565 *option_end = NUL;
5566 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
5569 if (opt_type == -3) /* invalid name */
5570 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005571 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 EMSG2(_("E113: Unknown option: %s"), *arg);
5573 ret = FAIL;
5574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
5577 if (opt_type == -2) /* hidden string option */
5578 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005579 rettv->v_type = VAR_STRING;
5580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else if (opt_type == -1) /* hidden number option */
5583 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 rettv->v_type = VAR_NUMBER;
5585 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 }
5587 else if (opt_type == 1) /* number option */
5588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005589 rettv->v_type = VAR_NUMBER;
5590 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 }
5592 else /* string option */
5593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 rettv->v_type = VAR_STRING;
5595 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
5597 }
5598 else if (working && (opt_type == -2 || opt_type == -1))
5599 ret = FAIL;
5600
5601 *option_end = c; /* put back for error messages */
5602 *arg = option_end;
5603
5604 return ret;
5605}
5606
5607/*
5608 * Allocate a variable for a string constant.
5609 * Return OK or FAIL.
5610 */
5611 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 int evaluate;
5616{
5617 char_u *p;
5618 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 int extra = 0;
5620
5621 /*
5622 * Find the end of the string, skipping backslashed characters.
5623 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005624 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 {
5626 if (*p == '\\' && p[1] != NUL)
5627 {
5628 ++p;
5629 /* A "\<x>" form occupies at least 4 characters, and produces up
5630 * to 6 characters: reserve space for 2 extra */
5631 if (*p == '<')
5632 extra += 2;
5633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
5635
5636 if (*p != '"')
5637 {
5638 EMSG2(_("E114: Missing quote: %s"), *arg);
5639 return FAIL;
5640 }
5641
5642 /* If only parsing, set *arg and return here */
5643 if (!evaluate)
5644 {
5645 *arg = p + 1;
5646 return OK;
5647 }
5648
5649 /*
5650 * Copy the string into allocated memory, handling backslashed
5651 * characters.
5652 */
5653 name = alloc((unsigned)(p - *arg + extra));
5654 if (name == NULL)
5655 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 rettv->v_type = VAR_STRING;
5657 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 if (*p == '\\')
5662 {
5663 switch (*++p)
5664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 case 'b': *name++ = BS; ++p; break;
5666 case 'e': *name++ = ESC; ++p; break;
5667 case 'f': *name++ = FF; ++p; break;
5668 case 'n': *name++ = NL; ++p; break;
5669 case 'r': *name++ = CAR; ++p; break;
5670 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 case 'X': /* hex: "\x1", "\x12" */
5673 case 'x':
5674 case 'u': /* Unicode: "\u0023" */
5675 case 'U':
5676 if (vim_isxdigit(p[1]))
5677 {
5678 int n, nr;
5679 int c = toupper(*p);
5680
5681 if (c == 'X')
5682 n = 2;
5683 else
5684 n = 4;
5685 nr = 0;
5686 while (--n >= 0 && vim_isxdigit(p[1]))
5687 {
5688 ++p;
5689 nr = (nr << 4) + hex2nr(*p);
5690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692#ifdef FEAT_MBYTE
5693 /* For "\u" store the number according to
5694 * 'encoding'. */
5695 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 else
5698#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 break;
5702
5703 /* octal: "\1", "\12", "\123" */
5704 case '0':
5705 case '1':
5706 case '2':
5707 case '3':
5708 case '4':
5709 case '5':
5710 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 case '7': *name = *p++ - '0';
5712 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 *name = (*name << 3) + *p++ - '0';
5715 if (*p >= '0' && *p <= '7')
5716 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720
5721 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005722 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 if (extra != 0)
5724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005725 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 break;
5727 }
5728 /* FALLTHROUGH */
5729
Bram Moolenaar8c711452005-01-14 21:53:12 +00005730 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 break;
5732 }
5733 }
5734 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005735 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005738 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 *arg = p + 1;
5740
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 return OK;
5742}
5743
5744/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005745 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 * Return OK or FAIL.
5747 */
5748 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005749get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 int evaluate;
5753{
5754 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005756 int reduce = 0;
5757
5758 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005760 */
5761 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 break;
5767 ++reduce;
5768 ++p;
5769 }
5770 }
5771
Bram Moolenaar8c711452005-01-14 21:53:12 +00005772 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005773 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 return FAIL;
5776 }
5777
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779 if (!evaluate)
5780 {
5781 *arg = p + 1;
5782 return OK;
5783 }
5784
5785 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 */
5788 str = alloc((unsigned)((p - *arg) - reduce));
5789 if (str == NULL)
5790 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 rettv->v_type = VAR_STRING;
5792 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005796 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005797 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005799 break;
5800 ++p;
5801 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005803 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005805 *arg = p + 1;
5806
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 return OK;
5808}
5809
5810/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811 * Allocate a variable for a List and fill it from "*arg".
5812 * Return OK or FAIL.
5813 */
5814 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005815get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005816 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005817 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005818 int evaluate;
5819{
Bram Moolenaar33570922005-01-25 22:26:29 +00005820 list_T *l = NULL;
5821 typval_T tv;
5822 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005823
5824 if (evaluate)
5825 {
5826 l = list_alloc();
5827 if (l == NULL)
5828 return FAIL;
5829 }
5830
5831 *arg = skipwhite(*arg + 1);
5832 while (**arg != ']' && **arg != NUL)
5833 {
5834 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5835 goto failret;
5836 if (evaluate)
5837 {
5838 item = listitem_alloc();
5839 if (item != NULL)
5840 {
5841 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005842 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 list_append(l, item);
5844 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005845 else
5846 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847 }
5848
5849 if (**arg == ']')
5850 break;
5851 if (**arg != ',')
5852 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005853 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 goto failret;
5855 }
5856 *arg = skipwhite(*arg + 1);
5857 }
5858
5859 if (**arg != ']')
5860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862failret:
5863 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005864 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 return FAIL;
5866 }
5867
5868 *arg = skipwhite(*arg + 1);
5869 if (evaluate)
5870 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005871 rettv->v_type = VAR_LIST;
5872 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 ++l->lv_refcount;
5874 }
5875
5876 return OK;
5877}
5878
5879/*
5880 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005881 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005883 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884list_alloc()
5885{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005886 list_T *l;
5887
5888 l = (list_T *)alloc_clear(sizeof(list_T));
5889 if (l != NULL)
5890 {
5891 /* Prepend the list to the list of lists for garbage collection. */
5892 if (first_list != NULL)
5893 first_list->lv_used_prev = l;
5894 l->lv_used_prev = NULL;
5895 l->lv_used_next = first_list;
5896 first_list = l;
5897 }
5898 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005899}
5900
5901/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005902 * Allocate an empty list for a return value.
5903 * Returns OK or FAIL.
5904 */
5905 static int
5906rettv_list_alloc(rettv)
5907 typval_T *rettv;
5908{
5909 list_T *l = list_alloc();
5910
5911 if (l == NULL)
5912 return FAIL;
5913
5914 rettv->vval.v_list = l;
5915 rettv->v_type = VAR_LIST;
5916 ++l->lv_refcount;
5917 return OK;
5918}
5919
5920/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921 * Unreference a list: decrement the reference count and free it when it
5922 * becomes zero.
5923 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005924 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005927{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005928 if (l != NULL && --l->lv_refcount <= 0)
5929 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930}
5931
5932/*
5933 * Free a list, including all items it points to.
5934 * Ignores the reference count.
5935 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005936 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005937list_free(l, recurse)
5938 list_T *l;
5939 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940{
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005942
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005943 /* Remove the list from the list of lists for garbage collection. */
5944 if (l->lv_used_prev == NULL)
5945 first_list = l->lv_used_next;
5946 else
5947 l->lv_used_prev->lv_used_next = l->lv_used_next;
5948 if (l->lv_used_next != NULL)
5949 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5950
Bram Moolenaard9fba312005-06-26 22:34:35 +00005951 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005953 /* Remove the item before deleting it. */
5954 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005955 if (recurse || (item->li_tv.v_type != VAR_LIST
5956 && item->li_tv.v_type != VAR_DICT))
5957 clear_tv(&item->li_tv);
5958 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959 }
5960 vim_free(l);
5961}
5962
5963/*
5964 * Allocate a list item.
5965 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005966 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967listitem_alloc()
5968{
Bram Moolenaar33570922005-01-25 22:26:29 +00005969 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005970}
5971
5972/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005973 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005975 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005976listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005979 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 vim_free(item);
5981}
5982
5983/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005984 * Remove a list item from a List and free it. Also clears the value.
5985 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005986 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005987listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 list_T *l;
5989 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005990{
5991 list_remove(l, item, item);
5992 listitem_free(item);
5993}
5994
5995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996 * Get the number of items in a list.
5997 */
5998 static long
5999list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 if (l == NULL)
6003 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006004 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005}
6006
6007/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006008 * Return TRUE when two lists have exactly the same values.
6009 */
6010 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006011list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006012 list_T *l1;
6013 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006016{
Bram Moolenaar33570922005-01-25 22:26:29 +00006017 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006019 if (l1 == NULL || l2 == NULL)
6020 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006021 if (l1 == l2)
6022 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006023 if (list_len(l1) != list_len(l2))
6024 return FALSE;
6025
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006026 for (item1 = l1->lv_first, item2 = l2->lv_first;
6027 item1 != NULL && item2 != NULL;
6028 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006029 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006030 return FALSE;
6031 return item1 == NULL && item2 == NULL;
6032}
6033
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006034#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6035 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006036/*
6037 * Return the dictitem that an entry in a hashtable points to.
6038 */
6039 dictitem_T *
6040dict_lookup(hi)
6041 hashitem_T *hi;
6042{
6043 return HI2DI(hi);
6044}
6045#endif
6046
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006047/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048 * Return TRUE when two dictionaries have exactly the same key/values.
6049 */
6050 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006051dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 dict_T *d1;
6053 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006055 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 hashitem_T *hi;
6058 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006060
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006061 if (d1 == NULL || d2 == NULL)
6062 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006063 if (d1 == d2)
6064 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006065 if (dict_len(d1) != dict_len(d2))
6066 return FALSE;
6067
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006068 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006070 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006071 if (!HASHITEM_EMPTY(hi))
6072 {
6073 item2 = dict_find(d2, hi->hi_key, -1);
6074 if (item2 == NULL)
6075 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006076 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 return FALSE;
6078 --todo;
6079 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006080 }
6081 return TRUE;
6082}
6083
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084static int tv_equal_recurse_limit;
6085
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006086/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006087 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006088 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006089 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090 */
6091 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006092tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 typval_T *tv1;
6094 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 int ic; /* ignore case */
6096 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006097{
6098 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006099 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006100 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006101 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006102
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006103 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006104 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006105
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 * recursiveness to a limit. We guess they are equal then.
6108 * A fixed limit has the problem of still taking an awful long time.
6109 * Reduce the limit every time running into it. That should work fine for
6110 * deeply linked structures that are not recursively linked and catch
6111 * recursiveness quickly. */
6112 if (!recursive)
6113 tv_equal_recurse_limit = 1000;
6114 if (recursive_cnt >= tv_equal_recurse_limit)
6115 {
6116 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006117 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006119
6120 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006123 ++recursive_cnt;
6124 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6125 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006126 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006127
6128 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006129 ++recursive_cnt;
6130 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6131 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006132 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006133
6134 case VAR_FUNC:
6135 return (tv1->vval.v_string != NULL
6136 && tv2->vval.v_string != NULL
6137 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6138
6139 case VAR_NUMBER:
6140 return tv1->vval.v_number == tv2->vval.v_number;
6141
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006142#ifdef FEAT_FLOAT
6143 case VAR_FLOAT:
6144 return tv1->vval.v_float == tv2->vval.v_float;
6145#endif
6146
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006147 case VAR_STRING:
6148 s1 = get_tv_string_buf(tv1, buf1);
6149 s2 = get_tv_string_buf(tv2, buf2);
6150 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006151 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006152
6153 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006154 return TRUE;
6155}
6156
6157/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006158 * Locate item with index "n" in list "l" and return it.
6159 * A negative index is counted from the end; -1 is the last item.
6160 * Returns NULL when "n" is out of range.
6161 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006162 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 long n;
6166{
Bram Moolenaar33570922005-01-25 22:26:29 +00006167 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168 long idx;
6169
6170 if (l == NULL)
6171 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006172
6173 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006175 n = l->lv_len + n;
6176
6177 /* Check for index out of range. */
6178 if (n < 0 || n >= l->lv_len)
6179 return NULL;
6180
6181 /* When there is a cached index may start search from there. */
6182 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006183 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006184 if (n < l->lv_idx / 2)
6185 {
6186 /* closest to the start of the list */
6187 item = l->lv_first;
6188 idx = 0;
6189 }
6190 else if (n > (l->lv_idx + l->lv_len) / 2)
6191 {
6192 /* closest to the end of the list */
6193 item = l->lv_last;
6194 idx = l->lv_len - 1;
6195 }
6196 else
6197 {
6198 /* closest to the cached index */
6199 item = l->lv_idx_item;
6200 idx = l->lv_idx;
6201 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006202 }
6203 else
6204 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006205 if (n < l->lv_len / 2)
6206 {
6207 /* closest to the start of the list */
6208 item = l->lv_first;
6209 idx = 0;
6210 }
6211 else
6212 {
6213 /* closest to the end of the list */
6214 item = l->lv_last;
6215 idx = l->lv_len - 1;
6216 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006218
6219 while (n > idx)
6220 {
6221 /* search forward */
6222 item = item->li_next;
6223 ++idx;
6224 }
6225 while (n < idx)
6226 {
6227 /* search backward */
6228 item = item->li_prev;
6229 --idx;
6230 }
6231
6232 /* cache the used index */
6233 l->lv_idx = idx;
6234 l->lv_idx_item = item;
6235
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006236 return item;
6237}
6238
6239/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006240 * Get list item "l[idx]" as a number.
6241 */
6242 static long
6243list_find_nr(l, idx, errorp)
6244 list_T *l;
6245 long idx;
6246 int *errorp; /* set to TRUE when something wrong */
6247{
6248 listitem_T *li;
6249
6250 li = list_find(l, idx);
6251 if (li == NULL)
6252 {
6253 if (errorp != NULL)
6254 *errorp = TRUE;
6255 return -1L;
6256 }
6257 return get_tv_number_chk(&li->li_tv, errorp);
6258}
6259
6260/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006261 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6262 */
6263 char_u *
6264list_find_str(l, idx)
6265 list_T *l;
6266 long idx;
6267{
6268 listitem_T *li;
6269
6270 li = list_find(l, idx - 1);
6271 if (li == NULL)
6272 {
6273 EMSGN(_(e_listidx), idx);
6274 return NULL;
6275 }
6276 return get_tv_string(&li->li_tv);
6277}
6278
6279/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006280 * Locate "item" list "l" and return its index.
6281 * Returns -1 when "item" is not in the list.
6282 */
6283 static long
6284list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006285 list_T *l;
6286 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287{
6288 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006289 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290
6291 if (l == NULL)
6292 return -1;
6293 idx = 0;
6294 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6295 ++idx;
6296 if (li == NULL)
6297 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006298 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006299}
6300
6301/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 * Append item "item" to the end of list "l".
6303 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006304 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006306 list_T *l;
6307 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308{
6309 if (l->lv_last == NULL)
6310 {
6311 /* empty list */
6312 l->lv_first = item;
6313 l->lv_last = item;
6314 item->li_prev = NULL;
6315 }
6316 else
6317 {
6318 l->lv_last->li_next = item;
6319 item->li_prev = l->lv_last;
6320 l->lv_last = item;
6321 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006322 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323 item->li_next = NULL;
6324}
6325
6326/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006330 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006332 list_T *l;
6333 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006334{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006335 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006336
Bram Moolenaar05159a02005-02-26 23:04:13 +00006337 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006338 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006339 copy_tv(tv, &li->li_tv);
6340 list_append(l, li);
6341 return OK;
6342}
6343
6344/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006345 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006346 * Return FAIL when out of memory.
6347 */
6348 int
6349list_append_dict(list, dict)
6350 list_T *list;
6351 dict_T *dict;
6352{
6353 listitem_T *li = listitem_alloc();
6354
6355 if (li == NULL)
6356 return FAIL;
6357 li->li_tv.v_type = VAR_DICT;
6358 li->li_tv.v_lock = 0;
6359 li->li_tv.vval.v_dict = dict;
6360 list_append(list, li);
6361 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 return OK;
6363}
6364
6365/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006366 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006367 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006368 * Returns FAIL when out of memory.
6369 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006370 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006371list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006372 list_T *l;
6373 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006374 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375{
6376 listitem_T *li = listitem_alloc();
6377
6378 if (li == NULL)
6379 return FAIL;
6380 list_append(l, li);
6381 li->li_tv.v_type = VAR_STRING;
6382 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006383 if (str == NULL)
6384 li->li_tv.vval.v_string = NULL;
6385 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006386 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006387 return FAIL;
6388 return OK;
6389}
6390
6391/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006392 * Append "n" to list "l".
6393 * Returns FAIL when out of memory.
6394 */
6395 static int
6396list_append_number(l, n)
6397 list_T *l;
6398 varnumber_T n;
6399{
6400 listitem_T *li;
6401
6402 li = listitem_alloc();
6403 if (li == NULL)
6404 return FAIL;
6405 li->li_tv.v_type = VAR_NUMBER;
6406 li->li_tv.v_lock = 0;
6407 li->li_tv.vval.v_number = n;
6408 list_append(l, li);
6409 return OK;
6410}
6411
6412/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006414 * If "item" is NULL append at the end.
6415 * Return FAIL when out of memory.
6416 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006417 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006418list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006419 list_T *l;
6420 typval_T *tv;
6421 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006422{
Bram Moolenaar33570922005-01-25 22:26:29 +00006423 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424
6425 if (ni == NULL)
6426 return FAIL;
6427 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006428 list_insert(l, ni, item);
6429 return OK;
6430}
6431
6432 void
6433list_insert(l, ni, item)
6434 list_T *l;
6435 listitem_T *ni;
6436 listitem_T *item;
6437{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006438 if (item == NULL)
6439 /* Append new item at end of list. */
6440 list_append(l, ni);
6441 else
6442 {
6443 /* Insert new item before existing item. */
6444 ni->li_prev = item->li_prev;
6445 ni->li_next = item;
6446 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006447 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006448 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006449 ++l->lv_idx;
6450 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006452 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006454 l->lv_idx_item = NULL;
6455 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006457 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006459}
6460
6461/*
6462 * Extend "l1" with "l2".
6463 * If "bef" is NULL append at the end, otherwise insert before this item.
6464 * Returns FAIL when out of memory.
6465 */
6466 static int
6467list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l1;
6469 list_T *l2;
6470 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006473 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006475 /* We also quit the loop when we have inserted the original item count of
6476 * the list, avoid a hang when we extend a list with itself. */
6477 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6479 return FAIL;
6480 return OK;
6481}
6482
6483/*
6484 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6485 * Return FAIL when out of memory.
6486 */
6487 static int
6488list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006489 list_T *l1;
6490 list_T *l2;
6491 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006492{
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006495 if (l1 == NULL || l2 == NULL)
6496 return FAIL;
6497
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006498 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 if (l == NULL)
6501 return FAIL;
6502 tv->v_type = VAR_LIST;
6503 tv->vval.v_list = l;
6504
6505 /* append all items from the second list */
6506 return list_extend(l, l2, NULL);
6507}
6508
6509/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006510 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006512 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 * Returns NULL when out of memory.
6514 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006516list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006519 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520{
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 list_T *copy;
6522 listitem_T *item;
6523 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524
6525 if (orig == NULL)
6526 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006527
6528 copy = list_alloc();
6529 if (copy != NULL)
6530 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006531 if (copyID != 0)
6532 {
6533 /* Do this before adding the items, because one of the items may
6534 * refer back to this list. */
6535 orig->lv_copyID = copyID;
6536 orig->lv_copylist = copy;
6537 }
6538 for (item = orig->lv_first; item != NULL && !got_int;
6539 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 {
6541 ni = listitem_alloc();
6542 if (ni == NULL)
6543 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006545 {
6546 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6547 {
6548 vim_free(ni);
6549 break;
6550 }
6551 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006553 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006554 list_append(copy, ni);
6555 }
6556 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006557 if (item != NULL)
6558 {
6559 list_unref(copy);
6560 copy = NULL;
6561 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006562 }
6563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006564 return copy;
6565}
6566
6567/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006569 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006570 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006571 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006572list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 list_T *l;
6574 listitem_T *item;
6575 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576{
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006578
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 /* notify watchers */
6580 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006582 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 list_fix_watch(l, ip);
6584 if (ip == item2)
6585 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006587
6588 if (item2->li_next == NULL)
6589 l->lv_last = item->li_prev;
6590 else
6591 item2->li_next->li_prev = item->li_prev;
6592 if (item->li_prev == NULL)
6593 l->lv_first = item2->li_next;
6594 else
6595 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006596 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597}
6598
6599/*
6600 * Return an allocated string with the string representation of a list.
6601 * May return NULL.
6602 */
6603 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006604list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006605 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006606 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006607{
6608 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609
6610 if (tv->vval.v_list == NULL)
6611 return NULL;
6612 ga_init2(&ga, (int)sizeof(char), 80);
6613 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006614 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006615 {
6616 vim_free(ga.ga_data);
6617 return NULL;
6618 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 ga_append(&ga, ']');
6620 ga_append(&ga, NUL);
6621 return (char_u *)ga.ga_data;
6622}
6623
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006624typedef struct join_S {
6625 char_u *s;
6626 char_u *tofree;
6627} join_T;
6628
6629 static int
6630list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6631 garray_T *gap; /* to store the result in */
6632 list_T *l;
6633 char_u *sep;
6634 int echo_style;
6635 int copyID;
6636 garray_T *join_gap; /* to keep each list item string */
6637{
6638 int i;
6639 join_T *p;
6640 int len;
6641 int sumlen = 0;
6642 int first = TRUE;
6643 char_u *tofree;
6644 char_u numbuf[NUMBUFLEN];
6645 listitem_T *item;
6646 char_u *s;
6647
6648 /* Stringify each item in the list. */
6649 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6650 {
6651 if (echo_style)
6652 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6653 else
6654 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6655 if (s == NULL)
6656 return FAIL;
6657
6658 len = (int)STRLEN(s);
6659 sumlen += len;
6660
6661 ga_grow(join_gap, 1);
6662 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6663 if (tofree != NULL || s != numbuf)
6664 {
6665 p->s = s;
6666 p->tofree = tofree;
6667 }
6668 else
6669 {
6670 p->s = vim_strnsave(s, len);
6671 p->tofree = p->s;
6672 }
6673
6674 line_breakcheck();
6675 }
6676
6677 /* Allocate result buffer with its total size, avoid re-allocation and
6678 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6679 if (join_gap->ga_len >= 2)
6680 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6681 if (ga_grow(gap, sumlen + 2) == FAIL)
6682 return FAIL;
6683
6684 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6685 {
6686 if (first)
6687 first = FALSE;
6688 else
6689 ga_concat(gap, sep);
6690 p = ((join_T *)join_gap->ga_data) + i;
6691
6692 if (p->s != NULL)
6693 ga_concat(gap, p->s);
6694 line_breakcheck();
6695 }
6696
6697 return OK;
6698}
6699
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006701 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006702 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006703 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006704 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006705 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006706list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006707 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006708 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006709 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006710 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006711 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006712{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006713 garray_T join_ga;
6714 int retval;
6715 join_T *p;
6716 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006717
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006718 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6719 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6720
6721 /* Dispose each item in join_ga. */
6722 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 p = (join_T *)join_ga.ga_data;
6725 for (i = 0; i < join_ga.ga_len; ++i)
6726 {
6727 vim_free(p->tofree);
6728 ++p;
6729 }
6730 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006731 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732
6733 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734}
6735
6736/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006737 * Garbage collection for lists and dictionaries.
6738 *
6739 * We use reference counts to be able to free most items right away when they
6740 * are no longer used. But for composite items it's possible that it becomes
6741 * unused while the reference count is > 0: When there is a recursive
6742 * reference. Example:
6743 * :let l = [1, 2, 3]
6744 * :let d = {9: l}
6745 * :let l[1] = d
6746 *
6747 * Since this is quite unusual we handle this with garbage collection: every
6748 * once in a while find out which lists and dicts are not referenced from any
6749 * variable.
6750 *
6751 * Here is a good reference text about garbage collection (refers to Python
6752 * but it applies to all reference-counting mechanisms):
6753 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006754 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006755
6756/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006757 * Do garbage collection for lists and dicts.
6758 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006759 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006760 int
6761garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006762{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006763 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 buf_T *buf;
6765 win_T *wp;
6766 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006767 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768 int did_free;
6769 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006770#ifdef FEAT_WINDOWS
6771 tabpage_T *tp;
6772#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006773
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006774 /* Only do this once. */
6775 want_garbage_collect = FALSE;
6776 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006777 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006778
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006779 /* We advance by two because we add one for items referenced through
6780 * previous_funccal. */
6781 current_copyID += COPYID_INC;
6782 copyID = current_copyID;
6783
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784 /*
6785 * 1. Go through all accessible variables and mark all lists and dicts
6786 * with copyID.
6787 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006788
6789 /* Don't free variables in the previous_funccal list unless they are only
6790 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006791 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006792 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6793 {
6794 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6795 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6796 }
6797
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006798 /* script-local variables */
6799 for (i = 1; i <= ga_scripts.ga_len; ++i)
6800 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6801
6802 /* buffer-local variables */
6803 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006804 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805
6806 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006807 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006808 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006809#ifdef FEAT_AUTOCMD
6810 if (aucmd_win != NULL)
6811 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6812#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006813
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006814#ifdef FEAT_WINDOWS
6815 /* tabpage-local variables */
6816 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006817 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006818#endif
6819
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 /* global variables */
6821 set_ref_in_ht(&globvarht, copyID);
6822
6823 /* function-local variables */
6824 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6825 {
6826 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6827 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6828 }
6829
Bram Moolenaard812df62008-11-09 12:46:09 +00006830 /* v: vars */
6831 set_ref_in_ht(&vimvarht, copyID);
6832
Bram Moolenaar1dced572012-04-05 16:54:08 +02006833#ifdef FEAT_LUA
6834 set_ref_in_lua(copyID);
6835#endif
6836
Bram Moolenaardb913952012-06-29 12:54:53 +02006837#ifdef FEAT_PYTHON
6838 set_ref_in_python(copyID);
6839#endif
6840
6841#ifdef FEAT_PYTHON3
6842 set_ref_in_python3(copyID);
6843#endif
6844
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006845 /*
6846 * 2. Free lists and dictionaries that are not referenced.
6847 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 did_free = free_unref_items(copyID);
6849
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006850 /*
6851 * 3. Check if any funccal can be freed now.
6852 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 for (pfc = &previous_funccal; *pfc != NULL; )
6854 {
6855 if (can_free_funccal(*pfc, copyID))
6856 {
6857 fc = *pfc;
6858 *pfc = fc->caller;
6859 free_funccal(fc, TRUE);
6860 did_free = TRUE;
6861 did_free_funccal = TRUE;
6862 }
6863 else
6864 pfc = &(*pfc)->caller;
6865 }
6866 if (did_free_funccal)
6867 /* When a funccal was freed some more items might be garbage
6868 * collected, so run again. */
6869 (void)garbage_collect();
6870
6871 return did_free;
6872}
6873
6874/*
6875 * Free lists and dictionaries that are no longer referenced.
6876 */
6877 static int
6878free_unref_items(copyID)
6879 int copyID;
6880{
6881 dict_T *dd;
6882 list_T *ll;
6883 int did_free = FALSE;
6884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 */
6888 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006891 /* Free the Dictionary and ordinary items it contains, but don't
6892 * recurse into Lists and Dictionaries, they will be in the list
6893 * of dicts or list of lists. */
6894 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 did_free = TRUE;
6896
6897 /* restart, next dict may also have been freed */
6898 dd = first_dict;
6899 }
6900 else
6901 dd = dd->dv_used_next;
6902
6903 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006904 * Go through the list of lists and free items without the copyID.
6905 * But don't free a list that has a watcher (used in a for loop), these
6906 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 */
6908 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6910 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006912 /* Free the List and ordinary items it contains, but don't recurse
6913 * into Lists and Dictionaries, they will be in the list of dicts
6914 * or list of lists. */
6915 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 did_free = TRUE;
6917
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006918 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 ll = first_list;
6920 }
6921 else
6922 ll = ll->lv_used_next;
6923
6924 return did_free;
6925}
6926
6927/*
6928 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6929 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006930 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931set_ref_in_ht(ht, copyID)
6932 hashtab_T *ht;
6933 int copyID;
6934{
6935 int todo;
6936 hashitem_T *hi;
6937
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006938 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939 for (hi = ht->ht_array; todo > 0; ++hi)
6940 if (!HASHITEM_EMPTY(hi))
6941 {
6942 --todo;
6943 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6944 }
6945}
6946
6947/*
6948 * Mark all lists and dicts referenced through list "l" with "copyID".
6949 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006950 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951set_ref_in_list(l, copyID)
6952 list_T *l;
6953 int copyID;
6954{
6955 listitem_T *li;
6956
6957 for (li = l->lv_first; li != NULL; li = li->li_next)
6958 set_ref_in_item(&li->li_tv, copyID);
6959}
6960
6961/*
6962 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6963 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006964 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006965set_ref_in_item(tv, copyID)
6966 typval_T *tv;
6967 int copyID;
6968{
6969 dict_T *dd;
6970 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006971
6972 switch (tv->v_type)
6973 {
6974 case VAR_DICT:
6975 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006976 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 /* Didn't see this dict yet. */
6979 dd->dv_copyID = copyID;
6980 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006981 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006983
6984 case VAR_LIST:
6985 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006986 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006987 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 /* Didn't see this list yet. */
6989 ll->lv_copyID = copyID;
6990 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006991 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006995}
6996
6997/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006998 * Allocate an empty header for a dictionary.
6999 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007000 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001dict_alloc()
7002{
Bram Moolenaar33570922005-01-25 22:26:29 +00007003 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007004
Bram Moolenaar33570922005-01-25 22:26:29 +00007005 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007006 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007007 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007008 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 if (first_dict != NULL)
7010 first_dict->dv_used_prev = d;
7011 d->dv_used_next = first_dict;
7012 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007013 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007016 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007017 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007018 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007019 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007020 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007021 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007022}
7023
7024/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007025 * Allocate an empty dict for a return value.
7026 * Returns OK or FAIL.
7027 */
7028 static int
7029rettv_dict_alloc(rettv)
7030 typval_T *rettv;
7031{
7032 dict_T *d = dict_alloc();
7033
7034 if (d == NULL)
7035 return FAIL;
7036
7037 rettv->vval.v_dict = d;
7038 rettv->v_type = VAR_DICT;
7039 ++d->dv_refcount;
7040 return OK;
7041}
7042
7043
7044/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007045 * Unreference a Dictionary: decrement the reference count and free it when it
7046 * becomes zero.
7047 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007048 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007049dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007050 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007051{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007052 if (d != NULL && --d->dv_refcount <= 0)
7053 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007054}
7055
7056/*
7057 * Free a Dictionary, including all items it contains.
7058 * Ignores the reference count.
7059 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007060 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007061dict_free(d, recurse)
7062 dict_T *d;
7063 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007064{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007065 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007066 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007067 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007068
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 /* Remove the dict from the list of dicts for garbage collection. */
7070 if (d->dv_used_prev == NULL)
7071 first_dict = d->dv_used_next;
7072 else
7073 d->dv_used_prev->dv_used_next = d->dv_used_next;
7074 if (d->dv_used_next != NULL)
7075 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7076
7077 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007078 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007079 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007080 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007082 if (!HASHITEM_EMPTY(hi))
7083 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007084 /* Remove the item before deleting it, just in case there is
7085 * something recursive causing trouble. */
7086 di = HI2DI(hi);
7087 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007088 if (recurse || (di->di_tv.v_type != VAR_LIST
7089 && di->di_tv.v_type != VAR_DICT))
7090 clear_tv(&di->di_tv);
7091 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 --todo;
7093 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007094 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007096 vim_free(d);
7097}
7098
7099/*
7100 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007101 * The "key" is copied to the new item.
7102 * Note that the value of the item "di_tv" still needs to be initialized!
7103 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007104 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007105 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007106dictitem_alloc(key)
7107 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007108{
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007111 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007114 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007115 di->di_flags = 0;
7116 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007117 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118}
7119
7120/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007121 * Make a copy of a Dictionary item.
7122 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007124dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007126{
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007128
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007129 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7130 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007131 if (di != NULL)
7132 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007134 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007135 copy_tv(&org->di_tv, &di->di_tv);
7136 }
7137 return di;
7138}
7139
7140/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 * Remove item "item" from Dictionary "dict" and free it.
7142 */
7143 static void
7144dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dict_T *dict;
7146 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007147{
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007149
Bram Moolenaar33570922005-01-25 22:26:29 +00007150 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 if (HASHITEM_EMPTY(hi))
7152 EMSG2(_(e_intern2), "dictitem_remove()");
7153 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155 dictitem_free(item);
7156}
7157
7158/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007159 * Free a dict item. Also clears the value.
7160 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007161 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007162dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007164{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007165 clear_tv(&item->di_tv);
7166 vim_free(item);
7167}
7168
7169/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007170 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7171 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007173 * Returns NULL when out of memory.
7174 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007175 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007176dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007177 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007179 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180{
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *copy;
7182 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007183 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007184 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007185
7186 if (orig == NULL)
7187 return NULL;
7188
7189 copy = dict_alloc();
7190 if (copy != NULL)
7191 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 if (copyID != 0)
7193 {
7194 orig->dv_copyID = copyID;
7195 orig->dv_copydict = copy;
7196 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007197 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007198 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007200 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007202 --todo;
7203
7204 di = dictitem_alloc(hi->hi_key);
7205 if (di == NULL)
7206 break;
7207 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007208 {
7209 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7210 copyID) == FAIL)
7211 {
7212 vim_free(di);
7213 break;
7214 }
7215 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007216 else
7217 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7218 if (dict_add(copy, di) == FAIL)
7219 {
7220 dictitem_free(di);
7221 break;
7222 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007223 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007224 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007225
Bram Moolenaare9a41262005-01-15 22:18:47 +00007226 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 if (todo > 0)
7228 {
7229 dict_unref(copy);
7230 copy = NULL;
7231 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007232 }
7233
7234 return copy;
7235}
7236
7237/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007239 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007241 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 dict_T *d;
7244 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245{
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247}
7248
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007250 * Add a number or string entry to dictionary "d".
7251 * When "str" is NULL use number "nr", otherwise use "str".
7252 * Returns FAIL when out of memory and when key already exists.
7253 */
7254 int
7255dict_add_nr_str(d, key, nr, str)
7256 dict_T *d;
7257 char *key;
7258 long nr;
7259 char_u *str;
7260{
7261 dictitem_T *item;
7262
7263 item = dictitem_alloc((char_u *)key);
7264 if (item == NULL)
7265 return FAIL;
7266 item->di_tv.v_lock = 0;
7267 if (str == NULL)
7268 {
7269 item->di_tv.v_type = VAR_NUMBER;
7270 item->di_tv.vval.v_number = nr;
7271 }
7272 else
7273 {
7274 item->di_tv.v_type = VAR_STRING;
7275 item->di_tv.vval.v_string = vim_strsave(str);
7276 }
7277 if (dict_add(d, item) == FAIL)
7278 {
7279 dictitem_free(item);
7280 return FAIL;
7281 }
7282 return OK;
7283}
7284
7285/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007286 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007287 * Returns FAIL when out of memory and when key already exists.
7288 */
7289 int
7290dict_add_list(d, key, list)
7291 dict_T *d;
7292 char *key;
7293 list_T *list;
7294{
7295 dictitem_T *item;
7296
7297 item = dictitem_alloc((char_u *)key);
7298 if (item == NULL)
7299 return FAIL;
7300 item->di_tv.v_lock = 0;
7301 item->di_tv.v_type = VAR_LIST;
7302 item->di_tv.vval.v_list = list;
7303 if (dict_add(d, item) == FAIL)
7304 {
7305 dictitem_free(item);
7306 return FAIL;
7307 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007308 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007309 return OK;
7310}
7311
7312/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 * Get the number of items in a Dictionary.
7314 */
7315 static long
7316dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007317 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007318{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007319 if (d == NULL)
7320 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007321 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007322}
7323
7324/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007325 * Find item "key[len]" in Dictionary "d".
7326 * If "len" is negative use strlen(key).
7327 * Returns NULL when not found.
7328 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007329 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 char_u *key;
7333 int len;
7334{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335#define AKEYLEN 200
7336 char_u buf[AKEYLEN];
7337 char_u *akey;
7338 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007339 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341 if (len < 0)
7342 akey = key;
7343 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 tofree = akey = vim_strnsave(key, len);
7346 if (akey == NULL)
7347 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007348 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 else
7350 {
7351 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007352 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 akey = buf;
7354 }
7355
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 vim_free(tofree);
7358 if (HASHITEM_EMPTY(hi))
7359 return NULL;
7360 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361}
7362
7363/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007364 * Get a string item from a dictionary.
7365 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007366 * Returns NULL if the entry doesn't exist or out of memory.
7367 */
7368 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007369get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007370 dict_T *d;
7371 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007372 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007373{
7374 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007375 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007376
7377 di = dict_find(d, key, -1);
7378 if (di == NULL)
7379 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007380 s = get_tv_string(&di->di_tv);
7381 if (save && s != NULL)
7382 s = vim_strsave(s);
7383 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007384}
7385
7386/*
7387 * Get a number item from a dictionary.
7388 * Returns 0 if the entry doesn't exist or out of memory.
7389 */
7390 long
7391get_dict_number(d, key)
7392 dict_T *d;
7393 char_u *key;
7394{
7395 dictitem_T *di;
7396
7397 di = dict_find(d, key, -1);
7398 if (di == NULL)
7399 return 0;
7400 return get_tv_number(&di->di_tv);
7401}
7402
7403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 * Return an allocated string with the string representation of a Dictionary.
7405 * May return NULL.
7406 */
7407 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007408dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007410 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411{
7412 garray_T ga;
7413 int first = TRUE;
7414 char_u *tofree;
7415 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007418 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007419 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007421 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422 return NULL;
7423 ga_init2(&ga, (int)sizeof(char), 80);
7424 ga_append(&ga, '{');
7425
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007426 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007427 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007431 --todo;
7432
7433 if (first)
7434 first = FALSE;
7435 else
7436 ga_concat(&ga, (char_u *)", ");
7437
7438 tofree = string_quote(hi->hi_key, FALSE);
7439 if (tofree != NULL)
7440 {
7441 ga_concat(&ga, tofree);
7442 vim_free(tofree);
7443 }
7444 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007445 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007446 if (s != NULL)
7447 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007448 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007449 if (s == NULL)
7450 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007453 if (todo > 0)
7454 {
7455 vim_free(ga.ga_data);
7456 return NULL;
7457 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458
7459 ga_append(&ga, '}');
7460 ga_append(&ga, NUL);
7461 return (char_u *)ga.ga_data;
7462}
7463
7464/*
7465 * Allocate a variable for a Dictionary and fill it from "*arg".
7466 * Return OK or FAIL. Returns NOTDONE for {expr}.
7467 */
7468 static int
7469get_dict_tv(arg, rettv, evaluate)
7470 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007471 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472 int evaluate;
7473{
Bram Moolenaar33570922005-01-25 22:26:29 +00007474 dict_T *d = NULL;
7475 typval_T tvkey;
7476 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007477 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007478 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007480 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007481
7482 /*
7483 * First check if it's not a curly-braces thing: {expr}.
7484 * Must do this without evaluating, otherwise a function may be called
7485 * twice. Unfortunately this means we need to call eval1() twice for the
7486 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007487 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489 if (*start != '}')
7490 {
7491 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7492 return FAIL;
7493 if (*start == '}')
7494 return NOTDONE;
7495 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496
7497 if (evaluate)
7498 {
7499 d = dict_alloc();
7500 if (d == NULL)
7501 return FAIL;
7502 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 tvkey.v_type = VAR_UNKNOWN;
7504 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505
7506 *arg = skipwhite(*arg + 1);
7507 while (**arg != '}' && **arg != NUL)
7508 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 goto failret;
7511 if (**arg != ':')
7512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007513 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007514 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 goto failret;
7516 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007517 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007519 key = get_tv_string_buf_chk(&tvkey, buf);
7520 if (key == NULL || *key == NUL)
7521 {
7522 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7523 if (key != NULL)
7524 EMSG(_(e_emptykey));
7525 clear_tv(&tvkey);
7526 goto failret;
7527 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007528 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529
7530 *arg = skipwhite(*arg + 1);
7531 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7532 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007533 if (evaluate)
7534 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 goto failret;
7536 }
7537 if (evaluate)
7538 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540 if (item != NULL)
7541 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007542 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 clear_tv(&tv);
7545 goto failret;
7546 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 item = dictitem_alloc(key);
7548 clear_tv(&tvkey);
7549 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007552 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007553 if (dict_add(d, item) == FAIL)
7554 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007555 }
7556 }
7557
7558 if (**arg == '}')
7559 break;
7560 if (**arg != ',')
7561 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007562 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 goto failret;
7564 }
7565 *arg = skipwhite(*arg + 1);
7566 }
7567
7568 if (**arg != '}')
7569 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007570 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571failret:
7572 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007573 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007574 return FAIL;
7575 }
7576
7577 *arg = skipwhite(*arg + 1);
7578 if (evaluate)
7579 {
7580 rettv->v_type = VAR_DICT;
7581 rettv->vval.v_dict = d;
7582 ++d->dv_refcount;
7583 }
7584
7585 return OK;
7586}
7587
Bram Moolenaar8c711452005-01-14 21:53:12 +00007588/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007589 * Return a string with the string representation of a variable.
7590 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007591 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007592 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007593 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007594 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007595 */
7596 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007597echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007598 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007599 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007600 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007602{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007603 static int recurse = 0;
7604 char_u *r = NULL;
7605
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007607 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007608 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007609 *tofree = NULL;
7610 return NULL;
7611 }
7612 ++recurse;
7613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007614 switch (tv->v_type)
7615 {
7616 case VAR_FUNC:
7617 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 r = tv->vval.v_string;
7619 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007622 if (tv->vval.v_list == NULL)
7623 {
7624 *tofree = NULL;
7625 r = NULL;
7626 }
7627 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7628 {
7629 *tofree = NULL;
7630 r = (char_u *)"[...]";
7631 }
7632 else
7633 {
7634 tv->vval.v_list->lv_copyID = copyID;
7635 *tofree = list2string(tv, copyID);
7636 r = *tofree;
7637 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007638 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007639
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007641 if (tv->vval.v_dict == NULL)
7642 {
7643 *tofree = NULL;
7644 r = NULL;
7645 }
7646 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7647 {
7648 *tofree = NULL;
7649 r = (char_u *)"{...}";
7650 }
7651 else
7652 {
7653 tv->vval.v_dict->dv_copyID = copyID;
7654 *tofree = dict2string(tv, copyID);
7655 r = *tofree;
7656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007658
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 case VAR_STRING:
7660 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007661 *tofree = NULL;
7662 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007663 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007664
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007665#ifdef FEAT_FLOAT
7666 case VAR_FLOAT:
7667 *tofree = NULL;
7668 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7669 r = numbuf;
7670 break;
7671#endif
7672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007675 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007676 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007677
7678 --recurse;
7679 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680}
7681
7682/*
7683 * Return a string with the string representation of a variable.
7684 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7685 * "numbuf" is used for a number.
7686 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007687 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007688 */
7689 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007690tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007692 char_u **tofree;
7693 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007694 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007695{
7696 switch (tv->v_type)
7697 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007698 case VAR_FUNC:
7699 *tofree = string_quote(tv->vval.v_string, TRUE);
7700 return *tofree;
7701 case VAR_STRING:
7702 *tofree = string_quote(tv->vval.v_string, FALSE);
7703 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007704#ifdef FEAT_FLOAT
7705 case VAR_FLOAT:
7706 *tofree = NULL;
7707 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7708 return numbuf;
7709#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007710 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007712 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007713 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007714 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007715 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007716 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007717 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007718}
7719
7720/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007721 * Return string "str" in ' quotes, doubling ' characters.
7722 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007724 */
7725 static char_u *
7726string_quote(str, function)
7727 char_u *str;
7728 int function;
7729{
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007731 char_u *p, *r, *s;
7732
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 len = (function ? 13 : 3);
7734 if (str != NULL)
7735 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007736 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007737 for (p = str; *p != NUL; mb_ptr_adv(p))
7738 if (*p == '\'')
7739 ++len;
7740 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 s = r = alloc(len);
7742 if (r != NULL)
7743 {
7744 if (function)
7745 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007746 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007747 r += 10;
7748 }
7749 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007751 if (str != NULL)
7752 for (p = str; *p != NUL; )
7753 {
7754 if (*p == '\'')
7755 *r++ = '\'';
7756 MB_COPY_CHAR(p, r);
7757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007759 if (function)
7760 *r++ = ')';
7761 *r++ = NUL;
7762 }
7763 return s;
7764}
7765
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007766#ifdef FEAT_FLOAT
7767/*
7768 * Convert the string "text" to a floating point number.
7769 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7770 * this always uses a decimal point.
7771 * Returns the length of the text that was consumed.
7772 */
7773 static int
7774string2float(text, value)
7775 char_u *text;
7776 float_T *value; /* result stored here */
7777{
7778 char *s = (char *)text;
7779 float_T f;
7780
7781 f = strtod(s, &s);
7782 *value = f;
7783 return (int)((char_u *)s - text);
7784}
7785#endif
7786
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 * Get the value of an environment variable.
7789 * "arg" is pointing to the '$'. It is advanced to after the name.
7790 * If the environment variable was not set, silently assume it is empty.
7791 * Always return OK.
7792 */
7793 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007794get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 int evaluate;
7798{
7799 char_u *string = NULL;
7800 int len;
7801 int cc;
7802 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007803 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804
7805 ++*arg;
7806 name = *arg;
7807 len = get_env_len(arg);
7808 if (evaluate)
7809 {
7810 if (len != 0)
7811 {
7812 cc = name[len];
7813 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007814 /* first try vim_getenv(), fast for normal environment vars */
7815 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007817 {
7818 if (!mustfree)
7819 string = vim_strsave(string);
7820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 else
7822 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007823 if (mustfree)
7824 vim_free(string);
7825
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 /* next try expanding things like $VIM and ${HOME} */
7827 string = expand_env_save(name - 1);
7828 if (string != NULL && *string == '$')
7829 {
7830 vim_free(string);
7831 string = NULL;
7832 }
7833 }
7834 name[len] = cc;
7835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 rettv->v_type = VAR_STRING;
7837 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 }
7839
7840 return OK;
7841}
7842
7843/*
7844 * Array with names and number of arguments of all internal functions
7845 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7846 */
7847static struct fst
7848{
7849 char *f_name; /* function name */
7850 char f_min_argc; /* minimal number of arguments */
7851 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007853 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854} functions[] =
7855{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007856#ifdef FEAT_FLOAT
7857 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007858 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007859#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007860 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007861 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 {"append", 2, 2, f_append},
7863 {"argc", 0, 0, f_argc},
7864 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007865 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007866#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007867 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007869 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007872 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"bufexists", 1, 1, f_bufexists},
7874 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7875 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7876 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7877 {"buflisted", 1, 1, f_buflisted},
7878 {"bufloaded", 1, 1, f_bufloaded},
7879 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007880 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"bufwinnr", 1, 1, f_bufwinnr},
7882 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007883 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007884 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007885 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#ifdef FEAT_FLOAT
7887 {"ceil", 1, 1, f_ceil},
7888#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007889 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007890 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007892 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007894#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007895 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007896 {"complete_add", 1, 1, f_complete_add},
7897 {"complete_check", 0, 0, f_complete_check},
7898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007900 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007901#ifdef FEAT_FLOAT
7902 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007903 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007904#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007907 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007908 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"delete", 1, 1, f_delete},
7910 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007911 {"diff_filler", 1, 1, f_diff_filler},
7912 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007913 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"eventhandler", 0, 0, f_eventhandler},
7917 {"executable", 1, 1, f_executable},
7918 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007919#ifdef FEAT_FLOAT
7920 {"exp", 1, 1, f_exp},
7921#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007922 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007923 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007924 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7926 {"filereadable", 1, 1, f_filereadable},
7927 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007928 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007929 {"finddir", 1, 3, f_finddir},
7930 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007931#ifdef FEAT_FLOAT
7932 {"float2nr", 1, 1, f_float2nr},
7933 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007934 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007935#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007936 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 {"fnamemodify", 2, 2, f_fnamemodify},
7938 {"foldclosed", 1, 1, f_foldclosed},
7939 {"foldclosedend", 1, 1, f_foldclosedend},
7940 {"foldlevel", 1, 1, f_foldlevel},
7941 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007942 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007944 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007945 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007946 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007947 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007948 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"getchar", 0, 1, f_getchar},
7950 {"getcharmod", 0, 0, f_getcharmod},
7951 {"getcmdline", 0, 0, f_getcmdline},
7952 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007953 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007955 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007956 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"getfsize", 1, 1, f_getfsize},
7958 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007959 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007960 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007961 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007962 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007963 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007964 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007965 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007966 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007968 {"gettabvar", 2, 3, f_gettabvar},
7969 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"getwinposx", 0, 0, f_getwinposx},
7971 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007972 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007973 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007974 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007976 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007977 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007978 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7980 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7981 {"histadd", 2, 2, f_histadd},
7982 {"histdel", 1, 2, f_histdel},
7983 {"histget", 1, 2, f_histget},
7984 {"histnr", 1, 1, f_histnr},
7985 {"hlID", 1, 1, f_hlID},
7986 {"hlexists", 1, 1, f_hlexists},
7987 {"hostname", 0, 0, f_hostname},
7988 {"iconv", 3, 3, f_iconv},
7989 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007991 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007993 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {"inputrestore", 0, 0, f_inputrestore},
7995 {"inputsave", 0, 0, f_inputsave},
7996 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007997 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007998 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008000 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008001 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008002 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008003 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008005 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {"libcall", 3, 3, f_libcall},
8007 {"libcallnr", 3, 3, f_libcallnr},
8008 {"line", 1, 1, f_line},
8009 {"line2byte", 1, 1, f_line2byte},
8010 {"lispindent", 1, 1, f_lispindent},
8011 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008013 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008014 {"log10", 1, 1, f_log10},
8015#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008016#ifdef FEAT_LUA
8017 {"luaeval", 1, 2, f_luaeval},
8018#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008019 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008020 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008021 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008022 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008023 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008024 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008025 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008026 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008027 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008028 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008029 {"max", 1, 1, f_max},
8030 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008031#ifdef vim_mkdir
8032 {"mkdir", 1, 3, f_mkdir},
8033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008034 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008035#ifdef FEAT_MZSCHEME
8036 {"mzeval", 1, 1, f_mzeval},
8037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008039 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008040 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008041 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008042#ifdef FEAT_FLOAT
8043 {"pow", 2, 2, f_pow},
8044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008046 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008047 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008048#ifdef FEAT_PYTHON3
8049 {"py3eval", 1, 1, f_py3eval},
8050#endif
8051#ifdef FEAT_PYTHON
8052 {"pyeval", 1, 1, f_pyeval},
8053#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008055 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008056 {"reltime", 0, 2, f_reltime},
8057 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"remote_expr", 2, 3, f_remote_expr},
8059 {"remote_foreground", 1, 1, f_remote_foreground},
8060 {"remote_peek", 1, 2, f_remote_peek},
8061 {"remote_read", 1, 1, f_remote_read},
8062 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008063 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008065 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008067 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008068#ifdef FEAT_FLOAT
8069 {"round", 1, 1, f_round},
8070#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008071 {"screenattr", 2, 2, f_screenattr},
8072 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008073 {"screencol", 0, 0, f_screencol},
8074 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008075 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008076 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008077 {"searchpair", 3, 7, f_searchpair},
8078 {"searchpairpos", 3, 7, f_searchpairpos},
8079 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"server2client", 2, 2, f_server2client},
8081 {"serverlist", 0, 0, f_serverlist},
8082 {"setbufvar", 3, 3, f_setbufvar},
8083 {"setcmdpos", 1, 1, f_setcmdpos},
8084 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008085 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008086 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008087 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008088 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008090 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008091 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008093#ifdef FEAT_CRYPT
8094 {"sha256", 1, 1, f_sha256},
8095#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008096 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008097 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008099#ifdef FEAT_FLOAT
8100 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008101 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008102#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008103 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008104 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008105 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008106 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008107 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008108#ifdef FEAT_FLOAT
8109 {"sqrt", 1, 1, f_sqrt},
8110 {"str2float", 1, 1, f_str2float},
8111#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008112 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008113 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008114 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#ifdef HAVE_STRFTIME
8116 {"strftime", 1, 2, f_strftime},
8117#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008118 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"strlen", 1, 1, f_strlen},
8121 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008122 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008124 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 {"submatch", 1, 1, f_submatch},
8126 {"substitute", 4, 4, f_substitute},
8127 {"synID", 3, 3, f_synID},
8128 {"synIDattr", 2, 3, f_synIDattr},
8129 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008130 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008131 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008132 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008133 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008134 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008135 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008136 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008137 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008138#ifdef FEAT_FLOAT
8139 {"tan", 1, 1, f_tan},
8140 {"tanh", 1, 1, f_tanh},
8141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008143 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"tolower", 1, 1, f_tolower},
8145 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008146 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008147#ifdef FEAT_FLOAT
8148 {"trunc", 1, 1, f_trunc},
8149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008151 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008152 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008153 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"virtcol", 1, 1, f_virtcol},
8155 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008156 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 {"winbufnr", 1, 1, f_winbufnr},
8158 {"wincol", 0, 0, f_wincol},
8159 {"winheight", 1, 1, f_winheight},
8160 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008161 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008163 {"winrestview", 1, 1, f_winrestview},
8164 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008166 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008167 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168};
8169
8170#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8171
8172/*
8173 * Function given to ExpandGeneric() to obtain the list of internal
8174 * or user defined function names.
8175 */
8176 char_u *
8177get_function_name(xp, idx)
8178 expand_T *xp;
8179 int idx;
8180{
8181 static int intidx = -1;
8182 char_u *name;
8183
8184 if (idx == 0)
8185 intidx = -1;
8186 if (intidx < 0)
8187 {
8188 name = get_user_func_name(xp, idx);
8189 if (name != NULL)
8190 return name;
8191 }
8192 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8193 {
8194 STRCPY(IObuff, functions[intidx].f_name);
8195 STRCAT(IObuff, "(");
8196 if (functions[intidx].f_max_argc == 0)
8197 STRCAT(IObuff, ")");
8198 return IObuff;
8199 }
8200
8201 return NULL;
8202}
8203
8204/*
8205 * Function given to ExpandGeneric() to obtain the list of internal or
8206 * user defined variable or function names.
8207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 char_u *
8209get_expr_name(xp, idx)
8210 expand_T *xp;
8211 int idx;
8212{
8213 static int intidx = -1;
8214 char_u *name;
8215
8216 if (idx == 0)
8217 intidx = -1;
8218 if (intidx < 0)
8219 {
8220 name = get_function_name(xp, idx);
8221 if (name != NULL)
8222 return name;
8223 }
8224 return get_user_var_name(xp, ++intidx);
8225}
8226
8227#endif /* FEAT_CMDL_COMPL */
8228
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008229#if defined(EBCDIC) || defined(PROTO)
8230/*
8231 * Compare struct fst by function name.
8232 */
8233 static int
8234compare_func_name(s1, s2)
8235 const void *s1;
8236 const void *s2;
8237{
8238 struct fst *p1 = (struct fst *)s1;
8239 struct fst *p2 = (struct fst *)s2;
8240
8241 return STRCMP(p1->f_name, p2->f_name);
8242}
8243
8244/*
8245 * Sort the function table by function name.
8246 * The sorting of the table above is ASCII dependant.
8247 * On machines using EBCDIC we have to sort it.
8248 */
8249 static void
8250sortFunctions()
8251{
8252 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8253
8254 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8255}
8256#endif
8257
8258
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259/*
8260 * Find internal function in table above.
8261 * Return index, or -1 if not found
8262 */
8263 static int
8264find_internal_func(name)
8265 char_u *name; /* name of the function */
8266{
8267 int first = 0;
8268 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8269 int cmp;
8270 int x;
8271
8272 /*
8273 * Find the function name in the table. Binary search.
8274 */
8275 while (first <= last)
8276 {
8277 x = first + ((unsigned)(last - first) >> 1);
8278 cmp = STRCMP(name, functions[x].f_name);
8279 if (cmp < 0)
8280 last = x - 1;
8281 else if (cmp > 0)
8282 first = x + 1;
8283 else
8284 return x;
8285 }
8286 return -1;
8287}
8288
8289/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8291 * name it contains, otherwise return "name".
8292 */
8293 static char_u *
8294deref_func_name(name, lenp)
8295 char_u *name;
8296 int *lenp;
8297{
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008299 int cc;
8300
8301 cc = name[*lenp];
8302 name[*lenp] = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01008303 v = find_var(name, NULL, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008304 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008305 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008306 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008308 {
8309 *lenp = 0;
8310 return (char_u *)""; /* just in case */
8311 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008312 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008313 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008314 }
8315
8316 return name;
8317}
8318
8319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 * Allocate a variable for the result of a function.
8321 * Return OK or FAIL.
8322 */
8323 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008324get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8325 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 char_u *name; /* name of the function */
8327 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 char_u **arg; /* argument, pointing to the '(' */
8330 linenr_T firstline; /* first line of range */
8331 linenr_T lastline; /* last line of range */
8332 int *doesrange; /* return: function handled range */
8333 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
8336 char_u *argp;
8337 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008338 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 int argcount = 0; /* number of arguments found */
8340
8341 /*
8342 * Get the arguments.
8343 */
8344 argp = *arg;
8345 while (argcount < MAX_FUNC_ARGS)
8346 {
8347 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8348 if (*argp == ')' || *argp == ',' || *argp == NUL)
8349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8351 {
8352 ret = FAIL;
8353 break;
8354 }
8355 ++argcount;
8356 if (*argp != ',')
8357 break;
8358 }
8359 if (*argp == ')')
8360 ++argp;
8361 else
8362 ret = FAIL;
8363
8364 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008365 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008366 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008368 {
8369 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008370 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008371 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008372 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374
8375 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008376 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377
8378 *arg = skipwhite(argp);
8379 return ret;
8380}
8381
8382
8383/*
8384 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008385 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008386 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 */
8388 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008389call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008390 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008391 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008393 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008395 typval_T *argvars; /* vars for arguments, must have "argcount"
8396 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 linenr_T firstline; /* first line of range */
8398 linenr_T lastline; /* last line of range */
8399 int *doesrange; /* return: function handled range */
8400 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008401 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402{
8403 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404#define ERROR_UNKNOWN 0
8405#define ERROR_TOOMANY 1
8406#define ERROR_TOOFEW 2
8407#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008408#define ERROR_DICT 4
8409#define ERROR_NONE 5
8410#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 int error = ERROR_NONE;
8412 int i;
8413 int llen;
8414 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415#define FLEN_FIXED 40
8416 char_u fname_buf[FLEN_FIXED + 1];
8417 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008418 char_u *name;
8419
8420 /* Make a copy of the name, if it comes from a funcref variable it could
8421 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008422 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008423 if (name == NULL)
8424 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425
8426 /*
8427 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8428 * Change <SNR>123_name() to K_SNR 123_name().
8429 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 llen = eval_fname_script(name);
8432 if (llen > 0)
8433 {
8434 fname_buf[0] = K_SPECIAL;
8435 fname_buf[1] = KS_EXTRA;
8436 fname_buf[2] = (int)KE_SNR;
8437 i = 3;
8438 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8439 {
8440 if (current_SID <= 0)
8441 error = ERROR_SCRIPT;
8442 else
8443 {
8444 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8445 i = (int)STRLEN(fname_buf);
8446 }
8447 }
8448 if (i + STRLEN(name + llen) < FLEN_FIXED)
8449 {
8450 STRCPY(fname_buf + i, name + llen);
8451 fname = fname_buf;
8452 }
8453 else
8454 {
8455 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8456 if (fname == NULL)
8457 error = ERROR_OTHER;
8458 else
8459 {
8460 mch_memmove(fname, fname_buf, (size_t)i);
8461 STRCPY(fname + i, name + llen);
8462 }
8463 }
8464 }
8465 else
8466 fname = name;
8467
8468 *doesrange = FALSE;
8469
8470
8471 /* execute the function if no errors detected and executing */
8472 if (evaluate && error == ERROR_NONE)
8473 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008474 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8475 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 error = ERROR_UNKNOWN;
8477
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008478 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
8480 /*
8481 * User defined function.
8482 */
8483 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008484
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008486 /* Trigger FuncUndefined event, may load the function. */
8487 if (fp == NULL
8488 && apply_autocmds(EVENT_FUNCUNDEFINED,
8489 fname, fname, TRUE, NULL)
8490 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008492 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 fp = find_func(fname);
8494 }
8495#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008496 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008497 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008498 {
8499 /* loaded a package, search for the function again */
8500 fp = find_func(fname);
8501 }
8502
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 if (fp != NULL)
8504 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008505 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008507 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008509 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008511 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008512 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 else
8514 {
8515 /*
8516 * Call the user function.
8517 * Save and restore search patterns, script variables and
8518 * redo buffer.
8519 */
8520 save_search_patterns();
8521 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008522 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008523 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008524 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008525 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8526 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8527 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008528 /* Function was unreferenced while being used, free it
8529 * now. */
8530 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 restoreRedobuff();
8532 restore_search_patterns();
8533 error = ERROR_NONE;
8534 }
8535 }
8536 }
8537 else
8538 {
8539 /*
8540 * Find the function name in the table, call its implementation.
8541 */
8542 i = find_internal_func(fname);
8543 if (i >= 0)
8544 {
8545 if (argcount < functions[i].f_min_argc)
8546 error = ERROR_TOOFEW;
8547 else if (argcount > functions[i].f_max_argc)
8548 error = ERROR_TOOMANY;
8549 else
8550 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008551 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008552 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 error = ERROR_NONE;
8554 }
8555 }
8556 }
8557 /*
8558 * The function call (or "FuncUndefined" autocommand sequence) might
8559 * have been aborted by an error, an interrupt, or an explicitly thrown
8560 * exception that has not been caught so far. This situation can be
8561 * tested for by calling aborting(). For an error in an internal
8562 * function or for the "E132" error in call_user_func(), however, the
8563 * throw point at which the "force_abort" flag (temporarily reset by
8564 * emsg()) is normally updated has not been reached yet. We need to
8565 * update that flag first to make aborting() reliable.
8566 */
8567 update_force_abort();
8568 }
8569 if (error == ERROR_NONE)
8570 ret = OK;
8571
8572 /*
8573 * Report an error unless the argument evaluation or function call has been
8574 * cancelled due to an aborting error, an interrupt, or an exception.
8575 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008576 if (!aborting())
8577 {
8578 switch (error)
8579 {
8580 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008581 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008582 break;
8583 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008584 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008585 break;
8586 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008587 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008588 name);
8589 break;
8590 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008591 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008592 name);
8593 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008594 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008595 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008596 name);
8597 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008598 }
8599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 if (fname != name && fname != fname_buf)
8602 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008603 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
8605 return ret;
8606}
8607
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008608/*
8609 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008610 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008611 */
8612 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008613emsg_funcname(ermsg, name)
8614 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008615 char_u *name;
8616{
8617 char_u *p;
8618
8619 if (*name == K_SPECIAL)
8620 p = concat_str((char_u *)"<SNR>", name + 3);
8621 else
8622 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008623 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008624 if (p != name)
8625 vim_free(p);
8626}
8627
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008628/*
8629 * Return TRUE for a non-zero Number and a non-empty String.
8630 */
8631 static int
8632non_zero_arg(argvars)
8633 typval_T *argvars;
8634{
8635 return ((argvars[0].v_type == VAR_NUMBER
8636 && argvars[0].vval.v_number != 0)
8637 || (argvars[0].v_type == VAR_STRING
8638 && argvars[0].vval.v_string != NULL
8639 && *argvars[0].vval.v_string != NUL));
8640}
8641
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642/*********************************************
8643 * Implementation of the built-in functions
8644 */
8645
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008646#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008647static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8648
8649/*
8650 * Get the float value of "argvars[0]" into "f".
8651 * Returns FAIL when the argument is not a Number or Float.
8652 */
8653 static int
8654get_float_arg(argvars, f)
8655 typval_T *argvars;
8656 float_T *f;
8657{
8658 if (argvars[0].v_type == VAR_FLOAT)
8659 {
8660 *f = argvars[0].vval.v_float;
8661 return OK;
8662 }
8663 if (argvars[0].v_type == VAR_NUMBER)
8664 {
8665 *f = (float_T)argvars[0].vval.v_number;
8666 return OK;
8667 }
8668 EMSG(_("E808: Number or Float required"));
8669 return FAIL;
8670}
8671
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008672/*
8673 * "abs(expr)" function
8674 */
8675 static void
8676f_abs(argvars, rettv)
8677 typval_T *argvars;
8678 typval_T *rettv;
8679{
8680 if (argvars[0].v_type == VAR_FLOAT)
8681 {
8682 rettv->v_type = VAR_FLOAT;
8683 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8684 }
8685 else
8686 {
8687 varnumber_T n;
8688 int error = FALSE;
8689
8690 n = get_tv_number_chk(&argvars[0], &error);
8691 if (error)
8692 rettv->vval.v_number = -1;
8693 else if (n > 0)
8694 rettv->vval.v_number = n;
8695 else
8696 rettv->vval.v_number = -n;
8697 }
8698}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008699
8700/*
8701 * "acos()" function
8702 */
8703 static void
8704f_acos(argvars, rettv)
8705 typval_T *argvars;
8706 typval_T *rettv;
8707{
8708 float_T f;
8709
8710 rettv->v_type = VAR_FLOAT;
8711 if (get_float_arg(argvars, &f) == OK)
8712 rettv->vval.v_float = acos(f);
8713 else
8714 rettv->vval.v_float = 0.0;
8715}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008716#endif
8717
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008719 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 */
8721 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008722f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725{
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008728 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008729 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008731 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008732 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008733 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008734 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008735 }
8736 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008737 EMSG(_(e_listreq));
8738}
8739
8740/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008741 * "and(expr, expr)" function
8742 */
8743 static void
8744f_and(argvars, rettv)
8745 typval_T *argvars;
8746 typval_T *rettv;
8747{
8748 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8749 & get_tv_number_chk(&argvars[1], NULL);
8750}
8751
8752/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008753 * "append(lnum, string/list)" function
8754 */
8755 static void
8756f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008757 typval_T *argvars;
8758 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008759{
8760 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008761 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 list_T *l = NULL;
8763 listitem_T *li = NULL;
8764 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008765 long added = 0;
8766
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008767 /* When coming here from Insert mode, sync undo, so that this can be
8768 * undone separately from what was previously inserted. */
8769 if (u_sync_once == 2)
8770 {
8771 u_sync_once = 1; /* notify that u_sync() was called */
8772 u_sync(TRUE);
8773 }
8774
Bram Moolenaar0d660222005-01-07 21:51:51 +00008775 lnum = get_tv_lnum(argvars);
8776 if (lnum >= 0
8777 && lnum <= curbuf->b_ml.ml_line_count
8778 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008779 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008780 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008781 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008782 l = argvars[1].vval.v_list;
8783 if (l == NULL)
8784 return;
8785 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008786 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008787 for (;;)
8788 {
8789 if (l == NULL)
8790 tv = &argvars[1]; /* append a string */
8791 else if (li == NULL)
8792 break; /* end of list */
8793 else
8794 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008795 line = get_tv_string_chk(tv);
8796 if (line == NULL) /* type error */
8797 {
8798 rettv->vval.v_number = 1; /* Failed */
8799 break;
8800 }
8801 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008802 ++added;
8803 if (l == NULL)
8804 break;
8805 li = li->li_next;
8806 }
8807
8808 appended_lines_mark(lnum, added);
8809 if (curwin->w_cursor.lnum > lnum)
8810 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008812 else
8813 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
8815
8816/*
8817 * "argc()" function
8818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008821 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825}
8826
8827/*
8828 * "argidx()" function
8829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008832 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008835 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836}
8837
8838/*
8839 * "argv(nr)" function
8840 */
8841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008843 typval_T *argvars;
8844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
8846 int idx;
8847
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008848 if (argvars[0].v_type != VAR_UNKNOWN)
8849 {
8850 idx = get_tv_number_chk(&argvars[0], NULL);
8851 if (idx >= 0 && idx < ARGCOUNT)
8852 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8853 else
8854 rettv->vval.v_string = NULL;
8855 rettv->v_type = VAR_STRING;
8856 }
8857 else if (rettv_list_alloc(rettv) == OK)
8858 for (idx = 0; idx < ARGCOUNT; ++idx)
8859 list_append_string(rettv->vval.v_list,
8860 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861}
8862
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008863#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008864/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008865 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008866 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008867 static void
8868f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008869 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008870 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008871{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008872 float_T f;
8873
8874 rettv->v_type = VAR_FLOAT;
8875 if (get_float_arg(argvars, &f) == OK)
8876 rettv->vval.v_float = asin(f);
8877 else
8878 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008879}
8880
8881/*
8882 * "atan()" function
8883 */
8884 static void
8885f_atan(argvars, rettv)
8886 typval_T *argvars;
8887 typval_T *rettv;
8888{
8889 float_T f;
8890
8891 rettv->v_type = VAR_FLOAT;
8892 if (get_float_arg(argvars, &f) == OK)
8893 rettv->vval.v_float = atan(f);
8894 else
8895 rettv->vval.v_float = 0.0;
8896}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008897
8898/*
8899 * "atan2()" function
8900 */
8901 static void
8902f_atan2(argvars, rettv)
8903 typval_T *argvars;
8904 typval_T *rettv;
8905{
8906 float_T fx, fy;
8907
8908 rettv->v_type = VAR_FLOAT;
8909 if (get_float_arg(argvars, &fx) == OK
8910 && get_float_arg(&argvars[1], &fy) == OK)
8911 rettv->vval.v_float = atan2(fx, fy);
8912 else
8913 rettv->vval.v_float = 0.0;
8914}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008915#endif
8916
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917/*
8918 * "browse(save, title, initdir, default)" function
8919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008922 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924{
8925#ifdef FEAT_BROWSE
8926 int save;
8927 char_u *title;
8928 char_u *initdir;
8929 char_u *defname;
8930 char_u buf[NUMBUFLEN];
8931 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008932 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 save = get_tv_number_chk(&argvars[0], &error);
8935 title = get_tv_string_chk(&argvars[1]);
8936 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8937 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 if (error || title == NULL || initdir == NULL || defname == NULL)
8940 rettv->vval.v_string = NULL;
8941 else
8942 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008943 do_browse(save ? BROWSE_SAVE : 0,
8944 title, defname, NULL, initdir, NULL, curbuf);
8945#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008947#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008949}
8950
8951/*
8952 * "browsedir(title, initdir)" function
8953 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008956 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008957 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008958{
8959#ifdef FEAT_BROWSE
8960 char_u *title;
8961 char_u *initdir;
8962 char_u buf[NUMBUFLEN];
8963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964 title = get_tv_string_chk(&argvars[0]);
8965 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008966
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008967 if (title == NULL || initdir == NULL)
8968 rettv->vval.v_string = NULL;
8969 else
8970 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008971 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008973 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008975 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
8977
Bram Moolenaar33570922005-01-25 22:26:29 +00008978static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008979
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980/*
8981 * Find a buffer by number or exact name.
8982 */
8983 static buf_T *
8984find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008985 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986{
8987 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008989 if (avar->v_type == VAR_NUMBER)
8990 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008991 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008994 if (buf == NULL)
8995 {
8996 /* No full path name match, try a match with a URL or a "nofile"
8997 * buffer, these don't use the full path. */
8998 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8999 if (buf->b_fname != NULL
9000 && (path_with_url(buf->b_fname)
9001#ifdef FEAT_QUICKFIX
9002 || bt_nofile(buf)
9003#endif
9004 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009006 break;
9007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 }
9009 return buf;
9010}
9011
9012/*
9013 * "bufexists(expr)" function
9014 */
9015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009017 typval_T *argvars;
9018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021}
9022
9023/*
9024 * "buflisted(expr)" function
9025 */
9026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009028 typval_T *argvars;
9029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
9031 buf_T *buf;
9032
9033 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009034 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035}
9036
9037/*
9038 * "bufloaded(expr)" function
9039 */
9040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009041f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009042 typval_T *argvars;
9043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044{
9045 buf_T *buf;
9046
9047 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049}
9050
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009051static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009052
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053/*
9054 * Get buffer by number or pattern.
9055 */
9056 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009057get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009058 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009059 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 int save_magic;
9063 char_u *save_cpo;
9064 buf_T *buf;
9065
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009066 if (tv->v_type == VAR_NUMBER)
9067 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009068 if (tv->v_type != VAR_STRING)
9069 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 if (name == NULL || *name == NUL)
9071 return curbuf;
9072 if (name[0] == '$' && name[1] == NUL)
9073 return lastbuf;
9074
9075 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9076 save_magic = p_magic;
9077 p_magic = TRUE;
9078 save_cpo = p_cpo;
9079 p_cpo = (char_u *)"";
9080
9081 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009082 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
9084 p_magic = save_magic;
9085 p_cpo = save_cpo;
9086
9087 /* If not found, try expanding the name, like done for bufexists(). */
9088 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090
9091 return buf;
9092}
9093
9094/*
9095 * "bufname(expr)" function
9096 */
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 buf_T *buf;
9103
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009104 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009106 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 --emsg_off;
9113}
9114
9115/*
9116 * "bufnr(expr)" function
9117 */
9118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009119f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009120 typval_T *argvars;
9121 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122{
9123 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009124 int error = FALSE;
9125 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009127 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009129 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009130 --emsg_off;
9131
9132 /* If the buffer isn't found and the second argument is not zero create a
9133 * new buffer. */
9134 if (buf == NULL
9135 && argvars[1].v_type != VAR_UNKNOWN
9136 && get_tv_number_chk(&argvars[1], &error) != 0
9137 && !error
9138 && (name = get_tv_string_chk(&argvars[0])) != NULL
9139 && !error)
9140 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9141
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009143 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009145 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146}
9147
9148/*
9149 * "bufwinnr(nr)" function
9150 */
9151 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009153 typval_T *argvars;
9154 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155{
9156#ifdef FEAT_WINDOWS
9157 win_T *wp;
9158 int winnr = 0;
9159#endif
9160 buf_T *buf;
9161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009164 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165#ifdef FEAT_WINDOWS
9166 for (wp = firstwin; wp; wp = wp->w_next)
9167 {
9168 ++winnr;
9169 if (wp->w_buffer == buf)
9170 break;
9171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175#endif
9176 --emsg_off;
9177}
9178
9179/*
9180 * "byte2line(byte)" function
9181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009184 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189#else
9190 long boff = 0;
9191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009192 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 (linenr_T)0, &boff);
9198#endif
9199}
9200
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009201 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009202byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009203 typval_T *argvars;
9204 typval_T *rettv;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009205 int comp;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009206{
9207#ifdef FEAT_MBYTE
9208 char_u *t;
9209#endif
9210 char_u *str;
9211 long idx;
9212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009213 str = get_tv_string_chk(&argvars[0]);
9214 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009216 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009217 return;
9218
9219#ifdef FEAT_MBYTE
9220 t = str;
9221 for ( ; idx > 0; idx--)
9222 {
9223 if (*t == NUL) /* EOL reached */
9224 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009225 if (enc_utf8 && comp)
9226 t += utf_ptr2len(t);
9227 else
9228 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009229 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009230 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009231#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009232 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009233 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009234#endif
9235}
9236
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009237/*
9238 * "byteidx()" function
9239 */
9240 static void
9241f_byteidx(argvars, rettv)
9242 typval_T *argvars;
9243 typval_T *rettv;
9244{
9245 byteidx(argvars, rettv, FALSE);
9246}
9247
9248/*
9249 * "byteidxcomp()" function
9250 */
9251 static void
9252f_byteidxcomp(argvars, rettv)
9253 typval_T *argvars;
9254 typval_T *rettv;
9255{
9256 byteidx(argvars, rettv, TRUE);
9257}
9258
Bram Moolenaardb913952012-06-29 12:54:53 +02009259 int
9260func_call(name, args, selfdict, rettv)
9261 char_u *name;
9262 typval_T *args;
9263 dict_T *selfdict;
9264 typval_T *rettv;
9265{
9266 listitem_T *item;
9267 typval_T argv[MAX_FUNC_ARGS + 1];
9268 int argc = 0;
9269 int dummy;
9270 int r = 0;
9271
9272 for (item = args->vval.v_list->lv_first; item != NULL;
9273 item = item->li_next)
9274 {
9275 if (argc == MAX_FUNC_ARGS)
9276 {
9277 EMSG(_("E699: Too many arguments"));
9278 break;
9279 }
9280 /* Make a copy of each argument. This is needed to be able to set
9281 * v_lock to VAR_FIXED in the copy without changing the original list.
9282 */
9283 copy_tv(&item->li_tv, &argv[argc++]);
9284 }
9285
9286 if (item == NULL)
9287 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9288 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9289 &dummy, TRUE, selfdict);
9290
9291 /* Free the arguments. */
9292 while (argc > 0)
9293 clear_tv(&argv[--argc]);
9294
9295 return r;
9296}
9297
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009298/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009299 * "call(func, arglist)" function
9300 */
9301 static void
9302f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009303 typval_T *argvars;
9304 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009305{
9306 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009307 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009308
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009309 if (argvars[1].v_type != VAR_LIST)
9310 {
9311 EMSG(_(e_listreq));
9312 return;
9313 }
9314 if (argvars[1].vval.v_list == NULL)
9315 return;
9316
9317 if (argvars[0].v_type == VAR_FUNC)
9318 func = argvars[0].vval.v_string;
9319 else
9320 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009321 if (*func == NUL)
9322 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009323
Bram Moolenaare9a41262005-01-15 22:18:47 +00009324 if (argvars[2].v_type != VAR_UNKNOWN)
9325 {
9326 if (argvars[2].v_type != VAR_DICT)
9327 {
9328 EMSG(_(e_dictreq));
9329 return;
9330 }
9331 selfdict = argvars[2].vval.v_dict;
9332 }
9333
Bram Moolenaardb913952012-06-29 12:54:53 +02009334 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009335}
9336
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009337#ifdef FEAT_FLOAT
9338/*
9339 * "ceil({float})" function
9340 */
9341 static void
9342f_ceil(argvars, rettv)
9343 typval_T *argvars;
9344 typval_T *rettv;
9345{
9346 float_T f;
9347
9348 rettv->v_type = VAR_FLOAT;
9349 if (get_float_arg(argvars, &f) == OK)
9350 rettv->vval.v_float = ceil(f);
9351 else
9352 rettv->vval.v_float = 0.0;
9353}
9354#endif
9355
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009356/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009357 * "changenr()" function
9358 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009359 static void
9360f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009361 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009362 typval_T *rettv;
9363{
9364 rettv->vval.v_number = curbuf->b_u_seq_cur;
9365}
9366
9367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 * "char2nr(string)" function
9369 */
9370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009372 typval_T *argvars;
9373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374{
9375#ifdef FEAT_MBYTE
9376 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009377 {
9378 int utf8 = 0;
9379
9380 if (argvars[1].v_type != VAR_UNKNOWN)
9381 utf8 = get_tv_number_chk(&argvars[1], NULL);
9382
9383 if (utf8)
9384 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9385 else
9386 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 else
9389#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391}
9392
9393/*
9394 * "cindent(lnum)" function
9395 */
9396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009398 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400{
9401#ifdef FEAT_CINDENT
9402 pos_T pos;
9403 linenr_T lnum;
9404
9405 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9408 {
9409 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 curwin->w_cursor = pos;
9412 }
9413 else
9414#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009415 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416}
9417
9418/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009419 * "clearmatches()" function
9420 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009421 static void
9422f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009423 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009424 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009425{
9426#ifdef FEAT_SEARCH_EXTRA
9427 clear_matches(curwin);
9428#endif
9429}
9430
9431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 * "col(string)" function
9433 */
9434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009436 typval_T *argvars;
9437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 colnr_T col = 0;
9440 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009441 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009442
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009443 fp = var2fpos(&argvars[0], FALSE, &fnum);
9444 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 {
9446 if (fp->col == MAXCOL)
9447 {
9448 /* '> can be MAXCOL, get the length of the line then */
9449 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009450 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 else
9452 col = MAXCOL;
9453 }
9454 else
9455 {
9456 col = fp->col + 1;
9457#ifdef FEAT_VIRTUALEDIT
9458 /* col(".") when the cursor is on the NUL at the end of the line
9459 * because of "coladd" can be seen as an extra column. */
9460 if (virtual_active() && fp == &curwin->w_cursor)
9461 {
9462 char_u *p = ml_get_cursor();
9463
9464 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9465 curwin->w_virtcol - curwin->w_cursor.coladd))
9466 {
9467# ifdef FEAT_MBYTE
9468 int l;
9469
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009470 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 col += l;
9472# else
9473 if (*p != NUL && p[1] == NUL)
9474 ++col;
9475# endif
9476 }
9477 }
9478#endif
9479 }
9480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009481 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482}
9483
Bram Moolenaar572cb562005-08-05 21:35:02 +00009484#if defined(FEAT_INS_EXPAND)
9485/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009486 * "complete()" function
9487 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009488 static void
9489f_complete(argvars, rettv)
9490 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009491 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009492{
9493 int startcol;
9494
9495 if ((State & INSERT) == 0)
9496 {
9497 EMSG(_("E785: complete() can only be used in Insert mode"));
9498 return;
9499 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009500
9501 /* Check for undo allowed here, because if something was already inserted
9502 * the line was already saved for undo and this check isn't done. */
9503 if (!undo_allowed())
9504 return;
9505
Bram Moolenaarade00832006-03-10 21:46:58 +00009506 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9507 {
9508 EMSG(_(e_invarg));
9509 return;
9510 }
9511
9512 startcol = get_tv_number_chk(&argvars[0], NULL);
9513 if (startcol <= 0)
9514 return;
9515
9516 set_completion(startcol - 1, argvars[1].vval.v_list);
9517}
9518
9519/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009520 * "complete_add()" function
9521 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009522 static void
9523f_complete_add(argvars, rettv)
9524 typval_T *argvars;
9525 typval_T *rettv;
9526{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009527 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009528}
9529
9530/*
9531 * "complete_check()" function
9532 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009533 static void
9534f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009535 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009536 typval_T *rettv;
9537{
9538 int saved = RedrawingDisabled;
9539
9540 RedrawingDisabled = 0;
9541 ins_compl_check_keys(0);
9542 rettv->vval.v_number = compl_interrupted;
9543 RedrawingDisabled = saved;
9544}
9545#endif
9546
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547/*
9548 * "confirm(message, buttons[, default [, type]])" function
9549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009551f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009552 typval_T *argvars UNUSED;
9553 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554{
9555#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9556 char_u *message;
9557 char_u *buttons = NULL;
9558 char_u buf[NUMBUFLEN];
9559 char_u buf2[NUMBUFLEN];
9560 int def = 1;
9561 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009562 char_u *typestr;
9563 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009565 message = get_tv_string_chk(&argvars[0]);
9566 if (message == NULL)
9567 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009568 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009570 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9571 if (buttons == NULL)
9572 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009573 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009576 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009578 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9579 if (typestr == NULL)
9580 error = TRUE;
9581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 switch (TOUPPER_ASC(*typestr))
9584 {
9585 case 'E': type = VIM_ERROR; break;
9586 case 'Q': type = VIM_QUESTION; break;
9587 case 'I': type = VIM_INFO; break;
9588 case 'W': type = VIM_WARNING; break;
9589 case 'G': type = VIM_GENERIC; break;
9590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591 }
9592 }
9593 }
9594 }
9595
9596 if (buttons == NULL || *buttons == NUL)
9597 buttons = (char_u *)_("&Ok");
9598
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009599 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009600 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009601 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009602#endif
9603}
9604
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009605/*
9606 * "copy()" function
9607 */
9608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009610 typval_T *argvars;
9611 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009612{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009613 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009614}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009616#ifdef FEAT_FLOAT
9617/*
9618 * "cos()" function
9619 */
9620 static void
9621f_cos(argvars, rettv)
9622 typval_T *argvars;
9623 typval_T *rettv;
9624{
9625 float_T f;
9626
9627 rettv->v_type = VAR_FLOAT;
9628 if (get_float_arg(argvars, &f) == OK)
9629 rettv->vval.v_float = cos(f);
9630 else
9631 rettv->vval.v_float = 0.0;
9632}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009633
9634/*
9635 * "cosh()" function
9636 */
9637 static void
9638f_cosh(argvars, rettv)
9639 typval_T *argvars;
9640 typval_T *rettv;
9641{
9642 float_T f;
9643
9644 rettv->v_type = VAR_FLOAT;
9645 if (get_float_arg(argvars, &f) == OK)
9646 rettv->vval.v_float = cosh(f);
9647 else
9648 rettv->vval.v_float = 0.0;
9649}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009650#endif
9651
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009653 * "count()" function
9654 */
9655 static void
9656f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009657 typval_T *argvars;
9658 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009659{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009660 long n = 0;
9661 int ic = FALSE;
9662
Bram Moolenaare9a41262005-01-15 22:18:47 +00009663 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009665 listitem_T *li;
9666 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009667 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009668
Bram Moolenaare9a41262005-01-15 22:18:47 +00009669 if ((l = argvars[0].vval.v_list) != NULL)
9670 {
9671 li = l->lv_first;
9672 if (argvars[2].v_type != VAR_UNKNOWN)
9673 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009674 int error = FALSE;
9675
9676 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009677 if (argvars[3].v_type != VAR_UNKNOWN)
9678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009679 idx = get_tv_number_chk(&argvars[3], &error);
9680 if (!error)
9681 {
9682 li = list_find(l, idx);
9683 if (li == NULL)
9684 EMSGN(_(e_listidx), idx);
9685 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009686 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009687 if (error)
9688 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009689 }
9690
9691 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009692 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009693 ++n;
9694 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009695 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009696 else if (argvars[0].v_type == VAR_DICT)
9697 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009698 int todo;
9699 dict_T *d;
9700 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009701
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009702 if ((d = argvars[0].vval.v_dict) != NULL)
9703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009704 int error = FALSE;
9705
Bram Moolenaare9a41262005-01-15 22:18:47 +00009706 if (argvars[2].v_type != VAR_UNKNOWN)
9707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009708 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009709 if (argvars[3].v_type != VAR_UNKNOWN)
9710 EMSG(_(e_invarg));
9711 }
9712
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009713 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009715 {
9716 if (!HASHITEM_EMPTY(hi))
9717 {
9718 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009719 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009720 ++n;
9721 }
9722 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009723 }
9724 }
9725 else
9726 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009727 rettv->vval.v_number = n;
9728}
9729
9730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9732 *
9733 * Checks the existence of a cscope connection.
9734 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009736f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009737 typval_T *argvars UNUSED;
9738 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009739{
9740#ifdef FEAT_CSCOPE
9741 int num = 0;
9742 char_u *dbpath = NULL;
9743 char_u *prepend = NULL;
9744 char_u buf[NUMBUFLEN];
9745
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009746 if (argvars[0].v_type != VAR_UNKNOWN
9747 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749 num = (int)get_tv_number(&argvars[0]);
9750 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009751 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 }
9754
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009755 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756#endif
9757}
9758
9759/*
9760 * "cursor(lnum, col)" function
9761 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009762 * Moves the cursor to the specified line and column.
9763 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009766f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009767 typval_T *argvars;
9768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769{
9770 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009771#ifdef FEAT_VIRTUALEDIT
9772 long coladd = 0;
9773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009775 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009776 if (argvars[1].v_type == VAR_UNKNOWN)
9777 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009778 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009779
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009780 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009781 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009782 line = pos.lnum;
9783 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009784#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009785 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009786#endif
9787 }
9788 else
9789 {
9790 line = get_tv_lnum(argvars);
9791 col = get_tv_number_chk(&argvars[1], NULL);
9792#ifdef FEAT_VIRTUALEDIT
9793 if (argvars[2].v_type != VAR_UNKNOWN)
9794 coladd = get_tv_number_chk(&argvars[2], NULL);
9795#endif
9796 }
9797 if (line < 0 || col < 0
9798#ifdef FEAT_VIRTUALEDIT
9799 || coladd < 0
9800#endif
9801 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009802 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 if (line > 0)
9804 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805 if (col > 0)
9806 curwin->w_cursor.col = col - 1;
9807#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009808 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809#endif
9810
9811 /* Make sure the cursor is in a valid position. */
9812 check_cursor();
9813#ifdef FEAT_MBYTE
9814 /* Correct cursor for multi-byte character. */
9815 if (has_mbyte)
9816 mb_adjust_cursor();
9817#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009818
9819 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009820 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821}
9822
9823/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009824 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 */
9826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009828 typval_T *argvars;
9829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009831 int noref = 0;
9832
9833 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009834 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009835 if (noref < 0 || noref > 1)
9836 EMSG(_(e_invarg));
9837 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009838 {
9839 current_copyID += COPYID_INC;
9840 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842}
9843
9844/*
9845 * "delete()" function
9846 */
9847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009849 typval_T *argvars;
9850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851{
9852 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009855 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856}
9857
9858/*
9859 * "did_filetype()" function
9860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009863 typval_T *argvars UNUSED;
9864 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865{
9866#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868#endif
9869}
9870
9871/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009872 * "diff_filler()" function
9873 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009876 typval_T *argvars UNUSED;
9877 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009878{
9879#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009881#endif
9882}
9883
9884/*
9885 * "diff_hlID()" function
9886 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009889 typval_T *argvars UNUSED;
9890 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009891{
9892#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009893 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009894 static linenr_T prev_lnum = 0;
9895 static int changedtick = 0;
9896 static int fnum = 0;
9897 static int change_start = 0;
9898 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009899 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009900 int filler_lines;
9901 int col;
9902
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009903 if (lnum < 0) /* ignore type error in {lnum} arg */
9904 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009905 if (lnum != prev_lnum
9906 || changedtick != curbuf->b_changedtick
9907 || fnum != curbuf->b_fnum)
9908 {
9909 /* New line, buffer, change: need to get the values. */
9910 filler_lines = diff_check(curwin, lnum);
9911 if (filler_lines < 0)
9912 {
9913 if (filler_lines == -1)
9914 {
9915 change_start = MAXCOL;
9916 change_end = -1;
9917 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9918 hlID = HLF_ADD; /* added line */
9919 else
9920 hlID = HLF_CHD; /* changed line */
9921 }
9922 else
9923 hlID = HLF_ADD; /* added line */
9924 }
9925 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009926 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009927 prev_lnum = lnum;
9928 changedtick = curbuf->b_changedtick;
9929 fnum = curbuf->b_fnum;
9930 }
9931
9932 if (hlID == HLF_CHD || hlID == HLF_TXD)
9933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009934 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009935 if (col >= change_start && col <= change_end)
9936 hlID = HLF_TXD; /* changed text */
9937 else
9938 hlID = HLF_CHD; /* changed line */
9939 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009940 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009941#endif
9942}
9943
9944/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009945 * "empty({expr})" function
9946 */
9947 static void
9948f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009949 typval_T *argvars;
9950 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009951{
9952 int n;
9953
9954 switch (argvars[0].v_type)
9955 {
9956 case VAR_STRING:
9957 case VAR_FUNC:
9958 n = argvars[0].vval.v_string == NULL
9959 || *argvars[0].vval.v_string == NUL;
9960 break;
9961 case VAR_NUMBER:
9962 n = argvars[0].vval.v_number == 0;
9963 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009964#ifdef FEAT_FLOAT
9965 case VAR_FLOAT:
9966 n = argvars[0].vval.v_float == 0.0;
9967 break;
9968#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009969 case VAR_LIST:
9970 n = argvars[0].vval.v_list == NULL
9971 || argvars[0].vval.v_list->lv_first == NULL;
9972 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009973 case VAR_DICT:
9974 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009977 default:
9978 EMSG2(_(e_intern2), "f_empty()");
9979 n = 0;
9980 }
9981
9982 rettv->vval.v_number = n;
9983}
9984
9985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986 * "escape({string}, {chars})" function
9987 */
9988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009990 typval_T *argvars;
9991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992{
9993 char_u buf[NUMBUFLEN];
9994
Bram Moolenaar758711c2005-02-02 23:11:38 +00009995 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9996 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998}
9999
10000/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010001 * "eval()" function
10002 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010003 static void
10004f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010005 typval_T *argvars;
10006 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010007{
10008 char_u *s;
10009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010010 s = get_tv_string_chk(&argvars[0]);
10011 if (s != NULL)
10012 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010014 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10015 {
10016 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010017 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010018 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010019 else if (*s != NUL)
10020 EMSG(_(e_trailing));
10021}
10022
10023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010024 * "eventhandler()" function
10025 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010028 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032}
10033
10034/*
10035 * "executable()" function
10036 */
10037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010038f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010039 typval_T *argvars;
10040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043}
10044
10045/*
10046 * "exists()" function
10047 */
10048 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010049f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010050 typval_T *argvars;
10051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052{
10053 char_u *p;
10054 char_u *name;
10055 int n = FALSE;
10056 int len = 0;
10057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 if (*p == '$') /* environment variable */
10060 {
10061 /* first try "normal" environment variables (fast) */
10062 if (mch_getenv(p + 1) != NULL)
10063 n = TRUE;
10064 else
10065 {
10066 /* try expanding things like $VIM and ${HOME} */
10067 p = expand_env_save(p);
10068 if (p != NULL && *p != '$')
10069 n = TRUE;
10070 vim_free(p);
10071 }
10072 }
10073 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010076 if (*skipwhite(p) != NUL)
10077 n = FALSE; /* trailing garbage */
10078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 else if (*p == '*') /* internal or user defined function */
10080 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010081 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082 }
10083 else if (*p == ':')
10084 {
10085 n = cmd_exists(p + 1);
10086 }
10087 else if (*p == '#')
10088 {
10089#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010090 if (p[1] == '#')
10091 n = autocmd_supported(p + 2);
10092 else
10093 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094#endif
10095 }
10096 else /* internal variable */
10097 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010098 char_u *tofree;
10099 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010101 /* get_name_len() takes care of expanding curly braces */
10102 name = p;
10103 len = get_name_len(&p, &tofree, TRUE, FALSE);
10104 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010106 if (tofree != NULL)
10107 name = tofree;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010010108 n = (get_var_tv(name, len, &tv, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010109 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010111 /* handle d.key, l[idx], f(expr) */
10112 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10113 if (n)
10114 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 }
10116 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010117 if (*p != NUL)
10118 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010120 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010121 }
10122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124}
10125
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010126#ifdef FEAT_FLOAT
10127/*
10128 * "exp()" function
10129 */
10130 static void
10131f_exp(argvars, rettv)
10132 typval_T *argvars;
10133 typval_T *rettv;
10134{
10135 float_T f;
10136
10137 rettv->v_type = VAR_FLOAT;
10138 if (get_float_arg(argvars, &f) == OK)
10139 rettv->vval.v_float = exp(f);
10140 else
10141 rettv->vval.v_float = 0.0;
10142}
10143#endif
10144
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145/*
10146 * "expand()" function
10147 */
10148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010149f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010150 typval_T *argvars;
10151 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152{
10153 char_u *s;
10154 int len;
10155 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010156 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010159 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010162 if (argvars[1].v_type != VAR_UNKNOWN
10163 && argvars[2].v_type != VAR_UNKNOWN
10164 && get_tv_number_chk(&argvars[2], &error)
10165 && !error)
10166 {
10167 rettv->v_type = VAR_LIST;
10168 rettv->vval.v_list = NULL;
10169 }
10170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 if (*s == '%' || *s == '#' || *s == '<')
10173 {
10174 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010175 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010177 if (rettv->v_type == VAR_LIST)
10178 {
10179 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10180 list_append_string(rettv->vval.v_list, result, -1);
10181 else
10182 vim_free(result);
10183 }
10184 else
10185 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 }
10187 else
10188 {
10189 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010190 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010191 if (argvars[1].v_type != VAR_UNKNOWN
10192 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010193 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 if (!error)
10195 {
10196 ExpandInit(&xpc);
10197 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010198 if (p_wic)
10199 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010200 if (rettv->v_type == VAR_STRING)
10201 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10202 options, WILD_ALL);
10203 else if (rettv_list_alloc(rettv) != FAIL)
10204 {
10205 int i;
10206
10207 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10208 for (i = 0; i < xpc.xp_numfiles; i++)
10209 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10210 ExpandCleanup(&xpc);
10211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 }
10213 else
10214 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 }
10216}
10217
10218/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010219 * Go over all entries in "d2" and add them to "d1".
10220 * When "action" is "error" then a duplicate key is an error.
10221 * When "action" is "force" then a duplicate key is overwritten.
10222 * Otherwise duplicate keys are ignored ("action" is "keep").
10223 */
10224 void
10225dict_extend(d1, d2, action)
10226 dict_T *d1;
10227 dict_T *d2;
10228 char_u *action;
10229{
10230 dictitem_T *di1;
10231 hashitem_T *hi2;
10232 int todo;
10233
10234 todo = (int)d2->dv_hashtab.ht_used;
10235 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10236 {
10237 if (!HASHITEM_EMPTY(hi2))
10238 {
10239 --todo;
10240 di1 = dict_find(d1, hi2->hi_key, -1);
10241 if (d1->dv_scope != 0)
10242 {
10243 /* Disallow replacing a builtin function in l: and g:.
10244 * Check the key to be valid when adding to any
10245 * scope. */
10246 if (d1->dv_scope == VAR_DEF_SCOPE
10247 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10248 && var_check_func_name(hi2->hi_key,
10249 di1 == NULL))
10250 break;
10251 if (!valid_varname(hi2->hi_key))
10252 break;
10253 }
10254 if (di1 == NULL)
10255 {
10256 di1 = dictitem_copy(HI2DI(hi2));
10257 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10258 dictitem_free(di1);
10259 }
10260 else if (*action == 'e')
10261 {
10262 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10263 break;
10264 }
10265 else if (*action == 'f' && HI2DI(hi2) != di1)
10266 {
10267 clear_tv(&di1->di_tv);
10268 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10269 }
10270 }
10271 }
10272}
10273
10274/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010275 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010277 */
10278 static void
10279f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010280 typval_T *argvars;
10281 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010282{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010283 char *arg_errmsg = N_("extend() argument");
10284
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010286 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010287 list_T *l1, *l2;
10288 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010289 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010290 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010291
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 l1 = argvars[0].vval.v_list;
10293 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010294 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010295 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010296 {
10297 if (argvars[2].v_type != VAR_UNKNOWN)
10298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010299 before = get_tv_number_chk(&argvars[2], &error);
10300 if (error)
10301 return; /* type error; errmsg already given */
10302
Bram Moolenaar758711c2005-02-02 23:11:38 +000010303 if (before == l1->lv_len)
10304 item = NULL;
10305 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010306 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010307 item = list_find(l1, before);
10308 if (item == NULL)
10309 {
10310 EMSGN(_(e_listidx), before);
10311 return;
10312 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 }
10314 }
10315 else
10316 item = NULL;
10317 list_extend(l1, l2, item);
10318
Bram Moolenaare9a41262005-01-15 22:18:47 +000010319 copy_tv(&argvars[0], rettv);
10320 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010321 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010322 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10323 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010324 dict_T *d1, *d2;
10325 char_u *action;
10326 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010327
10328 d1 = argvars[0].vval.v_dict;
10329 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010330 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010331 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 {
10333 /* Check the third argument. */
10334 if (argvars[2].v_type != VAR_UNKNOWN)
10335 {
10336 static char *(av[]) = {"keep", "force", "error"};
10337
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010338 action = get_tv_string_chk(&argvars[2]);
10339 if (action == NULL)
10340 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010341 for (i = 0; i < 3; ++i)
10342 if (STRCMP(action, av[i]) == 0)
10343 break;
10344 if (i == 3)
10345 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010346 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010347 return;
10348 }
10349 }
10350 else
10351 action = (char_u *)"force";
10352
Bram Moolenaara9922d62013-05-30 13:01:18 +020010353 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010354
Bram Moolenaare9a41262005-01-15 22:18:47 +000010355 copy_tv(&argvars[0], rettv);
10356 }
10357 }
10358 else
10359 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010360}
10361
10362/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010363 * "feedkeys()" function
10364 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010365 static void
10366f_feedkeys(argvars, rettv)
10367 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010368 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010369{
10370 int remap = TRUE;
10371 char_u *keys, *flags;
10372 char_u nbuf[NUMBUFLEN];
10373 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010374 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010375
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010376 /* This is not allowed in the sandbox. If the commands would still be
10377 * executed in the sandbox it would be OK, but it probably happens later,
10378 * when "sandbox" is no longer set. */
10379 if (check_secure())
10380 return;
10381
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010382 keys = get_tv_string(&argvars[0]);
10383 if (*keys != NUL)
10384 {
10385 if (argvars[1].v_type != VAR_UNKNOWN)
10386 {
10387 flags = get_tv_string_buf(&argvars[1], nbuf);
10388 for ( ; *flags != NUL; ++flags)
10389 {
10390 switch (*flags)
10391 {
10392 case 'n': remap = FALSE; break;
10393 case 'm': remap = TRUE; break;
10394 case 't': typed = TRUE; break;
10395 }
10396 }
10397 }
10398
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010399 /* Need to escape K_SPECIAL and CSI before putting the string in the
10400 * typeahead buffer. */
10401 keys_esc = vim_strsave_escape_csi(keys);
10402 if (keys_esc != NULL)
10403 {
10404 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010405 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010406 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010407 if (vgetc_busy)
10408 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010409 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010410 }
10411}
10412
10413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414 * "filereadable()" function
10415 */
10416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010417f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 typval_T *argvars;
10419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010421 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422 char_u *p;
10423 int n;
10424
Bram Moolenaarc236c162008-07-13 17:41:49 +000010425#ifndef O_NONBLOCK
10426# define O_NONBLOCK 0
10427#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010429 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10430 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 {
10432 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010433 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 }
10435 else
10436 n = FALSE;
10437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439}
10440
10441/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010442 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 * rights to write into.
10444 */
10445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010446f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010447 typval_T *argvars;
10448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010450 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010451}
10452
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010453static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010454
10455 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010456findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010457 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010458 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010459 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010460{
10461#ifdef FEAT_SEARCHPATH
10462 char_u *fname;
10463 char_u *fresult = NULL;
10464 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10465 char_u *p;
10466 char_u pathbuf[NUMBUFLEN];
10467 int count = 1;
10468 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010469 int error = FALSE;
10470#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010471
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010472 rettv->vval.v_string = NULL;
10473 rettv->v_type = VAR_STRING;
10474
10475#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010477
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010478 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010480 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10481 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010482 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010483 else
10484 {
10485 if (*p != NUL)
10486 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010489 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010490 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010491 }
10492
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010493 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10494 error = TRUE;
10495
10496 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010498 do
10499 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010500 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010501 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010502 fresult = find_file_in_path_option(first ? fname : NULL,
10503 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010504 0, first, path,
10505 find_what,
10506 curbuf->b_ffname,
10507 find_what == FINDFILE_DIR
10508 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010509 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010510
10511 if (fresult != NULL && rettv->v_type == VAR_LIST)
10512 list_append_string(rettv->vval.v_list, fresult, -1);
10513
10514 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010515 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010516
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010517 if (rettv->v_type == VAR_STRING)
10518 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010519#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010520}
10521
Bram Moolenaar33570922005-01-25 22:26:29 +000010522static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10523static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010524
10525/*
10526 * Implementation of map() and filter().
10527 */
10528 static void
10529filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010530 typval_T *argvars;
10531 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010532 int map;
10533{
10534 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010535 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010536 listitem_T *li, *nli;
10537 list_T *l = NULL;
10538 dictitem_T *di;
10539 hashtab_T *ht;
10540 hashitem_T *hi;
10541 dict_T *d = NULL;
10542 typval_T save_val;
10543 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010545 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010546 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10547 char *arg_errmsg = (map ? N_("map() argument")
10548 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010549 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010550 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010551
Bram Moolenaare9a41262005-01-15 22:18:47 +000010552 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010553 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010554 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010555 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010556 return;
10557 }
10558 else if (argvars[0].v_type == VAR_DICT)
10559 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010560 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010561 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010562 return;
10563 }
10564 else
10565 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010566 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010567 return;
10568 }
10569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 expr = get_tv_string_buf_chk(&argvars[1], buf);
10571 /* On type errors, the preceding call has already displayed an error
10572 * message. Avoid a misleading error message for an empty string that
10573 * was not passed as argument. */
10574 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010575 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 prepare_vimvar(VV_VAL, &save_val);
10577 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010578
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010579 /* We reset "did_emsg" to be able to detect whether an error
10580 * occurred during evaluation of the expression. */
10581 save_did_emsg = did_emsg;
10582 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010583
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010584 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010586 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 vimvars[VV_KEY].vv_type = VAR_STRING;
10588
10589 ht = &d->dv_hashtab;
10590 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010591 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010592 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010593 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010594 if (!HASHITEM_EMPTY(hi))
10595 {
10596 --todo;
10597 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010598 if (tv_check_lock(di->di_tv.v_lock,
10599 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 break;
10601 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010602 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010603 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 break;
10605 if (!map && rem)
10606 dictitem_remove(d, di);
10607 clear_tv(&vimvars[VV_KEY].vv_tv);
10608 }
10609 }
10610 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 }
10612 else
10613 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010614 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010616 for (li = l->lv_first; li != NULL; li = nli)
10617 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010618 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010619 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010621 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010622 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010623 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010624 break;
10625 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010626 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010627 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010628 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010629 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010630
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010631 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010633
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010634 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010636
10637 copy_tv(&argvars[0], rettv);
10638}
10639
10640 static int
10641filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010642 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010643 char_u *expr;
10644 int map;
10645 int *remp;
10646{
Bram Moolenaar33570922005-01-25 22:26:29 +000010647 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010648 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010649 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010650
Bram Moolenaar33570922005-01-25 22:26:29 +000010651 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010652 s = expr;
10653 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010654 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010655 if (*s != NUL) /* check for trailing chars after expr */
10656 {
10657 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010658 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010659 }
10660 if (map)
10661 {
10662 /* map(): replace the list item value */
10663 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010664 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010665 *tv = rettv;
10666 }
10667 else
10668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010669 int error = FALSE;
10670
Bram Moolenaare9a41262005-01-15 22:18:47 +000010671 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010672 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010673 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010674 /* On type error, nothing has been removed; return FAIL to stop the
10675 * loop. The error message was given by get_tv_number_chk(). */
10676 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010677 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010678 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010679 retval = OK;
10680theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010681 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010682 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010683}
10684
10685/*
10686 * "filter()" function
10687 */
10688 static void
10689f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010690 typval_T *argvars;
10691 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010692{
10693 filter_map(argvars, rettv, FALSE);
10694}
10695
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010696/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010697 * "finddir({fname}[, {path}[, {count}]])" function
10698 */
10699 static void
10700f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010701 typval_T *argvars;
10702 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010703{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010704 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010705}
10706
10707/*
10708 * "findfile({fname}[, {path}[, {count}]])" function
10709 */
10710 static void
10711f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010712 typval_T *argvars;
10713 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010714{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010715 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716}
10717
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010718#ifdef FEAT_FLOAT
10719/*
10720 * "float2nr({float})" function
10721 */
10722 static void
10723f_float2nr(argvars, rettv)
10724 typval_T *argvars;
10725 typval_T *rettv;
10726{
10727 float_T f;
10728
10729 if (get_float_arg(argvars, &f) == OK)
10730 {
10731 if (f < -0x7fffffff)
10732 rettv->vval.v_number = -0x7fffffff;
10733 else if (f > 0x7fffffff)
10734 rettv->vval.v_number = 0x7fffffff;
10735 else
10736 rettv->vval.v_number = (varnumber_T)f;
10737 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010738}
10739
10740/*
10741 * "floor({float})" function
10742 */
10743 static void
10744f_floor(argvars, rettv)
10745 typval_T *argvars;
10746 typval_T *rettv;
10747{
10748 float_T f;
10749
10750 rettv->v_type = VAR_FLOAT;
10751 if (get_float_arg(argvars, &f) == OK)
10752 rettv->vval.v_float = floor(f);
10753 else
10754 rettv->vval.v_float = 0.0;
10755}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010756
10757/*
10758 * "fmod()" function
10759 */
10760 static void
10761f_fmod(argvars, rettv)
10762 typval_T *argvars;
10763 typval_T *rettv;
10764{
10765 float_T fx, fy;
10766
10767 rettv->v_type = VAR_FLOAT;
10768 if (get_float_arg(argvars, &fx) == OK
10769 && get_float_arg(&argvars[1], &fy) == OK)
10770 rettv->vval.v_float = fmod(fx, fy);
10771 else
10772 rettv->vval.v_float = 0.0;
10773}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010774#endif
10775
Bram Moolenaar0d660222005-01-07 21:51:51 +000010776/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010777 * "fnameescape({string})" function
10778 */
10779 static void
10780f_fnameescape(argvars, rettv)
10781 typval_T *argvars;
10782 typval_T *rettv;
10783{
10784 rettv->vval.v_string = vim_strsave_fnameescape(
10785 get_tv_string(&argvars[0]), FALSE);
10786 rettv->v_type = VAR_STRING;
10787}
10788
10789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 * "fnamemodify({fname}, {mods})" function
10791 */
10792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010794 typval_T *argvars;
10795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796{
10797 char_u *fname;
10798 char_u *mods;
10799 int usedlen = 0;
10800 int len;
10801 char_u *fbuf = NULL;
10802 char_u buf[NUMBUFLEN];
10803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010804 fname = get_tv_string_chk(&argvars[0]);
10805 mods = get_tv_string_buf_chk(&argvars[1], buf);
10806 if (fname == NULL || mods == NULL)
10807 fname = NULL;
10808 else
10809 {
10810 len = (int)STRLEN(fname);
10811 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010816 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010818 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819 vim_free(fbuf);
10820}
10821
Bram Moolenaar33570922005-01-25 22:26:29 +000010822static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823
10824/*
10825 * "foldclosed()" function
10826 */
10827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010828foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010829 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010830 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010831 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832{
10833#ifdef FEAT_FOLDING
10834 linenr_T lnum;
10835 linenr_T first, last;
10836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010837 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10839 {
10840 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10841 {
10842 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010843 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 return;
10847 }
10848 }
10849#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010850 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851}
10852
10853/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010854 * "foldclosed()" function
10855 */
10856 static void
10857f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010858 typval_T *argvars;
10859 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010860{
10861 foldclosed_both(argvars, rettv, FALSE);
10862}
10863
10864/*
10865 * "foldclosedend()" function
10866 */
10867 static void
10868f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010869 typval_T *argvars;
10870 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010871{
10872 foldclosed_both(argvars, rettv, TRUE);
10873}
10874
10875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 * "foldlevel()" function
10877 */
10878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010880 typval_T *argvars UNUSED;
10881 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882{
10883#ifdef FEAT_FOLDING
10884 linenr_T lnum;
10885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010888 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890}
10891
10892/*
10893 * "foldtext()" function
10894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010897 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899{
10900#ifdef FEAT_FOLDING
10901 linenr_T lnum;
10902 char_u *s;
10903 char_u *r;
10904 int len;
10905 char *txt;
10906#endif
10907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 rettv->v_type = VAR_STRING;
10909 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010911 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10912 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10913 <= curbuf->b_ml.ml_line_count
10914 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 {
10916 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010917 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10918 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 {
10920 if (!linewhite(lnum))
10921 break;
10922 ++lnum;
10923 }
10924
10925 /* Find interesting text in this line. */
10926 s = skipwhite(ml_get(lnum));
10927 /* skip C comment-start */
10928 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010931 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010933 {
10934 s = skipwhite(ml_get(lnum + 1));
10935 if (*s == '*')
10936 s = skipwhite(s + 1);
10937 }
10938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939 txt = _("+-%s%3ld lines: ");
10940 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010941 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 + 20 /* for %3ld */
10943 + STRLEN(s))); /* concatenated */
10944 if (r != NULL)
10945 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010946 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10947 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10948 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 len = (int)STRLEN(r);
10950 STRCAT(r, s);
10951 /* remove 'foldmarker' and 'commentstring' */
10952 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 }
10955 }
10956#endif
10957}
10958
10959/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010960 * "foldtextresult(lnum)" function
10961 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010964 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010965 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010966{
10967#ifdef FEAT_FOLDING
10968 linenr_T lnum;
10969 char_u *text;
10970 char_u buf[51];
10971 foldinfo_T foldinfo;
10972 int fold_count;
10973#endif
10974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 rettv->v_type = VAR_STRING;
10976 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010977#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010979 /* treat illegal types and illegal string values for {lnum} the same */
10980 if (lnum < 0)
10981 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010982 fold_count = foldedCount(curwin, lnum, &foldinfo);
10983 if (fold_count > 0)
10984 {
10985 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10986 &foldinfo, buf);
10987 if (text == buf)
10988 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010989 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010990 }
10991#endif
10992}
10993
10994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 * "foreground()" function
10996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010999 typval_T *argvars UNUSED;
11000 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002#ifdef FEAT_GUI
11003 if (gui.in_use)
11004 gui_mch_set_foreground();
11005#else
11006# ifdef WIN32
11007 win32_set_foreground();
11008# endif
11009#endif
11010}
11011
11012/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011013 * "function()" function
11014 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011017 typval_T *argvars;
11018 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011019{
11020 char_u *s;
11021
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011023 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011024 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011025 /* Don't check an autoload name for existence here. */
11026 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011027 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011028 else
11029 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011030 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011031 {
11032 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011033 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011034
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011035 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11036 * also be called from another script. Using trans_function_name()
11037 * would also work, but some plugins depend on the name being
11038 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011039 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011040 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011041 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011042 if (rettv->vval.v_string != NULL)
11043 {
11044 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011045 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011046 }
11047 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011048 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011049 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011051 }
11052}
11053
11054/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011055 * "garbagecollect()" function
11056 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011057 static void
11058f_garbagecollect(argvars, rettv)
11059 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011060 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011061{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011062 /* This is postponed until we are back at the toplevel, because we may be
11063 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11064 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011065
11066 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11067 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011068}
11069
11070/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011071 * "get()" function
11072 */
11073 static void
11074f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011075 typval_T *argvars;
11076 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011077{
Bram Moolenaar33570922005-01-25 22:26:29 +000011078 listitem_T *li;
11079 list_T *l;
11080 dictitem_T *di;
11081 dict_T *d;
11082 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011083
Bram Moolenaare9a41262005-01-15 22:18:47 +000011084 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011085 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011086 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011088 int error = FALSE;
11089
11090 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11091 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011092 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011093 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011094 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011095 else if (argvars[0].v_type == VAR_DICT)
11096 {
11097 if ((d = argvars[0].vval.v_dict) != NULL)
11098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011099 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011100 if (di != NULL)
11101 tv = &di->di_tv;
11102 }
11103 }
11104 else
11105 EMSG2(_(e_listdictarg), "get()");
11106
11107 if (tv == NULL)
11108 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011110 copy_tv(&argvars[2], rettv);
11111 }
11112 else
11113 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011114}
11115
Bram Moolenaar342337a2005-07-21 21:11:17 +000011116static 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 +000011117
11118/*
11119 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011120 * Return a range (from start to end) of lines in rettv from the specified
11121 * buffer.
11122 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011123 */
11124 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011125get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011126 buf_T *buf;
11127 linenr_T start;
11128 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011129 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011130 typval_T *rettv;
11131{
11132 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011133
Bram Moolenaar959a1432013-12-14 12:17:38 +010011134 rettv->v_type = VAR_STRING;
11135 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011136 if (retlist && rettv_list_alloc(rettv) == FAIL)
11137 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011138
11139 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11140 return;
11141
11142 if (!retlist)
11143 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011144 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11145 p = ml_get_buf(buf, start, FALSE);
11146 else
11147 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011148 rettv->vval.v_string = vim_strsave(p);
11149 }
11150 else
11151 {
11152 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011153 return;
11154
11155 if (start < 1)
11156 start = 1;
11157 if (end > buf->b_ml.ml_line_count)
11158 end = buf->b_ml.ml_line_count;
11159 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011160 if (list_append_string(rettv->vval.v_list,
11161 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011162 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011163 }
11164}
11165
11166/*
11167 * "getbufline()" function
11168 */
11169 static void
11170f_getbufline(argvars, rettv)
11171 typval_T *argvars;
11172 typval_T *rettv;
11173{
11174 linenr_T lnum;
11175 linenr_T end;
11176 buf_T *buf;
11177
11178 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11179 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011180 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011181 --emsg_off;
11182
Bram Moolenaar661b1822005-07-28 22:36:45 +000011183 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011184 if (argvars[2].v_type == VAR_UNKNOWN)
11185 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011186 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011187 end = get_tv_lnum_buf(&argvars[2], buf);
11188
Bram Moolenaar342337a2005-07-21 21:11:17 +000011189 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011190}
11191
Bram Moolenaar0d660222005-01-07 21:51:51 +000011192/*
11193 * "getbufvar()" function
11194 */
11195 static void
11196f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011197 typval_T *argvars;
11198 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011199{
11200 buf_T *buf;
11201 buf_T *save_curbuf;
11202 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011203 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011204 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011206 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11207 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011208 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011209 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011210
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011211 rettv->v_type = VAR_STRING;
11212 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011213
11214 if (buf != NULL && varname != NULL)
11215 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011216 /* set curbuf to be our buf, temporarily */
11217 save_curbuf = curbuf;
11218 curbuf = buf;
11219
Bram Moolenaar0d660222005-01-07 21:51:51 +000011220 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011221 {
11222 if (get_option_tv(&varname, rettv, TRUE) == OK)
11223 done = TRUE;
11224 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011225 else if (STRCMP(varname, "changedtick") == 0)
11226 {
11227 rettv->v_type = VAR_NUMBER;
11228 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011229 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011230 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011231 else
11232 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011233 /* Look up the variable. */
11234 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11235 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11236 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011238 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011239 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011240 done = TRUE;
11241 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011242 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011243
11244 /* restore previous notion of curbuf */
11245 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011246 }
11247
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011248 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11249 /* use the default value */
11250 copy_tv(&argvars[2], rettv);
11251
Bram Moolenaar0d660222005-01-07 21:51:51 +000011252 --emsg_off;
11253}
11254
11255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 * "getchar()" function
11257 */
11258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011260 typval_T *argvars;
11261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262{
11263 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011264 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011266 /* Position the cursor. Needed after a message that ends in a space. */
11267 windgoto(msg_row, msg_col);
11268
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 ++no_mapping;
11270 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011271 for (;;)
11272 {
11273 if (argvars[0].v_type == VAR_UNKNOWN)
11274 /* getchar(): blocking wait. */
11275 n = safe_vgetc();
11276 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11277 /* getchar(1): only check if char avail */
11278 n = vpeekc();
11279 else if (error || vpeekc() == NUL)
11280 /* illegal argument or getchar(0) and no char avail: return zero */
11281 n = 0;
11282 else
11283 /* getchar(0) and char avail: return char */
11284 n = safe_vgetc();
11285 if (n == K_IGNORE)
11286 continue;
11287 break;
11288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 --no_mapping;
11290 --allow_keys;
11291
Bram Moolenaar219b8702006-11-01 14:32:36 +000011292 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11293 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11294 vimvars[VV_MOUSE_COL].vv_nr = 0;
11295
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 if (IS_SPECIAL(n) || mod_mask != 0)
11298 {
11299 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11300 int i = 0;
11301
11302 /* Turn a special key into three bytes, plus modifier. */
11303 if (mod_mask != 0)
11304 {
11305 temp[i++] = K_SPECIAL;
11306 temp[i++] = KS_MODIFIER;
11307 temp[i++] = mod_mask;
11308 }
11309 if (IS_SPECIAL(n))
11310 {
11311 temp[i++] = K_SPECIAL;
11312 temp[i++] = K_SECOND(n);
11313 temp[i++] = K_THIRD(n);
11314 }
11315#ifdef FEAT_MBYTE
11316 else if (has_mbyte)
11317 i += (*mb_char2bytes)(n, temp + i);
11318#endif
11319 else
11320 temp[i++] = n;
11321 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322 rettv->v_type = VAR_STRING;
11323 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011324
11325#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011326 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011327 {
11328 int row = mouse_row;
11329 int col = mouse_col;
11330 win_T *win;
11331 linenr_T lnum;
11332# ifdef FEAT_WINDOWS
11333 win_T *wp;
11334# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011335 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011336
11337 if (row >= 0 && col >= 0)
11338 {
11339 /* Find the window at the mouse coordinates and compute the
11340 * text position. */
11341 win = mouse_find_win(&row, &col);
11342 (void)mouse_comp_pos(win, &row, &col, &lnum);
11343# ifdef FEAT_WINDOWS
11344 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011345 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011346# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011347 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011348 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11349 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11350 }
11351 }
11352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011353 }
11354}
11355
11356/*
11357 * "getcharmod()" function
11358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011361 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011362 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011364 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365}
11366
11367/*
11368 * "getcmdline()" function
11369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011372 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011375 rettv->v_type = VAR_STRING;
11376 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377}
11378
11379/*
11380 * "getcmdpos()" function
11381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011383f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011384 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388}
11389
11390/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011391 * "getcmdtype()" function
11392 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011393 static void
11394f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011395 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011396 typval_T *rettv;
11397{
11398 rettv->v_type = VAR_STRING;
11399 rettv->vval.v_string = alloc(2);
11400 if (rettv->vval.v_string != NULL)
11401 {
11402 rettv->vval.v_string[0] = get_cmdline_type();
11403 rettv->vval.v_string[1] = NUL;
11404 }
11405}
11406
11407/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408 * "getcwd()" function
11409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011411f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011412 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011415 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011418 rettv->vval.v_string = NULL;
11419 cwd = alloc(MAXPATHL);
11420 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011422 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11423 {
11424 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011425#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011426 if (rettv->vval.v_string != NULL)
11427 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011429 }
11430 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 }
11432}
11433
11434/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011435 * "getfontname()" function
11436 */
11437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011439 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011440 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011441{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011442 rettv->v_type = VAR_STRING;
11443 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011444#ifdef FEAT_GUI
11445 if (gui.in_use)
11446 {
11447 GuiFont font;
11448 char_u *name = NULL;
11449
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011450 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011451 {
11452 /* Get the "Normal" font. Either the name saved by
11453 * hl_set_font_name() or from the font ID. */
11454 font = gui.norm_font;
11455 name = hl_get_font_name();
11456 }
11457 else
11458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011459 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011460 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11461 return;
11462 font = gui_mch_get_font(name, FALSE);
11463 if (font == NOFONT)
11464 return; /* Invalid font name, return empty string. */
11465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011467 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011468 gui_mch_free_font(font);
11469 }
11470#endif
11471}
11472
11473/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011474 * "getfperm({fname})" function
11475 */
11476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011478 typval_T *argvars;
11479 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011480{
11481 char_u *fname;
11482 struct stat st;
11483 char_u *perm = NULL;
11484 char_u flags[] = "rwx";
11485 int i;
11486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011489 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011490 if (mch_stat((char *)fname, &st) >= 0)
11491 {
11492 perm = vim_strsave((char_u *)"---------");
11493 if (perm != NULL)
11494 {
11495 for (i = 0; i < 9; i++)
11496 {
11497 if (st.st_mode & (1 << (8 - i)))
11498 perm[i] = flags[i % 3];
11499 }
11500 }
11501 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011503}
11504
11505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 * "getfsize({fname})" function
11507 */
11508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011510 typval_T *argvars;
11511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512{
11513 char_u *fname;
11514 struct stat st;
11515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011518 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519
11520 if (mch_stat((char *)fname, &st) >= 0)
11521 {
11522 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011527
11528 /* non-perfect check for overflow */
11529 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11530 rettv->vval.v_number = -2;
11531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532 }
11533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535}
11536
11537/*
11538 * "getftime({fname})" function
11539 */
11540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011542 typval_T *argvars;
11543 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544{
11545 char_u *fname;
11546 struct stat st;
11547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011548 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549
11550 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011551 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554}
11555
11556/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011557 * "getftype({fname})" function
11558 */
11559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011561 typval_T *argvars;
11562 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011563{
11564 char_u *fname;
11565 struct stat st;
11566 char_u *type = NULL;
11567 char *t;
11568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011569 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011571 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011572 if (mch_lstat((char *)fname, &st) >= 0)
11573 {
11574#ifdef S_ISREG
11575 if (S_ISREG(st.st_mode))
11576 t = "file";
11577 else if (S_ISDIR(st.st_mode))
11578 t = "dir";
11579# ifdef S_ISLNK
11580 else if (S_ISLNK(st.st_mode))
11581 t = "link";
11582# endif
11583# ifdef S_ISBLK
11584 else if (S_ISBLK(st.st_mode))
11585 t = "bdev";
11586# endif
11587# ifdef S_ISCHR
11588 else if (S_ISCHR(st.st_mode))
11589 t = "cdev";
11590# endif
11591# ifdef S_ISFIFO
11592 else if (S_ISFIFO(st.st_mode))
11593 t = "fifo";
11594# endif
11595# ifdef S_ISSOCK
11596 else if (S_ISSOCK(st.st_mode))
11597 t = "fifo";
11598# endif
11599 else
11600 t = "other";
11601#else
11602# ifdef S_IFMT
11603 switch (st.st_mode & S_IFMT)
11604 {
11605 case S_IFREG: t = "file"; break;
11606 case S_IFDIR: t = "dir"; break;
11607# ifdef S_IFLNK
11608 case S_IFLNK: t = "link"; break;
11609# endif
11610# ifdef S_IFBLK
11611 case S_IFBLK: t = "bdev"; break;
11612# endif
11613# ifdef S_IFCHR
11614 case S_IFCHR: t = "cdev"; break;
11615# endif
11616# ifdef S_IFIFO
11617 case S_IFIFO: t = "fifo"; break;
11618# endif
11619# ifdef S_IFSOCK
11620 case S_IFSOCK: t = "socket"; break;
11621# endif
11622 default: t = "other";
11623 }
11624# else
11625 if (mch_isdir(fname))
11626 t = "dir";
11627 else
11628 t = "file";
11629# endif
11630#endif
11631 type = vim_strsave((char_u *)t);
11632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011634}
11635
11636/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011637 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011638 */
11639 static void
11640f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011641 typval_T *argvars;
11642 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011643{
11644 linenr_T lnum;
11645 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011646 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011647
11648 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011649 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011650 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011651 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011652 retlist = FALSE;
11653 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011654 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011655 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011657 retlist = TRUE;
11658 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011659
Bram Moolenaar342337a2005-07-21 21:11:17 +000011660 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011661}
11662
11663/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011664 * "getmatches()" function
11665 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011666 static void
11667f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011668 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011669 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011670{
11671#ifdef FEAT_SEARCH_EXTRA
11672 dict_T *dict;
11673 matchitem_T *cur = curwin->w_match_head;
11674
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011675 if (rettv_list_alloc(rettv) == OK)
11676 {
11677 while (cur != NULL)
11678 {
11679 dict = dict_alloc();
11680 if (dict == NULL)
11681 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011682 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11683 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11684 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11685 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11686 list_append_dict(rettv->vval.v_list, dict);
11687 cur = cur->next;
11688 }
11689 }
11690#endif
11691}
11692
11693/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011694 * "getpid()" function
11695 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011696 static void
11697f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011698 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011699 typval_T *rettv;
11700{
11701 rettv->vval.v_number = mch_get_pid();
11702}
11703
11704/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011705 * "getpos(string)" function
11706 */
11707 static void
11708f_getpos(argvars, rettv)
11709 typval_T *argvars;
11710 typval_T *rettv;
11711{
11712 pos_T *fp;
11713 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011714 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011715
11716 if (rettv_list_alloc(rettv) == OK)
11717 {
11718 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011719 fp = var2fpos(&argvars[0], TRUE, &fnum);
11720 if (fnum != -1)
11721 list_append_number(l, (varnumber_T)fnum);
11722 else
11723 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011724 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11725 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011726 list_append_number(l, (fp != NULL)
11727 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011728 : (varnumber_T)0);
11729 list_append_number(l,
11730#ifdef FEAT_VIRTUALEDIT
11731 (fp != NULL) ? (varnumber_T)fp->coladd :
11732#endif
11733 (varnumber_T)0);
11734 }
11735 else
11736 rettv->vval.v_number = FALSE;
11737}
11738
11739/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011740 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011741 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011742 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011743f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011744 typval_T *argvars UNUSED;
11745 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011746{
11747#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011748 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011749#endif
11750
Bram Moolenaar2641f772005-03-25 21:58:17 +000011751#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011752 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011753 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011754 wp = NULL;
11755 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11756 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011757 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011758 if (wp == NULL)
11759 return;
11760 }
11761
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011762 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011763 }
11764#endif
11765}
11766
11767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 * "getreg()" function
11769 */
11770 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011772 typval_T *argvars;
11773 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774{
11775 char_u *strregname;
11776 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011777 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011778 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011780 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 strregname = get_tv_string_chk(&argvars[0]);
11783 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011784 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011788 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 regname = (strregname == NULL ? '"' : *strregname);
11790 if (regname == 0)
11791 regname = '"';
11792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011794 rettv->vval.v_string = error ? NULL :
11795 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796}
11797
11798/*
11799 * "getregtype()" function
11800 */
11801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011803 typval_T *argvars;
11804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011805{
11806 char_u *strregname;
11807 int regname;
11808 char_u buf[NUMBUFLEN + 2];
11809 long reglen = 0;
11810
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011811 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011812 {
11813 strregname = get_tv_string_chk(&argvars[0]);
11814 if (strregname == NULL) /* type error; errmsg already given */
11815 {
11816 rettv->v_type = VAR_STRING;
11817 rettv->vval.v_string = NULL;
11818 return;
11819 }
11820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 else
11822 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011823 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824
11825 regname = (strregname == NULL ? '"' : *strregname);
11826 if (regname == 0)
11827 regname = '"';
11828
11829 buf[0] = NUL;
11830 buf[1] = NUL;
11831 switch (get_reg_type(regname, &reglen))
11832 {
11833 case MLINE: buf[0] = 'V'; break;
11834 case MCHAR: buf[0] = 'v'; break;
11835#ifdef FEAT_VISUAL
11836 case MBLOCK:
11837 buf[0] = Ctrl_V;
11838 sprintf((char *)buf + 1, "%ld", reglen + 1);
11839 break;
11840#endif
11841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011842 rettv->v_type = VAR_STRING;
11843 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844}
11845
11846/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011847 * "gettabvar()" function
11848 */
11849 static void
11850f_gettabvar(argvars, rettv)
11851 typval_T *argvars;
11852 typval_T *rettv;
11853{
11854 tabpage_T *tp;
11855 dictitem_T *v;
11856 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011857 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011858
11859 rettv->v_type = VAR_STRING;
11860 rettv->vval.v_string = NULL;
11861
11862 varname = get_tv_string_chk(&argvars[1]);
11863 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11864 if (tp != NULL && varname != NULL)
11865 {
11866 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011867 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011868 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011869 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011870 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011871 done = TRUE;
11872 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011873 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011874
11875 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011876 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011877}
11878
11879/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011880 * "gettabwinvar()" function
11881 */
11882 static void
11883f_gettabwinvar(argvars, rettv)
11884 typval_T *argvars;
11885 typval_T *rettv;
11886{
11887 getwinvar(argvars, rettv, 1);
11888}
11889
11890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 * "getwinposx()" function
11892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011894f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011895 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011896 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011898 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899#ifdef FEAT_GUI
11900 if (gui.in_use)
11901 {
11902 int x, y;
11903
11904 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011905 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011906 }
11907#endif
11908}
11909
11910/*
11911 * "getwinposy()" function
11912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011915 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919#ifdef FEAT_GUI
11920 if (gui.in_use)
11921 {
11922 int x, y;
11923
11924 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011925 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926 }
11927#endif
11928}
11929
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011930/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011931 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011932 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011933 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011934find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011935 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011936 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011937{
11938#ifdef FEAT_WINDOWS
11939 win_T *wp;
11940#endif
11941 int nr;
11942
11943 nr = get_tv_number_chk(vp, NULL);
11944
11945#ifdef FEAT_WINDOWS
11946 if (nr < 0)
11947 return NULL;
11948 if (nr == 0)
11949 return curwin;
11950
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011951 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11952 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011953 if (--nr <= 0)
11954 break;
11955 return wp;
11956#else
11957 if (nr == 0 || nr == 1)
11958 return curwin;
11959 return NULL;
11960#endif
11961}
11962
Bram Moolenaar071d4272004-06-13 20:20:40 +000011963/*
11964 * "getwinvar()" function
11965 */
11966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011968 typval_T *argvars;
11969 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011971 getwinvar(argvars, rettv, 0);
11972}
11973
11974/*
11975 * getwinvar() and gettabwinvar()
11976 */
11977 static void
11978getwinvar(argvars, rettv, off)
11979 typval_T *argvars;
11980 typval_T *rettv;
11981 int off; /* 1 for gettabwinvar() */
11982{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 win_T *win, *oldcurwin;
11984 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011985 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011986 tabpage_T *tp = NULL;
11987 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011988 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011990#ifdef FEAT_WINDOWS
11991 if (off == 1)
11992 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11993 else
11994 tp = curtab;
11995#endif
11996 win = find_win_by_nr(&argvars[off], tp);
11997 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011998 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012000 rettv->v_type = VAR_STRING;
12001 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002
12003 if (win != NULL && varname != NULL)
12004 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020012005 /* Set curwin to be our win, temporarily. Also set the tabpage,
12006 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020012007 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012008
Bram Moolenaar071d4272004-06-13 20:20:40 +000012009 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012010 {
12011 if (get_option_tv(&varname, rettv, 1) == OK)
12012 done = TRUE;
12013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 else
12015 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012016 /* Look up the variable. */
12017 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12018 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012020 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012021 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012022 done = TRUE;
12023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012025
12026 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012027 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028 }
12029
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012030 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12031 /* use the default return value */
12032 copy_tv(&argvars[off + 2], rettv);
12033
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034 --emsg_off;
12035}
12036
12037/*
12038 * "glob()" function
12039 */
12040 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012041f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 typval_T *argvars;
12043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012045 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012047 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012049 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012050 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012052 if (argvars[1].v_type != VAR_UNKNOWN)
12053 {
12054 if (get_tv_number_chk(&argvars[1], &error))
12055 options |= WILD_KEEP_ALL;
12056 if (argvars[2].v_type != VAR_UNKNOWN
12057 && get_tv_number_chk(&argvars[2], &error))
12058 {
12059 rettv->v_type = VAR_LIST;
12060 rettv->vval.v_list = NULL;
12061 }
12062 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012063 if (!error)
12064 {
12065 ExpandInit(&xpc);
12066 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012067 if (p_wic)
12068 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012069 if (rettv->v_type == VAR_STRING)
12070 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012071 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012072 else if (rettv_list_alloc(rettv) != FAIL)
12073 {
12074 int i;
12075
12076 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12077 NULL, options, WILD_ALL_KEEP);
12078 for (i = 0; i < xpc.xp_numfiles; i++)
12079 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12080
12081 ExpandCleanup(&xpc);
12082 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012083 }
12084 else
12085 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086}
12087
12088/*
12089 * "globpath()" function
12090 */
12091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012093 typval_T *argvars;
12094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012096 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012097 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012098 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012099 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012101 /* When the optional second argument is non-zero, don't remove matches
12102 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12103 if (argvars[2].v_type != VAR_UNKNOWN
12104 && get_tv_number_chk(&argvars[2], &error))
12105 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012107 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012108 rettv->vval.v_string = NULL;
12109 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012110 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12111 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112}
12113
12114/*
12115 * "has()" function
12116 */
12117 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012118f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012119 typval_T *argvars;
12120 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012121{
12122 int i;
12123 char_u *name;
12124 int n = FALSE;
12125 static char *(has_list[]) =
12126 {
12127#ifdef AMIGA
12128 "amiga",
12129# ifdef FEAT_ARP
12130 "arp",
12131# endif
12132#endif
12133#ifdef __BEOS__
12134 "beos",
12135#endif
12136#ifdef MSDOS
12137# ifdef DJGPP
12138 "dos32",
12139# else
12140 "dos16",
12141# endif
12142#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012143#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144 "mac",
12145#endif
12146#if defined(MACOS_X_UNIX)
12147 "macunix",
12148#endif
12149#ifdef OS2
12150 "os2",
12151#endif
12152#ifdef __QNX__
12153 "qnx",
12154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012155#ifdef UNIX
12156 "unix",
12157#endif
12158#ifdef VMS
12159 "vms",
12160#endif
12161#ifdef WIN16
12162 "win16",
12163#endif
12164#ifdef WIN32
12165 "win32",
12166#endif
12167#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12168 "win32unix",
12169#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012170#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171 "win64",
12172#endif
12173#ifdef EBCDIC
12174 "ebcdic",
12175#endif
12176#ifndef CASE_INSENSITIVE_FILENAME
12177 "fname_case",
12178#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012179#ifdef HAVE_ACL
12180 "acl",
12181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182#ifdef FEAT_ARABIC
12183 "arabic",
12184#endif
12185#ifdef FEAT_AUTOCMD
12186 "autocmd",
12187#endif
12188#ifdef FEAT_BEVAL
12189 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012190# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12191 "balloon_multiline",
12192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193#endif
12194#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12195 "builtin_terms",
12196# ifdef ALL_BUILTIN_TCAPS
12197 "all_builtin_terms",
12198# endif
12199#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012200#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12201 || defined(FEAT_GUI_W32) \
12202 || defined(FEAT_GUI_MOTIF))
12203 "browsefilter",
12204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205#ifdef FEAT_BYTEOFF
12206 "byte_offset",
12207#endif
12208#ifdef FEAT_CINDENT
12209 "cindent",
12210#endif
12211#ifdef FEAT_CLIENTSERVER
12212 "clientserver",
12213#endif
12214#ifdef FEAT_CLIPBOARD
12215 "clipboard",
12216#endif
12217#ifdef FEAT_CMDL_COMPL
12218 "cmdline_compl",
12219#endif
12220#ifdef FEAT_CMDHIST
12221 "cmdline_hist",
12222#endif
12223#ifdef FEAT_COMMENTS
12224 "comments",
12225#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012226#ifdef FEAT_CONCEAL
12227 "conceal",
12228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012229#ifdef FEAT_CRYPT
12230 "cryptv",
12231#endif
12232#ifdef FEAT_CSCOPE
12233 "cscope",
12234#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012235#ifdef FEAT_CURSORBIND
12236 "cursorbind",
12237#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012238#ifdef CURSOR_SHAPE
12239 "cursorshape",
12240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012241#ifdef DEBUG
12242 "debug",
12243#endif
12244#ifdef FEAT_CON_DIALOG
12245 "dialog_con",
12246#endif
12247#ifdef FEAT_GUI_DIALOG
12248 "dialog_gui",
12249#endif
12250#ifdef FEAT_DIFF
12251 "diff",
12252#endif
12253#ifdef FEAT_DIGRAPHS
12254 "digraphs",
12255#endif
12256#ifdef FEAT_DND
12257 "dnd",
12258#endif
12259#ifdef FEAT_EMACS_TAGS
12260 "emacs_tags",
12261#endif
12262 "eval", /* always present, of course! */
12263#ifdef FEAT_EX_EXTRA
12264 "ex_extra",
12265#endif
12266#ifdef FEAT_SEARCH_EXTRA
12267 "extra_search",
12268#endif
12269#ifdef FEAT_FKMAP
12270 "farsi",
12271#endif
12272#ifdef FEAT_SEARCHPATH
12273 "file_in_path",
12274#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012275#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012276 "filterpipe",
12277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278#ifdef FEAT_FIND_ID
12279 "find_in_path",
12280#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012281#ifdef FEAT_FLOAT
12282 "float",
12283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284#ifdef FEAT_FOLDING
12285 "folding",
12286#endif
12287#ifdef FEAT_FOOTER
12288 "footer",
12289#endif
12290#if !defined(USE_SYSTEM) && defined(UNIX)
12291 "fork",
12292#endif
12293#ifdef FEAT_GETTEXT
12294 "gettext",
12295#endif
12296#ifdef FEAT_GUI
12297 "gui",
12298#endif
12299#ifdef FEAT_GUI_ATHENA
12300# ifdef FEAT_GUI_NEXTAW
12301 "gui_neXtaw",
12302# else
12303 "gui_athena",
12304# endif
12305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306#ifdef FEAT_GUI_GTK
12307 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012310#ifdef FEAT_GUI_GNOME
12311 "gui_gnome",
12312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313#ifdef FEAT_GUI_MAC
12314 "gui_mac",
12315#endif
12316#ifdef FEAT_GUI_MOTIF
12317 "gui_motif",
12318#endif
12319#ifdef FEAT_GUI_PHOTON
12320 "gui_photon",
12321#endif
12322#ifdef FEAT_GUI_W16
12323 "gui_win16",
12324#endif
12325#ifdef FEAT_GUI_W32
12326 "gui_win32",
12327#endif
12328#ifdef FEAT_HANGULIN
12329 "hangul_input",
12330#endif
12331#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12332 "iconv",
12333#endif
12334#ifdef FEAT_INS_EXPAND
12335 "insert_expand",
12336#endif
12337#ifdef FEAT_JUMPLIST
12338 "jumplist",
12339#endif
12340#ifdef FEAT_KEYMAP
12341 "keymap",
12342#endif
12343#ifdef FEAT_LANGMAP
12344 "langmap",
12345#endif
12346#ifdef FEAT_LIBCALL
12347 "libcall",
12348#endif
12349#ifdef FEAT_LINEBREAK
12350 "linebreak",
12351#endif
12352#ifdef FEAT_LISP
12353 "lispindent",
12354#endif
12355#ifdef FEAT_LISTCMDS
12356 "listcmds",
12357#endif
12358#ifdef FEAT_LOCALMAP
12359 "localmap",
12360#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012361#ifdef FEAT_LUA
12362# ifndef DYNAMIC_LUA
12363 "lua",
12364# endif
12365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366#ifdef FEAT_MENU
12367 "menu",
12368#endif
12369#ifdef FEAT_SESSION
12370 "mksession",
12371#endif
12372#ifdef FEAT_MODIFY_FNAME
12373 "modify_fname",
12374#endif
12375#ifdef FEAT_MOUSE
12376 "mouse",
12377#endif
12378#ifdef FEAT_MOUSESHAPE
12379 "mouseshape",
12380#endif
12381#if defined(UNIX) || defined(VMS)
12382# ifdef FEAT_MOUSE_DEC
12383 "mouse_dec",
12384# endif
12385# ifdef FEAT_MOUSE_GPM
12386 "mouse_gpm",
12387# endif
12388# ifdef FEAT_MOUSE_JSB
12389 "mouse_jsbterm",
12390# endif
12391# ifdef FEAT_MOUSE_NET
12392 "mouse_netterm",
12393# endif
12394# ifdef FEAT_MOUSE_PTERM
12395 "mouse_pterm",
12396# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012397# ifdef FEAT_MOUSE_SGR
12398 "mouse_sgr",
12399# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012400# ifdef FEAT_SYSMOUSE
12401 "mouse_sysmouse",
12402# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012403# ifdef FEAT_MOUSE_URXVT
12404 "mouse_urxvt",
12405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406# ifdef FEAT_MOUSE_XTERM
12407 "mouse_xterm",
12408# endif
12409#endif
12410#ifdef FEAT_MBYTE
12411 "multi_byte",
12412#endif
12413#ifdef FEAT_MBYTE_IME
12414 "multi_byte_ime",
12415#endif
12416#ifdef FEAT_MULTI_LANG
12417 "multi_lang",
12418#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012419#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012420#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012421 "mzscheme",
12422#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424#ifdef FEAT_OLE
12425 "ole",
12426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427#ifdef FEAT_PATH_EXTRA
12428 "path_extra",
12429#endif
12430#ifdef FEAT_PERL
12431#ifndef DYNAMIC_PERL
12432 "perl",
12433#endif
12434#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012435#ifdef FEAT_PERSISTENT_UNDO
12436 "persistent_undo",
12437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438#ifdef FEAT_PYTHON
12439#ifndef DYNAMIC_PYTHON
12440 "python",
12441#endif
12442#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012443#ifdef FEAT_PYTHON3
12444#ifndef DYNAMIC_PYTHON3
12445 "python3",
12446#endif
12447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448#ifdef FEAT_POSTSCRIPT
12449 "postscript",
12450#endif
12451#ifdef FEAT_PRINTER
12452 "printer",
12453#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012454#ifdef FEAT_PROFILE
12455 "profile",
12456#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012457#ifdef FEAT_RELTIME
12458 "reltime",
12459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460#ifdef FEAT_QUICKFIX
12461 "quickfix",
12462#endif
12463#ifdef FEAT_RIGHTLEFT
12464 "rightleft",
12465#endif
12466#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12467 "ruby",
12468#endif
12469#ifdef FEAT_SCROLLBIND
12470 "scrollbind",
12471#endif
12472#ifdef FEAT_CMDL_INFO
12473 "showcmd",
12474 "cmdline_info",
12475#endif
12476#ifdef FEAT_SIGNS
12477 "signs",
12478#endif
12479#ifdef FEAT_SMARTINDENT
12480 "smartindent",
12481#endif
12482#ifdef FEAT_SNIFF
12483 "sniff",
12484#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012485#ifdef STARTUPTIME
12486 "startuptime",
12487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488#ifdef FEAT_STL_OPT
12489 "statusline",
12490#endif
12491#ifdef FEAT_SUN_WORKSHOP
12492 "sun_workshop",
12493#endif
12494#ifdef FEAT_NETBEANS_INTG
12495 "netbeans_intg",
12496#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012497#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012498 "spell",
12499#endif
12500#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501 "syntax",
12502#endif
12503#if defined(USE_SYSTEM) || !defined(UNIX)
12504 "system",
12505#endif
12506#ifdef FEAT_TAG_BINS
12507 "tag_binary",
12508#endif
12509#ifdef FEAT_TAG_OLDSTATIC
12510 "tag_old_static",
12511#endif
12512#ifdef FEAT_TAG_ANYWHITE
12513 "tag_any_white",
12514#endif
12515#ifdef FEAT_TCL
12516# ifndef DYNAMIC_TCL
12517 "tcl",
12518# endif
12519#endif
12520#ifdef TERMINFO
12521 "terminfo",
12522#endif
12523#ifdef FEAT_TERMRESPONSE
12524 "termresponse",
12525#endif
12526#ifdef FEAT_TEXTOBJ
12527 "textobjects",
12528#endif
12529#ifdef HAVE_TGETENT
12530 "tgetent",
12531#endif
12532#ifdef FEAT_TITLE
12533 "title",
12534#endif
12535#ifdef FEAT_TOOLBAR
12536 "toolbar",
12537#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012538#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12539 "unnamedplus",
12540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541#ifdef FEAT_USR_CMDS
12542 "user-commands", /* was accidentally included in 5.4 */
12543 "user_commands",
12544#endif
12545#ifdef FEAT_VIMINFO
12546 "viminfo",
12547#endif
12548#ifdef FEAT_VERTSPLIT
12549 "vertsplit",
12550#endif
12551#ifdef FEAT_VIRTUALEDIT
12552 "virtualedit",
12553#endif
12554#ifdef FEAT_VISUAL
12555 "visual",
12556#endif
12557#ifdef FEAT_VISUALEXTRA
12558 "visualextra",
12559#endif
12560#ifdef FEAT_VREPLACE
12561 "vreplace",
12562#endif
12563#ifdef FEAT_WILDIGN
12564 "wildignore",
12565#endif
12566#ifdef FEAT_WILDMENU
12567 "wildmenu",
12568#endif
12569#ifdef FEAT_WINDOWS
12570 "windows",
12571#endif
12572#ifdef FEAT_WAK
12573 "winaltkeys",
12574#endif
12575#ifdef FEAT_WRITEBACKUP
12576 "writebackup",
12577#endif
12578#ifdef FEAT_XIM
12579 "xim",
12580#endif
12581#ifdef FEAT_XFONTSET
12582 "xfontset",
12583#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012584#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012585 "xpm",
12586 "xpm_w32", /* for backward compatibility */
12587#else
12588# if defined(HAVE_XPM)
12589 "xpm",
12590# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592#ifdef USE_XSMP
12593 "xsmp",
12594#endif
12595#ifdef USE_XSMP_INTERACT
12596 "xsmp_interact",
12597#endif
12598#ifdef FEAT_XCLIPBOARD
12599 "xterm_clipboard",
12600#endif
12601#ifdef FEAT_XTERM_SAVE
12602 "xterm_save",
12603#endif
12604#if defined(UNIX) && defined(FEAT_X11)
12605 "X11",
12606#endif
12607 NULL
12608 };
12609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 for (i = 0; has_list[i] != NULL; ++i)
12612 if (STRICMP(name, has_list[i]) == 0)
12613 {
12614 n = TRUE;
12615 break;
12616 }
12617
12618 if (n == FALSE)
12619 {
12620 if (STRNICMP(name, "patch", 5) == 0)
12621 n = has_patch(atoi((char *)name + 5));
12622 else if (STRICMP(name, "vim_starting") == 0)
12623 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012624#ifdef FEAT_MBYTE
12625 else if (STRICMP(name, "multi_byte_encoding") == 0)
12626 n = has_mbyte;
12627#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012628#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12629 else if (STRICMP(name, "balloon_multiline") == 0)
12630 n = multiline_balloon_available();
12631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632#ifdef DYNAMIC_TCL
12633 else if (STRICMP(name, "tcl") == 0)
12634 n = tcl_enabled(FALSE);
12635#endif
12636#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12637 else if (STRICMP(name, "iconv") == 0)
12638 n = iconv_enabled(FALSE);
12639#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012640#ifdef DYNAMIC_LUA
12641 else if (STRICMP(name, "lua") == 0)
12642 n = lua_enabled(FALSE);
12643#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012644#ifdef DYNAMIC_MZSCHEME
12645 else if (STRICMP(name, "mzscheme") == 0)
12646 n = mzscheme_enabled(FALSE);
12647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012648#ifdef DYNAMIC_RUBY
12649 else if (STRICMP(name, "ruby") == 0)
12650 n = ruby_enabled(FALSE);
12651#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012652#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653#ifdef DYNAMIC_PYTHON
12654 else if (STRICMP(name, "python") == 0)
12655 n = python_enabled(FALSE);
12656#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012657#endif
12658#ifdef FEAT_PYTHON3
12659#ifdef DYNAMIC_PYTHON3
12660 else if (STRICMP(name, "python3") == 0)
12661 n = python3_enabled(FALSE);
12662#endif
12663#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664#ifdef DYNAMIC_PERL
12665 else if (STRICMP(name, "perl") == 0)
12666 n = perl_enabled(FALSE);
12667#endif
12668#ifdef FEAT_GUI
12669 else if (STRICMP(name, "gui_running") == 0)
12670 n = (gui.in_use || gui.starting);
12671# ifdef FEAT_GUI_W32
12672 else if (STRICMP(name, "gui_win32s") == 0)
12673 n = gui_is_win32s();
12674# endif
12675# ifdef FEAT_BROWSE
12676 else if (STRICMP(name, "browse") == 0)
12677 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12678# endif
12679#endif
12680#ifdef FEAT_SYN_HL
12681 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012682 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683#endif
12684#if defined(WIN3264)
12685 else if (STRICMP(name, "win95") == 0)
12686 n = mch_windows95();
12687#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012688#ifdef FEAT_NETBEANS_INTG
12689 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012690 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 }
12693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695}
12696
12697/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012698 * "has_key()" function
12699 */
12700 static void
12701f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 typval_T *argvars;
12703 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012704{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012705 if (argvars[0].v_type != VAR_DICT)
12706 {
12707 EMSG(_(e_dictreq));
12708 return;
12709 }
12710 if (argvars[0].vval.v_dict == NULL)
12711 return;
12712
12713 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012714 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012715}
12716
12717/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012718 * "haslocaldir()" function
12719 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012720 static void
12721f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012722 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012723 typval_T *rettv;
12724{
12725 rettv->vval.v_number = (curwin->w_localdir != NULL);
12726}
12727
12728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 * "hasmapto()" function
12730 */
12731 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012733 typval_T *argvars;
12734 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735{
12736 char_u *name;
12737 char_u *mode;
12738 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012739 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012742 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 mode = (char_u *)"nvo";
12744 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012747 if (argvars[2].v_type != VAR_UNKNOWN)
12748 abbr = get_tv_number(&argvars[2]);
12749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750
Bram Moolenaar2c932302006-03-18 21:42:09 +000012751 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012752 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755}
12756
12757/*
12758 * "histadd()" function
12759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012761f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012762 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764{
12765#ifdef FEAT_CMDHIST
12766 int histype;
12767 char_u *str;
12768 char_u buf[NUMBUFLEN];
12769#endif
12770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012771 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772 if (check_restricted() || check_secure())
12773 return;
12774#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012775 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12776 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 if (histype >= 0)
12778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012779 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 if (*str != NUL)
12781 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012782 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 return;
12786 }
12787 }
12788#endif
12789}
12790
12791/*
12792 * "histdel()" function
12793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012795f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012796 typval_T *argvars UNUSED;
12797 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798{
12799#ifdef FEAT_CMDHIST
12800 int n;
12801 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012802 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012804 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12805 if (str == NULL)
12806 n = 0;
12807 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012809 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012810 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012812 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012813 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012814 else
12815 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012816 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 get_tv_string_buf(&argvars[1], buf));
12818 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819#endif
12820}
12821
12822/*
12823 * "histget()" function
12824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012827 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829{
12830#ifdef FEAT_CMDHIST
12831 int type;
12832 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012833 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012835 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12836 if (str == NULL)
12837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012839 {
12840 type = get_histtype(str);
12841 if (argvars[1].v_type == VAR_UNKNOWN)
12842 idx = get_history_idx(type);
12843 else
12844 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12845 /* -1 on type error */
12846 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852}
12853
12854/*
12855 * "histnr()" function
12856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012859 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012860 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861{
12862 int i;
12863
12864#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012865 char_u *history = get_tv_string_chk(&argvars[0]);
12866
12867 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 if (i >= HIST_CMD && i < HIST_COUNT)
12869 i = get_history_idx(i);
12870 else
12871#endif
12872 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874}
12875
12876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 * "highlightID(name)" function
12878 */
12879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012880f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012881 typval_T *argvars;
12882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885}
12886
12887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012888 * "highlight_exists()" function
12889 */
12890 static void
12891f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012892 typval_T *argvars;
12893 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012894{
12895 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12896}
12897
12898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 * "hostname()" function
12900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012903 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905{
12906 char_u hostname[256];
12907
12908 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 rettv->v_type = VAR_STRING;
12910 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012911}
12912
12913/*
12914 * iconv() function
12915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012917f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012918 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012919 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920{
12921#ifdef FEAT_MBYTE
12922 char_u buf1[NUMBUFLEN];
12923 char_u buf2[NUMBUFLEN];
12924 char_u *from, *to, *str;
12925 vimconv_T vimconv;
12926#endif
12927
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 rettv->v_type = VAR_STRING;
12929 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930
12931#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932 str = get_tv_string(&argvars[0]);
12933 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12934 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 vimconv.vc_type = CONV_NONE;
12936 convert_setup(&vimconv, from, to);
12937
12938 /* If the encodings are equal, no conversion needed. */
12939 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012942 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943
12944 convert_setup(&vimconv, NULL, NULL);
12945 vim_free(from);
12946 vim_free(to);
12947#endif
12948}
12949
12950/*
12951 * "indent()" function
12952 */
12953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012954f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012955 typval_T *argvars;
12956 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012957{
12958 linenr_T lnum;
12959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965}
12966
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012967/*
12968 * "index()" function
12969 */
12970 static void
12971f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012972 typval_T *argvars;
12973 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012974{
Bram Moolenaar33570922005-01-25 22:26:29 +000012975 list_T *l;
12976 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012977 long idx = 0;
12978 int ic = FALSE;
12979
12980 rettv->vval.v_number = -1;
12981 if (argvars[0].v_type != VAR_LIST)
12982 {
12983 EMSG(_(e_listreq));
12984 return;
12985 }
12986 l = argvars[0].vval.v_list;
12987 if (l != NULL)
12988 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012989 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012990 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012991 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012992 int error = FALSE;
12993
Bram Moolenaar758711c2005-02-02 23:11:38 +000012994 /* Start at specified item. Use the cached index that list_find()
12995 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012996 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012997 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012998 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012999 ic = get_tv_number_chk(&argvars[3], &error);
13000 if (error)
13001 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013002 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013003
Bram Moolenaar758711c2005-02-02 23:11:38 +000013004 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010013005 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013006 {
13007 rettv->vval.v_number = idx;
13008 break;
13009 }
13010 }
13011}
13012
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013static int inputsecret_flag = 0;
13014
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013015static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13016
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013018 * This function is used by f_input() and f_inputdialog() functions. The third
13019 * argument to f_input() specifies the type of completion to use at the
13020 * prompt. The third argument to f_inputdialog() specifies the value to return
13021 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022 */
13023 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013024get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013025 typval_T *argvars;
13026 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013027 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030 char_u *p = NULL;
13031 int c;
13032 char_u buf[NUMBUFLEN];
13033 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013034 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013035 int xp_type = EXPAND_NOTHING;
13036 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013038 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013039 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013040
13041#ifdef NO_CONSOLE_INPUT
13042 /* While starting up, there is no place to enter text. */
13043 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045#endif
13046
13047 cmd_silent = FALSE; /* Want to see the prompt. */
13048 if (prompt != NULL)
13049 {
13050 /* Only the part of the message after the last NL is considered as
13051 * prompt for the command line */
13052 p = vim_strrchr(prompt, '\n');
13053 if (p == NULL)
13054 p = prompt;
13055 else
13056 {
13057 ++p;
13058 c = *p;
13059 *p = NUL;
13060 msg_start();
13061 msg_clr_eos();
13062 msg_puts_attr(prompt, echo_attr);
13063 msg_didout = FALSE;
13064 msg_starthere();
13065 *p = c;
13066 }
13067 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013069 if (argvars[1].v_type != VAR_UNKNOWN)
13070 {
13071 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13072 if (defstr != NULL)
13073 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013074
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013075 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013076 {
13077 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013078 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013079 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013080
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013081 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013082 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013083
Bram Moolenaar4463f292005-09-25 22:20:24 +000013084 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13085 if (xp_name == NULL)
13086 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013087
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013088 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013089
Bram Moolenaar4463f292005-09-25 22:20:24 +000013090 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13091 &xp_arg) == FAIL)
13092 return;
13093 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013094 }
13095
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013096 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013097 {
13098# ifdef FEAT_EX_EXTRA
13099 int save_ex_normal_busy = ex_normal_busy;
13100 ex_normal_busy = 0;
13101# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013103 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13104 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013105# ifdef FEAT_EX_EXTRA
13106 ex_normal_busy = save_ex_normal_busy;
13107# endif
13108 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013109 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013110 && argvars[1].v_type != VAR_UNKNOWN
13111 && argvars[2].v_type != VAR_UNKNOWN)
13112 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13113 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013114
13115 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013117 /* since the user typed this, no need to wait for return */
13118 need_wait_return = FALSE;
13119 msg_didout = FALSE;
13120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 cmd_silent = cmd_silent_save;
13122}
13123
13124/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013125 * "input()" function
13126 * Also handles inputsecret() when inputsecret is set.
13127 */
13128 static void
13129f_input(argvars, rettv)
13130 typval_T *argvars;
13131 typval_T *rettv;
13132{
13133 get_user_input(argvars, rettv, FALSE);
13134}
13135
13136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137 * "inputdialog()" function
13138 */
13139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013141 typval_T *argvars;
13142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013143{
13144#if defined(FEAT_GUI_TEXTDIALOG)
13145 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13146 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13147 {
13148 char_u *message;
13149 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013150 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013152 message = get_tv_string_chk(&argvars[0]);
13153 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013154 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013155 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 else
13157 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013158 if (message != NULL && defstr != NULL
13159 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013160 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 else
13163 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013164 if (message != NULL && defstr != NULL
13165 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013166 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167 rettv->vval.v_string = vim_strsave(
13168 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013170 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 }
13174 else
13175#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013176 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013177}
13178
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013179/*
13180 * "inputlist()" function
13181 */
13182 static void
13183f_inputlist(argvars, rettv)
13184 typval_T *argvars;
13185 typval_T *rettv;
13186{
13187 listitem_T *li;
13188 int selected;
13189 int mouse_used;
13190
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013191#ifdef NO_CONSOLE_INPUT
13192 /* While starting up, there is no place to enter text. */
13193 if (no_console_input())
13194 return;
13195#endif
13196 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13197 {
13198 EMSG2(_(e_listarg), "inputlist()");
13199 return;
13200 }
13201
13202 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013203 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013204 lines_left = Rows; /* avoid more prompt */
13205 msg_scroll = TRUE;
13206 msg_clr_eos();
13207
13208 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13209 {
13210 msg_puts(get_tv_string(&li->li_tv));
13211 msg_putchar('\n');
13212 }
13213
13214 /* Ask for choice. */
13215 selected = prompt_for_number(&mouse_used);
13216 if (mouse_used)
13217 selected -= lines_left;
13218
13219 rettv->vval.v_number = selected;
13220}
13221
13222
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13224
13225/*
13226 * "inputrestore()" function
13227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013230 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013231 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232{
13233 if (ga_userinput.ga_len > 0)
13234 {
13235 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13237 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013238 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 }
13240 else if (p_verbose > 1)
13241 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013242 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013244 }
13245}
13246
13247/*
13248 * "inputsave()" function
13249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013252 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013253 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013255 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256 if (ga_grow(&ga_userinput, 1) == OK)
13257 {
13258 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13259 + ga_userinput.ga_len);
13260 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013261 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 }
13263 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013264 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265}
13266
13267/*
13268 * "inputsecret()" function
13269 */
13270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013271f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013272 typval_T *argvars;
13273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274{
13275 ++cmdline_star;
13276 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 --cmdline_star;
13279 --inputsecret_flag;
13280}
13281
13282/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013283 * "insert()" function
13284 */
13285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013286f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013287 typval_T *argvars;
13288 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013289{
13290 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013291 listitem_T *item;
13292 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013293 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013294
13295 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013296 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013297 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013298 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013299 {
13300 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013301 before = get_tv_number_chk(&argvars[2], &error);
13302 if (error)
13303 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013304
Bram Moolenaar758711c2005-02-02 23:11:38 +000013305 if (before == l->lv_len)
13306 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013307 else
13308 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013309 item = list_find(l, before);
13310 if (item == NULL)
13311 {
13312 EMSGN(_(e_listidx), before);
13313 l = NULL;
13314 }
13315 }
13316 if (l != NULL)
13317 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013318 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013319 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013320 }
13321 }
13322}
13323
13324/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013325 * "invert(expr)" function
13326 */
13327 static void
13328f_invert(argvars, rettv)
13329 typval_T *argvars;
13330 typval_T *rettv;
13331{
13332 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13333}
13334
13335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336 * "isdirectory()" function
13337 */
13338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013340 typval_T *argvars;
13341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013343 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344}
13345
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013346/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013347 * "islocked()" function
13348 */
13349 static void
13350f_islocked(argvars, rettv)
13351 typval_T *argvars;
13352 typval_T *rettv;
13353{
13354 lval_T lv;
13355 char_u *end;
13356 dictitem_T *di;
13357
13358 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013359 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
13360 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013361 if (end != NULL && lv.ll_name != NULL)
13362 {
13363 if (*end != NUL)
13364 EMSG(_(e_trailing));
13365 else
13366 {
13367 if (lv.ll_tv == NULL)
13368 {
13369 if (check_changedtick(lv.ll_name))
13370 rettv->vval.v_number = 1; /* always locked */
13371 else
13372 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010013373 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013374 if (di != NULL)
13375 {
13376 /* Consider a variable locked when:
13377 * 1. the variable itself is locked
13378 * 2. the value of the variable is locked.
13379 * 3. the List or Dict value is locked.
13380 */
13381 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13382 || tv_islocked(&di->di_tv));
13383 }
13384 }
13385 }
13386 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013387 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013388 else if (lv.ll_newkey != NULL)
13389 EMSG2(_(e_dictkey), lv.ll_newkey);
13390 else if (lv.ll_list != NULL)
13391 /* List item. */
13392 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13393 else
13394 /* Dictionary item. */
13395 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13396 }
13397 }
13398
13399 clear_lval(&lv);
13400}
13401
Bram Moolenaar33570922005-01-25 22:26:29 +000013402static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013403
13404/*
13405 * Turn a dict into a list:
13406 * "what" == 0: list of keys
13407 * "what" == 1: list of values
13408 * "what" == 2: list of items
13409 */
13410 static void
13411dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013412 typval_T *argvars;
13413 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013414 int what;
13415{
Bram Moolenaar33570922005-01-25 22:26:29 +000013416 list_T *l2;
13417 dictitem_T *di;
13418 hashitem_T *hi;
13419 listitem_T *li;
13420 listitem_T *li2;
13421 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013422 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013423
Bram Moolenaar8c711452005-01-14 21:53:12 +000013424 if (argvars[0].v_type != VAR_DICT)
13425 {
13426 EMSG(_(e_dictreq));
13427 return;
13428 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013429 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013430 return;
13431
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013432 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013433 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013434
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013435 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013437 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013438 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013439 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013440 --todo;
13441 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013442
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013443 li = listitem_alloc();
13444 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013445 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013446 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013447
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013448 if (what == 0)
13449 {
13450 /* keys() */
13451 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013452 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013453 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13454 }
13455 else if (what == 1)
13456 {
13457 /* values() */
13458 copy_tv(&di->di_tv, &li->li_tv);
13459 }
13460 else
13461 {
13462 /* items() */
13463 l2 = list_alloc();
13464 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013465 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013466 li->li_tv.vval.v_list = l2;
13467 if (l2 == NULL)
13468 break;
13469 ++l2->lv_refcount;
13470
13471 li2 = listitem_alloc();
13472 if (li2 == NULL)
13473 break;
13474 list_append(l2, li2);
13475 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013476 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013477 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13478
13479 li2 = listitem_alloc();
13480 if (li2 == NULL)
13481 break;
13482 list_append(l2, li2);
13483 copy_tv(&di->di_tv, &li2->li_tv);
13484 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013485 }
13486 }
13487}
13488
13489/*
13490 * "items(dict)" function
13491 */
13492 static void
13493f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013494 typval_T *argvars;
13495 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013496{
13497 dict_list(argvars, rettv, 2);
13498}
13499
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013501 * "join()" function
13502 */
13503 static void
13504f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013505 typval_T *argvars;
13506 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013507{
13508 garray_T ga;
13509 char_u *sep;
13510
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013511 if (argvars[0].v_type != VAR_LIST)
13512 {
13513 EMSG(_(e_listreq));
13514 return;
13515 }
13516 if (argvars[0].vval.v_list == NULL)
13517 return;
13518 if (argvars[1].v_type == VAR_UNKNOWN)
13519 sep = (char_u *)" ";
13520 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013521 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013522
13523 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013524
13525 if (sep != NULL)
13526 {
13527 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013528 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013529 ga_append(&ga, NUL);
13530 rettv->vval.v_string = (char_u *)ga.ga_data;
13531 }
13532 else
13533 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013534}
13535
13536/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013537 * "keys()" function
13538 */
13539 static void
13540f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013541 typval_T *argvars;
13542 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013543{
13544 dict_list(argvars, rettv, 0);
13545}
13546
13547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013548 * "last_buffer_nr()" function.
13549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013551f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013552 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554{
13555 int n = 0;
13556 buf_T *buf;
13557
13558 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13559 if (n < buf->b_fnum)
13560 n = buf->b_fnum;
13561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013562 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013563}
13564
13565/*
13566 * "len()" function
13567 */
13568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013569f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013570 typval_T *argvars;
13571 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013572{
13573 switch (argvars[0].v_type)
13574 {
13575 case VAR_STRING:
13576 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 rettv->vval.v_number = (varnumber_T)STRLEN(
13578 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013579 break;
13580 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013582 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013583 case VAR_DICT:
13584 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13585 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013586 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013587 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013588 break;
13589 }
13590}
13591
Bram Moolenaar33570922005-01-25 22:26:29 +000013592static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013593
13594 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013596 typval_T *argvars;
13597 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013598 int type;
13599{
13600#ifdef FEAT_LIBCALL
13601 char_u *string_in;
13602 char_u **string_result;
13603 int nr_result;
13604#endif
13605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013607 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013608 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013609
13610 if (check_restricted() || check_secure())
13611 return;
13612
13613#ifdef FEAT_LIBCALL
13614 /* The first two args must be strings, otherwise its meaningless */
13615 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13616 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013617 string_in = NULL;
13618 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013619 string_in = argvars[2].vval.v_string;
13620 if (type == VAR_NUMBER)
13621 string_result = NULL;
13622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013624 if (mch_libcall(argvars[0].vval.v_string,
13625 argvars[1].vval.v_string,
13626 string_in,
13627 argvars[2].vval.v_number,
13628 string_result,
13629 &nr_result) == OK
13630 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013632 }
13633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634}
13635
13636/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013637 * "libcall()" function
13638 */
13639 static void
13640f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013641 typval_T *argvars;
13642 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013643{
13644 libcall_common(argvars, rettv, VAR_STRING);
13645}
13646
13647/*
13648 * "libcallnr()" function
13649 */
13650 static void
13651f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013652 typval_T *argvars;
13653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013654{
13655 libcall_common(argvars, rettv, VAR_NUMBER);
13656}
13657
13658/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 * "line(string)" function
13660 */
13661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013663 typval_T *argvars;
13664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665{
13666 linenr_T lnum = 0;
13667 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013668 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013670 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671 if (fp != NULL)
13672 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013673 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013674}
13675
13676/*
13677 * "line2byte(lnum)" function
13678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013680f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013681 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683{
13684#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013685 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686#else
13687 linenr_T lnum;
13688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013691 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013693 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13694 if (rettv->vval.v_number >= 0)
13695 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696#endif
13697}
13698
13699/*
13700 * "lispindent(lnum)" function
13701 */
13702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013703f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013704 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706{
13707#ifdef FEAT_LISP
13708 pos_T pos;
13709 linenr_T lnum;
13710
13711 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13714 {
13715 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 curwin->w_cursor = pos;
13718 }
13719 else
13720#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013721 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722}
13723
13724/*
13725 * "localtime()" function
13726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013728f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733}
13734
Bram Moolenaar33570922005-01-25 22:26:29 +000013735static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736
13737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013738get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013739 typval_T *argvars;
13740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 int exact;
13742{
13743 char_u *keys;
13744 char_u *which;
13745 char_u buf[NUMBUFLEN];
13746 char_u *keys_buf = NULL;
13747 char_u *rhs;
13748 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013749 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013750 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013751 mapblock_T *mp;
13752 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013753
13754 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013755 rettv->v_type = VAR_STRING;
13756 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 if (*keys == NUL)
13760 return;
13761
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013762 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013764 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013765 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013766 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013767 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013768 if (argvars[3].v_type != VAR_UNKNOWN)
13769 get_dict = get_tv_number(&argvars[3]);
13770 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772 else
13773 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013774 if (which == NULL)
13775 return;
13776
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 mode = get_map_mode(&which, 0);
13778
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013779 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013780 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013782
13783 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013784 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013785 /* Return a string. */
13786 if (rhs != NULL)
13787 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013788
Bram Moolenaarbd743252010-10-20 21:23:33 +020013789 }
13790 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13791 {
13792 /* Return a dictionary. */
13793 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13794 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13795 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013796
Bram Moolenaarbd743252010-10-20 21:23:33 +020013797 dict_add_nr_str(dict, "lhs", 0L, lhs);
13798 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13799 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13800 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13801 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13802 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13803 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013804 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013805 dict_add_nr_str(dict, "mode", 0L, mapmode);
13806
13807 vim_free(lhs);
13808 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013809 }
13810}
13811
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013812#ifdef FEAT_FLOAT
13813/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013814 * "log()" function
13815 */
13816 static void
13817f_log(argvars, rettv)
13818 typval_T *argvars;
13819 typval_T *rettv;
13820{
13821 float_T f;
13822
13823 rettv->v_type = VAR_FLOAT;
13824 if (get_float_arg(argvars, &f) == OK)
13825 rettv->vval.v_float = log(f);
13826 else
13827 rettv->vval.v_float = 0.0;
13828}
13829
13830/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013831 * "log10()" function
13832 */
13833 static void
13834f_log10(argvars, rettv)
13835 typval_T *argvars;
13836 typval_T *rettv;
13837{
13838 float_T f;
13839
13840 rettv->v_type = VAR_FLOAT;
13841 if (get_float_arg(argvars, &f) == OK)
13842 rettv->vval.v_float = log10(f);
13843 else
13844 rettv->vval.v_float = 0.0;
13845}
13846#endif
13847
Bram Moolenaar1dced572012-04-05 16:54:08 +020013848#ifdef FEAT_LUA
13849/*
13850 * "luaeval()" function
13851 */
13852 static void
13853f_luaeval(argvars, rettv)
13854 typval_T *argvars;
13855 typval_T *rettv;
13856{
13857 char_u *str;
13858 char_u buf[NUMBUFLEN];
13859
13860 str = get_tv_string_buf(&argvars[0], buf);
13861 do_luaeval(str, argvars + 1, rettv);
13862}
13863#endif
13864
Bram Moolenaar071d4272004-06-13 20:20:40 +000013865/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013866 * "map()" function
13867 */
13868 static void
13869f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013870 typval_T *argvars;
13871 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013872{
13873 filter_map(argvars, rettv, TRUE);
13874}
13875
13876/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013877 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878 */
13879 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013880f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013881 typval_T *argvars;
13882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013884 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885}
13886
13887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013888 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 */
13890 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013891f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013892 typval_T *argvars;
13893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013895 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896}
13897
Bram Moolenaar33570922005-01-25 22:26:29 +000013898static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899
13900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013901find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013902 typval_T *argvars;
13903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 int type;
13905{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013906 char_u *str = NULL;
13907 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 char_u *pat;
13909 regmatch_T regmatch;
13910 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013911 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 char_u *save_cpo;
13913 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013914 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013915 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013916 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013917 list_T *l = NULL;
13918 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013919 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013920 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921
13922 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13923 save_cpo = p_cpo;
13924 p_cpo = (char_u *)"";
13925
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013926 rettv->vval.v_number = -1;
13927 if (type == 3)
13928 {
13929 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013930 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013931 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013932 }
13933 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013935 rettv->v_type = VAR_STRING;
13936 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013939 if (argvars[0].v_type == VAR_LIST)
13940 {
13941 if ((l = argvars[0].vval.v_list) == NULL)
13942 goto theend;
13943 li = l->lv_first;
13944 }
13945 else
13946 expr = str = get_tv_string(&argvars[0]);
13947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013948 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13949 if (pat == NULL)
13950 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013951
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013952 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013954 int error = FALSE;
13955
13956 start = get_tv_number_chk(&argvars[2], &error);
13957 if (error)
13958 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013959 if (l != NULL)
13960 {
13961 li = list_find(l, start);
13962 if (li == NULL)
13963 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013964 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013965 }
13966 else
13967 {
13968 if (start < 0)
13969 start = 0;
13970 if (start > (long)STRLEN(str))
13971 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013972 /* When "count" argument is there ignore matches before "start",
13973 * otherwise skip part of the string. Differs when pattern is "^"
13974 * or "\<". */
13975 if (argvars[3].v_type != VAR_UNKNOWN)
13976 startcol = start;
13977 else
13978 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013979 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013980
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013981 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013982 nth = get_tv_number_chk(&argvars[3], &error);
13983 if (error)
13984 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985 }
13986
13987 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13988 if (regmatch.regprog != NULL)
13989 {
13990 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013991
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013992 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013993 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013994 if (l != NULL)
13995 {
13996 if (li == NULL)
13997 {
13998 match = FALSE;
13999 break;
14000 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014001 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014002 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014003 if (str == NULL)
14004 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014005 }
14006
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000014007 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014008
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014009 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014010 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014011 if (l == NULL && !match)
14012 break;
14013
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014014 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014015 if (l != NULL)
14016 {
14017 li = li->li_next;
14018 ++idx;
14019 }
14020 else
14021 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014022#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014023 startcol = (colnr_T)(regmatch.startp[0]
14024 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014025#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014026 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014027#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014028 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014029 }
14030
14031 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014033 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014034 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014035 int i;
14036
14037 /* return list with matched string and submatches */
14038 for (i = 0; i < NSUBEXP; ++i)
14039 {
14040 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014041 {
14042 if (list_append_string(rettv->vval.v_list,
14043 (char_u *)"", 0) == FAIL)
14044 break;
14045 }
14046 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014047 regmatch.startp[i],
14048 (int)(regmatch.endp[i] - regmatch.startp[i]))
14049 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014050 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014051 }
14052 }
14053 else if (type == 2)
14054 {
14055 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014056 if (l != NULL)
14057 copy_tv(&li->li_tv, rettv);
14058 else
14059 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014061 }
14062 else if (l != NULL)
14063 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 else
14065 {
14066 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014067 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014068 (varnumber_T)(regmatch.startp[0] - str);
14069 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014070 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014072 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 }
14074 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014075 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 }
14077
14078theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014079 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080 p_cpo = save_cpo;
14081}
14082
14083/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014084 * "match()" function
14085 */
14086 static void
14087f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014088 typval_T *argvars;
14089 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014090{
14091 find_some_match(argvars, rettv, 1);
14092}
14093
14094/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014095 * "matchadd()" function
14096 */
14097 static void
14098f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014099 typval_T *argvars UNUSED;
14100 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014101{
14102#ifdef FEAT_SEARCH_EXTRA
14103 char_u buf[NUMBUFLEN];
14104 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14105 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14106 int prio = 10; /* default priority */
14107 int id = -1;
14108 int error = FALSE;
14109
14110 rettv->vval.v_number = -1;
14111
14112 if (grp == NULL || pat == NULL)
14113 return;
14114 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014115 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014116 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014117 if (argvars[3].v_type != VAR_UNKNOWN)
14118 id = get_tv_number_chk(&argvars[3], &error);
14119 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014120 if (error == TRUE)
14121 return;
14122 if (id >= 1 && id <= 3)
14123 {
14124 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14125 return;
14126 }
14127
14128 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14129#endif
14130}
14131
14132/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014133 * "matcharg()" function
14134 */
14135 static void
14136f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014137 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014138 typval_T *rettv;
14139{
14140 if (rettv_list_alloc(rettv) == OK)
14141 {
14142#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014143 int id = get_tv_number(&argvars[0]);
14144 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014145
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014146 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014147 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014148 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14149 {
14150 list_append_string(rettv->vval.v_list,
14151 syn_id2name(m->hlg_id), -1);
14152 list_append_string(rettv->vval.v_list, m->pattern, -1);
14153 }
14154 else
14155 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010014156 list_append_string(rettv->vval.v_list, NULL, -1);
14157 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014158 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014159 }
14160#endif
14161 }
14162}
14163
14164/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014165 * "matchdelete()" function
14166 */
14167 static void
14168f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014169 typval_T *argvars UNUSED;
14170 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014171{
14172#ifdef FEAT_SEARCH_EXTRA
14173 rettv->vval.v_number = match_delete(curwin,
14174 (int)get_tv_number(&argvars[0]), TRUE);
14175#endif
14176}
14177
14178/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014179 * "matchend()" function
14180 */
14181 static void
14182f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014183 typval_T *argvars;
14184 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014185{
14186 find_some_match(argvars, rettv, 0);
14187}
14188
14189/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014190 * "matchlist()" function
14191 */
14192 static void
14193f_matchlist(argvars, rettv)
14194 typval_T *argvars;
14195 typval_T *rettv;
14196{
14197 find_some_match(argvars, rettv, 3);
14198}
14199
14200/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014201 * "matchstr()" function
14202 */
14203 static void
14204f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014205 typval_T *argvars;
14206 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014207{
14208 find_some_match(argvars, rettv, 2);
14209}
14210
Bram Moolenaar33570922005-01-25 22:26:29 +000014211static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014212
14213 static void
14214max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014215 typval_T *argvars;
14216 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014217 int domax;
14218{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014219 long n = 0;
14220 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014221 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014222
14223 if (argvars[0].v_type == VAR_LIST)
14224 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014225 list_T *l;
14226 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014227
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014228 l = argvars[0].vval.v_list;
14229 if (l != NULL)
14230 {
14231 li = l->lv_first;
14232 if (li != NULL)
14233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014234 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014235 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014236 {
14237 li = li->li_next;
14238 if (li == NULL)
14239 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014240 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014241 if (domax ? i > n : i < n)
14242 n = i;
14243 }
14244 }
14245 }
14246 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014247 else if (argvars[0].v_type == VAR_DICT)
14248 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014249 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014250 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014251 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014252 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014253
14254 d = argvars[0].vval.v_dict;
14255 if (d != NULL)
14256 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014257 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014258 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014259 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014260 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014261 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014262 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014264 if (first)
14265 {
14266 n = i;
14267 first = FALSE;
14268 }
14269 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014270 n = i;
14271 }
14272 }
14273 }
14274 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014275 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014276 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014277 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014278}
14279
14280/*
14281 * "max()" function
14282 */
14283 static void
14284f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014285 typval_T *argvars;
14286 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014287{
14288 max_min(argvars, rettv, TRUE);
14289}
14290
14291/*
14292 * "min()" function
14293 */
14294 static void
14295f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014296 typval_T *argvars;
14297 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014298{
14299 max_min(argvars, rettv, FALSE);
14300}
14301
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014302static int mkdir_recurse __ARGS((char_u *dir, int prot));
14303
14304/*
14305 * Create the directory in which "dir" is located, and higher levels when
14306 * needed.
14307 */
14308 static int
14309mkdir_recurse(dir, prot)
14310 char_u *dir;
14311 int prot;
14312{
14313 char_u *p;
14314 char_u *updir;
14315 int r = FAIL;
14316
14317 /* Get end of directory name in "dir".
14318 * We're done when it's "/" or "c:/". */
14319 p = gettail_sep(dir);
14320 if (p <= get_past_head(dir))
14321 return OK;
14322
14323 /* If the directory exists we're done. Otherwise: create it.*/
14324 updir = vim_strnsave(dir, (int)(p - dir));
14325 if (updir == NULL)
14326 return FAIL;
14327 if (mch_isdir(updir))
14328 r = OK;
14329 else if (mkdir_recurse(updir, prot) == OK)
14330 r = vim_mkdir_emsg(updir, prot);
14331 vim_free(updir);
14332 return r;
14333}
14334
14335#ifdef vim_mkdir
14336/*
14337 * "mkdir()" function
14338 */
14339 static void
14340f_mkdir(argvars, rettv)
14341 typval_T *argvars;
14342 typval_T *rettv;
14343{
14344 char_u *dir;
14345 char_u buf[NUMBUFLEN];
14346 int prot = 0755;
14347
14348 rettv->vval.v_number = FAIL;
14349 if (check_restricted() || check_secure())
14350 return;
14351
14352 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014353 if (*dir == NUL)
14354 rettv->vval.v_number = FAIL;
14355 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014356 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014357 if (*gettail(dir) == NUL)
14358 /* remove trailing slashes */
14359 *gettail_sep(dir) = NUL;
14360
14361 if (argvars[1].v_type != VAR_UNKNOWN)
14362 {
14363 if (argvars[2].v_type != VAR_UNKNOWN)
14364 prot = get_tv_number_chk(&argvars[2], NULL);
14365 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14366 mkdir_recurse(dir, prot);
14367 }
14368 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014369 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014370}
14371#endif
14372
Bram Moolenaar0d660222005-01-07 21:51:51 +000014373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 * "mode()" function
14375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014378 typval_T *argvars;
14379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014381 char_u buf[3];
14382
14383 buf[1] = NUL;
14384 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385
14386#ifdef FEAT_VISUAL
14387 if (VIsual_active)
14388 {
14389 if (VIsual_select)
14390 buf[0] = VIsual_mode + 's' - 'v';
14391 else
14392 buf[0] = VIsual_mode;
14393 }
14394 else
14395#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014396 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14397 || State == CONFIRM)
14398 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014400 if (State == ASKMORE)
14401 buf[1] = 'm';
14402 else if (State == CONFIRM)
14403 buf[1] = '?';
14404 }
14405 else if (State == EXTERNCMD)
14406 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 else if (State & INSERT)
14408 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014409#ifdef FEAT_VREPLACE
14410 if (State & VREPLACE_FLAG)
14411 {
14412 buf[0] = 'R';
14413 buf[1] = 'v';
14414 }
14415 else
14416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417 if (State & REPLACE_FLAG)
14418 buf[0] = 'R';
14419 else
14420 buf[0] = 'i';
14421 }
14422 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014425 if (exmode_active)
14426 buf[1] = 'v';
14427 }
14428 else if (exmode_active)
14429 {
14430 buf[0] = 'c';
14431 buf[1] = 'e';
14432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014434 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014436 if (finish_op)
14437 buf[1] = 'o';
14438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014440 /* Clear out the minor mode when the argument is not a non-zero number or
14441 * non-empty string. */
14442 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014443 buf[1] = NUL;
14444
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 rettv->vval.v_string = vim_strsave(buf);
14446 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447}
14448
Bram Moolenaar429fa852013-04-15 12:27:36 +020014449#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014450/*
14451 * "mzeval()" function
14452 */
14453 static void
14454f_mzeval(argvars, rettv)
14455 typval_T *argvars;
14456 typval_T *rettv;
14457{
14458 char_u *str;
14459 char_u buf[NUMBUFLEN];
14460
14461 str = get_tv_string_buf(&argvars[0], buf);
14462 do_mzeval(str, rettv);
14463}
Bram Moolenaar75676462013-01-30 14:55:42 +010014464
14465 void
14466mzscheme_call_vim(name, args, rettv)
14467 char_u *name;
14468 typval_T *args;
14469 typval_T *rettv;
14470{
14471 typval_T argvars[3];
14472
14473 argvars[0].v_type = VAR_STRING;
14474 argvars[0].vval.v_string = name;
14475 copy_tv(args, &argvars[1]);
14476 argvars[2].v_type = VAR_UNKNOWN;
14477 f_call(argvars, rettv);
14478 clear_tv(&argvars[1]);
14479}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014480#endif
14481
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014483 * "nextnonblank()" function
14484 */
14485 static void
14486f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014487 typval_T *argvars;
14488 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014489{
14490 linenr_T lnum;
14491
14492 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495 {
14496 lnum = 0;
14497 break;
14498 }
14499 if (*skipwhite(ml_get(lnum)) != NUL)
14500 break;
14501 }
14502 rettv->vval.v_number = lnum;
14503}
14504
14505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 * "nr2char()" function
14507 */
14508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014510 typval_T *argvars;
14511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512{
14513 char_u buf[NUMBUFLEN];
14514
14515#ifdef FEAT_MBYTE
14516 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014517 {
14518 int utf8 = 0;
14519
14520 if (argvars[1].v_type != VAR_UNKNOWN)
14521 utf8 = get_tv_number_chk(&argvars[1], NULL);
14522 if (utf8)
14523 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14524 else
14525 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 else
14528#endif
14529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014530 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 buf[1] = NUL;
14532 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014533 rettv->v_type = VAR_STRING;
14534 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014535}
14536
14537/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014538 * "or(expr, expr)" function
14539 */
14540 static void
14541f_or(argvars, rettv)
14542 typval_T *argvars;
14543 typval_T *rettv;
14544{
14545 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14546 | get_tv_number_chk(&argvars[1], NULL);
14547}
14548
14549/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014550 * "pathshorten()" function
14551 */
14552 static void
14553f_pathshorten(argvars, rettv)
14554 typval_T *argvars;
14555 typval_T *rettv;
14556{
14557 char_u *p;
14558
14559 rettv->v_type = VAR_STRING;
14560 p = get_tv_string_chk(&argvars[0]);
14561 if (p == NULL)
14562 rettv->vval.v_string = NULL;
14563 else
14564 {
14565 p = vim_strsave(p);
14566 rettv->vval.v_string = p;
14567 if (p != NULL)
14568 shorten_dir(p);
14569 }
14570}
14571
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014572#ifdef FEAT_FLOAT
14573/*
14574 * "pow()" function
14575 */
14576 static void
14577f_pow(argvars, rettv)
14578 typval_T *argvars;
14579 typval_T *rettv;
14580{
14581 float_T fx, fy;
14582
14583 rettv->v_type = VAR_FLOAT;
14584 if (get_float_arg(argvars, &fx) == OK
14585 && get_float_arg(&argvars[1], &fy) == OK)
14586 rettv->vval.v_float = pow(fx, fy);
14587 else
14588 rettv->vval.v_float = 0.0;
14589}
14590#endif
14591
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014592/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014593 * "prevnonblank()" function
14594 */
14595 static void
14596f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014597 typval_T *argvars;
14598 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599{
14600 linenr_T lnum;
14601
14602 lnum = get_tv_lnum(argvars);
14603 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14604 lnum = 0;
14605 else
14606 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14607 --lnum;
14608 rettv->vval.v_number = lnum;
14609}
14610
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014611#ifdef HAVE_STDARG_H
14612/* This dummy va_list is here because:
14613 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14614 * - locally in the function results in a "used before set" warning
14615 * - using va_start() to initialize it gives "function with fixed args" error */
14616static va_list ap;
14617#endif
14618
Bram Moolenaar8c711452005-01-14 21:53:12 +000014619/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014620 * "printf()" function
14621 */
14622 static void
14623f_printf(argvars, rettv)
14624 typval_T *argvars;
14625 typval_T *rettv;
14626{
14627 rettv->v_type = VAR_STRING;
14628 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014629#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014630 {
14631 char_u buf[NUMBUFLEN];
14632 int len;
14633 char_u *s;
14634 int saved_did_emsg = did_emsg;
14635 char *fmt;
14636
14637 /* Get the required length, allocate the buffer and do it for real. */
14638 did_emsg = FALSE;
14639 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014640 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014641 if (!did_emsg)
14642 {
14643 s = alloc(len + 1);
14644 if (s != NULL)
14645 {
14646 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014647 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014648 }
14649 }
14650 did_emsg |= saved_did_emsg;
14651 }
14652#endif
14653}
14654
14655/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014656 * "pumvisible()" function
14657 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014658 static void
14659f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014660 typval_T *argvars UNUSED;
14661 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014662{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014663#ifdef FEAT_INS_EXPAND
14664 if (pum_visible())
14665 rettv->vval.v_number = 1;
14666#endif
14667}
14668
Bram Moolenaardb913952012-06-29 12:54:53 +020014669#ifdef FEAT_PYTHON3
14670/*
14671 * "py3eval()" function
14672 */
14673 static void
14674f_py3eval(argvars, rettv)
14675 typval_T *argvars;
14676 typval_T *rettv;
14677{
14678 char_u *str;
14679 char_u buf[NUMBUFLEN];
14680
14681 str = get_tv_string_buf(&argvars[0], buf);
14682 do_py3eval(str, rettv);
14683}
14684#endif
14685
14686#ifdef FEAT_PYTHON
14687/*
14688 * "pyeval()" function
14689 */
14690 static void
14691f_pyeval(argvars, rettv)
14692 typval_T *argvars;
14693 typval_T *rettv;
14694{
14695 char_u *str;
14696 char_u buf[NUMBUFLEN];
14697
14698 str = get_tv_string_buf(&argvars[0], buf);
14699 do_pyeval(str, rettv);
14700}
14701#endif
14702
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014703/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014704 * "range()" function
14705 */
14706 static void
14707f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014708 typval_T *argvars;
14709 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014710{
14711 long start;
14712 long end;
14713 long stride = 1;
14714 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014715 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014717 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014718 if (argvars[1].v_type == VAR_UNKNOWN)
14719 {
14720 end = start - 1;
14721 start = 0;
14722 }
14723 else
14724 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014725 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014726 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014727 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014728 }
14729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014730 if (error)
14731 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014732 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014733 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014734 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014735 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014736 else
14737 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014738 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014739 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014740 if (list_append_number(rettv->vval.v_list,
14741 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014742 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014743 }
14744}
14745
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014746/*
14747 * "readfile()" function
14748 */
14749 static void
14750f_readfile(argvars, rettv)
14751 typval_T *argvars;
14752 typval_T *rettv;
14753{
14754 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014755 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014756 char_u *fname;
14757 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014758 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14759 int io_size = sizeof(buf);
14760 int readlen; /* size of last fread() */
14761 char_u *prev = NULL; /* previously read bytes, if any */
14762 long prevlen = 0; /* length of data in prev */
14763 long prevsize = 0; /* size of prev buffer */
14764 long maxline = MAXLNUM;
14765 long cnt = 0;
14766 char_u *p; /* position in buf */
14767 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014768
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014769 if (argvars[1].v_type != VAR_UNKNOWN)
14770 {
14771 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14772 binary = TRUE;
14773 if (argvars[2].v_type != VAR_UNKNOWN)
14774 maxline = get_tv_number(&argvars[2]);
14775 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014776
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014777 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014778 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014779
14780 /* Always open the file in binary mode, library functions have a mind of
14781 * their own about CR-LF conversion. */
14782 fname = get_tv_string(&argvars[0]);
14783 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14784 {
14785 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14786 return;
14787 }
14788
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014789 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014790 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014791 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014792
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014793 /* This for loop processes what was read, but is also entered at end
14794 * of file so that either:
14795 * - an incomplete line gets written
14796 * - a "binary" file gets an empty line at the end if it ends in a
14797 * newline. */
14798 for (p = buf, start = buf;
14799 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14800 ++p)
14801 {
14802 if (*p == '\n' || readlen <= 0)
14803 {
14804 listitem_T *li;
14805 char_u *s = NULL;
14806 long_u len = p - start;
14807
14808 /* Finished a line. Remove CRs before NL. */
14809 if (readlen > 0 && !binary)
14810 {
14811 while (len > 0 && start[len - 1] == '\r')
14812 --len;
14813 /* removal may cross back to the "prev" string */
14814 if (len == 0)
14815 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14816 --prevlen;
14817 }
14818 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014819 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014820 else
14821 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014822 /* Change "prev" buffer to be the right size. This way
14823 * the bytes are only copied once, and very long lines are
14824 * allocated only once. */
14825 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014826 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014827 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014828 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014829 prev = NULL; /* the list will own the string */
14830 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014831 }
14832 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014833 if (s == NULL)
14834 {
14835 do_outofmem_msg((long_u) prevlen + len + 1);
14836 failed = TRUE;
14837 break;
14838 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014839
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014840 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014841 {
14842 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014843 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014844 break;
14845 }
14846 li->li_tv.v_type = VAR_STRING;
14847 li->li_tv.v_lock = 0;
14848 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014849 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014850
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014851 start = p + 1; /* step over newline */
14852 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014853 break;
14854 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014855 else if (*p == NUL)
14856 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014857#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014858 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14859 * when finding the BF and check the previous two bytes. */
14860 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014861 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014862 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14863 * + 1, these may be in the "prev" string. */
14864 char_u back1 = p >= buf + 1 ? p[-1]
14865 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14866 char_u back2 = p >= buf + 2 ? p[-2]
14867 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14868 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014869
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014870 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014871 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014872 char_u *dest = p - 2;
14873
14874 /* Usually a BOM is at the beginning of a file, and so at
14875 * the beginning of a line; then we can just step over it.
14876 */
14877 if (start == dest)
14878 start = p + 1;
14879 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014880 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014881 /* have to shuffle buf to close gap */
14882 int adjust_prevlen = 0;
14883
14884 if (dest < buf)
14885 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014886 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014887 dest = buf;
14888 }
14889 if (readlen > p - buf + 1)
14890 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14891 readlen -= 3 - adjust_prevlen;
14892 prevlen -= adjust_prevlen;
14893 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014894 }
14895 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014896 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014897#endif
14898 } /* for */
14899
14900 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14901 break;
14902 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014903 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014904 /* There's part of a line in buf, store it in "prev". */
14905 if (p - start + prevlen >= prevsize)
14906 {
14907 /* need bigger "prev" buffer */
14908 char_u *newprev;
14909
14910 /* A common use case is ordinary text files and "prev" gets a
14911 * fragment of a line, so the first allocation is made
14912 * small, to avoid repeatedly 'allocing' large and
14913 * 'reallocing' small. */
14914 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014915 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014916 else
14917 {
14918 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014919 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014920 prevsize = grow50pc > growmin ? grow50pc : growmin;
14921 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014922 newprev = prev == NULL ? alloc(prevsize)
14923 : vim_realloc(prev, prevsize);
14924 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014925 {
14926 do_outofmem_msg((long_u)prevsize);
14927 failed = TRUE;
14928 break;
14929 }
14930 prev = newprev;
14931 }
14932 /* Add the line part to end of "prev". */
14933 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014934 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014935 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014936 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014937
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014938 /*
14939 * For a negative line count use only the lines at the end of the file,
14940 * free the rest.
14941 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014942 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014943 while (cnt > -maxline)
14944 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014945 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014946 --cnt;
14947 }
14948
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014949 if (failed)
14950 {
14951 list_free(rettv->vval.v_list, TRUE);
14952 /* readfile doc says an empty list is returned on error */
14953 rettv->vval.v_list = list_alloc();
14954 }
14955
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014956 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014957 fclose(fd);
14958}
14959
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014960#if defined(FEAT_RELTIME)
14961static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14962
14963/*
14964 * Convert a List to proftime_T.
14965 * Return FAIL when there is something wrong.
14966 */
14967 static int
14968list2proftime(arg, tm)
14969 typval_T *arg;
14970 proftime_T *tm;
14971{
14972 long n1, n2;
14973 int error = FALSE;
14974
14975 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14976 || arg->vval.v_list->lv_len != 2)
14977 return FAIL;
14978 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14979 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14980# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014981 tm->HighPart = n1;
14982 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014983# else
14984 tm->tv_sec = n1;
14985 tm->tv_usec = n2;
14986# endif
14987 return error ? FAIL : OK;
14988}
14989#endif /* FEAT_RELTIME */
14990
14991/*
14992 * "reltime()" function
14993 */
14994 static void
14995f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014996 typval_T *argvars UNUSED;
14997 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014998{
14999#ifdef FEAT_RELTIME
15000 proftime_T res;
15001 proftime_T start;
15002
15003 if (argvars[0].v_type == VAR_UNKNOWN)
15004 {
15005 /* No arguments: get current time. */
15006 profile_start(&res);
15007 }
15008 else if (argvars[1].v_type == VAR_UNKNOWN)
15009 {
15010 if (list2proftime(&argvars[0], &res) == FAIL)
15011 return;
15012 profile_end(&res);
15013 }
15014 else
15015 {
15016 /* Two arguments: compute the difference. */
15017 if (list2proftime(&argvars[0], &start) == FAIL
15018 || list2proftime(&argvars[1], &res) == FAIL)
15019 return;
15020 profile_sub(&res, &start);
15021 }
15022
15023 if (rettv_list_alloc(rettv) == OK)
15024 {
15025 long n1, n2;
15026
15027# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015028 n1 = res.HighPart;
15029 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015030# else
15031 n1 = res.tv_sec;
15032 n2 = res.tv_usec;
15033# endif
15034 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15035 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15036 }
15037#endif
15038}
15039
15040/*
15041 * "reltimestr()" function
15042 */
15043 static void
15044f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015045 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015046 typval_T *rettv;
15047{
15048#ifdef FEAT_RELTIME
15049 proftime_T tm;
15050#endif
15051
15052 rettv->v_type = VAR_STRING;
15053 rettv->vval.v_string = NULL;
15054#ifdef FEAT_RELTIME
15055 if (list2proftime(&argvars[0], &tm) == OK)
15056 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15057#endif
15058}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015059
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15061static void make_connection __ARGS((void));
15062static int check_connection __ARGS((void));
15063
15064 static void
15065make_connection()
15066{
15067 if (X_DISPLAY == NULL
15068# ifdef FEAT_GUI
15069 && !gui.in_use
15070# endif
15071 )
15072 {
15073 x_force_connect = TRUE;
15074 setup_term_clip();
15075 x_force_connect = FALSE;
15076 }
15077}
15078
15079 static int
15080check_connection()
15081{
15082 make_connection();
15083 if (X_DISPLAY == NULL)
15084 {
15085 EMSG(_("E240: No connection to Vim server"));
15086 return FAIL;
15087 }
15088 return OK;
15089}
15090#endif
15091
15092#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015093static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094
15095 static void
15096remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015097 typval_T *argvars;
15098 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099 int expr;
15100{
15101 char_u *server_name;
15102 char_u *keys;
15103 char_u *r = NULL;
15104 char_u buf[NUMBUFLEN];
15105# ifdef WIN32
15106 HWND w;
15107# else
15108 Window w;
15109# endif
15110
15111 if (check_restricted() || check_secure())
15112 return;
15113
15114# ifdef FEAT_X11
15115 if (check_connection() == FAIL)
15116 return;
15117# endif
15118
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015119 server_name = get_tv_string_chk(&argvars[0]);
15120 if (server_name == NULL)
15121 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 keys = get_tv_string_buf(&argvars[1], buf);
15123# ifdef WIN32
15124 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15125# else
15126 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15127 < 0)
15128# endif
15129 {
15130 if (r != NULL)
15131 EMSG(r); /* sending worked but evaluation failed */
15132 else
15133 EMSG2(_("E241: Unable to send to %s"), server_name);
15134 return;
15135 }
15136
15137 rettv->vval.v_string = r;
15138
15139 if (argvars[2].v_type != VAR_UNKNOWN)
15140 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015141 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015142 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015143 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015145 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015146 v.di_tv.v_type = VAR_STRING;
15147 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015148 idvar = get_tv_string_chk(&argvars[2]);
15149 if (idvar != NULL)
15150 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015151 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015152 }
15153}
15154#endif
15155
15156/*
15157 * "remote_expr()" function
15158 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159 static void
15160f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015161 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015162 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163{
15164 rettv->v_type = VAR_STRING;
15165 rettv->vval.v_string = NULL;
15166#ifdef FEAT_CLIENTSERVER
15167 remote_common(argvars, rettv, TRUE);
15168#endif
15169}
15170
15171/*
15172 * "remote_foreground()" function
15173 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174 static void
15175f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015176 typval_T *argvars UNUSED;
15177 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015178{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015179#ifdef FEAT_CLIENTSERVER
15180# ifdef WIN32
15181 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015182 {
15183 char_u *server_name = get_tv_string_chk(&argvars[0]);
15184
15185 if (server_name != NULL)
15186 serverForeground(server_name);
15187 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015188# else
15189 /* Send a foreground() expression to the server. */
15190 argvars[1].v_type = VAR_STRING;
15191 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15192 argvars[2].v_type = VAR_UNKNOWN;
15193 remote_common(argvars, rettv, TRUE);
15194 vim_free(argvars[1].vval.v_string);
15195# endif
15196#endif
15197}
15198
Bram Moolenaar0d660222005-01-07 21:51:51 +000015199 static void
15200f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015201 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015202 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015203{
15204#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015205 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206 char_u *s = NULL;
15207# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015208 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015209# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015210 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211
15212 if (check_restricted() || check_secure())
15213 {
15214 rettv->vval.v_number = -1;
15215 return;
15216 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015217 serverid = get_tv_string_chk(&argvars[0]);
15218 if (serverid == NULL)
15219 {
15220 rettv->vval.v_number = -1;
15221 return; /* type error; errmsg already given */
15222 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015224 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015225 if (n == 0)
15226 rettv->vval.v_number = -1;
15227 else
15228 {
15229 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15230 rettv->vval.v_number = (s != NULL);
15231 }
15232# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015233 if (check_connection() == FAIL)
15234 return;
15235
15236 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015237 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015238# endif
15239
15240 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015242 char_u *retvar;
15243
Bram Moolenaar33570922005-01-25 22:26:29 +000015244 v.di_tv.v_type = VAR_STRING;
15245 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015246 retvar = get_tv_string_chk(&argvars[1]);
15247 if (retvar != NULL)
15248 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015249 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015250 }
15251#else
15252 rettv->vval.v_number = -1;
15253#endif
15254}
15255
Bram Moolenaar0d660222005-01-07 21:51:51 +000015256 static void
15257f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015258 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015259 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015260{
15261 char_u *r = NULL;
15262
15263#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015264 char_u *serverid = get_tv_string_chk(&argvars[0]);
15265
15266 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015267 {
15268# ifdef WIN32
15269 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015270 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015271
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015272 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015273 if (n != 0)
15274 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15275 if (r == NULL)
15276# else
15277 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015278 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015279# endif
15280 EMSG(_("E277: Unable to read a server reply"));
15281 }
15282#endif
15283 rettv->v_type = VAR_STRING;
15284 rettv->vval.v_string = r;
15285}
15286
15287/*
15288 * "remote_send()" function
15289 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015290 static void
15291f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015293 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015294{
15295 rettv->v_type = VAR_STRING;
15296 rettv->vval.v_string = NULL;
15297#ifdef FEAT_CLIENTSERVER
15298 remote_common(argvars, rettv, FALSE);
15299#endif
15300}
15301
15302/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015303 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015304 */
15305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015307 typval_T *argvars;
15308 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015309{
Bram Moolenaar33570922005-01-25 22:26:29 +000015310 list_T *l;
15311 listitem_T *item, *item2;
15312 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015313 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015314 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015315 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015316 dict_T *d;
15317 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015318 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015319
Bram Moolenaar8c711452005-01-14 21:53:12 +000015320 if (argvars[0].v_type == VAR_DICT)
15321 {
15322 if (argvars[2].v_type != VAR_UNKNOWN)
15323 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015324 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015325 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015327 key = get_tv_string_chk(&argvars[1]);
15328 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015330 di = dict_find(d, key, -1);
15331 if (di == NULL)
15332 EMSG2(_(e_dictkey), key);
15333 else
15334 {
15335 *rettv = di->di_tv;
15336 init_tv(&di->di_tv);
15337 dictitem_remove(d, di);
15338 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015339 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015340 }
15341 }
15342 else if (argvars[0].v_type != VAR_LIST)
15343 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015344 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015345 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015346 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015347 int error = FALSE;
15348
15349 idx = get_tv_number_chk(&argvars[1], &error);
15350 if (error)
15351 ; /* type error: do nothing, errmsg already given */
15352 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015353 EMSGN(_(e_listidx), idx);
15354 else
15355 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015356 if (argvars[2].v_type == VAR_UNKNOWN)
15357 {
15358 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015359 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015360 *rettv = item->li_tv;
15361 vim_free(item);
15362 }
15363 else
15364 {
15365 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015366 end = get_tv_number_chk(&argvars[2], &error);
15367 if (error)
15368 ; /* type error: do nothing */
15369 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015370 EMSGN(_(e_listidx), end);
15371 else
15372 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015373 int cnt = 0;
15374
15375 for (li = item; li != NULL; li = li->li_next)
15376 {
15377 ++cnt;
15378 if (li == item2)
15379 break;
15380 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015381 if (li == NULL) /* didn't find "item2" after "item" */
15382 EMSG(_(e_invrange));
15383 else
15384 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015385 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015386 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015387 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015388 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015389 l->lv_first = item;
15390 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015391 item->li_prev = NULL;
15392 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015393 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015394 }
15395 }
15396 }
15397 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015398 }
15399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400}
15401
15402/*
15403 * "rename({from}, {to})" function
15404 */
15405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015406f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015407 typval_T *argvars;
15408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409{
15410 char_u buf[NUMBUFLEN];
15411
15412 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015415 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15416 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417}
15418
15419/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015420 * "repeat()" function
15421 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015422 static void
15423f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015424 typval_T *argvars;
15425 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015426{
15427 char_u *p;
15428 int n;
15429 int slen;
15430 int len;
15431 char_u *r;
15432 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015433
15434 n = get_tv_number(&argvars[1]);
15435 if (argvars[0].v_type == VAR_LIST)
15436 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015437 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015438 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015439 if (list_extend(rettv->vval.v_list,
15440 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015441 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015442 }
15443 else
15444 {
15445 p = get_tv_string(&argvars[0]);
15446 rettv->v_type = VAR_STRING;
15447 rettv->vval.v_string = NULL;
15448
15449 slen = (int)STRLEN(p);
15450 len = slen * n;
15451 if (len <= 0)
15452 return;
15453
15454 r = alloc(len + 1);
15455 if (r != NULL)
15456 {
15457 for (i = 0; i < n; i++)
15458 mch_memmove(r + i * slen, p, (size_t)slen);
15459 r[len] = NUL;
15460 }
15461
15462 rettv->vval.v_string = r;
15463 }
15464}
15465
15466/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 * "resolve()" function
15468 */
15469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015470f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015471 typval_T *argvars;
15472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473{
15474 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015475#ifdef HAVE_READLINK
15476 char_u *buf = NULL;
15477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015479 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015480#ifdef FEAT_SHORTCUT
15481 {
15482 char_u *v = NULL;
15483
15484 v = mch_resolve_shortcut(p);
15485 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015488 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 }
15490#else
15491# ifdef HAVE_READLINK
15492 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 char_u *cpy;
15494 int len;
15495 char_u *remain = NULL;
15496 char_u *q;
15497 int is_relative_to_current = FALSE;
15498 int has_trailing_pathsep = FALSE;
15499 int limit = 100;
15500
15501 p = vim_strsave(p);
15502
15503 if (p[0] == '.' && (vim_ispathsep(p[1])
15504 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15505 is_relative_to_current = TRUE;
15506
15507 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015508 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015509 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015511 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513
15514 q = getnextcomp(p);
15515 if (*q != NUL)
15516 {
15517 /* Separate the first path component in "p", and keep the
15518 * remainder (beginning with the path separator). */
15519 remain = vim_strsave(q - 1);
15520 q[-1] = NUL;
15521 }
15522
Bram Moolenaard9462e32011-04-11 21:35:11 +020015523 buf = alloc(MAXPATHL + 1);
15524 if (buf == NULL)
15525 goto fail;
15526
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527 for (;;)
15528 {
15529 for (;;)
15530 {
15531 len = readlink((char *)p, (char *)buf, MAXPATHL);
15532 if (len <= 0)
15533 break;
15534 buf[len] = NUL;
15535
15536 if (limit-- == 0)
15537 {
15538 vim_free(p);
15539 vim_free(remain);
15540 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015541 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 goto fail;
15543 }
15544
15545 /* Ensure that the result will have a trailing path separator
15546 * if the argument has one. */
15547 if (remain == NULL && has_trailing_pathsep)
15548 add_pathsep(buf);
15549
15550 /* Separate the first path component in the link value and
15551 * concatenate the remainders. */
15552 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15553 if (*q != NUL)
15554 {
15555 if (remain == NULL)
15556 remain = vim_strsave(q - 1);
15557 else
15558 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015559 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 if (cpy != NULL)
15561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 vim_free(remain);
15563 remain = cpy;
15564 }
15565 }
15566 q[-1] = NUL;
15567 }
15568
15569 q = gettail(p);
15570 if (q > p && *q == NUL)
15571 {
15572 /* Ignore trailing path separator. */
15573 q[-1] = NUL;
15574 q = gettail(p);
15575 }
15576 if (q > p && !mch_isFullName(buf))
15577 {
15578 /* symlink is relative to directory of argument */
15579 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15580 if (cpy != NULL)
15581 {
15582 STRCPY(cpy, p);
15583 STRCPY(gettail(cpy), buf);
15584 vim_free(p);
15585 p = cpy;
15586 }
15587 }
15588 else
15589 {
15590 vim_free(p);
15591 p = vim_strsave(buf);
15592 }
15593 }
15594
15595 if (remain == NULL)
15596 break;
15597
15598 /* Append the first path component of "remain" to "p". */
15599 q = getnextcomp(remain + 1);
15600 len = q - remain - (*q != NUL);
15601 cpy = vim_strnsave(p, STRLEN(p) + len);
15602 if (cpy != NULL)
15603 {
15604 STRNCAT(cpy, remain, len);
15605 vim_free(p);
15606 p = cpy;
15607 }
15608 /* Shorten "remain". */
15609 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015610 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 else
15612 {
15613 vim_free(remain);
15614 remain = NULL;
15615 }
15616 }
15617
15618 /* If the result is a relative path name, make it explicitly relative to
15619 * the current directory if and only if the argument had this form. */
15620 if (!vim_ispathsep(*p))
15621 {
15622 if (is_relative_to_current
15623 && *p != NUL
15624 && !(p[0] == '.'
15625 && (p[1] == NUL
15626 || vim_ispathsep(p[1])
15627 || (p[1] == '.'
15628 && (p[2] == NUL
15629 || vim_ispathsep(p[2]))))))
15630 {
15631 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015632 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 if (cpy != NULL)
15634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 vim_free(p);
15636 p = cpy;
15637 }
15638 }
15639 else if (!is_relative_to_current)
15640 {
15641 /* Strip leading "./". */
15642 q = p;
15643 while (q[0] == '.' && vim_ispathsep(q[1]))
15644 q += 2;
15645 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015646 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 }
15648 }
15649
15650 /* Ensure that the result will have no trailing path separator
15651 * if the argument had none. But keep "/" or "//". */
15652 if (!has_trailing_pathsep)
15653 {
15654 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015655 if (after_pathsep(p, q))
15656 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 }
15658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015659 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 }
15661# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015662 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663# endif
15664#endif
15665
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015666 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667
15668#ifdef HAVE_READLINK
15669fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015670 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015672 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015673}
15674
15675/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015676 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 */
15678 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015679f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015680 typval_T *argvars;
15681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682{
Bram Moolenaar33570922005-01-25 22:26:29 +000015683 list_T *l;
15684 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685
Bram Moolenaar0d660222005-01-07 21:51:51 +000015686 if (argvars[0].v_type != VAR_LIST)
15687 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015688 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015689 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015690 {
15691 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015692 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015693 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015694 while (li != NULL)
15695 {
15696 ni = li->li_prev;
15697 list_append(l, li);
15698 li = ni;
15699 }
15700 rettv->vval.v_list = l;
15701 rettv->v_type = VAR_LIST;
15702 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015703 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705}
15706
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015707#define SP_NOMOVE 0x01 /* don't move cursor */
15708#define SP_REPEAT 0x02 /* repeat to find outer pair */
15709#define SP_RETCOUNT 0x04 /* return matchcount */
15710#define SP_SETPCMARK 0x08 /* set previous context mark */
15711#define SP_START 0x10 /* accept match at start position */
15712#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15713#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015714
Bram Moolenaar33570922005-01-25 22:26:29 +000015715static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015716
15717/*
15718 * Get flags for a search function.
15719 * Possibly sets "p_ws".
15720 * Returns BACKWARD, FORWARD or zero (for an error).
15721 */
15722 static int
15723get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015724 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015725 int *flagsp;
15726{
15727 int dir = FORWARD;
15728 char_u *flags;
15729 char_u nbuf[NUMBUFLEN];
15730 int mask;
15731
15732 if (varp->v_type != VAR_UNKNOWN)
15733 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015734 flags = get_tv_string_buf_chk(varp, nbuf);
15735 if (flags == NULL)
15736 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015737 while (*flags != NUL)
15738 {
15739 switch (*flags)
15740 {
15741 case 'b': dir = BACKWARD; break;
15742 case 'w': p_ws = TRUE; break;
15743 case 'W': p_ws = FALSE; break;
15744 default: mask = 0;
15745 if (flagsp != NULL)
15746 switch (*flags)
15747 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015748 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015749 case 'e': mask = SP_END; break;
15750 case 'm': mask = SP_RETCOUNT; break;
15751 case 'n': mask = SP_NOMOVE; break;
15752 case 'p': mask = SP_SUBPAT; break;
15753 case 'r': mask = SP_REPEAT; break;
15754 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015755 }
15756 if (mask == 0)
15757 {
15758 EMSG2(_(e_invarg2), flags);
15759 dir = 0;
15760 }
15761 else
15762 *flagsp |= mask;
15763 }
15764 if (dir == 0)
15765 break;
15766 ++flags;
15767 }
15768 }
15769 return dir;
15770}
15771
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015773 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015775 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015776search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015777 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015778 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015779 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015781 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 char_u *pat;
15783 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015784 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 int save_p_ws = p_ws;
15786 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015787 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015788 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015789 proftime_T tm;
15790#ifdef FEAT_RELTIME
15791 long time_limit = 0;
15792#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015793 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015794 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015796 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015797 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015798 if (dir == 0)
15799 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015800 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015801 if (flags & SP_START)
15802 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015803 if (flags & SP_END)
15804 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015805
Bram Moolenaar76929292008-01-06 19:07:36 +000015806 /* Optional arguments: line number to stop searching and timeout. */
15807 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015808 {
15809 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15810 if (lnum_stop < 0)
15811 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015812#ifdef FEAT_RELTIME
15813 if (argvars[3].v_type != VAR_UNKNOWN)
15814 {
15815 time_limit = get_tv_number_chk(&argvars[3], NULL);
15816 if (time_limit < 0)
15817 goto theend;
15818 }
15819#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015820 }
15821
Bram Moolenaar76929292008-01-06 19:07:36 +000015822#ifdef FEAT_RELTIME
15823 /* Set the time limit, if there is one. */
15824 profile_setlimit(time_limit, &tm);
15825#endif
15826
Bram Moolenaar231334e2005-07-25 20:46:57 +000015827 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015828 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015829 * Check to make sure only those flags are set.
15830 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15831 * flags cannot be set. Check for that condition also.
15832 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015833 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015834 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015836 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015837 goto theend;
15838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015840 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015841 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015842 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015843 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015845 if (flags & SP_SUBPAT)
15846 retval = subpatnum;
15847 else
15848 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015849 if (flags & SP_SETPCMARK)
15850 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015852 if (match_pos != NULL)
15853 {
15854 /* Store the match cursor position */
15855 match_pos->lnum = pos.lnum;
15856 match_pos->col = pos.col + 1;
15857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 /* "/$" will put the cursor after the end of the line, may need to
15859 * correct that here */
15860 check_cursor();
15861 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015862
15863 /* If 'n' flag is used: restore cursor position. */
15864 if (flags & SP_NOMOVE)
15865 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015866 else
15867 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015868theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015870
15871 return retval;
15872}
15873
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015874#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015875
15876/*
15877 * round() is not in C90, use ceil() or floor() instead.
15878 */
15879 float_T
15880vim_round(f)
15881 float_T f;
15882{
15883 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15884}
15885
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015886/*
15887 * "round({float})" function
15888 */
15889 static void
15890f_round(argvars, rettv)
15891 typval_T *argvars;
15892 typval_T *rettv;
15893{
15894 float_T f;
15895
15896 rettv->v_type = VAR_FLOAT;
15897 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015898 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015899 else
15900 rettv->vval.v_float = 0.0;
15901}
15902#endif
15903
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015904/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015905 * "screenattr()" function
15906 */
15907 static void
15908f_screenattr(argvars, rettv)
15909 typval_T *argvars UNUSED;
15910 typval_T *rettv;
15911{
15912 int row;
15913 int col;
15914 int c;
15915
15916 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15917 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15918 if (row < 0 || row >= screen_Rows
15919 || col < 0 || col >= screen_Columns)
15920 c = -1;
15921 else
15922 c = ScreenAttrs[LineOffset[row] + col];
15923 rettv->vval.v_number = c;
15924}
15925
15926/*
15927 * "screenchar()" function
15928 */
15929 static void
15930f_screenchar(argvars, rettv)
15931 typval_T *argvars UNUSED;
15932 typval_T *rettv;
15933{
15934 int row;
15935 int col;
15936 int off;
15937 int c;
15938
15939 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15940 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15941 if (row < 0 || row >= screen_Rows
15942 || col < 0 || col >= screen_Columns)
15943 c = -1;
15944 else
15945 {
15946 off = LineOffset[row] + col;
15947#ifdef FEAT_MBYTE
15948 if (enc_utf8 && ScreenLinesUC[off] != 0)
15949 c = ScreenLinesUC[off];
15950 else
15951#endif
15952 c = ScreenLines[off];
15953 }
15954 rettv->vval.v_number = c;
15955}
15956
15957/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015958 * "screencol()" function
15959 *
15960 * First column is 1 to be consistent with virtcol().
15961 */
15962 static void
15963f_screencol(argvars, rettv)
15964 typval_T *argvars UNUSED;
15965 typval_T *rettv;
15966{
15967 rettv->vval.v_number = screen_screencol() + 1;
15968}
15969
15970/*
15971 * "screenrow()" function
15972 */
15973 static void
15974f_screenrow(argvars, rettv)
15975 typval_T *argvars UNUSED;
15976 typval_T *rettv;
15977{
15978 rettv->vval.v_number = screen_screenrow() + 1;
15979}
15980
15981/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015982 * "search()" function
15983 */
15984 static void
15985f_search(argvars, rettv)
15986 typval_T *argvars;
15987 typval_T *rettv;
15988{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015989 int flags = 0;
15990
15991 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992}
15993
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015995 * "searchdecl()" function
15996 */
15997 static void
15998f_searchdecl(argvars, rettv)
15999 typval_T *argvars;
16000 typval_T *rettv;
16001{
16002 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016003 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016004 int error = FALSE;
16005 char_u *name;
16006
16007 rettv->vval.v_number = 1; /* default: FAIL */
16008
16009 name = get_tv_string_chk(&argvars[0]);
16010 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000016011 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016012 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000016013 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16014 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16015 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016016 if (!error && name != NULL)
16017 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016018 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016019}
16020
16021/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016022 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016023 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016024 static int
16025searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016026 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016027 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016028{
16029 char_u *spat, *mpat, *epat;
16030 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 int dir;
16033 int flags = 0;
16034 char_u nbuf1[NUMBUFLEN];
16035 char_u nbuf2[NUMBUFLEN];
16036 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016037 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016038 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016039 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016042 spat = get_tv_string_chk(&argvars[0]);
16043 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16044 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16045 if (spat == NULL || mpat == NULL || epat == NULL)
16046 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016047
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 /* Handle the optional fourth argument: flags */
16049 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016050 if (dir == 0)
16051 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016052
16053 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016054 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16055 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016056 if ((flags & (SP_END | SP_SUBPAT)) != 0
16057 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016058 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016059 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016060 goto theend;
16061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016063 /* Using 'r' implies 'W', otherwise it doesn't work. */
16064 if (flags & SP_REPEAT)
16065 p_ws = FALSE;
16066
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016067 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016068 if (argvars[3].v_type == VAR_UNKNOWN
16069 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070 skip = (char_u *)"";
16071 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016072 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016073 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016074 if (argvars[5].v_type != VAR_UNKNOWN)
16075 {
16076 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16077 if (lnum_stop < 0)
16078 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016079#ifdef FEAT_RELTIME
16080 if (argvars[6].v_type != VAR_UNKNOWN)
16081 {
16082 time_limit = get_tv_number_chk(&argvars[6], NULL);
16083 if (time_limit < 0)
16084 goto theend;
16085 }
16086#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016087 }
16088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016089 if (skip == NULL)
16090 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016092 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016093 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016094
16095theend:
16096 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016097
16098 return retval;
16099}
16100
16101/*
16102 * "searchpair()" function
16103 */
16104 static void
16105f_searchpair(argvars, rettv)
16106 typval_T *argvars;
16107 typval_T *rettv;
16108{
16109 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16110}
16111
16112/*
16113 * "searchpairpos()" function
16114 */
16115 static void
16116f_searchpairpos(argvars, rettv)
16117 typval_T *argvars;
16118 typval_T *rettv;
16119{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016120 pos_T match_pos;
16121 int lnum = 0;
16122 int col = 0;
16123
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016124 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016125 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016126
16127 if (searchpair_cmn(argvars, &match_pos) > 0)
16128 {
16129 lnum = match_pos.lnum;
16130 col = match_pos.col;
16131 }
16132
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016133 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16134 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016135}
16136
16137/*
16138 * Search for a start/middle/end thing.
16139 * Used by searchpair(), see its documentation for the details.
16140 * Returns 0 or -1 for no match,
16141 */
16142 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016143do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16144 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016145 char_u *spat; /* start pattern */
16146 char_u *mpat; /* middle pattern */
16147 char_u *epat; /* end pattern */
16148 int dir; /* BACKWARD or FORWARD */
16149 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016150 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016151 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016152 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016153 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016154{
16155 char_u *save_cpo;
16156 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16157 long retval = 0;
16158 pos_T pos;
16159 pos_T firstpos;
16160 pos_T foundpos;
16161 pos_T save_cursor;
16162 pos_T save_pos;
16163 int n;
16164 int r;
16165 int nest = 1;
16166 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016167 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016168 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016169
16170 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16171 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016172 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016173
Bram Moolenaar76929292008-01-06 19:07:36 +000016174#ifdef FEAT_RELTIME
16175 /* Set the time limit, if there is one. */
16176 profile_setlimit(time_limit, &tm);
16177#endif
16178
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016179 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16180 * start/middle/end (pat3, for the top pair). */
16181 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16182 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16183 if (pat2 == NULL || pat3 == NULL)
16184 goto theend;
16185 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16186 if (*mpat == NUL)
16187 STRCPY(pat3, pat2);
16188 else
16189 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16190 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016191 if (flags & SP_START)
16192 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016193
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194 save_cursor = curwin->w_cursor;
16195 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016196 clearpos(&firstpos);
16197 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 pat = pat3;
16199 for (;;)
16200 {
16201 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016202 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16204 /* didn't find it or found the first match again: FAIL */
16205 break;
16206
16207 if (firstpos.lnum == 0)
16208 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016209 if (equalpos(pos, foundpos))
16210 {
16211 /* Found the same position again. Can happen with a pattern that
16212 * has "\zs" at the end and searching backwards. Advance one
16213 * character and try again. */
16214 if (dir == BACKWARD)
16215 decl(&pos);
16216 else
16217 incl(&pos);
16218 }
16219 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016220
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016221 /* clear the start flag to avoid getting stuck here */
16222 options &= ~SEARCH_START;
16223
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 /* If the skip pattern matches, ignore this match. */
16225 if (*skip != NUL)
16226 {
16227 save_pos = curwin->w_cursor;
16228 curwin->w_cursor = pos;
16229 r = eval_to_bool(skip, &err, NULL, FALSE);
16230 curwin->w_cursor = save_pos;
16231 if (err)
16232 {
16233 /* Evaluating {skip} caused an error, break here. */
16234 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016235 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 break;
16237 }
16238 if (r)
16239 continue;
16240 }
16241
16242 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16243 {
16244 /* Found end when searching backwards or start when searching
16245 * forward: nested pair. */
16246 ++nest;
16247 pat = pat2; /* nested, don't search for middle */
16248 }
16249 else
16250 {
16251 /* Found end when searching forward or start when searching
16252 * backward: end of (nested) pair; or found middle in outer pair. */
16253 if (--nest == 1)
16254 pat = pat3; /* outer level, search for middle */
16255 }
16256
16257 if (nest == 0)
16258 {
16259 /* Found the match: return matchcount or line number. */
16260 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016261 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016263 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016264 if (flags & SP_SETPCMARK)
16265 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 curwin->w_cursor = pos;
16267 if (!(flags & SP_REPEAT))
16268 break;
16269 nest = 1; /* search for next unmatched */
16270 }
16271 }
16272
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016273 if (match_pos != NULL)
16274 {
16275 /* Store the match cursor position */
16276 match_pos->lnum = curwin->w_cursor.lnum;
16277 match_pos->col = curwin->w_cursor.col + 1;
16278 }
16279
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016281 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 curwin->w_cursor = save_cursor;
16283
16284theend:
16285 vim_free(pat2);
16286 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016287 if (p_cpo == empty_option)
16288 p_cpo = save_cpo;
16289 else
16290 /* Darn, evaluating the {skip} expression changed the value. */
16291 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016292
16293 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294}
16295
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016296/*
16297 * "searchpos()" function
16298 */
16299 static void
16300f_searchpos(argvars, rettv)
16301 typval_T *argvars;
16302 typval_T *rettv;
16303{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016304 pos_T match_pos;
16305 int lnum = 0;
16306 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016307 int n;
16308 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016309
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016310 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016311 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016312
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016313 n = search_cmn(argvars, &match_pos, &flags);
16314 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016315 {
16316 lnum = match_pos.lnum;
16317 col = match_pos.col;
16318 }
16319
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016320 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16321 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016322 if (flags & SP_SUBPAT)
16323 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016324}
16325
16326
Bram Moolenaar0d660222005-01-07 21:51:51 +000016327 static void
16328f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016329 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016332#ifdef FEAT_CLIENTSERVER
16333 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016334 char_u *server = get_tv_string_chk(&argvars[0]);
16335 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016338 if (server == NULL || reply == NULL)
16339 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016340 if (check_restricted() || check_secure())
16341 return;
16342# ifdef FEAT_X11
16343 if (check_connection() == FAIL)
16344 return;
16345# endif
16346
16347 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016349 EMSG(_("E258: Unable to send to client"));
16350 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016352 rettv->vval.v_number = 0;
16353#else
16354 rettv->vval.v_number = -1;
16355#endif
16356}
16357
Bram Moolenaar0d660222005-01-07 21:51:51 +000016358 static void
16359f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016360 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016361 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016362{
16363 char_u *r = NULL;
16364
16365#ifdef FEAT_CLIENTSERVER
16366# ifdef WIN32
16367 r = serverGetVimNames();
16368# else
16369 make_connection();
16370 if (X_DISPLAY != NULL)
16371 r = serverGetVimNames(X_DISPLAY);
16372# endif
16373#endif
16374 rettv->v_type = VAR_STRING;
16375 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376}
16377
16378/*
16379 * "setbufvar()" function
16380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016382f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016383 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016384 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385{
16386 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016389 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390 char_u nbuf[NUMBUFLEN];
16391
16392 if (check_restricted() || check_secure())
16393 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016394 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16395 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016396 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 varp = &argvars[2];
16398
16399 if (buf != NULL && varname != NULL && varp != NULL)
16400 {
16401 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403
16404 if (*varname == '&')
16405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406 long numval;
16407 char_u *strval;
16408 int error = FALSE;
16409
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016411 numval = get_tv_number_chk(varp, &error);
16412 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016413 if (!error && strval != NULL)
16414 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 }
16416 else
16417 {
16418 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16419 if (bufvarname != NULL)
16420 {
16421 STRCPY(bufvarname, "b:");
16422 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016423 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016424 vim_free(bufvarname);
16425 }
16426 }
16427
16428 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016429 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431}
16432
16433/*
16434 * "setcmdpos()" function
16435 */
16436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016437f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016438 typval_T *argvars;
16439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 int pos = (int)get_tv_number(&argvars[0]) - 1;
16442
16443 if (pos >= 0)
16444 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016445}
16446
16447/*
16448 * "setline()" function
16449 */
16450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016451f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016452 typval_T *argvars;
16453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454{
16455 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016456 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016457 list_T *l = NULL;
16458 listitem_T *li = NULL;
16459 long added = 0;
16460 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016462 lnum = get_tv_lnum(&argvars[0]);
16463 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016465 l = argvars[1].vval.v_list;
16466 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016468 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016469 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016470
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016471 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016472 for (;;)
16473 {
16474 if (l != NULL)
16475 {
16476 /* list argument, get next string */
16477 if (li == NULL)
16478 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016479 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016480 li = li->li_next;
16481 }
16482
16483 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016484 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016485 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016486
16487 /* When coming here from Insert mode, sync undo, so that this can be
16488 * undone separately from what was previously inserted. */
16489 if (u_sync_once == 2)
16490 {
16491 u_sync_once = 1; /* notify that u_sync() was called */
16492 u_sync(TRUE);
16493 }
16494
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016495 if (lnum <= curbuf->b_ml.ml_line_count)
16496 {
16497 /* existing line, replace it */
16498 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16499 {
16500 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016501 if (lnum == curwin->w_cursor.lnum)
16502 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016503 rettv->vval.v_number = 0; /* OK */
16504 }
16505 }
16506 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16507 {
16508 /* lnum is one past the last line, append the line */
16509 ++added;
16510 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16511 rettv->vval.v_number = 0; /* OK */
16512 }
16513
16514 if (l == NULL) /* only one string argument */
16515 break;
16516 ++lnum;
16517 }
16518
16519 if (added > 0)
16520 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521}
16522
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016523static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16524
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016526 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016527 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016528 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016529set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016530 win_T *wp UNUSED;
16531 typval_T *list_arg UNUSED;
16532 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016533 typval_T *rettv;
16534{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016535#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016536 char_u *act;
16537 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016538#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016539
Bram Moolenaar2641f772005-03-25 21:58:17 +000016540 rettv->vval.v_number = -1;
16541
16542#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016543 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016544 EMSG(_(e_listreq));
16545 else
16546 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016547 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016548
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016549 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016550 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016551 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016552 if (act == NULL)
16553 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016554 if (*act == 'a' || *act == 'r')
16555 action = *act;
16556 }
16557
Bram Moolenaar81484f42012-12-05 15:16:47 +010016558 if (l != NULL && set_errorlist(wp, l, action,
16559 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016560 rettv->vval.v_number = 0;
16561 }
16562#endif
16563}
16564
16565/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016566 * "setloclist()" function
16567 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016568 static void
16569f_setloclist(argvars, rettv)
16570 typval_T *argvars;
16571 typval_T *rettv;
16572{
16573 win_T *win;
16574
16575 rettv->vval.v_number = -1;
16576
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016577 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016578 if (win != NULL)
16579 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16580}
16581
16582/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016583 * "setmatches()" function
16584 */
16585 static void
16586f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016587 typval_T *argvars UNUSED;
16588 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016589{
16590#ifdef FEAT_SEARCH_EXTRA
16591 list_T *l;
16592 listitem_T *li;
16593 dict_T *d;
16594
16595 rettv->vval.v_number = -1;
16596 if (argvars[0].v_type != VAR_LIST)
16597 {
16598 EMSG(_(e_listreq));
16599 return;
16600 }
16601 if ((l = argvars[0].vval.v_list) != NULL)
16602 {
16603
16604 /* To some extent make sure that we are dealing with a list from
16605 * "getmatches()". */
16606 li = l->lv_first;
16607 while (li != NULL)
16608 {
16609 if (li->li_tv.v_type != VAR_DICT
16610 || (d = li->li_tv.vval.v_dict) == NULL)
16611 {
16612 EMSG(_(e_invarg));
16613 return;
16614 }
16615 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16616 && dict_find(d, (char_u *)"pattern", -1) != NULL
16617 && dict_find(d, (char_u *)"priority", -1) != NULL
16618 && dict_find(d, (char_u *)"id", -1) != NULL))
16619 {
16620 EMSG(_(e_invarg));
16621 return;
16622 }
16623 li = li->li_next;
16624 }
16625
16626 clear_matches(curwin);
16627 li = l->lv_first;
16628 while (li != NULL)
16629 {
16630 d = li->li_tv.vval.v_dict;
16631 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16632 get_dict_string(d, (char_u *)"pattern", FALSE),
16633 (int)get_dict_number(d, (char_u *)"priority"),
16634 (int)get_dict_number(d, (char_u *)"id"));
16635 li = li->li_next;
16636 }
16637 rettv->vval.v_number = 0;
16638 }
16639#endif
16640}
16641
16642/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016643 * "setpos()" function
16644 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016645 static void
16646f_setpos(argvars, rettv)
16647 typval_T *argvars;
16648 typval_T *rettv;
16649{
16650 pos_T pos;
16651 int fnum;
16652 char_u *name;
16653
Bram Moolenaar08250432008-02-13 11:42:46 +000016654 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016655 name = get_tv_string_chk(argvars);
16656 if (name != NULL)
16657 {
16658 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16659 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016660 if (--pos.col < 0)
16661 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016662 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016663 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016664 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016665 if (fnum == curbuf->b_fnum)
16666 {
16667 curwin->w_cursor = pos;
16668 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016669 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016670 }
16671 else
16672 EMSG(_(e_invarg));
16673 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016674 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16675 {
16676 /* set mark */
16677 if (setmark_pos(name[1], &pos, fnum) == OK)
16678 rettv->vval.v_number = 0;
16679 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016680 else
16681 EMSG(_(e_invarg));
16682 }
16683 }
16684}
16685
16686/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016687 * "setqflist()" function
16688 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016689 static void
16690f_setqflist(argvars, rettv)
16691 typval_T *argvars;
16692 typval_T *rettv;
16693{
16694 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16695}
16696
16697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016698 * "setreg()" function
16699 */
16700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016701f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 typval_T *argvars;
16703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704{
16705 int regname;
16706 char_u *strregname;
16707 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016708 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 int append;
16710 char_u yank_type;
16711 long block_len;
16712
16713 block_len = -1;
16714 yank_type = MAUTO;
16715 append = FALSE;
16716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016717 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016718 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016720 if (strregname == NULL)
16721 return; /* type error; errmsg already given */
16722 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016723 if (regname == 0 || regname == '@')
16724 regname = '"';
16725 else if (regname == '=')
16726 return;
16727
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016728 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016730 stropt = get_tv_string_chk(&argvars[2]);
16731 if (stropt == NULL)
16732 return; /* type error */
16733 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016734 switch (*stropt)
16735 {
16736 case 'a': case 'A': /* append */
16737 append = TRUE;
16738 break;
16739 case 'v': case 'c': /* character-wise selection */
16740 yank_type = MCHAR;
16741 break;
16742 case 'V': case 'l': /* line-wise selection */
16743 yank_type = MLINE;
16744 break;
16745#ifdef FEAT_VISUAL
16746 case 'b': case Ctrl_V: /* block-wise selection */
16747 yank_type = MBLOCK;
16748 if (VIM_ISDIGIT(stropt[1]))
16749 {
16750 ++stropt;
16751 block_len = getdigits(&stropt) - 1;
16752 --stropt;
16753 }
16754 break;
16755#endif
16756 }
16757 }
16758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016759 strval = get_tv_string_chk(&argvars[1]);
16760 if (strval != NULL)
16761 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016762 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016763 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764}
16765
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016766/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016767 * "settabvar()" function
16768 */
16769 static void
16770f_settabvar(argvars, rettv)
16771 typval_T *argvars;
16772 typval_T *rettv;
16773{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016774#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016775 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016776 tabpage_T *tp;
16777#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016778 char_u *varname, *tabvarname;
16779 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016780
16781 rettv->vval.v_number = 0;
16782
16783 if (check_restricted() || check_secure())
16784 return;
16785
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016786#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016787 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016788#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016789 varname = get_tv_string_chk(&argvars[1]);
16790 varp = &argvars[2];
16791
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016792 if (varname != NULL && varp != NULL
16793#ifdef FEAT_WINDOWS
16794 && tp != NULL
16795#endif
16796 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016797 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016798#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016799 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016800 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016801#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016802
16803 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16804 if (tabvarname != NULL)
16805 {
16806 STRCPY(tabvarname, "t:");
16807 STRCPY(tabvarname + 2, varname);
16808 set_var(tabvarname, varp, TRUE);
16809 vim_free(tabvarname);
16810 }
16811
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016812#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016813 /* Restore current tabpage */
16814 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016815 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016816#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016817 }
16818}
16819
16820/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016821 * "settabwinvar()" function
16822 */
16823 static void
16824f_settabwinvar(argvars, rettv)
16825 typval_T *argvars;
16826 typval_T *rettv;
16827{
16828 setwinvar(argvars, rettv, 1);
16829}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830
16831/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016832 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016835f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016836 typval_T *argvars;
16837 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016839 setwinvar(argvars, rettv, 0);
16840}
16841
16842/*
16843 * "setwinvar()" and "settabwinvar()" functions
16844 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016845
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016846 static void
16847setwinvar(argvars, rettv, off)
16848 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016849 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016850 int off;
16851{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 win_T *win;
16853#ifdef FEAT_WINDOWS
16854 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016855 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016856#endif
16857 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016858 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016860 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861
16862 if (check_restricted() || check_secure())
16863 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016864
16865#ifdef FEAT_WINDOWS
16866 if (off == 1)
16867 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16868 else
16869 tp = curtab;
16870#endif
16871 win = find_win_by_nr(&argvars[off], tp);
16872 varname = get_tv_string_chk(&argvars[off + 1]);
16873 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874
16875 if (win != NULL && varname != NULL && varp != NULL)
16876 {
16877#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016878 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016879 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880#endif
16881
16882 if (*varname == '&')
16883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016884 long numval;
16885 char_u *strval;
16886 int error = FALSE;
16887
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016889 numval = get_tv_number_chk(varp, &error);
16890 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016891 if (!error && strval != NULL)
16892 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016893 }
16894 else
16895 {
16896 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16897 if (winvarname != NULL)
16898 {
16899 STRCPY(winvarname, "w:");
16900 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016901 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 vim_free(winvarname);
16903 }
16904 }
16905
16906#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016907 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016908#endif
16909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016910}
16911
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016912#ifdef FEAT_CRYPT
16913/*
16914 * "sha256({string})" function
16915 */
16916 static void
16917f_sha256(argvars, rettv)
16918 typval_T *argvars;
16919 typval_T *rettv;
16920{
16921 char_u *p;
16922
16923 p = get_tv_string(&argvars[0]);
16924 rettv->vval.v_string = vim_strsave(
16925 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16926 rettv->v_type = VAR_STRING;
16927}
16928#endif /* FEAT_CRYPT */
16929
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016931 * "shellescape({string})" function
16932 */
16933 static void
16934f_shellescape(argvars, rettv)
16935 typval_T *argvars;
16936 typval_T *rettv;
16937{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016938 rettv->vval.v_string = vim_strsave_shellescape(
16939 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016940 rettv->v_type = VAR_STRING;
16941}
16942
16943/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016944 * shiftwidth() function
16945 */
16946 static void
16947f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016948 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016949 typval_T *rettv;
16950{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016951 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016952}
16953
16954/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 */
16957 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016959 typval_T *argvars;
16960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016961{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 p = get_tv_string(&argvars[0]);
16965 rettv->vval.v_string = vim_strsave(p);
16966 simplify_filename(rettv->vval.v_string); /* simplify in place */
16967 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968}
16969
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016970#ifdef FEAT_FLOAT
16971/*
16972 * "sin()" function
16973 */
16974 static void
16975f_sin(argvars, rettv)
16976 typval_T *argvars;
16977 typval_T *rettv;
16978{
16979 float_T f;
16980
16981 rettv->v_type = VAR_FLOAT;
16982 if (get_float_arg(argvars, &f) == OK)
16983 rettv->vval.v_float = sin(f);
16984 else
16985 rettv->vval.v_float = 0.0;
16986}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016987
16988/*
16989 * "sinh()" function
16990 */
16991 static void
16992f_sinh(argvars, rettv)
16993 typval_T *argvars;
16994 typval_T *rettv;
16995{
16996 float_T f;
16997
16998 rettv->v_type = VAR_FLOAT;
16999 if (get_float_arg(argvars, &f) == OK)
17000 rettv->vval.v_float = sinh(f);
17001 else
17002 rettv->vval.v_float = 0.0;
17003}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017004#endif
17005
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006static int
17007#ifdef __BORLANDC__
17008 _RTLENTRYF
17009#endif
17010 item_compare __ARGS((const void *s1, const void *s2));
17011static int
17012#ifdef __BORLANDC__
17013 _RTLENTRYF
17014#endif
17015 item_compare2 __ARGS((const void *s1, const void *s2));
17016
17017static int item_compare_ic;
17018static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017019static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017020static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021#define ITEM_COMPARE_FAIL 999
17022
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026 static int
17027#ifdef __BORLANDC__
17028_RTLENTRYF
17029#endif
17030item_compare(s1, s2)
17031 const void *s1;
17032 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017034 char_u *p1, *p2;
17035 char_u *tofree1, *tofree2;
17036 int res;
17037 char_u numbuf1[NUMBUFLEN];
17038 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017040 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17041 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017042 if (p1 == NULL)
17043 p1 = (char_u *)"";
17044 if (p2 == NULL)
17045 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046 if (item_compare_ic)
17047 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049 res = STRCMP(p1, p2);
17050 vim_free(tofree1);
17051 vim_free(tofree2);
17052 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053}
17054
17055 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017056#ifdef __BORLANDC__
17057_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017059item_compare2(s1, s2)
17060 const void *s1;
17061 const void *s2;
17062{
17063 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017064 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017065 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017066 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017068 /* shortcut after failure in previous call; compare all items equal */
17069 if (item_compare_func_err)
17070 return 0;
17071
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017072 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17073 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017074 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17075 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076
17077 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017078 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017079 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17080 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081 clear_tv(&argv[0]);
17082 clear_tv(&argv[1]);
17083
17084 if (res == FAIL)
17085 res = ITEM_COMPARE_FAIL;
17086 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017087 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17088 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017089 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090 clear_tv(&rettv);
17091 return res;
17092}
17093
17094/*
17095 * "sort({list})" function
17096 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017099 typval_T *argvars;
17100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017101{
Bram Moolenaar33570922005-01-25 22:26:29 +000017102 list_T *l;
17103 listitem_T *li;
17104 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105 long len;
17106 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107
Bram Moolenaar0d660222005-01-07 21:51:51 +000017108 if (argvars[0].v_type != VAR_LIST)
17109 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 else
17111 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017113 if (l == NULL || tv_check_lock(l->lv_lock,
17114 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017115 return;
17116 rettv->vval.v_list = l;
17117 rettv->v_type = VAR_LIST;
17118 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017119
Bram Moolenaar0d660222005-01-07 21:51:51 +000017120 len = list_len(l);
17121 if (len <= 1)
17122 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123
Bram Moolenaar0d660222005-01-07 21:51:51 +000017124 item_compare_ic = FALSE;
17125 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017126 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017127 if (argvars[1].v_type != VAR_UNKNOWN)
17128 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017129 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017131 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017132 else
17133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017134 int error = FALSE;
17135
17136 i = get_tv_number_chk(&argvars[1], &error);
17137 if (error)
17138 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017139 if (i == 1)
17140 item_compare_ic = TRUE;
17141 else
17142 item_compare_func = get_tv_string(&argvars[1]);
17143 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017144
17145 if (argvars[2].v_type != VAR_UNKNOWN)
17146 {
17147 /* optional third argument: {dict} */
17148 if (argvars[2].v_type != VAR_DICT)
17149 {
17150 EMSG(_(e_dictreq));
17151 return;
17152 }
17153 item_compare_selfdict = argvars[2].vval.v_dict;
17154 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017156
Bram Moolenaar0d660222005-01-07 21:51:51 +000017157 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017158 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159 if (ptrs == NULL)
17160 return;
17161 i = 0;
17162 for (li = l->lv_first; li != NULL; li = li->li_next)
17163 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017165 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 /* test the compare function */
17167 if (item_compare_func != NULL
17168 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17169 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017170 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017172 {
17173 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017174 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017175 item_compare_func == NULL ? item_compare : item_compare2);
17176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017177 if (!item_compare_func_err)
17178 {
17179 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017180 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017181 l->lv_len = 0;
17182 for (i = 0; i < len; ++i)
17183 list_append(l, ptrs[i]);
17184 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017185 }
17186
17187 vim_free(ptrs);
17188 }
17189}
17190
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017191/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017192 * "soundfold({word})" function
17193 */
17194 static void
17195f_soundfold(argvars, rettv)
17196 typval_T *argvars;
17197 typval_T *rettv;
17198{
17199 char_u *s;
17200
17201 rettv->v_type = VAR_STRING;
17202 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017203#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017204 rettv->vval.v_string = eval_soundfold(s);
17205#else
17206 rettv->vval.v_string = vim_strsave(s);
17207#endif
17208}
17209
17210/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017211 * "spellbadword()" function
17212 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017213 static void
17214f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017215 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017216 typval_T *rettv;
17217{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017218 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017219 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017220 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017221
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017222 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017223 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017224
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017225#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017226 if (argvars[0].v_type == VAR_UNKNOWN)
17227 {
17228 /* Find the start and length of the badly spelled word. */
17229 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17230 if (len != 0)
17231 word = ml_get_cursor();
17232 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017233 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017234 {
17235 char_u *str = get_tv_string_chk(&argvars[0]);
17236 int capcol = -1;
17237
17238 if (str != NULL)
17239 {
17240 /* Check the argument for spelling. */
17241 while (*str != NUL)
17242 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017243 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017244 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017245 {
17246 word = str;
17247 break;
17248 }
17249 str += len;
17250 }
17251 }
17252 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017253#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017254
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017255 list_append_string(rettv->vval.v_list, word, len);
17256 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017257 attr == HLF_SPB ? "bad" :
17258 attr == HLF_SPR ? "rare" :
17259 attr == HLF_SPL ? "local" :
17260 attr == HLF_SPC ? "caps" :
17261 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017262}
17263
17264/*
17265 * "spellsuggest()" function
17266 */
17267 static void
17268f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017269 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017270 typval_T *rettv;
17271{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017272#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017273 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017274 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017275 int maxcount;
17276 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017277 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017278 listitem_T *li;
17279 int need_capital = FALSE;
17280#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017281
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017282 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017283 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017284
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017285#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017286 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017287 {
17288 str = get_tv_string(&argvars[0]);
17289 if (argvars[1].v_type != VAR_UNKNOWN)
17290 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017291 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017292 if (maxcount <= 0)
17293 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017294 if (argvars[2].v_type != VAR_UNKNOWN)
17295 {
17296 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17297 if (typeerr)
17298 return;
17299 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017300 }
17301 else
17302 maxcount = 25;
17303
Bram Moolenaar4770d092006-01-12 23:22:24 +000017304 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017305
17306 for (i = 0; i < ga.ga_len; ++i)
17307 {
17308 str = ((char_u **)ga.ga_data)[i];
17309
17310 li = listitem_alloc();
17311 if (li == NULL)
17312 vim_free(str);
17313 else
17314 {
17315 li->li_tv.v_type = VAR_STRING;
17316 li->li_tv.v_lock = 0;
17317 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017318 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017319 }
17320 }
17321 ga_clear(&ga);
17322 }
17323#endif
17324}
17325
Bram Moolenaar0d660222005-01-07 21:51:51 +000017326 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017327f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017328 typval_T *argvars;
17329 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017330{
17331 char_u *str;
17332 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017333 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017334 regmatch_T regmatch;
17335 char_u patbuf[NUMBUFLEN];
17336 char_u *save_cpo;
17337 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017338 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017339 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017340 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017341
17342 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17343 save_cpo = p_cpo;
17344 p_cpo = (char_u *)"";
17345
17346 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017347 if (argvars[1].v_type != VAR_UNKNOWN)
17348 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017349 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17350 if (pat == NULL)
17351 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017352 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017353 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017354 }
17355 if (pat == NULL || *pat == NUL)
17356 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017357
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017358 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017360 if (typeerr)
17361 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017362
Bram Moolenaar0d660222005-01-07 21:51:51 +000017363 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17364 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017365 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017366 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017367 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017368 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017369 if (*str == NUL)
17370 match = FALSE; /* empty item at the end */
17371 else
17372 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017373 if (match)
17374 end = regmatch.startp[0];
17375 else
17376 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017377 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17378 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017379 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017380 if (list_append_string(rettv->vval.v_list, str,
17381 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017382 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017383 }
17384 if (!match)
17385 break;
17386 /* Advance to just after the match. */
17387 if (regmatch.endp[0] > str)
17388 col = 0;
17389 else
17390 {
17391 /* Don't get stuck at the same match. */
17392#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017393 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017394#else
17395 col = 1;
17396#endif
17397 }
17398 str = regmatch.endp[0];
17399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017400
Bram Moolenaar473de612013-06-08 18:19:48 +020017401 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403
Bram Moolenaar0d660222005-01-07 21:51:51 +000017404 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405}
17406
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017407#ifdef FEAT_FLOAT
17408/*
17409 * "sqrt()" function
17410 */
17411 static void
17412f_sqrt(argvars, rettv)
17413 typval_T *argvars;
17414 typval_T *rettv;
17415{
17416 float_T f;
17417
17418 rettv->v_type = VAR_FLOAT;
17419 if (get_float_arg(argvars, &f) == OK)
17420 rettv->vval.v_float = sqrt(f);
17421 else
17422 rettv->vval.v_float = 0.0;
17423}
17424
17425/*
17426 * "str2float()" function
17427 */
17428 static void
17429f_str2float(argvars, rettv)
17430 typval_T *argvars;
17431 typval_T *rettv;
17432{
17433 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17434
17435 if (*p == '+')
17436 p = skipwhite(p + 1);
17437 (void)string2float(p, &rettv->vval.v_float);
17438 rettv->v_type = VAR_FLOAT;
17439}
17440#endif
17441
Bram Moolenaar2c932302006-03-18 21:42:09 +000017442/*
17443 * "str2nr()" function
17444 */
17445 static void
17446f_str2nr(argvars, rettv)
17447 typval_T *argvars;
17448 typval_T *rettv;
17449{
17450 int base = 10;
17451 char_u *p;
17452 long n;
17453
17454 if (argvars[1].v_type != VAR_UNKNOWN)
17455 {
17456 base = get_tv_number(&argvars[1]);
17457 if (base != 8 && base != 10 && base != 16)
17458 {
17459 EMSG(_(e_invarg));
17460 return;
17461 }
17462 }
17463
17464 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017465 if (*p == '+')
17466 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017467 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17468 rettv->vval.v_number = n;
17469}
17470
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471#ifdef HAVE_STRFTIME
17472/*
17473 * "strftime({format}[, {time}])" function
17474 */
17475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017476f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017477 typval_T *argvars;
17478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479{
17480 char_u result_buf[256];
17481 struct tm *curtime;
17482 time_t seconds;
17483 char_u *p;
17484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017485 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017487 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017488 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 seconds = time(NULL);
17490 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017491 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 curtime = localtime(&seconds);
17493 /* MSVC returns NULL for an invalid value of seconds. */
17494 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017495 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 else
17497 {
17498# ifdef FEAT_MBYTE
17499 vimconv_T conv;
17500 char_u *enc;
17501
17502 conv.vc_type = CONV_NONE;
17503 enc = enc_locale();
17504 convert_setup(&conv, p_enc, enc);
17505 if (conv.vc_type != CONV_NONE)
17506 p = string_convert(&conv, p, NULL);
17507# endif
17508 if (p != NULL)
17509 (void)strftime((char *)result_buf, sizeof(result_buf),
17510 (char *)p, curtime);
17511 else
17512 result_buf[0] = NUL;
17513
17514# ifdef FEAT_MBYTE
17515 if (conv.vc_type != CONV_NONE)
17516 vim_free(p);
17517 convert_setup(&conv, enc, p_enc);
17518 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017519 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017520 else
17521# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017522 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523
17524# ifdef FEAT_MBYTE
17525 /* Release conversion descriptors */
17526 convert_setup(&conv, NULL, NULL);
17527 vim_free(enc);
17528# endif
17529 }
17530}
17531#endif
17532
17533/*
17534 * "stridx()" function
17535 */
17536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017537f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017538 typval_T *argvars;
17539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017540{
17541 char_u buf[NUMBUFLEN];
17542 char_u *needle;
17543 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017544 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017546 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017548 needle = get_tv_string_chk(&argvars[1]);
17549 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017550 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017551 if (needle == NULL || haystack == NULL)
17552 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553
Bram Moolenaar33570922005-01-25 22:26:29 +000017554 if (argvars[2].v_type != VAR_UNKNOWN)
17555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017556 int error = FALSE;
17557
17558 start_idx = get_tv_number_chk(&argvars[2], &error);
17559 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017560 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017561 if (start_idx >= 0)
17562 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017563 }
17564
17565 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17566 if (pos != NULL)
17567 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568}
17569
17570/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017571 * "string()" function
17572 */
17573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017574f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017575 typval_T *argvars;
17576 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017577{
17578 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017579 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017581 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017582 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017583 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017584 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017585 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586}
17587
17588/*
17589 * "strlen()" function
17590 */
17591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017592f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017593 typval_T *argvars;
17594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017596 rettv->vval.v_number = (varnumber_T)(STRLEN(
17597 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598}
17599
17600/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017601 * "strchars()" function
17602 */
17603 static void
17604f_strchars(argvars, rettv)
17605 typval_T *argvars;
17606 typval_T *rettv;
17607{
17608 char_u *s = get_tv_string(&argvars[0]);
17609#ifdef FEAT_MBYTE
17610 varnumber_T len = 0;
17611
17612 while (*s != NUL)
17613 {
17614 mb_cptr2char_adv(&s);
17615 ++len;
17616 }
17617 rettv->vval.v_number = len;
17618#else
17619 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17620#endif
17621}
17622
17623/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017624 * "strdisplaywidth()" function
17625 */
17626 static void
17627f_strdisplaywidth(argvars, rettv)
17628 typval_T *argvars;
17629 typval_T *rettv;
17630{
17631 char_u *s = get_tv_string(&argvars[0]);
17632 int col = 0;
17633
17634 if (argvars[1].v_type != VAR_UNKNOWN)
17635 col = get_tv_number(&argvars[1]);
17636
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017637 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017638}
17639
17640/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017641 * "strwidth()" function
17642 */
17643 static void
17644f_strwidth(argvars, rettv)
17645 typval_T *argvars;
17646 typval_T *rettv;
17647{
17648 char_u *s = get_tv_string(&argvars[0]);
17649
17650 rettv->vval.v_number = (varnumber_T)(
17651#ifdef FEAT_MBYTE
17652 mb_string2cells(s, -1)
17653#else
17654 STRLEN(s)
17655#endif
17656 );
17657}
17658
17659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 * "strpart()" function
17661 */
17662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017663f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017664 typval_T *argvars;
17665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666{
17667 char_u *p;
17668 int n;
17669 int len;
17670 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017671 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017673 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017674 slen = (int)STRLEN(p);
17675
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017676 n = get_tv_number_chk(&argvars[1], &error);
17677 if (error)
17678 len = 0;
17679 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017680 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681 else
17682 len = slen - n; /* default len: all bytes that are available. */
17683
17684 /*
17685 * Only return the overlap between the specified part and the actual
17686 * string.
17687 */
17688 if (n < 0)
17689 {
17690 len += n;
17691 n = 0;
17692 }
17693 else if (n > slen)
17694 n = slen;
17695 if (len < 0)
17696 len = 0;
17697 else if (n + len > slen)
17698 len = slen - n;
17699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700 rettv->v_type = VAR_STRING;
17701 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702}
17703
17704/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017705 * "strridx()" function
17706 */
17707 static void
17708f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017709 typval_T *argvars;
17710 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017711{
17712 char_u buf[NUMBUFLEN];
17713 char_u *needle;
17714 char_u *haystack;
17715 char_u *rest;
17716 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017717 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017718
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017719 needle = get_tv_string_chk(&argvars[1]);
17720 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017721
17722 rettv->vval.v_number = -1;
17723 if (needle == NULL || haystack == NULL)
17724 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017725
17726 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017727 if (argvars[2].v_type != VAR_UNKNOWN)
17728 {
17729 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017730 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017731 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017732 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017733 }
17734 else
17735 end_idx = haystack_len;
17736
Bram Moolenaar0d660222005-01-07 21:51:51 +000017737 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017738 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017739 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017740 lastmatch = haystack + end_idx;
17741 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017743 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017744 for (rest = haystack; *rest != '\0'; ++rest)
17745 {
17746 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017747 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017748 break;
17749 lastmatch = rest;
17750 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017751 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017752
17753 if (lastmatch == NULL)
17754 rettv->vval.v_number = -1;
17755 else
17756 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17757}
17758
17759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 * "strtrans()" function
17761 */
17762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017763f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017764 typval_T *argvars;
17765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017767 rettv->v_type = VAR_STRING;
17768 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769}
17770
17771/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017772 * "submatch()" function
17773 */
17774 static void
17775f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017776 typval_T *argvars;
17777 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017778{
17779 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017780 rettv->vval.v_string =
17781 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017782}
17783
17784/*
17785 * "substitute()" function
17786 */
17787 static void
17788f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017789 typval_T *argvars;
17790 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017791{
17792 char_u patbuf[NUMBUFLEN];
17793 char_u subbuf[NUMBUFLEN];
17794 char_u flagsbuf[NUMBUFLEN];
17795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017796 char_u *str = get_tv_string_chk(&argvars[0]);
17797 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17798 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17799 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17800
Bram Moolenaar0d660222005-01-07 21:51:51 +000017801 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017802 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17803 rettv->vval.v_string = NULL;
17804 else
17805 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017806}
17807
17808/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017809 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017812f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017813 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815{
17816 int id = 0;
17817#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017818 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819 long col;
17820 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017821 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017823 lnum = get_tv_lnum(argvars); /* -1 on type error */
17824 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17825 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017827 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017828 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017829 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830#endif
17831
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017832 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833}
17834
17835/*
17836 * "synIDattr(id, what [, mode])" function
17837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017839f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017840 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842{
17843 char_u *p = NULL;
17844#ifdef FEAT_SYN_HL
17845 int id;
17846 char_u *what;
17847 char_u *mode;
17848 char_u modebuf[NUMBUFLEN];
17849 int modec;
17850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017851 id = get_tv_number(&argvars[0]);
17852 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017853 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017855 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017857 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 modec = 0; /* replace invalid with current */
17859 }
17860 else
17861 {
17862#ifdef FEAT_GUI
17863 if (gui.in_use)
17864 modec = 'g';
17865 else
17866#endif
17867 if (t_colors > 1)
17868 modec = 'c';
17869 else
17870 modec = 't';
17871 }
17872
17873
17874 switch (TOLOWER_ASC(what[0]))
17875 {
17876 case 'b':
17877 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17878 p = highlight_color(id, what, modec);
17879 else /* bold */
17880 p = highlight_has_attr(id, HL_BOLD, modec);
17881 break;
17882
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017883 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884 p = highlight_color(id, what, modec);
17885 break;
17886
17887 case 'i':
17888 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17889 p = highlight_has_attr(id, HL_INVERSE, modec);
17890 else /* italic */
17891 p = highlight_has_attr(id, HL_ITALIC, modec);
17892 break;
17893
17894 case 'n': /* name */
17895 p = get_highlight_name(NULL, id - 1);
17896 break;
17897
17898 case 'r': /* reverse */
17899 p = highlight_has_attr(id, HL_INVERSE, modec);
17900 break;
17901
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017902 case 's':
17903 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17904 p = highlight_color(id, what, modec);
17905 else /* standout */
17906 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 break;
17908
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017909 case 'u':
17910 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17911 /* underline */
17912 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17913 else
17914 /* undercurl */
17915 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 break;
17917 }
17918
17919 if (p != NULL)
17920 p = vim_strsave(p);
17921#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017922 rettv->v_type = VAR_STRING;
17923 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924}
17925
17926/*
17927 * "synIDtrans(id)" function
17928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017930f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017931 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933{
17934 int id;
17935
17936#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017937 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938
17939 if (id > 0)
17940 id = syn_get_final_id(id);
17941 else
17942#endif
17943 id = 0;
17944
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017945 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946}
17947
17948/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017949 * "synconcealed(lnum, col)" function
17950 */
17951 static void
17952f_synconcealed(argvars, rettv)
17953 typval_T *argvars UNUSED;
17954 typval_T *rettv;
17955{
17956#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17957 long lnum;
17958 long col;
17959 int syntax_flags = 0;
17960 int cchar;
17961 int matchid = 0;
17962 char_u str[NUMBUFLEN];
17963#endif
17964
17965 rettv->v_type = VAR_LIST;
17966 rettv->vval.v_list = NULL;
17967
17968#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17969 lnum = get_tv_lnum(argvars); /* -1 on type error */
17970 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17971
17972 vim_memset(str, NUL, sizeof(str));
17973
17974 if (rettv_list_alloc(rettv) != FAIL)
17975 {
17976 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17977 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17978 && curwin->w_p_cole > 0)
17979 {
17980 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17981 syntax_flags = get_syntax_info(&matchid);
17982
17983 /* get the conceal character */
17984 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17985 {
17986 cchar = syn_get_sub_char();
17987 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17988 cchar = lcs_conceal;
17989 if (cchar != NUL)
17990 {
17991# ifdef FEAT_MBYTE
17992 if (has_mbyte)
17993 (*mb_char2bytes)(cchar, str);
17994 else
17995# endif
17996 str[0] = cchar;
17997 }
17998 }
17999 }
18000
18001 list_append_number(rettv->vval.v_list,
18002 (syntax_flags & HL_CONCEAL) != 0);
18003 /* -1 to auto-determine strlen */
18004 list_append_string(rettv->vval.v_list, str, -1);
18005 list_append_number(rettv->vval.v_list, matchid);
18006 }
18007#endif
18008}
18009
18010/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018011 * "synstack(lnum, col)" function
18012 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018013 static void
18014f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018015 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018016 typval_T *rettv;
18017{
18018#ifdef FEAT_SYN_HL
18019 long lnum;
18020 long col;
18021 int i;
18022 int id;
18023#endif
18024
18025 rettv->v_type = VAR_LIST;
18026 rettv->vval.v_list = NULL;
18027
18028#ifdef FEAT_SYN_HL
18029 lnum = get_tv_lnum(argvars); /* -1 on type error */
18030 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18031
18032 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018033 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018034 && rettv_list_alloc(rettv) != FAIL)
18035 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018036 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018037 for (i = 0; ; ++i)
18038 {
18039 id = syn_get_stack_item(i);
18040 if (id < 0)
18041 break;
18042 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18043 break;
18044 }
18045 }
18046#endif
18047}
18048
18049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 * "system()" function
18051 */
18052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018053f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018054 typval_T *argvars;
18055 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018057 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018059 char_u *infile = NULL;
18060 char_u buf[NUMBUFLEN];
18061 int err = FALSE;
18062 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018064 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018065 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018066
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018067 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018068 {
18069 /*
18070 * Write the string to a temp file, to be used for input of the shell
18071 * command.
18072 */
18073 if ((infile = vim_tempname('i')) == NULL)
18074 {
18075 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018076 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018077 }
18078
18079 fd = mch_fopen((char *)infile, WRITEBIN);
18080 if (fd == NULL)
18081 {
18082 EMSG2(_(e_notopen), infile);
18083 goto done;
18084 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018085 p = get_tv_string_buf_chk(&argvars[1], buf);
18086 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018087 {
18088 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018089 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018090 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018091 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18092 err = TRUE;
18093 if (fclose(fd) != 0)
18094 err = TRUE;
18095 if (err)
18096 {
18097 EMSG(_("E677: Error writing temp file"));
18098 goto done;
18099 }
18100 }
18101
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018102 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18103 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018104
Bram Moolenaar071d4272004-06-13 20:20:40 +000018105#ifdef USE_CR
18106 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018107 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 {
18109 char_u *s;
18110
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018111 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 {
18113 if (*s == CAR)
18114 *s = NL;
18115 }
18116 }
18117#else
18118# ifdef USE_CRNL
18119 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018120 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 {
18122 char_u *s, *d;
18123
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018124 d = res;
18125 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126 {
18127 if (s[0] == CAR && s[1] == NL)
18128 ++s;
18129 *d++ = *s;
18130 }
18131 *d = NUL;
18132 }
18133# endif
18134#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018135
18136done:
18137 if (infile != NULL)
18138 {
18139 mch_remove(infile);
18140 vim_free(infile);
18141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018142 rettv->v_type = VAR_STRING;
18143 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144}
18145
18146/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018147 * "tabpagebuflist()" function
18148 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018149 static void
18150f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018151 typval_T *argvars UNUSED;
18152 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018153{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018154#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018155 tabpage_T *tp;
18156 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018157
18158 if (argvars[0].v_type == VAR_UNKNOWN)
18159 wp = firstwin;
18160 else
18161 {
18162 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18163 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018164 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018165 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018166 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018167 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018168 for (; wp != NULL; wp = wp->w_next)
18169 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018170 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018171 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018172 }
18173#endif
18174}
18175
18176
18177/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018178 * "tabpagenr()" function
18179 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018180 static void
18181f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018182 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018183 typval_T *rettv;
18184{
18185 int nr = 1;
18186#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018187 char_u *arg;
18188
18189 if (argvars[0].v_type != VAR_UNKNOWN)
18190 {
18191 arg = get_tv_string_chk(&argvars[0]);
18192 nr = 0;
18193 if (arg != NULL)
18194 {
18195 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018196 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018197 else
18198 EMSG2(_(e_invexpr2), arg);
18199 }
18200 }
18201 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018202 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018203#endif
18204 rettv->vval.v_number = nr;
18205}
18206
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018207
18208#ifdef FEAT_WINDOWS
18209static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18210
18211/*
18212 * Common code for tabpagewinnr() and winnr().
18213 */
18214 static int
18215get_winnr(tp, argvar)
18216 tabpage_T *tp;
18217 typval_T *argvar;
18218{
18219 win_T *twin;
18220 int nr = 1;
18221 win_T *wp;
18222 char_u *arg;
18223
18224 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18225 if (argvar->v_type != VAR_UNKNOWN)
18226 {
18227 arg = get_tv_string_chk(argvar);
18228 if (arg == NULL)
18229 nr = 0; /* type error; errmsg already given */
18230 else if (STRCMP(arg, "$") == 0)
18231 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18232 else if (STRCMP(arg, "#") == 0)
18233 {
18234 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18235 if (twin == NULL)
18236 nr = 0;
18237 }
18238 else
18239 {
18240 EMSG2(_(e_invexpr2), arg);
18241 nr = 0;
18242 }
18243 }
18244
18245 if (nr > 0)
18246 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18247 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018248 {
18249 if (wp == NULL)
18250 {
18251 /* didn't find it in this tabpage */
18252 nr = 0;
18253 break;
18254 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018255 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018256 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018257 return nr;
18258}
18259#endif
18260
18261/*
18262 * "tabpagewinnr()" function
18263 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018264 static void
18265f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018266 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018267 typval_T *rettv;
18268{
18269 int nr = 1;
18270#ifdef FEAT_WINDOWS
18271 tabpage_T *tp;
18272
18273 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18274 if (tp == NULL)
18275 nr = 0;
18276 else
18277 nr = get_winnr(tp, &argvars[1]);
18278#endif
18279 rettv->vval.v_number = nr;
18280}
18281
18282
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018283/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018284 * "tagfiles()" function
18285 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018286 static void
18287f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018288 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018289 typval_T *rettv;
18290{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018291 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018292 tagname_T tn;
18293 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018294
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018295 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018296 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018297 fname = alloc(MAXPATHL);
18298 if (fname == NULL)
18299 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018300
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018301 for (first = TRUE; ; first = FALSE)
18302 if (get_tagfname(&tn, first, fname) == FAIL
18303 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018304 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018305 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018306 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018307}
18308
18309/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018310 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018311 */
18312 static void
18313f_taglist(argvars, rettv)
18314 typval_T *argvars;
18315 typval_T *rettv;
18316{
18317 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018318
18319 tag_pattern = get_tv_string(&argvars[0]);
18320
18321 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018322 if (*tag_pattern == NUL)
18323 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018324
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018325 if (rettv_list_alloc(rettv) == OK)
18326 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018327}
18328
18329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018330 * "tempname()" function
18331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018333f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018334 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336{
18337 static int x = 'A';
18338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018339 rettv->v_type = VAR_STRING;
18340 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341
18342 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18343 * names. Skip 'I' and 'O', they are used for shell redirection. */
18344 do
18345 {
18346 if (x == 'Z')
18347 x = '0';
18348 else if (x == '9')
18349 x = 'A';
18350 else
18351 {
18352#ifdef EBCDIC
18353 if (x == 'I')
18354 x = 'J';
18355 else if (x == 'R')
18356 x = 'S';
18357 else
18358#endif
18359 ++x;
18360 }
18361 } while (x == 'I' || x == 'O');
18362}
18363
18364/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018365 * "test(list)" function: Just checking the walls...
18366 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018367 static void
18368f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018369 typval_T *argvars UNUSED;
18370 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018371{
18372 /* Used for unit testing. Change the code below to your liking. */
18373#if 0
18374 listitem_T *li;
18375 list_T *l;
18376 char_u *bad, *good;
18377
18378 if (argvars[0].v_type != VAR_LIST)
18379 return;
18380 l = argvars[0].vval.v_list;
18381 if (l == NULL)
18382 return;
18383 li = l->lv_first;
18384 if (li == NULL)
18385 return;
18386 bad = get_tv_string(&li->li_tv);
18387 li = li->li_next;
18388 if (li == NULL)
18389 return;
18390 good = get_tv_string(&li->li_tv);
18391 rettv->vval.v_number = test_edit_score(bad, good);
18392#endif
18393}
18394
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018395#ifdef FEAT_FLOAT
18396/*
18397 * "tan()" function
18398 */
18399 static void
18400f_tan(argvars, rettv)
18401 typval_T *argvars;
18402 typval_T *rettv;
18403{
18404 float_T f;
18405
18406 rettv->v_type = VAR_FLOAT;
18407 if (get_float_arg(argvars, &f) == OK)
18408 rettv->vval.v_float = tan(f);
18409 else
18410 rettv->vval.v_float = 0.0;
18411}
18412
18413/*
18414 * "tanh()" function
18415 */
18416 static void
18417f_tanh(argvars, rettv)
18418 typval_T *argvars;
18419 typval_T *rettv;
18420{
18421 float_T f;
18422
18423 rettv->v_type = VAR_FLOAT;
18424 if (get_float_arg(argvars, &f) == OK)
18425 rettv->vval.v_float = tanh(f);
18426 else
18427 rettv->vval.v_float = 0.0;
18428}
18429#endif
18430
Bram Moolenaard52d9742005-08-21 22:20:28 +000018431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432 * "tolower(string)" function
18433 */
18434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018435f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018436 typval_T *argvars;
18437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438{
18439 char_u *p;
18440
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018441 p = vim_strsave(get_tv_string(&argvars[0]));
18442 rettv->v_type = VAR_STRING;
18443 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444
18445 if (p != NULL)
18446 while (*p != NUL)
18447 {
18448#ifdef FEAT_MBYTE
18449 int l;
18450
18451 if (enc_utf8)
18452 {
18453 int c, lc;
18454
18455 c = utf_ptr2char(p);
18456 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018457 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 /* TODO: reallocate string when byte count changes. */
18459 if (utf_char2len(lc) == l)
18460 utf_char2bytes(lc, p);
18461 p += l;
18462 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018463 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 p += l; /* skip multi-byte character */
18465 else
18466#endif
18467 {
18468 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18469 ++p;
18470 }
18471 }
18472}
18473
18474/*
18475 * "toupper(string)" function
18476 */
18477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018478f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018479 typval_T *argvars;
18480 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018481{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018482 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018483 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484}
18485
18486/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018487 * "tr(string, fromstr, tostr)" function
18488 */
18489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018490f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018491 typval_T *argvars;
18492 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018493{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018494 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018495 char_u *fromstr;
18496 char_u *tostr;
18497 char_u *p;
18498#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018499 int inlen;
18500 int fromlen;
18501 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018502 int idx;
18503 char_u *cpstr;
18504 int cplen;
18505 int first = TRUE;
18506#endif
18507 char_u buf[NUMBUFLEN];
18508 char_u buf2[NUMBUFLEN];
18509 garray_T ga;
18510
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018511 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018512 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18513 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018514
18515 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018516 rettv->v_type = VAR_STRING;
18517 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018518 if (fromstr == NULL || tostr == NULL)
18519 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018520 ga_init2(&ga, (int)sizeof(char), 80);
18521
18522#ifdef FEAT_MBYTE
18523 if (!has_mbyte)
18524#endif
18525 /* not multi-byte: fromstr and tostr must be the same length */
18526 if (STRLEN(fromstr) != STRLEN(tostr))
18527 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018528#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018529error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018530#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018531 EMSG2(_(e_invarg2), fromstr);
18532 ga_clear(&ga);
18533 return;
18534 }
18535
18536 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018537 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018538 {
18539#ifdef FEAT_MBYTE
18540 if (has_mbyte)
18541 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018542 inlen = (*mb_ptr2len)(in_str);
18543 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018544 cplen = inlen;
18545 idx = 0;
18546 for (p = fromstr; *p != NUL; p += fromlen)
18547 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018548 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018549 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018550 {
18551 for (p = tostr; *p != NUL; p += tolen)
18552 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018553 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018554 if (idx-- == 0)
18555 {
18556 cplen = tolen;
18557 cpstr = p;
18558 break;
18559 }
18560 }
18561 if (*p == NUL) /* tostr is shorter than fromstr */
18562 goto error;
18563 break;
18564 }
18565 ++idx;
18566 }
18567
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018568 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018569 {
18570 /* Check that fromstr and tostr have the same number of
18571 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018572 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018573 first = FALSE;
18574 for (p = tostr; *p != NUL; p += tolen)
18575 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018576 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018577 --idx;
18578 }
18579 if (idx != 0)
18580 goto error;
18581 }
18582
18583 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018584 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018585 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018586
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018587 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018588 }
18589 else
18590#endif
18591 {
18592 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018593 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018594 if (p != NULL)
18595 ga_append(&ga, tostr[p - fromstr]);
18596 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018597 ga_append(&ga, *in_str);
18598 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018599 }
18600 }
18601
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018602 /* add a terminating NUL */
18603 ga_grow(&ga, 1);
18604 ga_append(&ga, NUL);
18605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018606 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018607}
18608
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018609#ifdef FEAT_FLOAT
18610/*
18611 * "trunc({float})" function
18612 */
18613 static void
18614f_trunc(argvars, rettv)
18615 typval_T *argvars;
18616 typval_T *rettv;
18617{
18618 float_T f;
18619
18620 rettv->v_type = VAR_FLOAT;
18621 if (get_float_arg(argvars, &f) == OK)
18622 /* trunc() is not in C90, use floor() or ceil() instead. */
18623 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18624 else
18625 rettv->vval.v_float = 0.0;
18626}
18627#endif
18628
Bram Moolenaar8299df92004-07-10 09:47:34 +000018629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 * "type(expr)" function
18631 */
18632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018633f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018634 typval_T *argvars;
18635 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018637 int n;
18638
18639 switch (argvars[0].v_type)
18640 {
18641 case VAR_NUMBER: n = 0; break;
18642 case VAR_STRING: n = 1; break;
18643 case VAR_FUNC: n = 2; break;
18644 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018645 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018646#ifdef FEAT_FLOAT
18647 case VAR_FLOAT: n = 5; break;
18648#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018649 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18650 }
18651 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652}
18653
18654/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018655 * "undofile(name)" function
18656 */
18657 static void
18658f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018659 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018660 typval_T *rettv;
18661{
18662 rettv->v_type = VAR_STRING;
18663#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018664 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018665 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018666
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018667 if (*fname == NUL)
18668 {
18669 /* If there is no file name there will be no undo file. */
18670 rettv->vval.v_string = NULL;
18671 }
18672 else
18673 {
18674 char_u *ffname = FullName_save(fname, FALSE);
18675
18676 if (ffname != NULL)
18677 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18678 vim_free(ffname);
18679 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018680 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018681#else
18682 rettv->vval.v_string = NULL;
18683#endif
18684}
18685
18686/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018687 * "undotree()" function
18688 */
18689 static void
18690f_undotree(argvars, rettv)
18691 typval_T *argvars UNUSED;
18692 typval_T *rettv;
18693{
18694 if (rettv_dict_alloc(rettv) == OK)
18695 {
18696 dict_T *dict = rettv->vval.v_dict;
18697 list_T *list;
18698
Bram Moolenaar730cde92010-06-27 05:18:54 +020018699 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018700 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018701 dict_add_nr_str(dict, "save_last",
18702 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018703 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18704 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018705 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018706
18707 list = list_alloc();
18708 if (list != NULL)
18709 {
18710 u_eval_tree(curbuf->b_u_oldhead, list);
18711 dict_add_list(dict, "entries", list);
18712 }
18713 }
18714}
18715
18716/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018717 * "values(dict)" function
18718 */
18719 static void
18720f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018721 typval_T *argvars;
18722 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018723{
18724 dict_list(argvars, rettv, 1);
18725}
18726
18727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 * "virtcol(string)" function
18729 */
18730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018732 typval_T *argvars;
18733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734{
18735 colnr_T vcol = 0;
18736 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018737 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018739 fp = var2fpos(&argvars[0], FALSE, &fnum);
18740 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18741 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742 {
18743 getvvcol(curwin, fp, NULL, NULL, &vcol);
18744 ++vcol;
18745 }
18746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018747 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748}
18749
18750/*
18751 * "visualmode()" function
18752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018754f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018755 typval_T *argvars UNUSED;
18756 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757{
18758#ifdef FEAT_VISUAL
18759 char_u str[2];
18760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018761 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 str[0] = curbuf->b_visual_mode_eval;
18763 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018764 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765
18766 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018767 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769#endif
18770}
18771
18772/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018773 * "wildmenumode()" function
18774 */
18775 static void
18776f_wildmenumode(argvars, rettv)
18777 typval_T *argvars UNUSED;
18778 typval_T *rettv UNUSED;
18779{
18780#ifdef FEAT_WILDMENU
18781 if (wild_menu_showing)
18782 rettv->vval.v_number = 1;
18783#endif
18784}
18785
18786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 * "winbufnr(nr)" function
18788 */
18789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018791 typval_T *argvars;
18792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793{
18794 win_T *wp;
18795
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018796 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018798 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018800 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801}
18802
18803/*
18804 * "wincol()" function
18805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018807f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018808 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810{
18811 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018812 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813}
18814
18815/*
18816 * "winheight(nr)" function
18817 */
18818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018819f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018820 typval_T *argvars;
18821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822{
18823 win_T *wp;
18824
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018825 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018829 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830}
18831
18832/*
18833 * "winline()" function
18834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018836f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018837 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839{
18840 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018841 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842}
18843
18844/*
18845 * "winnr()" function
18846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018848f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018849 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851{
18852 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018853
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018855 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018857 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858}
18859
18860/*
18861 * "winrestcmd()" function
18862 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018864f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018865 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018867{
18868#ifdef FEAT_WINDOWS
18869 win_T *wp;
18870 int winnr = 1;
18871 garray_T ga;
18872 char_u buf[50];
18873
18874 ga_init2(&ga, (int)sizeof(char), 70);
18875 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18876 {
18877 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18878 ga_concat(&ga, buf);
18879# ifdef FEAT_VERTSPLIT
18880 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18881 ga_concat(&ga, buf);
18882# endif
18883 ++winnr;
18884 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018885 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018887 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018891 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892}
18893
18894/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018895 * "winrestview()" function
18896 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018897 static void
18898f_winrestview(argvars, rettv)
18899 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018900 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018901{
18902 dict_T *dict;
18903
18904 if (argvars[0].v_type != VAR_DICT
18905 || (dict = argvars[0].vval.v_dict) == NULL)
18906 EMSG(_(e_invarg));
18907 else
18908 {
18909 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18910 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18911#ifdef FEAT_VIRTUALEDIT
18912 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18913#endif
18914 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018915 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018916
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018917 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018918#ifdef FEAT_DIFF
18919 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18920#endif
18921 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18922 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18923
18924 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018925 win_new_height(curwin, curwin->w_height);
18926# ifdef FEAT_VERTSPLIT
18927 win_new_width(curwin, W_WIDTH(curwin));
18928# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018929 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018930
18931 if (curwin->w_topline == 0)
18932 curwin->w_topline = 1;
18933 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18934 curwin->w_topline = curbuf->b_ml.ml_line_count;
18935#ifdef FEAT_DIFF
18936 check_topfill(curwin, TRUE);
18937#endif
18938 }
18939}
18940
18941/*
18942 * "winsaveview()" function
18943 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018944 static void
18945f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018946 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018947 typval_T *rettv;
18948{
18949 dict_T *dict;
18950
Bram Moolenaara800b422010-06-27 01:15:55 +020018951 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018952 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018953 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018954
18955 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18956 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18957#ifdef FEAT_VIRTUALEDIT
18958 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18959#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018960 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018961 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18962
18963 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18964#ifdef FEAT_DIFF
18965 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18966#endif
18967 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18968 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18969}
18970
18971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972 * "winwidth(nr)" function
18973 */
18974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018975f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018976 typval_T *argvars;
18977 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978{
18979 win_T *wp;
18980
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018981 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018983 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 else
18985#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018986 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018988 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018989#endif
18990}
18991
Bram Moolenaar071d4272004-06-13 20:20:40 +000018992/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018993 * "writefile()" function
18994 */
18995 static void
18996f_writefile(argvars, rettv)
18997 typval_T *argvars;
18998 typval_T *rettv;
18999{
19000 int binary = FALSE;
19001 char_u *fname;
19002 FILE *fd;
19003 listitem_T *li;
19004 char_u *s;
19005 int ret = 0;
19006 int c;
19007
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019008 if (check_restricted() || check_secure())
19009 return;
19010
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019011 if (argvars[0].v_type != VAR_LIST)
19012 {
19013 EMSG2(_(e_listarg), "writefile()");
19014 return;
19015 }
19016 if (argvars[0].vval.v_list == NULL)
19017 return;
19018
19019 if (argvars[2].v_type != VAR_UNKNOWN
19020 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19021 binary = TRUE;
19022
19023 /* Always open the file in binary mode, library functions have a mind of
19024 * their own about CR-LF conversion. */
19025 fname = get_tv_string(&argvars[1]);
19026 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19027 {
19028 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19029 ret = -1;
19030 }
19031 else
19032 {
19033 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19034 li = li->li_next)
19035 {
19036 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19037 {
19038 if (*s == '\n')
19039 c = putc(NUL, fd);
19040 else
19041 c = putc(*s, fd);
19042 if (c == EOF)
19043 {
19044 ret = -1;
19045 break;
19046 }
19047 }
19048 if (!binary || li->li_next != NULL)
19049 if (putc('\n', fd) == EOF)
19050 {
19051 ret = -1;
19052 break;
19053 }
19054 if (ret < 0)
19055 {
19056 EMSG(_(e_write));
19057 break;
19058 }
19059 }
19060 fclose(fd);
19061 }
19062
19063 rettv->vval.v_number = ret;
19064}
19065
19066/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019067 * "xor(expr, expr)" function
19068 */
19069 static void
19070f_xor(argvars, rettv)
19071 typval_T *argvars;
19072 typval_T *rettv;
19073{
19074 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19075 ^ get_tv_number_chk(&argvars[1], NULL);
19076}
19077
19078
19079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019080 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019081 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019082 */
19083 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019084var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019085 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019086 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019087 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019089 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019090 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019091 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092
Bram Moolenaara5525202006-03-02 22:52:09 +000019093 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019094 if (varp->v_type == VAR_LIST)
19095 {
19096 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019097 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019098 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019099 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019100
19101 l = varp->vval.v_list;
19102 if (l == NULL)
19103 return NULL;
19104
19105 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019106 pos.lnum = list_find_nr(l, 0L, &error);
19107 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019108 return NULL; /* invalid line number */
19109
19110 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019111 pos.col = list_find_nr(l, 1L, &error);
19112 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019113 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019114 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019115
19116 /* We accept "$" for the column number: last column. */
19117 li = list_find(l, 1L);
19118 if (li != NULL && li->li_tv.v_type == VAR_STRING
19119 && li->li_tv.vval.v_string != NULL
19120 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19121 pos.col = len + 1;
19122
Bram Moolenaara5525202006-03-02 22:52:09 +000019123 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019124 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019125 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019126 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019127
Bram Moolenaara5525202006-03-02 22:52:09 +000019128#ifdef FEAT_VIRTUALEDIT
19129 /* Get the virtual offset. Defaults to zero. */
19130 pos.coladd = list_find_nr(l, 2L, &error);
19131 if (error)
19132 pos.coladd = 0;
19133#endif
19134
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019135 return &pos;
19136 }
19137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019138 name = get_tv_string_chk(varp);
19139 if (name == NULL)
19140 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019141 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019143#ifdef FEAT_VISUAL
19144 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19145 {
19146 if (VIsual_active)
19147 return &VIsual;
19148 return &curwin->w_cursor;
19149 }
19150#endif
19151 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019153 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019154 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19155 return NULL;
19156 return pp;
19157 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019158
19159#ifdef FEAT_VIRTUALEDIT
19160 pos.coladd = 0;
19161#endif
19162
Bram Moolenaar477933c2007-07-17 14:32:23 +000019163 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019164 {
19165 pos.col = 0;
19166 if (name[1] == '0') /* "w0": first visible line */
19167 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019168 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019169 pos.lnum = curwin->w_topline;
19170 return &pos;
19171 }
19172 else if (name[1] == '$') /* "w$": last visible line */
19173 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019174 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019175 pos.lnum = curwin->w_botline - 1;
19176 return &pos;
19177 }
19178 }
19179 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019181 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019182 {
19183 pos.lnum = curbuf->b_ml.ml_line_count;
19184 pos.col = 0;
19185 }
19186 else
19187 {
19188 pos.lnum = curwin->w_cursor.lnum;
19189 pos.col = (colnr_T)STRLEN(ml_get_curline());
19190 }
19191 return &pos;
19192 }
19193 return NULL;
19194}
19195
19196/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019197 * Convert list in "arg" into a position and optional file number.
19198 * When "fnump" is NULL there is no file number, only 3 items.
19199 * Note that the column is passed on as-is, the caller may want to decrement
19200 * it to use 1 for the first column.
19201 * Return FAIL when conversion is not possible, doesn't check the position for
19202 * validity.
19203 */
19204 static int
19205list2fpos(arg, posp, fnump)
19206 typval_T *arg;
19207 pos_T *posp;
19208 int *fnump;
19209{
19210 list_T *l = arg->vval.v_list;
19211 long i = 0;
19212 long n;
19213
Bram Moolenaarbde35262006-07-23 20:12:24 +000019214 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19215 * when "fnump" isn't NULL and "coladd" is optional. */
19216 if (arg->v_type != VAR_LIST
19217 || l == NULL
19218 || l->lv_len < (fnump == NULL ? 2 : 3)
19219 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019220 return FAIL;
19221
19222 if (fnump != NULL)
19223 {
19224 n = list_find_nr(l, i++, NULL); /* fnum */
19225 if (n < 0)
19226 return FAIL;
19227 if (n == 0)
19228 n = curbuf->b_fnum; /* current buffer */
19229 *fnump = n;
19230 }
19231
19232 n = list_find_nr(l, i++, NULL); /* lnum */
19233 if (n < 0)
19234 return FAIL;
19235 posp->lnum = n;
19236
19237 n = list_find_nr(l, i++, NULL); /* col */
19238 if (n < 0)
19239 return FAIL;
19240 posp->col = n;
19241
19242#ifdef FEAT_VIRTUALEDIT
19243 n = list_find_nr(l, i, NULL);
19244 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019245 posp->coladd = 0;
19246 else
19247 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019248#endif
19249
19250 return OK;
19251}
19252
19253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019254 * Get the length of an environment variable name.
19255 * Advance "arg" to the first character after the name.
19256 * Return 0 for error.
19257 */
19258 static int
19259get_env_len(arg)
19260 char_u **arg;
19261{
19262 char_u *p;
19263 int len;
19264
19265 for (p = *arg; vim_isIDc(*p); ++p)
19266 ;
19267 if (p == *arg) /* no name found */
19268 return 0;
19269
19270 len = (int)(p - *arg);
19271 *arg = p;
19272 return len;
19273}
19274
19275/*
19276 * Get the length of the name of a function or internal variable.
19277 * "arg" is advanced to the first non-white character after the name.
19278 * Return 0 if something is wrong.
19279 */
19280 static int
19281get_id_len(arg)
19282 char_u **arg;
19283{
19284 char_u *p;
19285 int len;
19286
19287 /* Find the end of the name. */
19288 for (p = *arg; eval_isnamec(*p); ++p)
19289 ;
19290 if (p == *arg) /* no name found */
19291 return 0;
19292
19293 len = (int)(p - *arg);
19294 *arg = skipwhite(p);
19295
19296 return len;
19297}
19298
19299/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019300 * Get the length of the name of a variable or function.
19301 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019303 * Return -1 if curly braces expansion failed.
19304 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 * If the name contains 'magic' {}'s, expand them and return the
19306 * expanded name in an allocated string via 'alias' - caller must free.
19307 */
19308 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019309get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 char_u **arg;
19311 char_u **alias;
19312 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019313 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314{
19315 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 char_u *p;
19317 char_u *expr_start;
19318 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019319
19320 *alias = NULL; /* default to no alias */
19321
19322 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19323 && (*arg)[2] == (int)KE_SNR)
19324 {
19325 /* hard coded <SNR>, already translated */
19326 *arg += 3;
19327 return get_id_len(arg) + 3;
19328 }
19329 len = eval_fname_script(*arg);
19330 if (len > 0)
19331 {
19332 /* literal "<SID>", "s:" or "<SNR>" */
19333 *arg += len;
19334 }
19335
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019337 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019339 p = find_name_end(*arg, &expr_start, &expr_end,
19340 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 if (expr_start != NULL)
19342 {
19343 char_u *temp_string;
19344
19345 if (!evaluate)
19346 {
19347 len += (int)(p - *arg);
19348 *arg = skipwhite(p);
19349 return len;
19350 }
19351
19352 /*
19353 * Include any <SID> etc in the expanded string:
19354 * Thus the -len here.
19355 */
19356 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19357 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019358 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 *alias = temp_string;
19360 *arg = skipwhite(p);
19361 return (int)STRLEN(temp_string);
19362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363
19364 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019365 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 EMSG2(_(e_invexpr2), *arg);
19367
19368 return len;
19369}
19370
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019371/*
19372 * Find the end of a variable or function name, taking care of magic braces.
19373 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19374 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019375 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019376 * Return a pointer to just after the name. Equal to "arg" if there is no
19377 * valid name.
19378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019380find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 char_u *arg;
19382 char_u **expr_start;
19383 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019384 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019386 int mb_nest = 0;
19387 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 char_u *p;
19389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019390 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019392 *expr_start = NULL;
19393 *expr_end = NULL;
19394 }
19395
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019396 /* Quick check for valid starting character. */
19397 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19398 return arg;
19399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019400 for (p = arg; *p != NUL
19401 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019402 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019403 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019404 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019405 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019406 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019407 if (*p == '\'')
19408 {
19409 /* skip over 'string' to avoid counting [ and ] inside it. */
19410 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19411 ;
19412 if (*p == NUL)
19413 break;
19414 }
19415 else if (*p == '"')
19416 {
19417 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19418 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19419 if (*p == '\\' && p[1] != NUL)
19420 ++p;
19421 if (*p == NUL)
19422 break;
19423 }
19424
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019425 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019427 if (*p == '[')
19428 ++br_nest;
19429 else if (*p == ']')
19430 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019433 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019435 if (*p == '{')
19436 {
19437 mb_nest++;
19438 if (expr_start != NULL && *expr_start == NULL)
19439 *expr_start = p;
19440 }
19441 else if (*p == '}')
19442 {
19443 mb_nest--;
19444 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19445 *expr_end = p;
19446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 }
19449
19450 return p;
19451}
19452
19453/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019454 * Expands out the 'magic' {}'s in a variable/function name.
19455 * Note that this can call itself recursively, to deal with
19456 * constructs like foo{bar}{baz}{bam}
19457 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19458 * "in_start" ^
19459 * "expr_start" ^
19460 * "expr_end" ^
19461 * "in_end" ^
19462 *
19463 * Returns a new allocated string, which the caller must free.
19464 * Returns NULL for failure.
19465 */
19466 static char_u *
19467make_expanded_name(in_start, expr_start, expr_end, in_end)
19468 char_u *in_start;
19469 char_u *expr_start;
19470 char_u *expr_end;
19471 char_u *in_end;
19472{
19473 char_u c1;
19474 char_u *retval = NULL;
19475 char_u *temp_result;
19476 char_u *nextcmd = NULL;
19477
19478 if (expr_end == NULL || in_end == NULL)
19479 return NULL;
19480 *expr_start = NUL;
19481 *expr_end = NUL;
19482 c1 = *in_end;
19483 *in_end = NUL;
19484
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019485 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019486 if (temp_result != NULL && nextcmd == NULL)
19487 {
19488 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19489 + (in_end - expr_end) + 1));
19490 if (retval != NULL)
19491 {
19492 STRCPY(retval, in_start);
19493 STRCAT(retval, temp_result);
19494 STRCAT(retval, expr_end + 1);
19495 }
19496 }
19497 vim_free(temp_result);
19498
19499 *in_end = c1; /* put char back for error messages */
19500 *expr_start = '{';
19501 *expr_end = '}';
19502
19503 if (retval != NULL)
19504 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019505 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019506 if (expr_start != NULL)
19507 {
19508 /* Further expansion! */
19509 temp_result = make_expanded_name(retval, expr_start,
19510 expr_end, temp_result);
19511 vim_free(retval);
19512 retval = temp_result;
19513 }
19514 }
19515
19516 return retval;
19517}
19518
19519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019521 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 */
19523 static int
19524eval_isnamec(c)
19525 int c;
19526{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019527 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19528}
19529
19530/*
19531 * Return TRUE if character "c" can be used as the first character in a
19532 * variable or function name (excluding '{' and '}').
19533 */
19534 static int
19535eval_isnamec1(c)
19536 int c;
19537{
19538 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539}
19540
19541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 * Set number v: variable to "val".
19543 */
19544 void
19545set_vim_var_nr(idx, val)
19546 int idx;
19547 long val;
19548{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019549 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550}
19551
19552/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019553 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 */
19555 long
19556get_vim_var_nr(idx)
19557 int idx;
19558{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019559 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560}
19561
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019562/*
19563 * Get string v: variable value. Uses a static buffer, can only be used once.
19564 */
19565 char_u *
19566get_vim_var_str(idx)
19567 int idx;
19568{
19569 return get_tv_string(&vimvars[idx].vv_tv);
19570}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019571
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019573 * Get List v: variable value. Caller must take care of reference count when
19574 * needed.
19575 */
19576 list_T *
19577get_vim_var_list(idx)
19578 int idx;
19579{
19580 return vimvars[idx].vv_list;
19581}
19582
19583/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019584 * Set v:char to character "c".
19585 */
19586 void
19587set_vim_var_char(c)
19588 int c;
19589{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019590 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019591
19592#ifdef FEAT_MBYTE
19593 if (has_mbyte)
19594 buf[(*mb_char2bytes)(c, buf)] = NUL;
19595 else
19596#endif
19597 {
19598 buf[0] = c;
19599 buf[1] = NUL;
19600 }
19601 set_vim_var_string(VV_CHAR, buf, -1);
19602}
19603
19604/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019605 * Set v:count to "count" and v:count1 to "count1".
19606 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 */
19608 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019609set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 long count;
19611 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019612 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019614 if (set_prevcount)
19615 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019616 vimvars[VV_COUNT].vv_nr = count;
19617 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618}
19619
19620/*
19621 * Set string v: variable to a copy of "val".
19622 */
19623 void
19624set_vim_var_string(idx, val, len)
19625 int idx;
19626 char_u *val;
19627 int len; /* length of "val" to use or -1 (whole string) */
19628{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019629 /* Need to do this (at least) once, since we can't initialize a union.
19630 * Will always be invoked when "v:progname" is set. */
19631 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19632
Bram Moolenaare9a41262005-01-15 22:18:47 +000019633 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019635 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019637 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019639 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640}
19641
19642/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019643 * Set List v: variable to "val".
19644 */
19645 void
19646set_vim_var_list(idx, val)
19647 int idx;
19648 list_T *val;
19649{
19650 list_unref(vimvars[idx].vv_list);
19651 vimvars[idx].vv_list = val;
19652 if (val != NULL)
19653 ++val->lv_refcount;
19654}
19655
19656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657 * Set v:register if needed.
19658 */
19659 void
19660set_reg_var(c)
19661 int c;
19662{
19663 char_u regname;
19664
19665 if (c == 0 || c == ' ')
19666 regname = '"';
19667 else
19668 regname = c;
19669 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019670 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671 set_vim_var_string(VV_REG, &regname, 1);
19672}
19673
19674/*
19675 * Get or set v:exception. If "oldval" == NULL, return the current value.
19676 * Otherwise, restore the value to "oldval" and return NULL.
19677 * Must always be called in pairs to save and restore v:exception! Does not
19678 * take care of memory allocations.
19679 */
19680 char_u *
19681v_exception(oldval)
19682 char_u *oldval;
19683{
19684 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019685 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686
Bram Moolenaare9a41262005-01-15 22:18:47 +000019687 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688 return NULL;
19689}
19690
19691/*
19692 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19693 * Otherwise, restore the value to "oldval" and return NULL.
19694 * Must always be called in pairs to save and restore v:throwpoint! Does not
19695 * take care of memory allocations.
19696 */
19697 char_u *
19698v_throwpoint(oldval)
19699 char_u *oldval;
19700{
19701 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019702 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703
Bram Moolenaare9a41262005-01-15 22:18:47 +000019704 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 return NULL;
19706}
19707
19708#if defined(FEAT_AUTOCMD) || defined(PROTO)
19709/*
19710 * Set v:cmdarg.
19711 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19712 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19713 * Must always be called in pairs!
19714 */
19715 char_u *
19716set_cmdarg(eap, oldarg)
19717 exarg_T *eap;
19718 char_u *oldarg;
19719{
19720 char_u *oldval;
19721 char_u *newval;
19722 unsigned len;
19723
Bram Moolenaare9a41262005-01-15 22:18:47 +000019724 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019725 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019727 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019728 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019729 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 }
19731
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019732 if (eap->force_bin == FORCE_BIN)
19733 len = 6;
19734 else if (eap->force_bin == FORCE_NOBIN)
19735 len = 8;
19736 else
19737 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019738
19739 if (eap->read_edit)
19740 len += 7;
19741
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019742 if (eap->force_ff != 0)
19743 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19744# ifdef FEAT_MBYTE
19745 if (eap->force_enc != 0)
19746 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019747 if (eap->bad_char != 0)
19748 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019749# endif
19750
19751 newval = alloc(len + 1);
19752 if (newval == NULL)
19753 return NULL;
19754
19755 if (eap->force_bin == FORCE_BIN)
19756 sprintf((char *)newval, " ++bin");
19757 else if (eap->force_bin == FORCE_NOBIN)
19758 sprintf((char *)newval, " ++nobin");
19759 else
19760 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019761
19762 if (eap->read_edit)
19763 STRCAT(newval, " ++edit");
19764
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019765 if (eap->force_ff != 0)
19766 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19767 eap->cmd + eap->force_ff);
19768# ifdef FEAT_MBYTE
19769 if (eap->force_enc != 0)
19770 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19771 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019772 if (eap->bad_char == BAD_KEEP)
19773 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19774 else if (eap->bad_char == BAD_DROP)
19775 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19776 else if (eap->bad_char != 0)
19777 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019778# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019779 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019780 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019781}
19782#endif
19783
19784/*
19785 * Get the value of internal variable "name".
19786 * Return OK or FAIL.
19787 */
19788 static int
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019789get_var_tv(name, len, rettv, verbose, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019790 char_u *name;
19791 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019792 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019793 int verbose; /* may give error message */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019794 int no_autoload; /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019795{
19796 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019797 typval_T *tv = NULL;
19798 typval_T atv;
19799 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019800 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801
19802 /* truncate the name, so that we can use strcmp() */
19803 cc = name[len];
19804 name[len] = NUL;
19805
19806 /*
19807 * Check for "b:changedtick".
19808 */
19809 if (STRCMP(name, "b:changedtick") == 0)
19810 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019811 atv.v_type = VAR_NUMBER;
19812 atv.vval.v_number = curbuf->b_changedtick;
19813 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 }
19815
19816 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 * Check for user-defined variables.
19818 */
19819 else
19820 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010019821 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019823 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824 }
19825
Bram Moolenaare9a41262005-01-15 22:18:47 +000019826 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019828 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019829 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019830 ret = FAIL;
19831 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019832 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019833 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019834
19835 name[len] = cc;
19836
19837 return ret;
19838}
19839
19840/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019841 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19842 * Also handle function call with Funcref variable: func(expr)
19843 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19844 */
19845 static int
19846handle_subscript(arg, rettv, evaluate, verbose)
19847 char_u **arg;
19848 typval_T *rettv;
19849 int evaluate; /* do more than finding the end */
19850 int verbose; /* give error messages */
19851{
19852 int ret = OK;
19853 dict_T *selfdict = NULL;
19854 char_u *s;
19855 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019856 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019857
19858 while (ret == OK
19859 && (**arg == '['
19860 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019861 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019862 && !vim_iswhite(*(*arg - 1)))
19863 {
19864 if (**arg == '(')
19865 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019866 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019867 if (evaluate)
19868 {
19869 functv = *rettv;
19870 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019871
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019872 /* Invoke the function. Recursive! */
19873 s = functv.vval.v_string;
19874 }
19875 else
19876 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019877 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019878 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19879 &len, evaluate, selfdict);
19880
19881 /* Clear the funcref afterwards, so that deleting it while
19882 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010019883 if (evaluate)
19884 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019885
19886 /* Stop the expression evaluation when immediately aborting on
19887 * error, or when an interrupt occurred or an exception was thrown
19888 * but not caught. */
19889 if (aborting())
19890 {
19891 if (ret == OK)
19892 clear_tv(rettv);
19893 ret = FAIL;
19894 }
19895 dict_unref(selfdict);
19896 selfdict = NULL;
19897 }
19898 else /* **arg == '[' || **arg == '.' */
19899 {
19900 dict_unref(selfdict);
19901 if (rettv->v_type == VAR_DICT)
19902 {
19903 selfdict = rettv->vval.v_dict;
19904 if (selfdict != NULL)
19905 ++selfdict->dv_refcount;
19906 }
19907 else
19908 selfdict = NULL;
19909 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19910 {
19911 clear_tv(rettv);
19912 ret = FAIL;
19913 }
19914 }
19915 }
19916 dict_unref(selfdict);
19917 return ret;
19918}
19919
19920/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019921 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019922 * value).
19923 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019924 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019925alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019926{
Bram Moolenaar33570922005-01-25 22:26:29 +000019927 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019928}
19929
19930/*
19931 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 * The string "s" must have been allocated, it is consumed.
19933 * Return NULL for out of memory, the variable otherwise.
19934 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019935 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019936alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 char_u *s;
19938{
Bram Moolenaar33570922005-01-25 22:26:29 +000019939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 rettv = alloc_tv();
19942 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019944 rettv->v_type = VAR_STRING;
19945 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019946 }
19947 else
19948 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019950}
19951
19952/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019953 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019954 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019955 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019956free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019957 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958{
19959 if (varp != NULL)
19960 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019961 switch (varp->v_type)
19962 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019963 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019964 func_unref(varp->vval.v_string);
19965 /*FALLTHROUGH*/
19966 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019967 vim_free(varp->vval.v_string);
19968 break;
19969 case VAR_LIST:
19970 list_unref(varp->vval.v_list);
19971 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019972 case VAR_DICT:
19973 dict_unref(varp->vval.v_dict);
19974 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019975 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019976#ifdef FEAT_FLOAT
19977 case VAR_FLOAT:
19978#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019979 case VAR_UNKNOWN:
19980 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019981 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019982 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019983 break;
19984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 vim_free(varp);
19986 }
19987}
19988
19989/*
19990 * Free the memory for a variable value and set the value to NULL or 0.
19991 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019992 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019993clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019994 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995{
19996 if (varp != NULL)
19997 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019998 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020000 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020001 func_unref(varp->vval.v_string);
20002 /*FALLTHROUGH*/
20003 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020004 vim_free(varp->vval.v_string);
20005 varp->vval.v_string = NULL;
20006 break;
20007 case VAR_LIST:
20008 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020009 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020010 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020011 case VAR_DICT:
20012 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020013 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020014 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020016 varp->vval.v_number = 0;
20017 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020018#ifdef FEAT_FLOAT
20019 case VAR_FLOAT:
20020 varp->vval.v_float = 0.0;
20021 break;
20022#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020023 case VAR_UNKNOWN:
20024 break;
20025 default:
20026 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020028 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 }
20030}
20031
20032/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020033 * Set the value of a variable to NULL without freeing items.
20034 */
20035 static void
20036init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020037 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020038{
20039 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020041}
20042
20043/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044 * Get the number value of a variable.
20045 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020046 * For incompatible types, return 0.
20047 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20048 * caller of incompatible types: it sets *denote to TRUE if "denote"
20049 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020050 */
20051 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020052get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020055 int error = FALSE;
20056
20057 return get_tv_number_chk(varp, &error); /* return 0L on error */
20058}
20059
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020060 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020061get_tv_number_chk(varp, denote)
20062 typval_T *varp;
20063 int *denote;
20064{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020065 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020067 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020069 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020070 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020071#ifdef FEAT_FLOAT
20072 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020073 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020074 break;
20075#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020076 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020077 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020078 break;
20079 case VAR_STRING:
20080 if (varp->vval.v_string != NULL)
20081 vim_str2nr(varp->vval.v_string, NULL, NULL,
20082 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020083 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020084 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020085 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020086 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020087 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020088 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020089 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020090 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020091 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020092 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020094 if (denote == NULL) /* useful for values that must be unsigned */
20095 n = -1;
20096 else
20097 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020098 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099}
20100
20101/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020102 * Get the lnum from the first argument.
20103 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020104 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105 */
20106 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020107get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020108 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109{
Bram Moolenaar33570922005-01-25 22:26:29 +000020110 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 linenr_T lnum;
20112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020113 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 if (lnum == 0) /* no valid number, try using line() */
20115 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020116 rettv.v_type = VAR_NUMBER;
20117 f_line(argvars, &rettv);
20118 lnum = rettv.vval.v_number;
20119 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 }
20121 return lnum;
20122}
20123
20124/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020125 * Get the lnum from the first argument.
20126 * Also accepts "$", then "buf" is used.
20127 * Returns 0 on error.
20128 */
20129 static linenr_T
20130get_tv_lnum_buf(argvars, buf)
20131 typval_T *argvars;
20132 buf_T *buf;
20133{
20134 if (argvars[0].v_type == VAR_STRING
20135 && argvars[0].vval.v_string != NULL
20136 && argvars[0].vval.v_string[0] == '$'
20137 && buf != NULL)
20138 return buf->b_ml.ml_line_count;
20139 return get_tv_number_chk(&argvars[0], NULL);
20140}
20141
20142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 * Get the string value of a variable.
20144 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020145 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20146 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 * If the String variable has never been set, return an empty string.
20148 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020149 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20150 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151 */
20152 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020153get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020154 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155{
20156 static char_u mybuf[NUMBUFLEN];
20157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020158 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020159}
20160
20161 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020162get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020163 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020164 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020166 char_u *res = get_tv_string_buf_chk(varp, buf);
20167
20168 return res != NULL ? res : (char_u *)"";
20169}
20170
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020171 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020172get_tv_string_chk(varp)
20173 typval_T *varp;
20174{
20175 static char_u mybuf[NUMBUFLEN];
20176
20177 return get_tv_string_buf_chk(varp, mybuf);
20178}
20179
20180 static char_u *
20181get_tv_string_buf_chk(varp, buf)
20182 typval_T *varp;
20183 char_u *buf;
20184{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020185 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020187 case VAR_NUMBER:
20188 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20189 return buf;
20190 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020191 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020192 break;
20193 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020194 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020195 break;
20196 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020197 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020198 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020199#ifdef FEAT_FLOAT
20200 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020201 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020202 break;
20203#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020204 case VAR_STRING:
20205 if (varp->vval.v_string != NULL)
20206 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020207 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020208 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020209 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020210 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020212 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213}
20214
20215/*
20216 * Find variable "name" in the list of variables.
20217 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020218 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020219 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020220 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020222 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020223find_var(name, htp, no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020225 hashtab_T **htp;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020226 int no_autoload;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020229 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230
Bram Moolenaara7043832005-01-21 11:56:39 +000020231 ht = find_var_ht(name, &varname);
20232 if (htp != NULL)
20233 *htp = ht;
20234 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020236 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237}
20238
20239/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020240 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020241 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 static dictitem_T *
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020244find_var_in_ht(ht, htname, varname, no_autoload)
Bram Moolenaar33570922005-01-25 22:26:29 +000020245 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020246 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020247 char_u *varname;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020248 int no_autoload;
Bram Moolenaara7043832005-01-21 11:56:39 +000020249{
Bram Moolenaar33570922005-01-25 22:26:29 +000020250 hashitem_T *hi;
20251
20252 if (*varname == NUL)
20253 {
20254 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020255 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020256 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020257 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020258 case 'g': return &globvars_var;
20259 case 'v': return &vimvars_var;
20260 case 'b': return &curbuf->b_bufvar;
20261 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020262#ifdef FEAT_WINDOWS
20263 case 't': return &curtab->tp_winvar;
20264#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020265 case 'l': return current_funccal == NULL
20266 ? NULL : &current_funccal->l_vars_var;
20267 case 'a': return current_funccal == NULL
20268 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020269 }
20270 return NULL;
20271 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020272
20273 hi = hash_find(ht, varname);
20274 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020275 {
20276 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020277 * worked find the variable again. Don't auto-load a script if it was
20278 * loaded already, otherwise it would be loaded every time when
20279 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020280 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020281 {
20282 /* Note: script_autoload() may make "hi" invalid. It must either
20283 * be obtained again or not used. */
20284 if (!script_autoload(varname, FALSE) || aborting())
20285 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020286 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020287 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020288 if (HASHITEM_EMPTY(hi))
20289 return NULL;
20290 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020291 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020292}
20293
20294/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020295 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020296 * Set "varname" to the start of name without ':'.
20297 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020298 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020299find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300 char_u *name;
20301 char_u **varname;
20302{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020303 hashitem_T *hi;
20304
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 if (name[1] != ':')
20306 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020307 /* The name must not start with a colon or #. */
20308 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 return NULL;
20310 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020311
20312 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020313 hi = hash_find(&compat_hashtab, name);
20314 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020315 return &compat_hashtab;
20316
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020318 return &globvarht; /* global variable */
20319 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 }
20321 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020322 if (*name == 'g') /* global variable */
20323 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020324 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20325 */
20326 if (vim_strchr(name + 2, ':') != NULL
20327 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020328 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020330 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020332 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020333#ifdef FEAT_WINDOWS
20334 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020335 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020336#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020337 if (*name == 'v') /* v: variable */
20338 return &vimvarht;
20339 if (*name == 'a' && current_funccal != NULL) /* function argument */
20340 return &current_funccal->l_avars.dv_hashtab;
20341 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20342 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343 if (*name == 's' /* script variable */
20344 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20345 return &SCRIPT_VARS(current_SID);
20346 return NULL;
20347}
20348
20349/*
20350 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020351 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 * Returns NULL when it doesn't exist.
20353 */
20354 char_u *
20355get_var_value(name)
20356 char_u *name;
20357{
Bram Moolenaar33570922005-01-25 22:26:29 +000020358 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359
Bram Moolenaar6d977d62014-01-14 15:24:39 +010020360 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020361 if (v == NULL)
20362 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020363 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364}
20365
20366/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020367 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020368 * sourcing this script and when executing functions defined in the script.
20369 */
20370 void
20371new_script_vars(id)
20372 scid_T id;
20373{
Bram Moolenaara7043832005-01-21 11:56:39 +000020374 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020375 hashtab_T *ht;
20376 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020377
Bram Moolenaar071d4272004-06-13 20:20:40 +000020378 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20379 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020380 /* Re-allocating ga_data means that an ht_array pointing to
20381 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020383 for (i = 1; i <= ga_scripts.ga_len; ++i)
20384 {
20385 ht = &SCRIPT_VARS(i);
20386 if (ht->ht_mask == HT_INIT_SIZE - 1)
20387 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020388 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020389 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020390 }
20391
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392 while (ga_scripts.ga_len < id)
20393 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020394 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020395 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020396 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398 }
20399 }
20400}
20401
20402/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20404 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405 */
20406 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020407init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020408 dict_T *dict;
20409 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020410 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411{
Bram Moolenaar33570922005-01-25 22:26:29 +000020412 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020413 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020414 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020415 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020416 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020417 dict_var->di_tv.vval.v_dict = dict;
20418 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020419 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020420 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20421 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422}
20423
20424/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020425 * Unreference a dictionary initialized by init_var_dict().
20426 */
20427 void
20428unref_var_dict(dict)
20429 dict_T *dict;
20430{
20431 /* Now the dict needs to be freed if no one else is using it, go back to
20432 * normal reference counting. */
20433 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20434 dict_unref(dict);
20435}
20436
20437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020439 * Frees all allocated variables and the value they contain.
20440 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 */
20442 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020443vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020444 hashtab_T *ht;
20445{
20446 vars_clear_ext(ht, TRUE);
20447}
20448
20449/*
20450 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20451 */
20452 static void
20453vars_clear_ext(ht, free_val)
20454 hashtab_T *ht;
20455 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456{
Bram Moolenaara7043832005-01-21 11:56:39 +000020457 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020458 hashitem_T *hi;
20459 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460
Bram Moolenaar33570922005-01-25 22:26:29 +000020461 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020462 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020463 for (hi = ht->ht_array; todo > 0; ++hi)
20464 {
20465 if (!HASHITEM_EMPTY(hi))
20466 {
20467 --todo;
20468
Bram Moolenaar33570922005-01-25 22:26:29 +000020469 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020470 * ht_array might change then. hash_clear() takes care of it
20471 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020472 v = HI2DI(hi);
20473 if (free_val)
20474 clear_tv(&v->di_tv);
20475 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20476 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020477 }
20478 }
20479 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020480 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481}
20482
Bram Moolenaara7043832005-01-21 11:56:39 +000020483/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020484 * Delete a variable from hashtab "ht" at item "hi".
20485 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020488delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020489 hashtab_T *ht;
20490 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491{
Bram Moolenaar33570922005-01-25 22:26:29 +000020492 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020493
20494 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020495 clear_tv(&di->di_tv);
20496 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497}
20498
20499/*
20500 * List the value of one internal variable.
20501 */
20502 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020503list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020504 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020506 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020508 char_u *tofree;
20509 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020510 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020511
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020512 current_copyID += COPYID_INC;
20513 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020514 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020515 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020516 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517}
20518
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020520list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 char_u *prefix;
20522 char_u *name;
20523 int type;
20524 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020525 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526{
Bram Moolenaar31859182007-08-14 20:41:13 +000020527 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20528 msg_start();
20529 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 if (name != NULL) /* "a:" vars don't have a name stored */
20531 msg_puts(name);
20532 msg_putchar(' ');
20533 msg_advance(22);
20534 if (type == VAR_NUMBER)
20535 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020536 else if (type == VAR_FUNC)
20537 msg_putchar('*');
20538 else if (type == VAR_LIST)
20539 {
20540 msg_putchar('[');
20541 if (*string == '[')
20542 ++string;
20543 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020544 else if (type == VAR_DICT)
20545 {
20546 msg_putchar('{');
20547 if (*string == '{')
20548 ++string;
20549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 else
20551 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020552
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020554
20555 if (type == VAR_FUNC)
20556 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020557 if (*first)
20558 {
20559 msg_clr_eos();
20560 *first = FALSE;
20561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020562}
20563
20564/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020565 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020566 * If the variable already exists, the value is updated.
20567 * Otherwise the variable is created.
20568 */
20569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020570set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020572 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020573 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020574{
Bram Moolenaar33570922005-01-25 22:26:29 +000020575 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020576 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020577 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020579 ht = find_var_ht(name, &varname);
20580 if (ht == NULL || *varname == NUL)
20581 {
20582 EMSG2(_(e_illvar), name);
20583 return;
20584 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020585 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020586
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020587 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20588 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020589
Bram Moolenaar33570922005-01-25 22:26:29 +000020590 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020591 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020592 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020593 if (var_check_ro(v->di_flags, name)
20594 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020595 return;
20596 if (v->di_tv.v_type != tv->v_type
20597 && !((v->di_tv.v_type == VAR_STRING
20598 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020599 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020600 || tv->v_type == VAR_NUMBER))
20601#ifdef FEAT_FLOAT
20602 && !((v->di_tv.v_type == VAR_NUMBER
20603 || v->di_tv.v_type == VAR_FLOAT)
20604 && (tv->v_type == VAR_NUMBER
20605 || tv->v_type == VAR_FLOAT))
20606#endif
20607 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020608 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020609 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020610 return;
20611 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020612
20613 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020614 * Handle setting internal v: variables separately: we don't change
20615 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020616 */
20617 if (ht == &vimvarht)
20618 {
20619 if (v->di_tv.v_type == VAR_STRING)
20620 {
20621 vim_free(v->di_tv.vval.v_string);
20622 if (copy || tv->v_type != VAR_STRING)
20623 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20624 else
20625 {
20626 /* Take over the string to avoid an extra alloc/free. */
20627 v->di_tv.vval.v_string = tv->vval.v_string;
20628 tv->vval.v_string = NULL;
20629 }
20630 }
20631 else if (v->di_tv.v_type != VAR_NUMBER)
20632 EMSG2(_(e_intern2), "set_var()");
20633 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020634 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020635 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020636 if (STRCMP(varname, "searchforward") == 0)
20637 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010020638#ifdef FEAT_SEARCH_EXTRA
20639 else if (STRCMP(varname, "hlsearch") == 0)
20640 {
20641 no_hlsearch = !v->di_tv.vval.v_number;
20642 redraw_all_later(SOME_VALID);
20643 }
20644#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020645 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020646 return;
20647 }
20648
20649 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650 }
20651 else /* add a new variable */
20652 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020653 /* Can't add "v:" variable. */
20654 if (ht == &vimvarht)
20655 {
20656 EMSG2(_(e_illvar), name);
20657 return;
20658 }
20659
Bram Moolenaar92124a32005-06-17 22:03:40 +000020660 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020661 if (!valid_varname(varname))
20662 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020663
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020664 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20665 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020666 if (v == NULL)
20667 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020668 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020669 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020670 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020671 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020672 return;
20673 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020674 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020676
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020677 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020678 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020679 else
20680 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020681 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020682 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020683 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685}
20686
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020687/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020688 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020689 * Also give an error message.
20690 */
20691 static int
20692var_check_ro(flags, name)
20693 int flags;
20694 char_u *name;
20695{
20696 if (flags & DI_FLAGS_RO)
20697 {
20698 EMSG2(_(e_readonlyvar), name);
20699 return TRUE;
20700 }
20701 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20702 {
20703 EMSG2(_(e_readonlysbx), name);
20704 return TRUE;
20705 }
20706 return FALSE;
20707}
20708
20709/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020710 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20711 * Also give an error message.
20712 */
20713 static int
20714var_check_fixed(flags, name)
20715 int flags;
20716 char_u *name;
20717{
20718 if (flags & DI_FLAGS_FIX)
20719 {
20720 EMSG2(_("E795: Cannot delete variable %s"), name);
20721 return TRUE;
20722 }
20723 return FALSE;
20724}
20725
20726/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020727 * Check if a funcref is assigned to a valid variable name.
20728 * Return TRUE and give an error if not.
20729 */
20730 static int
20731var_check_func_name(name, new_var)
20732 char_u *name; /* points to start of variable name */
20733 int new_var; /* TRUE when creating the variable */
20734{
20735 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20736 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20737 ? name[2] : name[0]))
20738 {
20739 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20740 name);
20741 return TRUE;
20742 }
20743 /* Don't allow hiding a function. When "v" is not NULL we might be
20744 * assigning another function to the same var, the type is checked
20745 * below. */
20746 if (new_var && function_exists(name))
20747 {
20748 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20749 name);
20750 return TRUE;
20751 }
20752 return FALSE;
20753}
20754
20755/*
20756 * Check if a variable name is valid.
20757 * Return FALSE and give an error if not.
20758 */
20759 static int
20760valid_varname(varname)
20761 char_u *varname;
20762{
20763 char_u *p;
20764
20765 for (p = varname; *p != NUL; ++p)
20766 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20767 && *p != AUTOLOAD_CHAR)
20768 {
20769 EMSG2(_(e_illvar), varname);
20770 return FALSE;
20771 }
20772 return TRUE;
20773}
20774
20775/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020776 * Return TRUE if typeval "tv" is set to be locked (immutable).
20777 * Also give an error message, using "name".
20778 */
20779 static int
20780tv_check_lock(lock, name)
20781 int lock;
20782 char_u *name;
20783{
20784 if (lock & VAR_LOCKED)
20785 {
20786 EMSG2(_("E741: Value is locked: %s"),
20787 name == NULL ? (char_u *)_("Unknown") : name);
20788 return TRUE;
20789 }
20790 if (lock & VAR_FIXED)
20791 {
20792 EMSG2(_("E742: Cannot change value of %s"),
20793 name == NULL ? (char_u *)_("Unknown") : name);
20794 return TRUE;
20795 }
20796 return FALSE;
20797}
20798
20799/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020800 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020801 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020802 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020803 * It is OK for "from" and "to" to point to the same item. This is used to
20804 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020805 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020806 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020807copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020808 typval_T *from;
20809 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020811 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020812 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020813 switch (from->v_type)
20814 {
20815 case VAR_NUMBER:
20816 to->vval.v_number = from->vval.v_number;
20817 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020818#ifdef FEAT_FLOAT
20819 case VAR_FLOAT:
20820 to->vval.v_float = from->vval.v_float;
20821 break;
20822#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020823 case VAR_STRING:
20824 case VAR_FUNC:
20825 if (from->vval.v_string == NULL)
20826 to->vval.v_string = NULL;
20827 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020828 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020829 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020830 if (from->v_type == VAR_FUNC)
20831 func_ref(to->vval.v_string);
20832 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020833 break;
20834 case VAR_LIST:
20835 if (from->vval.v_list == NULL)
20836 to->vval.v_list = NULL;
20837 else
20838 {
20839 to->vval.v_list = from->vval.v_list;
20840 ++to->vval.v_list->lv_refcount;
20841 }
20842 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020843 case VAR_DICT:
20844 if (from->vval.v_dict == NULL)
20845 to->vval.v_dict = NULL;
20846 else
20847 {
20848 to->vval.v_dict = from->vval.v_dict;
20849 ++to->vval.v_dict->dv_refcount;
20850 }
20851 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020852 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020853 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020854 break;
20855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856}
20857
20858/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020859 * Make a copy of an item.
20860 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020861 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20862 * reference to an already copied list/dict can be used.
20863 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020864 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020865 static int
20866item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020867 typval_T *from;
20868 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020869 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020870 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020871{
20872 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020873 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020874
Bram Moolenaar33570922005-01-25 22:26:29 +000020875 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020876 {
20877 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020878 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020879 }
20880 ++recurse;
20881
20882 switch (from->v_type)
20883 {
20884 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020885#ifdef FEAT_FLOAT
20886 case VAR_FLOAT:
20887#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020888 case VAR_STRING:
20889 case VAR_FUNC:
20890 copy_tv(from, to);
20891 break;
20892 case VAR_LIST:
20893 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020894 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020895 if (from->vval.v_list == NULL)
20896 to->vval.v_list = NULL;
20897 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20898 {
20899 /* use the copy made earlier */
20900 to->vval.v_list = from->vval.v_list->lv_copylist;
20901 ++to->vval.v_list->lv_refcount;
20902 }
20903 else
20904 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20905 if (to->vval.v_list == NULL)
20906 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020907 break;
20908 case VAR_DICT:
20909 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020910 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020911 if (from->vval.v_dict == NULL)
20912 to->vval.v_dict = NULL;
20913 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20914 {
20915 /* use the copy made earlier */
20916 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20917 ++to->vval.v_dict->dv_refcount;
20918 }
20919 else
20920 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20921 if (to->vval.v_dict == NULL)
20922 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020923 break;
20924 default:
20925 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020926 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020927 }
20928 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020929 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020930}
20931
20932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 * ":echo expr1 ..." print each argument separated with a space, add a
20934 * newline at the end.
20935 * ":echon expr1 ..." print each argument plain.
20936 */
20937 void
20938ex_echo(eap)
20939 exarg_T *eap;
20940{
20941 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020942 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020943 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 char_u *p;
20945 int needclr = TRUE;
20946 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020947 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948
20949 if (eap->skip)
20950 ++emsg_skip;
20951 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20952 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020953 /* If eval1() causes an error message the text from the command may
20954 * still need to be cleared. E.g., "echo 22,44". */
20955 need_clr_eos = needclr;
20956
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020958 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 {
20960 /*
20961 * Report the invalid expression unless the expression evaluation
20962 * has been cancelled due to an aborting error, an interrupt, or an
20963 * exception.
20964 */
20965 if (!aborting())
20966 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020967 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 break;
20969 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020970 need_clr_eos = FALSE;
20971
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 if (!eap->skip)
20973 {
20974 if (atstart)
20975 {
20976 atstart = FALSE;
20977 /* Call msg_start() after eval1(), evaluating the expression
20978 * may cause a message to appear. */
20979 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020980 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020981 /* Mark the saved text as finishing the line, so that what
20982 * follows is displayed on a new line when scrolling back
20983 * at the more prompt. */
20984 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020985 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 }
20988 else if (eap->cmdidx == CMD_echo)
20989 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020990 current_copyID += COPYID_INC;
20991 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020992 if (p != NULL)
20993 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020995 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020997 if (*p != TAB && needclr)
20998 {
20999 /* remove any text still there from the command */
21000 msg_clr_eos();
21001 needclr = FALSE;
21002 }
21003 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 }
21005 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021006 {
21007#ifdef FEAT_MBYTE
21008 if (has_mbyte)
21009 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021010 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021011
21012 (void)msg_outtrans_len_attr(p, i, echo_attr);
21013 p += i - 1;
21014 }
21015 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021017 (void)msg_outtrans_len_attr(p, 1, echo_attr);
21018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021020 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021022 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023 arg = skipwhite(arg);
21024 }
21025 eap->nextcmd = check_nextcmd(arg);
21026
21027 if (eap->skip)
21028 --emsg_skip;
21029 else
21030 {
21031 /* remove text that may still be there from the command */
21032 if (needclr)
21033 msg_clr_eos();
21034 if (eap->cmdidx == CMD_echo)
21035 msg_end();
21036 }
21037}
21038
21039/*
21040 * ":echohl {name}".
21041 */
21042 void
21043ex_echohl(eap)
21044 exarg_T *eap;
21045{
21046 int id;
21047
21048 id = syn_name2id(eap->arg);
21049 if (id == 0)
21050 echo_attr = 0;
21051 else
21052 echo_attr = syn_id2attr(id);
21053}
21054
21055/*
21056 * ":execute expr1 ..." execute the result of an expression.
21057 * ":echomsg expr1 ..." Print a message
21058 * ":echoerr expr1 ..." Print an error
21059 * Each gets spaces around each argument and a newline at the end for
21060 * echo commands
21061 */
21062 void
21063ex_execute(eap)
21064 exarg_T *eap;
21065{
21066 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021067 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 int ret = OK;
21069 char_u *p;
21070 garray_T ga;
21071 int len;
21072 int save_did_emsg;
21073
21074 ga_init2(&ga, 1, 80);
21075
21076 if (eap->skip)
21077 ++emsg_skip;
21078 while (*arg != NUL && *arg != '|' && *arg != '\n')
21079 {
21080 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021081 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 {
21083 /*
21084 * Report the invalid expression unless the expression evaluation
21085 * has been cancelled due to an aborting error, an interrupt, or an
21086 * exception.
21087 */
21088 if (!aborting())
21089 EMSG2(_(e_invexpr2), p);
21090 ret = FAIL;
21091 break;
21092 }
21093
21094 if (!eap->skip)
21095 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021096 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 len = (int)STRLEN(p);
21098 if (ga_grow(&ga, len + 2) == FAIL)
21099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021100 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 ret = FAIL;
21102 break;
21103 }
21104 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021106 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 ga.ga_len += len;
21108 }
21109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021110 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 arg = skipwhite(arg);
21112 }
21113
21114 if (ret != FAIL && ga.ga_data != NULL)
21115 {
21116 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021118 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021119 out_flush();
21120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 else if (eap->cmdidx == CMD_echoerr)
21122 {
21123 /* We don't want to abort following commands, restore did_emsg. */
21124 save_did_emsg = did_emsg;
21125 EMSG((char_u *)ga.ga_data);
21126 if (!force_abort)
21127 did_emsg = save_did_emsg;
21128 }
21129 else if (eap->cmdidx == CMD_execute)
21130 do_cmdline((char_u *)ga.ga_data,
21131 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21132 }
21133
21134 ga_clear(&ga);
21135
21136 if (eap->skip)
21137 --emsg_skip;
21138
21139 eap->nextcmd = check_nextcmd(arg);
21140}
21141
21142/*
21143 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21144 * "arg" points to the "&" or '+' when called, to "option" when returning.
21145 * Returns NULL when no option name found. Otherwise pointer to the char
21146 * after the option name.
21147 */
21148 static char_u *
21149find_option_end(arg, opt_flags)
21150 char_u **arg;
21151 int *opt_flags;
21152{
21153 char_u *p = *arg;
21154
21155 ++p;
21156 if (*p == 'g' && p[1] == ':')
21157 {
21158 *opt_flags = OPT_GLOBAL;
21159 p += 2;
21160 }
21161 else if (*p == 'l' && p[1] == ':')
21162 {
21163 *opt_flags = OPT_LOCAL;
21164 p += 2;
21165 }
21166 else
21167 *opt_flags = 0;
21168
21169 if (!ASCII_ISALPHA(*p))
21170 return NULL;
21171 *arg = p;
21172
21173 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21174 p += 4; /* termcap option */
21175 else
21176 while (ASCII_ISALPHA(*p))
21177 ++p;
21178 return p;
21179}
21180
21181/*
21182 * ":function"
21183 */
21184 void
21185ex_function(eap)
21186 exarg_T *eap;
21187{
21188 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021189 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190 int j;
21191 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021193 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 char_u *name = NULL;
21195 char_u *p;
21196 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021197 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198 garray_T newargs;
21199 garray_T newlines;
21200 int varargs = FALSE;
21201 int mustend = FALSE;
21202 int flags = 0;
21203 ufunc_T *fp;
21204 int indent;
21205 int nesting;
21206 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021207 dictitem_T *v;
21208 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021209 static int func_nr = 0; /* number for nameless function */
21210 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021211 hashtab_T *ht;
21212 int todo;
21213 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021214 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215
21216 /*
21217 * ":function" without argument: list functions.
21218 */
21219 if (ends_excmd(*eap->arg))
21220 {
21221 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021222 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021223 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021224 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021225 {
21226 if (!HASHITEM_EMPTY(hi))
21227 {
21228 --todo;
21229 fp = HI2UF(hi);
21230 if (!isdigit(*fp->uf_name))
21231 list_func_head(fp, FALSE);
21232 }
21233 }
21234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235 eap->nextcmd = check_nextcmd(eap->arg);
21236 return;
21237 }
21238
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021239 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021240 * ":function /pat": list functions matching pattern.
21241 */
21242 if (*eap->arg == '/')
21243 {
21244 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21245 if (!eap->skip)
21246 {
21247 regmatch_T regmatch;
21248
21249 c = *p;
21250 *p = NUL;
21251 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21252 *p = c;
21253 if (regmatch.regprog != NULL)
21254 {
21255 regmatch.rm_ic = p_ic;
21256
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021257 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021258 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21259 {
21260 if (!HASHITEM_EMPTY(hi))
21261 {
21262 --todo;
21263 fp = HI2UF(hi);
21264 if (!isdigit(*fp->uf_name)
21265 && vim_regexec(&regmatch, fp->uf_name, 0))
21266 list_func_head(fp, FALSE);
21267 }
21268 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021269 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021270 }
21271 }
21272 if (*p == '/')
21273 ++p;
21274 eap->nextcmd = check_nextcmd(p);
21275 return;
21276 }
21277
21278 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021279 * Get the function name. There are these situations:
21280 * func normal function name
21281 * "name" == func, "fudi.fd_dict" == NULL
21282 * dict.func new dictionary entry
21283 * "name" == NULL, "fudi.fd_dict" set,
21284 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21285 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021286 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021287 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21288 * dict.func existing dict entry that's not a Funcref
21289 * "name" == NULL, "fudi.fd_dict" set,
21290 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021292 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021293 name = trans_function_name(&p, eap->skip, 0, &fudi);
21294 paren = (vim_strchr(p, '(') != NULL);
21295 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 {
21297 /*
21298 * Return on an invalid expression in braces, unless the expression
21299 * evaluation has been cancelled due to an aborting error, an
21300 * interrupt, or an exception.
21301 */
21302 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021304 if (!eap->skip && fudi.fd_newkey != NULL)
21305 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021306 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 else
21310 eap->skip = TRUE;
21311 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021312
Bram Moolenaar071d4272004-06-13 20:20:40 +000021313 /* An error in a function call during evaluation of an expression in magic
21314 * braces should not cause the function not to be defined. */
21315 saved_did_emsg = did_emsg;
21316 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317
21318 /*
21319 * ":function func" with only function name: list function.
21320 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021321 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021322 {
21323 if (!ends_excmd(*skipwhite(p)))
21324 {
21325 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021326 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021327 }
21328 eap->nextcmd = check_nextcmd(p);
21329 if (eap->nextcmd != NULL)
21330 *p = NUL;
21331 if (!eap->skip && !got_int)
21332 {
21333 fp = find_func(name);
21334 if (fp != NULL)
21335 {
21336 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021337 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021339 if (FUNCLINE(fp, j) == NULL)
21340 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021341 msg_putchar('\n');
21342 msg_outnum((long)(j + 1));
21343 if (j < 9)
21344 msg_putchar(' ');
21345 if (j < 99)
21346 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021347 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 out_flush(); /* show a line at a time */
21349 ui_breakcheck();
21350 }
21351 if (!got_int)
21352 {
21353 msg_putchar('\n');
21354 msg_puts((char_u *)" endfunction");
21355 }
21356 }
21357 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021358 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021360 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 }
21362
21363 /*
21364 * ":function name(arg1, arg2)" Define function.
21365 */
21366 p = skipwhite(p);
21367 if (*p != '(')
21368 {
21369 if (!eap->skip)
21370 {
21371 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021372 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 }
21374 /* attempt to continue by skipping some text */
21375 if (vim_strchr(p, '(') != NULL)
21376 p = vim_strchr(p, '(');
21377 }
21378 p = skipwhite(p + 1);
21379
21380 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21381 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21382
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021383 if (!eap->skip)
21384 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021385 /* Check the name of the function. Unless it's a dictionary function
21386 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021387 if (name != NULL)
21388 arg = name;
21389 else
21390 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021391 if (arg != NULL && (fudi.fd_di == NULL
21392 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021393 {
21394 if (*arg == K_SPECIAL)
21395 j = 3;
21396 else
21397 j = 0;
21398 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21399 : eval_isnamec(arg[j])))
21400 ++j;
21401 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021402 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021403 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021404 /* Disallow using the g: dict. */
21405 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21406 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021407 }
21408
Bram Moolenaar071d4272004-06-13 20:20:40 +000021409 /*
21410 * Isolate the arguments: "arg1, arg2, ...)"
21411 */
21412 while (*p != ')')
21413 {
21414 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21415 {
21416 varargs = TRUE;
21417 p += 3;
21418 mustend = TRUE;
21419 }
21420 else
21421 {
21422 arg = p;
21423 while (ASCII_ISALNUM(*p) || *p == '_')
21424 ++p;
21425 if (arg == p || isdigit(*arg)
21426 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21427 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21428 {
21429 if (!eap->skip)
21430 EMSG2(_("E125: Illegal argument: %s"), arg);
21431 break;
21432 }
21433 if (ga_grow(&newargs, 1) == FAIL)
21434 goto erret;
21435 c = *p;
21436 *p = NUL;
21437 arg = vim_strsave(arg);
21438 if (arg == NULL)
21439 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021440
21441 /* Check for duplicate argument name. */
21442 for (i = 0; i < newargs.ga_len; ++i)
21443 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21444 {
21445 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21446 goto erret;
21447 }
21448
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21450 *p = c;
21451 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 if (*p == ',')
21453 ++p;
21454 else
21455 mustend = TRUE;
21456 }
21457 p = skipwhite(p);
21458 if (mustend && *p != ')')
21459 {
21460 if (!eap->skip)
21461 EMSG2(_(e_invarg2), eap->arg);
21462 break;
21463 }
21464 }
21465 ++p; /* skip the ')' */
21466
Bram Moolenaare9a41262005-01-15 22:18:47 +000021467 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468 for (;;)
21469 {
21470 p = skipwhite(p);
21471 if (STRNCMP(p, "range", 5) == 0)
21472 {
21473 flags |= FC_RANGE;
21474 p += 5;
21475 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021476 else if (STRNCMP(p, "dict", 4) == 0)
21477 {
21478 flags |= FC_DICT;
21479 p += 4;
21480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 else if (STRNCMP(p, "abort", 5) == 0)
21482 {
21483 flags |= FC_ABORT;
21484 p += 5;
21485 }
21486 else
21487 break;
21488 }
21489
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021490 /* When there is a line break use what follows for the function body.
21491 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21492 if (*p == '\n')
21493 line_arg = p + 1;
21494 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 EMSG(_(e_trailing));
21496
21497 /*
21498 * Read the body of the function, until ":endfunction" is found.
21499 */
21500 if (KeyTyped)
21501 {
21502 /* Check if the function already exists, don't let the user type the
21503 * whole function before telling him it doesn't work! For a script we
21504 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021505 if (!eap->skip && !eap->forceit)
21506 {
21507 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21508 EMSG(_(e_funcdict));
21509 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021510 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021513 if (!eap->skip && did_emsg)
21514 goto erret;
21515
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 msg_putchar('\n'); /* don't overwrite the function name */
21517 cmdline_row = msg_row;
21518 }
21519
21520 indent = 2;
21521 nesting = 0;
21522 for (;;)
21523 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021524 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021525 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021526 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021527 saved_wait_return = FALSE;
21528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021530 sourcing_lnum_off = sourcing_lnum;
21531
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021532 if (line_arg != NULL)
21533 {
21534 /* Use eap->arg, split up in parts by line breaks. */
21535 theline = line_arg;
21536 p = vim_strchr(theline, '\n');
21537 if (p == NULL)
21538 line_arg += STRLEN(line_arg);
21539 else
21540 {
21541 *p = NUL;
21542 line_arg = p + 1;
21543 }
21544 }
21545 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021546 theline = getcmdline(':', 0L, indent);
21547 else
21548 theline = eap->getline(':', eap->cookie, indent);
21549 if (KeyTyped)
21550 lines_left = Rows - 1;
21551 if (theline == NULL)
21552 {
21553 EMSG(_("E126: Missing :endfunction"));
21554 goto erret;
21555 }
21556
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021557 /* Detect line continuation: sourcing_lnum increased more than one. */
21558 if (sourcing_lnum > sourcing_lnum_off + 1)
21559 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21560 else
21561 sourcing_lnum_off = 0;
21562
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563 if (skip_until != NULL)
21564 {
21565 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21566 * don't check for ":endfunc". */
21567 if (STRCMP(theline, skip_until) == 0)
21568 {
21569 vim_free(skip_until);
21570 skip_until = NULL;
21571 }
21572 }
21573 else
21574 {
21575 /* skip ':' and blanks*/
21576 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21577 ;
21578
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021579 /* Check for "endfunction". */
21580 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021582 if (line_arg == NULL)
21583 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 break;
21585 }
21586
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021587 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 * at "end". */
21589 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21590 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021591 else if (STRNCMP(p, "if", 2) == 0
21592 || STRNCMP(p, "wh", 2) == 0
21593 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 || STRNCMP(p, "try", 3) == 0)
21595 indent += 2;
21596
21597 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021598 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021600 if (*p == '!')
21601 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 p += eval_fname_script(p);
21603 if (ASCII_ISALPHA(*p))
21604 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021605 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021606 if (*skipwhite(p) == '(')
21607 {
21608 ++nesting;
21609 indent += 2;
21610 }
21611 }
21612 }
21613
21614 /* Check for ":append" or ":insert". */
21615 p = skip_range(p, NULL);
21616 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21617 || (p[0] == 'i'
21618 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21619 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21620 skip_until = vim_strsave((char_u *)".");
21621
21622 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21623 arg = skipwhite(skiptowhite(p));
21624 if (arg[0] == '<' && arg[1] =='<'
21625 && ((p[0] == 'p' && p[1] == 'y'
21626 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21627 || (p[0] == 'p' && p[1] == 'e'
21628 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21629 || (p[0] == 't' && p[1] == 'c'
21630 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021631 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21632 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21634 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021635 || (p[0] == 'm' && p[1] == 'z'
21636 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 ))
21638 {
21639 /* ":python <<" continues until a dot, like ":append" */
21640 p = skipwhite(arg + 2);
21641 if (*p == NUL)
21642 skip_until = vim_strsave((char_u *)".");
21643 else
21644 skip_until = vim_strsave(p);
21645 }
21646 }
21647
21648 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021649 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021650 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021651 if (line_arg == NULL)
21652 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021654 }
21655
21656 /* Copy the line to newly allocated memory. get_one_sourceline()
21657 * allocates 250 bytes per line, this saves 80% on average. The cost
21658 * is an extra alloc/free. */
21659 p = vim_strsave(theline);
21660 if (p != NULL)
21661 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021662 if (line_arg == NULL)
21663 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021664 theline = p;
21665 }
21666
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021667 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21668
21669 /* Add NULL lines for continuation lines, so that the line count is
21670 * equal to the index in the growarray. */
21671 while (sourcing_lnum_off-- > 0)
21672 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021673
21674 /* Check for end of eap->arg. */
21675 if (line_arg != NULL && *line_arg == NUL)
21676 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 }
21678
21679 /* Don't define the function when skipping commands or when an error was
21680 * detected. */
21681 if (eap->skip || did_emsg)
21682 goto erret;
21683
21684 /*
21685 * If there are no errors, add the function
21686 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021687 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021688 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021689 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000021690 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021691 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021692 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021693 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021694 goto erret;
21695 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021696
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021697 fp = find_func(name);
21698 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021699 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021700 if (!eap->forceit)
21701 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021702 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021703 goto erret;
21704 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021705 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021706 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021707 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708 name);
21709 goto erret;
21710 }
21711 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021712 ga_clear_strings(&(fp->uf_args));
21713 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021714 vim_free(name);
21715 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717 }
21718 else
21719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021720 char numbuf[20];
21721
21722 fp = NULL;
21723 if (fudi.fd_newkey == NULL && !eap->forceit)
21724 {
21725 EMSG(_(e_funcdict));
21726 goto erret;
21727 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021728 if (fudi.fd_di == NULL)
21729 {
21730 /* Can't add a function to a locked dictionary */
21731 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21732 goto erret;
21733 }
21734 /* Can't change an existing function if it is locked */
21735 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21736 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021737
21738 /* Give the function a sequential number. Can only be used with a
21739 * Funcref! */
21740 vim_free(name);
21741 sprintf(numbuf, "%d", ++func_nr);
21742 name = vim_strsave((char_u *)numbuf);
21743 if (name == NULL)
21744 goto erret;
21745 }
21746
21747 if (fp == NULL)
21748 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021749 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021750 {
21751 int slen, plen;
21752 char_u *scriptname;
21753
21754 /* Check that the autoload name matches the script name. */
21755 j = FAIL;
21756 if (sourcing_name != NULL)
21757 {
21758 scriptname = autoload_name(name);
21759 if (scriptname != NULL)
21760 {
21761 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021762 plen = (int)STRLEN(p);
21763 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021764 if (slen > plen && fnamecmp(p,
21765 sourcing_name + slen - plen) == 0)
21766 j = OK;
21767 vim_free(scriptname);
21768 }
21769 }
21770 if (j == FAIL)
21771 {
21772 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21773 goto erret;
21774 }
21775 }
21776
21777 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 if (fp == NULL)
21779 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021780
21781 if (fudi.fd_dict != NULL)
21782 {
21783 if (fudi.fd_di == NULL)
21784 {
21785 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021786 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021787 if (fudi.fd_di == NULL)
21788 {
21789 vim_free(fp);
21790 goto erret;
21791 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021792 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21793 {
21794 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021795 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021796 goto erret;
21797 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021798 }
21799 else
21800 /* overwrite existing dict entry */
21801 clear_tv(&fudi.fd_di->di_tv);
21802 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021803 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021804 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021805 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021806
21807 /* behave like "dict" was used */
21808 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021809 }
21810
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021812 STRCPY(fp->uf_name, name);
21813 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021814 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021815 fp->uf_args = newargs;
21816 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021817#ifdef FEAT_PROFILE
21818 fp->uf_tml_count = NULL;
21819 fp->uf_tml_total = NULL;
21820 fp->uf_tml_self = NULL;
21821 fp->uf_profiling = FALSE;
21822 if (prof_def_func())
21823 func_do_profile(fp);
21824#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021825 fp->uf_varargs = varargs;
21826 fp->uf_flags = flags;
21827 fp->uf_calls = 0;
21828 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021829 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830
21831erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 ga_clear_strings(&newargs);
21833 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021834ret_free:
21835 vim_free(skip_until);
21836 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021839 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840}
21841
21842/*
21843 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021844 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021846 * flags:
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021847 * TFN_INT: internal function name OK
21848 * TFN_QUIET: be quiet
21849 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000021850 * Advances "pp" to just after the function name (if no error).
21851 */
21852 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021853trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 char_u **pp;
21855 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021856 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021857 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021859 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 char_u *start;
21861 char_u *end;
21862 int lead;
21863 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021865 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021866
21867 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021868 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021870
21871 /* Check for hard coded <SNR>: already translated function ID (from a user
21872 * command). */
21873 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21874 && (*pp)[2] == (int)KE_SNR)
21875 {
21876 *pp += 3;
21877 len = get_id_len(pp) + 3;
21878 return vim_strnsave(start, len);
21879 }
21880
21881 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21882 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021884 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021886
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021887 /* Note that TFN_ flags use the same values as GLV_ flags. */
21888 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021889 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021890 if (end == start)
21891 {
21892 if (!skip)
21893 EMSG(_("E129: Function name required"));
21894 goto theend;
21895 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021896 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021897 {
21898 /*
21899 * Report an invalid expression in braces, unless the expression
21900 * evaluation has been cancelled due to an aborting error, an
21901 * interrupt, or an exception.
21902 */
21903 if (!aborting())
21904 {
21905 if (end != NULL)
21906 EMSG2(_(e_invarg2), start);
21907 }
21908 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021909 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021910 goto theend;
21911 }
21912
21913 if (lv.ll_tv != NULL)
21914 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021915 if (fdp != NULL)
21916 {
21917 fdp->fd_dict = lv.ll_dict;
21918 fdp->fd_newkey = lv.ll_newkey;
21919 lv.ll_newkey = NULL;
21920 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021921 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021922 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21923 {
21924 name = vim_strsave(lv.ll_tv->vval.v_string);
21925 *pp = end;
21926 }
21927 else
21928 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021929 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21930 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021931 EMSG(_(e_funcref));
21932 else
21933 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021934 name = NULL;
21935 }
21936 goto theend;
21937 }
21938
21939 if (lv.ll_name == NULL)
21940 {
21941 /* Error found, but continue after the function name. */
21942 *pp = end;
21943 goto theend;
21944 }
21945
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021946 /* Check if the name is a Funcref. If so, use the value. */
21947 if (lv.ll_exp_name != NULL)
21948 {
21949 len = (int)STRLEN(lv.ll_exp_name);
21950 name = deref_func_name(lv.ll_exp_name, &len);
21951 if (name == lv.ll_exp_name)
21952 name = NULL;
21953 }
21954 else
21955 {
21956 len = (int)(end - *pp);
21957 name = deref_func_name(*pp, &len);
21958 if (name == *pp)
21959 name = NULL;
21960 }
21961 if (name != NULL)
21962 {
21963 name = vim_strsave(name);
21964 *pp = end;
21965 goto theend;
21966 }
21967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021968 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021969 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021970 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021971 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21972 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21973 {
21974 /* When there was "s:" already or the name expanded to get a
21975 * leading "s:" then remove it. */
21976 lv.ll_name += 2;
21977 len -= 2;
21978 lead = 2;
21979 }
21980 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021981 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021982 {
21983 if (lead == 2) /* skip over "s:" */
21984 lv.ll_name += 2;
21985 len = (int)(end - lv.ll_name);
21986 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021987
21988 /*
21989 * Copy the function name to allocated memory.
21990 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21991 * Accept <SNR>123_name() outside a script.
21992 */
21993 if (skip)
21994 lead = 0; /* do nothing */
21995 else if (lead > 0)
21996 {
21997 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021998 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21999 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022000 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000022001 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022002 if (current_SID <= 0)
22003 {
22004 EMSG(_(e_usingsid));
22005 goto theend;
22006 }
22007 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
22008 lead += (int)STRLEN(sid_buf);
22009 }
22010 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022011 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022012 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022013 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022014 goto theend;
22015 }
22016 name = alloc((unsigned)(len + lead + 1));
22017 if (name != NULL)
22018 {
22019 if (lead > 0)
22020 {
22021 name[0] = K_SPECIAL;
22022 name[1] = KS_EXTRA;
22023 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000022024 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000022025 STRCPY(name + 3, sid_buf);
22026 }
22027 mch_memmove(name + lead, lv.ll_name, (size_t)len);
22028 name[len + lead] = NUL;
22029 }
22030 *pp = end;
22031
22032theend:
22033 clear_lval(&lv);
22034 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035}
22036
22037/*
22038 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22039 * Return 2 if "p" starts with "s:".
22040 * Return 0 otherwise.
22041 */
22042 static int
22043eval_fname_script(p)
22044 char_u *p;
22045{
22046 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22047 || STRNICMP(p + 1, "SNR>", 4) == 0))
22048 return 5;
22049 if (p[0] == 's' && p[1] == ':')
22050 return 2;
22051 return 0;
22052}
22053
22054/*
22055 * Return TRUE if "p" starts with "<SID>" or "s:".
22056 * Only works if eval_fname_script() returned non-zero for "p"!
22057 */
22058 static int
22059eval_fname_sid(p)
22060 char_u *p;
22061{
22062 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22063}
22064
22065/*
22066 * List the head of the function: "name(arg1, arg2)".
22067 */
22068 static void
22069list_func_head(fp, indent)
22070 ufunc_T *fp;
22071 int indent;
22072{
22073 int j;
22074
22075 msg_start();
22076 if (indent)
22077 MSG_PUTS(" ");
22078 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022079 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080 {
22081 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022082 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 }
22084 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022085 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022087 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022088 {
22089 if (j)
22090 MSG_PUTS(", ");
22091 msg_puts(FUNCARG(fp, j));
22092 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022093 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 {
22095 if (j)
22096 MSG_PUTS(", ");
22097 MSG_PUTS("...");
22098 }
22099 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022100 if (fp->uf_flags & FC_ABORT)
22101 MSG_PUTS(" abort");
22102 if (fp->uf_flags & FC_RANGE)
22103 MSG_PUTS(" range");
22104 if (fp->uf_flags & FC_DICT)
22105 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022106 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022107 if (p_verbose > 0)
22108 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109}
22110
22111/*
22112 * Find a function by name, return pointer to it in ufuncs.
22113 * Return NULL for unknown function.
22114 */
22115 static ufunc_T *
22116find_func(name)
22117 char_u *name;
22118{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022119 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022121 hi = hash_find(&func_hashtab, name);
22122 if (!HASHITEM_EMPTY(hi))
22123 return HI2UF(hi);
22124 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125}
22126
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022127#if defined(EXITFREE) || defined(PROTO)
22128 void
22129free_all_functions()
22130{
22131 hashitem_T *hi;
22132
22133 /* Need to start all over every time, because func_free() may change the
22134 * hash table. */
22135 while (func_hashtab.ht_used > 0)
22136 for (hi = func_hashtab.ht_array; ; ++hi)
22137 if (!HASHITEM_EMPTY(hi))
22138 {
22139 func_free(HI2UF(hi));
22140 break;
22141 }
22142}
22143#endif
22144
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022145 int
22146translated_function_exists(name)
22147 char_u *name;
22148{
22149 if (builtin_function(name))
22150 return find_internal_func(name) >= 0;
22151 return find_func(name) != NULL;
22152}
22153
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022154/*
22155 * Return TRUE if a function "name" exists.
22156 */
22157 static int
22158function_exists(name)
22159 char_u *name;
22160{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022161 char_u *nm = name;
22162 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022163 int n = FALSE;
22164
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022165 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
22166 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022167 nm = skipwhite(nm);
22168
22169 /* Only accept "funcname", "funcname ", "funcname (..." and
22170 * "funcname(...", not "funcname!...". */
22171 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022172 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022173 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022174 return n;
22175}
22176
Bram Moolenaara1544c02013-05-30 12:35:52 +020022177 char_u *
22178get_expanded_name(name, check)
22179 char_u *name;
22180 int check;
22181{
22182 char_u *nm = name;
22183 char_u *p;
22184
22185 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22186
22187 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022188 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022189 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022190
Bram Moolenaara1544c02013-05-30 12:35:52 +020022191 vim_free(p);
22192 return NULL;
22193}
22194
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022195/*
22196 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022197 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022198 */
22199 static int
22200builtin_function(name)
22201 char_u *name;
22202{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022203 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22204 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022205}
22206
Bram Moolenaar05159a02005-02-26 23:04:13 +000022207#if defined(FEAT_PROFILE) || defined(PROTO)
22208/*
22209 * Start profiling function "fp".
22210 */
22211 static void
22212func_do_profile(fp)
22213 ufunc_T *fp;
22214{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022215 int len = fp->uf_lines.ga_len;
22216
22217 if (len == 0)
22218 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022219 fp->uf_tm_count = 0;
22220 profile_zero(&fp->uf_tm_self);
22221 profile_zero(&fp->uf_tm_total);
22222 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022223 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022224 if (fp->uf_tml_total == NULL)
22225 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022226 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022227 if (fp->uf_tml_self == NULL)
22228 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022229 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022230 fp->uf_tml_idx = -1;
22231 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22232 || fp->uf_tml_self == NULL)
22233 return; /* out of memory */
22234
22235 fp->uf_profiling = TRUE;
22236}
22237
22238/*
22239 * Dump the profiling results for all functions in file "fd".
22240 */
22241 void
22242func_dump_profile(fd)
22243 FILE *fd;
22244{
22245 hashitem_T *hi;
22246 int todo;
22247 ufunc_T *fp;
22248 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022249 ufunc_T **sorttab;
22250 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022251
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022252 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022253 if (todo == 0)
22254 return; /* nothing to dump */
22255
Bram Moolenaar73830342005-02-28 22:48:19 +000022256 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22257
Bram Moolenaar05159a02005-02-26 23:04:13 +000022258 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22259 {
22260 if (!HASHITEM_EMPTY(hi))
22261 {
22262 --todo;
22263 fp = HI2UF(hi);
22264 if (fp->uf_profiling)
22265 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022266 if (sorttab != NULL)
22267 sorttab[st_len++] = fp;
22268
Bram Moolenaar05159a02005-02-26 23:04:13 +000022269 if (fp->uf_name[0] == K_SPECIAL)
22270 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22271 else
22272 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22273 if (fp->uf_tm_count == 1)
22274 fprintf(fd, "Called 1 time\n");
22275 else
22276 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22277 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22278 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22279 fprintf(fd, "\n");
22280 fprintf(fd, "count total (s) self (s)\n");
22281
22282 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22283 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022284 if (FUNCLINE(fp, i) == NULL)
22285 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022286 prof_func_line(fd, fp->uf_tml_count[i],
22287 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022288 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22289 }
22290 fprintf(fd, "\n");
22291 }
22292 }
22293 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022294
22295 if (sorttab != NULL && st_len > 0)
22296 {
22297 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22298 prof_total_cmp);
22299 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22300 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22301 prof_self_cmp);
22302 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22303 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022304
22305 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022306}
Bram Moolenaar73830342005-02-28 22:48:19 +000022307
22308 static void
22309prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22310 FILE *fd;
22311 ufunc_T **sorttab;
22312 int st_len;
22313 char *title;
22314 int prefer_self; /* when equal print only self time */
22315{
22316 int i;
22317 ufunc_T *fp;
22318
22319 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22320 fprintf(fd, "count total (s) self (s) function\n");
22321 for (i = 0; i < 20 && i < st_len; ++i)
22322 {
22323 fp = sorttab[i];
22324 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22325 prefer_self);
22326 if (fp->uf_name[0] == K_SPECIAL)
22327 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22328 else
22329 fprintf(fd, " %s()\n", fp->uf_name);
22330 }
22331 fprintf(fd, "\n");
22332}
22333
22334/*
22335 * Print the count and times for one function or function line.
22336 */
22337 static void
22338prof_func_line(fd, count, total, self, prefer_self)
22339 FILE *fd;
22340 int count;
22341 proftime_T *total;
22342 proftime_T *self;
22343 int prefer_self; /* when equal print only self time */
22344{
22345 if (count > 0)
22346 {
22347 fprintf(fd, "%5d ", count);
22348 if (prefer_self && profile_equal(total, self))
22349 fprintf(fd, " ");
22350 else
22351 fprintf(fd, "%s ", profile_msg(total));
22352 if (!prefer_self && profile_equal(total, self))
22353 fprintf(fd, " ");
22354 else
22355 fprintf(fd, "%s ", profile_msg(self));
22356 }
22357 else
22358 fprintf(fd, " ");
22359}
22360
22361/*
22362 * Compare function for total time sorting.
22363 */
22364 static int
22365#ifdef __BORLANDC__
22366_RTLENTRYF
22367#endif
22368prof_total_cmp(s1, s2)
22369 const void *s1;
22370 const void *s2;
22371{
22372 ufunc_T *p1, *p2;
22373
22374 p1 = *(ufunc_T **)s1;
22375 p2 = *(ufunc_T **)s2;
22376 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22377}
22378
22379/*
22380 * Compare function for self time sorting.
22381 */
22382 static int
22383#ifdef __BORLANDC__
22384_RTLENTRYF
22385#endif
22386prof_self_cmp(s1, s2)
22387 const void *s1;
22388 const void *s2;
22389{
22390 ufunc_T *p1, *p2;
22391
22392 p1 = *(ufunc_T **)s1;
22393 p2 = *(ufunc_T **)s2;
22394 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22395}
22396
Bram Moolenaar05159a02005-02-26 23:04:13 +000022397#endif
22398
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022399/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022400 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022401 * Return TRUE if a package was loaded.
22402 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022403 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022404script_autoload(name, reload)
22405 char_u *name;
22406 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022407{
22408 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022409 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022410 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022411 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022412
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022413 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022414 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022415 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022416 return FALSE;
22417
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022418 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022419
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022420 /* Find the name in the list of previously loaded package names. Skip
22421 * "autoload/", it's always the same. */
22422 for (i = 0; i < ga_loaded.ga_len; ++i)
22423 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22424 break;
22425 if (!reload && i < ga_loaded.ga_len)
22426 ret = FALSE; /* was loaded already */
22427 else
22428 {
22429 /* Remember the name if it wasn't loaded already. */
22430 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22431 {
22432 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22433 tofree = NULL;
22434 }
22435
22436 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022437 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022438 ret = TRUE;
22439 }
22440
22441 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022442 return ret;
22443}
22444
22445/*
22446 * Return the autoload script name for a function or variable name.
22447 * Returns NULL when out of memory.
22448 */
22449 static char_u *
22450autoload_name(name)
22451 char_u *name;
22452{
22453 char_u *p;
22454 char_u *scriptname;
22455
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022456 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022457 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22458 if (scriptname == NULL)
22459 return FALSE;
22460 STRCPY(scriptname, "autoload/");
22461 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022462 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022463 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022464 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022465 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022466 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022467}
22468
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22470
22471/*
22472 * Function given to ExpandGeneric() to obtain the list of user defined
22473 * function names.
22474 */
22475 char_u *
22476get_user_func_name(xp, idx)
22477 expand_T *xp;
22478 int idx;
22479{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022480 static long_u done;
22481 static hashitem_T *hi;
22482 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483
22484 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022486 done = 0;
22487 hi = func_hashtab.ht_array;
22488 }
22489 if (done < func_hashtab.ht_used)
22490 {
22491 if (done++ > 0)
22492 ++hi;
22493 while (HASHITEM_EMPTY(hi))
22494 ++hi;
22495 fp = HI2UF(hi);
22496
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022497 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022498 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022499
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022500 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22501 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502
22503 cat_func_name(IObuff, fp);
22504 if (xp->xp_context != EXPAND_USER_FUNC)
22505 {
22506 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022507 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022508 STRCAT(IObuff, ")");
22509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 return IObuff;
22511 }
22512 return NULL;
22513}
22514
22515#endif /* FEAT_CMDL_COMPL */
22516
22517/*
22518 * Copy the function name of "fp" to buffer "buf".
22519 * "buf" must be able to hold the function name plus three bytes.
22520 * Takes care of script-local function names.
22521 */
22522 static void
22523cat_func_name(buf, fp)
22524 char_u *buf;
22525 ufunc_T *fp;
22526{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022527 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528 {
22529 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022530 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022531 }
22532 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022533 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534}
22535
22536/*
22537 * ":delfunction {name}"
22538 */
22539 void
22540ex_delfunction(eap)
22541 exarg_T *eap;
22542{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022543 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 char_u *p;
22545 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022546 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547
22548 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022549 name = trans_function_name(&p, eap->skip, 0, &fudi);
22550 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022552 {
22553 if (fudi.fd_dict != NULL && !eap->skip)
22554 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 if (!ends_excmd(*skipwhite(p)))
22558 {
22559 vim_free(name);
22560 EMSG(_(e_trailing));
22561 return;
22562 }
22563 eap->nextcmd = check_nextcmd(p);
22564 if (eap->nextcmd != NULL)
22565 *p = NUL;
22566
22567 if (!eap->skip)
22568 fp = find_func(name);
22569 vim_free(name);
22570
22571 if (!eap->skip)
22572 {
22573 if (fp == NULL)
22574 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022575 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 return;
22577 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022578 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 {
22580 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22581 return;
22582 }
22583
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022584 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022586 /* Delete the dict item that refers to the function, it will
22587 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022588 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022590 else
22591 func_free(fp);
22592 }
22593}
22594
22595/*
22596 * Free a function and remove it from the list of functions.
22597 */
22598 static void
22599func_free(fp)
22600 ufunc_T *fp;
22601{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022602 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022603
22604 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022605 ga_clear_strings(&(fp->uf_args));
22606 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022607#ifdef FEAT_PROFILE
22608 vim_free(fp->uf_tml_count);
22609 vim_free(fp->uf_tml_total);
22610 vim_free(fp->uf_tml_self);
22611#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022612
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022613 /* remove the function from the function hashtable */
22614 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22615 if (HASHITEM_EMPTY(hi))
22616 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022617 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022618 hash_remove(&func_hashtab, hi);
22619
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022620 vim_free(fp);
22621}
22622
22623/*
22624 * Unreference a Function: decrement the reference count and free it when it
22625 * becomes zero. Only for numbered functions.
22626 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022627 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022628func_unref(name)
22629 char_u *name;
22630{
22631 ufunc_T *fp;
22632
22633 if (name != NULL && isdigit(*name))
22634 {
22635 fp = find_func(name);
22636 if (fp == NULL)
22637 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022638 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022639 {
22640 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022641 * when "uf_calls" becomes zero. */
22642 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022643 func_free(fp);
22644 }
22645 }
22646}
22647
22648/*
22649 * Count a reference to a Function.
22650 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022651 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022652func_ref(name)
22653 char_u *name;
22654{
22655 ufunc_T *fp;
22656
22657 if (name != NULL && isdigit(*name))
22658 {
22659 fp = find_func(name);
22660 if (fp == NULL)
22661 EMSG2(_(e_intern2), "func_ref()");
22662 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022663 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 }
22665}
22666
22667/*
22668 * Call a user function.
22669 */
22670 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022671call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 ufunc_T *fp; /* pointer to function */
22673 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022674 typval_T *argvars; /* arguments */
22675 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 linenr_T firstline; /* first line of range */
22677 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022678 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679{
Bram Moolenaar33570922005-01-25 22:26:29 +000022680 char_u *save_sourcing_name;
22681 linenr_T save_sourcing_lnum;
22682 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022683 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022684 int save_did_emsg;
22685 static int depth = 0;
22686 dictitem_T *v;
22687 int fixvar_idx = 0; /* index in fixvar[] */
22688 int i;
22689 int ai;
22690 char_u numbuf[NUMBUFLEN];
22691 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022692#ifdef FEAT_PROFILE
22693 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022694 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696
22697 /* If depth of calling is getting too high, don't execute the function */
22698 if (depth >= p_mfd)
22699 {
22700 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022701 rettv->v_type = VAR_NUMBER;
22702 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703 return;
22704 }
22705 ++depth;
22706
22707 line_breakcheck(); /* check for CTRL-C hit */
22708
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022709 fc = (funccall_T *)alloc(sizeof(funccall_T));
22710 fc->caller = current_funccal;
22711 current_funccal = fc;
22712 fc->func = fp;
22713 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022714 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022715 fc->linenr = 0;
22716 fc->returned = FALSE;
22717 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022719 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22720 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721
Bram Moolenaar33570922005-01-25 22:26:29 +000022722 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022723 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022724 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22725 * each argument variable and saves a lot of time.
22726 */
22727 /*
22728 * Init l: variables.
22729 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022730 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022731 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022732 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022733 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22734 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022735 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022736 name = v->di_key;
22737 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022739 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022740 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022741 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022742 v->di_tv.vval.v_dict = selfdict;
22743 ++selfdict->dv_refcount;
22744 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022745
Bram Moolenaar33570922005-01-25 22:26:29 +000022746 /*
22747 * Init a: variables.
22748 * Set a:0 to "argcount".
22749 * Set a:000 to a list with room for the "..." arguments.
22750 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022751 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022752 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022753 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022754 /* Use "name" to avoid a warning from some compiler that checks the
22755 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022756 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022757 name = v->di_key;
22758 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022760 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022761 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022762 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022763 v->di_tv.vval.v_list = &fc->l_varlist;
22764 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22765 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22766 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022767
22768 /*
22769 * Set a:firstline to "firstline" and a:lastline to "lastline".
22770 * Set a:name to named arguments.
22771 * Set a:N to the "..." arguments.
22772 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022773 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022774 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022775 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022776 (varnumber_T)lastline);
22777 for (i = 0; i < argcount; ++i)
22778 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022779 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022780 if (ai < 0)
22781 /* named argument a:name */
22782 name = FUNCARG(fp, i);
22783 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022784 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022785 /* "..." argument a:1, a:2, etc. */
22786 sprintf((char *)numbuf, "%d", ai + 1);
22787 name = numbuf;
22788 }
22789 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22790 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022791 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022792 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22793 }
22794 else
22795 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022796 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22797 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022798 if (v == NULL)
22799 break;
22800 v->di_flags = DI_FLAGS_RO;
22801 }
22802 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022803 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022804
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022805 /* Note: the values are copied directly to avoid alloc/free.
22806 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022807 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022808 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022809
22810 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22811 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022812 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22813 fc->l_listitems[ai].li_tv = argvars[i];
22814 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022815 }
22816 }
22817
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 /* Don't redraw while executing the function. */
22819 ++RedrawingDisabled;
22820 save_sourcing_name = sourcing_name;
22821 save_sourcing_lnum = sourcing_lnum;
22822 sourcing_lnum = 1;
22823 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022824 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 if (sourcing_name != NULL)
22826 {
22827 if (save_sourcing_name != NULL
22828 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22829 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22830 else
22831 STRCPY(sourcing_name, "function ");
22832 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22833
22834 if (p_verbose >= 12)
22835 {
22836 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022837 verbose_enter_scroll();
22838
Bram Moolenaar555b2802005-05-19 21:08:39 +000022839 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840 if (p_verbose >= 14)
22841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022843 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022844 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022845 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846
22847 msg_puts((char_u *)"(");
22848 for (i = 0; i < argcount; ++i)
22849 {
22850 if (i > 0)
22851 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022852 if (argvars[i].v_type == VAR_NUMBER)
22853 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 else
22855 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022856 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22857 if (s != NULL)
22858 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022859 if (vim_strsize(s) > MSG_BUF_CLEN)
22860 {
22861 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22862 s = buf;
22863 }
22864 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022865 vim_free(tofree);
22866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 }
22868 }
22869 msg_puts((char_u *)")");
22870 }
22871 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022872
22873 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 --no_wait_return;
22875 }
22876 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022877#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022878 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022879 {
22880 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22881 func_do_profile(fp);
22882 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022883 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022884 {
22885 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022886 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022887 profile_zero(&fp->uf_tm_children);
22888 }
22889 script_prof_save(&wait_start);
22890 }
22891#endif
22892
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022894 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 save_did_emsg = did_emsg;
22896 did_emsg = FALSE;
22897
22898 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022899 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22901
22902 --RedrawingDisabled;
22903
22904 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022905 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022907 clear_tv(rettv);
22908 rettv->v_type = VAR_NUMBER;
22909 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 }
22911
Bram Moolenaar05159a02005-02-26 23:04:13 +000022912#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022913 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022914 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022915 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022916 profile_end(&call_start);
22917 profile_sub_wait(&wait_start, &call_start);
22918 profile_add(&fp->uf_tm_total, &call_start);
22919 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022920 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022921 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022922 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22923 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022924 }
22925 }
22926#endif
22927
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 /* when being verbose, mention the return value */
22929 if (p_verbose >= 12)
22930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022931 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022932 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022935 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022936 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022937 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022938 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022939 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022941 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022942 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022943 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022944 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022945
Bram Moolenaar555b2802005-05-19 21:08:39 +000022946 /* The value may be very long. Skip the middle part, so that we
22947 * have some idea how it starts and ends. smsg() would always
22948 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022949 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022950 if (s != NULL)
22951 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022952 if (vim_strsize(s) > MSG_BUF_CLEN)
22953 {
22954 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22955 s = buf;
22956 }
22957 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022958 vim_free(tofree);
22959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960 }
22961 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022962
22963 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 --no_wait_return;
22965 }
22966
22967 vim_free(sourcing_name);
22968 sourcing_name = save_sourcing_name;
22969 sourcing_lnum = save_sourcing_lnum;
22970 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022971#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022972 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022973 script_prof_restore(&wait_start);
22974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975
22976 if (p_verbose >= 12 && sourcing_name != NULL)
22977 {
22978 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022979 verbose_enter_scroll();
22980
Bram Moolenaar555b2802005-05-19 21:08:39 +000022981 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022982 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022983
22984 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 --no_wait_return;
22986 }
22987
22988 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022989 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022991
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022992 /* If the a:000 list and the l: and a: dicts are not referenced we can
22993 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022994 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22995 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22996 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22997 {
22998 free_funccal(fc, FALSE);
22999 }
23000 else
23001 {
23002 hashitem_T *hi;
23003 listitem_T *li;
23004 int todo;
23005
23006 /* "fc" is still in use. This can happen when returning "a:000" or
23007 * assigning "l:" to a global variable.
23008 * Link "fc" in the list for garbage collection later. */
23009 fc->caller = previous_funccal;
23010 previous_funccal = fc;
23011
23012 /* Make a copy of the a: variables, since we didn't do that above. */
23013 todo = (int)fc->l_avars.dv_hashtab.ht_used;
23014 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
23015 {
23016 if (!HASHITEM_EMPTY(hi))
23017 {
23018 --todo;
23019 v = HI2DI(hi);
23020 copy_tv(&v->di_tv, &v->di_tv);
23021 }
23022 }
23023
23024 /* Make a copy of the a:000 items, since we didn't do that above. */
23025 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23026 copy_tv(&li->li_tv, &li->li_tv);
23027 }
23028}
23029
23030/*
23031 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023032 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023033 */
23034 static int
23035can_free_funccal(fc, copyID)
23036 funccall_T *fc;
23037 int copyID;
23038{
23039 return (fc->l_varlist.lv_copyID != copyID
23040 && fc->l_vars.dv_copyID != copyID
23041 && fc->l_avars.dv_copyID != copyID);
23042}
23043
23044/*
23045 * Free "fc" and what it contains.
23046 */
23047 static void
23048free_funccal(fc, free_val)
23049 funccall_T *fc;
23050 int free_val; /* a: vars were allocated */
23051{
23052 listitem_T *li;
23053
23054 /* The a: variables typevals may not have been allocated, only free the
23055 * allocated variables. */
23056 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23057
23058 /* free all l: variables */
23059 vars_clear(&fc->l_vars.dv_hashtab);
23060
23061 /* Free the a:000 variables if they were allocated. */
23062 if (free_val)
23063 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23064 clear_tv(&li->li_tv);
23065
23066 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067}
23068
23069/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023070 * Add a number variable "name" to dict "dp" with value "nr".
23071 */
23072 static void
23073add_nr_var(dp, v, name, nr)
23074 dict_T *dp;
23075 dictitem_T *v;
23076 char *name;
23077 varnumber_T nr;
23078{
23079 STRCPY(v->di_key, name);
23080 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23081 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23082 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023083 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023084 v->di_tv.vval.v_number = nr;
23085}
23086
23087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 * ":return [expr]"
23089 */
23090 void
23091ex_return(eap)
23092 exarg_T *eap;
23093{
23094 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023095 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 int returning = FALSE;
23097
23098 if (current_funccal == NULL)
23099 {
23100 EMSG(_("E133: :return not inside a function"));
23101 return;
23102 }
23103
23104 if (eap->skip)
23105 ++emsg_skip;
23106
23107 eap->nextcmd = NULL;
23108 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023109 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 {
23111 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023112 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 }
23116 /* It's safer to return also on error. */
23117 else if (!eap->skip)
23118 {
23119 /*
23120 * Return unless the expression evaluation has been cancelled due to an
23121 * aborting error, an interrupt, or an exception.
23122 */
23123 if (!aborting())
23124 returning = do_return(eap, FALSE, TRUE, NULL);
23125 }
23126
23127 /* When skipping or the return gets pending, advance to the next command
23128 * in this line (!returning). Otherwise, ignore the rest of the line.
23129 * Following lines will be ignored by get_func_line(). */
23130 if (returning)
23131 eap->nextcmd = NULL;
23132 else if (eap->nextcmd == NULL) /* no argument */
23133 eap->nextcmd = check_nextcmd(arg);
23134
23135 if (eap->skip)
23136 --emsg_skip;
23137}
23138
23139/*
23140 * Return from a function. Possibly makes the return pending. Also called
23141 * for a pending return at the ":endtry" or after returning from an extra
23142 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023143 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023144 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 * FALSE when the return gets pending.
23146 */
23147 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023148do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 exarg_T *eap;
23150 int reanimate;
23151 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023152 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153{
23154 int idx;
23155 struct condstack *cstack = eap->cstack;
23156
23157 if (reanimate)
23158 /* Undo the return. */
23159 current_funccal->returned = FALSE;
23160
23161 /*
23162 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23163 * not in its finally clause (which then is to be executed next) is found.
23164 * In this case, make the ":return" pending for execution at the ":endtry".
23165 * Otherwise, return normally.
23166 */
23167 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23168 if (idx >= 0)
23169 {
23170 cstack->cs_pending[idx] = CSTP_RETURN;
23171
23172 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023173 /* A pending return again gets pending. "rettv" points to an
23174 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023176 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 else
23178 {
23179 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023180 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023181 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023182 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023184 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 {
23186 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023187 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023188 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 else
23190 EMSG(_(e_outofmem));
23191 }
23192 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023193 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194
23195 if (reanimate)
23196 {
23197 /* The pending return value could be overwritten by a ":return"
23198 * without argument in a finally clause; reset the default
23199 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023200 current_funccal->rettv->v_type = VAR_NUMBER;
23201 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 }
23203 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023204 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 }
23206 else
23207 {
23208 current_funccal->returned = TRUE;
23209
23210 /* If the return is carried out now, store the return value. For
23211 * a return immediately after reanimation, the value is already
23212 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023213 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023215 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023216 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023218 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219 }
23220 }
23221
23222 return idx < 0;
23223}
23224
23225/*
23226 * Free the variable with a pending return value.
23227 */
23228 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023229discard_pending_return(rettv)
23230 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231{
Bram Moolenaar33570922005-01-25 22:26:29 +000023232 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233}
23234
23235/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023236 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 * is an allocated string. Used by report_pending() for verbose messages.
23238 */
23239 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023240get_return_cmd(rettv)
23241 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023243 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023244 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023245 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023247 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023248 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023249 if (s == NULL)
23250 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023251
23252 STRCPY(IObuff, ":return ");
23253 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23254 if (STRLEN(s) + 8 >= IOSIZE)
23255 STRCPY(IObuff + IOSIZE - 4, "...");
23256 vim_free(tofree);
23257 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258}
23259
23260/*
23261 * Get next function line.
23262 * Called by do_cmdline() to get the next line.
23263 * Returns allocated string, or NULL for end of function.
23264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265 char_u *
23266get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023267 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023269 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270{
Bram Moolenaar33570922005-01-25 22:26:29 +000023271 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023272 ufunc_T *fp = fcp->func;
23273 char_u *retval;
23274 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275
23276 /* If breakpoints have been added/deleted need to check for it. */
23277 if (fcp->dbg_tick != debug_tick)
23278 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023279 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 sourcing_lnum);
23281 fcp->dbg_tick = debug_tick;
23282 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023283#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023284 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023285 func_line_end(cookie);
23286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287
Bram Moolenaar05159a02005-02-26 23:04:13 +000023288 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023289 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23290 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 retval = NULL;
23292 else
23293 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023294 /* Skip NULL lines (continuation lines). */
23295 while (fcp->linenr < gap->ga_len
23296 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23297 ++fcp->linenr;
23298 if (fcp->linenr >= gap->ga_len)
23299 retval = NULL;
23300 else
23301 {
23302 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23303 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023304#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023305 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023306 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023307#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 }
23310
23311 /* Did we encounter a breakpoint? */
23312 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23313 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023314 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023316 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023317 sourcing_lnum);
23318 fcp->dbg_tick = debug_tick;
23319 }
23320
23321 return retval;
23322}
23323
Bram Moolenaar05159a02005-02-26 23:04:13 +000023324#if defined(FEAT_PROFILE) || defined(PROTO)
23325/*
23326 * Called when starting to read a function line.
23327 * "sourcing_lnum" must be correct!
23328 * When skipping lines it may not actually be executed, but we won't find out
23329 * until later and we need to store the time now.
23330 */
23331 void
23332func_line_start(cookie)
23333 void *cookie;
23334{
23335 funccall_T *fcp = (funccall_T *)cookie;
23336 ufunc_T *fp = fcp->func;
23337
23338 if (fp->uf_profiling && sourcing_lnum >= 1
23339 && sourcing_lnum <= fp->uf_lines.ga_len)
23340 {
23341 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023342 /* Skip continuation lines. */
23343 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23344 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023345 fp->uf_tml_execed = FALSE;
23346 profile_start(&fp->uf_tml_start);
23347 profile_zero(&fp->uf_tml_children);
23348 profile_get_wait(&fp->uf_tml_wait);
23349 }
23350}
23351
23352/*
23353 * Called when actually executing a function line.
23354 */
23355 void
23356func_line_exec(cookie)
23357 void *cookie;
23358{
23359 funccall_T *fcp = (funccall_T *)cookie;
23360 ufunc_T *fp = fcp->func;
23361
23362 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23363 fp->uf_tml_execed = TRUE;
23364}
23365
23366/*
23367 * Called when done with a function line.
23368 */
23369 void
23370func_line_end(cookie)
23371 void *cookie;
23372{
23373 funccall_T *fcp = (funccall_T *)cookie;
23374 ufunc_T *fp = fcp->func;
23375
23376 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23377 {
23378 if (fp->uf_tml_execed)
23379 {
23380 ++fp->uf_tml_count[fp->uf_tml_idx];
23381 profile_end(&fp->uf_tml_start);
23382 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023383 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023384 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23385 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023386 }
23387 fp->uf_tml_idx = -1;
23388 }
23389}
23390#endif
23391
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392/*
23393 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023394 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 */
23396 int
23397func_has_ended(cookie)
23398 void *cookie;
23399{
Bram Moolenaar33570922005-01-25 22:26:29 +000023400 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401
23402 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23403 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023404 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 || fcp->returned);
23406}
23407
23408/*
23409 * return TRUE if cookie indicates a function which "abort"s on errors.
23410 */
23411 int
23412func_has_abort(cookie)
23413 void *cookie;
23414{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023415 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023416}
23417
23418#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23419typedef enum
23420{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023421 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23422 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23423 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424} var_flavour_T;
23425
23426static var_flavour_T var_flavour __ARGS((char_u *varname));
23427
23428 static var_flavour_T
23429var_flavour(varname)
23430 char_u *varname;
23431{
23432 char_u *p = varname;
23433
23434 if (ASCII_ISUPPER(*p))
23435 {
23436 while (*(++p))
23437 if (ASCII_ISLOWER(*p))
23438 return VAR_FLAVOUR_SESSION;
23439 return VAR_FLAVOUR_VIMINFO;
23440 }
23441 else
23442 return VAR_FLAVOUR_DEFAULT;
23443}
23444#endif
23445
23446#if defined(FEAT_VIMINFO) || defined(PROTO)
23447/*
23448 * Restore global vars that start with a capital from the viminfo file
23449 */
23450 int
23451read_viminfo_varlist(virp, writing)
23452 vir_T *virp;
23453 int writing;
23454{
23455 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023456 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023457 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458
23459 if (!writing && (find_viminfo_parameter('!') != NULL))
23460 {
23461 tab = vim_strchr(virp->vir_line + 1, '\t');
23462 if (tab != NULL)
23463 {
23464 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023465 switch (*tab)
23466 {
23467 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023468#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023469 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023470#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023471 case 'D': type = VAR_DICT; break;
23472 case 'L': type = VAR_LIST; break;
23473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474
23475 tab = vim_strchr(tab, '\t');
23476 if (tab != NULL)
23477 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023478 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023479 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023480 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023482#ifdef FEAT_FLOAT
23483 else if (type == VAR_FLOAT)
23484 (void)string2float(tab + 1, &tv.vval.v_float);
23485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023487 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023488 if (type == VAR_DICT || type == VAR_LIST)
23489 {
23490 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23491
23492 if (etv == NULL)
23493 /* Failed to parse back the dict or list, use it as a
23494 * string. */
23495 tv.v_type = VAR_STRING;
23496 else
23497 {
23498 vim_free(tv.vval.v_string);
23499 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023500 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023501 }
23502 }
23503
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023504 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023505
23506 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023507 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023508 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23509 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023510 }
23511 }
23512 }
23513
23514 return viminfo_readline(virp);
23515}
23516
23517/*
23518 * Write global vars that start with a capital to the viminfo file
23519 */
23520 void
23521write_viminfo_varlist(fp)
23522 FILE *fp;
23523{
Bram Moolenaar33570922005-01-25 22:26:29 +000023524 hashitem_T *hi;
23525 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023526 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023527 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023528 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023529 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023530 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023531
23532 if (find_viminfo_parameter('!') == NULL)
23533 return;
23534
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023535 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023536
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023537 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023538 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023540 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023541 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023542 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023543 this_var = HI2DI(hi);
23544 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023545 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023546 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023547 {
23548 case VAR_STRING: s = "STR"; break;
23549 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023550#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023551 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023552#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023553 case VAR_DICT: s = "DIC"; break;
23554 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023555 default: continue;
23556 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023557 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023558 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023559 if (p != NULL)
23560 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023561 vim_free(tofree);
23562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 }
23564 }
23565}
23566#endif
23567
23568#if defined(FEAT_SESSION) || defined(PROTO)
23569 int
23570store_session_globals(fd)
23571 FILE *fd;
23572{
Bram Moolenaar33570922005-01-25 22:26:29 +000023573 hashitem_T *hi;
23574 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023575 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576 char_u *p, *t;
23577
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023578 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023579 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023581 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023583 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023584 this_var = HI2DI(hi);
23585 if ((this_var->di_tv.v_type == VAR_NUMBER
23586 || this_var->di_tv.v_type == VAR_STRING)
23587 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023588 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023589 /* Escape special characters with a backslash. Turn a LF and
23590 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023591 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023592 (char_u *)"\\\"\n\r");
23593 if (p == NULL) /* out of memory */
23594 break;
23595 for (t = p; *t != NUL; ++t)
23596 if (*t == '\n')
23597 *t = 'n';
23598 else if (*t == '\r')
23599 *t = 'r';
23600 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023601 this_var->di_key,
23602 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23603 : ' ',
23604 p,
23605 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23606 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023607 || put_eol(fd) == FAIL)
23608 {
23609 vim_free(p);
23610 return FAIL;
23611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 vim_free(p);
23613 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023614#ifdef FEAT_FLOAT
23615 else if (this_var->di_tv.v_type == VAR_FLOAT
23616 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23617 {
23618 float_T f = this_var->di_tv.vval.v_float;
23619 int sign = ' ';
23620
23621 if (f < 0)
23622 {
23623 f = -f;
23624 sign = '-';
23625 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023626 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023627 this_var->di_key, sign, f) < 0)
23628 || put_eol(fd) == FAIL)
23629 return FAIL;
23630 }
23631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023632 }
23633 }
23634 return OK;
23635}
23636#endif
23637
Bram Moolenaar661b1822005-07-28 22:36:45 +000023638/*
23639 * Display script name where an item was last set.
23640 * Should only be invoked when 'verbose' is non-zero.
23641 */
23642 void
23643last_set_msg(scriptID)
23644 scid_T scriptID;
23645{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023646 char_u *p;
23647
Bram Moolenaar661b1822005-07-28 22:36:45 +000023648 if (scriptID != 0)
23649 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023650 p = home_replace_save(NULL, get_scriptname(scriptID));
23651 if (p != NULL)
23652 {
23653 verbose_enter();
23654 MSG_PUTS(_("\n\tLast set from "));
23655 MSG_PUTS(p);
23656 vim_free(p);
23657 verbose_leave();
23658 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023659 }
23660}
23661
Bram Moolenaard812df62008-11-09 12:46:09 +000023662/*
23663 * List v:oldfiles in a nice way.
23664 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023665 void
23666ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023667 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023668{
23669 list_T *l = vimvars[VV_OLDFILES].vv_list;
23670 listitem_T *li;
23671 int nr = 0;
23672
23673 if (l == NULL)
23674 msg((char_u *)_("No old files"));
23675 else
23676 {
23677 msg_start();
23678 msg_scroll = TRUE;
23679 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23680 {
23681 msg_outnum((long)++nr);
23682 MSG_PUTS(": ");
23683 msg_outtrans(get_tv_string(&li->li_tv));
23684 msg_putchar('\n');
23685 out_flush(); /* output one line at a time */
23686 ui_breakcheck();
23687 }
23688 /* Assume "got_int" was set to truncate the listing. */
23689 got_int = FALSE;
23690
23691#ifdef FEAT_BROWSE_CMD
23692 if (cmdmod.browse)
23693 {
23694 quit_more = FALSE;
23695 nr = prompt_for_number(FALSE);
23696 msg_starthere();
23697 if (nr > 0)
23698 {
23699 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23700 (long)nr);
23701
23702 if (p != NULL)
23703 {
23704 p = expand_env_save(p);
23705 eap->arg = p;
23706 eap->cmdidx = CMD_edit;
23707 cmdmod.browse = FALSE;
23708 do_exedit(eap, NULL);
23709 vim_free(p);
23710 }
23711 }
23712 }
23713#endif
23714 }
23715}
23716
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717#endif /* FEAT_EVAL */
23718
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023720#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023721
23722#ifdef WIN3264
23723/*
23724 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23725 */
23726static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23727static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23728static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23729
23730/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023731 * Get the short path (8.3) for the filename in "fnamep".
23732 * Only works for a valid file name.
23733 * When the path gets longer "fnamep" is changed and the allocated buffer
23734 * is put in "bufp".
23735 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23736 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737 */
23738 static int
23739get_short_pathname(fnamep, bufp, fnamelen)
23740 char_u **fnamep;
23741 char_u **bufp;
23742 int *fnamelen;
23743{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023744 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 char_u *newbuf;
23746
23747 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 l = GetShortPathName(*fnamep, *fnamep, len);
23749 if (l > len - 1)
23750 {
23751 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023752 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 newbuf = vim_strnsave(*fnamep, l);
23754 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023755 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756
23757 vim_free(*bufp);
23758 *fnamep = *bufp = newbuf;
23759
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023760 /* Really should always succeed, as the buffer is big enough. */
23761 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023762 }
23763
23764 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023765 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766}
23767
23768/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023769 * Get the short path (8.3) for the filename in "fname". The converted
23770 * path is returned in "bufp".
23771 *
23772 * Some of the directories specified in "fname" may not exist. This function
23773 * will shorten the existing directories at the beginning of the path and then
23774 * append the remaining non-existing path.
23775 *
23776 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023777 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023778 * bufp - Pointer to an allocated buffer for the filename.
23779 * fnamelen - Length of the filename pointed to by fname
23780 *
23781 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 */
23783 static int
23784shortpath_for_invalid_fname(fname, bufp, fnamelen)
23785 char_u **fname;
23786 char_u **bufp;
23787 int *fnamelen;
23788{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023789 char_u *short_fname, *save_fname, *pbuf_unused;
23790 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023792 int old_len, len;
23793 int new_len, sfx_len;
23794 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795
23796 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023797 old_len = *fnamelen;
23798 save_fname = vim_strnsave(*fname, old_len);
23799 pbuf_unused = NULL;
23800 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023802 endp = save_fname + old_len - 1; /* Find the end of the copy */
23803 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023805 /*
23806 * Try shortening the supplied path till it succeeds by removing one
23807 * directory at a time from the tail of the path.
23808 */
23809 len = 0;
23810 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023812 /* go back one path-separator */
23813 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23814 --endp;
23815 if (endp <= save_fname)
23816 break; /* processed the complete path */
23817
23818 /*
23819 * Replace the path separator with a NUL and try to shorten the
23820 * resulting path.
23821 */
23822 ch = *endp;
23823 *endp = 0;
23824 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023825 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023826 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23827 {
23828 retval = FAIL;
23829 goto theend;
23830 }
23831 *endp = ch; /* preserve the string */
23832
23833 if (len > 0)
23834 break; /* successfully shortened the path */
23835
23836 /* failed to shorten the path. Skip the path separator */
23837 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 }
23839
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023840 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023842 /*
23843 * Succeeded in shortening the path. Now concatenate the shortened
23844 * path with the remaining path at the tail.
23845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023847 /* Compute the length of the new path. */
23848 sfx_len = (int)(save_endp - endp) + 1;
23849 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023851 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023853 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023854 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023855 /* There is not enough space in the currently allocated string,
23856 * copy it to a buffer big enough. */
23857 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023859 {
23860 retval = FAIL;
23861 goto theend;
23862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023863 }
23864 else
23865 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023866 /* Transfer short_fname to the main buffer (it's big enough),
23867 * unless get_short_pathname() did its work in-place. */
23868 *fname = *bufp = save_fname;
23869 if (short_fname != save_fname)
23870 vim_strncpy(save_fname, short_fname, len);
23871 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023873
23874 /* concat the not-shortened part of the path */
23875 vim_strncpy(*fname + len, endp, sfx_len);
23876 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023878
23879theend:
23880 vim_free(pbuf_unused);
23881 vim_free(save_fname);
23882
23883 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023884}
23885
23886/*
23887 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023888 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 */
23890 static int
23891shortpath_for_partial(fnamep, bufp, fnamelen)
23892 char_u **fnamep;
23893 char_u **bufp;
23894 int *fnamelen;
23895{
23896 int sepcount, len, tflen;
23897 char_u *p;
23898 char_u *pbuf, *tfname;
23899 int hasTilde;
23900
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023901 /* Count up the path separators from the RHS.. so we know which part
23902 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023904 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023905 if (vim_ispathsep(*p))
23906 ++sepcount;
23907
23908 /* Need full path first (use expand_env() to remove a "~/") */
23909 hasTilde = (**fnamep == '~');
23910 if (hasTilde)
23911 pbuf = tfname = expand_env_save(*fnamep);
23912 else
23913 pbuf = tfname = FullName_save(*fnamep, FALSE);
23914
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023915 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023916
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023917 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23918 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919
23920 if (len == 0)
23921 {
23922 /* Don't have a valid filename, so shorten the rest of the
23923 * path if we can. This CAN give us invalid 8.3 filenames, but
23924 * there's not a lot of point in guessing what it might be.
23925 */
23926 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023927 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 }
23930
23931 /* Count the paths backward to find the beginning of the desired string. */
23932 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023933 {
23934#ifdef FEAT_MBYTE
23935 if (has_mbyte)
23936 p -= mb_head_off(tfname, p);
23937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 if (vim_ispathsep(*p))
23939 {
23940 if (sepcount == 0 || (hasTilde && sepcount == 1))
23941 break;
23942 else
23943 sepcount --;
23944 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023946 if (hasTilde)
23947 {
23948 --p;
23949 if (p >= tfname)
23950 *p = '~';
23951 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023952 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 }
23954 else
23955 ++p;
23956
23957 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23958 vim_free(*bufp);
23959 *fnamelen = (int)STRLEN(p);
23960 *bufp = pbuf;
23961 *fnamep = p;
23962
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023963 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964}
23965#endif /* WIN3264 */
23966
23967/*
23968 * Adjust a filename, according to a string of modifiers.
23969 * *fnamep must be NUL terminated when called. When returning, the length is
23970 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023971 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 * When there is an error, *fnamep is set to NULL.
23973 */
23974 int
23975modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23976 char_u *src; /* string with modifiers */
23977 int *usedlen; /* characters after src that are used */
23978 char_u **fnamep; /* file name so far */
23979 char_u **bufp; /* buffer for allocated file name or NULL */
23980 int *fnamelen; /* length of fnamep */
23981{
23982 int valid = 0;
23983 char_u *tail;
23984 char_u *s, *p, *pbuf;
23985 char_u dirname[MAXPATHL];
23986 int c;
23987 int has_fullname = 0;
23988#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023989 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 int has_shortname = 0;
23991#endif
23992
23993repeat:
23994 /* ":p" - full path/file_name */
23995 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23996 {
23997 has_fullname = 1;
23998
23999 valid |= VALID_PATH;
24000 *usedlen += 2;
24001
24002 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
24003 if ((*fnamep)[0] == '~'
24004#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
24005 && ((*fnamep)[1] == '/'
24006# ifdef BACKSLASH_IN_FILENAME
24007 || (*fnamep)[1] == '\\'
24008# endif
24009 || (*fnamep)[1] == NUL)
24010
24011#endif
24012 )
24013 {
24014 *fnamep = expand_env_save(*fnamep);
24015 vim_free(*bufp); /* free any allocated file name */
24016 *bufp = *fnamep;
24017 if (*fnamep == NULL)
24018 return -1;
24019 }
24020
24021 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024022 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 {
24024 if (vim_ispathsep(*p)
24025 && p[1] == '.'
24026 && (p[2] == NUL
24027 || vim_ispathsep(p[2])
24028 || (p[2] == '.'
24029 && (p[3] == NUL || vim_ispathsep(p[3])))))
24030 break;
24031 }
24032
24033 /* FullName_save() is slow, don't use it when not needed. */
24034 if (*p != NUL || !vim_isAbsName(*fnamep))
24035 {
24036 *fnamep = FullName_save(*fnamep, *p != NUL);
24037 vim_free(*bufp); /* free any allocated file name */
24038 *bufp = *fnamep;
24039 if (*fnamep == NULL)
24040 return -1;
24041 }
24042
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024043#ifdef WIN3264
24044# if _WIN32_WINNT >= 0x0500
24045 if (vim_strchr(*fnamep, '~') != NULL)
24046 {
24047 /* Expand 8.3 filename to full path. Needed to make sure the same
24048 * file does not have two different names.
24049 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24050 p = alloc(_MAX_PATH + 1);
24051 if (p != NULL)
24052 {
24053 if (GetLongPathName(*fnamep, p, MAXPATHL))
24054 {
24055 vim_free(*bufp);
24056 *bufp = *fnamep = p;
24057 }
24058 else
24059 vim_free(p);
24060 }
24061 }
24062# endif
24063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 /* Append a path separator to a directory. */
24065 if (mch_isdir(*fnamep))
24066 {
24067 /* Make room for one or two extra characters. */
24068 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24069 vim_free(*bufp); /* free any allocated file name */
24070 *bufp = *fnamep;
24071 if (*fnamep == NULL)
24072 return -1;
24073 add_pathsep(*fnamep);
24074 }
24075 }
24076
24077 /* ":." - path relative to the current directory */
24078 /* ":~" - path relative to the home directory */
24079 /* ":8" - shortname path - postponed till after */
24080 while (src[*usedlen] == ':'
24081 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24082 {
24083 *usedlen += 2;
24084 if (c == '8')
24085 {
24086#ifdef WIN3264
24087 has_shortname = 1; /* Postpone this. */
24088#endif
24089 continue;
24090 }
24091 pbuf = NULL;
24092 /* Need full path first (use expand_env() to remove a "~/") */
24093 if (!has_fullname)
24094 {
24095 if (c == '.' && **fnamep == '~')
24096 p = pbuf = expand_env_save(*fnamep);
24097 else
24098 p = pbuf = FullName_save(*fnamep, FALSE);
24099 }
24100 else
24101 p = *fnamep;
24102
24103 has_fullname = 0;
24104
24105 if (p != NULL)
24106 {
24107 if (c == '.')
24108 {
24109 mch_dirname(dirname, MAXPATHL);
24110 s = shorten_fname(p, dirname);
24111 if (s != NULL)
24112 {
24113 *fnamep = s;
24114 if (pbuf != NULL)
24115 {
24116 vim_free(*bufp); /* free any allocated file name */
24117 *bufp = pbuf;
24118 pbuf = NULL;
24119 }
24120 }
24121 }
24122 else
24123 {
24124 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24125 /* Only replace it when it starts with '~' */
24126 if (*dirname == '~')
24127 {
24128 s = vim_strsave(dirname);
24129 if (s != NULL)
24130 {
24131 *fnamep = s;
24132 vim_free(*bufp);
24133 *bufp = s;
24134 }
24135 }
24136 }
24137 vim_free(pbuf);
24138 }
24139 }
24140
24141 tail = gettail(*fnamep);
24142 *fnamelen = (int)STRLEN(*fnamep);
24143
24144 /* ":h" - head, remove "/file_name", can be repeated */
24145 /* Don't remove the first "/" or "c:\" */
24146 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24147 {
24148 valid |= VALID_HEAD;
24149 *usedlen += 2;
24150 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024151 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024152 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 *fnamelen = (int)(tail - *fnamep);
24154#ifdef VMS
24155 if (*fnamelen > 0)
24156 *fnamelen += 1; /* the path separator is part of the path */
24157#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024158 if (*fnamelen == 0)
24159 {
24160 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24161 p = vim_strsave((char_u *)".");
24162 if (p == NULL)
24163 return -1;
24164 vim_free(*bufp);
24165 *bufp = *fnamep = tail = p;
24166 *fnamelen = 1;
24167 }
24168 else
24169 {
24170 while (tail > s && !after_pathsep(s, tail))
24171 mb_ptr_back(*fnamep, tail);
24172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 }
24174
24175 /* ":8" - shortname */
24176 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24177 {
24178 *usedlen += 2;
24179#ifdef WIN3264
24180 has_shortname = 1;
24181#endif
24182 }
24183
24184#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024185 /*
24186 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 */
24188 if (has_shortname)
24189 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024190 /* Copy the string if it is shortened by :h and when it wasn't copied
24191 * yet, because we are going to change it in place. Avoids changing
24192 * the buffer name for "%:8". */
24193 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024194 {
24195 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024196 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197 return -1;
24198 vim_free(*bufp);
24199 *bufp = *fnamep = p;
24200 }
24201
24202 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024203 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 if (!has_fullname && !vim_isAbsName(*fnamep))
24205 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024206 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 return -1;
24208 }
24209 else
24210 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024211 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212
Bram Moolenaardc935552011-08-17 15:23:23 +020024213 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024215 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 return -1;
24217
24218 if (l == 0)
24219 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024220 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024221 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024222 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223 return -1;
24224 }
24225 *fnamelen = l;
24226 }
24227 }
24228#endif /* WIN3264 */
24229
24230 /* ":t" - tail, just the basename */
24231 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24232 {
24233 *usedlen += 2;
24234 *fnamelen -= (int)(tail - *fnamep);
24235 *fnamep = tail;
24236 }
24237
24238 /* ":e" - extension, can be repeated */
24239 /* ":r" - root, without extension, can be repeated */
24240 while (src[*usedlen] == ':'
24241 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24242 {
24243 /* find a '.' in the tail:
24244 * - for second :e: before the current fname
24245 * - otherwise: The last '.'
24246 */
24247 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24248 s = *fnamep - 2;
24249 else
24250 s = *fnamep + *fnamelen - 1;
24251 for ( ; s > tail; --s)
24252 if (s[0] == '.')
24253 break;
24254 if (src[*usedlen + 1] == 'e') /* :e */
24255 {
24256 if (s > tail)
24257 {
24258 *fnamelen += (int)(*fnamep - (s + 1));
24259 *fnamep = s + 1;
24260#ifdef VMS
24261 /* cut version from the extension */
24262 s = *fnamep + *fnamelen - 1;
24263 for ( ; s > *fnamep; --s)
24264 if (s[0] == ';')
24265 break;
24266 if (s > *fnamep)
24267 *fnamelen = s - *fnamep;
24268#endif
24269 }
24270 else if (*fnamep <= tail)
24271 *fnamelen = 0;
24272 }
24273 else /* :r */
24274 {
24275 if (s > tail) /* remove one extension */
24276 *fnamelen = (int)(s - *fnamep);
24277 }
24278 *usedlen += 2;
24279 }
24280
24281 /* ":s?pat?foo?" - substitute */
24282 /* ":gs?pat?foo?" - global substitute */
24283 if (src[*usedlen] == ':'
24284 && (src[*usedlen + 1] == 's'
24285 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24286 {
24287 char_u *str;
24288 char_u *pat;
24289 char_u *sub;
24290 int sep;
24291 char_u *flags;
24292 int didit = FALSE;
24293
24294 flags = (char_u *)"";
24295 s = src + *usedlen + 2;
24296 if (src[*usedlen + 1] == 'g')
24297 {
24298 flags = (char_u *)"g";
24299 ++s;
24300 }
24301
24302 sep = *s++;
24303 if (sep)
24304 {
24305 /* find end of pattern */
24306 p = vim_strchr(s, sep);
24307 if (p != NULL)
24308 {
24309 pat = vim_strnsave(s, (int)(p - s));
24310 if (pat != NULL)
24311 {
24312 s = p + 1;
24313 /* find end of substitution */
24314 p = vim_strchr(s, sep);
24315 if (p != NULL)
24316 {
24317 sub = vim_strnsave(s, (int)(p - s));
24318 str = vim_strnsave(*fnamep, *fnamelen);
24319 if (sub != NULL && str != NULL)
24320 {
24321 *usedlen = (int)(p + 1 - src);
24322 s = do_string_sub(str, pat, sub, flags);
24323 if (s != NULL)
24324 {
24325 *fnamep = s;
24326 *fnamelen = (int)STRLEN(s);
24327 vim_free(*bufp);
24328 *bufp = s;
24329 didit = TRUE;
24330 }
24331 }
24332 vim_free(sub);
24333 vim_free(str);
24334 }
24335 vim_free(pat);
24336 }
24337 }
24338 /* after using ":s", repeat all the modifiers */
24339 if (didit)
24340 goto repeat;
24341 }
24342 }
24343
24344 return valid;
24345}
24346
24347/*
24348 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24349 * "flags" can be "g" to do a global substitute.
24350 * Returns an allocated string, NULL for error.
24351 */
24352 char_u *
24353do_string_sub(str, pat, sub, flags)
24354 char_u *str;
24355 char_u *pat;
24356 char_u *sub;
24357 char_u *flags;
24358{
24359 int sublen;
24360 regmatch_T regmatch;
24361 int i;
24362 int do_all;
24363 char_u *tail;
24364 garray_T ga;
24365 char_u *ret;
24366 char_u *save_cpo;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024367 int zero_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368
24369 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24370 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024371 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372
24373 ga_init2(&ga, 1, 200);
24374
24375 do_all = (flags[0] == 'g');
24376
24377 regmatch.rm_ic = p_ic;
24378 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24379 if (regmatch.regprog != NULL)
24380 {
24381 tail = str;
24382 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24383 {
24384 /*
24385 * Get some space for a temporary buffer to do the substitution
24386 * into. It will contain:
24387 * - The text up to where the match is.
24388 * - The substituted text.
24389 * - The text after the match.
24390 */
24391 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24392 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24393 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24394 {
24395 ga_clear(&ga);
24396 break;
24397 }
24398
24399 /* copy the text up to where the match is */
24400 i = (int)(regmatch.startp[0] - tail);
24401 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24402 /* add the substituted text */
24403 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24404 + ga.ga_len + i, TRUE, TRUE, FALSE);
24405 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024406 zero_width = (tail == regmatch.endp[0]
24407 || regmatch.startp[0] == regmatch.endp[0]);
24408 tail = regmatch.endp[0];
24409 if (*tail == NUL)
24410 break;
24411 if (zero_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 {
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024413 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024414 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24415 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 if (!do_all)
24418 break;
24419 }
24420
24421 if (ga.ga_data != NULL)
24422 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24423
Bram Moolenaar473de612013-06-08 18:19:48 +020024424 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024425 }
24426
24427 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24428 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024429 if (p_cpo == empty_option)
24430 p_cpo = save_cpo;
24431 else
24432 /* Darn, evaluating {sub} expression changed the value. */
24433 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024434
24435 return ret;
24436}
24437
24438#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */