blob: 6d6f9b40cdda8ca2e1f4c25ba926628086cdfda1 [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 Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
Bram Moolenaar314f11d2010-08-09 22:07:08 +020025#ifdef VMS
26# include <float.h>
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef MACOS
30# include <time.h> /* for time_t */
31#endif
32
Bram Moolenaar8c8de832008-06-24 22:58:06 +000033#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
34# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
36
Bram Moolenaar33570922005-01-25 22:26:29 +000037#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000039#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
40 be freed. */
41
Bram Moolenaar071d4272004-06-13 20:20:40 +000042/*
Bram Moolenaar33570922005-01-25 22:26:29 +000043 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
44 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000045 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
46 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
47 * HI2DI() converts a hashitem pointer to a dictitem pointer.
48 */
Bram Moolenaar33570922005-01-25 22:26:29 +000049static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000050#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000051#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000052#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000053
54/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000055 * Structure returned by get_lval() and used by set_var_lval().
56 * For a plain name:
57 * "name" points to the variable name.
58 * "exp_name" is NULL.
59 * "tv" is NULL
60 * For a magic braces name:
61 * "name" points to the expanded variable name.
62 * "exp_name" is non-NULL, to be freed later.
63 * "tv" is NULL
64 * For an index in a list:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the (first) list item value
68 * "li" points to the (first) list item
69 * "range", "n1", "n2" and "empty2" indicate what items are used.
70 * For an existing Dict item:
71 * "name" points to the (expanded) variable name.
72 * "exp_name" NULL or non-NULL, to be freed later.
73 * "tv" points to the dict item value
74 * "newkey" is NULL
75 * For a non-existing Dict item:
76 * "name" points to the (expanded) variable name.
77 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000078 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 * "newkey" is the key for the new item.
80 */
81typedef struct lval_S
82{
83 char_u *ll_name; /* start of variable name (can be NULL) */
84 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 isn't NULL it's the Dict to which to add
87 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 listitem_T *ll_li; /* The list item or NULL. */
89 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000090 int ll_range; /* TRUE when a [i:j] range was used */
91 long ll_n1; /* First index for list */
92 long ll_n2; /* Second index for list range */
93 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000094 dict_T *ll_dict; /* The Dictionary or NULL */
95 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000096 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000097} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000098
Bram Moolenaar8c711452005-01-14 21:53:12 +000099
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000100static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000101static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000102static char *e_undefvar = N_("E121: Undefined variable: %s");
103static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000105static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000106static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_listreq = N_("E714: List required");
108static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000109static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000110static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
111static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
112static char *e_funcdict = N_("E717: Dictionary entry already exists");
113static char *e_funcref = N_("E718: Funcref required");
114static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
115static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000116static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000117static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000119/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000120 * All user-defined global variables are stored in dictionary "globvardict".
121 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000123static dict_T globvardict;
124static dictitem_T globvars_var;
125#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128 * Old Vim variables such as "v:version" are also available without the "v:".
129 * Also in functions. We need a special hashtable for them.
130 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000131static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000132
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200133/* When using exists() don't auto-load a script. */
134static int no_autoload = FALSE;
135
Bram Moolenaar532c7802005-01-27 14:44:31 +0000136/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 * When recursively copying lists and dicts we need to remember which ones we
138 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140 */
141static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000142#define COPYID_INC 2
143#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000144
145/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000146 * Array to hold the hashtab with variables local to each sourced script.
147 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000149typedef struct
150{
151 dictitem_T sv_var;
152 dict_T sv_dict;
153} scriptvar_T;
154
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200155static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
156#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
157#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
159static int echo_attr = 0; /* attributes used for ":echo" */
160
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161/* Values for trans_function_name() argument: */
162#define TFN_INT 1 /* internal function name OK */
163#define TFN_QUIET 2 /* no error messages */
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000358 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
375/*
376 * The v: variables are stored in dictionary "vimvardict".
377 * "vimvars_var" is the variable that is used for the "l:" scope.
378 */
379static dict_T vimvardict;
380static dictitem_T vimvars_var;
381#define vimvarht vimvardict.dv_hashtab
382
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
384static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
385#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
386static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
387#endif
388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
404static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
408static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
409static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
410static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
411static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
412static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
413static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
414static void item_lock __ARGS((typval_T *tv, int deep, int lock));
415static int tv_islocked __ARGS((typval_T *tv));
416
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
418static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000423static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
424static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000426static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
429static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
430static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000431static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static listitem_T *listitem_alloc __ARGS((void));
433static void listitem_free __ARGS((listitem_T *item));
434static void listitem_remove __ARGS((list_T *l, listitem_T *item));
435static long list_len __ARGS((list_T *l));
436static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
437static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
438static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000440static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000443static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
445static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
446static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *list2string __ARGS((typval_T *tv, int copyID));
450static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000451static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000452static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
453static void set_ref_in_list __ARGS((list_T *l, int copyID));
454static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200455static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000457static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
459static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000460static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000462static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000464static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
465static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
468static int string2float __ARGS((char_u *text, float_T *value));
469#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
471static int find_internal_func __ARGS((char_u *name));
472static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
473static 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 +0200474static 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 +0000475static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000476static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000477
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
479static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200480static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200488static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200490static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#ifdef FEAT_FLOAT
504static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000506static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000509static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000512static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000513static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000518#ifdef FEAT_FLOAT
519static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200520static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
525static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536#ifdef FEAT_FLOAT
537static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
538#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000541static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#ifdef FEAT_FLOAT
548static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000551#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000552static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000561static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000563static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000569static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000577static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000578static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000579static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000580static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200583static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000584static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000592static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000606static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200625static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000634static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000640#ifdef vim_mkdir
641static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100644#ifdef FEAT_MZSCHEME
645static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000649static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000650#ifdef FEAT_FLOAT
651static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000654static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000655static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000657static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000658static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#ifdef FEAT_FLOAT
671static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
672#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000674static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000676static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000683static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000684static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000685static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000686static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200688static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000689static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000691static void f_shellescape __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));
750static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000756static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000759static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000761static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000762static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static int get_env_len __ARGS((char_u **arg));
764static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000766static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
767#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
768#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
769 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000770static 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 +0000771static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000773static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
774static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static typval_T *alloc_tv __ARGS((void));
776static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void init_tv __ARGS((typval_T *varp));
778static long get_tv_number __ARGS((typval_T *varp));
779static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000780static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static char_u *get_tv_string __ARGS((typval_T *varp));
782static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000783static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000785static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
787static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
788static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000789static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
790static 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 +0000791static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
792static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000793static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000794static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000795static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
797static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
798static int eval_fname_script __ARGS((char_u *p));
799static int eval_fname_sid __ARGS((char_u *p));
800static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static ufunc_T *find_func __ARGS((char_u *name));
802static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000803static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
805static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000806static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
807static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
808static int
809# ifdef __BORLANDC__
810 _RTLENTRYF
811# endif
812 prof_total_cmp __ARGS((const void *s1, const void *s2));
813static int
814# ifdef __BORLANDC__
815 _RTLENTRYF
816# endif
817 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000819static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000820static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000821static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void func_free __ARGS((ufunc_T *fp));
823static void func_unref __ARGS((char_u *name));
824static void func_ref __ARGS((char_u *name));
825static 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 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000842/* Character used as separated in autoload function/variable names. */
843#define AUTOLOAD_CHAR '#'
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
854 init_var_dict(&globvardict, &globvars_var);
855 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000856 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000857 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
859 for (i = 0; i < VV_LEN; ++i)
860 {
861 p = &vimvars[i];
862 STRCPY(p->vv_di.di_key, p->vv_name);
863 if (p->vv_flags & VV_RO)
864 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
865 else if (p->vv_flags & VV_RO_SBX)
866 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
867 else
868 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000869
870 /* add to v: scope dict, unless the value is not always available */
871 if (p->vv_type != VAR_UNKNOWN)
872 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000874 /* add to compat scope dict */
875 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000877 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200878
879#ifdef EBCDIC
880 /*
881 * Sort the function table, to enable binary sort.
882 */
883 sortFunctions();
884#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000885}
886
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000887#if defined(EXITFREE) || defined(PROTO)
888 void
889eval_clear()
890{
891 int i;
892 struct vimvar *p;
893
894 for (i = 0; i < VV_LEN; ++i)
895 {
896 p = &vimvars[i];
897 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000898 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000899 vim_free(p->vv_str);
900 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000901 }
902 else if (p->vv_di.di_tv.v_type == VAR_LIST)
903 {
904 list_unref(p->vv_list);
905 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000906 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907 }
908 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000909 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000910 hash_clear(&compat_hashtab);
911
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000913
914 /* global variables */
915 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000916
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000917 /* autoloaded script names */
918 ga_clear_strings(&ga_loaded);
919
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200920 /* script-local variables */
921 for (i = 1; i <= ga_scripts.ga_len; ++i)
922 {
923 vars_clear(&SCRIPT_VARS(i));
924 vim_free(SCRIPT_SV(i));
925 }
926 ga_clear(&ga_scripts);
927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000928 /* unreferenced lists and dicts */
929 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000930
931 /* functions */
932 free_all_functions();
933 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000934}
935#endif
936
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 * Return the name of the executed function.
939 */
940 char_u *
941func_name(cookie)
942 void *cookie;
943{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000944 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945}
946
947/*
948 * Return the address holding the next breakpoint line for a funccall cookie.
949 */
950 linenr_T *
951func_breakpoint(cookie)
952 void *cookie;
953{
Bram Moolenaar33570922005-01-25 22:26:29 +0000954 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955}
956
957/*
958 * Return the address holding the debug tick for a funccall cookie.
959 */
960 int *
961func_dbg_tick(cookie)
962 void *cookie;
963{
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965}
966
967/*
968 * Return the nesting level for a funccall cookie.
969 */
970 int
971func_level(cookie)
972 void *cookie;
973{
Bram Moolenaar33570922005-01-25 22:26:29 +0000974 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975}
976
977/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000978funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000980/* pointer to list of previously used funccal, still around because some
981 * item in it is still being used. */
982funccall_T *previous_funccal = NULL;
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984/*
985 * Return TRUE when a function was ended by a ":return" command.
986 */
987 int
988current_func_returned()
989{
990 return current_funccal->returned;
991}
992
993
994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 * Set an internal variable to a string value. Creates the variable if it does
996 * not already exist.
997 */
998 void
999set_internal_string_var(name, value)
1000 char_u *name;
1001 char_u *value;
1002{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001003 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001004 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
1006 val = vim_strsave(value);
1007 if (val != NULL)
1008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001013 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 }
1016}
1017
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001019static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020static char_u *redir_endp = NULL;
1021static char_u *redir_varname = NULL;
1022
1023/*
1024 * Start recording command output to a variable
1025 * Returns OK if successfully completed the setup. FAIL otherwise.
1026 */
1027 int
1028var_redir_start(name, append)
1029 char_u *name;
1030 int append; /* append to an existing variable */
1031{
1032 int save_emsg;
1033 int err;
1034 typval_T tv;
1035
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001036 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001037 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 {
1039 EMSG(_(e_invarg));
1040 return FAIL;
1041 }
1042
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001043 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044 redir_varname = vim_strsave(name);
1045 if (redir_varname == NULL)
1046 return FAIL;
1047
1048 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1049 if (redir_lval == NULL)
1050 {
1051 var_redir_stop();
1052 return FAIL;
1053 }
1054
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001055 /* The output is stored in growarray "redir_ga" until redirection ends. */
1056 ga_init2(&redir_ga, (int)sizeof(char), 500);
1057
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001059 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1060 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1062 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001063 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 if (redir_endp != NULL && *redir_endp != NUL)
1065 /* Trailing characters are present after the variable name */
1066 EMSG(_(e_trailing));
1067 else
1068 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001069 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 var_redir_stop();
1071 return FAIL;
1072 }
1073
1074 /* check if we can write to the variable: set it to or append an empty
1075 * string */
1076 save_emsg = did_emsg;
1077 did_emsg = FALSE;
1078 tv.v_type = VAR_STRING;
1079 tv.vval.v_string = (char_u *)"";
1080 if (append)
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1082 else
1083 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001084 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001086 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 if (err)
1088 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001089 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 var_redir_stop();
1091 return FAIL;
1092 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093
1094 return OK;
1095}
1096
1097/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001098 * Append "value[value_len]" to the variable set by var_redir_start().
1099 * The actual appending is postponed until redirection ends, because the value
1100 * appended may in fact be the string we write to, changing it may cause freed
1101 * memory to be used:
1102 * :redir => foo
1103 * :let foo
1104 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 */
1106 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001111 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112
1113 if (redir_lval == NULL)
1114 return;
1115
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 {
1123 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 }
1126 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128}
1129
1130/*
1131 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001132 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 */
1134 void
1135var_redir_stop()
1136{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 typval_T tv;
1138
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 if (redir_lval != NULL)
1140 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 /* If there was no error: assign the text to the variable. */
1142 if (redir_endp != NULL)
1143 {
1144 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1145 tv.v_type = VAR_STRING;
1146 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001147 /* Call get_lval() again, if it's inside a Dict or List it may
1148 * have changed. */
1149 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1150 FALSE, FALSE, FALSE, FNE_CHECK_START);
1151 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1152 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1153 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 /* free the collected output */
1157 vim_free(redir_ga.ga_data);
1158 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001159
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 vim_free(redir_lval);
1161 redir_lval = NULL;
1162 }
1163 vim_free(redir_varname);
1164 redir_varname = NULL;
1165}
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167# if defined(FEAT_MBYTE) || defined(PROTO)
1168 int
1169eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1170 char_u *enc_from;
1171 char_u *enc_to;
1172 char_u *fname_from;
1173 char_u *fname_to;
1174{
1175 int err = FALSE;
1176
1177 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1178 set_vim_var_string(VV_CC_TO, enc_to, -1);
1179 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1180 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1181 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1182 err = TRUE;
1183 set_vim_var_string(VV_CC_FROM, NULL, -1);
1184 set_vim_var_string(VV_CC_TO, NULL, -1);
1185 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1186 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1187
1188 if (err)
1189 return FAIL;
1190 return OK;
1191}
1192# endif
1193
1194# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1195 int
1196eval_printexpr(fname, args)
1197 char_u *fname;
1198 char_u *args;
1199{
1200 int err = FALSE;
1201
1202 set_vim_var_string(VV_FNAME_IN, fname, -1);
1203 set_vim_var_string(VV_CMDARG, args, -1);
1204 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1205 err = TRUE;
1206 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1207 set_vim_var_string(VV_CMDARG, NULL, -1);
1208
1209 if (err)
1210 {
1211 mch_remove(fname);
1212 return FAIL;
1213 }
1214 return OK;
1215}
1216# endif
1217
1218# if defined(FEAT_DIFF) || defined(PROTO)
1219 void
1220eval_diff(origfile, newfile, outfile)
1221 char_u *origfile;
1222 char_u *newfile;
1223 char_u *outfile;
1224{
1225 int err = FALSE;
1226
1227 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1228 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1229 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1230 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1231 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1232 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1233 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1234}
1235
1236 void
1237eval_patch(origfile, difffile, outfile)
1238 char_u *origfile;
1239 char_u *difffile;
1240 char_u *outfile;
1241{
1242 int err;
1243
1244 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1245 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1246 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1247 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1248 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1249 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1250 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1251}
1252# endif
1253
1254/*
1255 * Top level evaluation function, returning a boolean.
1256 * Sets "error" to TRUE if there was an error.
1257 * Return TRUE or FALSE.
1258 */
1259 int
1260eval_to_bool(arg, error, nextcmd, skip)
1261 char_u *arg;
1262 int *error;
1263 char_u **nextcmd;
1264 int skip; /* only parse, don't execute */
1265{
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 int retval = FALSE;
1268
1269 if (skip)
1270 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 else
1274 {
1275 *error = FALSE;
1276 if (!skip)
1277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001278 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281 }
1282 if (skip)
1283 --emsg_skip;
1284
1285 return retval;
1286}
1287
1288/*
1289 * Top level evaluation function, returning a string. If "skip" is TRUE,
1290 * only parsing to "nextcmd" is done, without reporting errors. Return
1291 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1292 */
1293 char_u *
1294eval_to_string_skip(arg, nextcmd, skip)
1295 char_u *arg;
1296 char_u **nextcmd;
1297 int skip; /* only parse, don't execute */
1298{
Bram Moolenaar33570922005-01-25 22:26:29 +00001299 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 char_u *retval;
1301
1302 if (skip)
1303 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 retval = NULL;
1306 else
1307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001308 retval = vim_strsave(get_tv_string(&tv));
1309 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 if (skip)
1312 --emsg_skip;
1313
1314 return retval;
1315}
1316
1317/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001318 * Skip over an expression at "*pp".
1319 * Return FAIL for an error, OK otherwise.
1320 */
1321 int
1322skip_expr(pp)
1323 char_u **pp;
1324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001326
1327 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001328 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329}
1330
1331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001333 * When "convert" is TRUE convert a List into a sequence of lines and convert
1334 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 * Return pointer to allocated memory, or NULL for failure.
1336 */
1337 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 char_u *arg;
1340 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001345 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001347 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 retval = NULL;
1352 else
1353 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001354 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001355 {
1356 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001357 if (tv.vval.v_list != NULL)
1358 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001359 ga_append(&ga, NUL);
1360 retval = (char_u *)ga.ga_data;
1361 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362#ifdef FEAT_FLOAT
1363 else if (convert && tv.v_type == VAR_FLOAT)
1364 {
1365 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1366 retval = vim_strsave(numbuf);
1367 }
1368#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001369 else
1370 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373
1374 return retval;
1375}
1376
1377/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001378 * Call eval_to_string() without using current local variables and using
1379 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 */
1381 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 char_u *arg;
1384 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 char_u *retval;
1388 void *save_funccalp;
1389
1390 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 if (use_sandbox)
1392 ++sandbox;
1393 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001394 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001395 if (use_sandbox)
1396 --sandbox;
1397 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 restore_funccal(save_funccalp);
1399 return retval;
1400}
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402/*
1403 * Top level evaluation function, returning a number.
1404 * Evaluates "expr" silently.
1405 * Returns -1 for an error.
1406 */
1407 int
1408eval_to_number(expr)
1409 char_u *expr;
1410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001413 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
1415 ++emsg_off;
1416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 retval = -1;
1419 else
1420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001421 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 --emsg_off;
1425
1426 return retval;
1427}
1428
Bram Moolenaara40058a2005-07-11 22:42:07 +00001429/*
1430 * Prepare v: variable "idx" to be used.
1431 * Save the current typeval in "save_tv".
1432 * When not used yet add the variable to the v: hashtable.
1433 */
1434 static void
1435prepare_vimvar(idx, save_tv)
1436 int idx;
1437 typval_T *save_tv;
1438{
1439 *save_tv = vimvars[idx].vv_tv;
1440 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1441 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1442}
1443
1444/*
1445 * Restore v: variable "idx" to typeval "save_tv".
1446 * When no longer defined, remove the variable from the v: hashtable.
1447 */
1448 static void
1449restore_vimvar(idx, save_tv)
1450 int idx;
1451 typval_T *save_tv;
1452{
1453 hashitem_T *hi;
1454
Bram Moolenaara40058a2005-07-11 22:42:07 +00001455 vimvars[idx].vv_tv = *save_tv;
1456 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1457 {
1458 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1459 if (HASHITEM_EMPTY(hi))
1460 EMSG2(_(e_intern2), "restore_vimvar()");
1461 else
1462 hash_remove(&vimvarht, hi);
1463 }
1464}
1465
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001466#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001467/*
1468 * Evaluate an expression to a list with suggestions.
1469 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001470 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001471 */
1472 list_T *
1473eval_spell_expr(badword, expr)
1474 char_u *badword;
1475 char_u *expr;
1476{
1477 typval_T save_val;
1478 typval_T rettv;
1479 list_T *list = NULL;
1480 char_u *p = skipwhite(expr);
1481
1482 /* Set "v:val" to the bad word. */
1483 prepare_vimvar(VV_VAL, &save_val);
1484 vimvars[VV_VAL].vv_type = VAR_STRING;
1485 vimvars[VV_VAL].vv_str = badword;
1486 if (p_verbose == 0)
1487 ++emsg_off;
1488
1489 if (eval1(&p, &rettv, TRUE) == OK)
1490 {
1491 if (rettv.v_type != VAR_LIST)
1492 clear_tv(&rettv);
1493 else
1494 list = rettv.vval.v_list;
1495 }
1496
1497 if (p_verbose == 0)
1498 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001499 restore_vimvar(VV_VAL, &save_val);
1500
1501 return list;
1502}
1503
1504/*
1505 * "list" is supposed to contain two items: a word and a number. Return the
1506 * word in "pp" and the number as the return value.
1507 * Return -1 if anything isn't right.
1508 * Used to get the good word and score from the eval_spell_expr() result.
1509 */
1510 int
1511get_spellword(list, pp)
1512 list_T *list;
1513 char_u **pp;
1514{
1515 listitem_T *li;
1516
1517 li = list->lv_first;
1518 if (li == NULL)
1519 return -1;
1520 *pp = get_tv_string(&li->li_tv);
1521
1522 li = li->li_next;
1523 if (li == NULL)
1524 return -1;
1525 return get_tv_number(&li->li_tv);
1526}
1527#endif
1528
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001529/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001530 * Top level evaluation function.
1531 * Returns an allocated typval_T with the result.
1532 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001533 */
1534 typval_T *
1535eval_expr(arg, nextcmd)
1536 char_u *arg;
1537 char_u **nextcmd;
1538{
1539 typval_T *tv;
1540
1541 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 {
1544 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 }
1547
1548 return tv;
1549}
1550
1551
Bram Moolenaar4f688582007-07-24 12:34:30 +00001552#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1553 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001556 * Uses argv[argc] for the function arguments. Only Number and String
1557 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001560 static int
1561call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 char_u *func;
1563 int argc;
1564 char_u **argv;
1565 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 long n;
1570 int len;
1571 int i;
1572 int doesrange;
1573 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001576 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 for (i = 0; i < argc; i++)
1581 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001582 /* Pass a NULL or empty argument as an empty string */
1583 if (argv[i] == NULL || *argv[i] == NUL)
1584 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001585 argvars[i].v_type = VAR_STRING;
1586 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001587 continue;
1588 }
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 /* Recognize a number argument, the others must be strings. */
1591 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1592 if (len != 0 && len == (int)STRLEN(argv[i]))
1593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001594 argvars[i].v_type = VAR_NUMBER;
1595 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 else
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 }
1603
1604 if (safe)
1605 {
1606 save_funccalp = save_funccal();
1607 ++sandbox;
1608 }
1609
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1611 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (safe)
1615 {
1616 --sandbox;
1617 restore_funccal(save_funccalp);
1618 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 vim_free(argvars);
1620
1621 if (ret == FAIL)
1622 clear_tv(rettv);
1623
1624 return ret;
1625}
1626
Bram Moolenaar4f688582007-07-24 12:34:30 +00001627# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001629 * Call vimL function "func" and return the result as a string.
1630 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001631 * Uses argv[argc] for the function arguments.
1632 */
1633 void *
1634call_func_retstr(func, argc, argv, safe)
1635 char_u *func;
1636 int argc;
1637 char_u **argv;
1638 int safe; /* use the sandbox */
1639{
1640 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001641 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642
1643 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1644 return NULL;
1645
1646 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 return retval;
1649}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001650# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651
Bram Moolenaar4f688582007-07-24 12:34:30 +00001652# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001654 * Call vimL function "func" and return the result as a number.
1655 * Returns -1 when calling the function fails.
1656 * Uses argv[argc] for the function arguments.
1657 */
1658 long
1659call_func_retnr(func, argc, argv, safe)
1660 char_u *func;
1661 int argc;
1662 char_u **argv;
1663 int safe; /* use the sandbox */
1664{
1665 typval_T rettv;
1666 long retval;
1667
1668 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1669 return -1;
1670
1671 retval = get_tv_number_chk(&rettv, NULL);
1672 clear_tv(&rettv);
1673 return retval;
1674}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001675# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001676
1677/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001678 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001680 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 */
1682 void *
1683call_func_retlist(func, argc, argv, safe)
1684 char_u *func;
1685 int argc;
1686 char_u **argv;
1687 int safe; /* use the sandbox */
1688{
1689 typval_T rettv;
1690
1691 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1692 return NULL;
1693
1694 if (rettv.v_type != VAR_LIST)
1695 {
1696 clear_tv(&rettv);
1697 return NULL;
1698 }
1699
1700 return rettv.vval.v_list;
1701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#endif
1703
Bram Moolenaar4f688582007-07-24 12:34:30 +00001704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705/*
1706 * Save the current function call pointer, and set it to NULL.
1707 * Used when executing autocommands and for ":source".
1708 */
1709 void *
1710save_funccal()
1711{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001712 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 current_funccal = NULL;
1715 return (void *)fc;
1716}
1717
1718 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001719restore_funccal(vfc)
1720 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001722 funccall_T *fc = (funccall_T *)vfc;
1723
1724 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725}
1726
Bram Moolenaar05159a02005-02-26 23:04:13 +00001727#if defined(FEAT_PROFILE) || defined(PROTO)
1728/*
1729 * Prepare profiling for entering a child or something else that is not
1730 * counted for the script/function itself.
1731 * Should always be called in pair with prof_child_exit().
1732 */
1733 void
1734prof_child_enter(tm)
1735 proftime_T *tm; /* place to store waittime */
1736{
1737 funccall_T *fc = current_funccal;
1738
1739 if (fc != NULL && fc->func->uf_profiling)
1740 profile_start(&fc->prof_child);
1741 script_prof_save(tm);
1742}
1743
1744/*
1745 * Take care of time spent in a child.
1746 * Should always be called after prof_child_enter().
1747 */
1748 void
1749prof_child_exit(tm)
1750 proftime_T *tm; /* where waittime was stored */
1751{
1752 funccall_T *fc = current_funccal;
1753
1754 if (fc != NULL && fc->func->uf_profiling)
1755 {
1756 profile_end(&fc->prof_child);
1757 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1758 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1759 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1760 }
1761 script_prof_restore(tm);
1762}
1763#endif
1764
1765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_FOLDING
1767/*
1768 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1769 * it in "*cp". Doesn't give error messages.
1770 */
1771 int
1772eval_foldexpr(arg, cp)
1773 char_u *arg;
1774 int *cp;
1775{
Bram Moolenaar33570922005-01-25 22:26:29 +00001776 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 int retval;
1778 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001779 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1780 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001783 if (use_sandbox)
1784 ++sandbox;
1785 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 retval = 0;
1789 else
1790 {
1791 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 if (tv.v_type == VAR_NUMBER)
1793 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001794 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval = 0;
1796 else
1797 {
1798 /* If the result is a string, check if there is a non-digit before
1799 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (!VIM_ISDIGIT(*s) && *s != '-')
1802 *cp = *s++;
1803 retval = atol((char *)s);
1804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
1807 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001808 if (use_sandbox)
1809 --sandbox;
1810 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
1812 return retval;
1813}
1814#endif
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 * ":let" list all variable values
1818 * ":let var1 var2" list variable values
1819 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 * ":let var += expr" assignment command.
1821 * ":let var -= expr" assignment command.
1822 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 */
1825 void
1826ex_let(eap)
1827 exarg_T *eap;
1828{
1829 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001831 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 int var_count = 0;
1834 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001836 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001837 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaardb552d602006-03-23 22:59:57 +00001839 argend = skip_var_list(arg, &var_count, &semicolon);
1840 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001842 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1843 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001847 /*
1848 * ":let" without "=": list variables
1849 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 if (*arg == '[')
1851 EMSG(_(e_invarg));
1852 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001854 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001858 list_glob_vars(&first);
1859 list_buf_vars(&first);
1860 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001861#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001863#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_script_vars(&first);
1865 list_func_vars(&first);
1866 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 eap->nextcmd = check_nextcmd(arg);
1869 }
1870 else
1871 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872 op[0] = '=';
1873 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875 {
1876 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1877 op[0] = expr[-1]; /* +=, -= or .= */
1878 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (eap->skip)
1882 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (eap->skip)
1885 {
1886 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 --emsg_skip;
1889 }
1890 else if (i != FAIL)
1891 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 }
1897}
1898
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899/*
1900 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1901 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 * When "nextchars" is not NULL it points to a string with characters that
1903 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1904 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 * Returns OK or FAIL;
1906 */
1907 static int
1908ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1909 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 int copy; /* copy values from "tv", don't move */
1912 int semicolon; /* from skip_var_list() */
1913 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915{
1916 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 listitem_T *item;
1920 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921
1922 if (*arg != '[')
1923 {
1924 /*
1925 * ":let var = expr" or ":for var in list"
1926 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 return FAIL;
1929 return OK;
1930 }
1931
1932 /*
1933 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1934 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001935 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 {
1937 EMSG(_(e_listreq));
1938 return FAIL;
1939 }
1940
1941 i = list_len(l);
1942 if (semicolon == 0 && var_count < i)
1943 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001944 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 return FAIL;
1946 }
1947 if (var_count - semicolon > i)
1948 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001949 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 return FAIL;
1951 }
1952
1953 item = l->lv_first;
1954 while (*arg != ']')
1955 {
1956 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 item = item->li_next;
1959 if (arg == NULL)
1960 return FAIL;
1961
1962 arg = skipwhite(arg);
1963 if (*arg == ';')
1964 {
1965 /* Put the rest of the list (may be empty) in the var after ';'.
1966 * Create a new list for this. */
1967 l = list_alloc();
1968 if (l == NULL)
1969 return FAIL;
1970 while (item != NULL)
1971 {
1972 list_append_tv(l, &item->li_tv);
1973 item = item->li_next;
1974 }
1975
1976 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001977 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 ltv.vval.v_list = l;
1979 l->lv_refcount = 1;
1980
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1982 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 clear_tv(&ltv);
1984 if (arg == NULL)
1985 return FAIL;
1986 break;
1987 }
1988 else if (*arg != ',' && *arg != ']')
1989 {
1990 EMSG2(_(e_intern2), "ex_let_vars()");
1991 return FAIL;
1992 }
1993 }
1994
1995 return OK;
1996}
1997
1998/*
1999 * Skip over assignable variable "var" or list of variables "[var, var]".
2000 * Used for ":let varvar = expr" and ":for varvar in expr".
2001 * For "[var, var]" increment "*var_count" for each variable.
2002 * for "[var, var; var]" set "semicolon".
2003 * Return NULL for an error.
2004 */
2005 static char_u *
2006skip_var_list(arg, var_count, semicolon)
2007 char_u *arg;
2008 int *var_count;
2009 int *semicolon;
2010{
2011 char_u *p, *s;
2012
2013 if (*arg == '[')
2014 {
2015 /* "[var, var]": find the matching ']'. */
2016 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002017 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 {
2019 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2020 s = skip_var_one(p);
2021 if (s == p)
2022 {
2023 EMSG2(_(e_invarg2), p);
2024 return NULL;
2025 }
2026 ++*var_count;
2027
2028 p = skipwhite(s);
2029 if (*p == ']')
2030 break;
2031 else if (*p == ';')
2032 {
2033 if (*semicolon == 1)
2034 {
2035 EMSG(_("Double ; in list of variables"));
2036 return NULL;
2037 }
2038 *semicolon = 1;
2039 }
2040 else if (*p != ',')
2041 {
2042 EMSG2(_(e_invarg2), p);
2043 return NULL;
2044 }
2045 }
2046 return p + 1;
2047 }
2048 else
2049 return skip_var_one(arg);
2050}
2051
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002053 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002054 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 static char_u *
2057skip_var_one(arg)
2058 char_u *arg;
2059{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002060 if (*arg == '@' && arg[1] != NUL)
2061 return arg + 2;
2062 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2063 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064}
2065
Bram Moolenaara7043832005-01-21 11:56:39 +00002066/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002067 * List variables for hashtab "ht" with prefix "prefix".
2068 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002075 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002076{
Bram Moolenaar33570922005-01-25 22:26:29 +00002077 hashitem_T *hi;
2078 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 int todo;
2080
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002081 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002082 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2083 {
2084 if (!HASHITEM_EMPTY(hi))
2085 {
2086 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 di = HI2DI(hi);
2088 if (empty || di->di_tv.v_type != VAR_STRING
2089 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002091 }
2092 }
2093}
2094
2095/*
2096 * List global variables.
2097 */
2098 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002099list_glob_vars(first)
2100 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002101{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002102 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002103}
2104
2105/*
2106 * List buffer variables.
2107 */
2108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_buf_vars(first)
2110 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112 char_u numbuf[NUMBUFLEN];
2113
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2115 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116
2117 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2119 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002120}
2121
2122/*
2123 * List window variables.
2124 */
2125 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126list_win_vars(first)
2127 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002128{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2130 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002131}
2132
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002133#ifdef FEAT_WINDOWS
2134/*
2135 * List tab page variables.
2136 */
2137 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138list_tab_vars(first)
2139 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2142 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002143}
2144#endif
2145
Bram Moolenaara7043832005-01-21 11:56:39 +00002146/*
2147 * List Vim variables.
2148 */
2149 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150list_vim_vars(first)
2151 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154}
2155
2156/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002157 * List script-local variables, if there is a script.
2158 */
2159 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160list_script_vars(first)
2161 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002162{
2163 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2165 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002166}
2167
2168/*
2169 * List function variables, if there is a function.
2170 */
2171 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_func_vars(first)
2173 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002174{
2175 if (current_funccal != NULL)
2176 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002178}
2179
2180/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 * List variables in "arg".
2182 */
2183 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 exarg_T *eap;
2186 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188{
2189 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 char_u *name_start;
2193 char_u *arg_subsc;
2194 char_u *tofree;
2195 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196
2197 while (!ends_excmd(*arg) && !got_int)
2198 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002201 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2203 {
2204 emsg_severe = TRUE;
2205 EMSG(_(e_trailing));
2206 break;
2207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 /* get_name_len() takes care of expanding curly braces */
2212 name_start = name = arg;
2213 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2214 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 /* This is mainly to keep test 49 working: when expanding
2217 * curly braces fails overrule the exception error message. */
2218 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 emsg_severe = TRUE;
2221 EMSG2(_(e_invarg2), arg);
2222 break;
2223 }
2224 error = TRUE;
2225 }
2226 else
2227 {
2228 if (tofree != NULL)
2229 name = tofree;
2230 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 else
2233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 /* handle d.key, l[idx], f(expr) */
2235 arg_subsc = arg;
2236 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002237 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002244 case 'g': list_glob_vars(first); break;
2245 case 'b': list_buf_vars(first); break;
2246 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002247#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002248 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 'v': list_vim_vars(first); break;
2251 case 's': list_script_vars(first); break;
2252 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 default:
2254 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 }
2257 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 {
2259 char_u numbuf[NUMBUFLEN];
2260 char_u *tf;
2261 int c;
2262 char_u *s;
2263
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002264 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 c = *arg;
2266 *arg = NUL;
2267 list_one_var_a((char_u *)"",
2268 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002269 tv.v_type,
2270 s == NULL ? (char_u *)"" : s,
2271 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 *arg = c;
2273 vim_free(tf);
2274 }
2275 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 }
2278 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279
2280 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282
2283 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285
2286 return arg;
2287}
2288
2289/*
2290 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2291 * Returns a pointer to the char just after the var name.
2292 * Returns NULL if there is an error.
2293 */
2294 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002297 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002299 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301{
2302 int c1;
2303 char_u *name;
2304 char_u *p;
2305 char_u *arg_end = NULL;
2306 int len;
2307 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309
2310 /*
2311 * ":let $VAR = expr": Set environment variable.
2312 */
2313 if (*arg == '$')
2314 {
2315 /* Find the end of the name. */
2316 ++arg;
2317 name = arg;
2318 len = get_env_len(&arg);
2319 if (len == 0)
2320 EMSG2(_(e_invarg2), name - 1);
2321 else
2322 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 if (op != NULL && (*op == '+' || *op == '-'))
2324 EMSG2(_(e_letwrong), op);
2325 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002326 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 EMSG(_(e_letunexp));
2328 else
2329 {
2330 c1 = name[len];
2331 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002332 p = get_tv_string_chk(tv);
2333 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334 {
2335 int mustfree = FALSE;
2336 char_u *s = vim_getenv(name, &mustfree);
2337
2338 if (s != NULL)
2339 {
2340 p = tofree = concat_str(s, p);
2341 if (mustfree)
2342 vim_free(s);
2343 }
2344 }
2345 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 if (STRICMP(name, "HOME") == 0)
2349 init_homedir();
2350 else if (didset_vim && STRICMP(name, "VIM") == 0)
2351 didset_vim = FALSE;
2352 else if (didset_vimruntime
2353 && STRICMP(name, "VIMRUNTIME") == 0)
2354 didset_vimruntime = FALSE;
2355 arg_end = arg;
2356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 }
2360 }
2361 }
2362
2363 /*
2364 * ":let &option = expr": Set option value.
2365 * ":let &l:option = expr": Set local option value.
2366 * ":let &g:option = expr": Set global option value.
2367 */
2368 else if (*arg == '&')
2369 {
2370 /* Find the end of the name. */
2371 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 if (p == NULL || (endchars != NULL
2373 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 EMSG(_(e_letunexp));
2375 else
2376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 long n;
2378 int opt_type;
2379 long numval;
2380 char_u *stringval = NULL;
2381 char_u *s;
2382
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 c1 = *p;
2384 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385
2386 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002387 s = get_tv_string_chk(tv); /* != NULL if number or string */
2388 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 {
2390 opt_type = get_option_value(arg, &numval,
2391 &stringval, opt_flags);
2392 if ((opt_type == 1 && *op == '.')
2393 || (opt_type == 0 && *op != '.'))
2394 EMSG2(_(e_letwrong), op);
2395 else
2396 {
2397 if (opt_type == 1) /* number */
2398 {
2399 if (*op == '+')
2400 n = numval + n;
2401 else
2402 n = numval - n;
2403 }
2404 else if (opt_type == 0 && stringval != NULL) /* string */
2405 {
2406 s = concat_str(stringval, s);
2407 vim_free(stringval);
2408 stringval = s;
2409 }
2410 }
2411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002412 if (s != NULL)
2413 {
2414 set_option_value(arg, n, s, opt_flags);
2415 arg_end = p;
2416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 }
2420 }
2421
2422 /*
2423 * ":let @r = expr": Set register contents.
2424 */
2425 else if (*arg == '@')
2426 {
2427 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 if (op != NULL && (*op == '+' || *op == '-'))
2429 EMSG2(_(e_letwrong), op);
2430 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002431 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 EMSG(_(e_letunexp));
2433 else
2434 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002435 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 char_u *s;
2437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 p = get_tv_string_chk(tv);
2439 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002441 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 if (s != NULL)
2443 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002444 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 vim_free(s);
2446 }
2447 }
2448 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 arg_end = arg + 1;
2452 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002453 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 }
2455 }
2456
2457 /*
2458 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002461 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002463 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002465 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2469 EMSG(_(e_letunexp));
2470 else
2471 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 arg_end = p;
2474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478
2479 else
2480 EMSG2(_(e_invarg2), arg);
2481
2482 return arg_end;
2483}
2484
2485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2487 */
2488 static int
2489check_changedtick(arg)
2490 char_u *arg;
2491{
2492 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2493 {
2494 EMSG2(_(e_readonlyvar), arg);
2495 return TRUE;
2496 }
2497 return FALSE;
2498}
2499
2500/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 * Get an lval: variable, Dict item or List item that can be assigned a value
2502 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2503 * "name.key", "name.key[expr]" etc.
2504 * Indexing only works if "name" is an existing List or Dictionary.
2505 * "name" points to the start of the name.
2506 * If "rettv" is not NULL it points to the value to be assigned.
2507 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2508 * wrong; must end in space or cmd separator.
2509 *
2510 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002511 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 */
2514 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 typval_T *rettv;
2518 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 int unlet;
2520 int skip;
2521 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 char_u *expr_start, *expr_end;
2526 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 dictitem_T *v;
2528 typval_T var1;
2529 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002531 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538
2539 if (skip)
2540 {
2541 /* When skipping just find the end of the name. */
2542 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002543 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 }
2545
2546 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002547 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 if (expr_start != NULL)
2549 {
2550 /* Don't expand the name when we already know there is an error. */
2551 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2552 && *p != '[' && *p != '.')
2553 {
2554 EMSG(_(e_trailing));
2555 return NULL;
2556 }
2557
2558 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2559 if (lp->ll_exp_name == NULL)
2560 {
2561 /* Report an invalid expression in braces, unless the
2562 * expression evaluation has been cancelled due to an
2563 * aborting error, an interrupt, or an exception. */
2564 if (!aborting() && !quiet)
2565 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002566 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 EMSG2(_(e_invarg2), name);
2568 return NULL;
2569 }
2570 }
2571 lp->ll_name = lp->ll_exp_name;
2572 }
2573 else
2574 lp->ll_name = name;
2575
2576 /* Without [idx] or .key we are done. */
2577 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2578 return p;
2579
2580 cc = *p;
2581 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002582 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (v == NULL && !quiet)
2584 EMSG2(_(e_undefvar), lp->ll_name);
2585 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 if (v == NULL)
2587 return NULL;
2588
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 /*
2590 * Loop until no more [idx] or .key is following.
2591 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2596 && !(lp->ll_tv->v_type == VAR_DICT
2597 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (!quiet)
2600 EMSG(_("E689: Can only index a List or Dictionary"));
2601 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_("E708: [:] must come last"));
2607 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002609
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 len = -1;
2611 if (*p == '.')
2612 {
2613 key = p + 1;
2614 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2615 ;
2616 if (len == 0)
2617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_(e_emptykey));
2620 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 }
2622 p = key + len;
2623 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002624 else
2625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 if (*p == ':')
2629 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 else
2631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 empty1 = FALSE;
2633 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002635 if (get_tv_string_chk(&var1) == NULL)
2636 {
2637 /* not a number or string */
2638 clear_tv(&var1);
2639 return NULL;
2640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642
2643 /* Optionally get the second index [ :expr]. */
2644 if (*p == ':')
2645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002649 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 if (!empty1)
2651 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (rettv != NULL && (rettv->v_type != VAR_LIST
2655 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!quiet)
2658 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 if (!empty1)
2660 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 }
2663 p = skipwhite(p + 1);
2664 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 else
2667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (!empty1)
2672 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002675 if (get_tv_string_chk(&var2) == NULL)
2676 {
2677 /* not a number or string */
2678 if (!empty1)
2679 clear_tv(&var1);
2680 clear_tv(&var2);
2681 return NULL;
2682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
2692 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 }
2699
2700 /* Skip to past ']'. */
2701 ++p;
2702 }
2703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 {
2706 if (len == -1)
2707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (*key == NUL)
2711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
2713 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
2717 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 lp->ll_list = NULL;
2719 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002720 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002723 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002727 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (len == -1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 }
2732 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (len == -1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 p = NULL;
2740 break;
2741 }
2742 if (len == -1)
2743 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746 else
2747 {
2748 /*
2749 * Get the number and item for the only or first index of the List.
2750 */
2751 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 else
2754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002755 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 clear_tv(&var1);
2757 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002758 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 lp->ll_list = lp->ll_tv->vval.v_list;
2760 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2761 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002763 if (lp->ll_n1 < 0)
2764 {
2765 lp->ll_n1 = 0;
2766 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2767 }
2768 }
2769 if (lp->ll_li == NULL)
2770 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775
2776 /*
2777 * May need to find the item or absolute index for the second
2778 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 * When no index given: "lp->ll_empty2" is TRUE.
2780 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
2793
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2795 if (lp->ll_n1 < 0)
2796 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2797 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002803 }
2804
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 return p;
2806}
2807
2808/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002809 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 */
2811 static void
2812clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002813 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814{
2815 vim_free(lp->ll_exp_name);
2816 vim_free(lp->ll_newkey);
2817}
2818
2819/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002820 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002822 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 */
2824 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002828 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831{
2832 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002833 listitem_T *ri;
2834 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835
2836 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002837 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002839 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 cc = *endp;
2841 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002842 if (op != NULL && *op != '=')
2843 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002844 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002846 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002847 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002848 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 {
2850 if (tv_op(&tv, rettv, op) == OK)
2851 set_var(lp->ll_name, &tv, FALSE);
2852 clear_tv(&tv);
2853 }
2854 }
2855 else
2856 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002858 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002860 else if (tv_check_lock(lp->ll_newkey == NULL
2861 ? lp->ll_tv->v_lock
2862 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2863 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 else if (lp->ll_range)
2865 {
2866 /*
2867 * Assign the List values to the list items.
2868 */
2869 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002870 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 if (op != NULL && *op != '=')
2872 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2873 else
2874 {
2875 clear_tv(&lp->ll_li->li_tv);
2876 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2877 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 ri = ri->li_next;
2879 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2880 break;
2881 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002884 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 ri = NULL;
2887 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002889 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_li = lp->ll_li->li_next;
2891 ++lp->ll_n1;
2892 }
2893 if (ri != NULL)
2894 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 else if (lp->ll_empty2
2896 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 : lp->ll_n1 != lp->ll_n2)
2898 EMSG(_("E711: List value has not enough items"));
2899 }
2900 else
2901 {
2902 /*
2903 * Assign to a List or Dictionary item.
2904 */
2905 if (lp->ll_newkey != NULL)
2906 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 if (op != NULL && *op != '=')
2908 {
2909 EMSG2(_(e_letwrong), op);
2910 return;
2911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002914 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 if (di == NULL)
2916 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002917 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2918 {
2919 vim_free(di);
2920 return;
2921 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 else if (op != NULL && *op != '=')
2925 {
2926 tv_op(lp->ll_tv, rettv, op);
2927 return;
2928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 /*
2933 * Assign the value to the variable or list item.
2934 */
2935 if (copy)
2936 copy_tv(rettv, lp->ll_tv);
2937 else
2938 {
2939 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002940 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942 }
2943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944}
2945
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002946/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2948 * Returns OK or FAIL.
2949 */
2950 static int
2951tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002952 typval_T *tv1;
2953 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 char_u *op;
2955{
2956 long n;
2957 char_u numbuf[NUMBUFLEN];
2958 char_u *s;
2959
2960 /* Can't do anything with a Funcref or a Dict on the right. */
2961 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2962 {
2963 switch (tv1->v_type)
2964 {
2965 case VAR_DICT:
2966 case VAR_FUNC:
2967 break;
2968
2969 case VAR_LIST:
2970 if (*op != '+' || tv2->v_type != VAR_LIST)
2971 break;
2972 /* List += List */
2973 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2974 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2975 return OK;
2976
2977 case VAR_NUMBER:
2978 case VAR_STRING:
2979 if (tv2->v_type == VAR_LIST)
2980 break;
2981 if (*op == '+' || *op == '-')
2982 {
2983 /* nr += nr or nr -= nr*/
2984 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985#ifdef FEAT_FLOAT
2986 if (tv2->v_type == VAR_FLOAT)
2987 {
2988 float_T f = n;
2989
2990 if (*op == '+')
2991 f += tv2->vval.v_float;
2992 else
2993 f -= tv2->vval.v_float;
2994 clear_tv(tv1);
2995 tv1->v_type = VAR_FLOAT;
2996 tv1->vval.v_float = f;
2997 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999#endif
3000 {
3001 if (*op == '+')
3002 n += get_tv_number(tv2);
3003 else
3004 n -= get_tv_number(tv2);
3005 clear_tv(tv1);
3006 tv1->v_type = VAR_NUMBER;
3007 tv1->vval.v_number = n;
3008 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 }
3010 else
3011 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003012 if (tv2->v_type == VAR_FLOAT)
3013 break;
3014
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 /* str .= str */
3016 s = get_tv_string(tv1);
3017 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3018 clear_tv(tv1);
3019 tv1->v_type = VAR_STRING;
3020 tv1->vval.v_string = s;
3021 }
3022 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003023
3024#ifdef FEAT_FLOAT
3025 case VAR_FLOAT:
3026 {
3027 float_T f;
3028
3029 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3030 && tv2->v_type != VAR_NUMBER
3031 && tv2->v_type != VAR_STRING))
3032 break;
3033 if (tv2->v_type == VAR_FLOAT)
3034 f = tv2->vval.v_float;
3035 else
3036 f = get_tv_number(tv2);
3037 if (*op == '+')
3038 tv1->vval.v_float += f;
3039 else
3040 tv1->vval.v_float -= f;
3041 }
3042 return OK;
3043#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 }
3045 }
3046
3047 EMSG2(_(e_letwrong), op);
3048 return FAIL;
3049}
3050
3051/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003052 * Add a watcher to a list.
3053 */
3054 static void
3055list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003056 list_T *l;
3057 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058{
3059 lw->lw_next = l->lv_watch;
3060 l->lv_watch = lw;
3061}
3062
3063/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003064 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003065 * No warning when it isn't found...
3066 */
3067 static void
3068list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003069 list_T *l;
3070 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003071{
Bram Moolenaar33570922005-01-25 22:26:29 +00003072 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003073
3074 lwp = &l->lv_watch;
3075 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3076 {
3077 if (lw == lwrem)
3078 {
3079 *lwp = lw->lw_next;
3080 break;
3081 }
3082 lwp = &lw->lw_next;
3083 }
3084}
3085
3086/*
3087 * Just before removing an item from a list: advance watchers to the next
3088 * item.
3089 */
3090 static void
3091list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003092 list_T *l;
3093 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003094{
Bram Moolenaar33570922005-01-25 22:26:29 +00003095 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096
3097 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3098 if (lw->lw_item == item)
3099 lw->lw_item = item->li_next;
3100}
3101
3102/*
3103 * Evaluate the expression used in a ":for var in expr" command.
3104 * "arg" points to "var".
3105 * Set "*errp" to TRUE for an error, FALSE otherwise;
3106 * Return a pointer that holds the info. Null when there is an error.
3107 */
3108 void *
3109eval_for_line(arg, errp, nextcmdp, skip)
3110 char_u *arg;
3111 int *errp;
3112 char_u **nextcmdp;
3113 int skip;
3114{
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003116 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003117 typval_T tv;
3118 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119
3120 *errp = TRUE; /* default: there is an error */
3121
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123 if (fi == NULL)
3124 return NULL;
3125
3126 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3127 if (expr == NULL)
3128 return fi;
3129
3130 expr = skipwhite(expr);
3131 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3132 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003133 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 return fi;
3135 }
3136
3137 if (skip)
3138 ++emsg_skip;
3139 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3140 {
3141 *errp = FALSE;
3142 if (!skip)
3143 {
3144 l = tv.vval.v_list;
3145 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003146 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003148 clear_tv(&tv);
3149 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 else
3151 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003152 /* No need to increment the refcount, it's already set for the
3153 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154 fi->fi_list = l;
3155 list_add_watch(l, &fi->fi_lw);
3156 fi->fi_lw.lw_item = l->lv_first;
3157 }
3158 }
3159 }
3160 if (skip)
3161 --emsg_skip;
3162
3163 return fi;
3164}
3165
3166/*
3167 * Use the first item in a ":for" list. Advance to the next.
3168 * Assign the values to the variable (list). "arg" points to the first one.
3169 * Return TRUE when a valid item was found, FALSE when at end of list or
3170 * something wrong.
3171 */
3172 int
3173next_for_item(fi_void, arg)
3174 void *fi_void;
3175 char_u *arg;
3176{
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180
3181 item = fi->fi_lw.lw_item;
3182 if (item == NULL)
3183 result = FALSE;
3184 else
3185 {
3186 fi->fi_lw.lw_item = item->li_next;
3187 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3188 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3189 }
3190 return result;
3191}
3192
3193/*
3194 * Free the structure used to store info used by ":for".
3195 */
3196 void
3197free_for_info(fi_void)
3198 void *fi_void;
3199{
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003202 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 list_unref(fi->fi_list);
3206 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207 vim_free(fi);
3208}
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3211
3212 void
3213set_context_for_expression(xp, arg, cmdidx)
3214 expand_T *xp;
3215 char_u *arg;
3216 cmdidx_T cmdidx;
3217{
3218 int got_eq = FALSE;
3219 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 if (cmdidx == CMD_let)
3223 {
3224 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003225 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 {
3227 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003228 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 {
3230 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 if (vim_iswhite(*p))
3233 break;
3234 }
3235 return;
3236 }
3237 }
3238 else
3239 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3240 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 while ((xp->xp_pattern = vim_strpbrk(arg,
3242 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3243 {
3244 c = *xp->xp_pattern;
3245 if (c == '&')
3246 {
3247 c = xp->xp_pattern[1];
3248 if (c == '&')
3249 {
3250 ++xp->xp_pattern;
3251 xp->xp_context = cmdidx != CMD_let || got_eq
3252 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3253 }
3254 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003257 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3258 xp->xp_pattern += 2;
3259
3260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 else if (c == '$')
3263 {
3264 /* environment variable */
3265 xp->xp_context = EXPAND_ENV_VARS;
3266 }
3267 else if (c == '=')
3268 {
3269 got_eq = TRUE;
3270 xp->xp_context = EXPAND_EXPRESSION;
3271 }
3272 else if (c == '<'
3273 && xp->xp_context == EXPAND_FUNCTIONS
3274 && vim_strchr(xp->xp_pattern, '(') == NULL)
3275 {
3276 /* Function name can start with "<SNR>" */
3277 break;
3278 }
3279 else if (cmdidx != CMD_let || got_eq)
3280 {
3281 if (c == '"') /* string */
3282 {
3283 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3284 if (c == '\\' && xp->xp_pattern[1] != NUL)
3285 ++xp->xp_pattern;
3286 xp->xp_context = EXPAND_NOTHING;
3287 }
3288 else if (c == '\'') /* literal string */
3289 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003290 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3292 /* skip */ ;
3293 xp->xp_context = EXPAND_NOTHING;
3294 }
3295 else if (c == '|')
3296 {
3297 if (xp->xp_pattern[1] == '|')
3298 {
3299 ++xp->xp_pattern;
3300 xp->xp_context = EXPAND_EXPRESSION;
3301 }
3302 else
3303 xp->xp_context = EXPAND_COMMANDS;
3304 }
3305 else
3306 xp->xp_context = EXPAND_EXPRESSION;
3307 }
3308 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309 /* Doesn't look like something valid, expand as an expression
3310 * anyway. */
3311 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 arg = xp->xp_pattern;
3313 if (*arg != NUL)
3314 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3315 /* skip */ ;
3316 }
3317 xp->xp_pattern = arg;
3318}
3319
3320#endif /* FEAT_CMDL_COMPL */
3321
3322/*
3323 * ":1,25call func(arg1, arg2)" function call.
3324 */
3325 void
3326ex_call(eap)
3327 exarg_T *eap;
3328{
3329 char_u *arg = eap->arg;
3330 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003332 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003334 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 linenr_T lnum;
3336 int doesrange;
3337 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003338 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003340 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003341 if (fudi.fd_newkey != NULL)
3342 {
3343 /* Still need to give an error message for missing key. */
3344 EMSG2(_(e_dictkey), fudi.fd_newkey);
3345 vim_free(fudi.fd_newkey);
3346 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003347 if (tofree == NULL)
3348 return;
3349
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003350 /* Increase refcount on dictionary, it could get deleted when evaluating
3351 * the arguments. */
3352 if (fudi.fd_dict != NULL)
3353 ++fudi.fd_dict->dv_refcount;
3354
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003355 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003356 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003357 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaar532c7802005-01-27 14:44:31 +00003359 /* Skip white space to allow ":call func ()". Not good, but required for
3360 * backward compatibility. */
3361 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364 if (*startarg != '(')
3365 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003366 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 goto end;
3368 }
3369
3370 /*
3371 * When skipping, evaluate the function once, to find the end of the
3372 * arguments.
3373 * When the function takes a range, this is discovered after the first
3374 * call, and the loop is broken.
3375 */
3376 if (eap->skip)
3377 {
3378 ++emsg_skip;
3379 lnum = eap->line2; /* do it once, also with an invalid range */
3380 }
3381 else
3382 lnum = eap->line1;
3383 for ( ; lnum <= eap->line2; ++lnum)
3384 {
3385 if (!eap->skip && eap->addr_count > 0)
3386 {
3387 curwin->w_cursor.lnum = lnum;
3388 curwin->w_cursor.col = 0;
3389 }
3390 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003391 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003392 eap->line1, eap->line2, &doesrange,
3393 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
3395 failed = TRUE;
3396 break;
3397 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003398
3399 /* Handle a function returning a Funcref, Dictionary or List. */
3400 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3401 {
3402 failed = TRUE;
3403 break;
3404 }
3405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003406 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 if (doesrange || eap->skip)
3408 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003411 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003412 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003413 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (aborting())
3415 break;
3416 }
3417 if (eap->skip)
3418 --emsg_skip;
3419
3420 if (!failed)
3421 {
3422 /* Check for trailing illegal characters and a following command. */
3423 if (!ends_excmd(*arg))
3424 {
3425 emsg_severe = TRUE;
3426 EMSG(_(e_trailing));
3427 }
3428 else
3429 eap->nextcmd = check_nextcmd(arg);
3430 }
3431
3432end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003433 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435}
3436
3437/*
3438 * ":unlet[!] var1 ... " command.
3439 */
3440 void
3441ex_unlet(eap)
3442 exarg_T *eap;
3443{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003444 ex_unletlock(eap, eap->arg, 0);
3445}
3446
3447/*
3448 * ":lockvar" and ":unlockvar" commands
3449 */
3450 void
3451ex_lockvar(eap)
3452 exarg_T *eap;
3453{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003455 int deep = 2;
3456
3457 if (eap->forceit)
3458 deep = -1;
3459 else if (vim_isdigit(*arg))
3460 {
3461 deep = getdigits(&arg);
3462 arg = skipwhite(arg);
3463 }
3464
3465 ex_unletlock(eap, arg, deep);
3466}
3467
3468/*
3469 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3470 */
3471 static void
3472ex_unletlock(eap, argstart, deep)
3473 exarg_T *eap;
3474 char_u *argstart;
3475 int deep;
3476{
3477 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003480 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
3482 do
3483 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003484 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003485 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3486 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003487 if (lv.ll_name == NULL)
3488 error = TRUE; /* error but continue parsing */
3489 if (name_end == NULL || (!vim_iswhite(*name_end)
3490 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003492 if (name_end != NULL)
3493 {
3494 emsg_severe = TRUE;
3495 EMSG(_(e_trailing));
3496 }
3497 if (!(eap->skip || error))
3498 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
3500 }
3501
3502 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003503 {
3504 if (eap->cmdidx == CMD_unlet)
3505 {
3506 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3507 error = TRUE;
3508 }
3509 else
3510 {
3511 if (do_lock_var(&lv, name_end, deep,
3512 eap->cmdidx == CMD_lockvar) == FAIL)
3513 error = TRUE;
3514 }
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003517 if (!eap->skip)
3518 clear_lval(&lv);
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 arg = skipwhite(name_end);
3521 } while (!ends_excmd(*arg));
3522
3523 eap->nextcmd = check_nextcmd(arg);
3524}
3525
Bram Moolenaar8c711452005-01-14 21:53:12 +00003526 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003527do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003528 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003529 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003530 int forceit;
3531{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003532 int ret = OK;
3533 int cc;
3534
3535 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003536 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003537 cc = *name_end;
3538 *name_end = NUL;
3539
3540 /* Normal name or expanded name. */
3541 if (check_changedtick(lp->ll_name))
3542 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003543 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003544 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003545 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003546 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003547 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3548 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003549 else if (lp->ll_range)
3550 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552
3553 /* Delete a range of List items. */
3554 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3555 {
3556 li = lp->ll_li->li_next;
3557 listitem_remove(lp->ll_list, lp->ll_li);
3558 lp->ll_li = li;
3559 ++lp->ll_n1;
3560 }
3561 }
3562 else
3563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 /* unlet a List item. */
3566 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003567 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003569 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003570 }
3571
3572 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573}
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575/*
3576 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 */
3579 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583{
Bram Moolenaar33570922005-01-25 22:26:29 +00003584 hashtab_T *ht;
3585 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003586 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003587 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
Bram Moolenaar33570922005-01-25 22:26:29 +00003589 ht = find_var_ht(name, &varname);
3590 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003592 hi = hash_find(ht, varname);
3593 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003594 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003595 di = HI2DI(hi);
3596 if (var_check_fixed(di->di_flags, name)
3597 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 return FAIL;
3599 delete_var(ht, hi);
3600 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603 if (forceit)
3604 return OK;
3605 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 return FAIL;
3607}
3608
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609/*
3610 * Lock or unlock variable indicated by "lp".
3611 * "deep" is the levels to go (-1 for unlimited);
3612 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3613 */
3614 static int
3615do_lock_var(lp, name_end, deep, lock)
3616 lval_T *lp;
3617 char_u *name_end;
3618 int deep;
3619 int lock;
3620{
3621 int ret = OK;
3622 int cc;
3623 dictitem_T *di;
3624
3625 if (deep == 0) /* nothing to do */
3626 return OK;
3627
3628 if (lp->ll_tv == NULL)
3629 {
3630 cc = *name_end;
3631 *name_end = NUL;
3632
3633 /* Normal name or expanded name. */
3634 if (check_changedtick(lp->ll_name))
3635 ret = FAIL;
3636 else
3637 {
3638 di = find_var(lp->ll_name, NULL);
3639 if (di == NULL)
3640 ret = FAIL;
3641 else
3642 {
3643 if (lock)
3644 di->di_flags |= DI_FLAGS_LOCK;
3645 else
3646 di->di_flags &= ~DI_FLAGS_LOCK;
3647 item_lock(&di->di_tv, deep, lock);
3648 }
3649 }
3650 *name_end = cc;
3651 }
3652 else if (lp->ll_range)
3653 {
3654 listitem_T *li = lp->ll_li;
3655
3656 /* (un)lock a range of List items. */
3657 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3658 {
3659 item_lock(&li->li_tv, deep, lock);
3660 li = li->li_next;
3661 ++lp->ll_n1;
3662 }
3663 }
3664 else if (lp->ll_list != NULL)
3665 /* (un)lock a List item. */
3666 item_lock(&lp->ll_li->li_tv, deep, lock);
3667 else
3668 /* un(lock) a Dictionary item. */
3669 item_lock(&lp->ll_di->di_tv, deep, lock);
3670
3671 return ret;
3672}
3673
3674/*
3675 * Lock or unlock an item. "deep" is nr of levels to go.
3676 */
3677 static void
3678item_lock(tv, deep, lock)
3679 typval_T *tv;
3680 int deep;
3681 int lock;
3682{
3683 static int recurse = 0;
3684 list_T *l;
3685 listitem_T *li;
3686 dict_T *d;
3687 hashitem_T *hi;
3688 int todo;
3689
3690 if (recurse >= DICT_MAXNEST)
3691 {
3692 EMSG(_("E743: variable nested too deep for (un)lock"));
3693 return;
3694 }
3695 if (deep == 0)
3696 return;
3697 ++recurse;
3698
3699 /* lock/unlock the item itself */
3700 if (lock)
3701 tv->v_lock |= VAR_LOCKED;
3702 else
3703 tv->v_lock &= ~VAR_LOCKED;
3704
3705 switch (tv->v_type)
3706 {
3707 case VAR_LIST:
3708 if ((l = tv->vval.v_list) != NULL)
3709 {
3710 if (lock)
3711 l->lv_lock |= VAR_LOCKED;
3712 else
3713 l->lv_lock &= ~VAR_LOCKED;
3714 if (deep < 0 || deep > 1)
3715 /* recursive: lock/unlock the items the List contains */
3716 for (li = l->lv_first; li != NULL; li = li->li_next)
3717 item_lock(&li->li_tv, deep - 1, lock);
3718 }
3719 break;
3720 case VAR_DICT:
3721 if ((d = tv->vval.v_dict) != NULL)
3722 {
3723 if (lock)
3724 d->dv_lock |= VAR_LOCKED;
3725 else
3726 d->dv_lock &= ~VAR_LOCKED;
3727 if (deep < 0 || deep > 1)
3728 {
3729 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003730 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3732 {
3733 if (!HASHITEM_EMPTY(hi))
3734 {
3735 --todo;
3736 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3737 }
3738 }
3739 }
3740 }
3741 }
3742 --recurse;
3743}
3744
Bram Moolenaara40058a2005-07-11 22:42:07 +00003745/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003746 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3747 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003748 */
3749 static int
3750tv_islocked(tv)
3751 typval_T *tv;
3752{
3753 return (tv->v_lock & VAR_LOCKED)
3754 || (tv->v_type == VAR_LIST
3755 && tv->vval.v_list != NULL
3756 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3757 || (tv->v_type == VAR_DICT
3758 && tv->vval.v_dict != NULL
3759 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3760}
3761
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3763/*
3764 * Delete all "menutrans_" variables.
3765 */
3766 void
3767del_menutrans_vars()
3768{
Bram Moolenaar33570922005-01-25 22:26:29 +00003769 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
Bram Moolenaar33570922005-01-25 22:26:29 +00003772 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003773 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003774 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003775 {
3776 if (!HASHITEM_EMPTY(hi))
3777 {
3778 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3780 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 }
3782 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003783 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784}
3785#endif
3786
3787#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3788
3789/*
3790 * Local string buffer for the next two functions to store a variable name
3791 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3792 * get_user_var_name().
3793 */
3794
3795static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3796
3797static char_u *varnamebuf = NULL;
3798static int varnamebuflen = 0;
3799
3800/*
3801 * Function to concatenate a prefix and a variable name.
3802 */
3803 static char_u *
3804cat_prefix_varname(prefix, name)
3805 int prefix;
3806 char_u *name;
3807{
3808 int len;
3809
3810 len = (int)STRLEN(name) + 3;
3811 if (len > varnamebuflen)
3812 {
3813 vim_free(varnamebuf);
3814 len += 10; /* some additional space */
3815 varnamebuf = alloc(len);
3816 if (varnamebuf == NULL)
3817 {
3818 varnamebuflen = 0;
3819 return NULL;
3820 }
3821 varnamebuflen = len;
3822 }
3823 *varnamebuf = prefix;
3824 varnamebuf[1] = ':';
3825 STRCPY(varnamebuf + 2, name);
3826 return varnamebuf;
3827}
3828
3829/*
3830 * Function given to ExpandGeneric() to obtain the list of user defined
3831 * (global/buffer/window/built-in) variable names.
3832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 char_u *
3834get_user_var_name(xp, idx)
3835 expand_T *xp;
3836 int idx;
3837{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003838 static long_u gdone;
3839 static long_u bdone;
3840 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003841#ifdef FEAT_WINDOWS
3842 static long_u tdone;
3843#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003844 static int vidx;
3845 static hashitem_T *hi;
3846 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003849 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003851#ifdef FEAT_WINDOWS
3852 tdone = 0;
3853#endif
3854 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003855
3856 /* Global variables */
3857 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003859 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003861 else
3862 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003863 while (HASHITEM_EMPTY(hi))
3864 ++hi;
3865 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3866 return cat_prefix_varname('g', hi->hi_key);
3867 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003869
3870 /* b: variables */
3871 ht = &curbuf->b_vars.dv_hashtab;
3872 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003874 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003875 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003876 else
3877 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003878 while (HASHITEM_EMPTY(hi))
3879 ++hi;
3880 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003882 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003884 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 return (char_u *)"b:changedtick";
3886 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003887
3888 /* w: variables */
3889 ht = &curwin->w_vars.dv_hashtab;
3890 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003892 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003893 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003894 else
3895 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003896 while (HASHITEM_EMPTY(hi))
3897 ++hi;
3898 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003900
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003901#ifdef FEAT_WINDOWS
3902 /* t: variables */
3903 ht = &curtab->tp_vars.dv_hashtab;
3904 if (tdone < ht->ht_used)
3905 {
3906 if (tdone++ == 0)
3907 hi = ht->ht_array;
3908 else
3909 ++hi;
3910 while (HASHITEM_EMPTY(hi))
3911 ++hi;
3912 return cat_prefix_varname('t', hi->hi_key);
3913 }
3914#endif
3915
Bram Moolenaar33570922005-01-25 22:26:29 +00003916 /* v: variables */
3917 if (vidx < VV_LEN)
3918 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
3920 vim_free(varnamebuf);
3921 varnamebuf = NULL;
3922 varnamebuflen = 0;
3923 return NULL;
3924}
3925
3926#endif /* FEAT_CMDL_COMPL */
3927
3928/*
3929 * types for expressions.
3930 */
3931typedef enum
3932{
3933 TYPE_UNKNOWN = 0
3934 , TYPE_EQUAL /* == */
3935 , TYPE_NEQUAL /* != */
3936 , TYPE_GREATER /* > */
3937 , TYPE_GEQUAL /* >= */
3938 , TYPE_SMALLER /* < */
3939 , TYPE_SEQUAL /* <= */
3940 , TYPE_MATCH /* =~ */
3941 , TYPE_NOMATCH /* !~ */
3942} exptype_T;
3943
3944/*
3945 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003946 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3948 */
3949
3950/*
3951 * Handle zero level expression.
3952 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003954 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 * Return OK or FAIL.
3956 */
3957 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003958eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 char_u **nextcmd;
3962 int evaluate;
3963{
3964 int ret;
3965 char_u *p;
3966
3967 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (ret == FAIL || !ends_excmd(*p))
3970 {
3971 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 /*
3974 * Report the invalid expression unless the expression evaluation has
3975 * been cancelled due to an aborting error, an interrupt, or an
3976 * exception.
3977 */
3978 if (!aborting())
3979 EMSG2(_(e_invexpr2), arg);
3980 ret = FAIL;
3981 }
3982 if (nextcmd != NULL)
3983 *nextcmd = check_nextcmd(p);
3984
3985 return ret;
3986}
3987
3988/*
3989 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003990 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 *
3992 * "arg" must point to the first non-white of the expression.
3993 * "arg" is advanced to the next non-white after the recognized expression.
3994 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003995 * Note: "rettv.v_lock" is not set.
3996 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 * Return OK or FAIL.
3998 */
3999 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004000eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 int evaluate;
4004{
4005 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
4008 /*
4009 * Get the first variable.
4010 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 return FAIL;
4013
4014 if ((*arg)[0] == '?')
4015 {
4016 result = FALSE;
4017 if (evaluate)
4018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004019 int error = FALSE;
4020
4021 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004024 if (error)
4025 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027
4028 /*
4029 * Get the second variable.
4030 */
4031 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 return FAIL;
4034
4035 /*
4036 * Check for the ":".
4037 */
4038 if ((*arg)[0] != ':')
4039 {
4040 EMSG(_("E109: Missing ':' after '?'"));
4041 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 return FAIL;
4044 }
4045
4046 /*
4047 * Get the third variable.
4048 */
4049 *arg = skipwhite(*arg + 1);
4050 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4051 {
4052 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return FAIL;
4055 }
4056 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004057 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059
4060 return OK;
4061}
4062
4063/*
4064 * Handle first level expression:
4065 * expr2 || expr2 || expr2 logical OR
4066 *
4067 * "arg" must point to the first non-white of the expression.
4068 * "arg" is advanced to the next non-white after the recognized expression.
4069 *
4070 * Return OK or FAIL.
4071 */
4072 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 int evaluate;
4077{
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 long result;
4080 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004081 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 /*
4084 * Get the first variable.
4085 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return FAIL;
4088
4089 /*
4090 * Repeat until there is no following "||".
4091 */
4092 first = TRUE;
4093 result = FALSE;
4094 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4095 {
4096 if (evaluate && first)
4097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004098 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004101 if (error)
4102 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 first = FALSE;
4104 }
4105
4106 /*
4107 * Get the second variable.
4108 */
4109 *arg = skipwhite(*arg + 2);
4110 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4111 return FAIL;
4112
4113 /*
4114 * Compute the result.
4115 */
4116 if (evaluate && !result)
4117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004118 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004121 if (error)
4122 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 if (evaluate)
4125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 rettv->v_type = VAR_NUMBER;
4127 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 }
4130
4131 return OK;
4132}
4133
4134/*
4135 * Handle second level expression:
4136 * expr3 && expr3 && expr3 logical AND
4137 *
4138 * "arg" must point to the first non-white of the expression.
4139 * "arg" is advanced to the next non-white after the recognized expression.
4140 *
4141 * Return OK or FAIL.
4142 */
4143 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 int evaluate;
4148{
Bram Moolenaar33570922005-01-25 22:26:29 +00004149 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 long result;
4151 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154 /*
4155 * Get the first variable.
4156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 /*
4161 * Repeat until there is no following "&&".
4162 */
4163 first = TRUE;
4164 result = TRUE;
4165 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4166 {
4167 if (evaluate && first)
4168 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 if (error)
4173 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 first = FALSE;
4175 }
4176
4177 /*
4178 * Get the second variable.
4179 */
4180 *arg = skipwhite(*arg + 2);
4181 if (eval4(arg, &var2, evaluate && result) == FAIL)
4182 return FAIL;
4183
4184 /*
4185 * Compute the result.
4186 */
4187 if (evaluate && result)
4188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 if (error)
4193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 if (evaluate)
4196 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 rettv->v_type = VAR_NUMBER;
4198 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 }
4201
4202 return OK;
4203}
4204
4205/*
4206 * Handle third level expression:
4207 * var1 == var2
4208 * var1 =~ var2
4209 * var1 != var2
4210 * var1 !~ var2
4211 * var1 > var2
4212 * var1 >= var2
4213 * var1 < var2
4214 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004215 * var1 is var2
4216 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
4221 * Return OK or FAIL.
4222 */
4223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int evaluate;
4228{
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 char_u *p;
4231 int i;
4232 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004233 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int len = 2;
4235 long n1, n2;
4236 char_u *s1, *s2;
4237 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4238 regmatch_T regmatch;
4239 int ic;
4240 char_u *save_cpo;
4241
4242 /*
4243 * Get the first variable.
4244 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return FAIL;
4247
4248 p = *arg;
4249 switch (p[0])
4250 {
4251 case '=': if (p[1] == '=')
4252 type = TYPE_EQUAL;
4253 else if (p[1] == '~')
4254 type = TYPE_MATCH;
4255 break;
4256 case '!': if (p[1] == '=')
4257 type = TYPE_NEQUAL;
4258 else if (p[1] == '~')
4259 type = TYPE_NOMATCH;
4260 break;
4261 case '>': if (p[1] != '=')
4262 {
4263 type = TYPE_GREATER;
4264 len = 1;
4265 }
4266 else
4267 type = TYPE_GEQUAL;
4268 break;
4269 case '<': if (p[1] != '=')
4270 {
4271 type = TYPE_SMALLER;
4272 len = 1;
4273 }
4274 else
4275 type = TYPE_SEQUAL;
4276 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004277 case 'i': if (p[1] == 's')
4278 {
4279 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4280 len = 5;
4281 if (!vim_isIDc(p[len]))
4282 {
4283 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4284 type_is = TRUE;
4285 }
4286 }
4287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289
4290 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004291 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 */
4293 if (type != TYPE_UNKNOWN)
4294 {
4295 /* extra question mark appended: ignore case */
4296 if (p[len] == '?')
4297 {
4298 ic = TRUE;
4299 ++len;
4300 }
4301 /* extra '#' appended: match case */
4302 else if (p[len] == '#')
4303 {
4304 ic = FALSE;
4305 ++len;
4306 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004307 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 else
4309 ic = p_ic;
4310
4311 /*
4312 * Get the second variable.
4313 */
4314 *arg = skipwhite(p + len);
4315 if (eval5(arg, &var2, evaluate) == FAIL)
4316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004317 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return FAIL;
4319 }
4320
4321 if (evaluate)
4322 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004323 if (type_is && rettv->v_type != var2.v_type)
4324 {
4325 /* For "is" a different type always means FALSE, for "notis"
4326 * it means TRUE. */
4327 n1 = (type == TYPE_NEQUAL);
4328 }
4329 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4330 {
4331 if (type_is)
4332 {
4333 n1 = (rettv->v_type == var2.v_type
4334 && rettv->vval.v_list == var2.vval.v_list);
4335 if (type == TYPE_NEQUAL)
4336 n1 = !n1;
4337 }
4338 else if (rettv->v_type != var2.v_type
4339 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4340 {
4341 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004342 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004343 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004344 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004345 clear_tv(rettv);
4346 clear_tv(&var2);
4347 return FAIL;
4348 }
4349 else
4350 {
4351 /* Compare two Lists for being equal or unequal. */
4352 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4353 if (type == TYPE_NEQUAL)
4354 n1 = !n1;
4355 }
4356 }
4357
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004358 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4359 {
4360 if (type_is)
4361 {
4362 n1 = (rettv->v_type == var2.v_type
4363 && rettv->vval.v_dict == var2.vval.v_dict);
4364 if (type == TYPE_NEQUAL)
4365 n1 = !n1;
4366 }
4367 else if (rettv->v_type != var2.v_type
4368 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4369 {
4370 if (rettv->v_type != var2.v_type)
4371 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4372 else
4373 EMSG(_("E736: Invalid operation for Dictionary"));
4374 clear_tv(rettv);
4375 clear_tv(&var2);
4376 return FAIL;
4377 }
4378 else
4379 {
4380 /* Compare two Dictionaries for being equal or unequal. */
4381 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4382 if (type == TYPE_NEQUAL)
4383 n1 = !n1;
4384 }
4385 }
4386
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4388 {
4389 if (rettv->v_type != var2.v_type
4390 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4391 {
4392 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004393 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004394 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004395 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 clear_tv(rettv);
4397 clear_tv(&var2);
4398 return FAIL;
4399 }
4400 else
4401 {
4402 /* Compare two Funcrefs for being equal or unequal. */
4403 if (rettv->vval.v_string == NULL
4404 || var2.vval.v_string == NULL)
4405 n1 = FALSE;
4406 else
4407 n1 = STRCMP(rettv->vval.v_string,
4408 var2.vval.v_string) == 0;
4409 if (type == TYPE_NEQUAL)
4410 n1 = !n1;
4411 }
4412 }
4413
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004414#ifdef FEAT_FLOAT
4415 /*
4416 * If one of the two variables is a float, compare as a float.
4417 * When using "=~" or "!~", always compare as string.
4418 */
4419 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4420 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4421 {
4422 float_T f1, f2;
4423
4424 if (rettv->v_type == VAR_FLOAT)
4425 f1 = rettv->vval.v_float;
4426 else
4427 f1 = get_tv_number(rettv);
4428 if (var2.v_type == VAR_FLOAT)
4429 f2 = var2.vval.v_float;
4430 else
4431 f2 = get_tv_number(&var2);
4432 n1 = FALSE;
4433 switch (type)
4434 {
4435 case TYPE_EQUAL: n1 = (f1 == f2); break;
4436 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4437 case TYPE_GREATER: n1 = (f1 > f2); break;
4438 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4439 case TYPE_SMALLER: n1 = (f1 < f2); break;
4440 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4441 case TYPE_UNKNOWN:
4442 case TYPE_MATCH:
4443 case TYPE_NOMATCH: break; /* avoid gcc warning */
4444 }
4445 }
4446#endif
4447
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 /*
4449 * If one of the two variables is a number, compare as a number.
4450 * When using "=~" or "!~", always compare as string.
4451 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004452 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 n1 = get_tv_number(rettv);
4456 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 switch (type)
4458 {
4459 case TYPE_EQUAL: n1 = (n1 == n2); break;
4460 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4461 case TYPE_GREATER: n1 = (n1 > n2); break;
4462 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4463 case TYPE_SMALLER: n1 = (n1 < n2); break;
4464 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4465 case TYPE_UNKNOWN:
4466 case TYPE_MATCH:
4467 case TYPE_NOMATCH: break; /* avoid gcc warning */
4468 }
4469 }
4470 else
4471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004472 s1 = get_tv_string_buf(rettv, buf1);
4473 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4475 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4476 else
4477 i = 0;
4478 n1 = FALSE;
4479 switch (type)
4480 {
4481 case TYPE_EQUAL: n1 = (i == 0); break;
4482 case TYPE_NEQUAL: n1 = (i != 0); break;
4483 case TYPE_GREATER: n1 = (i > 0); break;
4484 case TYPE_GEQUAL: n1 = (i >= 0); break;
4485 case TYPE_SMALLER: n1 = (i < 0); break;
4486 case TYPE_SEQUAL: n1 = (i <= 0); break;
4487
4488 case TYPE_MATCH:
4489 case TYPE_NOMATCH:
4490 /* avoid 'l' flag in 'cpoptions' */
4491 save_cpo = p_cpo;
4492 p_cpo = (char_u *)"";
4493 regmatch.regprog = vim_regcomp(s2,
4494 RE_MAGIC + RE_STRING);
4495 regmatch.rm_ic = ic;
4496 if (regmatch.regprog != NULL)
4497 {
4498 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4499 vim_free(regmatch.regprog);
4500 if (type == TYPE_NOMATCH)
4501 n1 = !n1;
4502 }
4503 p_cpo = save_cpo;
4504 break;
4505
4506 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4507 }
4508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509 clear_tv(rettv);
4510 clear_tv(&var2);
4511 rettv->v_type = VAR_NUMBER;
4512 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514 }
4515
4516 return OK;
4517}
4518
4519/*
4520 * Handle fourth level expression:
4521 * + number addition
4522 * - number subtraction
4523 * . string concatenation
4524 *
4525 * "arg" must point to the first non-white of the expression.
4526 * "arg" is advanced to the next non-white after the recognized expression.
4527 *
4528 * Return OK or FAIL.
4529 */
4530 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 int evaluate;
4535{
Bram Moolenaar33570922005-01-25 22:26:29 +00004536 typval_T var2;
4537 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 int op;
4539 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004540#ifdef FEAT_FLOAT
4541 float_T f1 = 0, f2 = 0;
4542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 char_u *s1, *s2;
4544 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4545 char_u *p;
4546
4547 /*
4548 * Get the first variable.
4549 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004550 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 return FAIL;
4552
4553 /*
4554 * Repeat computing, until no '+', '-' or '.' is following.
4555 */
4556 for (;;)
4557 {
4558 op = **arg;
4559 if (op != '+' && op != '-' && op != '.')
4560 break;
4561
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004562 if ((op != '+' || rettv->v_type != VAR_LIST)
4563#ifdef FEAT_FLOAT
4564 && (op == '.' || rettv->v_type != VAR_FLOAT)
4565#endif
4566 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 {
4568 /* For "list + ...", an illegal use of the first operand as
4569 * a number cannot be determined before evaluating the 2nd
4570 * operand: if this is also a list, all is ok.
4571 * For "something . ...", "something - ..." or "non-list + ...",
4572 * we know that the first operand needs to be a string or number
4573 * without evaluating the 2nd operand. So check before to avoid
4574 * side effects after an error. */
4575 if (evaluate && get_tv_string_chk(rettv) == NULL)
4576 {
4577 clear_tv(rettv);
4578 return FAIL;
4579 }
4580 }
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 /*
4583 * Get the second variable.
4584 */
4585 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004586 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 return FAIL;
4590 }
4591
4592 if (evaluate)
4593 {
4594 /*
4595 * Compute the result.
4596 */
4597 if (op == '.')
4598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004599 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4600 s2 = get_tv_string_buf_chk(&var2, buf2);
4601 if (s2 == NULL) /* type error ? */
4602 {
4603 clear_tv(rettv);
4604 clear_tv(&var2);
4605 return FAIL;
4606 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004607 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 clear_tv(rettv);
4609 rettv->v_type = VAR_STRING;
4610 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004612 else if (op == '+' && rettv->v_type == VAR_LIST
4613 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004614 {
4615 /* concatenate Lists */
4616 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4617 &var3) == FAIL)
4618 {
4619 clear_tv(rettv);
4620 clear_tv(&var2);
4621 return FAIL;
4622 }
4623 clear_tv(rettv);
4624 *rettv = var3;
4625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 else
4627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004628 int error = FALSE;
4629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630#ifdef FEAT_FLOAT
4631 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633 f1 = rettv->vval.v_float;
4634 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004635 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004636 else
4637#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004639 n1 = get_tv_number_chk(rettv, &error);
4640 if (error)
4641 {
4642 /* This can only happen for "list + non-list". For
4643 * "non-list + ..." or "something - ...", we returned
4644 * before evaluating the 2nd operand. */
4645 clear_tv(rettv);
4646 return FAIL;
4647 }
4648#ifdef FEAT_FLOAT
4649 if (var2.v_type == VAR_FLOAT)
4650 f1 = n1;
4651#endif
4652 }
4653#ifdef FEAT_FLOAT
4654 if (var2.v_type == VAR_FLOAT)
4655 {
4656 f2 = var2.vval.v_float;
4657 n2 = 0;
4658 }
4659 else
4660#endif
4661 {
4662 n2 = get_tv_number_chk(&var2, &error);
4663 if (error)
4664 {
4665 clear_tv(rettv);
4666 clear_tv(&var2);
4667 return FAIL;
4668 }
4669#ifdef FEAT_FLOAT
4670 if (rettv->v_type == VAR_FLOAT)
4671 f2 = n2;
4672#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004675
4676#ifdef FEAT_FLOAT
4677 /* If there is a float on either side the result is a float. */
4678 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4679 {
4680 if (op == '+')
4681 f1 = f1 + f2;
4682 else
4683 f1 = f1 - f2;
4684 rettv->v_type = VAR_FLOAT;
4685 rettv->vval.v_float = f1;
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004688#endif
4689 {
4690 if (op == '+')
4691 n1 = n1 + n2;
4692 else
4693 n1 = n1 - n2;
4694 rettv->v_type = VAR_NUMBER;
4695 rettv->vval.v_number = n1;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700 }
4701 return OK;
4702}
4703
4704/*
4705 * Handle fifth level expression:
4706 * * number multiplication
4707 * / number division
4708 * % number modulo
4709 *
4710 * "arg" must point to the first non-white of the expression.
4711 * "arg" is advanced to the next non-white after the recognized expression.
4712 *
4713 * Return OK or FAIL.
4714 */
4715 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004716eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004720 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
Bram Moolenaar33570922005-01-25 22:26:29 +00004722 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 int op;
4724 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725#ifdef FEAT_FLOAT
4726 int use_float = FALSE;
4727 float_T f1 = 0, f2;
4728#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
4731 /*
4732 * Get the first variable.
4733 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004734 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 return FAIL;
4736
4737 /*
4738 * Repeat computing, until no '*', '/' or '%' is following.
4739 */
4740 for (;;)
4741 {
4742 op = **arg;
4743 if (op != '*' && op != '/' && op != '%')
4744 break;
4745
4746 if (evaluate)
4747 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004748#ifdef FEAT_FLOAT
4749 if (rettv->v_type == VAR_FLOAT)
4750 {
4751 f1 = rettv->vval.v_float;
4752 use_float = TRUE;
4753 n1 = 0;
4754 }
4755 else
4756#endif
4757 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 if (error)
4760 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762 else
4763 n1 = 0;
4764
4765 /*
4766 * Get the second variable.
4767 */
4768 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004769 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 return FAIL;
4771
4772 if (evaluate)
4773 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774#ifdef FEAT_FLOAT
4775 if (var2.v_type == VAR_FLOAT)
4776 {
4777 if (!use_float)
4778 {
4779 f1 = n1;
4780 use_float = TRUE;
4781 }
4782 f2 = var2.vval.v_float;
4783 n2 = 0;
4784 }
4785 else
4786#endif
4787 {
4788 n2 = get_tv_number_chk(&var2, &error);
4789 clear_tv(&var2);
4790 if (error)
4791 return FAIL;
4792#ifdef FEAT_FLOAT
4793 if (use_float)
4794 f2 = n2;
4795#endif
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
4798 /*
4799 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802#ifdef FEAT_FLOAT
4803 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 if (op == '*')
4806 f1 = f1 * f2;
4807 else if (op == '/')
4808 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004809# ifdef VMS
4810 /* VMS crashes on divide by zero, work around it */
4811 if (f2 == 0.0)
4812 {
4813 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004814 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004815 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004816 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004817 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004818 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004819 }
4820 else
4821 f1 = f1 / f2;
4822# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004823 /* We rely on the floating point library to handle divide
4824 * by zero to result in "inf" and not a crash. */
4825 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004826# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004829 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004830 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831 return FAIL;
4832 }
4833 rettv->v_type = VAR_FLOAT;
4834 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839 if (op == '*')
4840 n1 = n1 * n2;
4841 else if (op == '/')
4842 {
4843 if (n2 == 0) /* give an error message? */
4844 {
4845 if (n1 == 0)
4846 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4847 else if (n1 < 0)
4848 n1 = -0x7fffffffL;
4849 else
4850 n1 = 0x7fffffffL;
4851 }
4852 else
4853 n1 = n1 / n2;
4854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856 {
4857 if (n2 == 0) /* give an error message? */
4858 n1 = 0;
4859 else
4860 n1 = n1 % n2;
4861 }
4862 rettv->v_type = VAR_NUMBER;
4863 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 }
4867
4868 return OK;
4869}
4870
4871/*
4872 * Handle sixth level expression:
4873 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004874 * "string" string constant
4875 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 * &option-name option value
4877 * @r register contents
4878 * identifier variable value
4879 * function() function call
4880 * $VAR environment variable
4881 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004882 * [expr, expr] List
4883 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 *
4885 * Also handle:
4886 * ! in front logical NOT
4887 * - in front unary minus
4888 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004889 * trailing [] subscript in String or List
4890 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 *
4892 * "arg" must point to the first non-white of the expression.
4893 * "arg" is advanced to the next non-white after the recognized expression.
4894 *
4895 * Return OK or FAIL.
4896 */
4897 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004898eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004902 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 long n;
4905 int len;
4906 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 char_u *start_leader, *end_leader;
4908 int ret = OK;
4909 char_u *alias;
4910
4911 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 /*
4918 * Skip '!' and '-' characters. They are handled later.
4919 */
4920 start_leader = *arg;
4921 while (**arg == '!' || **arg == '-' || **arg == '+')
4922 *arg = skipwhite(*arg + 1);
4923 end_leader = *arg;
4924
4925 switch (**arg)
4926 {
4927 /*
4928 * Number constant.
4929 */
4930 case '0':
4931 case '1':
4932 case '2':
4933 case '3':
4934 case '4':
4935 case '5':
4936 case '6':
4937 case '7':
4938 case '8':
4939 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940 {
4941#ifdef FEAT_FLOAT
4942 char_u *p = skipdigits(*arg + 1);
4943 int get_float = FALSE;
4944
4945 /* We accept a float when the format matches
4946 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004947 * strict to avoid backwards compatibility problems.
4948 * Don't look for a float after the "." operator, so that
4949 * ":let vers = 1.2.3" doesn't fail. */
4950 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952 get_float = TRUE;
4953 p = skipdigits(p + 2);
4954 if (*p == 'e' || *p == 'E')
4955 {
4956 ++p;
4957 if (*p == '-' || *p == '+')
4958 ++p;
4959 if (!vim_isdigit(*p))
4960 get_float = FALSE;
4961 else
4962 p = skipdigits(p + 1);
4963 }
4964 if (ASCII_ISALPHA(*p) || *p == '.')
4965 get_float = FALSE;
4966 }
4967 if (get_float)
4968 {
4969 float_T f;
4970
4971 *arg += string2float(*arg, &f);
4972 if (evaluate)
4973 {
4974 rettv->v_type = VAR_FLOAT;
4975 rettv->vval.v_float = f;
4976 }
4977 }
4978 else
4979#endif
4980 {
4981 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4982 *arg += len;
4983 if (evaluate)
4984 {
4985 rettv->v_type = VAR_NUMBER;
4986 rettv->vval.v_number = n;
4987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
4992 /*
4993 * String constant: "string".
4994 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 break;
4997
4998 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002 break;
5003
5004 /*
5005 * List: [expr, expr]
5006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 break;
5009
5010 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 * Dictionary: {key: val, key: val}
5012 */
5013 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5014 break;
5015
5016 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005017 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005019 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 break;
5021
5022 /*
5023 * Environment variable: $VAR.
5024 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 break;
5027
5028 /*
5029 * Register contents: @r.
5030 */
5031 case '@': ++*arg;
5032 if (evaluate)
5033 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005034 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005035 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 if (**arg != NUL)
5038 ++*arg;
5039 break;
5040
5041 /*
5042 * nested expression: (expression).
5043 */
5044 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005045 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (**arg == ')')
5047 ++*arg;
5048 else if (ret == OK)
5049 {
5050 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 ret = FAIL;
5053 }
5054 break;
5055
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 break;
5058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059
5060 if (ret == NOTDONE)
5061 {
5062 /*
5063 * Must be a variable or function name.
5064 * Can also be a curly-braces kind of name: {expr}.
5065 */
5066 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005067 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 if (alias != NULL)
5069 s = alias;
5070
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005071 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 ret = FAIL;
5073 else
5074 {
5075 if (**arg == '(') /* recursive! */
5076 {
5077 /* If "s" is the name of a variable of type VAR_FUNC
5078 * use its contents. */
5079 s = deref_func_name(s, &len);
5080
5081 /* Invoke the function. */
5082 ret = get_func_tv(s, len, rettv, arg,
5083 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005084 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 /* Stop the expression evaluation when immediately
5086 * aborting on error, or when an interrupt occurred or
5087 * an exception was thrown but not caught. */
5088 if (aborting())
5089 {
5090 if (ret == OK)
5091 clear_tv(rettv);
5092 ret = FAIL;
5093 }
5094 }
5095 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005096 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005097 else
5098 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 }
5100
5101 if (alias != NULL)
5102 vim_free(alias);
5103 }
5104
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 *arg = skipwhite(*arg);
5106
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005107 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5108 * expr(expr). */
5109 if (ret == OK)
5110 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112 /*
5113 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5114 */
5115 if (ret == OK && evaluate && end_leader > start_leader)
5116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005117 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 int val = 0;
5119#ifdef FEAT_FLOAT
5120 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005121
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005122 if (rettv->v_type == VAR_FLOAT)
5123 f = rettv->vval.v_float;
5124 else
5125#endif
5126 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005127 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005129 clear_tv(rettv);
5130 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005132 else
5133 {
5134 while (end_leader > start_leader)
5135 {
5136 --end_leader;
5137 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 {
5139#ifdef FEAT_FLOAT
5140 if (rettv->v_type == VAR_FLOAT)
5141 f = !f;
5142 else
5143#endif
5144 val = !val;
5145 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005146 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 {
5148#ifdef FEAT_FLOAT
5149 if (rettv->v_type == VAR_FLOAT)
5150 f = -f;
5151 else
5152#endif
5153 val = -val;
5154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005155 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005156#ifdef FEAT_FLOAT
5157 if (rettv->v_type == VAR_FLOAT)
5158 {
5159 clear_tv(rettv);
5160 rettv->vval.v_float = f;
5161 }
5162 else
5163#endif
5164 {
5165 clear_tv(rettv);
5166 rettv->v_type = VAR_NUMBER;
5167 rettv->vval.v_number = val;
5168 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171
5172 return ret;
5173}
5174
5175/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005176 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5177 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5179 */
5180 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005182 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005183 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005184 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005185 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186{
5187 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005190 long len = -1;
5191 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005192 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005195 if (rettv->v_type == VAR_FUNC
5196#ifdef FEAT_FLOAT
5197 || rettv->v_type == VAR_FLOAT
5198#endif
5199 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005200 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005201 if (verbose)
5202 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203 return FAIL;
5204 }
5205
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 /*
5209 * dict.name
5210 */
5211 key = *arg + 1;
5212 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5213 ;
5214 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217 }
5218 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 /*
5221 * something[idx]
5222 *
5223 * Get the (first) variable from inside the [].
5224 */
5225 *arg = skipwhite(*arg + 1);
5226 if (**arg == ':')
5227 empty1 = TRUE;
5228 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5229 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5231 {
5232 /* not a number or string */
5233 clear_tv(&var1);
5234 return FAIL;
5235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236
5237 /*
5238 * Get the second variable from inside the [:].
5239 */
5240 if (**arg == ':')
5241 {
5242 range = TRUE;
5243 *arg = skipwhite(*arg + 1);
5244 if (**arg == ']')
5245 empty2 = TRUE;
5246 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 if (!empty1)
5249 clear_tv(&var1);
5250 return FAIL;
5251 }
5252 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5253 {
5254 /* not a number or string */
5255 if (!empty1)
5256 clear_tv(&var1);
5257 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 return FAIL;
5259 }
5260 }
5261
5262 /* Check for the ']'. */
5263 if (**arg != ']')
5264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 if (verbose)
5266 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 clear_tv(&var1);
5268 if (range)
5269 clear_tv(&var2);
5270 return FAIL;
5271 }
5272 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 }
5274
5275 if (evaluate)
5276 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 n1 = 0;
5278 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005280 n1 = get_tv_number(&var1);
5281 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 }
5283 if (range)
5284 {
5285 if (empty2)
5286 n2 = -1;
5287 else
5288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005289 n2 = get_tv_number(&var2);
5290 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292 }
5293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005294 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 {
5296 case VAR_NUMBER:
5297 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 len = (long)STRLEN(s);
5300 if (range)
5301 {
5302 /* The resulting variable is a substring. If the indexes
5303 * are out of range the result is empty. */
5304 if (n1 < 0)
5305 {
5306 n1 = len + n1;
5307 if (n1 < 0)
5308 n1 = 0;
5309 }
5310 if (n2 < 0)
5311 n2 = len + n2;
5312 else if (n2 >= len)
5313 n2 = len;
5314 if (n1 >= len || n2 < 0 || n1 > n2)
5315 s = NULL;
5316 else
5317 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5318 }
5319 else
5320 {
5321 /* The resulting variable is a string of a single
5322 * character. If the index is too big or negative the
5323 * result is empty. */
5324 if (n1 >= len || n1 < 0)
5325 s = NULL;
5326 else
5327 s = vim_strnsave(s + n1, 1);
5328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005329 clear_tv(rettv);
5330 rettv->v_type = VAR_STRING;
5331 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 break;
5333
5334 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005335 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 if (n1 < 0)
5337 n1 = len + n1;
5338 if (!empty1 && (n1 < 0 || n1 >= len))
5339 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005340 /* For a range we allow invalid values and return an empty
5341 * list. A list index out of range is an error. */
5342 if (!range)
5343 {
5344 if (verbose)
5345 EMSGN(_(e_listidx), n1);
5346 return FAIL;
5347 }
5348 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 }
5350 if (range)
5351 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 list_T *l;
5353 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354
5355 if (n2 < 0)
5356 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005357 else if (n2 >= len)
5358 n2 = len - 1;
5359 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005360 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 l = list_alloc();
5362 if (l == NULL)
5363 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 n1 <= n2; ++n1)
5366 {
5367 if (list_append_tv(l, &item->li_tv) == FAIL)
5368 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005369 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 return FAIL;
5371 }
5372 item = item->li_next;
5373 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 clear_tv(rettv);
5375 rettv->v_type = VAR_LIST;
5376 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005377 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 }
5379 else
5380 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005381 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 clear_tv(rettv);
5383 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 }
5385 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386
5387 case VAR_DICT:
5388 if (range)
5389 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005390 if (verbose)
5391 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 if (len == -1)
5393 clear_tv(&var1);
5394 return FAIL;
5395 }
5396 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398
5399 if (len == -1)
5400 {
5401 key = get_tv_string(&var1);
5402 if (*key == NUL)
5403 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005404 if (verbose)
5405 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 clear_tv(&var1);
5407 return FAIL;
5408 }
5409 }
5410
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005411 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005413 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005414 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415 if (len == -1)
5416 clear_tv(&var1);
5417 if (item == NULL)
5418 return FAIL;
5419
5420 copy_tv(&item->di_tv, &var1);
5421 clear_tv(rettv);
5422 *rettv = var1;
5423 }
5424 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 }
5426 }
5427
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 return OK;
5429}
5430
5431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 * Get an option value.
5433 * "arg" points to the '&' or '+' before the option name.
5434 * "arg" is advanced to character after the option name.
5435 * Return OK or FAIL.
5436 */
5437 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005440 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 int evaluate;
5442{
5443 char_u *option_end;
5444 long numval;
5445 char_u *stringval;
5446 int opt_type;
5447 int c;
5448 int working = (**arg == '+'); /* has("+option") */
5449 int ret = OK;
5450 int opt_flags;
5451
5452 /*
5453 * Isolate the option name and find its value.
5454 */
5455 option_end = find_option_end(arg, &opt_flags);
5456 if (option_end == NULL)
5457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 EMSG2(_("E112: Option name missing: %s"), *arg);
5460 return FAIL;
5461 }
5462
5463 if (!evaluate)
5464 {
5465 *arg = option_end;
5466 return OK;
5467 }
5468
5469 c = *option_end;
5470 *option_end = NUL;
5471 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
5474 if (opt_type == -3) /* invalid name */
5475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 EMSG2(_("E113: Unknown option: %s"), *arg);
5478 ret = FAIL;
5479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 if (opt_type == -2) /* hidden string option */
5483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005484 rettv->v_type = VAR_STRING;
5485 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 else if (opt_type == -1) /* hidden number option */
5488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489 rettv->v_type = VAR_NUMBER;
5490 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492 else if (opt_type == 1) /* number option */
5493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 rettv->v_type = VAR_NUMBER;
5495 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497 else /* string option */
5498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 rettv->v_type = VAR_STRING;
5500 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502 }
5503 else if (working && (opt_type == -2 || opt_type == -1))
5504 ret = FAIL;
5505
5506 *option_end = c; /* put back for error messages */
5507 *arg = option_end;
5508
5509 return ret;
5510}
5511
5512/*
5513 * Allocate a variable for a string constant.
5514 * Return OK or FAIL.
5515 */
5516 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 int evaluate;
5521{
5522 char_u *p;
5523 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 int extra = 0;
5525
5526 /*
5527 * Find the end of the string, skipping backslashed characters.
5528 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005529 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
5531 if (*p == '\\' && p[1] != NUL)
5532 {
5533 ++p;
5534 /* A "\<x>" form occupies at least 4 characters, and produces up
5535 * to 6 characters: reserve space for 2 extra */
5536 if (*p == '<')
5537 extra += 2;
5538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
5540
5541 if (*p != '"')
5542 {
5543 EMSG2(_("E114: Missing quote: %s"), *arg);
5544 return FAIL;
5545 }
5546
5547 /* If only parsing, set *arg and return here */
5548 if (!evaluate)
5549 {
5550 *arg = p + 1;
5551 return OK;
5552 }
5553
5554 /*
5555 * Copy the string into allocated memory, handling backslashed
5556 * characters.
5557 */
5558 name = alloc((unsigned)(p - *arg + extra));
5559 if (name == NULL)
5560 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 rettv->v_type = VAR_STRING;
5562 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
Bram Moolenaar8c711452005-01-14 21:53:12 +00005564 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
5566 if (*p == '\\')
5567 {
5568 switch (*++p)
5569 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 case 'b': *name++ = BS; ++p; break;
5571 case 'e': *name++ = ESC; ++p; break;
5572 case 'f': *name++ = FF; ++p; break;
5573 case 'n': *name++ = NL; ++p; break;
5574 case 'r': *name++ = CAR; ++p; break;
5575 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
5577 case 'X': /* hex: "\x1", "\x12" */
5578 case 'x':
5579 case 'u': /* Unicode: "\u0023" */
5580 case 'U':
5581 if (vim_isxdigit(p[1]))
5582 {
5583 int n, nr;
5584 int c = toupper(*p);
5585
5586 if (c == 'X')
5587 n = 2;
5588 else
5589 n = 4;
5590 nr = 0;
5591 while (--n >= 0 && vim_isxdigit(p[1]))
5592 {
5593 ++p;
5594 nr = (nr << 4) + hex2nr(*p);
5595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597#ifdef FEAT_MBYTE
5598 /* For "\u" store the number according to
5599 * 'encoding'. */
5600 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 else
5603#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 break;
5607
5608 /* octal: "\1", "\12", "\123" */
5609 case '0':
5610 case '1':
5611 case '2':
5612 case '3':
5613 case '4':
5614 case '5':
5615 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 case '7': *name = *p++ - '0';
5617 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 *name = (*name << 3) + *p++ - '0';
5620 if (*p >= '0' && *p <= '7')
5621 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 break;
5625
5626 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 if (extra != 0)
5629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 break;
5632 }
5633 /* FALLTHROUGH */
5634
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 break;
5637 }
5638 }
5639 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 *arg = p + 1;
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 return OK;
5647}
5648
5649/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 * Return OK or FAIL.
5652 */
5653 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 int evaluate;
5658{
5659 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005660 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 int reduce = 0;
5662
5663 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 */
5666 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5667 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005668 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005671 break;
5672 ++reduce;
5673 ++p;
5674 }
5675 }
5676
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 return FAIL;
5681 }
5682
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 if (!evaluate)
5685 {
5686 *arg = p + 1;
5687 return OK;
5688 }
5689
5690 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005692 */
5693 str = alloc((unsigned)((p - *arg) - reduce));
5694 if (str == NULL)
5695 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 rettv->v_type = VAR_STRING;
5697 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005698
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704 break;
5705 ++p;
5706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005710 *arg = p + 1;
5711
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 return OK;
5713}
5714
5715/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 * Allocate a variable for a List and fill it from "*arg".
5717 * Return OK or FAIL.
5718 */
5719 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005720get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005722 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723 int evaluate;
5724{
Bram Moolenaar33570922005-01-25 22:26:29 +00005725 list_T *l = NULL;
5726 typval_T tv;
5727 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728
5729 if (evaluate)
5730 {
5731 l = list_alloc();
5732 if (l == NULL)
5733 return FAIL;
5734 }
5735
5736 *arg = skipwhite(*arg + 1);
5737 while (**arg != ']' && **arg != NUL)
5738 {
5739 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5740 goto failret;
5741 if (evaluate)
5742 {
5743 item = listitem_alloc();
5744 if (item != NULL)
5745 {
5746 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005747 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748 list_append(l, item);
5749 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005750 else
5751 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752 }
5753
5754 if (**arg == ']')
5755 break;
5756 if (**arg != ',')
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 goto failret;
5760 }
5761 *arg = skipwhite(*arg + 1);
5762 }
5763
5764 if (**arg != ']')
5765 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767failret:
5768 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005769 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 return FAIL;
5771 }
5772
5773 *arg = skipwhite(*arg + 1);
5774 if (evaluate)
5775 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005776 rettv->v_type = VAR_LIST;
5777 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778 ++l->lv_refcount;
5779 }
5780
5781 return OK;
5782}
5783
5784/*
5785 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005786 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005788 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789list_alloc()
5790{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005791 list_T *l;
5792
5793 l = (list_T *)alloc_clear(sizeof(list_T));
5794 if (l != NULL)
5795 {
5796 /* Prepend the list to the list of lists for garbage collection. */
5797 if (first_list != NULL)
5798 first_list->lv_used_prev = l;
5799 l->lv_used_prev = NULL;
5800 l->lv_used_next = first_list;
5801 first_list = l;
5802 }
5803 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804}
5805
5806/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005807 * Allocate an empty list for a return value.
5808 * Returns OK or FAIL.
5809 */
5810 static int
5811rettv_list_alloc(rettv)
5812 typval_T *rettv;
5813{
5814 list_T *l = list_alloc();
5815
5816 if (l == NULL)
5817 return FAIL;
5818
5819 rettv->vval.v_list = l;
5820 rettv->v_type = VAR_LIST;
5821 ++l->lv_refcount;
5822 return OK;
5823}
5824
5825/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 * Unreference a list: decrement the reference count and free it when it
5827 * becomes zero.
5828 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005829 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005833 if (l != NULL && --l->lv_refcount <= 0)
5834 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835}
5836
5837/*
5838 * Free a list, including all items it points to.
5839 * Ignores the reference count.
5840 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005841 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005842list_free(l, recurse)
5843 list_T *l;
5844 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845{
Bram Moolenaar33570922005-01-25 22:26:29 +00005846 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005848 /* Remove the list from the list of lists for garbage collection. */
5849 if (l->lv_used_prev == NULL)
5850 first_list = l->lv_used_next;
5851 else
5852 l->lv_used_prev->lv_used_next = l->lv_used_next;
5853 if (l->lv_used_next != NULL)
5854 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5855
Bram Moolenaard9fba312005-06-26 22:34:35 +00005856 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005858 /* Remove the item before deleting it. */
5859 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005860 if (recurse || (item->li_tv.v_type != VAR_LIST
5861 && item->li_tv.v_type != VAR_DICT))
5862 clear_tv(&item->li_tv);
5863 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 }
5865 vim_free(l);
5866}
5867
5868/*
5869 * Allocate a list item.
5870 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872listitem_alloc()
5873{
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875}
5876
5877/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005878 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 */
5880 static void
5881listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005882 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005884 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 vim_free(item);
5886}
5887
5888/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005889 * Remove a list item from a List and free it. Also clears the value.
5890 */
5891 static void
5892listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 list_T *l;
5894 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005895{
5896 list_remove(l, item, item);
5897 listitem_free(item);
5898}
5899
5900/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 * Get the number of items in a list.
5902 */
5903 static long
5904list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 if (l == NULL)
5908 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005909 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910}
5911
5912/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005913 * Return TRUE when two lists have exactly the same values.
5914 */
5915 static int
5916list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 list_T *l1;
5918 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005919 int ic; /* ignore case for strings */
5920{
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005922
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005923 if (l1 == NULL || l2 == NULL)
5924 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005925 if (l1 == l2)
5926 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005927 if (list_len(l1) != list_len(l2))
5928 return FALSE;
5929
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005930 for (item1 = l1->lv_first, item2 = l2->lv_first;
5931 item1 != NULL && item2 != NULL;
5932 item1 = item1->li_next, item2 = item2->li_next)
5933 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5934 return FALSE;
5935 return item1 == NULL && item2 == NULL;
5936}
5937
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005938#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5939 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005940/*
5941 * Return the dictitem that an entry in a hashtable points to.
5942 */
5943 dictitem_T *
5944dict_lookup(hi)
5945 hashitem_T *hi;
5946{
5947 return HI2DI(hi);
5948}
5949#endif
5950
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005951/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005952 * Return TRUE when two dictionaries have exactly the same key/values.
5953 */
5954 static int
5955dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 dict_T *d1;
5957 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005958 int ic; /* ignore case for strings */
5959{
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 hashitem_T *hi;
5961 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005962 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005963
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005964 if (d1 == NULL || d2 == NULL)
5965 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005966 if (d1 == d2)
5967 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005968 if (dict_len(d1) != dict_len(d2))
5969 return FALSE;
5970
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005971 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005973 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005974 if (!HASHITEM_EMPTY(hi))
5975 {
5976 item2 = dict_find(d2, hi->hi_key, -1);
5977 if (item2 == NULL)
5978 return FALSE;
5979 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5980 return FALSE;
5981 --todo;
5982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005983 }
5984 return TRUE;
5985}
5986
5987/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005988 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005989 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005990 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005991 */
5992 static int
5993tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 typval_T *tv1;
5995 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 int ic; /* ignore case */
5997{
5998 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005999 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006000 static int recursive = 0; /* cach recursive loops */
6001 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006003 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006004 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006005 /* Catch lists and dicts that have an endless loop by limiting
6006 * recursiveness to 1000. We guess they are equal then. */
6007 if (recursive >= 1000)
6008 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006009
6010 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006012 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006013 ++recursive;
6014 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
6015 --recursive;
6016 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006017
6018 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006019 ++recursive;
6020 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
6021 --recursive;
6022 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006023
6024 case VAR_FUNC:
6025 return (tv1->vval.v_string != NULL
6026 && tv2->vval.v_string != NULL
6027 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6028
6029 case VAR_NUMBER:
6030 return tv1->vval.v_number == tv2->vval.v_number;
6031
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006032#ifdef FEAT_FLOAT
6033 case VAR_FLOAT:
6034 return tv1->vval.v_float == tv2->vval.v_float;
6035#endif
6036
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006037 case VAR_STRING:
6038 s1 = get_tv_string_buf(tv1, buf1);
6039 s2 = get_tv_string_buf(tv2, buf2);
6040 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006041 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006042
6043 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006044 return TRUE;
6045}
6046
6047/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 * Locate item with index "n" in list "l" and return it.
6049 * A negative index is counted from the end; -1 is the last item.
6050 * Returns NULL when "n" is out of range.
6051 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 long n;
6056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 long idx;
6059
6060 if (l == NULL)
6061 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006062
6063 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006065 n = l->lv_len + n;
6066
6067 /* Check for index out of range. */
6068 if (n < 0 || n >= l->lv_len)
6069 return NULL;
6070
6071 /* When there is a cached index may start search from there. */
6072 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006074 if (n < l->lv_idx / 2)
6075 {
6076 /* closest to the start of the list */
6077 item = l->lv_first;
6078 idx = 0;
6079 }
6080 else if (n > (l->lv_idx + l->lv_len) / 2)
6081 {
6082 /* closest to the end of the list */
6083 item = l->lv_last;
6084 idx = l->lv_len - 1;
6085 }
6086 else
6087 {
6088 /* closest to the cached index */
6089 item = l->lv_idx_item;
6090 idx = l->lv_idx;
6091 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092 }
6093 else
6094 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006095 if (n < l->lv_len / 2)
6096 {
6097 /* closest to the start of the list */
6098 item = l->lv_first;
6099 idx = 0;
6100 }
6101 else
6102 {
6103 /* closest to the end of the list */
6104 item = l->lv_last;
6105 idx = l->lv_len - 1;
6106 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006108
6109 while (n > idx)
6110 {
6111 /* search forward */
6112 item = item->li_next;
6113 ++idx;
6114 }
6115 while (n < idx)
6116 {
6117 /* search backward */
6118 item = item->li_prev;
6119 --idx;
6120 }
6121
6122 /* cache the used index */
6123 l->lv_idx = idx;
6124 l->lv_idx_item = item;
6125
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006126 return item;
6127}
6128
6129/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006130 * Get list item "l[idx]" as a number.
6131 */
6132 static long
6133list_find_nr(l, idx, errorp)
6134 list_T *l;
6135 long idx;
6136 int *errorp; /* set to TRUE when something wrong */
6137{
6138 listitem_T *li;
6139
6140 li = list_find(l, idx);
6141 if (li == NULL)
6142 {
6143 if (errorp != NULL)
6144 *errorp = TRUE;
6145 return -1L;
6146 }
6147 return get_tv_number_chk(&li->li_tv, errorp);
6148}
6149
6150/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006151 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6152 */
6153 char_u *
6154list_find_str(l, idx)
6155 list_T *l;
6156 long idx;
6157{
6158 listitem_T *li;
6159
6160 li = list_find(l, idx - 1);
6161 if (li == NULL)
6162 {
6163 EMSGN(_(e_listidx), idx);
6164 return NULL;
6165 }
6166 return get_tv_string(&li->li_tv);
6167}
6168
6169/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006170 * Locate "item" list "l" and return its index.
6171 * Returns -1 when "item" is not in the list.
6172 */
6173 static long
6174list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 list_T *l;
6176 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006177{
6178 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006180
6181 if (l == NULL)
6182 return -1;
6183 idx = 0;
6184 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6185 ++idx;
6186 if (li == NULL)
6187 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006188 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006189}
6190
6191/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 * Append item "item" to the end of list "l".
6193 */
6194 static void
6195list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 list_T *l;
6197 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198{
6199 if (l->lv_last == NULL)
6200 {
6201 /* empty list */
6202 l->lv_first = item;
6203 l->lv_last = item;
6204 item->li_prev = NULL;
6205 }
6206 else
6207 {
6208 l->lv_last->li_next = item;
6209 item->li_prev = l->lv_last;
6210 l->lv_last = item;
6211 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006212 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006213 item->li_next = NULL;
6214}
6215
6216/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006217 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006218 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006220 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006222 list_T *l;
6223 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006225 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006226
Bram Moolenaar05159a02005-02-26 23:04:13 +00006227 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006229 copy_tv(tv, &li->li_tv);
6230 list_append(l, li);
6231 return OK;
6232}
6233
6234/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006235 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006236 * Return FAIL when out of memory.
6237 */
6238 int
6239list_append_dict(list, dict)
6240 list_T *list;
6241 dict_T *dict;
6242{
6243 listitem_T *li = listitem_alloc();
6244
6245 if (li == NULL)
6246 return FAIL;
6247 li->li_tv.v_type = VAR_DICT;
6248 li->li_tv.v_lock = 0;
6249 li->li_tv.vval.v_dict = dict;
6250 list_append(list, li);
6251 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 return OK;
6253}
6254
6255/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006256 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006257 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006258 * Returns FAIL when out of memory.
6259 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006260 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006261list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006262 list_T *l;
6263 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006264 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006265{
6266 listitem_T *li = listitem_alloc();
6267
6268 if (li == NULL)
6269 return FAIL;
6270 list_append(l, li);
6271 li->li_tv.v_type = VAR_STRING;
6272 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006273 if (str == NULL)
6274 li->li_tv.vval.v_string = NULL;
6275 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006276 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006277 return FAIL;
6278 return OK;
6279}
6280
6281/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006282 * Append "n" to list "l".
6283 * Returns FAIL when out of memory.
6284 */
6285 static int
6286list_append_number(l, n)
6287 list_T *l;
6288 varnumber_T n;
6289{
6290 listitem_T *li;
6291
6292 li = listitem_alloc();
6293 if (li == NULL)
6294 return FAIL;
6295 li->li_tv.v_type = VAR_NUMBER;
6296 li->li_tv.v_lock = 0;
6297 li->li_tv.vval.v_number = n;
6298 list_append(l, li);
6299 return OK;
6300}
6301
6302/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006303 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006304 * If "item" is NULL append at the end.
6305 * Return FAIL when out of memory.
6306 */
6307 static int
6308list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 list_T *l;
6310 typval_T *tv;
6311 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006312{
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006314
6315 if (ni == NULL)
6316 return FAIL;
6317 copy_tv(tv, &ni->li_tv);
6318 if (item == NULL)
6319 /* Append new item at end of list. */
6320 list_append(l, ni);
6321 else
6322 {
6323 /* Insert new item before existing item. */
6324 ni->li_prev = item->li_prev;
6325 ni->li_next = item;
6326 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006327 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006329 ++l->lv_idx;
6330 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006332 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006333 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006334 l->lv_idx_item = NULL;
6335 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006337 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 }
6339 return OK;
6340}
6341
6342/*
6343 * Extend "l1" with "l2".
6344 * If "bef" is NULL append at the end, otherwise insert before this item.
6345 * Returns FAIL when out of memory.
6346 */
6347 static int
6348list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 list_T *l1;
6350 list_T *l2;
6351 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352{
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006354 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006356 /* We also quit the loop when we have inserted the original item count of
6357 * the list, avoid a hang when we extend a list with itself. */
6358 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006359 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6360 return FAIL;
6361 return OK;
6362}
6363
6364/*
6365 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6366 * Return FAIL when out of memory.
6367 */
6368 static int
6369list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 list_T *l1;
6371 list_T *l2;
6372 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006373{
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006376 if (l1 == NULL || l2 == NULL)
6377 return FAIL;
6378
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006380 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006381 if (l == NULL)
6382 return FAIL;
6383 tv->v_type = VAR_LIST;
6384 tv->vval.v_list = l;
6385
6386 /* append all items from the second list */
6387 return list_extend(l, l2, NULL);
6388}
6389
6390/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006391 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006393 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 * Returns NULL when out of memory.
6395 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006397list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401{
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 list_T *copy;
6403 listitem_T *item;
6404 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405
6406 if (orig == NULL)
6407 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408
6409 copy = list_alloc();
6410 if (copy != NULL)
6411 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006412 if (copyID != 0)
6413 {
6414 /* Do this before adding the items, because one of the items may
6415 * refer back to this list. */
6416 orig->lv_copyID = copyID;
6417 orig->lv_copylist = copy;
6418 }
6419 for (item = orig->lv_first; item != NULL && !got_int;
6420 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 {
6422 ni = listitem_alloc();
6423 if (ni == NULL)
6424 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006425 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006426 {
6427 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6428 {
6429 vim_free(ni);
6430 break;
6431 }
6432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006434 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435 list_append(copy, ni);
6436 }
6437 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 if (item != NULL)
6439 {
6440 list_unref(copy);
6441 copy = NULL;
6442 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 }
6444
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445 return copy;
6446}
6447
6448/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006449 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006450 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006453list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 list_T *l;
6455 listitem_T *item;
6456 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457{
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 /* notify watchers */
6461 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006462 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 list_fix_watch(l, ip);
6465 if (ip == item2)
6466 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468
6469 if (item2->li_next == NULL)
6470 l->lv_last = item->li_prev;
6471 else
6472 item2->li_next->li_prev = item->li_prev;
6473 if (item->li_prev == NULL)
6474 l->lv_first = item2->li_next;
6475 else
6476 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006477 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478}
6479
6480/*
6481 * Return an allocated string with the string representation of a list.
6482 * May return NULL.
6483 */
6484 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006485list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488{
6489 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490
6491 if (tv->vval.v_list == NULL)
6492 return NULL;
6493 ga_init2(&ga, (int)sizeof(char), 80);
6494 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006495 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 {
6497 vim_free(ga.ga_data);
6498 return NULL;
6499 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500 ga_append(&ga, ']');
6501 ga_append(&ga, NUL);
6502 return (char_u *)ga.ga_data;
6503}
6504
6505/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006506 * Join list "l" into a string in "*gap", using separator "sep".
6507 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006508 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006509 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006511list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006512 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006514 char_u *sep;
6515 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006516 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006517{
6518 int first = TRUE;
6519 char_u *tofree;
6520 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006522 char_u *s;
6523
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006525 {
6526 if (first)
6527 first = FALSE;
6528 else
6529 ga_concat(gap, sep);
6530
6531 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006532 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006533 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006534 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006535 if (s != NULL)
6536 ga_concat(gap, s);
6537 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006538 if (s == NULL)
6539 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006540 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006541 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006542 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006543}
6544
6545/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006546 * Garbage collection for lists and dictionaries.
6547 *
6548 * We use reference counts to be able to free most items right away when they
6549 * are no longer used. But for composite items it's possible that it becomes
6550 * unused while the reference count is > 0: When there is a recursive
6551 * reference. Example:
6552 * :let l = [1, 2, 3]
6553 * :let d = {9: l}
6554 * :let l[1] = d
6555 *
6556 * Since this is quite unusual we handle this with garbage collection: every
6557 * once in a while find out which lists and dicts are not referenced from any
6558 * variable.
6559 *
6560 * Here is a good reference text about garbage collection (refers to Python
6561 * but it applies to all reference-counting mechanisms):
6562 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006563 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006564
6565/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006566 * Do garbage collection for lists and dicts.
6567 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006568 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006569 int
6570garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006571{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006572 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006573 buf_T *buf;
6574 win_T *wp;
6575 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006576 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006577 int did_free;
6578 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006579#ifdef FEAT_WINDOWS
6580 tabpage_T *tp;
6581#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006582
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006583 /* Only do this once. */
6584 want_garbage_collect = FALSE;
6585 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006586 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006587
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006588 /* We advance by two because we add one for items referenced through
6589 * previous_funccal. */
6590 current_copyID += COPYID_INC;
6591 copyID = current_copyID;
6592
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006593 /*
6594 * 1. Go through all accessible variables and mark all lists and dicts
6595 * with copyID.
6596 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006597
6598 /* Don't free variables in the previous_funccal list unless they are only
6599 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006600 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006601 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6602 {
6603 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6604 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6605 }
6606
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006607 /* script-local variables */
6608 for (i = 1; i <= ga_scripts.ga_len; ++i)
6609 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6610
6611 /* buffer-local variables */
6612 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6613 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6614
6615 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006616 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006617 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6618
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006619#ifdef FEAT_WINDOWS
6620 /* tabpage-local variables */
6621 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6622 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6623#endif
6624
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006625 /* global variables */
6626 set_ref_in_ht(&globvarht, copyID);
6627
6628 /* function-local variables */
6629 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6630 {
6631 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6632 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6633 }
6634
Bram Moolenaard812df62008-11-09 12:46:09 +00006635 /* v: vars */
6636 set_ref_in_ht(&vimvarht, copyID);
6637
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006638 /*
6639 * 2. Free lists and dictionaries that are not referenced.
6640 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006641 did_free = free_unref_items(copyID);
6642
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006643 /*
6644 * 3. Check if any funccal can be freed now.
6645 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006646 for (pfc = &previous_funccal; *pfc != NULL; )
6647 {
6648 if (can_free_funccal(*pfc, copyID))
6649 {
6650 fc = *pfc;
6651 *pfc = fc->caller;
6652 free_funccal(fc, TRUE);
6653 did_free = TRUE;
6654 did_free_funccal = TRUE;
6655 }
6656 else
6657 pfc = &(*pfc)->caller;
6658 }
6659 if (did_free_funccal)
6660 /* When a funccal was freed some more items might be garbage
6661 * collected, so run again. */
6662 (void)garbage_collect();
6663
6664 return did_free;
6665}
6666
6667/*
6668 * Free lists and dictionaries that are no longer referenced.
6669 */
6670 static int
6671free_unref_items(copyID)
6672 int copyID;
6673{
6674 dict_T *dd;
6675 list_T *ll;
6676 int did_free = FALSE;
6677
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006678 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006679 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006680 */
6681 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006682 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006683 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006684 /* Free the Dictionary and ordinary items it contains, but don't
6685 * recurse into Lists and Dictionaries, they will be in the list
6686 * of dicts or list of lists. */
6687 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006688 did_free = TRUE;
6689
6690 /* restart, next dict may also have been freed */
6691 dd = first_dict;
6692 }
6693 else
6694 dd = dd->dv_used_next;
6695
6696 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006697 * Go through the list of lists and free items without the copyID.
6698 * But don't free a list that has a watcher (used in a for loop), these
6699 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006700 */
6701 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006702 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6703 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006704 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006705 /* Free the List and ordinary items it contains, but don't recurse
6706 * into Lists and Dictionaries, they will be in the list of dicts
6707 * or list of lists. */
6708 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006709 did_free = TRUE;
6710
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006711 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006712 ll = first_list;
6713 }
6714 else
6715 ll = ll->lv_used_next;
6716
6717 return did_free;
6718}
6719
6720/*
6721 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6722 */
6723 static void
6724set_ref_in_ht(ht, copyID)
6725 hashtab_T *ht;
6726 int copyID;
6727{
6728 int todo;
6729 hashitem_T *hi;
6730
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006731 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 for (hi = ht->ht_array; todo > 0; ++hi)
6733 if (!HASHITEM_EMPTY(hi))
6734 {
6735 --todo;
6736 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6737 }
6738}
6739
6740/*
6741 * Mark all lists and dicts referenced through list "l" with "copyID".
6742 */
6743 static void
6744set_ref_in_list(l, copyID)
6745 list_T *l;
6746 int copyID;
6747{
6748 listitem_T *li;
6749
6750 for (li = l->lv_first; li != NULL; li = li->li_next)
6751 set_ref_in_item(&li->li_tv, copyID);
6752}
6753
6754/*
6755 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6756 */
6757 static void
6758set_ref_in_item(tv, copyID)
6759 typval_T *tv;
6760 int copyID;
6761{
6762 dict_T *dd;
6763 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764
6765 switch (tv->v_type)
6766 {
6767 case VAR_DICT:
6768 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006769 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006770 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006771 /* Didn't see this dict yet. */
6772 dd->dv_copyID = copyID;
6773 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006774 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006776
6777 case VAR_LIST:
6778 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006779 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006780 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781 /* Didn't see this list yet. */
6782 ll->lv_copyID = copyID;
6783 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006784 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006786 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006788}
6789
6790/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791 * Allocate an empty header for a dictionary.
6792 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006793 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006794dict_alloc()
6795{
Bram Moolenaar33570922005-01-25 22:26:29 +00006796 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006797
Bram Moolenaar33570922005-01-25 22:26:29 +00006798 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006799 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006800 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006801 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006802 if (first_dict != NULL)
6803 first_dict->dv_used_prev = d;
6804 d->dv_used_next = first_dict;
6805 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006806 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807
Bram Moolenaar33570922005-01-25 22:26:29 +00006808 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006809 d->dv_lock = 0;
6810 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006811 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006812 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006813 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006814}
6815
6816/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006817 * Allocate an empty dict for a return value.
6818 * Returns OK or FAIL.
6819 */
6820 static int
6821rettv_dict_alloc(rettv)
6822 typval_T *rettv;
6823{
6824 dict_T *d = dict_alloc();
6825
6826 if (d == NULL)
6827 return FAIL;
6828
6829 rettv->vval.v_dict = d;
6830 rettv->v_type = VAR_DICT;
6831 ++d->dv_refcount;
6832 return OK;
6833}
6834
6835
6836/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006837 * Unreference a Dictionary: decrement the reference count and free it when it
6838 * becomes zero.
6839 */
6840 static void
6841dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006842 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006843{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006844 if (d != NULL && --d->dv_refcount <= 0)
6845 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006846}
6847
6848/*
6849 * Free a Dictionary, including all items it contains.
6850 * Ignores the reference count.
6851 */
6852 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006853dict_free(d, recurse)
6854 dict_T *d;
6855 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006856{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006857 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006858 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006859 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006860
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 /* Remove the dict from the list of dicts for garbage collection. */
6862 if (d->dv_used_prev == NULL)
6863 first_dict = d->dv_used_next;
6864 else
6865 d->dv_used_prev->dv_used_next = d->dv_used_next;
6866 if (d->dv_used_next != NULL)
6867 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6868
6869 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006870 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006871 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006872 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006874 if (!HASHITEM_EMPTY(hi))
6875 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006876 /* Remove the item before deleting it, just in case there is
6877 * something recursive causing trouble. */
6878 di = HI2DI(hi);
6879 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006880 if (recurse || (di->di_tv.v_type != VAR_LIST
6881 && di->di_tv.v_type != VAR_DICT))
6882 clear_tv(&di->di_tv);
6883 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006884 --todo;
6885 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006886 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006887 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006888 vim_free(d);
6889}
6890
6891/*
6892 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006893 * The "key" is copied to the new item.
6894 * Note that the value of the item "di_tv" still needs to be initialized!
6895 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006896 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006897 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006898dictitem_alloc(key)
6899 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006900{
Bram Moolenaar33570922005-01-25 22:26:29 +00006901 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006902
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006903 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006904 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006906 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006907 di->di_flags = 0;
6908 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006909 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006910}
6911
6912/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006913 * Make a copy of a Dictionary item.
6914 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006915 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006916dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006918{
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006920
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006921 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6922 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006923 if (di != NULL)
6924 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006925 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006926 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006927 copy_tv(&org->di_tv, &di->di_tv);
6928 }
6929 return di;
6930}
6931
6932/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006933 * Remove item "item" from Dictionary "dict" and free it.
6934 */
6935 static void
6936dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006937 dict_T *dict;
6938 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006939{
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006941
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006943 if (HASHITEM_EMPTY(hi))
6944 EMSG2(_(e_intern2), "dictitem_remove()");
6945 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006947 dictitem_free(item);
6948}
6949
6950/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006951 * Free a dict item. Also clears the value.
6952 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006953 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006954dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006955 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006956{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006957 clear_tv(&item->di_tv);
6958 vim_free(item);
6959}
6960
6961/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6963 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006964 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965 * Returns NULL when out of memory.
6966 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006968dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006971 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006972{
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 dict_T *copy;
6974 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006975 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006977
6978 if (orig == NULL)
6979 return NULL;
6980
6981 copy = dict_alloc();
6982 if (copy != NULL)
6983 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006984 if (copyID != 0)
6985 {
6986 orig->dv_copyID = copyID;
6987 orig->dv_copydict = copy;
6988 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006989 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006990 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006991 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006992 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006993 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006994 --todo;
6995
6996 di = dictitem_alloc(hi->hi_key);
6997 if (di == NULL)
6998 break;
6999 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007000 {
7001 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7002 copyID) == FAIL)
7003 {
7004 vim_free(di);
7005 break;
7006 }
7007 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007008 else
7009 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7010 if (dict_add(copy, di) == FAIL)
7011 {
7012 dictitem_free(di);
7013 break;
7014 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007016 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007017
Bram Moolenaare9a41262005-01-15 22:18:47 +00007018 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007019 if (todo > 0)
7020 {
7021 dict_unref(copy);
7022 copy = NULL;
7023 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024 }
7025
7026 return copy;
7027}
7028
7029/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007031 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007033 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 dict_T *d;
7036 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007037{
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007039}
7040
Bram Moolenaar8c711452005-01-14 21:53:12 +00007041/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007042 * Add a number or string entry to dictionary "d".
7043 * When "str" is NULL use number "nr", otherwise use "str".
7044 * Returns FAIL when out of memory and when key already exists.
7045 */
7046 int
7047dict_add_nr_str(d, key, nr, str)
7048 dict_T *d;
7049 char *key;
7050 long nr;
7051 char_u *str;
7052{
7053 dictitem_T *item;
7054
7055 item = dictitem_alloc((char_u *)key);
7056 if (item == NULL)
7057 return FAIL;
7058 item->di_tv.v_lock = 0;
7059 if (str == NULL)
7060 {
7061 item->di_tv.v_type = VAR_NUMBER;
7062 item->di_tv.vval.v_number = nr;
7063 }
7064 else
7065 {
7066 item->di_tv.v_type = VAR_STRING;
7067 item->di_tv.vval.v_string = vim_strsave(str);
7068 }
7069 if (dict_add(d, item) == FAIL)
7070 {
7071 dictitem_free(item);
7072 return FAIL;
7073 }
7074 return OK;
7075}
7076
7077/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007078 * Add a list entry to dictionary "d".
7079 * Returns FAIL when out of memory and when key already exists.
7080 */
7081 int
7082dict_add_list(d, key, list)
7083 dict_T *d;
7084 char *key;
7085 list_T *list;
7086{
7087 dictitem_T *item;
7088
7089 item = dictitem_alloc((char_u *)key);
7090 if (item == NULL)
7091 return FAIL;
7092 item->di_tv.v_lock = 0;
7093 item->di_tv.v_type = VAR_LIST;
7094 item->di_tv.vval.v_list = list;
7095 if (dict_add(d, item) == FAIL)
7096 {
7097 dictitem_free(item);
7098 return FAIL;
7099 }
7100 return OK;
7101}
7102
7103/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007104 * Get the number of items in a Dictionary.
7105 */
7106 static long
7107dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007108 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007109{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110 if (d == NULL)
7111 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007112 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007113}
7114
7115/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007116 * Find item "key[len]" in Dictionary "d".
7117 * If "len" is negative use strlen(key).
7118 * Returns NULL when not found.
7119 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007120 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007122 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007123 char_u *key;
7124 int len;
7125{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126#define AKEYLEN 200
7127 char_u buf[AKEYLEN];
7128 char_u *akey;
7129 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007131
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007132 if (len < 0)
7133 akey = key;
7134 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007135 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007136 tofree = akey = vim_strnsave(key, len);
7137 if (akey == NULL)
7138 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007139 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007140 else
7141 {
7142 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007143 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007144 akey = buf;
7145 }
7146
Bram Moolenaar33570922005-01-25 22:26:29 +00007147 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007148 vim_free(tofree);
7149 if (HASHITEM_EMPTY(hi))
7150 return NULL;
7151 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007152}
7153
7154/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007155 * Get a string item from a dictionary.
7156 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007157 * Returns NULL if the entry doesn't exist or out of memory.
7158 */
7159 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007160get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007161 dict_T *d;
7162 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007163 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007164{
7165 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007166 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007167
7168 di = dict_find(d, key, -1);
7169 if (di == NULL)
7170 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007171 s = get_tv_string(&di->di_tv);
7172 if (save && s != NULL)
7173 s = vim_strsave(s);
7174 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007175}
7176
7177/*
7178 * Get a number item from a dictionary.
7179 * Returns 0 if the entry doesn't exist or out of memory.
7180 */
7181 long
7182get_dict_number(d, key)
7183 dict_T *d;
7184 char_u *key;
7185{
7186 dictitem_T *di;
7187
7188 di = dict_find(d, key, -1);
7189 if (di == NULL)
7190 return 0;
7191 return get_tv_number(&di->di_tv);
7192}
7193
7194/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007195 * Return an allocated string with the string representation of a Dictionary.
7196 * May return NULL.
7197 */
7198 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007199dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007200 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007201 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007202{
7203 garray_T ga;
7204 int first = TRUE;
7205 char_u *tofree;
7206 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007207 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007209 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007210 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007211
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213 return NULL;
7214 ga_init2(&ga, (int)sizeof(char), 80);
7215 ga_append(&ga, '{');
7216
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007217 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007218 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007220 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 --todo;
7223
7224 if (first)
7225 first = FALSE;
7226 else
7227 ga_concat(&ga, (char_u *)", ");
7228
7229 tofree = string_quote(hi->hi_key, FALSE);
7230 if (tofree != NULL)
7231 {
7232 ga_concat(&ga, tofree);
7233 vim_free(tofree);
7234 }
7235 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007236 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007237 if (s != NULL)
7238 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007239 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007240 if (s == NULL)
7241 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007244 if (todo > 0)
7245 {
7246 vim_free(ga.ga_data);
7247 return NULL;
7248 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007249
7250 ga_append(&ga, '}');
7251 ga_append(&ga, NUL);
7252 return (char_u *)ga.ga_data;
7253}
7254
7255/*
7256 * Allocate a variable for a Dictionary and fill it from "*arg".
7257 * Return OK or FAIL. Returns NOTDONE for {expr}.
7258 */
7259 static int
7260get_dict_tv(arg, rettv, evaluate)
7261 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 int evaluate;
7264{
Bram Moolenaar33570922005-01-25 22:26:29 +00007265 dict_T *d = NULL;
7266 typval_T tvkey;
7267 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007268 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272
7273 /*
7274 * First check if it's not a curly-braces thing: {expr}.
7275 * Must do this without evaluating, otherwise a function may be called
7276 * twice. Unfortunately this means we need to call eval1() twice for the
7277 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007278 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280 if (*start != '}')
7281 {
7282 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7283 return FAIL;
7284 if (*start == '}')
7285 return NOTDONE;
7286 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287
7288 if (evaluate)
7289 {
7290 d = dict_alloc();
7291 if (d == NULL)
7292 return FAIL;
7293 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007294 tvkey.v_type = VAR_UNKNOWN;
7295 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296
7297 *arg = skipwhite(*arg + 1);
7298 while (**arg != '}' && **arg != NUL)
7299 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301 goto failret;
7302 if (**arg != ':')
7303 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007304 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 goto failret;
7307 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007308 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007309 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007310 key = get_tv_string_buf_chk(&tvkey, buf);
7311 if (key == NULL || *key == NUL)
7312 {
7313 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7314 if (key != NULL)
7315 EMSG(_(e_emptykey));
7316 clear_tv(&tvkey);
7317 goto failret;
7318 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320
7321 *arg = skipwhite(*arg + 1);
7322 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7323 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007324 if (evaluate)
7325 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 goto failret;
7327 }
7328 if (evaluate)
7329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 if (item != NULL)
7332 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007333 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007334 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335 clear_tv(&tv);
7336 goto failret;
7337 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007338 item = dictitem_alloc(key);
7339 clear_tv(&tvkey);
7340 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007343 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007344 if (dict_add(d, item) == FAIL)
7345 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007346 }
7347 }
7348
7349 if (**arg == '}')
7350 break;
7351 if (**arg != ',')
7352 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007353 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354 goto failret;
7355 }
7356 *arg = skipwhite(*arg + 1);
7357 }
7358
7359 if (**arg != '}')
7360 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007361 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007362failret:
7363 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007364 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365 return FAIL;
7366 }
7367
7368 *arg = skipwhite(*arg + 1);
7369 if (evaluate)
7370 {
7371 rettv->v_type = VAR_DICT;
7372 rettv->vval.v_dict = d;
7373 ++d->dv_refcount;
7374 }
7375
7376 return OK;
7377}
7378
Bram Moolenaar8c711452005-01-14 21:53:12 +00007379/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007380 * Return a string with the string representation of a variable.
7381 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007382 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007383 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007384 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007385 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007386 */
7387 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007388echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007390 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007391 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007392 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007393{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007394 static int recurse = 0;
7395 char_u *r = NULL;
7396
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007399 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 *tofree = NULL;
7401 return NULL;
7402 }
7403 ++recurse;
7404
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007405 switch (tv->v_type)
7406 {
7407 case VAR_FUNC:
7408 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 r = tv->vval.v_string;
7410 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007411
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007412 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007413 if (tv->vval.v_list == NULL)
7414 {
7415 *tofree = NULL;
7416 r = NULL;
7417 }
7418 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7419 {
7420 *tofree = NULL;
7421 r = (char_u *)"[...]";
7422 }
7423 else
7424 {
7425 tv->vval.v_list->lv_copyID = copyID;
7426 *tofree = list2string(tv, copyID);
7427 r = *tofree;
7428 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007430
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007432 if (tv->vval.v_dict == NULL)
7433 {
7434 *tofree = NULL;
7435 r = NULL;
7436 }
7437 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7438 {
7439 *tofree = NULL;
7440 r = (char_u *)"{...}";
7441 }
7442 else
7443 {
7444 tv->vval.v_dict->dv_copyID = copyID;
7445 *tofree = dict2string(tv, copyID);
7446 r = *tofree;
7447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007449
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007450 case VAR_STRING:
7451 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007452 *tofree = NULL;
7453 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007454 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007455
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007456#ifdef FEAT_FLOAT
7457 case VAR_FLOAT:
7458 *tofree = NULL;
7459 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7460 r = numbuf;
7461 break;
7462#endif
7463
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007464 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007465 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007467 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468
7469 --recurse;
7470 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007471}
7472
7473/*
7474 * Return a string with the string representation of a variable.
7475 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7476 * "numbuf" is used for a number.
7477 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007478 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007479 */
7480 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007481tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007482 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007483 char_u **tofree;
7484 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007485 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007486{
7487 switch (tv->v_type)
7488 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007489 case VAR_FUNC:
7490 *tofree = string_quote(tv->vval.v_string, TRUE);
7491 return *tofree;
7492 case VAR_STRING:
7493 *tofree = string_quote(tv->vval.v_string, FALSE);
7494 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007495#ifdef FEAT_FLOAT
7496 case VAR_FLOAT:
7497 *tofree = NULL;
7498 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7499 return numbuf;
7500#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007502 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007503 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007504 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007505 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007507 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007508 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007509}
7510
7511/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007512 * Return string "str" in ' quotes, doubling ' characters.
7513 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007515 */
7516 static char_u *
7517string_quote(str, function)
7518 char_u *str;
7519 int function;
7520{
Bram Moolenaar33570922005-01-25 22:26:29 +00007521 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007522 char_u *p, *r, *s;
7523
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 len = (function ? 13 : 3);
7525 if (str != NULL)
7526 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007527 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007528 for (p = str; *p != NUL; mb_ptr_adv(p))
7529 if (*p == '\'')
7530 ++len;
7531 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007532 s = r = alloc(len);
7533 if (r != NULL)
7534 {
7535 if (function)
7536 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007538 r += 10;
7539 }
7540 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007541 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007542 if (str != NULL)
7543 for (p = str; *p != NUL; )
7544 {
7545 if (*p == '\'')
7546 *r++ = '\'';
7547 MB_COPY_CHAR(p, r);
7548 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007550 if (function)
7551 *r++ = ')';
7552 *r++ = NUL;
7553 }
7554 return s;
7555}
7556
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007557#ifdef FEAT_FLOAT
7558/*
7559 * Convert the string "text" to a floating point number.
7560 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7561 * this always uses a decimal point.
7562 * Returns the length of the text that was consumed.
7563 */
7564 static int
7565string2float(text, value)
7566 char_u *text;
7567 float_T *value; /* result stored here */
7568{
7569 char *s = (char *)text;
7570 float_T f;
7571
7572 f = strtod(s, &s);
7573 *value = f;
7574 return (int)((char_u *)s - text);
7575}
7576#endif
7577
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579 * Get the value of an environment variable.
7580 * "arg" is pointing to the '$'. It is advanced to after the name.
7581 * If the environment variable was not set, silently assume it is empty.
7582 * Always return OK.
7583 */
7584 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007585get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 int evaluate;
7589{
7590 char_u *string = NULL;
7591 int len;
7592 int cc;
7593 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007594 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595
7596 ++*arg;
7597 name = *arg;
7598 len = get_env_len(arg);
7599 if (evaluate)
7600 {
7601 if (len != 0)
7602 {
7603 cc = name[len];
7604 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007605 /* first try vim_getenv(), fast for normal environment vars */
7606 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007608 {
7609 if (!mustfree)
7610 string = vim_strsave(string);
7611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 else
7613 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007614 if (mustfree)
7615 vim_free(string);
7616
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 /* next try expanding things like $VIM and ${HOME} */
7618 string = expand_env_save(name - 1);
7619 if (string != NULL && *string == '$')
7620 {
7621 vim_free(string);
7622 string = NULL;
7623 }
7624 }
7625 name[len] = cc;
7626 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007627 rettv->v_type = VAR_STRING;
7628 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 }
7630
7631 return OK;
7632}
7633
7634/*
7635 * Array with names and number of arguments of all internal functions
7636 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7637 */
7638static struct fst
7639{
7640 char *f_name; /* function name */
7641 char f_min_argc; /* minimal number of arguments */
7642 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007643 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007644 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645} functions[] =
7646{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007647#ifdef FEAT_FLOAT
7648 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007649 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007650#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007651 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {"append", 2, 2, f_append},
7653 {"argc", 0, 0, f_argc},
7654 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007655 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007656#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007657 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007658 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007659 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007662 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {"bufexists", 1, 1, f_bufexists},
7664 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7665 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7666 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7667 {"buflisted", 1, 1, f_buflisted},
7668 {"bufloaded", 1, 1, f_bufloaded},
7669 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007670 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {"bufwinnr", 1, 1, f_bufwinnr},
7672 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007673 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007674 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675#ifdef FEAT_FLOAT
7676 {"ceil", 1, 1, f_ceil},
7677#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007678 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 {"char2nr", 1, 1, f_char2nr},
7680 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007681 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007683#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007684 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007685 {"complete_add", 1, 1, f_complete_add},
7686 {"complete_check", 0, 0, f_complete_check},
7687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007689 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007690#ifdef FEAT_FLOAT
7691 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007692 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007693#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007694 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007696 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007697 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {"delete", 1, 1, f_delete},
7699 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007700 {"diff_filler", 1, 1, f_diff_filler},
7701 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007702 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {"eventhandler", 0, 0, f_eventhandler},
7706 {"executable", 1, 1, f_executable},
7707 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007708#ifdef FEAT_FLOAT
7709 {"exp", 1, 1, f_exp},
7710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007712 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007713 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7715 {"filereadable", 1, 1, f_filereadable},
7716 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007717 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007718 {"finddir", 1, 3, f_finddir},
7719 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007720#ifdef FEAT_FLOAT
7721 {"float2nr", 1, 1, f_float2nr},
7722 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007723 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007724#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007725 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 {"fnamemodify", 2, 2, f_fnamemodify},
7727 {"foldclosed", 1, 1, f_foldclosed},
7728 {"foldclosedend", 1, 1, f_foldclosedend},
7729 {"foldlevel", 1, 1, f_foldlevel},
7730 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007731 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007733 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007734 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007735 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007736 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 {"getbufvar", 2, 2, f_getbufvar},
7738 {"getchar", 0, 1, f_getchar},
7739 {"getcharmod", 0, 0, f_getcharmod},
7740 {"getcmdline", 0, 0, f_getcmdline},
7741 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007742 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007744 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007745 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 {"getfsize", 1, 1, f_getfsize},
7747 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007748 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007749 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007750 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007751 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007752 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007753 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007754 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007755 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007757 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007758 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {"getwinposx", 0, 0, f_getwinposx},
7760 {"getwinposy", 0, 0, f_getwinposy},
7761 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007762 {"glob", 1, 2, f_glob},
7763 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007765 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007766 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007767 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7769 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7770 {"histadd", 2, 2, f_histadd},
7771 {"histdel", 1, 2, f_histdel},
7772 {"histget", 1, 2, f_histget},
7773 {"histnr", 1, 1, f_histnr},
7774 {"hlID", 1, 1, f_hlID},
7775 {"hlexists", 1, 1, f_hlexists},
7776 {"hostname", 0, 0, f_hostname},
7777 {"iconv", 3, 3, f_iconv},
7778 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007779 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007780 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007782 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 {"inputrestore", 0, 0, f_inputrestore},
7784 {"inputsave", 0, 0, f_inputsave},
7785 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007786 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007788 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007789 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007790 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007791 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007793 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {"libcall", 3, 3, f_libcall},
7795 {"libcallnr", 3, 3, f_libcallnr},
7796 {"line", 1, 1, f_line},
7797 {"line2byte", 1, 1, f_line2byte},
7798 {"lispindent", 1, 1, f_lispindent},
7799 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007800#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007801 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007802 {"log10", 1, 1, f_log10},
7803#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007804 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007805 {"maparg", 1, 3, f_maparg},
7806 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007807 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007808 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007809 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007810 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007811 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007812 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007813 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007814 {"max", 1, 1, f_max},
7815 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007816#ifdef vim_mkdir
7817 {"mkdir", 1, 3, f_mkdir},
7818#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007819 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007820#ifdef FEAT_MZSCHEME
7821 {"mzeval", 1, 1, f_mzeval},
7822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {"nextnonblank", 1, 1, f_nextnonblank},
7824 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007825 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007826#ifdef FEAT_FLOAT
7827 {"pow", 2, 2, f_pow},
7828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007830 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007831 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007832 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007833 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007834 {"reltime", 0, 2, f_reltime},
7835 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 {"remote_expr", 2, 3, f_remote_expr},
7837 {"remote_foreground", 1, 1, f_remote_foreground},
7838 {"remote_peek", 1, 2, f_remote_peek},
7839 {"remote_read", 1, 1, f_remote_read},
7840 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007841 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007843 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007845 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007846#ifdef FEAT_FLOAT
7847 {"round", 1, 1, f_round},
7848#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007849 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007850 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007851 {"searchpair", 3, 7, f_searchpair},
7852 {"searchpairpos", 3, 7, f_searchpairpos},
7853 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 {"server2client", 2, 2, f_server2client},
7855 {"serverlist", 0, 0, f_serverlist},
7856 {"setbufvar", 3, 3, f_setbufvar},
7857 {"setcmdpos", 1, 1, f_setcmdpos},
7858 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007859 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007860 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007861 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007862 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007864 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007865 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007867 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007869#ifdef FEAT_FLOAT
7870 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007871 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007872#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007873 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007874 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007875 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007876 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007877 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007878#ifdef FEAT_FLOAT
7879 {"sqrt", 1, 1, f_sqrt},
7880 {"str2float", 1, 1, f_str2float},
7881#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007882 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007883 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007884 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885#ifdef HAVE_STRFTIME
7886 {"strftime", 1, 2, f_strftime},
7887#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007888 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007889 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 {"strlen", 1, 1, f_strlen},
7891 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007892 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007894 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"submatch", 1, 1, f_submatch},
7896 {"substitute", 4, 4, f_substitute},
7897 {"synID", 3, 3, f_synID},
7898 {"synIDattr", 2, 3, f_synIDattr},
7899 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007900 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007901 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007902 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007903 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007904 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007905 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007906 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007907 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007908#ifdef FEAT_FLOAT
7909 {"tan", 1, 1, f_tan},
7910 {"tanh", 1, 1, f_tanh},
7911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007913 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"tolower", 1, 1, f_tolower},
7915 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007916 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917#ifdef FEAT_FLOAT
7918 {"trunc", 1, 1, f_trunc},
7919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007921 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007922 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 {"virtcol", 1, 1, f_virtcol},
7925 {"visualmode", 0, 1, f_visualmode},
7926 {"winbufnr", 1, 1, f_winbufnr},
7927 {"wincol", 0, 0, f_wincol},
7928 {"winheight", 1, 1, f_winheight},
7929 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007930 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007932 {"winrestview", 1, 1, f_winrestview},
7933 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007935 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936};
7937
7938#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7939
7940/*
7941 * Function given to ExpandGeneric() to obtain the list of internal
7942 * or user defined function names.
7943 */
7944 char_u *
7945get_function_name(xp, idx)
7946 expand_T *xp;
7947 int idx;
7948{
7949 static int intidx = -1;
7950 char_u *name;
7951
7952 if (idx == 0)
7953 intidx = -1;
7954 if (intidx < 0)
7955 {
7956 name = get_user_func_name(xp, idx);
7957 if (name != NULL)
7958 return name;
7959 }
7960 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7961 {
7962 STRCPY(IObuff, functions[intidx].f_name);
7963 STRCAT(IObuff, "(");
7964 if (functions[intidx].f_max_argc == 0)
7965 STRCAT(IObuff, ")");
7966 return IObuff;
7967 }
7968
7969 return NULL;
7970}
7971
7972/*
7973 * Function given to ExpandGeneric() to obtain the list of internal or
7974 * user defined variable or function names.
7975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 char_u *
7977get_expr_name(xp, idx)
7978 expand_T *xp;
7979 int idx;
7980{
7981 static int intidx = -1;
7982 char_u *name;
7983
7984 if (idx == 0)
7985 intidx = -1;
7986 if (intidx < 0)
7987 {
7988 name = get_function_name(xp, idx);
7989 if (name != NULL)
7990 return name;
7991 }
7992 return get_user_var_name(xp, ++intidx);
7993}
7994
7995#endif /* FEAT_CMDL_COMPL */
7996
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007997#if defined(EBCDIC) || defined(PROTO)
7998/*
7999 * Compare struct fst by function name.
8000 */
8001 static int
8002compare_func_name(s1, s2)
8003 const void *s1;
8004 const void *s2;
8005{
8006 struct fst *p1 = (struct fst *)s1;
8007 struct fst *p2 = (struct fst *)s2;
8008
8009 return STRCMP(p1->f_name, p2->f_name);
8010}
8011
8012/*
8013 * Sort the function table by function name.
8014 * The sorting of the table above is ASCII dependant.
8015 * On machines using EBCDIC we have to sort it.
8016 */
8017 static void
8018sortFunctions()
8019{
8020 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8021
8022 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8023}
8024#endif
8025
8026
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027/*
8028 * Find internal function in table above.
8029 * Return index, or -1 if not found
8030 */
8031 static int
8032find_internal_func(name)
8033 char_u *name; /* name of the function */
8034{
8035 int first = 0;
8036 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8037 int cmp;
8038 int x;
8039
8040 /*
8041 * Find the function name in the table. Binary search.
8042 */
8043 while (first <= last)
8044 {
8045 x = first + ((unsigned)(last - first) >> 1);
8046 cmp = STRCMP(name, functions[x].f_name);
8047 if (cmp < 0)
8048 last = x - 1;
8049 else if (cmp > 0)
8050 first = x + 1;
8051 else
8052 return x;
8053 }
8054 return -1;
8055}
8056
8057/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008058 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8059 * name it contains, otherwise return "name".
8060 */
8061 static char_u *
8062deref_func_name(name, lenp)
8063 char_u *name;
8064 int *lenp;
8065{
Bram Moolenaar33570922005-01-25 22:26:29 +00008066 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008067 int cc;
8068
8069 cc = name[*lenp];
8070 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008071 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008072 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008073 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008074 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008076 {
8077 *lenp = 0;
8078 return (char_u *)""; /* just in case */
8079 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008080 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008081 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008082 }
8083
8084 return name;
8085}
8086
8087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 * Allocate a variable for the result of a function.
8089 * Return OK or FAIL.
8090 */
8091 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008092get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8093 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 char_u *name; /* name of the function */
8095 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 char_u **arg; /* argument, pointing to the '(' */
8098 linenr_T firstline; /* first line of range */
8099 linenr_T lastline; /* last line of range */
8100 int *doesrange; /* return: function handled range */
8101 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008102 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104 char_u *argp;
8105 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008106 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 int argcount = 0; /* number of arguments found */
8108
8109 /*
8110 * Get the arguments.
8111 */
8112 argp = *arg;
8113 while (argcount < MAX_FUNC_ARGS)
8114 {
8115 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8116 if (*argp == ')' || *argp == ',' || *argp == NUL)
8117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8119 {
8120 ret = FAIL;
8121 break;
8122 }
8123 ++argcount;
8124 if (*argp != ',')
8125 break;
8126 }
8127 if (*argp == ')')
8128 ++argp;
8129 else
8130 ret = FAIL;
8131
8132 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008133 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008134 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008136 {
8137 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008138 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008139 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008140 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142
8143 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008144 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145
8146 *arg = skipwhite(argp);
8147 return ret;
8148}
8149
8150
8151/*
8152 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008153 * Return OK when the function can't be called, FAIL otherwise.
8154 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 */
8156 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008157call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008158 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008159 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008161 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008163 typval_T *argvars; /* vars for arguments, must have "argcount"
8164 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 linenr_T firstline; /* first line of range */
8166 linenr_T lastline; /* last line of range */
8167 int *doesrange; /* return: function handled range */
8168 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008169 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170{
8171 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172#define ERROR_UNKNOWN 0
8173#define ERROR_TOOMANY 1
8174#define ERROR_TOOFEW 2
8175#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008176#define ERROR_DICT 4
8177#define ERROR_NONE 5
8178#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 int error = ERROR_NONE;
8180 int i;
8181 int llen;
8182 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183#define FLEN_FIXED 40
8184 char_u fname_buf[FLEN_FIXED + 1];
8185 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008186 char_u *name;
8187
8188 /* Make a copy of the name, if it comes from a funcref variable it could
8189 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008190 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008191 if (name == NULL)
8192 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193
8194 /*
8195 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8196 * Change <SNR>123_name() to K_SNR 123_name().
8197 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 llen = eval_fname_script(name);
8200 if (llen > 0)
8201 {
8202 fname_buf[0] = K_SPECIAL;
8203 fname_buf[1] = KS_EXTRA;
8204 fname_buf[2] = (int)KE_SNR;
8205 i = 3;
8206 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8207 {
8208 if (current_SID <= 0)
8209 error = ERROR_SCRIPT;
8210 else
8211 {
8212 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8213 i = (int)STRLEN(fname_buf);
8214 }
8215 }
8216 if (i + STRLEN(name + llen) < FLEN_FIXED)
8217 {
8218 STRCPY(fname_buf + i, name + llen);
8219 fname = fname_buf;
8220 }
8221 else
8222 {
8223 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8224 if (fname == NULL)
8225 error = ERROR_OTHER;
8226 else
8227 {
8228 mch_memmove(fname, fname_buf, (size_t)i);
8229 STRCPY(fname + i, name + llen);
8230 }
8231 }
8232 }
8233 else
8234 fname = name;
8235
8236 *doesrange = FALSE;
8237
8238
8239 /* execute the function if no errors detected and executing */
8240 if (evaluate && error == ERROR_NONE)
8241 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008242 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8243 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 error = ERROR_UNKNOWN;
8245
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008246 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {
8248 /*
8249 * User defined function.
8250 */
8251 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008252
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008254 /* Trigger FuncUndefined event, may load the function. */
8255 if (fp == NULL
8256 && apply_autocmds(EVENT_FUNCUNDEFINED,
8257 fname, fname, TRUE, NULL)
8258 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008260 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 fp = find_func(fname);
8262 }
8263#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008264 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008265 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008266 {
8267 /* loaded a package, search for the function again */
8268 fp = find_func(fname);
8269 }
8270
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 if (fp != NULL)
8272 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008273 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008275 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008277 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008279 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008280 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 else
8282 {
8283 /*
8284 * Call the user function.
8285 * Save and restore search patterns, script variables and
8286 * redo buffer.
8287 */
8288 save_search_patterns();
8289 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008290 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008292 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008293 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8294 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8295 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008296 /* Function was unreferenced while being used, free it
8297 * now. */
8298 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 restoreRedobuff();
8300 restore_search_patterns();
8301 error = ERROR_NONE;
8302 }
8303 }
8304 }
8305 else
8306 {
8307 /*
8308 * Find the function name in the table, call its implementation.
8309 */
8310 i = find_internal_func(fname);
8311 if (i >= 0)
8312 {
8313 if (argcount < functions[i].f_min_argc)
8314 error = ERROR_TOOFEW;
8315 else if (argcount > functions[i].f_max_argc)
8316 error = ERROR_TOOMANY;
8317 else
8318 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008319 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 error = ERROR_NONE;
8322 }
8323 }
8324 }
8325 /*
8326 * The function call (or "FuncUndefined" autocommand sequence) might
8327 * have been aborted by an error, an interrupt, or an explicitly thrown
8328 * exception that has not been caught so far. This situation can be
8329 * tested for by calling aborting(). For an error in an internal
8330 * function or for the "E132" error in call_user_func(), however, the
8331 * throw point at which the "force_abort" flag (temporarily reset by
8332 * emsg()) is normally updated has not been reached yet. We need to
8333 * update that flag first to make aborting() reliable.
8334 */
8335 update_force_abort();
8336 }
8337 if (error == ERROR_NONE)
8338 ret = OK;
8339
8340 /*
8341 * Report an error unless the argument evaluation or function call has been
8342 * cancelled due to an aborting error, an interrupt, or an exception.
8343 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008344 if (!aborting())
8345 {
8346 switch (error)
8347 {
8348 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008349 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008350 break;
8351 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008352 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008353 break;
8354 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008355 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008356 name);
8357 break;
8358 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008359 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008360 name);
8361 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008362 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008363 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008364 name);
8365 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008366 }
8367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 if (fname != name && fname != fname_buf)
8370 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008371 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372
8373 return ret;
8374}
8375
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008376/*
8377 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008378 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008379 */
8380 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008381emsg_funcname(ermsg, name)
8382 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008383 char_u *name;
8384{
8385 char_u *p;
8386
8387 if (*name == K_SPECIAL)
8388 p = concat_str((char_u *)"<SNR>", name + 3);
8389 else
8390 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008391 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008392 if (p != name)
8393 vim_free(p);
8394}
8395
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008396/*
8397 * Return TRUE for a non-zero Number and a non-empty String.
8398 */
8399 static int
8400non_zero_arg(argvars)
8401 typval_T *argvars;
8402{
8403 return ((argvars[0].v_type == VAR_NUMBER
8404 && argvars[0].vval.v_number != 0)
8405 || (argvars[0].v_type == VAR_STRING
8406 && argvars[0].vval.v_string != NULL
8407 && *argvars[0].vval.v_string != NUL));
8408}
8409
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410/*********************************************
8411 * Implementation of the built-in functions
8412 */
8413
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008414#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008415static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8416
8417/*
8418 * Get the float value of "argvars[0]" into "f".
8419 * Returns FAIL when the argument is not a Number or Float.
8420 */
8421 static int
8422get_float_arg(argvars, f)
8423 typval_T *argvars;
8424 float_T *f;
8425{
8426 if (argvars[0].v_type == VAR_FLOAT)
8427 {
8428 *f = argvars[0].vval.v_float;
8429 return OK;
8430 }
8431 if (argvars[0].v_type == VAR_NUMBER)
8432 {
8433 *f = (float_T)argvars[0].vval.v_number;
8434 return OK;
8435 }
8436 EMSG(_("E808: Number or Float required"));
8437 return FAIL;
8438}
8439
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008440/*
8441 * "abs(expr)" function
8442 */
8443 static void
8444f_abs(argvars, rettv)
8445 typval_T *argvars;
8446 typval_T *rettv;
8447{
8448 if (argvars[0].v_type == VAR_FLOAT)
8449 {
8450 rettv->v_type = VAR_FLOAT;
8451 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8452 }
8453 else
8454 {
8455 varnumber_T n;
8456 int error = FALSE;
8457
8458 n = get_tv_number_chk(&argvars[0], &error);
8459 if (error)
8460 rettv->vval.v_number = -1;
8461 else if (n > 0)
8462 rettv->vval.v_number = n;
8463 else
8464 rettv->vval.v_number = -n;
8465 }
8466}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008467
8468/*
8469 * "acos()" function
8470 */
8471 static void
8472f_acos(argvars, rettv)
8473 typval_T *argvars;
8474 typval_T *rettv;
8475{
8476 float_T f;
8477
8478 rettv->v_type = VAR_FLOAT;
8479 if (get_float_arg(argvars, &f) == OK)
8480 rettv->vval.v_float = acos(f);
8481 else
8482 rettv->vval.v_float = 0.0;
8483}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008484#endif
8485
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008487 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 */
8489 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008490f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008491 typval_T *argvars;
8492 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493{
Bram Moolenaar33570922005-01-25 22:26:29 +00008494 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008496 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008497 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008499 if ((l = argvars[0].vval.v_list) != NULL
8500 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8501 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008503 }
8504 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008505 EMSG(_(e_listreq));
8506}
8507
8508/*
8509 * "append(lnum, string/list)" function
8510 */
8511 static void
8512f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 typval_T *argvars;
8514 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008515{
8516 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008517 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008518 list_T *l = NULL;
8519 listitem_T *li = NULL;
8520 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008521 long added = 0;
8522
Bram Moolenaar0d660222005-01-07 21:51:51 +00008523 lnum = get_tv_lnum(argvars);
8524 if (lnum >= 0
8525 && lnum <= curbuf->b_ml.ml_line_count
8526 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008528 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008530 l = argvars[1].vval.v_list;
8531 if (l == NULL)
8532 return;
8533 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008534 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008535 for (;;)
8536 {
8537 if (l == NULL)
8538 tv = &argvars[1]; /* append a string */
8539 else if (li == NULL)
8540 break; /* end of list */
8541 else
8542 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008543 line = get_tv_string_chk(tv);
8544 if (line == NULL) /* type error */
8545 {
8546 rettv->vval.v_number = 1; /* Failed */
8547 break;
8548 }
8549 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008550 ++added;
8551 if (l == NULL)
8552 break;
8553 li = li->li_next;
8554 }
8555
8556 appended_lines_mark(lnum, added);
8557 if (curwin->w_cursor.lnum > lnum)
8558 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008560 else
8561 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562}
8563
8564/*
8565 * "argc()" function
8566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008568f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008569 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008572 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573}
8574
8575/*
8576 * "argidx()" function
8577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008580 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008581 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008583 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584}
8585
8586/*
8587 * "argv(nr)" function
8588 */
8589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T *argvars;
8592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 int idx;
8595
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008596 if (argvars[0].v_type != VAR_UNKNOWN)
8597 {
8598 idx = get_tv_number_chk(&argvars[0], NULL);
8599 if (idx >= 0 && idx < ARGCOUNT)
8600 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8601 else
8602 rettv->vval.v_string = NULL;
8603 rettv->v_type = VAR_STRING;
8604 }
8605 else if (rettv_list_alloc(rettv) == OK)
8606 for (idx = 0; idx < ARGCOUNT; ++idx)
8607 list_append_string(rettv->vval.v_list,
8608 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
8610
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008611#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008612/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008613 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008614 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008615 static void
8616f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008617 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008618 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008619{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008620 float_T f;
8621
8622 rettv->v_type = VAR_FLOAT;
8623 if (get_float_arg(argvars, &f) == OK)
8624 rettv->vval.v_float = asin(f);
8625 else
8626 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008627}
8628
8629/*
8630 * "atan()" function
8631 */
8632 static void
8633f_atan(argvars, rettv)
8634 typval_T *argvars;
8635 typval_T *rettv;
8636{
8637 float_T f;
8638
8639 rettv->v_type = VAR_FLOAT;
8640 if (get_float_arg(argvars, &f) == OK)
8641 rettv->vval.v_float = atan(f);
8642 else
8643 rettv->vval.v_float = 0.0;
8644}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008645
8646/*
8647 * "atan2()" function
8648 */
8649 static void
8650f_atan2(argvars, rettv)
8651 typval_T *argvars;
8652 typval_T *rettv;
8653{
8654 float_T fx, fy;
8655
8656 rettv->v_type = VAR_FLOAT;
8657 if (get_float_arg(argvars, &fx) == OK
8658 && get_float_arg(&argvars[1], &fy) == OK)
8659 rettv->vval.v_float = atan2(fx, fy);
8660 else
8661 rettv->vval.v_float = 0.0;
8662}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008663#endif
8664
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665/*
8666 * "browse(save, title, initdir, default)" function
8667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008669f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008670 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672{
8673#ifdef FEAT_BROWSE
8674 int save;
8675 char_u *title;
8676 char_u *initdir;
8677 char_u *defname;
8678 char_u buf[NUMBUFLEN];
8679 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008680 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008682 save = get_tv_number_chk(&argvars[0], &error);
8683 title = get_tv_string_chk(&argvars[1]);
8684 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8685 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008687 if (error || title == NULL || initdir == NULL || defname == NULL)
8688 rettv->vval.v_string = NULL;
8689 else
8690 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008691 do_browse(save ? BROWSE_SAVE : 0,
8692 title, defname, NULL, initdir, NULL, curbuf);
8693#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008694 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008695#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008696 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008697}
8698
8699/*
8700 * "browsedir(title, initdir)" function
8701 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008703f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008704 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008706{
8707#ifdef FEAT_BROWSE
8708 char_u *title;
8709 char_u *initdir;
8710 char_u buf[NUMBUFLEN];
8711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008712 title = get_tv_string_chk(&argvars[0]);
8713 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008715 if (title == NULL || initdir == NULL)
8716 rettv->vval.v_string = NULL;
8717 else
8718 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008719 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008721 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724}
8725
Bram Moolenaar33570922005-01-25 22:26:29 +00008726static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008727
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728/*
8729 * Find a buffer by number or exact name.
8730 */
8731 static buf_T *
8732find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734{
8735 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008737 if (avar->v_type == VAR_NUMBER)
8738 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008739 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008741 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008742 if (buf == NULL)
8743 {
8744 /* No full path name match, try a match with a URL or a "nofile"
8745 * buffer, these don't use the full path. */
8746 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8747 if (buf->b_fname != NULL
8748 && (path_with_url(buf->b_fname)
8749#ifdef FEAT_QUICKFIX
8750 || bt_nofile(buf)
8751#endif
8752 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008753 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008754 break;
8755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 }
8757 return buf;
8758}
8759
8760/*
8761 * "bufexists(expr)" function
8762 */
8763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008764f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008765 typval_T *argvars;
8766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008768 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769}
8770
8771/*
8772 * "buflisted(expr)" function
8773 */
8774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008775f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 typval_T *argvars;
8777 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 buf_T *buf;
8780
8781 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783}
8784
8785/*
8786 * "bufloaded(expr)" function
8787 */
8788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008789f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008790 typval_T *argvars;
8791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792{
8793 buf_T *buf;
8794
8795 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008796 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797}
8798
Bram Moolenaar33570922005-01-25 22:26:29 +00008799static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008800
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801/*
8802 * Get buffer by number or pattern.
8803 */
8804 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 int save_magic;
8810 char_u *save_cpo;
8811 buf_T *buf;
8812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813 if (tv->v_type == VAR_NUMBER)
8814 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008815 if (tv->v_type != VAR_STRING)
8816 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 if (name == NULL || *name == NUL)
8818 return curbuf;
8819 if (name[0] == '$' && name[1] == NUL)
8820 return lastbuf;
8821
8822 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8823 save_magic = p_magic;
8824 p_magic = TRUE;
8825 save_cpo = p_cpo;
8826 p_cpo = (char_u *)"";
8827
8828 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8829 TRUE, FALSE));
8830
8831 p_magic = save_magic;
8832 p_cpo = save_cpo;
8833
8834 /* If not found, try expanding the name, like done for bufexists(). */
8835 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837
8838 return buf;
8839}
8840
8841/*
8842 * "bufname(expr)" function
8843 */
8844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008845f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008846 typval_T *argvars;
8847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 buf_T *buf;
8850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008851 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008853 buf = get_buf_tv(&argvars[0]);
8854 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 --emsg_off;
8860}
8861
8862/*
8863 * "bufnr(expr)" function
8864 */
8865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008866f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008867 typval_T *argvars;
8868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869{
8870 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008871 int error = FALSE;
8872 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008874 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008877 --emsg_off;
8878
8879 /* If the buffer isn't found and the second argument is not zero create a
8880 * new buffer. */
8881 if (buf == NULL
8882 && argvars[1].v_type != VAR_UNKNOWN
8883 && get_tv_number_chk(&argvars[1], &error) != 0
8884 && !error
8885 && (name = get_tv_string_chk(&argvars[0])) != NULL
8886 && !error)
8887 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8888
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008890 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893}
8894
8895/*
8896 * "bufwinnr(nr)" function
8897 */
8898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008900 typval_T *argvars;
8901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903#ifdef FEAT_WINDOWS
8904 win_T *wp;
8905 int winnr = 0;
8906#endif
8907 buf_T *buf;
8908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912#ifdef FEAT_WINDOWS
8913 for (wp = firstwin; wp; wp = wp->w_next)
8914 {
8915 ++winnr;
8916 if (wp->w_buffer == buf)
8917 break;
8918 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922#endif
8923 --emsg_off;
8924}
8925
8926/*
8927 * "byte2line(byte)" function
8928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008931 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933{
8934#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936#else
8937 long boff = 0;
8938
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008939 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 (linenr_T)0, &boff);
8945#endif
8946}
8947
8948/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008949 * "byteidx()" function
8950 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008951 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 typval_T *argvars;
8954 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008955{
8956#ifdef FEAT_MBYTE
8957 char_u *t;
8958#endif
8959 char_u *str;
8960 long idx;
8961
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008962 str = get_tv_string_chk(&argvars[0]);
8963 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008964 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008965 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008966 return;
8967
8968#ifdef FEAT_MBYTE
8969 t = str;
8970 for ( ; idx > 0; idx--)
8971 {
8972 if (*t == NUL) /* EOL reached */
8973 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008974 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008975 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008976 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008977#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008978 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008980#endif
8981}
8982
8983/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008984 * "call(func, arglist)" function
8985 */
8986 static void
8987f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008988 typval_T *argvars;
8989 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008990{
8991 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008992 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008993 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008994 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008995 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008997
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008998 if (argvars[1].v_type != VAR_LIST)
8999 {
9000 EMSG(_(e_listreq));
9001 return;
9002 }
9003 if (argvars[1].vval.v_list == NULL)
9004 return;
9005
9006 if (argvars[0].v_type == VAR_FUNC)
9007 func = argvars[0].vval.v_string;
9008 else
9009 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009010 if (*func == NUL)
9011 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009012
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 if (argvars[2].v_type != VAR_UNKNOWN)
9014 {
9015 if (argvars[2].v_type != VAR_DICT)
9016 {
9017 EMSG(_(e_dictreq));
9018 return;
9019 }
9020 selfdict = argvars[2].vval.v_dict;
9021 }
9022
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009023 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9024 item = item->li_next)
9025 {
9026 if (argc == MAX_FUNC_ARGS)
9027 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009028 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009029 break;
9030 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009031 /* Make a copy of each argument. This is needed to be able to set
9032 * v_lock to VAR_FIXED in the copy without changing the original list.
9033 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009034 copy_tv(&item->li_tv, &argv[argc++]);
9035 }
9036
9037 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009038 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009039 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9040 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009041
9042 /* Free the arguments. */
9043 while (argc > 0)
9044 clear_tv(&argv[--argc]);
9045}
9046
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009047#ifdef FEAT_FLOAT
9048/*
9049 * "ceil({float})" function
9050 */
9051 static void
9052f_ceil(argvars, rettv)
9053 typval_T *argvars;
9054 typval_T *rettv;
9055{
9056 float_T f;
9057
9058 rettv->v_type = VAR_FLOAT;
9059 if (get_float_arg(argvars, &f) == OK)
9060 rettv->vval.v_float = ceil(f);
9061 else
9062 rettv->vval.v_float = 0.0;
9063}
9064#endif
9065
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009066/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009067 * "changenr()" function
9068 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009069 static void
9070f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009071 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009072 typval_T *rettv;
9073{
9074 rettv->vval.v_number = curbuf->b_u_seq_cur;
9075}
9076
9077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 * "char2nr(string)" function
9079 */
9080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009082 typval_T *argvars;
9083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084{
9085#ifdef FEAT_MBYTE
9086 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009087 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 else
9089#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091}
9092
9093/*
9094 * "cindent(lnum)" function
9095 */
9096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 typval_T *argvars;
9099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
9101#ifdef FEAT_CINDENT
9102 pos_T pos;
9103 linenr_T lnum;
9104
9105 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9108 {
9109 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 curwin->w_cursor = pos;
9112 }
9113 else
9114#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
9118/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009119 * "clearmatches()" function
9120 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009121 static void
9122f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009123 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009124 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009125{
9126#ifdef FEAT_SEARCH_EXTRA
9127 clear_matches(curwin);
9128#endif
9129}
9130
9131/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 * "col(string)" function
9133 */
9134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009136 typval_T *argvars;
9137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139 colnr_T col = 0;
9140 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009141 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009143 fp = var2fpos(&argvars[0], FALSE, &fnum);
9144 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 {
9146 if (fp->col == MAXCOL)
9147 {
9148 /* '> can be MAXCOL, get the length of the line then */
9149 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009150 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 else
9152 col = MAXCOL;
9153 }
9154 else
9155 {
9156 col = fp->col + 1;
9157#ifdef FEAT_VIRTUALEDIT
9158 /* col(".") when the cursor is on the NUL at the end of the line
9159 * because of "coladd" can be seen as an extra column. */
9160 if (virtual_active() && fp == &curwin->w_cursor)
9161 {
9162 char_u *p = ml_get_cursor();
9163
9164 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9165 curwin->w_virtcol - curwin->w_cursor.coladd))
9166 {
9167# ifdef FEAT_MBYTE
9168 int l;
9169
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009170 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 col += l;
9172# else
9173 if (*p != NUL && p[1] == NUL)
9174 ++col;
9175# endif
9176 }
9177 }
9178#endif
9179 }
9180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009181 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182}
9183
Bram Moolenaar572cb562005-08-05 21:35:02 +00009184#if defined(FEAT_INS_EXPAND)
9185/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009186 * "complete()" function
9187 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009188 static void
9189f_complete(argvars, rettv)
9190 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009191 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009192{
9193 int startcol;
9194
9195 if ((State & INSERT) == 0)
9196 {
9197 EMSG(_("E785: complete() can only be used in Insert mode"));
9198 return;
9199 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009200
9201 /* Check for undo allowed here, because if something was already inserted
9202 * the line was already saved for undo and this check isn't done. */
9203 if (!undo_allowed())
9204 return;
9205
Bram Moolenaarade00832006-03-10 21:46:58 +00009206 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9207 {
9208 EMSG(_(e_invarg));
9209 return;
9210 }
9211
9212 startcol = get_tv_number_chk(&argvars[0], NULL);
9213 if (startcol <= 0)
9214 return;
9215
9216 set_completion(startcol - 1, argvars[1].vval.v_list);
9217}
9218
9219/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009220 * "complete_add()" function
9221 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009222 static void
9223f_complete_add(argvars, rettv)
9224 typval_T *argvars;
9225 typval_T *rettv;
9226{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009227 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009228}
9229
9230/*
9231 * "complete_check()" function
9232 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009233 static void
9234f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009235 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009236 typval_T *rettv;
9237{
9238 int saved = RedrawingDisabled;
9239
9240 RedrawingDisabled = 0;
9241 ins_compl_check_keys(0);
9242 rettv->vval.v_number = compl_interrupted;
9243 RedrawingDisabled = saved;
9244}
9245#endif
9246
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247/*
9248 * "confirm(message, buttons[, default [, type]])" function
9249 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009251f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009252 typval_T *argvars UNUSED;
9253 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254{
9255#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9256 char_u *message;
9257 char_u *buttons = NULL;
9258 char_u buf[NUMBUFLEN];
9259 char_u buf2[NUMBUFLEN];
9260 int def = 1;
9261 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009262 char_u *typestr;
9263 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009265 message = get_tv_string_chk(&argvars[0]);
9266 if (message == NULL)
9267 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009268 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009270 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9271 if (buttons == NULL)
9272 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009273 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009275 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009276 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009278 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9279 if (typestr == NULL)
9280 error = TRUE;
9281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009283 switch (TOUPPER_ASC(*typestr))
9284 {
9285 case 'E': type = VIM_ERROR; break;
9286 case 'Q': type = VIM_QUESTION; break;
9287 case 'I': type = VIM_INFO; break;
9288 case 'W': type = VIM_WARNING; break;
9289 case 'G': type = VIM_GENERIC; break;
9290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291 }
9292 }
9293 }
9294 }
9295
9296 if (buttons == NULL || *buttons == NUL)
9297 buttons = (char_u *)_("&Ok");
9298
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009299 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009300 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302#endif
9303}
9304
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009305/*
9306 * "copy()" function
9307 */
9308 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009309f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009310 typval_T *argvars;
9311 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009312{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009313 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009314}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009316#ifdef FEAT_FLOAT
9317/*
9318 * "cos()" function
9319 */
9320 static void
9321f_cos(argvars, rettv)
9322 typval_T *argvars;
9323 typval_T *rettv;
9324{
9325 float_T f;
9326
9327 rettv->v_type = VAR_FLOAT;
9328 if (get_float_arg(argvars, &f) == OK)
9329 rettv->vval.v_float = cos(f);
9330 else
9331 rettv->vval.v_float = 0.0;
9332}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009333
9334/*
9335 * "cosh()" function
9336 */
9337 static void
9338f_cosh(argvars, rettv)
9339 typval_T *argvars;
9340 typval_T *rettv;
9341{
9342 float_T f;
9343
9344 rettv->v_type = VAR_FLOAT;
9345 if (get_float_arg(argvars, &f) == OK)
9346 rettv->vval.v_float = cosh(f);
9347 else
9348 rettv->vval.v_float = 0.0;
9349}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009350#endif
9351
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009353 * "count()" function
9354 */
9355 static void
9356f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009357 typval_T *argvars;
9358 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009359{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009360 long n = 0;
9361 int ic = FALSE;
9362
Bram Moolenaare9a41262005-01-15 22:18:47 +00009363 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009364 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009365 listitem_T *li;
9366 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009367 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009368
Bram Moolenaare9a41262005-01-15 22:18:47 +00009369 if ((l = argvars[0].vval.v_list) != NULL)
9370 {
9371 li = l->lv_first;
9372 if (argvars[2].v_type != VAR_UNKNOWN)
9373 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009374 int error = FALSE;
9375
9376 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009377 if (argvars[3].v_type != VAR_UNKNOWN)
9378 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009379 idx = get_tv_number_chk(&argvars[3], &error);
9380 if (!error)
9381 {
9382 li = list_find(l, idx);
9383 if (li == NULL)
9384 EMSGN(_(e_listidx), idx);
9385 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009386 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009387 if (error)
9388 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009389 }
9390
9391 for ( ; li != NULL; li = li->li_next)
9392 if (tv_equal(&li->li_tv, &argvars[1], ic))
9393 ++n;
9394 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009395 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009396 else if (argvars[0].v_type == VAR_DICT)
9397 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009398 int todo;
9399 dict_T *d;
9400 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009401
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009402 if ((d = argvars[0].vval.v_dict) != NULL)
9403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009404 int error = FALSE;
9405
Bram Moolenaare9a41262005-01-15 22:18:47 +00009406 if (argvars[2].v_type != VAR_UNKNOWN)
9407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009408 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009409 if (argvars[3].v_type != VAR_UNKNOWN)
9410 EMSG(_(e_invarg));
9411 }
9412
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009413 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009414 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009415 {
9416 if (!HASHITEM_EMPTY(hi))
9417 {
9418 --todo;
9419 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9420 ++n;
9421 }
9422 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009423 }
9424 }
9425 else
9426 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009427 rettv->vval.v_number = n;
9428}
9429
9430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9432 *
9433 * Checks the existence of a cscope connection.
9434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009437 typval_T *argvars UNUSED;
9438 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440#ifdef FEAT_CSCOPE
9441 int num = 0;
9442 char_u *dbpath = NULL;
9443 char_u *prepend = NULL;
9444 char_u buf[NUMBUFLEN];
9445
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009446 if (argvars[0].v_type != VAR_UNKNOWN
9447 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 num = (int)get_tv_number(&argvars[0]);
9450 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009451 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009452 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 }
9454
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456#endif
9457}
9458
9459/*
9460 * "cursor(lnum, col)" function
9461 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009462 * Moves the cursor to the specified line and column.
9463 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009466f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009467 typval_T *argvars;
9468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009471#ifdef FEAT_VIRTUALEDIT
9472 long coladd = 0;
9473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009475 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009476 if (argvars[1].v_type == VAR_UNKNOWN)
9477 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009478 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009479
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009480 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009481 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009482 line = pos.lnum;
9483 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009484#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009485 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009486#endif
9487 }
9488 else
9489 {
9490 line = get_tv_lnum(argvars);
9491 col = get_tv_number_chk(&argvars[1], NULL);
9492#ifdef FEAT_VIRTUALEDIT
9493 if (argvars[2].v_type != VAR_UNKNOWN)
9494 coladd = get_tv_number_chk(&argvars[2], NULL);
9495#endif
9496 }
9497 if (line < 0 || col < 0
9498#ifdef FEAT_VIRTUALEDIT
9499 || coladd < 0
9500#endif
9501 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009502 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 if (line > 0)
9504 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 if (col > 0)
9506 curwin->w_cursor.col = col - 1;
9507#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009508 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509#endif
9510
9511 /* Make sure the cursor is in a valid position. */
9512 check_cursor();
9513#ifdef FEAT_MBYTE
9514 /* Correct cursor for multi-byte character. */
9515 if (has_mbyte)
9516 mb_adjust_cursor();
9517#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009518
9519 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009520 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521}
9522
9523/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009524 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 */
9526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009527f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009528 typval_T *argvars;
9529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009531 int noref = 0;
9532
9533 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009535 if (noref < 0 || noref > 1)
9536 EMSG(_(e_invarg));
9537 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009538 {
9539 current_copyID += COPYID_INC;
9540 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542}
9543
9544/*
9545 * "delete()" function
9546 */
9547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009548f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009549 typval_T *argvars;
9550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551{
9552 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556}
9557
9558/*
9559 * "did_filetype()" function
9560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009563 typval_T *argvars UNUSED;
9564 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565{
9566#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568#endif
9569}
9570
9571/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009572 * "diff_filler()" function
9573 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009576 typval_T *argvars UNUSED;
9577 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009578{
9579#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009581#endif
9582}
9583
9584/*
9585 * "diff_hlID()" function
9586 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009588f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009589 typval_T *argvars UNUSED;
9590 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009591{
9592#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009593 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009594 static linenr_T prev_lnum = 0;
9595 static int changedtick = 0;
9596 static int fnum = 0;
9597 static int change_start = 0;
9598 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009599 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009600 int filler_lines;
9601 int col;
9602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009603 if (lnum < 0) /* ignore type error in {lnum} arg */
9604 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009605 if (lnum != prev_lnum
9606 || changedtick != curbuf->b_changedtick
9607 || fnum != curbuf->b_fnum)
9608 {
9609 /* New line, buffer, change: need to get the values. */
9610 filler_lines = diff_check(curwin, lnum);
9611 if (filler_lines < 0)
9612 {
9613 if (filler_lines == -1)
9614 {
9615 change_start = MAXCOL;
9616 change_end = -1;
9617 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9618 hlID = HLF_ADD; /* added line */
9619 else
9620 hlID = HLF_CHD; /* changed line */
9621 }
9622 else
9623 hlID = HLF_ADD; /* added line */
9624 }
9625 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009626 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009627 prev_lnum = lnum;
9628 changedtick = curbuf->b_changedtick;
9629 fnum = curbuf->b_fnum;
9630 }
9631
9632 if (hlID == HLF_CHD || hlID == HLF_TXD)
9633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009635 if (col >= change_start && col <= change_end)
9636 hlID = HLF_TXD; /* changed text */
9637 else
9638 hlID = HLF_CHD; /* changed line */
9639 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009640 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009641#endif
9642}
9643
9644/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009645 * "empty({expr})" function
9646 */
9647 static void
9648f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009649 typval_T *argvars;
9650 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009651{
9652 int n;
9653
9654 switch (argvars[0].v_type)
9655 {
9656 case VAR_STRING:
9657 case VAR_FUNC:
9658 n = argvars[0].vval.v_string == NULL
9659 || *argvars[0].vval.v_string == NUL;
9660 break;
9661 case VAR_NUMBER:
9662 n = argvars[0].vval.v_number == 0;
9663 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009664#ifdef FEAT_FLOAT
9665 case VAR_FLOAT:
9666 n = argvars[0].vval.v_float == 0.0;
9667 break;
9668#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009669 case VAR_LIST:
9670 n = argvars[0].vval.v_list == NULL
9671 || argvars[0].vval.v_list->lv_first == NULL;
9672 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009673 case VAR_DICT:
9674 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009675 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009676 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009677 default:
9678 EMSG2(_(e_intern2), "f_empty()");
9679 n = 0;
9680 }
9681
9682 rettv->vval.v_number = n;
9683}
9684
9685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 * "escape({string}, {chars})" function
9687 */
9688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009689f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009690 typval_T *argvars;
9691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692{
9693 char_u buf[NUMBUFLEN];
9694
Bram Moolenaar758711c2005-02-02 23:11:38 +00009695 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9696 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698}
9699
9700/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009701 * "eval()" function
9702 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009703 static void
9704f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009705 typval_T *argvars;
9706 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009707{
9708 char_u *s;
9709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009710 s = get_tv_string_chk(&argvars[0]);
9711 if (s != NULL)
9712 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009714 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9715 {
9716 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009717 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009718 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009719 else if (*s != NUL)
9720 EMSG(_(e_trailing));
9721}
9722
9723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 * "eventhandler()" function
9725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009728 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732}
9733
9734/*
9735 * "executable()" function
9736 */
9737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009739 typval_T *argvars;
9740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744
9745/*
9746 * "exists()" function
9747 */
9748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009750 typval_T *argvars;
9751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752{
9753 char_u *p;
9754 char_u *name;
9755 int n = FALSE;
9756 int len = 0;
9757
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009758 no_autoload = TRUE;
9759
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009760 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 if (*p == '$') /* environment variable */
9762 {
9763 /* first try "normal" environment variables (fast) */
9764 if (mch_getenv(p + 1) != NULL)
9765 n = TRUE;
9766 else
9767 {
9768 /* try expanding things like $VIM and ${HOME} */
9769 p = expand_env_save(p);
9770 if (p != NULL && *p != '$')
9771 n = TRUE;
9772 vim_free(p);
9773 }
9774 }
9775 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009778 if (*skipwhite(p) != NUL)
9779 n = FALSE; /* trailing garbage */
9780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 else if (*p == '*') /* internal or user defined function */
9782 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009783 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 }
9785 else if (*p == ':')
9786 {
9787 n = cmd_exists(p + 1);
9788 }
9789 else if (*p == '#')
9790 {
9791#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009792 if (p[1] == '#')
9793 n = autocmd_supported(p + 2);
9794 else
9795 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796#endif
9797 }
9798 else /* internal variable */
9799 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009800 char_u *tofree;
9801 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009803 /* get_name_len() takes care of expanding curly braces */
9804 name = p;
9805 len = get_name_len(&p, &tofree, TRUE, FALSE);
9806 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009808 if (tofree != NULL)
9809 name = tofree;
9810 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9811 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009813 /* handle d.key, l[idx], f(expr) */
9814 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9815 if (n)
9816 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 }
9818 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009819 if (*p != NUL)
9820 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009822 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 }
9824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009826
9827 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828}
9829
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009830#ifdef FEAT_FLOAT
9831/*
9832 * "exp()" function
9833 */
9834 static void
9835f_exp(argvars, rettv)
9836 typval_T *argvars;
9837 typval_T *rettv;
9838{
9839 float_T f;
9840
9841 rettv->v_type = VAR_FLOAT;
9842 if (get_float_arg(argvars, &f) == OK)
9843 rettv->vval.v_float = exp(f);
9844 else
9845 rettv->vval.v_float = 0.0;
9846}
9847#endif
9848
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849/*
9850 * "expand()" function
9851 */
9852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009854 typval_T *argvars;
9855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856{
9857 char_u *s;
9858 int len;
9859 char_u *errormsg;
9860 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9861 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009862 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 rettv->v_type = VAR_STRING;
9865 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 if (*s == '%' || *s == '#' || *s == '<')
9867 {
9868 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009869 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870 --emsg_off;
9871 }
9872 else
9873 {
9874 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009875 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 if (argvars[1].v_type != VAR_UNKNOWN
9877 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009879 if (!error)
9880 {
9881 ExpandInit(&xpc);
9882 xpc.xp_context = EXPAND_FILES;
9883 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009884 }
9885 else
9886 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887 }
9888}
9889
9890/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009891 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009892 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009893 */
9894 static void
9895f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 typval_T *argvars;
9897 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009898{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009899 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009900 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009901 list_T *l1, *l2;
9902 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009903 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009905
Bram Moolenaare9a41262005-01-15 22:18:47 +00009906 l1 = argvars[0].vval.v_list;
9907 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009908 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9909 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009910 {
9911 if (argvars[2].v_type != VAR_UNKNOWN)
9912 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009913 before = get_tv_number_chk(&argvars[2], &error);
9914 if (error)
9915 return; /* type error; errmsg already given */
9916
Bram Moolenaar758711c2005-02-02 23:11:38 +00009917 if (before == l1->lv_len)
9918 item = NULL;
9919 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009921 item = list_find(l1, before);
9922 if (item == NULL)
9923 {
9924 EMSGN(_(e_listidx), before);
9925 return;
9926 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009927 }
9928 }
9929 else
9930 item = NULL;
9931 list_extend(l1, l2, item);
9932
Bram Moolenaare9a41262005-01-15 22:18:47 +00009933 copy_tv(&argvars[0], rettv);
9934 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009935 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009936 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9937 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009938 dict_T *d1, *d2;
9939 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009940 char_u *action;
9941 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009942 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009943 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009944
9945 d1 = argvars[0].vval.v_dict;
9946 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009947 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9948 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009949 {
9950 /* Check the third argument. */
9951 if (argvars[2].v_type != VAR_UNKNOWN)
9952 {
9953 static char *(av[]) = {"keep", "force", "error"};
9954
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009955 action = get_tv_string_chk(&argvars[2]);
9956 if (action == NULL)
9957 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009958 for (i = 0; i < 3; ++i)
9959 if (STRCMP(action, av[i]) == 0)
9960 break;
9961 if (i == 3)
9962 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009963 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009964 return;
9965 }
9966 }
9967 else
9968 action = (char_u *)"force";
9969
9970 /* Go over all entries in the second dict and add them to the
9971 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009972 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009973 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009975 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009977 --todo;
9978 di1 = dict_find(d1, hi2->hi_key, -1);
9979 if (di1 == NULL)
9980 {
9981 di1 = dictitem_copy(HI2DI(hi2));
9982 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9983 dictitem_free(di1);
9984 }
9985 else if (*action == 'e')
9986 {
9987 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9988 break;
9989 }
9990 else if (*action == 'f')
9991 {
9992 clear_tv(&di1->di_tv);
9993 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9994 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009995 }
9996 }
9997
Bram Moolenaare9a41262005-01-15 22:18:47 +00009998 copy_tv(&argvars[0], rettv);
9999 }
10000 }
10001 else
10002 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010003}
10004
10005/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010006 * "feedkeys()" function
10007 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010008 static void
10009f_feedkeys(argvars, rettv)
10010 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010011 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010012{
10013 int remap = TRUE;
10014 char_u *keys, *flags;
10015 char_u nbuf[NUMBUFLEN];
10016 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010017 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010018
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010019 /* This is not allowed in the sandbox. If the commands would still be
10020 * executed in the sandbox it would be OK, but it probably happens later,
10021 * when "sandbox" is no longer set. */
10022 if (check_secure())
10023 return;
10024
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010025 keys = get_tv_string(&argvars[0]);
10026 if (*keys != NUL)
10027 {
10028 if (argvars[1].v_type != VAR_UNKNOWN)
10029 {
10030 flags = get_tv_string_buf(&argvars[1], nbuf);
10031 for ( ; *flags != NUL; ++flags)
10032 {
10033 switch (*flags)
10034 {
10035 case 'n': remap = FALSE; break;
10036 case 'm': remap = TRUE; break;
10037 case 't': typed = TRUE; break;
10038 }
10039 }
10040 }
10041
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010042 /* Need to escape K_SPECIAL and CSI before putting the string in the
10043 * typeahead buffer. */
10044 keys_esc = vim_strsave_escape_csi(keys);
10045 if (keys_esc != NULL)
10046 {
10047 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010048 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010049 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010050 if (vgetc_busy)
10051 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010052 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010053 }
10054}
10055
10056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 * "filereadable()" function
10058 */
10059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010061 typval_T *argvars;
10062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010064 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 char_u *p;
10066 int n;
10067
Bram Moolenaarc236c162008-07-13 17:41:49 +000010068#ifndef O_NONBLOCK
10069# define O_NONBLOCK 0
10070#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010072 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10073 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 {
10075 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010076 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077 }
10078 else
10079 n = FALSE;
10080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082}
10083
10084/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010085 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 * rights to write into.
10087 */
10088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010090 typval_T *argvars;
10091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010093 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094}
10095
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010096static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010097
10098 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010099findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010100 typval_T *argvars;
10101 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010102 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010103{
10104#ifdef FEAT_SEARCHPATH
10105 char_u *fname;
10106 char_u *fresult = NULL;
10107 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10108 char_u *p;
10109 char_u pathbuf[NUMBUFLEN];
10110 int count = 1;
10111 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010112 int error = FALSE;
10113#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010114
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010115 rettv->vval.v_string = NULL;
10116 rettv->v_type = VAR_STRING;
10117
10118#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010119 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010120
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010121 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010122 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010123 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10124 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010125 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010126 else
10127 {
10128 if (*p != NUL)
10129 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010130
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010131 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010132 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010134 }
10135
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010136 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10137 error = TRUE;
10138
10139 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141 do
10142 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010143 if (rettv->v_type == VAR_STRING)
10144 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 fresult = find_file_in_path_option(first ? fname : NULL,
10146 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010147 0, first, path,
10148 find_what,
10149 curbuf->b_ffname,
10150 find_what == FINDFILE_DIR
10151 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010152 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010153
10154 if (fresult != NULL && rettv->v_type == VAR_LIST)
10155 list_append_string(rettv->vval.v_list, fresult, -1);
10156
10157 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010158 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010159
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010160 if (rettv->v_type == VAR_STRING)
10161 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010162#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010163}
10164
Bram Moolenaar33570922005-01-25 22:26:29 +000010165static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10166static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010167
10168/*
10169 * Implementation of map() and filter().
10170 */
10171 static void
10172filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010173 typval_T *argvars;
10174 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010175 int map;
10176{
10177 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010178 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010179 listitem_T *li, *nli;
10180 list_T *l = NULL;
10181 dictitem_T *di;
10182 hashtab_T *ht;
10183 hashitem_T *hi;
10184 dict_T *d = NULL;
10185 typval_T save_val;
10186 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010187 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010188 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010189 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010190 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010191 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010192
Bram Moolenaare9a41262005-01-15 22:18:47 +000010193 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010194 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010195 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010196 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010197 return;
10198 }
10199 else if (argvars[0].v_type == VAR_DICT)
10200 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010201 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010202 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010203 return;
10204 }
10205 else
10206 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010207 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010208 return;
10209 }
10210
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010211 expr = get_tv_string_buf_chk(&argvars[1], buf);
10212 /* On type errors, the preceding call has already displayed an error
10213 * message. Avoid a misleading error message for an empty string that
10214 * was not passed as argument. */
10215 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010217 prepare_vimvar(VV_VAL, &save_val);
10218 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010219
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010220 /* We reset "did_emsg" to be able to detect whether an error
10221 * occurred during evaluation of the expression. */
10222 save_did_emsg = did_emsg;
10223 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010224
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010225 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010226 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 vimvars[VV_KEY].vv_type = VAR_STRING;
10229
10230 ht = &d->dv_hashtab;
10231 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010232 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010233 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010234 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 if (!HASHITEM_EMPTY(hi))
10236 {
10237 --todo;
10238 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010239 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010240 break;
10241 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010242 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010243 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010244 break;
10245 if (!map && rem)
10246 dictitem_remove(d, di);
10247 clear_tv(&vimvars[VV_KEY].vv_tv);
10248 }
10249 }
10250 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010251 }
10252 else
10253 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010254 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010256 for (li = l->lv_first; li != NULL; li = nli)
10257 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010258 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010259 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010260 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010261 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010262 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010263 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010264 break;
10265 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010266 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010267 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010268 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010269 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010270
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010271 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010272 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010273
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010274 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010275 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010276
10277 copy_tv(&argvars[0], rettv);
10278}
10279
10280 static int
10281filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010282 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 char_u *expr;
10284 int map;
10285 int *remp;
10286{
Bram Moolenaar33570922005-01-25 22:26:29 +000010287 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010288 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010289 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290
Bram Moolenaar33570922005-01-25 22:26:29 +000010291 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 s = expr;
10293 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010294 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295 if (*s != NUL) /* check for trailing chars after expr */
10296 {
10297 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010298 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010299 }
10300 if (map)
10301 {
10302 /* map(): replace the list item value */
10303 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010304 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010305 *tv = rettv;
10306 }
10307 else
10308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010309 int error = FALSE;
10310
Bram Moolenaare9a41262005-01-15 22:18:47 +000010311 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010312 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 /* On type error, nothing has been removed; return FAIL to stop the
10315 * loop. The error message was given by get_tv_number_chk(). */
10316 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010317 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010319 retval = OK;
10320theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010321 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010322 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010323}
10324
10325/*
10326 * "filter()" function
10327 */
10328 static void
10329f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010330 typval_T *argvars;
10331 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010332{
10333 filter_map(argvars, rettv, FALSE);
10334}
10335
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010336/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010337 * "finddir({fname}[, {path}[, {count}]])" function
10338 */
10339 static void
10340f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010341 typval_T *argvars;
10342 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010343{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010344 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010345}
10346
10347/*
10348 * "findfile({fname}[, {path}[, {count}]])" function
10349 */
10350 static void
10351f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010352 typval_T *argvars;
10353 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010354{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010355 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010356}
10357
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010358#ifdef FEAT_FLOAT
10359/*
10360 * "float2nr({float})" function
10361 */
10362 static void
10363f_float2nr(argvars, rettv)
10364 typval_T *argvars;
10365 typval_T *rettv;
10366{
10367 float_T f;
10368
10369 if (get_float_arg(argvars, &f) == OK)
10370 {
10371 if (f < -0x7fffffff)
10372 rettv->vval.v_number = -0x7fffffff;
10373 else if (f > 0x7fffffff)
10374 rettv->vval.v_number = 0x7fffffff;
10375 else
10376 rettv->vval.v_number = (varnumber_T)f;
10377 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010378}
10379
10380/*
10381 * "floor({float})" function
10382 */
10383 static void
10384f_floor(argvars, rettv)
10385 typval_T *argvars;
10386 typval_T *rettv;
10387{
10388 float_T f;
10389
10390 rettv->v_type = VAR_FLOAT;
10391 if (get_float_arg(argvars, &f) == OK)
10392 rettv->vval.v_float = floor(f);
10393 else
10394 rettv->vval.v_float = 0.0;
10395}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010396
10397/*
10398 * "fmod()" function
10399 */
10400 static void
10401f_fmod(argvars, rettv)
10402 typval_T *argvars;
10403 typval_T *rettv;
10404{
10405 float_T fx, fy;
10406
10407 rettv->v_type = VAR_FLOAT;
10408 if (get_float_arg(argvars, &fx) == OK
10409 && get_float_arg(&argvars[1], &fy) == OK)
10410 rettv->vval.v_float = fmod(fx, fy);
10411 else
10412 rettv->vval.v_float = 0.0;
10413}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010414#endif
10415
Bram Moolenaar0d660222005-01-07 21:51:51 +000010416/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010417 * "fnameescape({string})" function
10418 */
10419 static void
10420f_fnameescape(argvars, rettv)
10421 typval_T *argvars;
10422 typval_T *rettv;
10423{
10424 rettv->vval.v_string = vim_strsave_fnameescape(
10425 get_tv_string(&argvars[0]), FALSE);
10426 rettv->v_type = VAR_STRING;
10427}
10428
10429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 * "fnamemodify({fname}, {mods})" function
10431 */
10432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010433f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010434 typval_T *argvars;
10435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436{
10437 char_u *fname;
10438 char_u *mods;
10439 int usedlen = 0;
10440 int len;
10441 char_u *fbuf = NULL;
10442 char_u buf[NUMBUFLEN];
10443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010444 fname = get_tv_string_chk(&argvars[0]);
10445 mods = get_tv_string_buf_chk(&argvars[1], buf);
10446 if (fname == NULL || mods == NULL)
10447 fname = NULL;
10448 else
10449 {
10450 len = (int)STRLEN(fname);
10451 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 vim_free(fbuf);
10460}
10461
Bram Moolenaar33570922005-01-25 22:26:29 +000010462static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463
10464/*
10465 * "foldclosed()" function
10466 */
10467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010469 typval_T *argvars;
10470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471 int end;
10472{
10473#ifdef FEAT_FOLDING
10474 linenr_T lnum;
10475 linenr_T first, last;
10476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10479 {
10480 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10481 {
10482 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 return;
10487 }
10488 }
10489#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010490 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010491}
10492
10493/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010494 * "foldclosed()" function
10495 */
10496 static void
10497f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010498 typval_T *argvars;
10499 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010500{
10501 foldclosed_both(argvars, rettv, FALSE);
10502}
10503
10504/*
10505 * "foldclosedend()" function
10506 */
10507 static void
10508f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010509 typval_T *argvars;
10510 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010511{
10512 foldclosed_both(argvars, rettv, TRUE);
10513}
10514
10515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010516 * "foldlevel()" function
10517 */
10518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010519f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010520 typval_T *argvars;
10521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522{
10523#ifdef FEAT_FOLDING
10524 linenr_T lnum;
10525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010526 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530}
10531
10532/*
10533 * "foldtext()" function
10534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010537 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
10540#ifdef FEAT_FOLDING
10541 linenr_T lnum;
10542 char_u *s;
10543 char_u *r;
10544 int len;
10545 char *txt;
10546#endif
10547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 rettv->v_type = VAR_STRING;
10549 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010551 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10552 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10553 <= curbuf->b_ml.ml_line_count
10554 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555 {
10556 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010557 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10558 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 {
10560 if (!linewhite(lnum))
10561 break;
10562 ++lnum;
10563 }
10564
10565 /* Find interesting text in this line. */
10566 s = skipwhite(ml_get(lnum));
10567 /* skip C comment-start */
10568 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010571 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010572 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010573 {
10574 s = skipwhite(ml_get(lnum + 1));
10575 if (*s == '*')
10576 s = skipwhite(s + 1);
10577 }
10578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 txt = _("+-%s%3ld lines: ");
10580 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010581 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 + 20 /* for %3ld */
10583 + STRLEN(s))); /* concatenated */
10584 if (r != NULL)
10585 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10587 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10588 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 len = (int)STRLEN(r);
10590 STRCAT(r, s);
10591 /* remove 'foldmarker' and 'commentstring' */
10592 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010593 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 }
10595 }
10596#endif
10597}
10598
10599/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010600 * "foldtextresult(lnum)" function
10601 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010602 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010603f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010604 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010605 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010606{
10607#ifdef FEAT_FOLDING
10608 linenr_T lnum;
10609 char_u *text;
10610 char_u buf[51];
10611 foldinfo_T foldinfo;
10612 int fold_count;
10613#endif
10614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010615 rettv->v_type = VAR_STRING;
10616 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010617#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619 /* treat illegal types and illegal string values for {lnum} the same */
10620 if (lnum < 0)
10621 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010622 fold_count = foldedCount(curwin, lnum, &foldinfo);
10623 if (fold_count > 0)
10624 {
10625 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10626 &foldinfo, buf);
10627 if (text == buf)
10628 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010630 }
10631#endif
10632}
10633
10634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 * "foreground()" function
10636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010639 typval_T *argvars UNUSED;
10640 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642#ifdef FEAT_GUI
10643 if (gui.in_use)
10644 gui_mch_set_foreground();
10645#else
10646# ifdef WIN32
10647 win32_set_foreground();
10648# endif
10649#endif
10650}
10651
10652/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010653 * "function()" function
10654 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010657 typval_T *argvars;
10658 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010659{
10660 char_u *s;
10661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010662 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010663 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010664 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010665 /* Don't check an autoload name for existence here. */
10666 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010667 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010668 else
10669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670 rettv->vval.v_string = vim_strsave(s);
10671 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010672 }
10673}
10674
10675/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010676 * "garbagecollect()" function
10677 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010678 static void
10679f_garbagecollect(argvars, rettv)
10680 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010681 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010682{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010683 /* This is postponed until we are back at the toplevel, because we may be
10684 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10685 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010686
10687 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10688 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010689}
10690
10691/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010692 * "get()" function
10693 */
10694 static void
10695f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010696 typval_T *argvars;
10697 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010698{
Bram Moolenaar33570922005-01-25 22:26:29 +000010699 listitem_T *li;
10700 list_T *l;
10701 dictitem_T *di;
10702 dict_T *d;
10703 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010704
Bram Moolenaare9a41262005-01-15 22:18:47 +000010705 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010706 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010707 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010708 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010709 int error = FALSE;
10710
10711 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10712 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010713 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010714 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010715 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010716 else if (argvars[0].v_type == VAR_DICT)
10717 {
10718 if ((d = argvars[0].vval.v_dict) != NULL)
10719 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010720 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010721 if (di != NULL)
10722 tv = &di->di_tv;
10723 }
10724 }
10725 else
10726 EMSG2(_(e_listdictarg), "get()");
10727
10728 if (tv == NULL)
10729 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010731 copy_tv(&argvars[2], rettv);
10732 }
10733 else
10734 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010735}
10736
Bram Moolenaar342337a2005-07-21 21:11:17 +000010737static 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 +000010738
10739/*
10740 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010741 * Return a range (from start to end) of lines in rettv from the specified
10742 * buffer.
10743 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010744 */
10745 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010746get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010747 buf_T *buf;
10748 linenr_T start;
10749 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010750 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010751 typval_T *rettv;
10752{
10753 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010754
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010755 if (retlist && rettv_list_alloc(rettv) == FAIL)
10756 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010757
10758 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10759 return;
10760
10761 if (!retlist)
10762 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010763 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10764 p = ml_get_buf(buf, start, FALSE);
10765 else
10766 p = (char_u *)"";
10767
10768 rettv->v_type = VAR_STRING;
10769 rettv->vval.v_string = vim_strsave(p);
10770 }
10771 else
10772 {
10773 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010774 return;
10775
10776 if (start < 1)
10777 start = 1;
10778 if (end > buf->b_ml.ml_line_count)
10779 end = buf->b_ml.ml_line_count;
10780 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010781 if (list_append_string(rettv->vval.v_list,
10782 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010783 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010784 }
10785}
10786
10787/*
10788 * "getbufline()" function
10789 */
10790 static void
10791f_getbufline(argvars, rettv)
10792 typval_T *argvars;
10793 typval_T *rettv;
10794{
10795 linenr_T lnum;
10796 linenr_T end;
10797 buf_T *buf;
10798
10799 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10800 ++emsg_off;
10801 buf = get_buf_tv(&argvars[0]);
10802 --emsg_off;
10803
Bram Moolenaar661b1822005-07-28 22:36:45 +000010804 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010805 if (argvars[2].v_type == VAR_UNKNOWN)
10806 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010807 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010808 end = get_tv_lnum_buf(&argvars[2], buf);
10809
Bram Moolenaar342337a2005-07-21 21:11:17 +000010810 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010811}
10812
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813/*
10814 * "getbufvar()" function
10815 */
10816 static void
10817f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010818 typval_T *argvars;
10819 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010820{
10821 buf_T *buf;
10822 buf_T *save_curbuf;
10823 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010824 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010825
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010826 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10827 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010828 ++emsg_off;
10829 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010830
10831 rettv->v_type = VAR_STRING;
10832 rettv->vval.v_string = NULL;
10833
10834 if (buf != NULL && varname != NULL)
10835 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010836 /* set curbuf to be our buf, temporarily */
10837 save_curbuf = curbuf;
10838 curbuf = buf;
10839
Bram Moolenaar0d660222005-01-07 21:51:51 +000010840 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010841 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010842 else
10843 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010844 if (*varname == NUL)
10845 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10846 * scope prefix before the NUL byte is required by
10847 * find_var_in_ht(). */
10848 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010849 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010850 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010851 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010852 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010853 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010854
10855 /* restore previous notion of curbuf */
10856 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010857 }
10858
10859 --emsg_off;
10860}
10861
10862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010863 * "getchar()" function
10864 */
10865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010866f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010867 typval_T *argvars;
10868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869{
10870 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010873 /* Position the cursor. Needed after a message that ends in a space. */
10874 windgoto(msg_row, msg_col);
10875
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 ++no_mapping;
10877 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010878 for (;;)
10879 {
10880 if (argvars[0].v_type == VAR_UNKNOWN)
10881 /* getchar(): blocking wait. */
10882 n = safe_vgetc();
10883 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10884 /* getchar(1): only check if char avail */
10885 n = vpeekc();
10886 else if (error || vpeekc() == NUL)
10887 /* illegal argument or getchar(0) and no char avail: return zero */
10888 n = 0;
10889 else
10890 /* getchar(0) and char avail: return char */
10891 n = safe_vgetc();
10892 if (n == K_IGNORE)
10893 continue;
10894 break;
10895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 --no_mapping;
10897 --allow_keys;
10898
Bram Moolenaar219b8702006-11-01 14:32:36 +000010899 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10900 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10901 vimvars[VV_MOUSE_COL].vv_nr = 0;
10902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010903 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 if (IS_SPECIAL(n) || mod_mask != 0)
10905 {
10906 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10907 int i = 0;
10908
10909 /* Turn a special key into three bytes, plus modifier. */
10910 if (mod_mask != 0)
10911 {
10912 temp[i++] = K_SPECIAL;
10913 temp[i++] = KS_MODIFIER;
10914 temp[i++] = mod_mask;
10915 }
10916 if (IS_SPECIAL(n))
10917 {
10918 temp[i++] = K_SPECIAL;
10919 temp[i++] = K_SECOND(n);
10920 temp[i++] = K_THIRD(n);
10921 }
10922#ifdef FEAT_MBYTE
10923 else if (has_mbyte)
10924 i += (*mb_char2bytes)(n, temp + i);
10925#endif
10926 else
10927 temp[i++] = n;
10928 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 rettv->v_type = VAR_STRING;
10930 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010931
10932#ifdef FEAT_MOUSE
10933 if (n == K_LEFTMOUSE
10934 || n == K_LEFTMOUSE_NM
10935 || n == K_LEFTDRAG
10936 || n == K_LEFTRELEASE
10937 || n == K_LEFTRELEASE_NM
10938 || n == K_MIDDLEMOUSE
10939 || n == K_MIDDLEDRAG
10940 || n == K_MIDDLERELEASE
10941 || n == K_RIGHTMOUSE
10942 || n == K_RIGHTDRAG
10943 || n == K_RIGHTRELEASE
10944 || n == K_X1MOUSE
10945 || n == K_X1DRAG
10946 || n == K_X1RELEASE
10947 || n == K_X2MOUSE
10948 || n == K_X2DRAG
10949 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010950 || n == K_MOUSELEFT
10951 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010952 || n == K_MOUSEDOWN
10953 || n == K_MOUSEUP)
10954 {
10955 int row = mouse_row;
10956 int col = mouse_col;
10957 win_T *win;
10958 linenr_T lnum;
10959# ifdef FEAT_WINDOWS
10960 win_T *wp;
10961# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010962 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010963
10964 if (row >= 0 && col >= 0)
10965 {
10966 /* Find the window at the mouse coordinates and compute the
10967 * text position. */
10968 win = mouse_find_win(&row, &col);
10969 (void)mouse_comp_pos(win, &row, &col, &lnum);
10970# ifdef FEAT_WINDOWS
10971 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010972 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010973# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010974 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010975 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10976 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10977 }
10978 }
10979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980 }
10981}
10982
10983/*
10984 * "getcharmod()" function
10985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010988 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010991 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992}
10993
10994/*
10995 * "getcmdline()" function
10996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010999 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002 rettv->v_type = VAR_STRING;
11003 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004}
11005
11006/*
11007 * "getcmdpos()" function
11008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011011 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011012 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011014 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015}
11016
11017/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011018 * "getcmdtype()" function
11019 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011020 static void
11021f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011022 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011023 typval_T *rettv;
11024{
11025 rettv->v_type = VAR_STRING;
11026 rettv->vval.v_string = alloc(2);
11027 if (rettv->vval.v_string != NULL)
11028 {
11029 rettv->vval.v_string[0] = get_cmdline_type();
11030 rettv->vval.v_string[1] = NUL;
11031 }
11032}
11033
11034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035 * "getcwd()" function
11036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011038f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011039 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041{
11042 char_u cwd[MAXPATHL];
11043
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 else
11048 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011049 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011051 if (rettv->vval.v_string != NULL)
11052 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053#endif
11054 }
11055}
11056
11057/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011058 * "getfontname()" function
11059 */
11060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011062 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011063 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011064{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->v_type = VAR_STRING;
11066 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011067#ifdef FEAT_GUI
11068 if (gui.in_use)
11069 {
11070 GuiFont font;
11071 char_u *name = NULL;
11072
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011073 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011074 {
11075 /* Get the "Normal" font. Either the name saved by
11076 * hl_set_font_name() or from the font ID. */
11077 font = gui.norm_font;
11078 name = hl_get_font_name();
11079 }
11080 else
11081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011082 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011083 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11084 return;
11085 font = gui_mch_get_font(name, FALSE);
11086 if (font == NOFONT)
11087 return; /* Invalid font name, return empty string. */
11088 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011089 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011090 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011091 gui_mch_free_font(font);
11092 }
11093#endif
11094}
11095
11096/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011097 * "getfperm({fname})" function
11098 */
11099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011101 typval_T *argvars;
11102 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011103{
11104 char_u *fname;
11105 struct stat st;
11106 char_u *perm = NULL;
11107 char_u flags[] = "rwx";
11108 int i;
11109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011110 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011113 if (mch_stat((char *)fname, &st) >= 0)
11114 {
11115 perm = vim_strsave((char_u *)"---------");
11116 if (perm != NULL)
11117 {
11118 for (i = 0; i < 9; i++)
11119 {
11120 if (st.st_mode & (1 << (8 - i)))
11121 perm[i] = flags[i % 3];
11122 }
11123 }
11124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011126}
11127
11128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 * "getfsize({fname})" function
11130 */
11131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011132f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011133 typval_T *argvars;
11134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011135{
11136 char_u *fname;
11137 struct stat st;
11138
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142
11143 if (mch_stat((char *)fname, &st) >= 0)
11144 {
11145 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011148 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011149 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011150
11151 /* non-perfect check for overflow */
11152 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11153 rettv->vval.v_number = -2;
11154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011155 }
11156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158}
11159
11160/*
11161 * "getftime({fname})" function
11162 */
11163 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011164f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011165 typval_T *argvars;
11166 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011167{
11168 char_u *fname;
11169 struct stat st;
11170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172
11173 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011174 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177}
11178
11179/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011180 * "getftype({fname})" function
11181 */
11182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011184 typval_T *argvars;
11185 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011186{
11187 char_u *fname;
11188 struct stat st;
11189 char_u *type = NULL;
11190 char *t;
11191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011195 if (mch_lstat((char *)fname, &st) >= 0)
11196 {
11197#ifdef S_ISREG
11198 if (S_ISREG(st.st_mode))
11199 t = "file";
11200 else if (S_ISDIR(st.st_mode))
11201 t = "dir";
11202# ifdef S_ISLNK
11203 else if (S_ISLNK(st.st_mode))
11204 t = "link";
11205# endif
11206# ifdef S_ISBLK
11207 else if (S_ISBLK(st.st_mode))
11208 t = "bdev";
11209# endif
11210# ifdef S_ISCHR
11211 else if (S_ISCHR(st.st_mode))
11212 t = "cdev";
11213# endif
11214# ifdef S_ISFIFO
11215 else if (S_ISFIFO(st.st_mode))
11216 t = "fifo";
11217# endif
11218# ifdef S_ISSOCK
11219 else if (S_ISSOCK(st.st_mode))
11220 t = "fifo";
11221# endif
11222 else
11223 t = "other";
11224#else
11225# ifdef S_IFMT
11226 switch (st.st_mode & S_IFMT)
11227 {
11228 case S_IFREG: t = "file"; break;
11229 case S_IFDIR: t = "dir"; break;
11230# ifdef S_IFLNK
11231 case S_IFLNK: t = "link"; break;
11232# endif
11233# ifdef S_IFBLK
11234 case S_IFBLK: t = "bdev"; break;
11235# endif
11236# ifdef S_IFCHR
11237 case S_IFCHR: t = "cdev"; break;
11238# endif
11239# ifdef S_IFIFO
11240 case S_IFIFO: t = "fifo"; break;
11241# endif
11242# ifdef S_IFSOCK
11243 case S_IFSOCK: t = "socket"; break;
11244# endif
11245 default: t = "other";
11246 }
11247# else
11248 if (mch_isdir(fname))
11249 t = "dir";
11250 else
11251 t = "file";
11252# endif
11253#endif
11254 type = vim_strsave((char_u *)t);
11255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011257}
11258
11259/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011260 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011261 */
11262 static void
11263f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011264 typval_T *argvars;
11265 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011266{
11267 linenr_T lnum;
11268 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011269 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011270
11271 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011272 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011273 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011274 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011275 retlist = FALSE;
11276 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011277 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011278 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011279 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011280 retlist = TRUE;
11281 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011282
Bram Moolenaar342337a2005-07-21 21:11:17 +000011283 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011284}
11285
11286/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011287 * "getmatches()" function
11288 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011289 static void
11290f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011291 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011292 typval_T *rettv;
11293{
11294#ifdef FEAT_SEARCH_EXTRA
11295 dict_T *dict;
11296 matchitem_T *cur = curwin->w_match_head;
11297
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011298 if (rettv_list_alloc(rettv) == OK)
11299 {
11300 while (cur != NULL)
11301 {
11302 dict = dict_alloc();
11303 if (dict == NULL)
11304 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011305 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11306 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11307 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11308 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11309 list_append_dict(rettv->vval.v_list, dict);
11310 cur = cur->next;
11311 }
11312 }
11313#endif
11314}
11315
11316/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011317 * "getpid()" function
11318 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011319 static void
11320f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011321 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011322 typval_T *rettv;
11323{
11324 rettv->vval.v_number = mch_get_pid();
11325}
11326
11327/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011328 * "getpos(string)" function
11329 */
11330 static void
11331f_getpos(argvars, rettv)
11332 typval_T *argvars;
11333 typval_T *rettv;
11334{
11335 pos_T *fp;
11336 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011337 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011338
11339 if (rettv_list_alloc(rettv) == OK)
11340 {
11341 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011342 fp = var2fpos(&argvars[0], TRUE, &fnum);
11343 if (fnum != -1)
11344 list_append_number(l, (varnumber_T)fnum);
11345 else
11346 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011347 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11348 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011349 list_append_number(l, (fp != NULL)
11350 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011351 : (varnumber_T)0);
11352 list_append_number(l,
11353#ifdef FEAT_VIRTUALEDIT
11354 (fp != NULL) ? (varnumber_T)fp->coladd :
11355#endif
11356 (varnumber_T)0);
11357 }
11358 else
11359 rettv->vval.v_number = FALSE;
11360}
11361
11362/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011363 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011364 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011365 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011366f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011367 typval_T *argvars UNUSED;
11368 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011369{
11370#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011371 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011372#endif
11373
Bram Moolenaar2641f772005-03-25 21:58:17 +000011374#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011375 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011376 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011377 wp = NULL;
11378 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11379 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011380 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011381 if (wp == NULL)
11382 return;
11383 }
11384
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011385 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011386 }
11387#endif
11388}
11389
11390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 * "getreg()" function
11392 */
11393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011394f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011395 typval_T *argvars;
11396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397{
11398 char_u *strregname;
11399 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011400 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011401 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011403 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011405 strregname = get_tv_string_chk(&argvars[0]);
11406 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011407 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011408 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011411 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 regname = (strregname == NULL ? '"' : *strregname);
11413 if (regname == 0)
11414 regname = '"';
11415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011416 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 rettv->vval.v_string = error ? NULL :
11418 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419}
11420
11421/*
11422 * "getregtype()" function
11423 */
11424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011426 typval_T *argvars;
11427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428{
11429 char_u *strregname;
11430 int regname;
11431 char_u buf[NUMBUFLEN + 2];
11432 long reglen = 0;
11433
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011434 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 {
11436 strregname = get_tv_string_chk(&argvars[0]);
11437 if (strregname == NULL) /* type error; errmsg already given */
11438 {
11439 rettv->v_type = VAR_STRING;
11440 rettv->vval.v_string = NULL;
11441 return;
11442 }
11443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 else
11445 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011446 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
11448 regname = (strregname == NULL ? '"' : *strregname);
11449 if (regname == 0)
11450 regname = '"';
11451
11452 buf[0] = NUL;
11453 buf[1] = NUL;
11454 switch (get_reg_type(regname, &reglen))
11455 {
11456 case MLINE: buf[0] = 'V'; break;
11457 case MCHAR: buf[0] = 'v'; break;
11458#ifdef FEAT_VISUAL
11459 case MBLOCK:
11460 buf[0] = Ctrl_V;
11461 sprintf((char *)buf + 1, "%ld", reglen + 1);
11462 break;
11463#endif
11464 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011465 rettv->v_type = VAR_STRING;
11466 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467}
11468
11469/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011470 * "gettabvar()" function
11471 */
11472 static void
11473f_gettabvar(argvars, rettv)
11474 typval_T *argvars;
11475 typval_T *rettv;
11476{
11477 tabpage_T *tp;
11478 dictitem_T *v;
11479 char_u *varname;
11480
11481 rettv->v_type = VAR_STRING;
11482 rettv->vval.v_string = NULL;
11483
11484 varname = get_tv_string_chk(&argvars[1]);
11485 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11486 if (tp != NULL && varname != NULL)
11487 {
11488 /* look up the variable */
11489 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11490 if (v != NULL)
11491 copy_tv(&v->di_tv, rettv);
11492 }
11493}
11494
11495/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011496 * "gettabwinvar()" function
11497 */
11498 static void
11499f_gettabwinvar(argvars, rettv)
11500 typval_T *argvars;
11501 typval_T *rettv;
11502{
11503 getwinvar(argvars, rettv, 1);
11504}
11505
11506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011507 * "getwinposx()" function
11508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011511 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011512 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515#ifdef FEAT_GUI
11516 if (gui.in_use)
11517 {
11518 int x, y;
11519
11520 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011521 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011522 }
11523#endif
11524}
11525
11526/*
11527 * "getwinposy()" function
11528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011531 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011532 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535#ifdef FEAT_GUI
11536 if (gui.in_use)
11537 {
11538 int x, y;
11539
11540 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 }
11543#endif
11544}
11545
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011546/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011547 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011548 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011549 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011550find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011551 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011552 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011553{
11554#ifdef FEAT_WINDOWS
11555 win_T *wp;
11556#endif
11557 int nr;
11558
11559 nr = get_tv_number_chk(vp, NULL);
11560
11561#ifdef FEAT_WINDOWS
11562 if (nr < 0)
11563 return NULL;
11564 if (nr == 0)
11565 return curwin;
11566
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011567 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11568 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011569 if (--nr <= 0)
11570 break;
11571 return wp;
11572#else
11573 if (nr == 0 || nr == 1)
11574 return curwin;
11575 return NULL;
11576#endif
11577}
11578
Bram Moolenaar071d4272004-06-13 20:20:40 +000011579/*
11580 * "getwinvar()" function
11581 */
11582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011583f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011584 typval_T *argvars;
11585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011587 getwinvar(argvars, rettv, 0);
11588}
11589
11590/*
11591 * getwinvar() and gettabwinvar()
11592 */
11593 static void
11594getwinvar(argvars, rettv, off)
11595 typval_T *argvars;
11596 typval_T *rettv;
11597 int off; /* 1 for gettabwinvar() */
11598{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011599 win_T *win, *oldcurwin;
11600 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011601 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011602 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011603
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011604#ifdef FEAT_WINDOWS
11605 if (off == 1)
11606 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11607 else
11608 tp = curtab;
11609#endif
11610 win = find_win_by_nr(&argvars[off], tp);
11611 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614 rettv->v_type = VAR_STRING;
11615 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616
11617 if (win != NULL && varname != NULL)
11618 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011619 /* Set curwin to be our win, temporarily. Also set curbuf, so
11620 * that we can get buffer-local options. */
11621 oldcurwin = curwin;
11622 curwin = win;
11623 curbuf = win->w_buffer;
11624
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011626 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 else
11628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011629 if (*varname == NUL)
11630 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11631 * scope prefix before the NUL byte is required by
11632 * find_var_in_ht(). */
11633 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011635 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011637 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011639
11640 /* restore previous notion of curwin */
11641 curwin = oldcurwin;
11642 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 }
11644
11645 --emsg_off;
11646}
11647
11648/*
11649 * "glob()" function
11650 */
11651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011653 typval_T *argvars;
11654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011656 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011658 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011660 /* When the optional second argument is non-zero, don't remove matches
11661 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11662 if (argvars[1].v_type != VAR_UNKNOWN
11663 && get_tv_number_chk(&argvars[1], &error))
11664 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011665 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011666 if (!error)
11667 {
11668 ExpandInit(&xpc);
11669 xpc.xp_context = EXPAND_FILES;
11670 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11671 NULL, flags, WILD_ALL);
11672 }
11673 else
11674 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675}
11676
11677/*
11678 * "globpath()" function
11679 */
11680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011682 typval_T *argvars;
11683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011685 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011687 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011688 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011690 /* When the optional second argument is non-zero, don't remove matches
11691 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11692 if (argvars[2].v_type != VAR_UNKNOWN
11693 && get_tv_number_chk(&argvars[2], &error))
11694 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011696 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011697 rettv->vval.v_string = NULL;
11698 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011699 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11700 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011701}
11702
11703/*
11704 * "has()" function
11705 */
11706 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011707f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011708 typval_T *argvars;
11709 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710{
11711 int i;
11712 char_u *name;
11713 int n = FALSE;
11714 static char *(has_list[]) =
11715 {
11716#ifdef AMIGA
11717 "amiga",
11718# ifdef FEAT_ARP
11719 "arp",
11720# endif
11721#endif
11722#ifdef __BEOS__
11723 "beos",
11724#endif
11725#ifdef MSDOS
11726# ifdef DJGPP
11727 "dos32",
11728# else
11729 "dos16",
11730# endif
11731#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011732#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 "mac",
11734#endif
11735#if defined(MACOS_X_UNIX)
11736 "macunix",
11737#endif
11738#ifdef OS2
11739 "os2",
11740#endif
11741#ifdef __QNX__
11742 "qnx",
11743#endif
11744#ifdef RISCOS
11745 "riscos",
11746#endif
11747#ifdef UNIX
11748 "unix",
11749#endif
11750#ifdef VMS
11751 "vms",
11752#endif
11753#ifdef WIN16
11754 "win16",
11755#endif
11756#ifdef WIN32
11757 "win32",
11758#endif
11759#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11760 "win32unix",
11761#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011762#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763 "win64",
11764#endif
11765#ifdef EBCDIC
11766 "ebcdic",
11767#endif
11768#ifndef CASE_INSENSITIVE_FILENAME
11769 "fname_case",
11770#endif
11771#ifdef FEAT_ARABIC
11772 "arabic",
11773#endif
11774#ifdef FEAT_AUTOCMD
11775 "autocmd",
11776#endif
11777#ifdef FEAT_BEVAL
11778 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011779# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11780 "balloon_multiline",
11781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782#endif
11783#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11784 "builtin_terms",
11785# ifdef ALL_BUILTIN_TCAPS
11786 "all_builtin_terms",
11787# endif
11788#endif
11789#ifdef FEAT_BYTEOFF
11790 "byte_offset",
11791#endif
11792#ifdef FEAT_CINDENT
11793 "cindent",
11794#endif
11795#ifdef FEAT_CLIENTSERVER
11796 "clientserver",
11797#endif
11798#ifdef FEAT_CLIPBOARD
11799 "clipboard",
11800#endif
11801#ifdef FEAT_CMDL_COMPL
11802 "cmdline_compl",
11803#endif
11804#ifdef FEAT_CMDHIST
11805 "cmdline_hist",
11806#endif
11807#ifdef FEAT_COMMENTS
11808 "comments",
11809#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011810#ifdef FEAT_CONCEAL
11811 "conceal",
11812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813#ifdef FEAT_CRYPT
11814 "cryptv",
11815#endif
11816#ifdef FEAT_CSCOPE
11817 "cscope",
11818#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011819#ifdef FEAT_CURSORBIND
11820 "cursorbind",
11821#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011822#ifdef CURSOR_SHAPE
11823 "cursorshape",
11824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825#ifdef DEBUG
11826 "debug",
11827#endif
11828#ifdef FEAT_CON_DIALOG
11829 "dialog_con",
11830#endif
11831#ifdef FEAT_GUI_DIALOG
11832 "dialog_gui",
11833#endif
11834#ifdef FEAT_DIFF
11835 "diff",
11836#endif
11837#ifdef FEAT_DIGRAPHS
11838 "digraphs",
11839#endif
11840#ifdef FEAT_DND
11841 "dnd",
11842#endif
11843#ifdef FEAT_EMACS_TAGS
11844 "emacs_tags",
11845#endif
11846 "eval", /* always present, of course! */
11847#ifdef FEAT_EX_EXTRA
11848 "ex_extra",
11849#endif
11850#ifdef FEAT_SEARCH_EXTRA
11851 "extra_search",
11852#endif
11853#ifdef FEAT_FKMAP
11854 "farsi",
11855#endif
11856#ifdef FEAT_SEARCHPATH
11857 "file_in_path",
11858#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011859#if defined(UNIX) && !defined(USE_SYSTEM)
11860 "filterpipe",
11861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862#ifdef FEAT_FIND_ID
11863 "find_in_path",
11864#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011865#ifdef FEAT_FLOAT
11866 "float",
11867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868#ifdef FEAT_FOLDING
11869 "folding",
11870#endif
11871#ifdef FEAT_FOOTER
11872 "footer",
11873#endif
11874#if !defined(USE_SYSTEM) && defined(UNIX)
11875 "fork",
11876#endif
11877#ifdef FEAT_GETTEXT
11878 "gettext",
11879#endif
11880#ifdef FEAT_GUI
11881 "gui",
11882#endif
11883#ifdef FEAT_GUI_ATHENA
11884# ifdef FEAT_GUI_NEXTAW
11885 "gui_neXtaw",
11886# else
11887 "gui_athena",
11888# endif
11889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011890#ifdef FEAT_GUI_GTK
11891 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011894#ifdef FEAT_GUI_GNOME
11895 "gui_gnome",
11896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011897#ifdef FEAT_GUI_MAC
11898 "gui_mac",
11899#endif
11900#ifdef FEAT_GUI_MOTIF
11901 "gui_motif",
11902#endif
11903#ifdef FEAT_GUI_PHOTON
11904 "gui_photon",
11905#endif
11906#ifdef FEAT_GUI_W16
11907 "gui_win16",
11908#endif
11909#ifdef FEAT_GUI_W32
11910 "gui_win32",
11911#endif
11912#ifdef FEAT_HANGULIN
11913 "hangul_input",
11914#endif
11915#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11916 "iconv",
11917#endif
11918#ifdef FEAT_INS_EXPAND
11919 "insert_expand",
11920#endif
11921#ifdef FEAT_JUMPLIST
11922 "jumplist",
11923#endif
11924#ifdef FEAT_KEYMAP
11925 "keymap",
11926#endif
11927#ifdef FEAT_LANGMAP
11928 "langmap",
11929#endif
11930#ifdef FEAT_LIBCALL
11931 "libcall",
11932#endif
11933#ifdef FEAT_LINEBREAK
11934 "linebreak",
11935#endif
11936#ifdef FEAT_LISP
11937 "lispindent",
11938#endif
11939#ifdef FEAT_LISTCMDS
11940 "listcmds",
11941#endif
11942#ifdef FEAT_LOCALMAP
11943 "localmap",
11944#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011945#ifdef FEAT_LUA
11946# ifndef DYNAMIC_LUA
11947 "lua",
11948# endif
11949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950#ifdef FEAT_MENU
11951 "menu",
11952#endif
11953#ifdef FEAT_SESSION
11954 "mksession",
11955#endif
11956#ifdef FEAT_MODIFY_FNAME
11957 "modify_fname",
11958#endif
11959#ifdef FEAT_MOUSE
11960 "mouse",
11961#endif
11962#ifdef FEAT_MOUSESHAPE
11963 "mouseshape",
11964#endif
11965#if defined(UNIX) || defined(VMS)
11966# ifdef FEAT_MOUSE_DEC
11967 "mouse_dec",
11968# endif
11969# ifdef FEAT_MOUSE_GPM
11970 "mouse_gpm",
11971# endif
11972# ifdef FEAT_MOUSE_JSB
11973 "mouse_jsbterm",
11974# endif
11975# ifdef FEAT_MOUSE_NET
11976 "mouse_netterm",
11977# endif
11978# ifdef FEAT_MOUSE_PTERM
11979 "mouse_pterm",
11980# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011981# ifdef FEAT_SYSMOUSE
11982 "mouse_sysmouse",
11983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984# ifdef FEAT_MOUSE_XTERM
11985 "mouse_xterm",
11986# endif
11987#endif
11988#ifdef FEAT_MBYTE
11989 "multi_byte",
11990#endif
11991#ifdef FEAT_MBYTE_IME
11992 "multi_byte_ime",
11993#endif
11994#ifdef FEAT_MULTI_LANG
11995 "multi_lang",
11996#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011997#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011998#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011999 "mzscheme",
12000#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002#ifdef FEAT_OLE
12003 "ole",
12004#endif
12005#ifdef FEAT_OSFILETYPE
12006 "osfiletype",
12007#endif
12008#ifdef FEAT_PATH_EXTRA
12009 "path_extra",
12010#endif
12011#ifdef FEAT_PERL
12012#ifndef DYNAMIC_PERL
12013 "perl",
12014#endif
12015#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012016#ifdef FEAT_PERSISTENT_UNDO
12017 "persistent_undo",
12018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019#ifdef FEAT_PYTHON
12020#ifndef DYNAMIC_PYTHON
12021 "python",
12022#endif
12023#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012024#ifdef FEAT_PYTHON3
12025#ifndef DYNAMIC_PYTHON3
12026 "python3",
12027#endif
12028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029#ifdef FEAT_POSTSCRIPT
12030 "postscript",
12031#endif
12032#ifdef FEAT_PRINTER
12033 "printer",
12034#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012035#ifdef FEAT_PROFILE
12036 "profile",
12037#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012038#ifdef FEAT_RELTIME
12039 "reltime",
12040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041#ifdef FEAT_QUICKFIX
12042 "quickfix",
12043#endif
12044#ifdef FEAT_RIGHTLEFT
12045 "rightleft",
12046#endif
12047#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12048 "ruby",
12049#endif
12050#ifdef FEAT_SCROLLBIND
12051 "scrollbind",
12052#endif
12053#ifdef FEAT_CMDL_INFO
12054 "showcmd",
12055 "cmdline_info",
12056#endif
12057#ifdef FEAT_SIGNS
12058 "signs",
12059#endif
12060#ifdef FEAT_SMARTINDENT
12061 "smartindent",
12062#endif
12063#ifdef FEAT_SNIFF
12064 "sniff",
12065#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012066#ifdef STARTUPTIME
12067 "startuptime",
12068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069#ifdef FEAT_STL_OPT
12070 "statusline",
12071#endif
12072#ifdef FEAT_SUN_WORKSHOP
12073 "sun_workshop",
12074#endif
12075#ifdef FEAT_NETBEANS_INTG
12076 "netbeans_intg",
12077#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012078#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012079 "spell",
12080#endif
12081#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012082 "syntax",
12083#endif
12084#if defined(USE_SYSTEM) || !defined(UNIX)
12085 "system",
12086#endif
12087#ifdef FEAT_TAG_BINS
12088 "tag_binary",
12089#endif
12090#ifdef FEAT_TAG_OLDSTATIC
12091 "tag_old_static",
12092#endif
12093#ifdef FEAT_TAG_ANYWHITE
12094 "tag_any_white",
12095#endif
12096#ifdef FEAT_TCL
12097# ifndef DYNAMIC_TCL
12098 "tcl",
12099# endif
12100#endif
12101#ifdef TERMINFO
12102 "terminfo",
12103#endif
12104#ifdef FEAT_TERMRESPONSE
12105 "termresponse",
12106#endif
12107#ifdef FEAT_TEXTOBJ
12108 "textobjects",
12109#endif
12110#ifdef HAVE_TGETENT
12111 "tgetent",
12112#endif
12113#ifdef FEAT_TITLE
12114 "title",
12115#endif
12116#ifdef FEAT_TOOLBAR
12117 "toolbar",
12118#endif
12119#ifdef FEAT_USR_CMDS
12120 "user-commands", /* was accidentally included in 5.4 */
12121 "user_commands",
12122#endif
12123#ifdef FEAT_VIMINFO
12124 "viminfo",
12125#endif
12126#ifdef FEAT_VERTSPLIT
12127 "vertsplit",
12128#endif
12129#ifdef FEAT_VIRTUALEDIT
12130 "virtualedit",
12131#endif
12132#ifdef FEAT_VISUAL
12133 "visual",
12134#endif
12135#ifdef FEAT_VISUALEXTRA
12136 "visualextra",
12137#endif
12138#ifdef FEAT_VREPLACE
12139 "vreplace",
12140#endif
12141#ifdef FEAT_WILDIGN
12142 "wildignore",
12143#endif
12144#ifdef FEAT_WILDMENU
12145 "wildmenu",
12146#endif
12147#ifdef FEAT_WINDOWS
12148 "windows",
12149#endif
12150#ifdef FEAT_WAK
12151 "winaltkeys",
12152#endif
12153#ifdef FEAT_WRITEBACKUP
12154 "writebackup",
12155#endif
12156#ifdef FEAT_XIM
12157 "xim",
12158#endif
12159#ifdef FEAT_XFONTSET
12160 "xfontset",
12161#endif
12162#ifdef USE_XSMP
12163 "xsmp",
12164#endif
12165#ifdef USE_XSMP_INTERACT
12166 "xsmp_interact",
12167#endif
12168#ifdef FEAT_XCLIPBOARD
12169 "xterm_clipboard",
12170#endif
12171#ifdef FEAT_XTERM_SAVE
12172 "xterm_save",
12173#endif
12174#if defined(UNIX) && defined(FEAT_X11)
12175 "X11",
12176#endif
12177 NULL
12178 };
12179
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012180 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181 for (i = 0; has_list[i] != NULL; ++i)
12182 if (STRICMP(name, has_list[i]) == 0)
12183 {
12184 n = TRUE;
12185 break;
12186 }
12187
12188 if (n == FALSE)
12189 {
12190 if (STRNICMP(name, "patch", 5) == 0)
12191 n = has_patch(atoi((char *)name + 5));
12192 else if (STRICMP(name, "vim_starting") == 0)
12193 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012194#ifdef FEAT_MBYTE
12195 else if (STRICMP(name, "multi_byte_encoding") == 0)
12196 n = has_mbyte;
12197#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012198#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12199 else if (STRICMP(name, "balloon_multiline") == 0)
12200 n = multiline_balloon_available();
12201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202#ifdef DYNAMIC_TCL
12203 else if (STRICMP(name, "tcl") == 0)
12204 n = tcl_enabled(FALSE);
12205#endif
12206#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12207 else if (STRICMP(name, "iconv") == 0)
12208 n = iconv_enabled(FALSE);
12209#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012210#ifdef DYNAMIC_LUA
12211 else if (STRICMP(name, "lua") == 0)
12212 n = lua_enabled(FALSE);
12213#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012214#ifdef DYNAMIC_MZSCHEME
12215 else if (STRICMP(name, "mzscheme") == 0)
12216 n = mzscheme_enabled(FALSE);
12217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012218#ifdef DYNAMIC_RUBY
12219 else if (STRICMP(name, "ruby") == 0)
12220 n = ruby_enabled(FALSE);
12221#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012222#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012223#ifdef DYNAMIC_PYTHON
12224 else if (STRICMP(name, "python") == 0)
12225 n = python_enabled(FALSE);
12226#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012227#endif
12228#ifdef FEAT_PYTHON3
12229#ifdef DYNAMIC_PYTHON3
12230 else if (STRICMP(name, "python3") == 0)
12231 n = python3_enabled(FALSE);
12232#endif
12233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#ifdef DYNAMIC_PERL
12235 else if (STRICMP(name, "perl") == 0)
12236 n = perl_enabled(FALSE);
12237#endif
12238#ifdef FEAT_GUI
12239 else if (STRICMP(name, "gui_running") == 0)
12240 n = (gui.in_use || gui.starting);
12241# ifdef FEAT_GUI_W32
12242 else if (STRICMP(name, "gui_win32s") == 0)
12243 n = gui_is_win32s();
12244# endif
12245# ifdef FEAT_BROWSE
12246 else if (STRICMP(name, "browse") == 0)
12247 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12248# endif
12249#endif
12250#ifdef FEAT_SYN_HL
12251 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012252 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253#endif
12254#if defined(WIN3264)
12255 else if (STRICMP(name, "win95") == 0)
12256 n = mch_windows95();
12257#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012258#ifdef FEAT_NETBEANS_INTG
12259 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012260 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262 }
12263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265}
12266
12267/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012268 * "has_key()" function
12269 */
12270 static void
12271f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012272 typval_T *argvars;
12273 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012274{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012275 if (argvars[0].v_type != VAR_DICT)
12276 {
12277 EMSG(_(e_dictreq));
12278 return;
12279 }
12280 if (argvars[0].vval.v_dict == NULL)
12281 return;
12282
12283 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012284 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012285}
12286
12287/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012288 * "haslocaldir()" function
12289 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012290 static void
12291f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012292 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012293 typval_T *rettv;
12294{
12295 rettv->vval.v_number = (curwin->w_localdir != NULL);
12296}
12297
12298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 * "hasmapto()" function
12300 */
12301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012303 typval_T *argvars;
12304 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305{
12306 char_u *name;
12307 char_u *mode;
12308 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012309 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012311 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012312 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012313 mode = (char_u *)"nvo";
12314 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012315 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012317 if (argvars[2].v_type != VAR_UNKNOWN)
12318 abbr = get_tv_number(&argvars[2]);
12319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320
Bram Moolenaar2c932302006-03-18 21:42:09 +000012321 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012322 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325}
12326
12327/*
12328 * "histadd()" function
12329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012331f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
12335#ifdef FEAT_CMDHIST
12336 int histype;
12337 char_u *str;
12338 char_u buf[NUMBUFLEN];
12339#endif
12340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 if (check_restricted() || check_secure())
12343 return;
12344#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012345 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12346 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347 if (histype >= 0)
12348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 if (*str != NUL)
12351 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012352 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 return;
12356 }
12357 }
12358#endif
12359}
12360
12361/*
12362 * "histdel()" function
12363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012365f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012366 typval_T *argvars UNUSED;
12367 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368{
12369#ifdef FEAT_CMDHIST
12370 int n;
12371 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012372 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012374 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12375 if (str == NULL)
12376 n = 0;
12377 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012378 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012379 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012380 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012382 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384 else
12385 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012386 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387 get_tv_string_buf(&argvars[1], buf));
12388 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389#endif
12390}
12391
12392/*
12393 * "histget()" function
12394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012397 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012399{
12400#ifdef FEAT_CMDHIST
12401 int type;
12402 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012403 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12406 if (str == NULL)
12407 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012409 {
12410 type = get_histtype(str);
12411 if (argvars[1].v_type == VAR_UNKNOWN)
12412 idx = get_history_idx(type);
12413 else
12414 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12415 /* -1 on type error */
12416 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012419 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012421 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422}
12423
12424/*
12425 * "histnr()" function
12426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012429 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431{
12432 int i;
12433
12434#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012435 char_u *history = get_tv_string_chk(&argvars[0]);
12436
12437 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 if (i >= HIST_CMD && i < HIST_COUNT)
12439 i = get_history_idx(i);
12440 else
12441#endif
12442 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444}
12445
12446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 * "highlightID(name)" function
12448 */
12449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012451 typval_T *argvars;
12452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012454 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455}
12456
12457/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012458 * "highlight_exists()" function
12459 */
12460 static void
12461f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012462 typval_T *argvars;
12463 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012464{
12465 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12466}
12467
12468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 * "hostname()" function
12470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012473 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475{
12476 char_u hostname[256];
12477
12478 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012479 rettv->v_type = VAR_STRING;
12480 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012481}
12482
12483/*
12484 * iconv() function
12485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012487f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012488 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490{
12491#ifdef FEAT_MBYTE
12492 char_u buf1[NUMBUFLEN];
12493 char_u buf2[NUMBUFLEN];
12494 char_u *from, *to, *str;
12495 vimconv_T vimconv;
12496#endif
12497
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012498 rettv->v_type = VAR_STRING;
12499 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500
12501#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012502 str = get_tv_string(&argvars[0]);
12503 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12504 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505 vimconv.vc_type = CONV_NONE;
12506 convert_setup(&vimconv, from, to);
12507
12508 /* If the encodings are equal, no conversion needed. */
12509 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012510 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513
12514 convert_setup(&vimconv, NULL, NULL);
12515 vim_free(from);
12516 vim_free(to);
12517#endif
12518}
12519
12520/*
12521 * "indent()" function
12522 */
12523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012525 typval_T *argvars;
12526 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527{
12528 linenr_T lnum;
12529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012530 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535}
12536
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012537/*
12538 * "index()" function
12539 */
12540 static void
12541f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012542 typval_T *argvars;
12543 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012544{
Bram Moolenaar33570922005-01-25 22:26:29 +000012545 list_T *l;
12546 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012547 long idx = 0;
12548 int ic = FALSE;
12549
12550 rettv->vval.v_number = -1;
12551 if (argvars[0].v_type != VAR_LIST)
12552 {
12553 EMSG(_(e_listreq));
12554 return;
12555 }
12556 l = argvars[0].vval.v_list;
12557 if (l != NULL)
12558 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012559 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012560 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012562 int error = FALSE;
12563
Bram Moolenaar758711c2005-02-02 23:11:38 +000012564 /* Start at specified item. Use the cached index that list_find()
12565 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012566 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012567 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012568 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012569 ic = get_tv_number_chk(&argvars[3], &error);
12570 if (error)
12571 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012572 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012573
Bram Moolenaar758711c2005-02-02 23:11:38 +000012574 for ( ; item != NULL; item = item->li_next, ++idx)
12575 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012576 {
12577 rettv->vval.v_number = idx;
12578 break;
12579 }
12580 }
12581}
12582
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583static int inputsecret_flag = 0;
12584
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012585static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12586
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012588 * This function is used by f_input() and f_inputdialog() functions. The third
12589 * argument to f_input() specifies the type of completion to use at the
12590 * prompt. The third argument to f_inputdialog() specifies the value to return
12591 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 */
12593 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012594get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012595 typval_T *argvars;
12596 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012597 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012598{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012599 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600 char_u *p = NULL;
12601 int c;
12602 char_u buf[NUMBUFLEN];
12603 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012604 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012605 int xp_type = EXPAND_NOTHING;
12606 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012608 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012609 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610
12611#ifdef NO_CONSOLE_INPUT
12612 /* While starting up, there is no place to enter text. */
12613 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615#endif
12616
12617 cmd_silent = FALSE; /* Want to see the prompt. */
12618 if (prompt != NULL)
12619 {
12620 /* Only the part of the message after the last NL is considered as
12621 * prompt for the command line */
12622 p = vim_strrchr(prompt, '\n');
12623 if (p == NULL)
12624 p = prompt;
12625 else
12626 {
12627 ++p;
12628 c = *p;
12629 *p = NUL;
12630 msg_start();
12631 msg_clr_eos();
12632 msg_puts_attr(prompt, echo_attr);
12633 msg_didout = FALSE;
12634 msg_starthere();
12635 *p = c;
12636 }
12637 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012639 if (argvars[1].v_type != VAR_UNKNOWN)
12640 {
12641 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12642 if (defstr != NULL)
12643 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012645 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012646 {
12647 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012648 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012649 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012650
Bram Moolenaar4463f292005-09-25 22:20:24 +000012651 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012652
Bram Moolenaar4463f292005-09-25 22:20:24 +000012653 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12654 if (xp_name == NULL)
12655 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012656
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012657 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012658
Bram Moolenaar4463f292005-09-25 22:20:24 +000012659 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12660 &xp_arg) == FAIL)
12661 return;
12662 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012663 }
12664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012665 if (defstr != NULL)
12666 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012667 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12668 xp_type, xp_arg);
12669
12670 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012672 /* since the user typed this, no need to wait for return */
12673 need_wait_return = FALSE;
12674 msg_didout = FALSE;
12675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 cmd_silent = cmd_silent_save;
12677}
12678
12679/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012680 * "input()" function
12681 * Also handles inputsecret() when inputsecret is set.
12682 */
12683 static void
12684f_input(argvars, rettv)
12685 typval_T *argvars;
12686 typval_T *rettv;
12687{
12688 get_user_input(argvars, rettv, FALSE);
12689}
12690
12691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 * "inputdialog()" function
12693 */
12694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012696 typval_T *argvars;
12697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698{
12699#if defined(FEAT_GUI_TEXTDIALOG)
12700 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12701 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12702 {
12703 char_u *message;
12704 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012705 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 message = get_tv_string_chk(&argvars[0]);
12708 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012709 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012710 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 else
12712 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012713 if (message != NULL && defstr != NULL
12714 && do_dialog(VIM_QUESTION, NULL, message,
12715 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717 else
12718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012719 if (message != NULL && defstr != NULL
12720 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012721 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722 rettv->vval.v_string = vim_strsave(
12723 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012725 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 }
12729 else
12730#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012731 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732}
12733
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012734/*
12735 * "inputlist()" function
12736 */
12737 static void
12738f_inputlist(argvars, rettv)
12739 typval_T *argvars;
12740 typval_T *rettv;
12741{
12742 listitem_T *li;
12743 int selected;
12744 int mouse_used;
12745
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012746#ifdef NO_CONSOLE_INPUT
12747 /* While starting up, there is no place to enter text. */
12748 if (no_console_input())
12749 return;
12750#endif
12751 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12752 {
12753 EMSG2(_(e_listarg), "inputlist()");
12754 return;
12755 }
12756
12757 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012758 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012759 lines_left = Rows; /* avoid more prompt */
12760 msg_scroll = TRUE;
12761 msg_clr_eos();
12762
12763 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12764 {
12765 msg_puts(get_tv_string(&li->li_tv));
12766 msg_putchar('\n');
12767 }
12768
12769 /* Ask for choice. */
12770 selected = prompt_for_number(&mouse_used);
12771 if (mouse_used)
12772 selected -= lines_left;
12773
12774 rettv->vval.v_number = selected;
12775}
12776
12777
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12779
12780/*
12781 * "inputrestore()" function
12782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012784f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012785 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012786 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787{
12788 if (ga_userinput.ga_len > 0)
12789 {
12790 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12792 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012793 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 }
12795 else if (p_verbose > 1)
12796 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012797 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012798 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 }
12800}
12801
12802/*
12803 * "inputsave()" function
12804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012807 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012810 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 if (ga_grow(&ga_userinput, 1) == OK)
12812 {
12813 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12814 + ga_userinput.ga_len);
12815 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012816 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 }
12818 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012819 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820}
12821
12822/*
12823 * "inputsecret()" function
12824 */
12825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012827 typval_T *argvars;
12828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012829{
12830 ++cmdline_star;
12831 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 --cmdline_star;
12834 --inputsecret_flag;
12835}
12836
12837/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012838 * "insert()" function
12839 */
12840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012842 typval_T *argvars;
12843 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012844{
12845 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 listitem_T *item;
12847 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012848 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012849
12850 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012851 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012852 else if ((l = argvars[0].vval.v_list) != NULL
12853 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012854 {
12855 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012856 before = get_tv_number_chk(&argvars[2], &error);
12857 if (error)
12858 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012859
Bram Moolenaar758711c2005-02-02 23:11:38 +000012860 if (before == l->lv_len)
12861 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012862 else
12863 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012864 item = list_find(l, before);
12865 if (item == NULL)
12866 {
12867 EMSGN(_(e_listidx), before);
12868 l = NULL;
12869 }
12870 }
12871 if (l != NULL)
12872 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012873 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012874 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012875 }
12876 }
12877}
12878
12879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 * "isdirectory()" function
12881 */
12882 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012884 typval_T *argvars;
12885 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888}
12889
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012890/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012891 * "islocked()" function
12892 */
12893 static void
12894f_islocked(argvars, rettv)
12895 typval_T *argvars;
12896 typval_T *rettv;
12897{
12898 lval_T lv;
12899 char_u *end;
12900 dictitem_T *di;
12901
12902 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012903 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12904 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012905 if (end != NULL && lv.ll_name != NULL)
12906 {
12907 if (*end != NUL)
12908 EMSG(_(e_trailing));
12909 else
12910 {
12911 if (lv.ll_tv == NULL)
12912 {
12913 if (check_changedtick(lv.ll_name))
12914 rettv->vval.v_number = 1; /* always locked */
12915 else
12916 {
12917 di = find_var(lv.ll_name, NULL);
12918 if (di != NULL)
12919 {
12920 /* Consider a variable locked when:
12921 * 1. the variable itself is locked
12922 * 2. the value of the variable is locked.
12923 * 3. the List or Dict value is locked.
12924 */
12925 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12926 || tv_islocked(&di->di_tv));
12927 }
12928 }
12929 }
12930 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012931 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012932 else if (lv.ll_newkey != NULL)
12933 EMSG2(_(e_dictkey), lv.ll_newkey);
12934 else if (lv.ll_list != NULL)
12935 /* List item. */
12936 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12937 else
12938 /* Dictionary item. */
12939 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12940 }
12941 }
12942
12943 clear_lval(&lv);
12944}
12945
Bram Moolenaar33570922005-01-25 22:26:29 +000012946static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012947
12948/*
12949 * Turn a dict into a list:
12950 * "what" == 0: list of keys
12951 * "what" == 1: list of values
12952 * "what" == 2: list of items
12953 */
12954 static void
12955dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012956 typval_T *argvars;
12957 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012958 int what;
12959{
Bram Moolenaar33570922005-01-25 22:26:29 +000012960 list_T *l2;
12961 dictitem_T *di;
12962 hashitem_T *hi;
12963 listitem_T *li;
12964 listitem_T *li2;
12965 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012966 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012967
Bram Moolenaar8c711452005-01-14 21:53:12 +000012968 if (argvars[0].v_type != VAR_DICT)
12969 {
12970 EMSG(_(e_dictreq));
12971 return;
12972 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012973 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012974 return;
12975
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012976 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012977 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012978
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012979 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012980 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012981 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012982 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012983 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012984 --todo;
12985 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012986
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012987 li = listitem_alloc();
12988 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012989 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012990 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012991
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012992 if (what == 0)
12993 {
12994 /* keys() */
12995 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012996 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012997 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12998 }
12999 else if (what == 1)
13000 {
13001 /* values() */
13002 copy_tv(&di->di_tv, &li->li_tv);
13003 }
13004 else
13005 {
13006 /* items() */
13007 l2 = list_alloc();
13008 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013009 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013010 li->li_tv.vval.v_list = l2;
13011 if (l2 == NULL)
13012 break;
13013 ++l2->lv_refcount;
13014
13015 li2 = listitem_alloc();
13016 if (li2 == NULL)
13017 break;
13018 list_append(l2, li2);
13019 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013020 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013021 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13022
13023 li2 = listitem_alloc();
13024 if (li2 == NULL)
13025 break;
13026 list_append(l2, li2);
13027 copy_tv(&di->di_tv, &li2->li_tv);
13028 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013029 }
13030 }
13031}
13032
13033/*
13034 * "items(dict)" function
13035 */
13036 static void
13037f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013038 typval_T *argvars;
13039 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013040{
13041 dict_list(argvars, rettv, 2);
13042}
13043
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013045 * "join()" function
13046 */
13047 static void
13048f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013049 typval_T *argvars;
13050 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013051{
13052 garray_T ga;
13053 char_u *sep;
13054
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013055 if (argvars[0].v_type != VAR_LIST)
13056 {
13057 EMSG(_(e_listreq));
13058 return;
13059 }
13060 if (argvars[0].vval.v_list == NULL)
13061 return;
13062 if (argvars[1].v_type == VAR_UNKNOWN)
13063 sep = (char_u *)" ";
13064 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013065 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013066
13067 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013068
13069 if (sep != NULL)
13070 {
13071 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013072 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013073 ga_append(&ga, NUL);
13074 rettv->vval.v_string = (char_u *)ga.ga_data;
13075 }
13076 else
13077 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013078}
13079
13080/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013081 * "keys()" function
13082 */
13083 static void
13084f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013085 typval_T *argvars;
13086 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013087{
13088 dict_list(argvars, rettv, 0);
13089}
13090
13091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 * "last_buffer_nr()" function.
13093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013096 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098{
13099 int n = 0;
13100 buf_T *buf;
13101
13102 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13103 if (n < buf->b_fnum)
13104 n = buf->b_fnum;
13105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013107}
13108
13109/*
13110 * "len()" function
13111 */
13112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013113f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013114 typval_T *argvars;
13115 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013116{
13117 switch (argvars[0].v_type)
13118 {
13119 case VAR_STRING:
13120 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121 rettv->vval.v_number = (varnumber_T)STRLEN(
13122 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013123 break;
13124 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013126 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013127 case VAR_DICT:
13128 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13129 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013130 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013131 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013132 break;
13133 }
13134}
13135
Bram Moolenaar33570922005-01-25 22:26:29 +000013136static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013137
13138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013139libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013140 typval_T *argvars;
13141 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013142 int type;
13143{
13144#ifdef FEAT_LIBCALL
13145 char_u *string_in;
13146 char_u **string_result;
13147 int nr_result;
13148#endif
13149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013150 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013151 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013153
13154 if (check_restricted() || check_secure())
13155 return;
13156
13157#ifdef FEAT_LIBCALL
13158 /* The first two args must be strings, otherwise its meaningless */
13159 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13160 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013161 string_in = NULL;
13162 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013163 string_in = argvars[2].vval.v_string;
13164 if (type == VAR_NUMBER)
13165 string_result = NULL;
13166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013168 if (mch_libcall(argvars[0].vval.v_string,
13169 argvars[1].vval.v_string,
13170 string_in,
13171 argvars[2].vval.v_number,
13172 string_result,
13173 &nr_result) == OK
13174 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013175 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013176 }
13177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178}
13179
13180/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013181 * "libcall()" function
13182 */
13183 static void
13184f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013185 typval_T *argvars;
13186 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013187{
13188 libcall_common(argvars, rettv, VAR_STRING);
13189}
13190
13191/*
13192 * "libcallnr()" function
13193 */
13194 static void
13195f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013196 typval_T *argvars;
13197 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013198{
13199 libcall_common(argvars, rettv, VAR_NUMBER);
13200}
13201
13202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 * "line(string)" function
13204 */
13205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013206f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 typval_T *argvars;
13208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209{
13210 linenr_T lnum = 0;
13211 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013212 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013214 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 if (fp != NULL)
13216 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013217 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218}
13219
13220/*
13221 * "line2byte(lnum)" function
13222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013224f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013225 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227{
13228#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230#else
13231 linenr_T lnum;
13232
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013233 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13238 if (rettv->vval.v_number >= 0)
13239 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240#endif
13241}
13242
13243/*
13244 * "lispindent(lnum)" function
13245 */
13246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013247f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013248 typval_T *argvars;
13249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250{
13251#ifdef FEAT_LISP
13252 pos_T pos;
13253 linenr_T lnum;
13254
13255 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013257 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13258 {
13259 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013260 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 curwin->w_cursor = pos;
13262 }
13263 else
13264#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013265 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266}
13267
13268/*
13269 * "localtime()" function
13270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013273 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013274 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013276 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277}
13278
Bram Moolenaar33570922005-01-25 22:26:29 +000013279static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280
13281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013283 typval_T *argvars;
13284 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 int exact;
13286{
13287 char_u *keys;
13288 char_u *which;
13289 char_u buf[NUMBUFLEN];
13290 char_u *keys_buf = NULL;
13291 char_u *rhs;
13292 int mode;
13293 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013294 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295
13296 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013297 rettv->v_type = VAR_STRING;
13298 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013300 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 if (*keys == NUL)
13302 return;
13303
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013304 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013306 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013307 if (argvars[2].v_type != VAR_UNKNOWN)
13308 abbr = get_tv_number(&argvars[2]);
13309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310 else
13311 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013312 if (which == NULL)
13313 return;
13314
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 mode = get_map_mode(&which, 0);
13316
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013317 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013318 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319 vim_free(keys_buf);
13320 if (rhs != NULL)
13321 {
13322 ga_init(&ga);
13323 ga.ga_itemsize = 1;
13324 ga.ga_growsize = 40;
13325
13326 while (*rhs != NUL)
13327 ga_concat(&ga, str2special(&rhs, FALSE));
13328
13329 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013330 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 }
13332}
13333
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013334#ifdef FEAT_FLOAT
13335/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013336 * "log()" function
13337 */
13338 static void
13339f_log(argvars, rettv)
13340 typval_T *argvars;
13341 typval_T *rettv;
13342{
13343 float_T f;
13344
13345 rettv->v_type = VAR_FLOAT;
13346 if (get_float_arg(argvars, &f) == OK)
13347 rettv->vval.v_float = log(f);
13348 else
13349 rettv->vval.v_float = 0.0;
13350}
13351
13352/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013353 * "log10()" function
13354 */
13355 static void
13356f_log10(argvars, rettv)
13357 typval_T *argvars;
13358 typval_T *rettv;
13359{
13360 float_T f;
13361
13362 rettv->v_type = VAR_FLOAT;
13363 if (get_float_arg(argvars, &f) == OK)
13364 rettv->vval.v_float = log10(f);
13365 else
13366 rettv->vval.v_float = 0.0;
13367}
13368#endif
13369
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013371 * "map()" function
13372 */
13373 static void
13374f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013375 typval_T *argvars;
13376 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013377{
13378 filter_map(argvars, rettv, TRUE);
13379}
13380
13381/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013382 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 */
13384 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013385f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 typval_T *argvars;
13387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013389 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390}
13391
13392/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013393 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394 */
13395 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013396f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013397 typval_T *argvars;
13398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013399{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013400 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401}
13402
Bram Moolenaar33570922005-01-25 22:26:29 +000013403static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404
13405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013406find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013407 typval_T *argvars;
13408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013409 int type;
13410{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013411 char_u *str = NULL;
13412 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 char_u *pat;
13414 regmatch_T regmatch;
13415 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013416 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 char_u *save_cpo;
13418 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013419 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013420 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013421 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013422 list_T *l = NULL;
13423 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013424 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013425 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426
13427 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13428 save_cpo = p_cpo;
13429 p_cpo = (char_u *)"";
13430
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013431 rettv->vval.v_number = -1;
13432 if (type == 3)
13433 {
13434 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013435 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013436 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013437 }
13438 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013440 rettv->v_type = VAR_STRING;
13441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013444 if (argvars[0].v_type == VAR_LIST)
13445 {
13446 if ((l = argvars[0].vval.v_list) == NULL)
13447 goto theend;
13448 li = l->lv_first;
13449 }
13450 else
13451 expr = str = get_tv_string(&argvars[0]);
13452
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013453 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13454 if (pat == NULL)
13455 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013456
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013457 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013459 int error = FALSE;
13460
13461 start = get_tv_number_chk(&argvars[2], &error);
13462 if (error)
13463 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464 if (l != NULL)
13465 {
13466 li = list_find(l, start);
13467 if (li == NULL)
13468 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013469 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013470 }
13471 else
13472 {
13473 if (start < 0)
13474 start = 0;
13475 if (start > (long)STRLEN(str))
13476 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013477 /* When "count" argument is there ignore matches before "start",
13478 * otherwise skip part of the string. Differs when pattern is "^"
13479 * or "\<". */
13480 if (argvars[3].v_type != VAR_UNKNOWN)
13481 startcol = start;
13482 else
13483 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013484 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013485
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013486 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013487 nth = get_tv_number_chk(&argvars[3], &error);
13488 if (error)
13489 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 }
13491
13492 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13493 if (regmatch.regprog != NULL)
13494 {
13495 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013496
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013497 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013498 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013499 if (l != NULL)
13500 {
13501 if (li == NULL)
13502 {
13503 match = FALSE;
13504 break;
13505 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013506 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013507 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013508 if (str == NULL)
13509 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013510 }
13511
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013512 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013513
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013514 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013515 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013516 if (l == NULL && !match)
13517 break;
13518
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013519 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013520 if (l != NULL)
13521 {
13522 li = li->li_next;
13523 ++idx;
13524 }
13525 else
13526 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013527#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013528 startcol = (colnr_T)(regmatch.startp[0]
13529 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013530#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013531 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013532#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013533 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013534 }
13535
13536 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013538 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013539 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013540 int i;
13541
13542 /* return list with matched string and submatches */
13543 for (i = 0; i < NSUBEXP; ++i)
13544 {
13545 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013546 {
13547 if (list_append_string(rettv->vval.v_list,
13548 (char_u *)"", 0) == FAIL)
13549 break;
13550 }
13551 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013552 regmatch.startp[i],
13553 (int)(regmatch.endp[i] - regmatch.startp[i]))
13554 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013555 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013556 }
13557 }
13558 else if (type == 2)
13559 {
13560 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013561 if (l != NULL)
13562 copy_tv(&li->li_tv, rettv);
13563 else
13564 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013565 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013566 }
13567 else if (l != NULL)
13568 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013569 else
13570 {
13571 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013572 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573 (varnumber_T)(regmatch.startp[0] - str);
13574 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013575 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013577 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 }
13579 }
13580 vim_free(regmatch.regprog);
13581 }
13582
13583theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013584 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585 p_cpo = save_cpo;
13586}
13587
13588/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013589 * "match()" function
13590 */
13591 static void
13592f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013593 typval_T *argvars;
13594 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013595{
13596 find_some_match(argvars, rettv, 1);
13597}
13598
13599/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013600 * "matchadd()" function
13601 */
13602 static void
13603f_matchadd(argvars, rettv)
13604 typval_T *argvars;
13605 typval_T *rettv;
13606{
13607#ifdef FEAT_SEARCH_EXTRA
13608 char_u buf[NUMBUFLEN];
13609 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13610 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13611 int prio = 10; /* default priority */
13612 int id = -1;
13613 int error = FALSE;
13614
13615 rettv->vval.v_number = -1;
13616
13617 if (grp == NULL || pat == NULL)
13618 return;
13619 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013620 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013621 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013622 if (argvars[3].v_type != VAR_UNKNOWN)
13623 id = get_tv_number_chk(&argvars[3], &error);
13624 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013625 if (error == TRUE)
13626 return;
13627 if (id >= 1 && id <= 3)
13628 {
13629 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13630 return;
13631 }
13632
13633 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13634#endif
13635}
13636
13637/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013638 * "matcharg()" function
13639 */
13640 static void
13641f_matcharg(argvars, rettv)
13642 typval_T *argvars;
13643 typval_T *rettv;
13644{
13645 if (rettv_list_alloc(rettv) == OK)
13646 {
13647#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013648 int id = get_tv_number(&argvars[0]);
13649 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013650
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013651 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013652 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013653 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13654 {
13655 list_append_string(rettv->vval.v_list,
13656 syn_id2name(m->hlg_id), -1);
13657 list_append_string(rettv->vval.v_list, m->pattern, -1);
13658 }
13659 else
13660 {
13661 list_append_string(rettv->vval.v_list, NUL, -1);
13662 list_append_string(rettv->vval.v_list, NUL, -1);
13663 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013664 }
13665#endif
13666 }
13667}
13668
13669/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013670 * "matchdelete()" function
13671 */
13672 static void
13673f_matchdelete(argvars, rettv)
13674 typval_T *argvars;
13675 typval_T *rettv;
13676{
13677#ifdef FEAT_SEARCH_EXTRA
13678 rettv->vval.v_number = match_delete(curwin,
13679 (int)get_tv_number(&argvars[0]), TRUE);
13680#endif
13681}
13682
13683/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013684 * "matchend()" function
13685 */
13686 static void
13687f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *argvars;
13689 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013690{
13691 find_some_match(argvars, rettv, 0);
13692}
13693
13694/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013695 * "matchlist()" function
13696 */
13697 static void
13698f_matchlist(argvars, rettv)
13699 typval_T *argvars;
13700 typval_T *rettv;
13701{
13702 find_some_match(argvars, rettv, 3);
13703}
13704
13705/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013706 * "matchstr()" function
13707 */
13708 static void
13709f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013710 typval_T *argvars;
13711 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013712{
13713 find_some_match(argvars, rettv, 2);
13714}
13715
Bram Moolenaar33570922005-01-25 22:26:29 +000013716static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013717
13718 static void
13719max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013720 typval_T *argvars;
13721 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013722 int domax;
13723{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013724 long n = 0;
13725 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013726 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013727
13728 if (argvars[0].v_type == VAR_LIST)
13729 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013730 list_T *l;
13731 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013732
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013733 l = argvars[0].vval.v_list;
13734 if (l != NULL)
13735 {
13736 li = l->lv_first;
13737 if (li != NULL)
13738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013739 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013740 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013741 {
13742 li = li->li_next;
13743 if (li == NULL)
13744 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013745 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013746 if (domax ? i > n : i < n)
13747 n = i;
13748 }
13749 }
13750 }
13751 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013752 else if (argvars[0].v_type == VAR_DICT)
13753 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013754 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013755 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013756 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013757 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013758
13759 d = argvars[0].vval.v_dict;
13760 if (d != NULL)
13761 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013762 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013764 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013765 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013766 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013767 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013768 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013769 if (first)
13770 {
13771 n = i;
13772 first = FALSE;
13773 }
13774 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013775 n = i;
13776 }
13777 }
13778 }
13779 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013780 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013781 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013783}
13784
13785/*
13786 * "max()" function
13787 */
13788 static void
13789f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013790 typval_T *argvars;
13791 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013792{
13793 max_min(argvars, rettv, TRUE);
13794}
13795
13796/*
13797 * "min()" function
13798 */
13799 static void
13800f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 typval_T *argvars;
13802 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013803{
13804 max_min(argvars, rettv, FALSE);
13805}
13806
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013807static int mkdir_recurse __ARGS((char_u *dir, int prot));
13808
13809/*
13810 * Create the directory in which "dir" is located, and higher levels when
13811 * needed.
13812 */
13813 static int
13814mkdir_recurse(dir, prot)
13815 char_u *dir;
13816 int prot;
13817{
13818 char_u *p;
13819 char_u *updir;
13820 int r = FAIL;
13821
13822 /* Get end of directory name in "dir".
13823 * We're done when it's "/" or "c:/". */
13824 p = gettail_sep(dir);
13825 if (p <= get_past_head(dir))
13826 return OK;
13827
13828 /* If the directory exists we're done. Otherwise: create it.*/
13829 updir = vim_strnsave(dir, (int)(p - dir));
13830 if (updir == NULL)
13831 return FAIL;
13832 if (mch_isdir(updir))
13833 r = OK;
13834 else if (mkdir_recurse(updir, prot) == OK)
13835 r = vim_mkdir_emsg(updir, prot);
13836 vim_free(updir);
13837 return r;
13838}
13839
13840#ifdef vim_mkdir
13841/*
13842 * "mkdir()" function
13843 */
13844 static void
13845f_mkdir(argvars, rettv)
13846 typval_T *argvars;
13847 typval_T *rettv;
13848{
13849 char_u *dir;
13850 char_u buf[NUMBUFLEN];
13851 int prot = 0755;
13852
13853 rettv->vval.v_number = FAIL;
13854 if (check_restricted() || check_secure())
13855 return;
13856
13857 dir = get_tv_string_buf(&argvars[0], buf);
13858 if (argvars[1].v_type != VAR_UNKNOWN)
13859 {
13860 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013861 prot = get_tv_number_chk(&argvars[2], NULL);
13862 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013863 mkdir_recurse(dir, prot);
13864 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013865 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013866}
13867#endif
13868
Bram Moolenaar0d660222005-01-07 21:51:51 +000013869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 * "mode()" function
13871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013873f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013874 typval_T *argvars;
13875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013877 char_u buf[3];
13878
13879 buf[1] = NUL;
13880 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881
13882#ifdef FEAT_VISUAL
13883 if (VIsual_active)
13884 {
13885 if (VIsual_select)
13886 buf[0] = VIsual_mode + 's' - 'v';
13887 else
13888 buf[0] = VIsual_mode;
13889 }
13890 else
13891#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013892 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13893 || State == CONFIRM)
13894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013896 if (State == ASKMORE)
13897 buf[1] = 'm';
13898 else if (State == CONFIRM)
13899 buf[1] = '?';
13900 }
13901 else if (State == EXTERNCMD)
13902 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 else if (State & INSERT)
13904 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013905#ifdef FEAT_VREPLACE
13906 if (State & VREPLACE_FLAG)
13907 {
13908 buf[0] = 'R';
13909 buf[1] = 'v';
13910 }
13911 else
13912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 if (State & REPLACE_FLAG)
13914 buf[0] = 'R';
13915 else
13916 buf[0] = 'i';
13917 }
13918 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013921 if (exmode_active)
13922 buf[1] = 'v';
13923 }
13924 else if (exmode_active)
13925 {
13926 buf[0] = 'c';
13927 buf[1] = 'e';
13928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013932 if (finish_op)
13933 buf[1] = 'o';
13934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013936 /* Clear out the minor mode when the argument is not a non-zero number or
13937 * non-empty string. */
13938 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013939 buf[1] = NUL;
13940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013941 rettv->vval.v_string = vim_strsave(buf);
13942 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943}
13944
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013945#ifdef FEAT_MZSCHEME
13946/*
13947 * "mzeval()" function
13948 */
13949 static void
13950f_mzeval(argvars, rettv)
13951 typval_T *argvars;
13952 typval_T *rettv;
13953{
13954 char_u *str;
13955 char_u buf[NUMBUFLEN];
13956
13957 str = get_tv_string_buf(&argvars[0], buf);
13958 do_mzeval(str, rettv);
13959}
13960#endif
13961
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013963 * "nextnonblank()" function
13964 */
13965 static void
13966f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013967 typval_T *argvars;
13968 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013969{
13970 linenr_T lnum;
13971
13972 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013974 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013975 {
13976 lnum = 0;
13977 break;
13978 }
13979 if (*skipwhite(ml_get(lnum)) != NUL)
13980 break;
13981 }
13982 rettv->vval.v_number = lnum;
13983}
13984
13985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986 * "nr2char()" function
13987 */
13988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013990 typval_T *argvars;
13991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992{
13993 char_u buf[NUMBUFLEN];
13994
13995#ifdef FEAT_MBYTE
13996 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013997 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013998 else
13999#endif
14000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014001 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 buf[1] = NUL;
14003 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014004 rettv->v_type = VAR_STRING;
14005 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014006}
14007
14008/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014009 * "pathshorten()" function
14010 */
14011 static void
14012f_pathshorten(argvars, rettv)
14013 typval_T *argvars;
14014 typval_T *rettv;
14015{
14016 char_u *p;
14017
14018 rettv->v_type = VAR_STRING;
14019 p = get_tv_string_chk(&argvars[0]);
14020 if (p == NULL)
14021 rettv->vval.v_string = NULL;
14022 else
14023 {
14024 p = vim_strsave(p);
14025 rettv->vval.v_string = p;
14026 if (p != NULL)
14027 shorten_dir(p);
14028 }
14029}
14030
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014031#ifdef FEAT_FLOAT
14032/*
14033 * "pow()" function
14034 */
14035 static void
14036f_pow(argvars, rettv)
14037 typval_T *argvars;
14038 typval_T *rettv;
14039{
14040 float_T fx, fy;
14041
14042 rettv->v_type = VAR_FLOAT;
14043 if (get_float_arg(argvars, &fx) == OK
14044 && get_float_arg(&argvars[1], &fy) == OK)
14045 rettv->vval.v_float = pow(fx, fy);
14046 else
14047 rettv->vval.v_float = 0.0;
14048}
14049#endif
14050
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014051/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014052 * "prevnonblank()" function
14053 */
14054 static void
14055f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014056 typval_T *argvars;
14057 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058{
14059 linenr_T lnum;
14060
14061 lnum = get_tv_lnum(argvars);
14062 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14063 lnum = 0;
14064 else
14065 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14066 --lnum;
14067 rettv->vval.v_number = lnum;
14068}
14069
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014070#ifdef HAVE_STDARG_H
14071/* This dummy va_list is here because:
14072 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14073 * - locally in the function results in a "used before set" warning
14074 * - using va_start() to initialize it gives "function with fixed args" error */
14075static va_list ap;
14076#endif
14077
Bram Moolenaar8c711452005-01-14 21:53:12 +000014078/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014079 * "printf()" function
14080 */
14081 static void
14082f_printf(argvars, rettv)
14083 typval_T *argvars;
14084 typval_T *rettv;
14085{
14086 rettv->v_type = VAR_STRING;
14087 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014088#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014089 {
14090 char_u buf[NUMBUFLEN];
14091 int len;
14092 char_u *s;
14093 int saved_did_emsg = did_emsg;
14094 char *fmt;
14095
14096 /* Get the required length, allocate the buffer and do it for real. */
14097 did_emsg = FALSE;
14098 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014099 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014100 if (!did_emsg)
14101 {
14102 s = alloc(len + 1);
14103 if (s != NULL)
14104 {
14105 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014106 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014107 }
14108 }
14109 did_emsg |= saved_did_emsg;
14110 }
14111#endif
14112}
14113
14114/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014115 * "pumvisible()" function
14116 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014117 static void
14118f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014119 typval_T *argvars UNUSED;
14120 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014121{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014122#ifdef FEAT_INS_EXPAND
14123 if (pum_visible())
14124 rettv->vval.v_number = 1;
14125#endif
14126}
14127
14128/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014129 * "range()" function
14130 */
14131 static void
14132f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014133 typval_T *argvars;
14134 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014135{
14136 long start;
14137 long end;
14138 long stride = 1;
14139 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014140 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014142 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014143 if (argvars[1].v_type == VAR_UNKNOWN)
14144 {
14145 end = start - 1;
14146 start = 0;
14147 }
14148 else
14149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014150 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014151 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014153 }
14154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014155 if (error)
14156 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014157 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014158 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014159 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014160 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014161 else
14162 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014163 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014164 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014165 if (list_append_number(rettv->vval.v_list,
14166 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014167 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014168 }
14169}
14170
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014171/*
14172 * "readfile()" function
14173 */
14174 static void
14175f_readfile(argvars, rettv)
14176 typval_T *argvars;
14177 typval_T *rettv;
14178{
14179 int binary = FALSE;
14180 char_u *fname;
14181 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014182 listitem_T *li;
14183#define FREAD_SIZE 200 /* optimized for text lines */
14184 char_u buf[FREAD_SIZE];
14185 int readlen; /* size of last fread() */
14186 int buflen; /* nr of valid chars in buf[] */
14187 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14188 int tolist; /* first byte in buf[] still to be put in list */
14189 int chop; /* how many CR to chop off */
14190 char_u *prev = NULL; /* previously read bytes, if any */
14191 int prevlen = 0; /* length of "prev" if not NULL */
14192 char_u *s;
14193 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014194 long maxline = MAXLNUM;
14195 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014196
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014197 if (argvars[1].v_type != VAR_UNKNOWN)
14198 {
14199 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14200 binary = TRUE;
14201 if (argvars[2].v_type != VAR_UNKNOWN)
14202 maxline = get_tv_number(&argvars[2]);
14203 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014204
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014205 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014206 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014207
14208 /* Always open the file in binary mode, library functions have a mind of
14209 * their own about CR-LF conversion. */
14210 fname = get_tv_string(&argvars[0]);
14211 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14212 {
14213 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14214 return;
14215 }
14216
14217 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014218 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014219 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014220 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014221 buflen = filtd + readlen;
14222 tolist = 0;
14223 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14224 {
14225 if (buf[filtd] == '\n' || readlen <= 0)
14226 {
14227 /* Only when in binary mode add an empty list item when the
14228 * last line ends in a '\n'. */
14229 if (!binary && readlen == 0 && filtd == 0)
14230 break;
14231
14232 /* Found end-of-line or end-of-file: add a text line to the
14233 * list. */
14234 chop = 0;
14235 if (!binary)
14236 while (filtd - chop - 1 >= tolist
14237 && buf[filtd - chop - 1] == '\r')
14238 ++chop;
14239 len = filtd - tolist - chop;
14240 if (prev == NULL)
14241 s = vim_strnsave(buf + tolist, len);
14242 else
14243 {
14244 s = alloc((unsigned)(prevlen + len + 1));
14245 if (s != NULL)
14246 {
14247 mch_memmove(s, prev, prevlen);
14248 vim_free(prev);
14249 prev = NULL;
14250 mch_memmove(s + prevlen, buf + tolist, len);
14251 s[prevlen + len] = NUL;
14252 }
14253 }
14254 tolist = filtd + 1;
14255
14256 li = listitem_alloc();
14257 if (li == NULL)
14258 {
14259 vim_free(s);
14260 break;
14261 }
14262 li->li_tv.v_type = VAR_STRING;
14263 li->li_tv.v_lock = 0;
14264 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014265 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014266
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014267 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014268 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014269 if (readlen <= 0)
14270 break;
14271 }
14272 else if (buf[filtd] == NUL)
14273 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014274#ifdef FEAT_MBYTE
14275 else if (buf[filtd] == 0xef
14276 && enc_utf8
14277 && filtd + 2 < buflen
14278 && !binary
14279 && buf[filtd + 1] == 0xbb
14280 && buf[filtd + 2] == 0xbf)
14281 {
14282 /* remove utf-8 byte order mark */
14283 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14284 --filtd;
14285 buflen -= 3;
14286 }
14287#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014288 }
14289 if (readlen <= 0)
14290 break;
14291
14292 if (tolist == 0)
14293 {
14294 /* "buf" is full, need to move text to an allocated buffer */
14295 if (prev == NULL)
14296 {
14297 prev = vim_strnsave(buf, buflen);
14298 prevlen = buflen;
14299 }
14300 else
14301 {
14302 s = alloc((unsigned)(prevlen + buflen));
14303 if (s != NULL)
14304 {
14305 mch_memmove(s, prev, prevlen);
14306 mch_memmove(s + prevlen, buf, buflen);
14307 vim_free(prev);
14308 prev = s;
14309 prevlen += buflen;
14310 }
14311 }
14312 filtd = 0;
14313 }
14314 else
14315 {
14316 mch_memmove(buf, buf + tolist, buflen - tolist);
14317 filtd -= tolist;
14318 }
14319 }
14320
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014321 /*
14322 * For a negative line count use only the lines at the end of the file,
14323 * free the rest.
14324 */
14325 if (maxline < 0)
14326 while (cnt > -maxline)
14327 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014328 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014329 --cnt;
14330 }
14331
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014332 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014333 fclose(fd);
14334}
14335
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014336#if defined(FEAT_RELTIME)
14337static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14338
14339/*
14340 * Convert a List to proftime_T.
14341 * Return FAIL when there is something wrong.
14342 */
14343 static int
14344list2proftime(arg, tm)
14345 typval_T *arg;
14346 proftime_T *tm;
14347{
14348 long n1, n2;
14349 int error = FALSE;
14350
14351 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14352 || arg->vval.v_list->lv_len != 2)
14353 return FAIL;
14354 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14355 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14356# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014357 tm->HighPart = n1;
14358 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014359# else
14360 tm->tv_sec = n1;
14361 tm->tv_usec = n2;
14362# endif
14363 return error ? FAIL : OK;
14364}
14365#endif /* FEAT_RELTIME */
14366
14367/*
14368 * "reltime()" function
14369 */
14370 static void
14371f_reltime(argvars, rettv)
14372 typval_T *argvars;
14373 typval_T *rettv;
14374{
14375#ifdef FEAT_RELTIME
14376 proftime_T res;
14377 proftime_T start;
14378
14379 if (argvars[0].v_type == VAR_UNKNOWN)
14380 {
14381 /* No arguments: get current time. */
14382 profile_start(&res);
14383 }
14384 else if (argvars[1].v_type == VAR_UNKNOWN)
14385 {
14386 if (list2proftime(&argvars[0], &res) == FAIL)
14387 return;
14388 profile_end(&res);
14389 }
14390 else
14391 {
14392 /* Two arguments: compute the difference. */
14393 if (list2proftime(&argvars[0], &start) == FAIL
14394 || list2proftime(&argvars[1], &res) == FAIL)
14395 return;
14396 profile_sub(&res, &start);
14397 }
14398
14399 if (rettv_list_alloc(rettv) == OK)
14400 {
14401 long n1, n2;
14402
14403# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014404 n1 = res.HighPart;
14405 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014406# else
14407 n1 = res.tv_sec;
14408 n2 = res.tv_usec;
14409# endif
14410 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14411 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14412 }
14413#endif
14414}
14415
14416/*
14417 * "reltimestr()" function
14418 */
14419 static void
14420f_reltimestr(argvars, rettv)
14421 typval_T *argvars;
14422 typval_T *rettv;
14423{
14424#ifdef FEAT_RELTIME
14425 proftime_T tm;
14426#endif
14427
14428 rettv->v_type = VAR_STRING;
14429 rettv->vval.v_string = NULL;
14430#ifdef FEAT_RELTIME
14431 if (list2proftime(&argvars[0], &tm) == OK)
14432 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14433#endif
14434}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014435
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14437static void make_connection __ARGS((void));
14438static int check_connection __ARGS((void));
14439
14440 static void
14441make_connection()
14442{
14443 if (X_DISPLAY == NULL
14444# ifdef FEAT_GUI
14445 && !gui.in_use
14446# endif
14447 )
14448 {
14449 x_force_connect = TRUE;
14450 setup_term_clip();
14451 x_force_connect = FALSE;
14452 }
14453}
14454
14455 static int
14456check_connection()
14457{
14458 make_connection();
14459 if (X_DISPLAY == NULL)
14460 {
14461 EMSG(_("E240: No connection to Vim server"));
14462 return FAIL;
14463 }
14464 return OK;
14465}
14466#endif
14467
14468#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014469static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014470
14471 static void
14472remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014473 typval_T *argvars;
14474 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014475 int expr;
14476{
14477 char_u *server_name;
14478 char_u *keys;
14479 char_u *r = NULL;
14480 char_u buf[NUMBUFLEN];
14481# ifdef WIN32
14482 HWND w;
14483# else
14484 Window w;
14485# endif
14486
14487 if (check_restricted() || check_secure())
14488 return;
14489
14490# ifdef FEAT_X11
14491 if (check_connection() == FAIL)
14492 return;
14493# endif
14494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014495 server_name = get_tv_string_chk(&argvars[0]);
14496 if (server_name == NULL)
14497 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014498 keys = get_tv_string_buf(&argvars[1], buf);
14499# ifdef WIN32
14500 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14501# else
14502 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14503 < 0)
14504# endif
14505 {
14506 if (r != NULL)
14507 EMSG(r); /* sending worked but evaluation failed */
14508 else
14509 EMSG2(_("E241: Unable to send to %s"), server_name);
14510 return;
14511 }
14512
14513 rettv->vval.v_string = r;
14514
14515 if (argvars[2].v_type != VAR_UNKNOWN)
14516 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014517 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014518 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014520
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014521 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014522 v.di_tv.v_type = VAR_STRING;
14523 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014524 idvar = get_tv_string_chk(&argvars[2]);
14525 if (idvar != NULL)
14526 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014527 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014528 }
14529}
14530#endif
14531
14532/*
14533 * "remote_expr()" function
14534 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014535 static void
14536f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014537 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014538 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014539{
14540 rettv->v_type = VAR_STRING;
14541 rettv->vval.v_string = NULL;
14542#ifdef FEAT_CLIENTSERVER
14543 remote_common(argvars, rettv, TRUE);
14544#endif
14545}
14546
14547/*
14548 * "remote_foreground()" function
14549 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014550 static void
14551f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014552 typval_T *argvars UNUSED;
14553 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014554{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014555#ifdef FEAT_CLIENTSERVER
14556# ifdef WIN32
14557 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 {
14559 char_u *server_name = get_tv_string_chk(&argvars[0]);
14560
14561 if (server_name != NULL)
14562 serverForeground(server_name);
14563 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014564# else
14565 /* Send a foreground() expression to the server. */
14566 argvars[1].v_type = VAR_STRING;
14567 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14568 argvars[2].v_type = VAR_UNKNOWN;
14569 remote_common(argvars, rettv, TRUE);
14570 vim_free(argvars[1].vval.v_string);
14571# endif
14572#endif
14573}
14574
Bram Moolenaar0d660222005-01-07 21:51:51 +000014575 static void
14576f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014577 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014578 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014579{
14580#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014581 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582 char_u *s = NULL;
14583# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014584 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014585# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014586 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014587
14588 if (check_restricted() || check_secure())
14589 {
14590 rettv->vval.v_number = -1;
14591 return;
14592 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014593 serverid = get_tv_string_chk(&argvars[0]);
14594 if (serverid == NULL)
14595 {
14596 rettv->vval.v_number = -1;
14597 return; /* type error; errmsg already given */
14598 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014599# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014600 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014601 if (n == 0)
14602 rettv->vval.v_number = -1;
14603 else
14604 {
14605 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14606 rettv->vval.v_number = (s != NULL);
14607 }
14608# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014609 if (check_connection() == FAIL)
14610 return;
14611
14612 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014613 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014614# endif
14615
14616 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14617 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014618 char_u *retvar;
14619
Bram Moolenaar33570922005-01-25 22:26:29 +000014620 v.di_tv.v_type = VAR_STRING;
14621 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014622 retvar = get_tv_string_chk(&argvars[1]);
14623 if (retvar != NULL)
14624 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014625 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014626 }
14627#else
14628 rettv->vval.v_number = -1;
14629#endif
14630}
14631
Bram Moolenaar0d660222005-01-07 21:51:51 +000014632 static void
14633f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014634 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014635 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014636{
14637 char_u *r = NULL;
14638
14639#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014640 char_u *serverid = get_tv_string_chk(&argvars[0]);
14641
14642 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014643 {
14644# ifdef WIN32
14645 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014646 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014647
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014648 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 if (n != 0)
14650 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14651 if (r == NULL)
14652# else
14653 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014655# endif
14656 EMSG(_("E277: Unable to read a server reply"));
14657 }
14658#endif
14659 rettv->v_type = VAR_STRING;
14660 rettv->vval.v_string = r;
14661}
14662
14663/*
14664 * "remote_send()" function
14665 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014666 static void
14667f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014668 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014669 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014670{
14671 rettv->v_type = VAR_STRING;
14672 rettv->vval.v_string = NULL;
14673#ifdef FEAT_CLIENTSERVER
14674 remote_common(argvars, rettv, FALSE);
14675#endif
14676}
14677
14678/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014679 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014680 */
14681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014683 typval_T *argvars;
14684 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014685{
Bram Moolenaar33570922005-01-25 22:26:29 +000014686 list_T *l;
14687 listitem_T *item, *item2;
14688 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014689 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014690 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014691 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014692 dict_T *d;
14693 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014694
Bram Moolenaar8c711452005-01-14 21:53:12 +000014695 if (argvars[0].v_type == VAR_DICT)
14696 {
14697 if (argvars[2].v_type != VAR_UNKNOWN)
14698 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014699 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014700 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014702 key = get_tv_string_chk(&argvars[1]);
14703 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014705 di = dict_find(d, key, -1);
14706 if (di == NULL)
14707 EMSG2(_(e_dictkey), key);
14708 else
14709 {
14710 *rettv = di->di_tv;
14711 init_tv(&di->di_tv);
14712 dictitem_remove(d, di);
14713 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014715 }
14716 }
14717 else if (argvars[0].v_type != VAR_LIST)
14718 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014719 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014720 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014721 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014722 int error = FALSE;
14723
14724 idx = get_tv_number_chk(&argvars[1], &error);
14725 if (error)
14726 ; /* type error: do nothing, errmsg already given */
14727 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014728 EMSGN(_(e_listidx), idx);
14729 else
14730 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014731 if (argvars[2].v_type == VAR_UNKNOWN)
14732 {
14733 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014734 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014735 *rettv = item->li_tv;
14736 vim_free(item);
14737 }
14738 else
14739 {
14740 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014741 end = get_tv_number_chk(&argvars[2], &error);
14742 if (error)
14743 ; /* type error: do nothing */
14744 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014745 EMSGN(_(e_listidx), end);
14746 else
14747 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014748 int cnt = 0;
14749
14750 for (li = item; li != NULL; li = li->li_next)
14751 {
14752 ++cnt;
14753 if (li == item2)
14754 break;
14755 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014756 if (li == NULL) /* didn't find "item2" after "item" */
14757 EMSG(_(e_invrange));
14758 else
14759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014760 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014761 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014762 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014763 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014764 l->lv_first = item;
14765 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014766 item->li_prev = NULL;
14767 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014768 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014769 }
14770 }
14771 }
14772 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014773 }
14774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775}
14776
14777/*
14778 * "rename({from}, {to})" function
14779 */
14780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014781f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014782 typval_T *argvars;
14783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784{
14785 char_u buf[NUMBUFLEN];
14786
14787 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014790 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14791 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792}
14793
14794/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014795 * "repeat()" function
14796 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014797 static void
14798f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014799 typval_T *argvars;
14800 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014801{
14802 char_u *p;
14803 int n;
14804 int slen;
14805 int len;
14806 char_u *r;
14807 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014808
14809 n = get_tv_number(&argvars[1]);
14810 if (argvars[0].v_type == VAR_LIST)
14811 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014812 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014813 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014814 if (list_extend(rettv->vval.v_list,
14815 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014816 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014817 }
14818 else
14819 {
14820 p = get_tv_string(&argvars[0]);
14821 rettv->v_type = VAR_STRING;
14822 rettv->vval.v_string = NULL;
14823
14824 slen = (int)STRLEN(p);
14825 len = slen * n;
14826 if (len <= 0)
14827 return;
14828
14829 r = alloc(len + 1);
14830 if (r != NULL)
14831 {
14832 for (i = 0; i < n; i++)
14833 mch_memmove(r + i * slen, p, (size_t)slen);
14834 r[len] = NUL;
14835 }
14836
14837 rettv->vval.v_string = r;
14838 }
14839}
14840
14841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014842 * "resolve()" function
14843 */
14844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014845f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014846 typval_T *argvars;
14847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014848{
14849 char_u *p;
14850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014851 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852#ifdef FEAT_SHORTCUT
14853 {
14854 char_u *v = NULL;
14855
14856 v = mch_resolve_shortcut(p);
14857 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014858 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014860 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861 }
14862#else
14863# ifdef HAVE_READLINK
14864 {
14865 char_u buf[MAXPATHL + 1];
14866 char_u *cpy;
14867 int len;
14868 char_u *remain = NULL;
14869 char_u *q;
14870 int is_relative_to_current = FALSE;
14871 int has_trailing_pathsep = FALSE;
14872 int limit = 100;
14873
14874 p = vim_strsave(p);
14875
14876 if (p[0] == '.' && (vim_ispathsep(p[1])
14877 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14878 is_relative_to_current = TRUE;
14879
14880 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014881 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 has_trailing_pathsep = TRUE;
14883
14884 q = getnextcomp(p);
14885 if (*q != NUL)
14886 {
14887 /* Separate the first path component in "p", and keep the
14888 * remainder (beginning with the path separator). */
14889 remain = vim_strsave(q - 1);
14890 q[-1] = NUL;
14891 }
14892
14893 for (;;)
14894 {
14895 for (;;)
14896 {
14897 len = readlink((char *)p, (char *)buf, MAXPATHL);
14898 if (len <= 0)
14899 break;
14900 buf[len] = NUL;
14901
14902 if (limit-- == 0)
14903 {
14904 vim_free(p);
14905 vim_free(remain);
14906 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014907 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 goto fail;
14909 }
14910
14911 /* Ensure that the result will have a trailing path separator
14912 * if the argument has one. */
14913 if (remain == NULL && has_trailing_pathsep)
14914 add_pathsep(buf);
14915
14916 /* Separate the first path component in the link value and
14917 * concatenate the remainders. */
14918 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14919 if (*q != NUL)
14920 {
14921 if (remain == NULL)
14922 remain = vim_strsave(q - 1);
14923 else
14924 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014925 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 if (cpy != NULL)
14927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 vim_free(remain);
14929 remain = cpy;
14930 }
14931 }
14932 q[-1] = NUL;
14933 }
14934
14935 q = gettail(p);
14936 if (q > p && *q == NUL)
14937 {
14938 /* Ignore trailing path separator. */
14939 q[-1] = NUL;
14940 q = gettail(p);
14941 }
14942 if (q > p && !mch_isFullName(buf))
14943 {
14944 /* symlink is relative to directory of argument */
14945 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14946 if (cpy != NULL)
14947 {
14948 STRCPY(cpy, p);
14949 STRCPY(gettail(cpy), buf);
14950 vim_free(p);
14951 p = cpy;
14952 }
14953 }
14954 else
14955 {
14956 vim_free(p);
14957 p = vim_strsave(buf);
14958 }
14959 }
14960
14961 if (remain == NULL)
14962 break;
14963
14964 /* Append the first path component of "remain" to "p". */
14965 q = getnextcomp(remain + 1);
14966 len = q - remain - (*q != NUL);
14967 cpy = vim_strnsave(p, STRLEN(p) + len);
14968 if (cpy != NULL)
14969 {
14970 STRNCAT(cpy, remain, len);
14971 vim_free(p);
14972 p = cpy;
14973 }
14974 /* Shorten "remain". */
14975 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014976 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014977 else
14978 {
14979 vim_free(remain);
14980 remain = NULL;
14981 }
14982 }
14983
14984 /* If the result is a relative path name, make it explicitly relative to
14985 * the current directory if and only if the argument had this form. */
14986 if (!vim_ispathsep(*p))
14987 {
14988 if (is_relative_to_current
14989 && *p != NUL
14990 && !(p[0] == '.'
14991 && (p[1] == NUL
14992 || vim_ispathsep(p[1])
14993 || (p[1] == '.'
14994 && (p[2] == NUL
14995 || vim_ispathsep(p[2]))))))
14996 {
14997 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014998 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999 if (cpy != NULL)
15000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 vim_free(p);
15002 p = cpy;
15003 }
15004 }
15005 else if (!is_relative_to_current)
15006 {
15007 /* Strip leading "./". */
15008 q = p;
15009 while (q[0] == '.' && vim_ispathsep(q[1]))
15010 q += 2;
15011 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015012 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013 }
15014 }
15015
15016 /* Ensure that the result will have no trailing path separator
15017 * if the argument had none. But keep "/" or "//". */
15018 if (!has_trailing_pathsep)
15019 {
15020 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015021 if (after_pathsep(p, q))
15022 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015023 }
15024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015025 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026 }
15027# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029# endif
15030#endif
15031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033
15034#ifdef HAVE_READLINK
15035fail:
15036#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015037 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038}
15039
15040/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015041 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042 */
15043 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015044f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015045 typval_T *argvars;
15046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047{
Bram Moolenaar33570922005-01-25 22:26:29 +000015048 list_T *l;
15049 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015050
Bram Moolenaar0d660222005-01-07 21:51:51 +000015051 if (argvars[0].v_type != VAR_LIST)
15052 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015053 else if ((l = argvars[0].vval.v_list) != NULL
15054 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 {
15056 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015057 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015058 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059 while (li != NULL)
15060 {
15061 ni = li->li_prev;
15062 list_append(l, li);
15063 li = ni;
15064 }
15065 rettv->vval.v_list = l;
15066 rettv->v_type = VAR_LIST;
15067 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015068 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070}
15071
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015072#define SP_NOMOVE 0x01 /* don't move cursor */
15073#define SP_REPEAT 0x02 /* repeat to find outer pair */
15074#define SP_RETCOUNT 0x04 /* return matchcount */
15075#define SP_SETPCMARK 0x08 /* set previous context mark */
15076#define SP_START 0x10 /* accept match at start position */
15077#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15078#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015079
Bram Moolenaar33570922005-01-25 22:26:29 +000015080static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081
15082/*
15083 * Get flags for a search function.
15084 * Possibly sets "p_ws".
15085 * Returns BACKWARD, FORWARD or zero (for an error).
15086 */
15087 static int
15088get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015089 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015090 int *flagsp;
15091{
15092 int dir = FORWARD;
15093 char_u *flags;
15094 char_u nbuf[NUMBUFLEN];
15095 int mask;
15096
15097 if (varp->v_type != VAR_UNKNOWN)
15098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015099 flags = get_tv_string_buf_chk(varp, nbuf);
15100 if (flags == NULL)
15101 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 while (*flags != NUL)
15103 {
15104 switch (*flags)
15105 {
15106 case 'b': dir = BACKWARD; break;
15107 case 'w': p_ws = TRUE; break;
15108 case 'W': p_ws = FALSE; break;
15109 default: mask = 0;
15110 if (flagsp != NULL)
15111 switch (*flags)
15112 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015113 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015114 case 'e': mask = SP_END; break;
15115 case 'm': mask = SP_RETCOUNT; break;
15116 case 'n': mask = SP_NOMOVE; break;
15117 case 'p': mask = SP_SUBPAT; break;
15118 case 'r': mask = SP_REPEAT; break;
15119 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015120 }
15121 if (mask == 0)
15122 {
15123 EMSG2(_(e_invarg2), flags);
15124 dir = 0;
15125 }
15126 else
15127 *flagsp |= mask;
15128 }
15129 if (dir == 0)
15130 break;
15131 ++flags;
15132 }
15133 }
15134 return dir;
15135}
15136
Bram Moolenaar071d4272004-06-13 20:20:40 +000015137/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015138 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015140 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015141search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015142 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015143 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015144 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015146 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 char_u *pat;
15148 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015149 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015150 int save_p_ws = p_ws;
15151 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015152 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015153 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015154 proftime_T tm;
15155#ifdef FEAT_RELTIME
15156 long time_limit = 0;
15157#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015158 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015159 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015161 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015162 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015163 if (dir == 0)
15164 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015165 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015166 if (flags & SP_START)
15167 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015168 if (flags & SP_END)
15169 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015170
Bram Moolenaar76929292008-01-06 19:07:36 +000015171 /* Optional arguments: line number to stop searching and timeout. */
15172 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015173 {
15174 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15175 if (lnum_stop < 0)
15176 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015177#ifdef FEAT_RELTIME
15178 if (argvars[3].v_type != VAR_UNKNOWN)
15179 {
15180 time_limit = get_tv_number_chk(&argvars[3], NULL);
15181 if (time_limit < 0)
15182 goto theend;
15183 }
15184#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015185 }
15186
Bram Moolenaar76929292008-01-06 19:07:36 +000015187#ifdef FEAT_RELTIME
15188 /* Set the time limit, if there is one. */
15189 profile_setlimit(time_limit, &tm);
15190#endif
15191
Bram Moolenaar231334e2005-07-25 20:46:57 +000015192 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015193 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015194 * Check to make sure only those flags are set.
15195 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15196 * flags cannot be set. Check for that condition also.
15197 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015198 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015199 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015200 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015201 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015202 goto theend;
15203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015204
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015205 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015206 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015207 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015208 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015210 if (flags & SP_SUBPAT)
15211 retval = subpatnum;
15212 else
15213 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015214 if (flags & SP_SETPCMARK)
15215 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015216 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015217 if (match_pos != NULL)
15218 {
15219 /* Store the match cursor position */
15220 match_pos->lnum = pos.lnum;
15221 match_pos->col = pos.col + 1;
15222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 /* "/$" will put the cursor after the end of the line, may need to
15224 * correct that here */
15225 check_cursor();
15226 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015227
15228 /* If 'n' flag is used: restore cursor position. */
15229 if (flags & SP_NOMOVE)
15230 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015231 else
15232 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015233theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015235
15236 return retval;
15237}
15238
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015239#ifdef FEAT_FLOAT
15240/*
15241 * "round({float})" function
15242 */
15243 static void
15244f_round(argvars, rettv)
15245 typval_T *argvars;
15246 typval_T *rettv;
15247{
15248 float_T f;
15249
15250 rettv->v_type = VAR_FLOAT;
15251 if (get_float_arg(argvars, &f) == OK)
15252 /* round() is not in C90, use ceil() or floor() instead. */
15253 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15254 else
15255 rettv->vval.v_float = 0.0;
15256}
15257#endif
15258
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015259/*
15260 * "search()" function
15261 */
15262 static void
15263f_search(argvars, rettv)
15264 typval_T *argvars;
15265 typval_T *rettv;
15266{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015267 int flags = 0;
15268
15269 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270}
15271
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015273 * "searchdecl()" function
15274 */
15275 static void
15276f_searchdecl(argvars, rettv)
15277 typval_T *argvars;
15278 typval_T *rettv;
15279{
15280 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015281 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015282 int error = FALSE;
15283 char_u *name;
15284
15285 rettv->vval.v_number = 1; /* default: FAIL */
15286
15287 name = get_tv_string_chk(&argvars[0]);
15288 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015289 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015290 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015291 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15292 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15293 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015294 if (!error && name != NULL)
15295 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015296 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015297}
15298
15299/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015300 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015302 static int
15303searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015304 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015305 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306{
15307 char_u *spat, *mpat, *epat;
15308 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 int dir;
15311 int flags = 0;
15312 char_u nbuf1[NUMBUFLEN];
15313 char_u nbuf2[NUMBUFLEN];
15314 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015315 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015316 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015317 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015318
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015320 spat = get_tv_string_chk(&argvars[0]);
15321 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15322 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15323 if (spat == NULL || mpat == NULL || epat == NULL)
15324 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326 /* Handle the optional fourth argument: flags */
15327 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015328 if (dir == 0)
15329 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015330
15331 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015332 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15333 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015334 if ((flags & (SP_END | SP_SUBPAT)) != 0
15335 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015336 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015337 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015338 goto theend;
15339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015341 /* Using 'r' implies 'W', otherwise it doesn't work. */
15342 if (flags & SP_REPEAT)
15343 p_ws = FALSE;
15344
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015345 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015346 if (argvars[3].v_type == VAR_UNKNOWN
15347 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348 skip = (char_u *)"";
15349 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015350 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015351 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015352 if (argvars[5].v_type != VAR_UNKNOWN)
15353 {
15354 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15355 if (lnum_stop < 0)
15356 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015357#ifdef FEAT_RELTIME
15358 if (argvars[6].v_type != VAR_UNKNOWN)
15359 {
15360 time_limit = get_tv_number_chk(&argvars[6], NULL);
15361 if (time_limit < 0)
15362 goto theend;
15363 }
15364#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015365 }
15366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015367 if (skip == NULL)
15368 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015370 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015371 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015372
15373theend:
15374 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015375
15376 return retval;
15377}
15378
15379/*
15380 * "searchpair()" function
15381 */
15382 static void
15383f_searchpair(argvars, rettv)
15384 typval_T *argvars;
15385 typval_T *rettv;
15386{
15387 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15388}
15389
15390/*
15391 * "searchpairpos()" function
15392 */
15393 static void
15394f_searchpairpos(argvars, rettv)
15395 typval_T *argvars;
15396 typval_T *rettv;
15397{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015398 pos_T match_pos;
15399 int lnum = 0;
15400 int col = 0;
15401
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015402 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015403 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015404
15405 if (searchpair_cmn(argvars, &match_pos) > 0)
15406 {
15407 lnum = match_pos.lnum;
15408 col = match_pos.col;
15409 }
15410
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015411 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15412 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015413}
15414
15415/*
15416 * Search for a start/middle/end thing.
15417 * Used by searchpair(), see its documentation for the details.
15418 * Returns 0 or -1 for no match,
15419 */
15420 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015421do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15422 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015423 char_u *spat; /* start pattern */
15424 char_u *mpat; /* middle pattern */
15425 char_u *epat; /* end pattern */
15426 int dir; /* BACKWARD or FORWARD */
15427 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015428 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015429 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015430 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015431 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015432{
15433 char_u *save_cpo;
15434 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15435 long retval = 0;
15436 pos_T pos;
15437 pos_T firstpos;
15438 pos_T foundpos;
15439 pos_T save_cursor;
15440 pos_T save_pos;
15441 int n;
15442 int r;
15443 int nest = 1;
15444 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015445 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015446 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015447
15448 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15449 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015450 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015451
Bram Moolenaar76929292008-01-06 19:07:36 +000015452#ifdef FEAT_RELTIME
15453 /* Set the time limit, if there is one. */
15454 profile_setlimit(time_limit, &tm);
15455#endif
15456
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015457 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15458 * start/middle/end (pat3, for the top pair). */
15459 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15460 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15461 if (pat2 == NULL || pat3 == NULL)
15462 goto theend;
15463 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15464 if (*mpat == NUL)
15465 STRCPY(pat3, pat2);
15466 else
15467 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15468 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015469 if (flags & SP_START)
15470 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015471
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 save_cursor = curwin->w_cursor;
15473 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015474 clearpos(&firstpos);
15475 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476 pat = pat3;
15477 for (;;)
15478 {
15479 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015480 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15482 /* didn't find it or found the first match again: FAIL */
15483 break;
15484
15485 if (firstpos.lnum == 0)
15486 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015487 if (equalpos(pos, foundpos))
15488 {
15489 /* Found the same position again. Can happen with a pattern that
15490 * has "\zs" at the end and searching backwards. Advance one
15491 * character and try again. */
15492 if (dir == BACKWARD)
15493 decl(&pos);
15494 else
15495 incl(&pos);
15496 }
15497 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015498
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015499 /* clear the start flag to avoid getting stuck here */
15500 options &= ~SEARCH_START;
15501
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 /* If the skip pattern matches, ignore this match. */
15503 if (*skip != NUL)
15504 {
15505 save_pos = curwin->w_cursor;
15506 curwin->w_cursor = pos;
15507 r = eval_to_bool(skip, &err, NULL, FALSE);
15508 curwin->w_cursor = save_pos;
15509 if (err)
15510 {
15511 /* Evaluating {skip} caused an error, break here. */
15512 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015513 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 break;
15515 }
15516 if (r)
15517 continue;
15518 }
15519
15520 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15521 {
15522 /* Found end when searching backwards or start when searching
15523 * forward: nested pair. */
15524 ++nest;
15525 pat = pat2; /* nested, don't search for middle */
15526 }
15527 else
15528 {
15529 /* Found end when searching forward or start when searching
15530 * backward: end of (nested) pair; or found middle in outer pair. */
15531 if (--nest == 1)
15532 pat = pat3; /* outer level, search for middle */
15533 }
15534
15535 if (nest == 0)
15536 {
15537 /* Found the match: return matchcount or line number. */
15538 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015539 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015541 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015542 if (flags & SP_SETPCMARK)
15543 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 curwin->w_cursor = pos;
15545 if (!(flags & SP_REPEAT))
15546 break;
15547 nest = 1; /* search for next unmatched */
15548 }
15549 }
15550
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015551 if (match_pos != NULL)
15552 {
15553 /* Store the match cursor position */
15554 match_pos->lnum = curwin->w_cursor.lnum;
15555 match_pos->col = curwin->w_cursor.col + 1;
15556 }
15557
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015559 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 curwin->w_cursor = save_cursor;
15561
15562theend:
15563 vim_free(pat2);
15564 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015565 if (p_cpo == empty_option)
15566 p_cpo = save_cpo;
15567 else
15568 /* Darn, evaluating the {skip} expression changed the value. */
15569 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015570
15571 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572}
15573
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015574/*
15575 * "searchpos()" function
15576 */
15577 static void
15578f_searchpos(argvars, rettv)
15579 typval_T *argvars;
15580 typval_T *rettv;
15581{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015582 pos_T match_pos;
15583 int lnum = 0;
15584 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015585 int n;
15586 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015587
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015588 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015589 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015590
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015591 n = search_cmn(argvars, &match_pos, &flags);
15592 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015593 {
15594 lnum = match_pos.lnum;
15595 col = match_pos.col;
15596 }
15597
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015598 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15599 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015600 if (flags & SP_SUBPAT)
15601 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015602}
15603
15604
Bram Moolenaar0d660222005-01-07 21:51:51 +000015605 static void
15606f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015607 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015608 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015610#ifdef FEAT_CLIENTSERVER
15611 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015612 char_u *server = get_tv_string_chk(&argvars[0]);
15613 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614
Bram Moolenaar0d660222005-01-07 21:51:51 +000015615 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015616 if (server == NULL || reply == NULL)
15617 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015618 if (check_restricted() || check_secure())
15619 return;
15620# ifdef FEAT_X11
15621 if (check_connection() == FAIL)
15622 return;
15623# endif
15624
15625 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015626 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015627 EMSG(_("E258: Unable to send to client"));
15628 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015630 rettv->vval.v_number = 0;
15631#else
15632 rettv->vval.v_number = -1;
15633#endif
15634}
15635
Bram Moolenaar0d660222005-01-07 21:51:51 +000015636 static void
15637f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015638 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640{
15641 char_u *r = NULL;
15642
15643#ifdef FEAT_CLIENTSERVER
15644# ifdef WIN32
15645 r = serverGetVimNames();
15646# else
15647 make_connection();
15648 if (X_DISPLAY != NULL)
15649 r = serverGetVimNames(X_DISPLAY);
15650# endif
15651#endif
15652 rettv->v_type = VAR_STRING;
15653 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654}
15655
15656/*
15657 * "setbufvar()" function
15658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015660f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015661 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015662 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663{
15664 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015667 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 char_u nbuf[NUMBUFLEN];
15669
15670 if (check_restricted() || check_secure())
15671 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015672 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15673 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015674 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675 varp = &argvars[2];
15676
15677 if (buf != NULL && varname != NULL && varp != NULL)
15678 {
15679 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681
15682 if (*varname == '&')
15683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015684 long numval;
15685 char_u *strval;
15686 int error = FALSE;
15687
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015689 numval = get_tv_number_chk(varp, &error);
15690 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015691 if (!error && strval != NULL)
15692 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693 }
15694 else
15695 {
15696 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15697 if (bufvarname != NULL)
15698 {
15699 STRCPY(bufvarname, "b:");
15700 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015701 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 vim_free(bufvarname);
15703 }
15704 }
15705
15706 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709}
15710
15711/*
15712 * "setcmdpos()" function
15713 */
15714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015715f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015716 typval_T *argvars;
15717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015719 int pos = (int)get_tv_number(&argvars[0]) - 1;
15720
15721 if (pos >= 0)
15722 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723}
15724
15725/*
15726 * "setline()" function
15727 */
15728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015729f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015730 typval_T *argvars;
15731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732{
15733 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015734 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015735 list_T *l = NULL;
15736 listitem_T *li = NULL;
15737 long added = 0;
15738 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015740 lnum = get_tv_lnum(&argvars[0]);
15741 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015743 l = argvars[1].vval.v_list;
15744 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015746 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015747 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015748
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015749 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015750 for (;;)
15751 {
15752 if (l != NULL)
15753 {
15754 /* list argument, get next string */
15755 if (li == NULL)
15756 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015757 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015758 li = li->li_next;
15759 }
15760
15761 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015762 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015763 break;
15764 if (lnum <= curbuf->b_ml.ml_line_count)
15765 {
15766 /* existing line, replace it */
15767 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15768 {
15769 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015770 if (lnum == curwin->w_cursor.lnum)
15771 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015772 rettv->vval.v_number = 0; /* OK */
15773 }
15774 }
15775 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15776 {
15777 /* lnum is one past the last line, append the line */
15778 ++added;
15779 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15780 rettv->vval.v_number = 0; /* OK */
15781 }
15782
15783 if (l == NULL) /* only one string argument */
15784 break;
15785 ++lnum;
15786 }
15787
15788 if (added > 0)
15789 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790}
15791
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015792static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15793
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015795 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015796 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015797 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015798set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015799 win_T *wp UNUSED;
15800 typval_T *list_arg UNUSED;
15801 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015802 typval_T *rettv;
15803{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015804#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015805 char_u *act;
15806 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015807#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015808
Bram Moolenaar2641f772005-03-25 21:58:17 +000015809 rettv->vval.v_number = -1;
15810
15811#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015812 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015813 EMSG(_(e_listreq));
15814 else
15815 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015816 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015817
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015818 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015819 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015820 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015821 if (act == NULL)
15822 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015823 if (*act == 'a' || *act == 'r')
15824 action = *act;
15825 }
15826
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015827 if (l != NULL && set_errorlist(wp, l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015828 rettv->vval.v_number = 0;
15829 }
15830#endif
15831}
15832
15833/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015834 * "setloclist()" function
15835 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015836 static void
15837f_setloclist(argvars, rettv)
15838 typval_T *argvars;
15839 typval_T *rettv;
15840{
15841 win_T *win;
15842
15843 rettv->vval.v_number = -1;
15844
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015845 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015846 if (win != NULL)
15847 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15848}
15849
15850/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015851 * "setmatches()" function
15852 */
15853 static void
15854f_setmatches(argvars, rettv)
15855 typval_T *argvars;
15856 typval_T *rettv;
15857{
15858#ifdef FEAT_SEARCH_EXTRA
15859 list_T *l;
15860 listitem_T *li;
15861 dict_T *d;
15862
15863 rettv->vval.v_number = -1;
15864 if (argvars[0].v_type != VAR_LIST)
15865 {
15866 EMSG(_(e_listreq));
15867 return;
15868 }
15869 if ((l = argvars[0].vval.v_list) != NULL)
15870 {
15871
15872 /* To some extent make sure that we are dealing with a list from
15873 * "getmatches()". */
15874 li = l->lv_first;
15875 while (li != NULL)
15876 {
15877 if (li->li_tv.v_type != VAR_DICT
15878 || (d = li->li_tv.vval.v_dict) == NULL)
15879 {
15880 EMSG(_(e_invarg));
15881 return;
15882 }
15883 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15884 && dict_find(d, (char_u *)"pattern", -1) != NULL
15885 && dict_find(d, (char_u *)"priority", -1) != NULL
15886 && dict_find(d, (char_u *)"id", -1) != NULL))
15887 {
15888 EMSG(_(e_invarg));
15889 return;
15890 }
15891 li = li->li_next;
15892 }
15893
15894 clear_matches(curwin);
15895 li = l->lv_first;
15896 while (li != NULL)
15897 {
15898 d = li->li_tv.vval.v_dict;
15899 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15900 get_dict_string(d, (char_u *)"pattern", FALSE),
15901 (int)get_dict_number(d, (char_u *)"priority"),
15902 (int)get_dict_number(d, (char_u *)"id"));
15903 li = li->li_next;
15904 }
15905 rettv->vval.v_number = 0;
15906 }
15907#endif
15908}
15909
15910/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015911 * "setpos()" function
15912 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015913 static void
15914f_setpos(argvars, rettv)
15915 typval_T *argvars;
15916 typval_T *rettv;
15917{
15918 pos_T pos;
15919 int fnum;
15920 char_u *name;
15921
Bram Moolenaar08250432008-02-13 11:42:46 +000015922 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015923 name = get_tv_string_chk(argvars);
15924 if (name != NULL)
15925 {
15926 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15927 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015928 if (--pos.col < 0)
15929 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015930 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015931 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015932 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015933 if (fnum == curbuf->b_fnum)
15934 {
15935 curwin->w_cursor = pos;
15936 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015937 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015938 }
15939 else
15940 EMSG(_(e_invarg));
15941 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015942 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15943 {
15944 /* set mark */
15945 if (setmark_pos(name[1], &pos, fnum) == OK)
15946 rettv->vval.v_number = 0;
15947 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015948 else
15949 EMSG(_(e_invarg));
15950 }
15951 }
15952}
15953
15954/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015955 * "setqflist()" function
15956 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015957 static void
15958f_setqflist(argvars, rettv)
15959 typval_T *argvars;
15960 typval_T *rettv;
15961{
15962 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15963}
15964
15965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015966 * "setreg()" function
15967 */
15968 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015969f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015970 typval_T *argvars;
15971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972{
15973 int regname;
15974 char_u *strregname;
15975 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015976 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977 int append;
15978 char_u yank_type;
15979 long block_len;
15980
15981 block_len = -1;
15982 yank_type = MAUTO;
15983 append = FALSE;
15984
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015985 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015986 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015988 if (strregname == NULL)
15989 return; /* type error; errmsg already given */
15990 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 if (regname == 0 || regname == '@')
15992 regname = '"';
15993 else if (regname == '=')
15994 return;
15995
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015996 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015998 stropt = get_tv_string_chk(&argvars[2]);
15999 if (stropt == NULL)
16000 return; /* type error */
16001 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016002 switch (*stropt)
16003 {
16004 case 'a': case 'A': /* append */
16005 append = TRUE;
16006 break;
16007 case 'v': case 'c': /* character-wise selection */
16008 yank_type = MCHAR;
16009 break;
16010 case 'V': case 'l': /* line-wise selection */
16011 yank_type = MLINE;
16012 break;
16013#ifdef FEAT_VISUAL
16014 case 'b': case Ctrl_V: /* block-wise selection */
16015 yank_type = MBLOCK;
16016 if (VIM_ISDIGIT(stropt[1]))
16017 {
16018 ++stropt;
16019 block_len = getdigits(&stropt) - 1;
16020 --stropt;
16021 }
16022 break;
16023#endif
16024 }
16025 }
16026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016027 strval = get_tv_string_chk(&argvars[1]);
16028 if (strval != NULL)
16029 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016031 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032}
16033
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016034/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016035 * "settabvar()" function
16036 */
16037 static void
16038f_settabvar(argvars, rettv)
16039 typval_T *argvars;
16040 typval_T *rettv;
16041{
16042 tabpage_T *save_curtab;
16043 char_u *varname, *tabvarname;
16044 typval_T *varp;
16045 tabpage_T *tp;
16046
16047 rettv->vval.v_number = 0;
16048
16049 if (check_restricted() || check_secure())
16050 return;
16051
16052 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16053 varname = get_tv_string_chk(&argvars[1]);
16054 varp = &argvars[2];
16055
16056 if (tp != NULL && varname != NULL && varp != NULL)
16057 {
16058 save_curtab = curtab;
16059 goto_tabpage_tp(tp);
16060
16061 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16062 if (tabvarname != NULL)
16063 {
16064 STRCPY(tabvarname, "t:");
16065 STRCPY(tabvarname + 2, varname);
16066 set_var(tabvarname, varp, TRUE);
16067 vim_free(tabvarname);
16068 }
16069
16070 /* Restore current tabpage */
16071 if (valid_tabpage(save_curtab))
16072 goto_tabpage_tp(save_curtab);
16073 }
16074}
16075
16076/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016077 * "settabwinvar()" function
16078 */
16079 static void
16080f_settabwinvar(argvars, rettv)
16081 typval_T *argvars;
16082 typval_T *rettv;
16083{
16084 setwinvar(argvars, rettv, 1);
16085}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086
16087/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016088 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016091f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016092 typval_T *argvars;
16093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016094{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016095 setwinvar(argvars, rettv, 0);
16096}
16097
16098/*
16099 * "setwinvar()" and "settabwinvar()" functions
16100 */
16101 static void
16102setwinvar(argvars, rettv, off)
16103 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016104 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016105 int off;
16106{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 win_T *win;
16108#ifdef FEAT_WINDOWS
16109 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016110 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016111#endif
16112 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016113 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016114 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016115 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116
16117 if (check_restricted() || check_secure())
16118 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016119
16120#ifdef FEAT_WINDOWS
16121 if (off == 1)
16122 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16123 else
16124 tp = curtab;
16125#endif
16126 win = find_win_by_nr(&argvars[off], tp);
16127 varname = get_tv_string_chk(&argvars[off + 1]);
16128 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129
16130 if (win != NULL && varname != NULL && varp != NULL)
16131 {
16132#ifdef FEAT_WINDOWS
16133 /* set curwin to be our win, temporarily */
16134 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016135 save_curtab = curtab;
16136 goto_tabpage_tp(tp);
16137 if (!win_valid(win))
16138 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 curwin = win;
16140 curbuf = curwin->w_buffer;
16141#endif
16142
16143 if (*varname == '&')
16144 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016145 long numval;
16146 char_u *strval;
16147 int error = FALSE;
16148
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016150 numval = get_tv_number_chk(varp, &error);
16151 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016152 if (!error && strval != NULL)
16153 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 }
16155 else
16156 {
16157 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16158 if (winvarname != NULL)
16159 {
16160 STRCPY(winvarname, "w:");
16161 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016162 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016163 vim_free(winvarname);
16164 }
16165 }
16166
16167#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016168 /* Restore current tabpage and window, if still valid (autocomands can
16169 * make them invalid). */
16170 if (valid_tabpage(save_curtab))
16171 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172 if (win_valid(save_curwin))
16173 {
16174 curwin = save_curwin;
16175 curbuf = curwin->w_buffer;
16176 }
16177#endif
16178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179}
16180
16181/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016182 * "shellescape({string})" function
16183 */
16184 static void
16185f_shellescape(argvars, rettv)
16186 typval_T *argvars;
16187 typval_T *rettv;
16188{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016189 rettv->vval.v_string = vim_strsave_shellescape(
16190 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016191 rettv->v_type = VAR_STRING;
16192}
16193
16194/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016195 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 */
16197 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016198f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016199 typval_T *argvars;
16200 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016202 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203
Bram Moolenaar0d660222005-01-07 21:51:51 +000016204 p = get_tv_string(&argvars[0]);
16205 rettv->vval.v_string = vim_strsave(p);
16206 simplify_filename(rettv->vval.v_string); /* simplify in place */
16207 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208}
16209
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016210#ifdef FEAT_FLOAT
16211/*
16212 * "sin()" function
16213 */
16214 static void
16215f_sin(argvars, rettv)
16216 typval_T *argvars;
16217 typval_T *rettv;
16218{
16219 float_T f;
16220
16221 rettv->v_type = VAR_FLOAT;
16222 if (get_float_arg(argvars, &f) == OK)
16223 rettv->vval.v_float = sin(f);
16224 else
16225 rettv->vval.v_float = 0.0;
16226}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016227
16228/*
16229 * "sinh()" function
16230 */
16231 static void
16232f_sinh(argvars, rettv)
16233 typval_T *argvars;
16234 typval_T *rettv;
16235{
16236 float_T f;
16237
16238 rettv->v_type = VAR_FLOAT;
16239 if (get_float_arg(argvars, &f) == OK)
16240 rettv->vval.v_float = sinh(f);
16241 else
16242 rettv->vval.v_float = 0.0;
16243}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016244#endif
16245
Bram Moolenaar0d660222005-01-07 21:51:51 +000016246static int
16247#ifdef __BORLANDC__
16248 _RTLENTRYF
16249#endif
16250 item_compare __ARGS((const void *s1, const void *s2));
16251static int
16252#ifdef __BORLANDC__
16253 _RTLENTRYF
16254#endif
16255 item_compare2 __ARGS((const void *s1, const void *s2));
16256
16257static int item_compare_ic;
16258static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016259static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260#define ITEM_COMPARE_FAIL 999
16261
Bram Moolenaar071d4272004-06-13 20:20:40 +000016262/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016263 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265 static int
16266#ifdef __BORLANDC__
16267_RTLENTRYF
16268#endif
16269item_compare(s1, s2)
16270 const void *s1;
16271 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016273 char_u *p1, *p2;
16274 char_u *tofree1, *tofree2;
16275 int res;
16276 char_u numbuf1[NUMBUFLEN];
16277 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016279 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16280 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016281 if (p1 == NULL)
16282 p1 = (char_u *)"";
16283 if (p2 == NULL)
16284 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016285 if (item_compare_ic)
16286 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016288 res = STRCMP(p1, p2);
16289 vim_free(tofree1);
16290 vim_free(tofree2);
16291 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292}
16293
16294 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295#ifdef __BORLANDC__
16296_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016297#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016298item_compare2(s1, s2)
16299 const void *s1;
16300 const void *s2;
16301{
16302 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016303 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016304 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016305 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016307 /* shortcut after failure in previous call; compare all items equal */
16308 if (item_compare_func_err)
16309 return 0;
16310
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016311 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16312 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016313 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16314 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016315
16316 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016317 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016318 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016319 clear_tv(&argv[0]);
16320 clear_tv(&argv[1]);
16321
16322 if (res == FAIL)
16323 res = ITEM_COMPARE_FAIL;
16324 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016325 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16326 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016327 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328 clear_tv(&rettv);
16329 return res;
16330}
16331
16332/*
16333 * "sort({list})" function
16334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016336f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016337 typval_T *argvars;
16338 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339{
Bram Moolenaar33570922005-01-25 22:26:29 +000016340 list_T *l;
16341 listitem_T *li;
16342 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016343 long len;
16344 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345
Bram Moolenaar0d660222005-01-07 21:51:51 +000016346 if (argvars[0].v_type != VAR_LIST)
16347 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 else
16349 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016350 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016351 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016352 return;
16353 rettv->vval.v_list = l;
16354 rettv->v_type = VAR_LIST;
16355 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016356
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357 len = list_len(l);
16358 if (len <= 1)
16359 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360
Bram Moolenaar0d660222005-01-07 21:51:51 +000016361 item_compare_ic = FALSE;
16362 item_compare_func = NULL;
16363 if (argvars[1].v_type != VAR_UNKNOWN)
16364 {
16365 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016366 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016367 else
16368 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016369 int error = FALSE;
16370
16371 i = get_tv_number_chk(&argvars[1], &error);
16372 if (error)
16373 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016374 if (i == 1)
16375 item_compare_ic = TRUE;
16376 else
16377 item_compare_func = get_tv_string(&argvars[1]);
16378 }
16379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380
Bram Moolenaar0d660222005-01-07 21:51:51 +000016381 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016382 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383 if (ptrs == NULL)
16384 return;
16385 i = 0;
16386 for (li = l->lv_first; li != NULL; li = li->li_next)
16387 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016389 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390 /* test the compare function */
16391 if (item_compare_func != NULL
16392 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16393 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016394 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016396 {
16397 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016398 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016399 item_compare_func == NULL ? item_compare : item_compare2);
16400
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016401 if (!item_compare_func_err)
16402 {
16403 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016404 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016405 l->lv_len = 0;
16406 for (i = 0; i < len; ++i)
16407 list_append(l, ptrs[i]);
16408 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016409 }
16410
16411 vim_free(ptrs);
16412 }
16413}
16414
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016415/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016416 * "soundfold({word})" function
16417 */
16418 static void
16419f_soundfold(argvars, rettv)
16420 typval_T *argvars;
16421 typval_T *rettv;
16422{
16423 char_u *s;
16424
16425 rettv->v_type = VAR_STRING;
16426 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016427#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016428 rettv->vval.v_string = eval_soundfold(s);
16429#else
16430 rettv->vval.v_string = vim_strsave(s);
16431#endif
16432}
16433
16434/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016435 * "spellbadword()" function
16436 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016437 static void
16438f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016439 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016440 typval_T *rettv;
16441{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016442 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016443 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016444 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016445
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016446 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016447 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016448
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016449#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016450 if (argvars[0].v_type == VAR_UNKNOWN)
16451 {
16452 /* Find the start and length of the badly spelled word. */
16453 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16454 if (len != 0)
16455 word = ml_get_cursor();
16456 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016457 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016458 {
16459 char_u *str = get_tv_string_chk(&argvars[0]);
16460 int capcol = -1;
16461
16462 if (str != NULL)
16463 {
16464 /* Check the argument for spelling. */
16465 while (*str != NUL)
16466 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016467 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016468 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016469 {
16470 word = str;
16471 break;
16472 }
16473 str += len;
16474 }
16475 }
16476 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016477#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016478
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016479 list_append_string(rettv->vval.v_list, word, len);
16480 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016481 attr == HLF_SPB ? "bad" :
16482 attr == HLF_SPR ? "rare" :
16483 attr == HLF_SPL ? "local" :
16484 attr == HLF_SPC ? "caps" :
16485 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016486}
16487
16488/*
16489 * "spellsuggest()" function
16490 */
16491 static void
16492f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016493 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016494 typval_T *rettv;
16495{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016496#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016497 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016498 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016499 int maxcount;
16500 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016501 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016502 listitem_T *li;
16503 int need_capital = FALSE;
16504#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016505
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016506 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016507 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016508
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016509#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016510 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016511 {
16512 str = get_tv_string(&argvars[0]);
16513 if (argvars[1].v_type != VAR_UNKNOWN)
16514 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016515 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016516 if (maxcount <= 0)
16517 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016518 if (argvars[2].v_type != VAR_UNKNOWN)
16519 {
16520 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16521 if (typeerr)
16522 return;
16523 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016524 }
16525 else
16526 maxcount = 25;
16527
Bram Moolenaar4770d092006-01-12 23:22:24 +000016528 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016529
16530 for (i = 0; i < ga.ga_len; ++i)
16531 {
16532 str = ((char_u **)ga.ga_data)[i];
16533
16534 li = listitem_alloc();
16535 if (li == NULL)
16536 vim_free(str);
16537 else
16538 {
16539 li->li_tv.v_type = VAR_STRING;
16540 li->li_tv.v_lock = 0;
16541 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016542 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016543 }
16544 }
16545 ga_clear(&ga);
16546 }
16547#endif
16548}
16549
Bram Moolenaar0d660222005-01-07 21:51:51 +000016550 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016551f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016552 typval_T *argvars;
16553 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016554{
16555 char_u *str;
16556 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016557 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016558 regmatch_T regmatch;
16559 char_u patbuf[NUMBUFLEN];
16560 char_u *save_cpo;
16561 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016562 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016563 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016564 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016565
16566 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16567 save_cpo = p_cpo;
16568 p_cpo = (char_u *)"";
16569
16570 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016571 if (argvars[1].v_type != VAR_UNKNOWN)
16572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016573 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16574 if (pat == NULL)
16575 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016576 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016577 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016578 }
16579 if (pat == NULL || *pat == NUL)
16580 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016581
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016582 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016583 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016584 if (typeerr)
16585 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016586
Bram Moolenaar0d660222005-01-07 21:51:51 +000016587 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16588 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016589 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016591 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016592 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016593 if (*str == NUL)
16594 match = FALSE; /* empty item at the end */
16595 else
16596 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016597 if (match)
16598 end = regmatch.startp[0];
16599 else
16600 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016601 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16602 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016603 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016604 if (list_append_string(rettv->vval.v_list, str,
16605 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016606 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016607 }
16608 if (!match)
16609 break;
16610 /* Advance to just after the match. */
16611 if (regmatch.endp[0] > str)
16612 col = 0;
16613 else
16614 {
16615 /* Don't get stuck at the same match. */
16616#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016617 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016618#else
16619 col = 1;
16620#endif
16621 }
16622 str = regmatch.endp[0];
16623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624
Bram Moolenaar0d660222005-01-07 21:51:51 +000016625 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627
Bram Moolenaar0d660222005-01-07 21:51:51 +000016628 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629}
16630
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016631#ifdef FEAT_FLOAT
16632/*
16633 * "sqrt()" function
16634 */
16635 static void
16636f_sqrt(argvars, rettv)
16637 typval_T *argvars;
16638 typval_T *rettv;
16639{
16640 float_T f;
16641
16642 rettv->v_type = VAR_FLOAT;
16643 if (get_float_arg(argvars, &f) == OK)
16644 rettv->vval.v_float = sqrt(f);
16645 else
16646 rettv->vval.v_float = 0.0;
16647}
16648
16649/*
16650 * "str2float()" function
16651 */
16652 static void
16653f_str2float(argvars, rettv)
16654 typval_T *argvars;
16655 typval_T *rettv;
16656{
16657 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16658
16659 if (*p == '+')
16660 p = skipwhite(p + 1);
16661 (void)string2float(p, &rettv->vval.v_float);
16662 rettv->v_type = VAR_FLOAT;
16663}
16664#endif
16665
Bram Moolenaar2c932302006-03-18 21:42:09 +000016666/*
16667 * "str2nr()" function
16668 */
16669 static void
16670f_str2nr(argvars, rettv)
16671 typval_T *argvars;
16672 typval_T *rettv;
16673{
16674 int base = 10;
16675 char_u *p;
16676 long n;
16677
16678 if (argvars[1].v_type != VAR_UNKNOWN)
16679 {
16680 base = get_tv_number(&argvars[1]);
16681 if (base != 8 && base != 10 && base != 16)
16682 {
16683 EMSG(_(e_invarg));
16684 return;
16685 }
16686 }
16687
16688 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016689 if (*p == '+')
16690 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016691 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16692 rettv->vval.v_number = n;
16693}
16694
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695#ifdef HAVE_STRFTIME
16696/*
16697 * "strftime({format}[, {time}])" function
16698 */
16699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016700f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016701 typval_T *argvars;
16702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016703{
16704 char_u result_buf[256];
16705 struct tm *curtime;
16706 time_t seconds;
16707 char_u *p;
16708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016709 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016711 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016712 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 seconds = time(NULL);
16714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016715 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716 curtime = localtime(&seconds);
16717 /* MSVC returns NULL for an invalid value of seconds. */
16718 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016719 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 else
16721 {
16722# ifdef FEAT_MBYTE
16723 vimconv_T conv;
16724 char_u *enc;
16725
16726 conv.vc_type = CONV_NONE;
16727 enc = enc_locale();
16728 convert_setup(&conv, p_enc, enc);
16729 if (conv.vc_type != CONV_NONE)
16730 p = string_convert(&conv, p, NULL);
16731# endif
16732 if (p != NULL)
16733 (void)strftime((char *)result_buf, sizeof(result_buf),
16734 (char *)p, curtime);
16735 else
16736 result_buf[0] = NUL;
16737
16738# ifdef FEAT_MBYTE
16739 if (conv.vc_type != CONV_NONE)
16740 vim_free(p);
16741 convert_setup(&conv, enc, p_enc);
16742 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016744 else
16745# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016746 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747
16748# ifdef FEAT_MBYTE
16749 /* Release conversion descriptors */
16750 convert_setup(&conv, NULL, NULL);
16751 vim_free(enc);
16752# endif
16753 }
16754}
16755#endif
16756
16757/*
16758 * "stridx()" function
16759 */
16760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016761f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016762 typval_T *argvars;
16763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764{
16765 char_u buf[NUMBUFLEN];
16766 char_u *needle;
16767 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016768 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016769 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016770 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016772 needle = get_tv_string_chk(&argvars[1]);
16773 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016774 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016775 if (needle == NULL || haystack == NULL)
16776 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016777
Bram Moolenaar33570922005-01-25 22:26:29 +000016778 if (argvars[2].v_type != VAR_UNKNOWN)
16779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016780 int error = FALSE;
16781
16782 start_idx = get_tv_number_chk(&argvars[2], &error);
16783 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016784 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016785 if (start_idx >= 0)
16786 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016787 }
16788
16789 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16790 if (pos != NULL)
16791 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792}
16793
16794/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016795 * "string()" function
16796 */
16797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016798f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016799 typval_T *argvars;
16800 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016801{
16802 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016803 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016805 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016806 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016807 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016808 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016809 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810}
16811
16812/*
16813 * "strlen()" function
16814 */
16815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016816f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016817 typval_T *argvars;
16818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016820 rettv->vval.v_number = (varnumber_T)(STRLEN(
16821 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822}
16823
16824/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016825 * "strchars()" function
16826 */
16827 static void
16828f_strchars(argvars, rettv)
16829 typval_T *argvars;
16830 typval_T *rettv;
16831{
16832 char_u *s = get_tv_string(&argvars[0]);
16833#ifdef FEAT_MBYTE
16834 varnumber_T len = 0;
16835
16836 while (*s != NUL)
16837 {
16838 mb_cptr2char_adv(&s);
16839 ++len;
16840 }
16841 rettv->vval.v_number = len;
16842#else
16843 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16844#endif
16845}
16846
16847/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016848 * "strdisplaywidth()" function
16849 */
16850 static void
16851f_strdisplaywidth(argvars, rettv)
16852 typval_T *argvars;
16853 typval_T *rettv;
16854{
16855 char_u *s = get_tv_string(&argvars[0]);
16856 int col = 0;
16857
16858 if (argvars[1].v_type != VAR_UNKNOWN)
16859 col = get_tv_number(&argvars[1]);
16860
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016861 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016862}
16863
16864/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016865 * "strwidth()" function
16866 */
16867 static void
16868f_strwidth(argvars, rettv)
16869 typval_T *argvars;
16870 typval_T *rettv;
16871{
16872 char_u *s = get_tv_string(&argvars[0]);
16873
16874 rettv->vval.v_number = (varnumber_T)(
16875#ifdef FEAT_MBYTE
16876 mb_string2cells(s, -1)
16877#else
16878 STRLEN(s)
16879#endif
16880 );
16881}
16882
16883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884 * "strpart()" function
16885 */
16886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016887f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016888 typval_T *argvars;
16889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016890{
16891 char_u *p;
16892 int n;
16893 int len;
16894 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016895 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016897 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 slen = (int)STRLEN(p);
16899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016900 n = get_tv_number_chk(&argvars[1], &error);
16901 if (error)
16902 len = 0;
16903 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016904 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016905 else
16906 len = slen - n; /* default len: all bytes that are available. */
16907
16908 /*
16909 * Only return the overlap between the specified part and the actual
16910 * string.
16911 */
16912 if (n < 0)
16913 {
16914 len += n;
16915 n = 0;
16916 }
16917 else if (n > slen)
16918 n = slen;
16919 if (len < 0)
16920 len = 0;
16921 else if (n + len > slen)
16922 len = slen - n;
16923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016924 rettv->v_type = VAR_STRING;
16925 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016926}
16927
16928/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016929 * "strridx()" function
16930 */
16931 static void
16932f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016933 typval_T *argvars;
16934 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016935{
16936 char_u buf[NUMBUFLEN];
16937 char_u *needle;
16938 char_u *haystack;
16939 char_u *rest;
16940 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016941 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016943 needle = get_tv_string_chk(&argvars[1]);
16944 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016945
16946 rettv->vval.v_number = -1;
16947 if (needle == NULL || haystack == NULL)
16948 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016949
16950 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016951 if (argvars[2].v_type != VAR_UNKNOWN)
16952 {
16953 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016954 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016955 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016956 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016957 }
16958 else
16959 end_idx = haystack_len;
16960
Bram Moolenaar0d660222005-01-07 21:51:51 +000016961 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016962 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016964 lastmatch = haystack + end_idx;
16965 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016967 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968 for (rest = haystack; *rest != '\0'; ++rest)
16969 {
16970 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016971 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972 break;
16973 lastmatch = rest;
16974 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016975 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976
16977 if (lastmatch == NULL)
16978 rettv->vval.v_number = -1;
16979 else
16980 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16981}
16982
16983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984 * "strtrans()" function
16985 */
16986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016987f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016988 typval_T *argvars;
16989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016991 rettv->v_type = VAR_STRING;
16992 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993}
16994
16995/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996 * "submatch()" function
16997 */
16998 static void
16999f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017000 typval_T *argvars;
17001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002{
17003 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017004 rettv->vval.v_string =
17005 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006}
17007
17008/*
17009 * "substitute()" function
17010 */
17011 static void
17012f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017013 typval_T *argvars;
17014 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015{
17016 char_u patbuf[NUMBUFLEN];
17017 char_u subbuf[NUMBUFLEN];
17018 char_u flagsbuf[NUMBUFLEN];
17019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017020 char_u *str = get_tv_string_chk(&argvars[0]);
17021 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17022 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17023 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17024
Bram Moolenaar0d660222005-01-07 21:51:51 +000017025 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017026 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17027 rettv->vval.v_string = NULL;
17028 else
17029 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030}
17031
17032/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017033 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017036f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017037 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017038 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039{
17040 int id = 0;
17041#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017042 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043 long col;
17044 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017045 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017047 lnum = get_tv_lnum(argvars); /* -1 on type error */
17048 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17049 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017052 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017053 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054#endif
17055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017056 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057}
17058
17059/*
17060 * "synIDattr(id, what [, mode])" function
17061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017063f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017064 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017065 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066{
17067 char_u *p = NULL;
17068#ifdef FEAT_SYN_HL
17069 int id;
17070 char_u *what;
17071 char_u *mode;
17072 char_u modebuf[NUMBUFLEN];
17073 int modec;
17074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075 id = get_tv_number(&argvars[0]);
17076 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017077 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017079 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017081 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082 modec = 0; /* replace invalid with current */
17083 }
17084 else
17085 {
17086#ifdef FEAT_GUI
17087 if (gui.in_use)
17088 modec = 'g';
17089 else
17090#endif
17091 if (t_colors > 1)
17092 modec = 'c';
17093 else
17094 modec = 't';
17095 }
17096
17097
17098 switch (TOLOWER_ASC(what[0]))
17099 {
17100 case 'b':
17101 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17102 p = highlight_color(id, what, modec);
17103 else /* bold */
17104 p = highlight_has_attr(id, HL_BOLD, modec);
17105 break;
17106
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017107 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017108 p = highlight_color(id, what, modec);
17109 break;
17110
17111 case 'i':
17112 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17113 p = highlight_has_attr(id, HL_INVERSE, modec);
17114 else /* italic */
17115 p = highlight_has_attr(id, HL_ITALIC, modec);
17116 break;
17117
17118 case 'n': /* name */
17119 p = get_highlight_name(NULL, id - 1);
17120 break;
17121
17122 case 'r': /* reverse */
17123 p = highlight_has_attr(id, HL_INVERSE, modec);
17124 break;
17125
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017126 case 's':
17127 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17128 p = highlight_color(id, what, modec);
17129 else /* standout */
17130 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131 break;
17132
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017133 case 'u':
17134 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17135 /* underline */
17136 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17137 else
17138 /* undercurl */
17139 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 break;
17141 }
17142
17143 if (p != NULL)
17144 p = vim_strsave(p);
17145#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017146 rettv->v_type = VAR_STRING;
17147 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148}
17149
17150/*
17151 * "synIDtrans(id)" function
17152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017154f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017155 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157{
17158 int id;
17159
17160#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017161 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017162
17163 if (id > 0)
17164 id = syn_get_final_id(id);
17165 else
17166#endif
17167 id = 0;
17168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017169 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170}
17171
17172/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017173 * "synconcealed(lnum, col)" function
17174 */
17175 static void
17176f_synconcealed(argvars, rettv)
17177 typval_T *argvars UNUSED;
17178 typval_T *rettv;
17179{
17180#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17181 long lnum;
17182 long col;
17183 int syntax_flags = 0;
17184 int cchar;
17185 int matchid = 0;
17186 char_u str[NUMBUFLEN];
17187#endif
17188
17189 rettv->v_type = VAR_LIST;
17190 rettv->vval.v_list = NULL;
17191
17192#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17193 lnum = get_tv_lnum(argvars); /* -1 on type error */
17194 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17195
17196 vim_memset(str, NUL, sizeof(str));
17197
17198 if (rettv_list_alloc(rettv) != FAIL)
17199 {
17200 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17201 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17202 && curwin->w_p_cole > 0)
17203 {
17204 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17205 syntax_flags = get_syntax_info(&matchid);
17206
17207 /* get the conceal character */
17208 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17209 {
17210 cchar = syn_get_sub_char();
17211 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17212 cchar = lcs_conceal;
17213 if (cchar != NUL)
17214 {
17215# ifdef FEAT_MBYTE
17216 if (has_mbyte)
17217 (*mb_char2bytes)(cchar, str);
17218 else
17219# endif
17220 str[0] = cchar;
17221 }
17222 }
17223 }
17224
17225 list_append_number(rettv->vval.v_list,
17226 (syntax_flags & HL_CONCEAL) != 0);
17227 /* -1 to auto-determine strlen */
17228 list_append_string(rettv->vval.v_list, str, -1);
17229 list_append_number(rettv->vval.v_list, matchid);
17230 }
17231#endif
17232}
17233
17234/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017235 * "synstack(lnum, col)" function
17236 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017237 static void
17238f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017239 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017240 typval_T *rettv;
17241{
17242#ifdef FEAT_SYN_HL
17243 long lnum;
17244 long col;
17245 int i;
17246 int id;
17247#endif
17248
17249 rettv->v_type = VAR_LIST;
17250 rettv->vval.v_list = NULL;
17251
17252#ifdef FEAT_SYN_HL
17253 lnum = get_tv_lnum(argvars); /* -1 on type error */
17254 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17255
17256 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017257 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017258 && rettv_list_alloc(rettv) != FAIL)
17259 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017260 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017261 for (i = 0; ; ++i)
17262 {
17263 id = syn_get_stack_item(i);
17264 if (id < 0)
17265 break;
17266 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17267 break;
17268 }
17269 }
17270#endif
17271}
17272
17273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 * "system()" function
17275 */
17276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017277f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017278 typval_T *argvars;
17279 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017281 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017283 char_u *infile = NULL;
17284 char_u buf[NUMBUFLEN];
17285 int err = FALSE;
17286 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017288 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017289 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017290
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017291 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017292 {
17293 /*
17294 * Write the string to a temp file, to be used for input of the shell
17295 * command.
17296 */
17297 if ((infile = vim_tempname('i')) == NULL)
17298 {
17299 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017300 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017301 }
17302
17303 fd = mch_fopen((char *)infile, WRITEBIN);
17304 if (fd == NULL)
17305 {
17306 EMSG2(_(e_notopen), infile);
17307 goto done;
17308 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017309 p = get_tv_string_buf_chk(&argvars[1], buf);
17310 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017311 {
17312 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017313 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017314 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017315 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17316 err = TRUE;
17317 if (fclose(fd) != 0)
17318 err = TRUE;
17319 if (err)
17320 {
17321 EMSG(_("E677: Error writing temp file"));
17322 goto done;
17323 }
17324 }
17325
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017326 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17327 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017328
Bram Moolenaar071d4272004-06-13 20:20:40 +000017329#ifdef USE_CR
17330 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017331 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 {
17333 char_u *s;
17334
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017335 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 {
17337 if (*s == CAR)
17338 *s = NL;
17339 }
17340 }
17341#else
17342# ifdef USE_CRNL
17343 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017344 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 {
17346 char_u *s, *d;
17347
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017348 d = res;
17349 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 {
17351 if (s[0] == CAR && s[1] == NL)
17352 ++s;
17353 *d++ = *s;
17354 }
17355 *d = NUL;
17356 }
17357# endif
17358#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017359
17360done:
17361 if (infile != NULL)
17362 {
17363 mch_remove(infile);
17364 vim_free(infile);
17365 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017366 rettv->v_type = VAR_STRING;
17367 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368}
17369
17370/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017371 * "tabpagebuflist()" function
17372 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017373 static void
17374f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017375 typval_T *argvars UNUSED;
17376 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017377{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017378#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017379 tabpage_T *tp;
17380 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017381
17382 if (argvars[0].v_type == VAR_UNKNOWN)
17383 wp = firstwin;
17384 else
17385 {
17386 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17387 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017388 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017389 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017390 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017391 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017392 for (; wp != NULL; wp = wp->w_next)
17393 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017394 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017395 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017396 }
17397#endif
17398}
17399
17400
17401/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017402 * "tabpagenr()" function
17403 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017404 static void
17405f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017406 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017407 typval_T *rettv;
17408{
17409 int nr = 1;
17410#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017411 char_u *arg;
17412
17413 if (argvars[0].v_type != VAR_UNKNOWN)
17414 {
17415 arg = get_tv_string_chk(&argvars[0]);
17416 nr = 0;
17417 if (arg != NULL)
17418 {
17419 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017420 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017421 else
17422 EMSG2(_(e_invexpr2), arg);
17423 }
17424 }
17425 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017426 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017427#endif
17428 rettv->vval.v_number = nr;
17429}
17430
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017431
17432#ifdef FEAT_WINDOWS
17433static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17434
17435/*
17436 * Common code for tabpagewinnr() and winnr().
17437 */
17438 static int
17439get_winnr(tp, argvar)
17440 tabpage_T *tp;
17441 typval_T *argvar;
17442{
17443 win_T *twin;
17444 int nr = 1;
17445 win_T *wp;
17446 char_u *arg;
17447
17448 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17449 if (argvar->v_type != VAR_UNKNOWN)
17450 {
17451 arg = get_tv_string_chk(argvar);
17452 if (arg == NULL)
17453 nr = 0; /* type error; errmsg already given */
17454 else if (STRCMP(arg, "$") == 0)
17455 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17456 else if (STRCMP(arg, "#") == 0)
17457 {
17458 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17459 if (twin == NULL)
17460 nr = 0;
17461 }
17462 else
17463 {
17464 EMSG2(_(e_invexpr2), arg);
17465 nr = 0;
17466 }
17467 }
17468
17469 if (nr > 0)
17470 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17471 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017472 {
17473 if (wp == NULL)
17474 {
17475 /* didn't find it in this tabpage */
17476 nr = 0;
17477 break;
17478 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017479 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017480 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017481 return nr;
17482}
17483#endif
17484
17485/*
17486 * "tabpagewinnr()" function
17487 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017488 static void
17489f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017490 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017491 typval_T *rettv;
17492{
17493 int nr = 1;
17494#ifdef FEAT_WINDOWS
17495 tabpage_T *tp;
17496
17497 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17498 if (tp == NULL)
17499 nr = 0;
17500 else
17501 nr = get_winnr(tp, &argvars[1]);
17502#endif
17503 rettv->vval.v_number = nr;
17504}
17505
17506
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017507/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017508 * "tagfiles()" function
17509 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017510 static void
17511f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017512 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017513 typval_T *rettv;
17514{
17515 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017516 tagname_T tn;
17517 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017518
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017519 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017520 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017521
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017522 for (first = TRUE; ; first = FALSE)
17523 if (get_tagfname(&tn, first, fname) == FAIL
17524 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017525 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017526 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017527}
17528
17529/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017530 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017531 */
17532 static void
17533f_taglist(argvars, rettv)
17534 typval_T *argvars;
17535 typval_T *rettv;
17536{
17537 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017538
17539 tag_pattern = get_tv_string(&argvars[0]);
17540
17541 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017542 if (*tag_pattern == NUL)
17543 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017544
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017545 if (rettv_list_alloc(rettv) == OK)
17546 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017547}
17548
17549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550 * "tempname()" function
17551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017553f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017554 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556{
17557 static int x = 'A';
17558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017559 rettv->v_type = VAR_STRING;
17560 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561
17562 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17563 * names. Skip 'I' and 'O', they are used for shell redirection. */
17564 do
17565 {
17566 if (x == 'Z')
17567 x = '0';
17568 else if (x == '9')
17569 x = 'A';
17570 else
17571 {
17572#ifdef EBCDIC
17573 if (x == 'I')
17574 x = 'J';
17575 else if (x == 'R')
17576 x = 'S';
17577 else
17578#endif
17579 ++x;
17580 }
17581 } while (x == 'I' || x == 'O');
17582}
17583
17584/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017585 * "test(list)" function: Just checking the walls...
17586 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017587 static void
17588f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017589 typval_T *argvars UNUSED;
17590 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017591{
17592 /* Used for unit testing. Change the code below to your liking. */
17593#if 0
17594 listitem_T *li;
17595 list_T *l;
17596 char_u *bad, *good;
17597
17598 if (argvars[0].v_type != VAR_LIST)
17599 return;
17600 l = argvars[0].vval.v_list;
17601 if (l == NULL)
17602 return;
17603 li = l->lv_first;
17604 if (li == NULL)
17605 return;
17606 bad = get_tv_string(&li->li_tv);
17607 li = li->li_next;
17608 if (li == NULL)
17609 return;
17610 good = get_tv_string(&li->li_tv);
17611 rettv->vval.v_number = test_edit_score(bad, good);
17612#endif
17613}
17614
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017615#ifdef FEAT_FLOAT
17616/*
17617 * "tan()" function
17618 */
17619 static void
17620f_tan(argvars, rettv)
17621 typval_T *argvars;
17622 typval_T *rettv;
17623{
17624 float_T f;
17625
17626 rettv->v_type = VAR_FLOAT;
17627 if (get_float_arg(argvars, &f) == OK)
17628 rettv->vval.v_float = tan(f);
17629 else
17630 rettv->vval.v_float = 0.0;
17631}
17632
17633/*
17634 * "tanh()" function
17635 */
17636 static void
17637f_tanh(argvars, rettv)
17638 typval_T *argvars;
17639 typval_T *rettv;
17640{
17641 float_T f;
17642
17643 rettv->v_type = VAR_FLOAT;
17644 if (get_float_arg(argvars, &f) == OK)
17645 rettv->vval.v_float = tanh(f);
17646 else
17647 rettv->vval.v_float = 0.0;
17648}
17649#endif
17650
Bram Moolenaard52d9742005-08-21 22:20:28 +000017651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 * "tolower(string)" function
17653 */
17654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017655f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017656 typval_T *argvars;
17657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658{
17659 char_u *p;
17660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017661 p = vim_strsave(get_tv_string(&argvars[0]));
17662 rettv->v_type = VAR_STRING;
17663 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664
17665 if (p != NULL)
17666 while (*p != NUL)
17667 {
17668#ifdef FEAT_MBYTE
17669 int l;
17670
17671 if (enc_utf8)
17672 {
17673 int c, lc;
17674
17675 c = utf_ptr2char(p);
17676 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017677 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017678 /* TODO: reallocate string when byte count changes. */
17679 if (utf_char2len(lc) == l)
17680 utf_char2bytes(lc, p);
17681 p += l;
17682 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017683 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684 p += l; /* skip multi-byte character */
17685 else
17686#endif
17687 {
17688 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17689 ++p;
17690 }
17691 }
17692}
17693
17694/*
17695 * "toupper(string)" function
17696 */
17697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017698f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017699 typval_T *argvars;
17700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017702 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017703 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704}
17705
17706/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017707 * "tr(string, fromstr, tostr)" function
17708 */
17709 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017710f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017711 typval_T *argvars;
17712 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017713{
17714 char_u *instr;
17715 char_u *fromstr;
17716 char_u *tostr;
17717 char_u *p;
17718#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017719 int inlen;
17720 int fromlen;
17721 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017722 int idx;
17723 char_u *cpstr;
17724 int cplen;
17725 int first = TRUE;
17726#endif
17727 char_u buf[NUMBUFLEN];
17728 char_u buf2[NUMBUFLEN];
17729 garray_T ga;
17730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017731 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017732 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17733 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017734
17735 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017736 rettv->v_type = VAR_STRING;
17737 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017738 if (fromstr == NULL || tostr == NULL)
17739 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017740 ga_init2(&ga, (int)sizeof(char), 80);
17741
17742#ifdef FEAT_MBYTE
17743 if (!has_mbyte)
17744#endif
17745 /* not multi-byte: fromstr and tostr must be the same length */
17746 if (STRLEN(fromstr) != STRLEN(tostr))
17747 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017748#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017749error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017750#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017751 EMSG2(_(e_invarg2), fromstr);
17752 ga_clear(&ga);
17753 return;
17754 }
17755
17756 /* fromstr and tostr have to contain the same number of chars */
17757 while (*instr != NUL)
17758 {
17759#ifdef FEAT_MBYTE
17760 if (has_mbyte)
17761 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017762 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017763 cpstr = instr;
17764 cplen = inlen;
17765 idx = 0;
17766 for (p = fromstr; *p != NUL; p += fromlen)
17767 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017768 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017769 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17770 {
17771 for (p = tostr; *p != NUL; p += tolen)
17772 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017773 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017774 if (idx-- == 0)
17775 {
17776 cplen = tolen;
17777 cpstr = p;
17778 break;
17779 }
17780 }
17781 if (*p == NUL) /* tostr is shorter than fromstr */
17782 goto error;
17783 break;
17784 }
17785 ++idx;
17786 }
17787
17788 if (first && cpstr == instr)
17789 {
17790 /* Check that fromstr and tostr have the same number of
17791 * (multi-byte) characters. Done only once when a character
17792 * of instr doesn't appear in fromstr. */
17793 first = FALSE;
17794 for (p = tostr; *p != NUL; p += tolen)
17795 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017796 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017797 --idx;
17798 }
17799 if (idx != 0)
17800 goto error;
17801 }
17802
17803 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017804 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017805 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017806
17807 instr += inlen;
17808 }
17809 else
17810#endif
17811 {
17812 /* When not using multi-byte chars we can do it faster. */
17813 p = vim_strchr(fromstr, *instr);
17814 if (p != NULL)
17815 ga_append(&ga, tostr[p - fromstr]);
17816 else
17817 ga_append(&ga, *instr);
17818 ++instr;
17819 }
17820 }
17821
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017822 /* add a terminating NUL */
17823 ga_grow(&ga, 1);
17824 ga_append(&ga, NUL);
17825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017826 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017827}
17828
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017829#ifdef FEAT_FLOAT
17830/*
17831 * "trunc({float})" function
17832 */
17833 static void
17834f_trunc(argvars, rettv)
17835 typval_T *argvars;
17836 typval_T *rettv;
17837{
17838 float_T f;
17839
17840 rettv->v_type = VAR_FLOAT;
17841 if (get_float_arg(argvars, &f) == OK)
17842 /* trunc() is not in C90, use floor() or ceil() instead. */
17843 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17844 else
17845 rettv->vval.v_float = 0.0;
17846}
17847#endif
17848
Bram Moolenaar8299df92004-07-10 09:47:34 +000017849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850 * "type(expr)" function
17851 */
17852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017853f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017854 typval_T *argvars;
17855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017857 int n;
17858
17859 switch (argvars[0].v_type)
17860 {
17861 case VAR_NUMBER: n = 0; break;
17862 case VAR_STRING: n = 1; break;
17863 case VAR_FUNC: n = 2; break;
17864 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017865 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017866#ifdef FEAT_FLOAT
17867 case VAR_FLOAT: n = 5; break;
17868#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017869 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17870 }
17871 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872}
17873
17874/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017875 * "undofile(name)" function
17876 */
17877 static void
17878f_undofile(argvars, rettv)
17879 typval_T *argvars;
17880 typval_T *rettv;
17881{
17882 rettv->v_type = VAR_STRING;
17883#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017884 {
17885 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17886
17887 if (ffname != NULL)
17888 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17889 vim_free(ffname);
17890 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017891#else
17892 rettv->vval.v_string = NULL;
17893#endif
17894}
17895
17896/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017897 * "undotree()" function
17898 */
17899 static void
17900f_undotree(argvars, rettv)
17901 typval_T *argvars UNUSED;
17902 typval_T *rettv;
17903{
17904 if (rettv_dict_alloc(rettv) == OK)
17905 {
17906 dict_T *dict = rettv->vval.v_dict;
17907 list_T *list;
17908
Bram Moolenaar730cde92010-06-27 05:18:54 +020017909 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017910 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017911 dict_add_nr_str(dict, "save_last",
17912 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017913 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17914 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017915 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017916
17917 list = list_alloc();
17918 if (list != NULL)
17919 {
17920 u_eval_tree(curbuf->b_u_oldhead, list);
17921 dict_add_list(dict, "entries", list);
17922 }
17923 }
17924}
17925
17926/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017927 * "values(dict)" function
17928 */
17929 static void
17930f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017931 typval_T *argvars;
17932 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017933{
17934 dict_list(argvars, rettv, 1);
17935}
17936
17937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017938 * "virtcol(string)" function
17939 */
17940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017941f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017942 typval_T *argvars;
17943 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017944{
17945 colnr_T vcol = 0;
17946 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017947 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017949 fp = var2fpos(&argvars[0], FALSE, &fnum);
17950 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17951 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 {
17953 getvvcol(curwin, fp, NULL, NULL, &vcol);
17954 ++vcol;
17955 }
17956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017957 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017958}
17959
17960/*
17961 * "visualmode()" function
17962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017964f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017965 typval_T *argvars UNUSED;
17966 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967{
17968#ifdef FEAT_VISUAL
17969 char_u str[2];
17970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017971 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 str[0] = curbuf->b_visual_mode_eval;
17973 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017974 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975
17976 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017977 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979#endif
17980}
17981
17982/*
17983 * "winbufnr(nr)" function
17984 */
17985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017986f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017987 typval_T *argvars;
17988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989{
17990 win_T *wp;
17991
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017992 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017994 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017996 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997}
17998
17999/*
18000 * "wincol()" function
18001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018003f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018004 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006{
18007 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018008 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009}
18010
18011/*
18012 * "winheight(nr)" function
18013 */
18014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018015f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018016 typval_T *argvars;
18017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018{
18019 win_T *wp;
18020
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018021 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018023 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018025 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026}
18027
18028/*
18029 * "winline()" function
18030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018032f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018033 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035{
18036 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018037 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038}
18039
18040/*
18041 * "winnr()" function
18042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018044f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018045 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018047{
18048 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018049
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018051 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018053 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054}
18055
18056/*
18057 * "winrestcmd()" function
18058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018060f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018061 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018062 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063{
18064#ifdef FEAT_WINDOWS
18065 win_T *wp;
18066 int winnr = 1;
18067 garray_T ga;
18068 char_u buf[50];
18069
18070 ga_init2(&ga, (int)sizeof(char), 70);
18071 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18072 {
18073 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18074 ga_concat(&ga, buf);
18075# ifdef FEAT_VERTSPLIT
18076 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18077 ga_concat(&ga, buf);
18078# endif
18079 ++winnr;
18080 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018081 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018083 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018085 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018087 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088}
18089
18090/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018091 * "winrestview()" function
18092 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018093 static void
18094f_winrestview(argvars, rettv)
18095 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018096 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018097{
18098 dict_T *dict;
18099
18100 if (argvars[0].v_type != VAR_DICT
18101 || (dict = argvars[0].vval.v_dict) == NULL)
18102 EMSG(_(e_invarg));
18103 else
18104 {
18105 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18106 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18107#ifdef FEAT_VIRTUALEDIT
18108 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18109#endif
18110 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018111 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018112
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018113 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018114#ifdef FEAT_DIFF
18115 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18116#endif
18117 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18118 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18119
18120 check_cursor();
18121 changed_cline_bef_curs();
18122 invalidate_botline();
18123 redraw_later(VALID);
18124
18125 if (curwin->w_topline == 0)
18126 curwin->w_topline = 1;
18127 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18128 curwin->w_topline = curbuf->b_ml.ml_line_count;
18129#ifdef FEAT_DIFF
18130 check_topfill(curwin, TRUE);
18131#endif
18132 }
18133}
18134
18135/*
18136 * "winsaveview()" function
18137 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018138 static void
18139f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018140 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018141 typval_T *rettv;
18142{
18143 dict_T *dict;
18144
Bram Moolenaara800b422010-06-27 01:15:55 +020018145 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018146 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018147 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018148
18149 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18150 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18151#ifdef FEAT_VIRTUALEDIT
18152 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18153#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018154 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018155 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18156
18157 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18158#ifdef FEAT_DIFF
18159 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18160#endif
18161 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18162 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18163}
18164
18165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 * "winwidth(nr)" function
18167 */
18168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018169f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018170 typval_T *argvars;
18171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172{
18173 win_T *wp;
18174
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018175 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018177 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 else
18179#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018180 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018182 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183#endif
18184}
18185
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018187 * "writefile()" function
18188 */
18189 static void
18190f_writefile(argvars, rettv)
18191 typval_T *argvars;
18192 typval_T *rettv;
18193{
18194 int binary = FALSE;
18195 char_u *fname;
18196 FILE *fd;
18197 listitem_T *li;
18198 char_u *s;
18199 int ret = 0;
18200 int c;
18201
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018202 if (check_restricted() || check_secure())
18203 return;
18204
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018205 if (argvars[0].v_type != VAR_LIST)
18206 {
18207 EMSG2(_(e_listarg), "writefile()");
18208 return;
18209 }
18210 if (argvars[0].vval.v_list == NULL)
18211 return;
18212
18213 if (argvars[2].v_type != VAR_UNKNOWN
18214 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18215 binary = TRUE;
18216
18217 /* Always open the file in binary mode, library functions have a mind of
18218 * their own about CR-LF conversion. */
18219 fname = get_tv_string(&argvars[1]);
18220 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18221 {
18222 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18223 ret = -1;
18224 }
18225 else
18226 {
18227 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18228 li = li->li_next)
18229 {
18230 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18231 {
18232 if (*s == '\n')
18233 c = putc(NUL, fd);
18234 else
18235 c = putc(*s, fd);
18236 if (c == EOF)
18237 {
18238 ret = -1;
18239 break;
18240 }
18241 }
18242 if (!binary || li->li_next != NULL)
18243 if (putc('\n', fd) == EOF)
18244 {
18245 ret = -1;
18246 break;
18247 }
18248 if (ret < 0)
18249 {
18250 EMSG(_(e_write));
18251 break;
18252 }
18253 }
18254 fclose(fd);
18255 }
18256
18257 rettv->vval.v_number = ret;
18258}
18259
18260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018262 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 */
18264 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018265var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018266 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018267 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018268 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018270 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018272 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273
Bram Moolenaara5525202006-03-02 22:52:09 +000018274 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018275 if (varp->v_type == VAR_LIST)
18276 {
18277 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018278 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018279 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018280 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018281
18282 l = varp->vval.v_list;
18283 if (l == NULL)
18284 return NULL;
18285
18286 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018287 pos.lnum = list_find_nr(l, 0L, &error);
18288 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018289 return NULL; /* invalid line number */
18290
18291 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018292 pos.col = list_find_nr(l, 1L, &error);
18293 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018294 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018295 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018296
18297 /* We accept "$" for the column number: last column. */
18298 li = list_find(l, 1L);
18299 if (li != NULL && li->li_tv.v_type == VAR_STRING
18300 && li->li_tv.vval.v_string != NULL
18301 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18302 pos.col = len + 1;
18303
Bram Moolenaara5525202006-03-02 22:52:09 +000018304 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018305 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018306 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018307 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018308
Bram Moolenaara5525202006-03-02 22:52:09 +000018309#ifdef FEAT_VIRTUALEDIT
18310 /* Get the virtual offset. Defaults to zero. */
18311 pos.coladd = list_find_nr(l, 2L, &error);
18312 if (error)
18313 pos.coladd = 0;
18314#endif
18315
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018316 return &pos;
18317 }
18318
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018319 name = get_tv_string_chk(varp);
18320 if (name == NULL)
18321 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018322 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018324#ifdef FEAT_VISUAL
18325 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18326 {
18327 if (VIsual_active)
18328 return &VIsual;
18329 return &curwin->w_cursor;
18330 }
18331#endif
18332 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018333 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018334 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18336 return NULL;
18337 return pp;
18338 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018339
18340#ifdef FEAT_VIRTUALEDIT
18341 pos.coladd = 0;
18342#endif
18343
Bram Moolenaar477933c2007-07-17 14:32:23 +000018344 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018345 {
18346 pos.col = 0;
18347 if (name[1] == '0') /* "w0": first visible line */
18348 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018349 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018350 pos.lnum = curwin->w_topline;
18351 return &pos;
18352 }
18353 else if (name[1] == '$') /* "w$": last visible line */
18354 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018355 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018356 pos.lnum = curwin->w_botline - 1;
18357 return &pos;
18358 }
18359 }
18360 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018362 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 {
18364 pos.lnum = curbuf->b_ml.ml_line_count;
18365 pos.col = 0;
18366 }
18367 else
18368 {
18369 pos.lnum = curwin->w_cursor.lnum;
18370 pos.col = (colnr_T)STRLEN(ml_get_curline());
18371 }
18372 return &pos;
18373 }
18374 return NULL;
18375}
18376
18377/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018378 * Convert list in "arg" into a position and optional file number.
18379 * When "fnump" is NULL there is no file number, only 3 items.
18380 * Note that the column is passed on as-is, the caller may want to decrement
18381 * it to use 1 for the first column.
18382 * Return FAIL when conversion is not possible, doesn't check the position for
18383 * validity.
18384 */
18385 static int
18386list2fpos(arg, posp, fnump)
18387 typval_T *arg;
18388 pos_T *posp;
18389 int *fnump;
18390{
18391 list_T *l = arg->vval.v_list;
18392 long i = 0;
18393 long n;
18394
Bram Moolenaarbde35262006-07-23 20:12:24 +000018395 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18396 * when "fnump" isn't NULL and "coladd" is optional. */
18397 if (arg->v_type != VAR_LIST
18398 || l == NULL
18399 || l->lv_len < (fnump == NULL ? 2 : 3)
18400 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018401 return FAIL;
18402
18403 if (fnump != NULL)
18404 {
18405 n = list_find_nr(l, i++, NULL); /* fnum */
18406 if (n < 0)
18407 return FAIL;
18408 if (n == 0)
18409 n = curbuf->b_fnum; /* current buffer */
18410 *fnump = n;
18411 }
18412
18413 n = list_find_nr(l, i++, NULL); /* lnum */
18414 if (n < 0)
18415 return FAIL;
18416 posp->lnum = n;
18417
18418 n = list_find_nr(l, i++, NULL); /* col */
18419 if (n < 0)
18420 return FAIL;
18421 posp->col = n;
18422
18423#ifdef FEAT_VIRTUALEDIT
18424 n = list_find_nr(l, i, NULL);
18425 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018426 posp->coladd = 0;
18427 else
18428 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018429#endif
18430
18431 return OK;
18432}
18433
18434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 * Get the length of an environment variable name.
18436 * Advance "arg" to the first character after the name.
18437 * Return 0 for error.
18438 */
18439 static int
18440get_env_len(arg)
18441 char_u **arg;
18442{
18443 char_u *p;
18444 int len;
18445
18446 for (p = *arg; vim_isIDc(*p); ++p)
18447 ;
18448 if (p == *arg) /* no name found */
18449 return 0;
18450
18451 len = (int)(p - *arg);
18452 *arg = p;
18453 return len;
18454}
18455
18456/*
18457 * Get the length of the name of a function or internal variable.
18458 * "arg" is advanced to the first non-white character after the name.
18459 * Return 0 if something is wrong.
18460 */
18461 static int
18462get_id_len(arg)
18463 char_u **arg;
18464{
18465 char_u *p;
18466 int len;
18467
18468 /* Find the end of the name. */
18469 for (p = *arg; eval_isnamec(*p); ++p)
18470 ;
18471 if (p == *arg) /* no name found */
18472 return 0;
18473
18474 len = (int)(p - *arg);
18475 *arg = skipwhite(p);
18476
18477 return len;
18478}
18479
18480/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018481 * Get the length of the name of a variable or function.
18482 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018484 * Return -1 if curly braces expansion failed.
18485 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018486 * If the name contains 'magic' {}'s, expand them and return the
18487 * expanded name in an allocated string via 'alias' - caller must free.
18488 */
18489 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018490get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 char_u **arg;
18492 char_u **alias;
18493 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018494 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495{
18496 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497 char_u *p;
18498 char_u *expr_start;
18499 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500
18501 *alias = NULL; /* default to no alias */
18502
18503 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18504 && (*arg)[2] == (int)KE_SNR)
18505 {
18506 /* hard coded <SNR>, already translated */
18507 *arg += 3;
18508 return get_id_len(arg) + 3;
18509 }
18510 len = eval_fname_script(*arg);
18511 if (len > 0)
18512 {
18513 /* literal "<SID>", "s:" or "<SNR>" */
18514 *arg += len;
18515 }
18516
Bram Moolenaar071d4272004-06-13 20:20:40 +000018517 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018518 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018520 p = find_name_end(*arg, &expr_start, &expr_end,
18521 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522 if (expr_start != NULL)
18523 {
18524 char_u *temp_string;
18525
18526 if (!evaluate)
18527 {
18528 len += (int)(p - *arg);
18529 *arg = skipwhite(p);
18530 return len;
18531 }
18532
18533 /*
18534 * Include any <SID> etc in the expanded string:
18535 * Thus the -len here.
18536 */
18537 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18538 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018539 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 *alias = temp_string;
18541 *arg = skipwhite(p);
18542 return (int)STRLEN(temp_string);
18543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544
18545 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018546 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 EMSG2(_(e_invexpr2), *arg);
18548
18549 return len;
18550}
18551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018552/*
18553 * Find the end of a variable or function name, taking care of magic braces.
18554 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18555 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018556 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018557 * Return a pointer to just after the name. Equal to "arg" if there is no
18558 * valid name.
18559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018561find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 char_u *arg;
18563 char_u **expr_start;
18564 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018565 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018567 int mb_nest = 0;
18568 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569 char_u *p;
18570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018573 *expr_start = NULL;
18574 *expr_end = NULL;
18575 }
18576
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018577 /* Quick check for valid starting character. */
18578 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18579 return arg;
18580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018581 for (p = arg; *p != NUL
18582 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018583 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018584 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018585 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018586 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018587 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018588 if (*p == '\'')
18589 {
18590 /* skip over 'string' to avoid counting [ and ] inside it. */
18591 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18592 ;
18593 if (*p == NUL)
18594 break;
18595 }
18596 else if (*p == '"')
18597 {
18598 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18599 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18600 if (*p == '\\' && p[1] != NUL)
18601 ++p;
18602 if (*p == NUL)
18603 break;
18604 }
18605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018606 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018608 if (*p == '[')
18609 ++br_nest;
18610 else if (*p == ']')
18611 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018614 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616 if (*p == '{')
18617 {
18618 mb_nest++;
18619 if (expr_start != NULL && *expr_start == NULL)
18620 *expr_start = p;
18621 }
18622 else if (*p == '}')
18623 {
18624 mb_nest--;
18625 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18626 *expr_end = p;
18627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 }
18630
18631 return p;
18632}
18633
18634/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018635 * Expands out the 'magic' {}'s in a variable/function name.
18636 * Note that this can call itself recursively, to deal with
18637 * constructs like foo{bar}{baz}{bam}
18638 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18639 * "in_start" ^
18640 * "expr_start" ^
18641 * "expr_end" ^
18642 * "in_end" ^
18643 *
18644 * Returns a new allocated string, which the caller must free.
18645 * Returns NULL for failure.
18646 */
18647 static char_u *
18648make_expanded_name(in_start, expr_start, expr_end, in_end)
18649 char_u *in_start;
18650 char_u *expr_start;
18651 char_u *expr_end;
18652 char_u *in_end;
18653{
18654 char_u c1;
18655 char_u *retval = NULL;
18656 char_u *temp_result;
18657 char_u *nextcmd = NULL;
18658
18659 if (expr_end == NULL || in_end == NULL)
18660 return NULL;
18661 *expr_start = NUL;
18662 *expr_end = NUL;
18663 c1 = *in_end;
18664 *in_end = NUL;
18665
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018666 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018667 if (temp_result != NULL && nextcmd == NULL)
18668 {
18669 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18670 + (in_end - expr_end) + 1));
18671 if (retval != NULL)
18672 {
18673 STRCPY(retval, in_start);
18674 STRCAT(retval, temp_result);
18675 STRCAT(retval, expr_end + 1);
18676 }
18677 }
18678 vim_free(temp_result);
18679
18680 *in_end = c1; /* put char back for error messages */
18681 *expr_start = '{';
18682 *expr_end = '}';
18683
18684 if (retval != NULL)
18685 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018686 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018687 if (expr_start != NULL)
18688 {
18689 /* Further expansion! */
18690 temp_result = make_expanded_name(retval, expr_start,
18691 expr_end, temp_result);
18692 vim_free(retval);
18693 retval = temp_result;
18694 }
18695 }
18696
18697 return retval;
18698}
18699
18700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018702 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 */
18704 static int
18705eval_isnamec(c)
18706 int c;
18707{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018708 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18709}
18710
18711/*
18712 * Return TRUE if character "c" can be used as the first character in a
18713 * variable or function name (excluding '{' and '}').
18714 */
18715 static int
18716eval_isnamec1(c)
18717 int c;
18718{
18719 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720}
18721
18722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 * Set number v: variable to "val".
18724 */
18725 void
18726set_vim_var_nr(idx, val)
18727 int idx;
18728 long val;
18729{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018730 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731}
18732
18733/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018734 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 */
18736 long
18737get_vim_var_nr(idx)
18738 int idx;
18739{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018740 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741}
18742
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018743/*
18744 * Get string v: variable value. Uses a static buffer, can only be used once.
18745 */
18746 char_u *
18747get_vim_var_str(idx)
18748 int idx;
18749{
18750 return get_tv_string(&vimvars[idx].vv_tv);
18751}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018752
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018754 * Get List v: variable value. Caller must take care of reference count when
18755 * needed.
18756 */
18757 list_T *
18758get_vim_var_list(idx)
18759 int idx;
18760{
18761 return vimvars[idx].vv_list;
18762}
18763
18764/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018765 * Set v:char to character "c".
18766 */
18767 void
18768set_vim_var_char(c)
18769 int c;
18770{
18771#ifdef FEAT_MBYTE
18772 char_u buf[MB_MAXBYTES];
18773#else
18774 char_u buf[2];
18775#endif
18776
18777#ifdef FEAT_MBYTE
18778 if (has_mbyte)
18779 buf[(*mb_char2bytes)(c, buf)] = NUL;
18780 else
18781#endif
18782 {
18783 buf[0] = c;
18784 buf[1] = NUL;
18785 }
18786 set_vim_var_string(VV_CHAR, buf, -1);
18787}
18788
18789/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018790 * Set v:count to "count" and v:count1 to "count1".
18791 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 */
18793 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018794set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 long count;
18796 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018797 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018799 if (set_prevcount)
18800 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018801 vimvars[VV_COUNT].vv_nr = count;
18802 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803}
18804
18805/*
18806 * Set string v: variable to a copy of "val".
18807 */
18808 void
18809set_vim_var_string(idx, val, len)
18810 int idx;
18811 char_u *val;
18812 int len; /* length of "val" to use or -1 (whole string) */
18813{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018814 /* Need to do this (at least) once, since we can't initialize a union.
18815 * Will always be invoked when "v:progname" is set. */
18816 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18817
Bram Moolenaare9a41262005-01-15 22:18:47 +000018818 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018820 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018822 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018824 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825}
18826
18827/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018828 * Set List v: variable to "val".
18829 */
18830 void
18831set_vim_var_list(idx, val)
18832 int idx;
18833 list_T *val;
18834{
18835 list_unref(vimvars[idx].vv_list);
18836 vimvars[idx].vv_list = val;
18837 if (val != NULL)
18838 ++val->lv_refcount;
18839}
18840
18841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842 * Set v:register if needed.
18843 */
18844 void
18845set_reg_var(c)
18846 int c;
18847{
18848 char_u regname;
18849
18850 if (c == 0 || c == ' ')
18851 regname = '"';
18852 else
18853 regname = c;
18854 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018855 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 set_vim_var_string(VV_REG, &regname, 1);
18857}
18858
18859/*
18860 * Get or set v:exception. If "oldval" == NULL, return the current value.
18861 * Otherwise, restore the value to "oldval" and return NULL.
18862 * Must always be called in pairs to save and restore v:exception! Does not
18863 * take care of memory allocations.
18864 */
18865 char_u *
18866v_exception(oldval)
18867 char_u *oldval;
18868{
18869 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018870 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018871
Bram Moolenaare9a41262005-01-15 22:18:47 +000018872 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873 return NULL;
18874}
18875
18876/*
18877 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18878 * Otherwise, restore the value to "oldval" and return NULL.
18879 * Must always be called in pairs to save and restore v:throwpoint! Does not
18880 * take care of memory allocations.
18881 */
18882 char_u *
18883v_throwpoint(oldval)
18884 char_u *oldval;
18885{
18886 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018887 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888
Bram Moolenaare9a41262005-01-15 22:18:47 +000018889 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 return NULL;
18891}
18892
18893#if defined(FEAT_AUTOCMD) || defined(PROTO)
18894/*
18895 * Set v:cmdarg.
18896 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18897 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18898 * Must always be called in pairs!
18899 */
18900 char_u *
18901set_cmdarg(eap, oldarg)
18902 exarg_T *eap;
18903 char_u *oldarg;
18904{
18905 char_u *oldval;
18906 char_u *newval;
18907 unsigned len;
18908
Bram Moolenaare9a41262005-01-15 22:18:47 +000018909 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018910 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018912 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018913 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018914 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 }
18916
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018917 if (eap->force_bin == FORCE_BIN)
18918 len = 6;
18919 else if (eap->force_bin == FORCE_NOBIN)
18920 len = 8;
18921 else
18922 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018923
18924 if (eap->read_edit)
18925 len += 7;
18926
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018927 if (eap->force_ff != 0)
18928 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18929# ifdef FEAT_MBYTE
18930 if (eap->force_enc != 0)
18931 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018932 if (eap->bad_char != 0)
18933 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018934# endif
18935
18936 newval = alloc(len + 1);
18937 if (newval == NULL)
18938 return NULL;
18939
18940 if (eap->force_bin == FORCE_BIN)
18941 sprintf((char *)newval, " ++bin");
18942 else if (eap->force_bin == FORCE_NOBIN)
18943 sprintf((char *)newval, " ++nobin");
18944 else
18945 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018946
18947 if (eap->read_edit)
18948 STRCAT(newval, " ++edit");
18949
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018950 if (eap->force_ff != 0)
18951 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18952 eap->cmd + eap->force_ff);
18953# ifdef FEAT_MBYTE
18954 if (eap->force_enc != 0)
18955 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18956 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018957 if (eap->bad_char == BAD_KEEP)
18958 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18959 else if (eap->bad_char == BAD_DROP)
18960 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18961 else if (eap->bad_char != 0)
18962 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018963# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018964 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018965 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018966}
18967#endif
18968
18969/*
18970 * Get the value of internal variable "name".
18971 * Return OK or FAIL.
18972 */
18973 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018974get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975 char_u *name;
18976 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018977 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018978 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018979{
18980 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018981 typval_T *tv = NULL;
18982 typval_T atv;
18983 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018984 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985
18986 /* truncate the name, so that we can use strcmp() */
18987 cc = name[len];
18988 name[len] = NUL;
18989
18990 /*
18991 * Check for "b:changedtick".
18992 */
18993 if (STRCMP(name, "b:changedtick") == 0)
18994 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018995 atv.v_type = VAR_NUMBER;
18996 atv.vval.v_number = curbuf->b_changedtick;
18997 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018998 }
18999
19000 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001 * Check for user-defined variables.
19002 */
19003 else
19004 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019005 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019007 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 }
19009
Bram Moolenaare9a41262005-01-15 22:18:47 +000019010 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019011 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019012 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019013 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019014 ret = FAIL;
19015 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019016 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019017 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018
19019 name[len] = cc;
19020
19021 return ret;
19022}
19023
19024/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019025 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19026 * Also handle function call with Funcref variable: func(expr)
19027 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19028 */
19029 static int
19030handle_subscript(arg, rettv, evaluate, verbose)
19031 char_u **arg;
19032 typval_T *rettv;
19033 int evaluate; /* do more than finding the end */
19034 int verbose; /* give error messages */
19035{
19036 int ret = OK;
19037 dict_T *selfdict = NULL;
19038 char_u *s;
19039 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019040 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019041
19042 while (ret == OK
19043 && (**arg == '['
19044 || (**arg == '.' && rettv->v_type == VAR_DICT)
19045 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19046 && !vim_iswhite(*(*arg - 1)))
19047 {
19048 if (**arg == '(')
19049 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019050 /* need to copy the funcref so that we can clear rettv */
19051 functv = *rettv;
19052 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019053
19054 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019055 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019056 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019057 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19058 &len, evaluate, selfdict);
19059
19060 /* Clear the funcref afterwards, so that deleting it while
19061 * evaluating the arguments is possible (see test55). */
19062 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019063
19064 /* Stop the expression evaluation when immediately aborting on
19065 * error, or when an interrupt occurred or an exception was thrown
19066 * but not caught. */
19067 if (aborting())
19068 {
19069 if (ret == OK)
19070 clear_tv(rettv);
19071 ret = FAIL;
19072 }
19073 dict_unref(selfdict);
19074 selfdict = NULL;
19075 }
19076 else /* **arg == '[' || **arg == '.' */
19077 {
19078 dict_unref(selfdict);
19079 if (rettv->v_type == VAR_DICT)
19080 {
19081 selfdict = rettv->vval.v_dict;
19082 if (selfdict != NULL)
19083 ++selfdict->dv_refcount;
19084 }
19085 else
19086 selfdict = NULL;
19087 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19088 {
19089 clear_tv(rettv);
19090 ret = FAIL;
19091 }
19092 }
19093 }
19094 dict_unref(selfdict);
19095 return ret;
19096}
19097
19098/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019099 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019100 * value).
19101 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019102 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019103alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019104{
Bram Moolenaar33570922005-01-25 22:26:29 +000019105 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019106}
19107
19108/*
19109 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019110 * The string "s" must have been allocated, it is consumed.
19111 * Return NULL for out of memory, the variable otherwise.
19112 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019113 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019114alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 char_u *s;
19116{
Bram Moolenaar33570922005-01-25 22:26:29 +000019117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019119 rettv = alloc_tv();
19120 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019122 rettv->v_type = VAR_STRING;
19123 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 }
19125 else
19126 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019127 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128}
19129
19130/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019131 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019133 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019134free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019135 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019136{
19137 if (varp != NULL)
19138 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019139 switch (varp->v_type)
19140 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019141 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019142 func_unref(varp->vval.v_string);
19143 /*FALLTHROUGH*/
19144 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019145 vim_free(varp->vval.v_string);
19146 break;
19147 case VAR_LIST:
19148 list_unref(varp->vval.v_list);
19149 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019150 case VAR_DICT:
19151 dict_unref(varp->vval.v_dict);
19152 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019153 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019154#ifdef FEAT_FLOAT
19155 case VAR_FLOAT:
19156#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019157 case VAR_UNKNOWN:
19158 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019159 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019160 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019161 break;
19162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019163 vim_free(varp);
19164 }
19165}
19166
19167/*
19168 * Free the memory for a variable value and set the value to NULL or 0.
19169 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019170 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019171clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019172 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173{
19174 if (varp != NULL)
19175 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019176 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019178 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019179 func_unref(varp->vval.v_string);
19180 /*FALLTHROUGH*/
19181 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019182 vim_free(varp->vval.v_string);
19183 varp->vval.v_string = NULL;
19184 break;
19185 case VAR_LIST:
19186 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019187 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019188 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019189 case VAR_DICT:
19190 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019191 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019192 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019193 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019194 varp->vval.v_number = 0;
19195 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019196#ifdef FEAT_FLOAT
19197 case VAR_FLOAT:
19198 varp->vval.v_float = 0.0;
19199 break;
19200#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019201 case VAR_UNKNOWN:
19202 break;
19203 default:
19204 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019206 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 }
19208}
19209
19210/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019211 * Set the value of a variable to NULL without freeing items.
19212 */
19213 static void
19214init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019215 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019216{
19217 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019218 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019219}
19220
19221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 * Get the number value of a variable.
19223 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019224 * For incompatible types, return 0.
19225 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19226 * caller of incompatible types: it sets *denote to TRUE if "denote"
19227 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 */
19229 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019230get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019231 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019232{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019233 int error = FALSE;
19234
19235 return get_tv_number_chk(varp, &error); /* return 0L on error */
19236}
19237
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019238 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019239get_tv_number_chk(varp, denote)
19240 typval_T *varp;
19241 int *denote;
19242{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019243 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019245 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019247 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019248 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019249#ifdef FEAT_FLOAT
19250 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019251 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019252 break;
19253#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019254 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019255 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019256 break;
19257 case VAR_STRING:
19258 if (varp->vval.v_string != NULL)
19259 vim_str2nr(varp->vval.v_string, NULL, NULL,
19260 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019261 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019262 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019263 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019264 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019265 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019266 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019267 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019268 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019269 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019270 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019272 if (denote == NULL) /* useful for values that must be unsigned */
19273 n = -1;
19274 else
19275 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019276 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277}
19278
19279/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019280 * Get the lnum from the first argument.
19281 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019282 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283 */
19284 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019285get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019286 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287{
Bram Moolenaar33570922005-01-25 22:26:29 +000019288 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 linenr_T lnum;
19290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019291 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 if (lnum == 0) /* no valid number, try using line() */
19293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019294 rettv.v_type = VAR_NUMBER;
19295 f_line(argvars, &rettv);
19296 lnum = rettv.vval.v_number;
19297 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 }
19299 return lnum;
19300}
19301
19302/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019303 * Get the lnum from the first argument.
19304 * Also accepts "$", then "buf" is used.
19305 * Returns 0 on error.
19306 */
19307 static linenr_T
19308get_tv_lnum_buf(argvars, buf)
19309 typval_T *argvars;
19310 buf_T *buf;
19311{
19312 if (argvars[0].v_type == VAR_STRING
19313 && argvars[0].vval.v_string != NULL
19314 && argvars[0].vval.v_string[0] == '$'
19315 && buf != NULL)
19316 return buf->b_ml.ml_line_count;
19317 return get_tv_number_chk(&argvars[0], NULL);
19318}
19319
19320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 * Get the string value of a variable.
19322 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019323 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19324 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325 * If the String variable has never been set, return an empty string.
19326 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019327 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19328 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 */
19330 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019331get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019332 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019333{
19334 static char_u mybuf[NUMBUFLEN];
19335
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019336 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337}
19338
19339 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019340get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019341 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019342 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019344 char_u *res = get_tv_string_buf_chk(varp, buf);
19345
19346 return res != NULL ? res : (char_u *)"";
19347}
19348
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019349 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019350get_tv_string_chk(varp)
19351 typval_T *varp;
19352{
19353 static char_u mybuf[NUMBUFLEN];
19354
19355 return get_tv_string_buf_chk(varp, mybuf);
19356}
19357
19358 static char_u *
19359get_tv_string_buf_chk(varp, buf)
19360 typval_T *varp;
19361 char_u *buf;
19362{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019363 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019365 case VAR_NUMBER:
19366 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19367 return buf;
19368 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019369 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019370 break;
19371 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019372 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019373 break;
19374 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019375 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019376 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019377#ifdef FEAT_FLOAT
19378 case VAR_FLOAT:
19379 EMSG(_("E806: using Float as a String"));
19380 break;
19381#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019382 case VAR_STRING:
19383 if (varp->vval.v_string != NULL)
19384 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019385 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019386 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019387 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019388 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019390 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391}
19392
19393/*
19394 * Find variable "name" in the list of variables.
19395 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019396 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019397 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019398 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019400 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019401find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019402 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019403 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019406 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407
Bram Moolenaara7043832005-01-21 11:56:39 +000019408 ht = find_var_ht(name, &varname);
19409 if (htp != NULL)
19410 *htp = ht;
19411 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019413 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414}
19415
19416/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019417 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019418 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019419 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019420 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019421find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019422 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019423 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019424 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019425{
Bram Moolenaar33570922005-01-25 22:26:29 +000019426 hashitem_T *hi;
19427
19428 if (*varname == NUL)
19429 {
19430 /* Must be something like "s:", otherwise "ht" would be NULL. */
19431 switch (varname[-2])
19432 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019433 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019434 case 'g': return &globvars_var;
19435 case 'v': return &vimvars_var;
19436 case 'b': return &curbuf->b_bufvar;
19437 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019438#ifdef FEAT_WINDOWS
19439 case 't': return &curtab->tp_winvar;
19440#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019441 case 'l': return current_funccal == NULL
19442 ? NULL : &current_funccal->l_vars_var;
19443 case 'a': return current_funccal == NULL
19444 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019445 }
19446 return NULL;
19447 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019448
19449 hi = hash_find(ht, varname);
19450 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019451 {
19452 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019453 * worked find the variable again. Don't auto-load a script if it was
19454 * loaded already, otherwise it would be loaded every time when
19455 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019456 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019457 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019458 hi = hash_find(ht, varname);
19459 if (HASHITEM_EMPTY(hi))
19460 return NULL;
19461 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019462 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019463}
19464
19465/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019466 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019467 * Set "varname" to the start of name without ':'.
19468 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019469 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019470find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 char_u *name;
19472 char_u **varname;
19473{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019474 hashitem_T *hi;
19475
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476 if (name[1] != ':')
19477 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019478 /* The name must not start with a colon or #. */
19479 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019480 return NULL;
19481 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019482
19483 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019484 hi = hash_find(&compat_hashtab, name);
19485 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019486 return &compat_hashtab;
19487
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019489 return &globvarht; /* global variable */
19490 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491 }
19492 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019493 if (*name == 'g') /* global variable */
19494 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019495 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19496 */
19497 if (vim_strchr(name + 2, ':') != NULL
19498 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019499 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019501 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019503 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019504#ifdef FEAT_WINDOWS
19505 if (*name == 't') /* tab page variable */
19506 return &curtab->tp_vars.dv_hashtab;
19507#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019508 if (*name == 'v') /* v: variable */
19509 return &vimvarht;
19510 if (*name == 'a' && current_funccal != NULL) /* function argument */
19511 return &current_funccal->l_avars.dv_hashtab;
19512 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19513 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 if (*name == 's' /* script variable */
19515 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19516 return &SCRIPT_VARS(current_SID);
19517 return NULL;
19518}
19519
19520/*
19521 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019522 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 * Returns NULL when it doesn't exist.
19524 */
19525 char_u *
19526get_var_value(name)
19527 char_u *name;
19528{
Bram Moolenaar33570922005-01-25 22:26:29 +000019529 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530
Bram Moolenaara7043832005-01-21 11:56:39 +000019531 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532 if (v == NULL)
19533 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019534 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019535}
19536
19537/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019538 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 * sourcing this script and when executing functions defined in the script.
19540 */
19541 void
19542new_script_vars(id)
19543 scid_T id;
19544{
Bram Moolenaara7043832005-01-21 11:56:39 +000019545 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019546 hashtab_T *ht;
19547 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019548
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19550 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019551 /* Re-allocating ga_data means that an ht_array pointing to
19552 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019553 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019554 for (i = 1; i <= ga_scripts.ga_len; ++i)
19555 {
19556 ht = &SCRIPT_VARS(i);
19557 if (ht->ht_mask == HT_INIT_SIZE - 1)
19558 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019559 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019560 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019561 }
19562
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 while (ga_scripts.ga_len < id)
19564 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019565 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019566 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019567 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569 }
19570 }
19571}
19572
19573/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019574 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19575 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576 */
19577 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019578init_var_dict(dict, dict_var)
19579 dict_T *dict;
19580 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581{
Bram Moolenaar33570922005-01-25 22:26:29 +000019582 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019583 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019584 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019585 dict_var->di_tv.vval.v_dict = dict;
19586 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019587 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019588 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19589 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590}
19591
19592/*
19593 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019594 * Frees all allocated variables and the value they contain.
19595 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 */
19597 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019598vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019599 hashtab_T *ht;
19600{
19601 vars_clear_ext(ht, TRUE);
19602}
19603
19604/*
19605 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19606 */
19607 static void
19608vars_clear_ext(ht, free_val)
19609 hashtab_T *ht;
19610 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611{
Bram Moolenaara7043832005-01-21 11:56:39 +000019612 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019613 hashitem_T *hi;
19614 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019617 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019618 for (hi = ht->ht_array; todo > 0; ++hi)
19619 {
19620 if (!HASHITEM_EMPTY(hi))
19621 {
19622 --todo;
19623
Bram Moolenaar33570922005-01-25 22:26:29 +000019624 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019625 * ht_array might change then. hash_clear() takes care of it
19626 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019627 v = HI2DI(hi);
19628 if (free_val)
19629 clear_tv(&v->di_tv);
19630 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19631 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019632 }
19633 }
19634 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019635 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636}
19637
Bram Moolenaara7043832005-01-21 11:56:39 +000019638/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019639 * Delete a variable from hashtab "ht" at item "hi".
19640 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019642 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019643delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 hashtab_T *ht;
19645 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646{
Bram Moolenaar33570922005-01-25 22:26:29 +000019647 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019648
19649 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019650 clear_tv(&di->di_tv);
19651 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652}
19653
19654/*
19655 * List the value of one internal variable.
19656 */
19657 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019658list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019659 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019660 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019661 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019663 char_u *tofree;
19664 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019665 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019666
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019667 current_copyID += COPYID_INC;
19668 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019669 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019670 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019671 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672}
19673
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019675list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 char_u *prefix;
19677 char_u *name;
19678 int type;
19679 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019680 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681{
Bram Moolenaar31859182007-08-14 20:41:13 +000019682 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19683 msg_start();
19684 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 if (name != NULL) /* "a:" vars don't have a name stored */
19686 msg_puts(name);
19687 msg_putchar(' ');
19688 msg_advance(22);
19689 if (type == VAR_NUMBER)
19690 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019691 else if (type == VAR_FUNC)
19692 msg_putchar('*');
19693 else if (type == VAR_LIST)
19694 {
19695 msg_putchar('[');
19696 if (*string == '[')
19697 ++string;
19698 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019699 else if (type == VAR_DICT)
19700 {
19701 msg_putchar('{');
19702 if (*string == '{')
19703 ++string;
19704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 else
19706 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019707
Bram Moolenaar071d4272004-06-13 20:20:40 +000019708 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019709
19710 if (type == VAR_FUNC)
19711 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019712 if (*first)
19713 {
19714 msg_clr_eos();
19715 *first = FALSE;
19716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717}
19718
19719/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 * If the variable already exists, the value is updated.
19722 * Otherwise the variable is created.
19723 */
19724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019725set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019726 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019727 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019728 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729{
Bram Moolenaar33570922005-01-25 22:26:29 +000019730 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019733 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019734
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019735 ht = find_var_ht(name, &varname);
19736 if (ht == NULL || *varname == NUL)
19737 {
19738 EMSG2(_(e_illvar), name);
19739 return;
19740 }
19741 v = find_var_in_ht(ht, varname, TRUE);
19742
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019743 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019744 {
19745 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19746 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19747 ? name[2] : name[0]))
19748 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019749 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019750 return;
19751 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019752 /* Don't allow hiding a function. When "v" is not NULL we migth be
19753 * assigning another function to the same var, the type is checked
19754 * below. */
19755 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019756 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019757 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019758 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019759 return;
19760 }
19761 }
19762
Bram Moolenaar33570922005-01-25 22:26:29 +000019763 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019765 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019766 if (var_check_ro(v->di_flags, name)
19767 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019768 return;
19769 if (v->di_tv.v_type != tv->v_type
19770 && !((v->di_tv.v_type == VAR_STRING
19771 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019772 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019773 || tv->v_type == VAR_NUMBER))
19774#ifdef FEAT_FLOAT
19775 && !((v->di_tv.v_type == VAR_NUMBER
19776 || v->di_tv.v_type == VAR_FLOAT)
19777 && (tv->v_type == VAR_NUMBER
19778 || tv->v_type == VAR_FLOAT))
19779#endif
19780 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019781 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019782 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019783 return;
19784 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019785
19786 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019787 * Handle setting internal v: variables separately: we don't change
19788 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019789 */
19790 if (ht == &vimvarht)
19791 {
19792 if (v->di_tv.v_type == VAR_STRING)
19793 {
19794 vim_free(v->di_tv.vval.v_string);
19795 if (copy || tv->v_type != VAR_STRING)
19796 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19797 else
19798 {
19799 /* Take over the string to avoid an extra alloc/free. */
19800 v->di_tv.vval.v_string = tv->vval.v_string;
19801 tv->vval.v_string = NULL;
19802 }
19803 }
19804 else if (v->di_tv.v_type != VAR_NUMBER)
19805 EMSG2(_(e_intern2), "set_var()");
19806 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019807 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019808 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019809 if (STRCMP(varname, "searchforward") == 0)
19810 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19811 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019812 return;
19813 }
19814
19815 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019816 }
19817 else /* add a new variable */
19818 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019819 /* Can't add "v:" variable. */
19820 if (ht == &vimvarht)
19821 {
19822 EMSG2(_(e_illvar), name);
19823 return;
19824 }
19825
Bram Moolenaar92124a32005-06-17 22:03:40 +000019826 /* Make sure the variable name is valid. */
19827 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019828 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19829 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019830 {
19831 EMSG2(_(e_illvar), varname);
19832 return;
19833 }
19834
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019835 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19836 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019837 if (v == NULL)
19838 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019839 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019840 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019842 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 return;
19844 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019845 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019846 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019847
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019848 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019849 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019850 else
19851 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019852 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019853 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019854 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856}
19857
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019858/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019859 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019860 * Also give an error message.
19861 */
19862 static int
19863var_check_ro(flags, name)
19864 int flags;
19865 char_u *name;
19866{
19867 if (flags & DI_FLAGS_RO)
19868 {
19869 EMSG2(_(e_readonlyvar), name);
19870 return TRUE;
19871 }
19872 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19873 {
19874 EMSG2(_(e_readonlysbx), name);
19875 return TRUE;
19876 }
19877 return FALSE;
19878}
19879
19880/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019881 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19882 * Also give an error message.
19883 */
19884 static int
19885var_check_fixed(flags, name)
19886 int flags;
19887 char_u *name;
19888{
19889 if (flags & DI_FLAGS_FIX)
19890 {
19891 EMSG2(_("E795: Cannot delete variable %s"), name);
19892 return TRUE;
19893 }
19894 return FALSE;
19895}
19896
19897/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019898 * Return TRUE if typeval "tv" is set to be locked (immutable).
19899 * Also give an error message, using "name".
19900 */
19901 static int
19902tv_check_lock(lock, name)
19903 int lock;
19904 char_u *name;
19905{
19906 if (lock & VAR_LOCKED)
19907 {
19908 EMSG2(_("E741: Value is locked: %s"),
19909 name == NULL ? (char_u *)_("Unknown") : name);
19910 return TRUE;
19911 }
19912 if (lock & VAR_FIXED)
19913 {
19914 EMSG2(_("E742: Cannot change value of %s"),
19915 name == NULL ? (char_u *)_("Unknown") : name);
19916 return TRUE;
19917 }
19918 return FALSE;
19919}
19920
19921/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019922 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019923 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019924 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019925 * It is OK for "from" and "to" to point to the same item. This is used to
19926 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019927 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019928 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019929copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019930 typval_T *from;
19931 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019933 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019934 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019935 switch (from->v_type)
19936 {
19937 case VAR_NUMBER:
19938 to->vval.v_number = from->vval.v_number;
19939 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019940#ifdef FEAT_FLOAT
19941 case VAR_FLOAT:
19942 to->vval.v_float = from->vval.v_float;
19943 break;
19944#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019945 case VAR_STRING:
19946 case VAR_FUNC:
19947 if (from->vval.v_string == NULL)
19948 to->vval.v_string = NULL;
19949 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019950 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019951 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019952 if (from->v_type == VAR_FUNC)
19953 func_ref(to->vval.v_string);
19954 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019955 break;
19956 case VAR_LIST:
19957 if (from->vval.v_list == NULL)
19958 to->vval.v_list = NULL;
19959 else
19960 {
19961 to->vval.v_list = from->vval.v_list;
19962 ++to->vval.v_list->lv_refcount;
19963 }
19964 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019965 case VAR_DICT:
19966 if (from->vval.v_dict == NULL)
19967 to->vval.v_dict = NULL;
19968 else
19969 {
19970 to->vval.v_dict = from->vval.v_dict;
19971 ++to->vval.v_dict->dv_refcount;
19972 }
19973 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019974 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019975 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019976 break;
19977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978}
19979
19980/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019981 * Make a copy of an item.
19982 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019983 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19984 * reference to an already copied list/dict can be used.
19985 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019986 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019987 static int
19988item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019989 typval_T *from;
19990 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019991 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019992 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019993{
19994 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019995 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019996
Bram Moolenaar33570922005-01-25 22:26:29 +000019997 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019998 {
19999 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020000 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020001 }
20002 ++recurse;
20003
20004 switch (from->v_type)
20005 {
20006 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020007#ifdef FEAT_FLOAT
20008 case VAR_FLOAT:
20009#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020010 case VAR_STRING:
20011 case VAR_FUNC:
20012 copy_tv(from, to);
20013 break;
20014 case VAR_LIST:
20015 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020016 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020017 if (from->vval.v_list == NULL)
20018 to->vval.v_list = NULL;
20019 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20020 {
20021 /* use the copy made earlier */
20022 to->vval.v_list = from->vval.v_list->lv_copylist;
20023 ++to->vval.v_list->lv_refcount;
20024 }
20025 else
20026 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20027 if (to->vval.v_list == NULL)
20028 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020029 break;
20030 case VAR_DICT:
20031 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020032 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020033 if (from->vval.v_dict == NULL)
20034 to->vval.v_dict = NULL;
20035 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20036 {
20037 /* use the copy made earlier */
20038 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20039 ++to->vval.v_dict->dv_refcount;
20040 }
20041 else
20042 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20043 if (to->vval.v_dict == NULL)
20044 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020045 break;
20046 default:
20047 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020048 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020049 }
20050 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020051 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020052}
20053
20054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 * ":echo expr1 ..." print each argument separated with a space, add a
20056 * newline at the end.
20057 * ":echon expr1 ..." print each argument plain.
20058 */
20059 void
20060ex_echo(eap)
20061 exarg_T *eap;
20062{
20063 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020064 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020065 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066 char_u *p;
20067 int needclr = TRUE;
20068 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020069 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020070
20071 if (eap->skip)
20072 ++emsg_skip;
20073 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20074 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020075 /* If eval1() causes an error message the text from the command may
20076 * still need to be cleared. E.g., "echo 22,44". */
20077 need_clr_eos = needclr;
20078
Bram Moolenaar071d4272004-06-13 20:20:40 +000020079 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020080 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 {
20082 /*
20083 * Report the invalid expression unless the expression evaluation
20084 * has been cancelled due to an aborting error, an interrupt, or an
20085 * exception.
20086 */
20087 if (!aborting())
20088 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020089 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090 break;
20091 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020092 need_clr_eos = FALSE;
20093
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 if (!eap->skip)
20095 {
20096 if (atstart)
20097 {
20098 atstart = FALSE;
20099 /* Call msg_start() after eval1(), evaluating the expression
20100 * may cause a message to appear. */
20101 if (eap->cmdidx == CMD_echo)
20102 msg_start();
20103 }
20104 else if (eap->cmdidx == CMD_echo)
20105 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020106 current_copyID += COPYID_INC;
20107 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020108 if (p != NULL)
20109 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020111 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020113 if (*p != TAB && needclr)
20114 {
20115 /* remove any text still there from the command */
20116 msg_clr_eos();
20117 needclr = FALSE;
20118 }
20119 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020120 }
20121 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020122 {
20123#ifdef FEAT_MBYTE
20124 if (has_mbyte)
20125 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020126 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020127
20128 (void)msg_outtrans_len_attr(p, i, echo_attr);
20129 p += i - 1;
20130 }
20131 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020133 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020136 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020138 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 arg = skipwhite(arg);
20140 }
20141 eap->nextcmd = check_nextcmd(arg);
20142
20143 if (eap->skip)
20144 --emsg_skip;
20145 else
20146 {
20147 /* remove text that may still be there from the command */
20148 if (needclr)
20149 msg_clr_eos();
20150 if (eap->cmdidx == CMD_echo)
20151 msg_end();
20152 }
20153}
20154
20155/*
20156 * ":echohl {name}".
20157 */
20158 void
20159ex_echohl(eap)
20160 exarg_T *eap;
20161{
20162 int id;
20163
20164 id = syn_name2id(eap->arg);
20165 if (id == 0)
20166 echo_attr = 0;
20167 else
20168 echo_attr = syn_id2attr(id);
20169}
20170
20171/*
20172 * ":execute expr1 ..." execute the result of an expression.
20173 * ":echomsg expr1 ..." Print a message
20174 * ":echoerr expr1 ..." Print an error
20175 * Each gets spaces around each argument and a newline at the end for
20176 * echo commands
20177 */
20178 void
20179ex_execute(eap)
20180 exarg_T *eap;
20181{
20182 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020183 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 int ret = OK;
20185 char_u *p;
20186 garray_T ga;
20187 int len;
20188 int save_did_emsg;
20189
20190 ga_init2(&ga, 1, 80);
20191
20192 if (eap->skip)
20193 ++emsg_skip;
20194 while (*arg != NUL && *arg != '|' && *arg != '\n')
20195 {
20196 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020197 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020198 {
20199 /*
20200 * Report the invalid expression unless the expression evaluation
20201 * has been cancelled due to an aborting error, an interrupt, or an
20202 * exception.
20203 */
20204 if (!aborting())
20205 EMSG2(_(e_invexpr2), p);
20206 ret = FAIL;
20207 break;
20208 }
20209
20210 if (!eap->skip)
20211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020212 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 len = (int)STRLEN(p);
20214 if (ga_grow(&ga, len + 2) == FAIL)
20215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020216 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217 ret = FAIL;
20218 break;
20219 }
20220 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 ga.ga_len += len;
20224 }
20225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020226 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 arg = skipwhite(arg);
20228 }
20229
20230 if (ret != FAIL && ga.ga_data != NULL)
20231 {
20232 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020235 out_flush();
20236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020237 else if (eap->cmdidx == CMD_echoerr)
20238 {
20239 /* We don't want to abort following commands, restore did_emsg. */
20240 save_did_emsg = did_emsg;
20241 EMSG((char_u *)ga.ga_data);
20242 if (!force_abort)
20243 did_emsg = save_did_emsg;
20244 }
20245 else if (eap->cmdidx == CMD_execute)
20246 do_cmdline((char_u *)ga.ga_data,
20247 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20248 }
20249
20250 ga_clear(&ga);
20251
20252 if (eap->skip)
20253 --emsg_skip;
20254
20255 eap->nextcmd = check_nextcmd(arg);
20256}
20257
20258/*
20259 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20260 * "arg" points to the "&" or '+' when called, to "option" when returning.
20261 * Returns NULL when no option name found. Otherwise pointer to the char
20262 * after the option name.
20263 */
20264 static char_u *
20265find_option_end(arg, opt_flags)
20266 char_u **arg;
20267 int *opt_flags;
20268{
20269 char_u *p = *arg;
20270
20271 ++p;
20272 if (*p == 'g' && p[1] == ':')
20273 {
20274 *opt_flags = OPT_GLOBAL;
20275 p += 2;
20276 }
20277 else if (*p == 'l' && p[1] == ':')
20278 {
20279 *opt_flags = OPT_LOCAL;
20280 p += 2;
20281 }
20282 else
20283 *opt_flags = 0;
20284
20285 if (!ASCII_ISALPHA(*p))
20286 return NULL;
20287 *arg = p;
20288
20289 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20290 p += 4; /* termcap option */
20291 else
20292 while (ASCII_ISALPHA(*p))
20293 ++p;
20294 return p;
20295}
20296
20297/*
20298 * ":function"
20299 */
20300 void
20301ex_function(eap)
20302 exarg_T *eap;
20303{
20304 char_u *theline;
20305 int j;
20306 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 char_u *name = NULL;
20309 char_u *p;
20310 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020311 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 garray_T newargs;
20313 garray_T newlines;
20314 int varargs = FALSE;
20315 int mustend = FALSE;
20316 int flags = 0;
20317 ufunc_T *fp;
20318 int indent;
20319 int nesting;
20320 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020321 dictitem_T *v;
20322 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020323 static int func_nr = 0; /* number for nameless function */
20324 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020325 hashtab_T *ht;
20326 int todo;
20327 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020328 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329
20330 /*
20331 * ":function" without argument: list functions.
20332 */
20333 if (ends_excmd(*eap->arg))
20334 {
20335 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020336 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020337 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020338 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020339 {
20340 if (!HASHITEM_EMPTY(hi))
20341 {
20342 --todo;
20343 fp = HI2UF(hi);
20344 if (!isdigit(*fp->uf_name))
20345 list_func_head(fp, FALSE);
20346 }
20347 }
20348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349 eap->nextcmd = check_nextcmd(eap->arg);
20350 return;
20351 }
20352
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020353 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020354 * ":function /pat": list functions matching pattern.
20355 */
20356 if (*eap->arg == '/')
20357 {
20358 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20359 if (!eap->skip)
20360 {
20361 regmatch_T regmatch;
20362
20363 c = *p;
20364 *p = NUL;
20365 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20366 *p = c;
20367 if (regmatch.regprog != NULL)
20368 {
20369 regmatch.rm_ic = p_ic;
20370
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020371 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020372 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20373 {
20374 if (!HASHITEM_EMPTY(hi))
20375 {
20376 --todo;
20377 fp = HI2UF(hi);
20378 if (!isdigit(*fp->uf_name)
20379 && vim_regexec(&regmatch, fp->uf_name, 0))
20380 list_func_head(fp, FALSE);
20381 }
20382 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020383 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020384 }
20385 }
20386 if (*p == '/')
20387 ++p;
20388 eap->nextcmd = check_nextcmd(p);
20389 return;
20390 }
20391
20392 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020393 * Get the function name. There are these situations:
20394 * func normal function name
20395 * "name" == func, "fudi.fd_dict" == NULL
20396 * dict.func new dictionary entry
20397 * "name" == NULL, "fudi.fd_dict" set,
20398 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20399 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020400 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020401 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20402 * dict.func existing dict entry that's not a Funcref
20403 * "name" == NULL, "fudi.fd_dict" set,
20404 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020407 name = trans_function_name(&p, eap->skip, 0, &fudi);
20408 paren = (vim_strchr(p, '(') != NULL);
20409 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020410 {
20411 /*
20412 * Return on an invalid expression in braces, unless the expression
20413 * evaluation has been cancelled due to an aborting error, an
20414 * interrupt, or an exception.
20415 */
20416 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020417 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020418 if (!eap->skip && fudi.fd_newkey != NULL)
20419 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020420 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 else
20424 eap->skip = TRUE;
20425 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020426
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 /* An error in a function call during evaluation of an expression in magic
20428 * braces should not cause the function not to be defined. */
20429 saved_did_emsg = did_emsg;
20430 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020431
20432 /*
20433 * ":function func" with only function name: list function.
20434 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020435 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020436 {
20437 if (!ends_excmd(*skipwhite(p)))
20438 {
20439 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020440 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020441 }
20442 eap->nextcmd = check_nextcmd(p);
20443 if (eap->nextcmd != NULL)
20444 *p = NUL;
20445 if (!eap->skip && !got_int)
20446 {
20447 fp = find_func(name);
20448 if (fp != NULL)
20449 {
20450 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020451 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020452 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020453 if (FUNCLINE(fp, j) == NULL)
20454 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 msg_putchar('\n');
20456 msg_outnum((long)(j + 1));
20457 if (j < 9)
20458 msg_putchar(' ');
20459 if (j < 99)
20460 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020461 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 out_flush(); /* show a line at a time */
20463 ui_breakcheck();
20464 }
20465 if (!got_int)
20466 {
20467 msg_putchar('\n');
20468 msg_puts((char_u *)" endfunction");
20469 }
20470 }
20471 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020472 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020474 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 }
20476
20477 /*
20478 * ":function name(arg1, arg2)" Define function.
20479 */
20480 p = skipwhite(p);
20481 if (*p != '(')
20482 {
20483 if (!eap->skip)
20484 {
20485 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020486 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487 }
20488 /* attempt to continue by skipping some text */
20489 if (vim_strchr(p, '(') != NULL)
20490 p = vim_strchr(p, '(');
20491 }
20492 p = skipwhite(p + 1);
20493
20494 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20495 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20496
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020497 if (!eap->skip)
20498 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020499 /* Check the name of the function. Unless it's a dictionary function
20500 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020501 if (name != NULL)
20502 arg = name;
20503 else
20504 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020505 if (arg != NULL && (fudi.fd_di == NULL
20506 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020507 {
20508 if (*arg == K_SPECIAL)
20509 j = 3;
20510 else
20511 j = 0;
20512 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20513 : eval_isnamec(arg[j])))
20514 ++j;
20515 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020516 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020517 }
20518 }
20519
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520 /*
20521 * Isolate the arguments: "arg1, arg2, ...)"
20522 */
20523 while (*p != ')')
20524 {
20525 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20526 {
20527 varargs = TRUE;
20528 p += 3;
20529 mustend = TRUE;
20530 }
20531 else
20532 {
20533 arg = p;
20534 while (ASCII_ISALNUM(*p) || *p == '_')
20535 ++p;
20536 if (arg == p || isdigit(*arg)
20537 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20538 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20539 {
20540 if (!eap->skip)
20541 EMSG2(_("E125: Illegal argument: %s"), arg);
20542 break;
20543 }
20544 if (ga_grow(&newargs, 1) == FAIL)
20545 goto erret;
20546 c = *p;
20547 *p = NUL;
20548 arg = vim_strsave(arg);
20549 if (arg == NULL)
20550 goto erret;
20551 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20552 *p = c;
20553 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 if (*p == ',')
20555 ++p;
20556 else
20557 mustend = TRUE;
20558 }
20559 p = skipwhite(p);
20560 if (mustend && *p != ')')
20561 {
20562 if (!eap->skip)
20563 EMSG2(_(e_invarg2), eap->arg);
20564 break;
20565 }
20566 }
20567 ++p; /* skip the ')' */
20568
Bram Moolenaare9a41262005-01-15 22:18:47 +000020569 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 for (;;)
20571 {
20572 p = skipwhite(p);
20573 if (STRNCMP(p, "range", 5) == 0)
20574 {
20575 flags |= FC_RANGE;
20576 p += 5;
20577 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020578 else if (STRNCMP(p, "dict", 4) == 0)
20579 {
20580 flags |= FC_DICT;
20581 p += 4;
20582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583 else if (STRNCMP(p, "abort", 5) == 0)
20584 {
20585 flags |= FC_ABORT;
20586 p += 5;
20587 }
20588 else
20589 break;
20590 }
20591
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020592 /* When there is a line break use what follows for the function body.
20593 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20594 if (*p == '\n')
20595 line_arg = p + 1;
20596 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 EMSG(_(e_trailing));
20598
20599 /*
20600 * Read the body of the function, until ":endfunction" is found.
20601 */
20602 if (KeyTyped)
20603 {
20604 /* Check if the function already exists, don't let the user type the
20605 * whole function before telling him it doesn't work! For a script we
20606 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020607 if (!eap->skip && !eap->forceit)
20608 {
20609 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20610 EMSG(_(e_funcdict));
20611 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020612 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020615 if (!eap->skip && did_emsg)
20616 goto erret;
20617
Bram Moolenaar071d4272004-06-13 20:20:40 +000020618 msg_putchar('\n'); /* don't overwrite the function name */
20619 cmdline_row = msg_row;
20620 }
20621
20622 indent = 2;
20623 nesting = 0;
20624 for (;;)
20625 {
20626 msg_scroll = TRUE;
20627 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020628 sourcing_lnum_off = sourcing_lnum;
20629
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020630 if (line_arg != NULL)
20631 {
20632 /* Use eap->arg, split up in parts by line breaks. */
20633 theline = line_arg;
20634 p = vim_strchr(theline, '\n');
20635 if (p == NULL)
20636 line_arg += STRLEN(line_arg);
20637 else
20638 {
20639 *p = NUL;
20640 line_arg = p + 1;
20641 }
20642 }
20643 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020644 theline = getcmdline(':', 0L, indent);
20645 else
20646 theline = eap->getline(':', eap->cookie, indent);
20647 if (KeyTyped)
20648 lines_left = Rows - 1;
20649 if (theline == NULL)
20650 {
20651 EMSG(_("E126: Missing :endfunction"));
20652 goto erret;
20653 }
20654
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020655 /* Detect line continuation: sourcing_lnum increased more than one. */
20656 if (sourcing_lnum > sourcing_lnum_off + 1)
20657 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20658 else
20659 sourcing_lnum_off = 0;
20660
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 if (skip_until != NULL)
20662 {
20663 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20664 * don't check for ":endfunc". */
20665 if (STRCMP(theline, skip_until) == 0)
20666 {
20667 vim_free(skip_until);
20668 skip_until = NULL;
20669 }
20670 }
20671 else
20672 {
20673 /* skip ':' and blanks*/
20674 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20675 ;
20676
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020677 /* Check for "endfunction". */
20678 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020679 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020680 if (line_arg == NULL)
20681 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682 break;
20683 }
20684
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020685 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 * at "end". */
20687 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20688 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020689 else if (STRNCMP(p, "if", 2) == 0
20690 || STRNCMP(p, "wh", 2) == 0
20691 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 || STRNCMP(p, "try", 3) == 0)
20693 indent += 2;
20694
20695 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020696 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020697 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020698 if (*p == '!')
20699 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020700 p += eval_fname_script(p);
20701 if (ASCII_ISALPHA(*p))
20702 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020703 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 if (*skipwhite(p) == '(')
20705 {
20706 ++nesting;
20707 indent += 2;
20708 }
20709 }
20710 }
20711
20712 /* Check for ":append" or ":insert". */
20713 p = skip_range(p, NULL);
20714 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20715 || (p[0] == 'i'
20716 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20717 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20718 skip_until = vim_strsave((char_u *)".");
20719
20720 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20721 arg = skipwhite(skiptowhite(p));
20722 if (arg[0] == '<' && arg[1] =='<'
20723 && ((p[0] == 'p' && p[1] == 'y'
20724 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20725 || (p[0] == 'p' && p[1] == 'e'
20726 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20727 || (p[0] == 't' && p[1] == 'c'
20728 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20729 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20730 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020731 || (p[0] == 'm' && p[1] == 'z'
20732 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733 ))
20734 {
20735 /* ":python <<" continues until a dot, like ":append" */
20736 p = skipwhite(arg + 2);
20737 if (*p == NUL)
20738 skip_until = vim_strsave((char_u *)".");
20739 else
20740 skip_until = vim_strsave(p);
20741 }
20742 }
20743
20744 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020745 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020746 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020747 if (line_arg == NULL)
20748 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020749 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020750 }
20751
20752 /* Copy the line to newly allocated memory. get_one_sourceline()
20753 * allocates 250 bytes per line, this saves 80% on average. The cost
20754 * is an extra alloc/free. */
20755 p = vim_strsave(theline);
20756 if (p != NULL)
20757 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020758 if (line_arg == NULL)
20759 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020760 theline = p;
20761 }
20762
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020763 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20764
20765 /* Add NULL lines for continuation lines, so that the line count is
20766 * equal to the index in the growarray. */
20767 while (sourcing_lnum_off-- > 0)
20768 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020769
20770 /* Check for end of eap->arg. */
20771 if (line_arg != NULL && *line_arg == NUL)
20772 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 }
20774
20775 /* Don't define the function when skipping commands or when an error was
20776 * detected. */
20777 if (eap->skip || did_emsg)
20778 goto erret;
20779
20780 /*
20781 * If there are no errors, add the function
20782 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020783 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020784 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020785 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020786 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020787 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020788 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020789 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020790 goto erret;
20791 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020792
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020793 fp = find_func(name);
20794 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020796 if (!eap->forceit)
20797 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020798 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020799 goto erret;
20800 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020801 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020802 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020803 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020804 name);
20805 goto erret;
20806 }
20807 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020808 ga_clear_strings(&(fp->uf_args));
20809 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020810 vim_free(name);
20811 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 }
20814 else
20815 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020816 char numbuf[20];
20817
20818 fp = NULL;
20819 if (fudi.fd_newkey == NULL && !eap->forceit)
20820 {
20821 EMSG(_(e_funcdict));
20822 goto erret;
20823 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020824 if (fudi.fd_di == NULL)
20825 {
20826 /* Can't add a function to a locked dictionary */
20827 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20828 goto erret;
20829 }
20830 /* Can't change an existing function if it is locked */
20831 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20832 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020833
20834 /* Give the function a sequential number. Can only be used with a
20835 * Funcref! */
20836 vim_free(name);
20837 sprintf(numbuf, "%d", ++func_nr);
20838 name = vim_strsave((char_u *)numbuf);
20839 if (name == NULL)
20840 goto erret;
20841 }
20842
20843 if (fp == NULL)
20844 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020845 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020846 {
20847 int slen, plen;
20848 char_u *scriptname;
20849
20850 /* Check that the autoload name matches the script name. */
20851 j = FAIL;
20852 if (sourcing_name != NULL)
20853 {
20854 scriptname = autoload_name(name);
20855 if (scriptname != NULL)
20856 {
20857 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020858 plen = (int)STRLEN(p);
20859 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020860 if (slen > plen && fnamecmp(p,
20861 sourcing_name + slen - plen) == 0)
20862 j = OK;
20863 vim_free(scriptname);
20864 }
20865 }
20866 if (j == FAIL)
20867 {
20868 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20869 goto erret;
20870 }
20871 }
20872
20873 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020874 if (fp == NULL)
20875 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020876
20877 if (fudi.fd_dict != NULL)
20878 {
20879 if (fudi.fd_di == NULL)
20880 {
20881 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020882 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020883 if (fudi.fd_di == NULL)
20884 {
20885 vim_free(fp);
20886 goto erret;
20887 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020888 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20889 {
20890 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020891 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020892 goto erret;
20893 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020894 }
20895 else
20896 /* overwrite existing dict entry */
20897 clear_tv(&fudi.fd_di->di_tv);
20898 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020899 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020900 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020901 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020902
20903 /* behave like "dict" was used */
20904 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020905 }
20906
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020908 STRCPY(fp->uf_name, name);
20909 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020911 fp->uf_args = newargs;
20912 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020913#ifdef FEAT_PROFILE
20914 fp->uf_tml_count = NULL;
20915 fp->uf_tml_total = NULL;
20916 fp->uf_tml_self = NULL;
20917 fp->uf_profiling = FALSE;
20918 if (prof_def_func())
20919 func_do_profile(fp);
20920#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020921 fp->uf_varargs = varargs;
20922 fp->uf_flags = flags;
20923 fp->uf_calls = 0;
20924 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020925 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926
20927erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 ga_clear_strings(&newargs);
20929 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020930ret_free:
20931 vim_free(skip_until);
20932 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935}
20936
20937/*
20938 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020939 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020941 * flags:
20942 * TFN_INT: internal function name OK
20943 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020944 * Advances "pp" to just after the function name (if no error).
20945 */
20946 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020947trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948 char_u **pp;
20949 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020950 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020951 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020953 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 char_u *start;
20955 char_u *end;
20956 int lead;
20957 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020959 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020960
20961 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020962 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020964
20965 /* Check for hard coded <SNR>: already translated function ID (from a user
20966 * command). */
20967 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20968 && (*pp)[2] == (int)KE_SNR)
20969 {
20970 *pp += 3;
20971 len = get_id_len(pp) + 3;
20972 return vim_strnsave(start, len);
20973 }
20974
20975 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20976 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020978 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020980
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020981 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20982 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020983 if (end == start)
20984 {
20985 if (!skip)
20986 EMSG(_("E129: Function name required"));
20987 goto theend;
20988 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020989 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020990 {
20991 /*
20992 * Report an invalid expression in braces, unless the expression
20993 * evaluation has been cancelled due to an aborting error, an
20994 * interrupt, or an exception.
20995 */
20996 if (!aborting())
20997 {
20998 if (end != NULL)
20999 EMSG2(_(e_invarg2), start);
21000 }
21001 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021002 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021003 goto theend;
21004 }
21005
21006 if (lv.ll_tv != NULL)
21007 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021008 if (fdp != NULL)
21009 {
21010 fdp->fd_dict = lv.ll_dict;
21011 fdp->fd_newkey = lv.ll_newkey;
21012 lv.ll_newkey = NULL;
21013 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021014 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021015 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21016 {
21017 name = vim_strsave(lv.ll_tv->vval.v_string);
21018 *pp = end;
21019 }
21020 else
21021 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021022 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21023 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021024 EMSG(_(e_funcref));
21025 else
21026 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021027 name = NULL;
21028 }
21029 goto theend;
21030 }
21031
21032 if (lv.ll_name == NULL)
21033 {
21034 /* Error found, but continue after the function name. */
21035 *pp = end;
21036 goto theend;
21037 }
21038
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021039 /* Check if the name is a Funcref. If so, use the value. */
21040 if (lv.ll_exp_name != NULL)
21041 {
21042 len = (int)STRLEN(lv.ll_exp_name);
21043 name = deref_func_name(lv.ll_exp_name, &len);
21044 if (name == lv.ll_exp_name)
21045 name = NULL;
21046 }
21047 else
21048 {
21049 len = (int)(end - *pp);
21050 name = deref_func_name(*pp, &len);
21051 if (name == *pp)
21052 name = NULL;
21053 }
21054 if (name != NULL)
21055 {
21056 name = vim_strsave(name);
21057 *pp = end;
21058 goto theend;
21059 }
21060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021061 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021062 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021063 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021064 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21065 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21066 {
21067 /* When there was "s:" already or the name expanded to get a
21068 * leading "s:" then remove it. */
21069 lv.ll_name += 2;
21070 len -= 2;
21071 lead = 2;
21072 }
21073 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021074 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021075 {
21076 if (lead == 2) /* skip over "s:" */
21077 lv.ll_name += 2;
21078 len = (int)(end - lv.ll_name);
21079 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021080
21081 /*
21082 * Copy the function name to allocated memory.
21083 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21084 * Accept <SNR>123_name() outside a script.
21085 */
21086 if (skip)
21087 lead = 0; /* do nothing */
21088 else if (lead > 0)
21089 {
21090 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021091 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21092 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021093 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021094 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021095 if (current_SID <= 0)
21096 {
21097 EMSG(_(e_usingsid));
21098 goto theend;
21099 }
21100 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21101 lead += (int)STRLEN(sid_buf);
21102 }
21103 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021104 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021105 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021106 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021107 goto theend;
21108 }
21109 name = alloc((unsigned)(len + lead + 1));
21110 if (name != NULL)
21111 {
21112 if (lead > 0)
21113 {
21114 name[0] = K_SPECIAL;
21115 name[1] = KS_EXTRA;
21116 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021117 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021118 STRCPY(name + 3, sid_buf);
21119 }
21120 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21121 name[len + lead] = NUL;
21122 }
21123 *pp = end;
21124
21125theend:
21126 clear_lval(&lv);
21127 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128}
21129
21130/*
21131 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21132 * Return 2 if "p" starts with "s:".
21133 * Return 0 otherwise.
21134 */
21135 static int
21136eval_fname_script(p)
21137 char_u *p;
21138{
21139 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21140 || STRNICMP(p + 1, "SNR>", 4) == 0))
21141 return 5;
21142 if (p[0] == 's' && p[1] == ':')
21143 return 2;
21144 return 0;
21145}
21146
21147/*
21148 * Return TRUE if "p" starts with "<SID>" or "s:".
21149 * Only works if eval_fname_script() returned non-zero for "p"!
21150 */
21151 static int
21152eval_fname_sid(p)
21153 char_u *p;
21154{
21155 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21156}
21157
21158/*
21159 * List the head of the function: "name(arg1, arg2)".
21160 */
21161 static void
21162list_func_head(fp, indent)
21163 ufunc_T *fp;
21164 int indent;
21165{
21166 int j;
21167
21168 msg_start();
21169 if (indent)
21170 MSG_PUTS(" ");
21171 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021172 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 {
21174 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021175 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176 }
21177 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021178 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021180 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 {
21182 if (j)
21183 MSG_PUTS(", ");
21184 msg_puts(FUNCARG(fp, j));
21185 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021186 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021187 {
21188 if (j)
21189 MSG_PUTS(", ");
21190 MSG_PUTS("...");
21191 }
21192 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021193 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021194 if (p_verbose > 0)
21195 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021196}
21197
21198/*
21199 * Find a function by name, return pointer to it in ufuncs.
21200 * Return NULL for unknown function.
21201 */
21202 static ufunc_T *
21203find_func(name)
21204 char_u *name;
21205{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021206 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021208 hi = hash_find(&func_hashtab, name);
21209 if (!HASHITEM_EMPTY(hi))
21210 return HI2UF(hi);
21211 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212}
21213
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021214#if defined(EXITFREE) || defined(PROTO)
21215 void
21216free_all_functions()
21217{
21218 hashitem_T *hi;
21219
21220 /* Need to start all over every time, because func_free() may change the
21221 * hash table. */
21222 while (func_hashtab.ht_used > 0)
21223 for (hi = func_hashtab.ht_array; ; ++hi)
21224 if (!HASHITEM_EMPTY(hi))
21225 {
21226 func_free(HI2UF(hi));
21227 break;
21228 }
21229}
21230#endif
21231
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021232/*
21233 * Return TRUE if a function "name" exists.
21234 */
21235 static int
21236function_exists(name)
21237 char_u *name;
21238{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021239 char_u *nm = name;
21240 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021241 int n = FALSE;
21242
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021243 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021244 nm = skipwhite(nm);
21245
21246 /* Only accept "funcname", "funcname ", "funcname (..." and
21247 * "funcname(...", not "funcname!...". */
21248 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021249 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021250 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021251 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021252 else
21253 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021254 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021255 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021256 return n;
21257}
21258
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021259/*
21260 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021261 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021262 */
21263 static int
21264builtin_function(name)
21265 char_u *name;
21266{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021267 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21268 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021269}
21270
Bram Moolenaar05159a02005-02-26 23:04:13 +000021271#if defined(FEAT_PROFILE) || defined(PROTO)
21272/*
21273 * Start profiling function "fp".
21274 */
21275 static void
21276func_do_profile(fp)
21277 ufunc_T *fp;
21278{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021279 int len = fp->uf_lines.ga_len;
21280
21281 if (len == 0)
21282 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021283 fp->uf_tm_count = 0;
21284 profile_zero(&fp->uf_tm_self);
21285 profile_zero(&fp->uf_tm_total);
21286 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021287 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021288 if (fp->uf_tml_total == NULL)
21289 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021290 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021291 if (fp->uf_tml_self == NULL)
21292 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021293 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021294 fp->uf_tml_idx = -1;
21295 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21296 || fp->uf_tml_self == NULL)
21297 return; /* out of memory */
21298
21299 fp->uf_profiling = TRUE;
21300}
21301
21302/*
21303 * Dump the profiling results for all functions in file "fd".
21304 */
21305 void
21306func_dump_profile(fd)
21307 FILE *fd;
21308{
21309 hashitem_T *hi;
21310 int todo;
21311 ufunc_T *fp;
21312 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021313 ufunc_T **sorttab;
21314 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021316 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021317 if (todo == 0)
21318 return; /* nothing to dump */
21319
Bram Moolenaar73830342005-02-28 22:48:19 +000021320 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21321
Bram Moolenaar05159a02005-02-26 23:04:13 +000021322 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21323 {
21324 if (!HASHITEM_EMPTY(hi))
21325 {
21326 --todo;
21327 fp = HI2UF(hi);
21328 if (fp->uf_profiling)
21329 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021330 if (sorttab != NULL)
21331 sorttab[st_len++] = fp;
21332
Bram Moolenaar05159a02005-02-26 23:04:13 +000021333 if (fp->uf_name[0] == K_SPECIAL)
21334 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21335 else
21336 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21337 if (fp->uf_tm_count == 1)
21338 fprintf(fd, "Called 1 time\n");
21339 else
21340 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21341 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21342 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21343 fprintf(fd, "\n");
21344 fprintf(fd, "count total (s) self (s)\n");
21345
21346 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21347 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021348 if (FUNCLINE(fp, i) == NULL)
21349 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021350 prof_func_line(fd, fp->uf_tml_count[i],
21351 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021352 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21353 }
21354 fprintf(fd, "\n");
21355 }
21356 }
21357 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021358
21359 if (sorttab != NULL && st_len > 0)
21360 {
21361 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21362 prof_total_cmp);
21363 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21364 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21365 prof_self_cmp);
21366 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21367 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021368
21369 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021370}
Bram Moolenaar73830342005-02-28 22:48:19 +000021371
21372 static void
21373prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21374 FILE *fd;
21375 ufunc_T **sorttab;
21376 int st_len;
21377 char *title;
21378 int prefer_self; /* when equal print only self time */
21379{
21380 int i;
21381 ufunc_T *fp;
21382
21383 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21384 fprintf(fd, "count total (s) self (s) function\n");
21385 for (i = 0; i < 20 && i < st_len; ++i)
21386 {
21387 fp = sorttab[i];
21388 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21389 prefer_self);
21390 if (fp->uf_name[0] == K_SPECIAL)
21391 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21392 else
21393 fprintf(fd, " %s()\n", fp->uf_name);
21394 }
21395 fprintf(fd, "\n");
21396}
21397
21398/*
21399 * Print the count and times for one function or function line.
21400 */
21401 static void
21402prof_func_line(fd, count, total, self, prefer_self)
21403 FILE *fd;
21404 int count;
21405 proftime_T *total;
21406 proftime_T *self;
21407 int prefer_self; /* when equal print only self time */
21408{
21409 if (count > 0)
21410 {
21411 fprintf(fd, "%5d ", count);
21412 if (prefer_self && profile_equal(total, self))
21413 fprintf(fd, " ");
21414 else
21415 fprintf(fd, "%s ", profile_msg(total));
21416 if (!prefer_self && profile_equal(total, self))
21417 fprintf(fd, " ");
21418 else
21419 fprintf(fd, "%s ", profile_msg(self));
21420 }
21421 else
21422 fprintf(fd, " ");
21423}
21424
21425/*
21426 * Compare function for total time sorting.
21427 */
21428 static int
21429#ifdef __BORLANDC__
21430_RTLENTRYF
21431#endif
21432prof_total_cmp(s1, s2)
21433 const void *s1;
21434 const void *s2;
21435{
21436 ufunc_T *p1, *p2;
21437
21438 p1 = *(ufunc_T **)s1;
21439 p2 = *(ufunc_T **)s2;
21440 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21441}
21442
21443/*
21444 * Compare function for self time sorting.
21445 */
21446 static int
21447#ifdef __BORLANDC__
21448_RTLENTRYF
21449#endif
21450prof_self_cmp(s1, s2)
21451 const void *s1;
21452 const void *s2;
21453{
21454 ufunc_T *p1, *p2;
21455
21456 p1 = *(ufunc_T **)s1;
21457 p2 = *(ufunc_T **)s2;
21458 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21459}
21460
Bram Moolenaar05159a02005-02-26 23:04:13 +000021461#endif
21462
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021463/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021464 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021465 * Return TRUE if a package was loaded.
21466 */
21467 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021468script_autoload(name, reload)
21469 char_u *name;
21470 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021471{
21472 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021473 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021474 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021475 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021476
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021477 /* Return quickly when autoload disabled. */
21478 if (no_autoload)
21479 return FALSE;
21480
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021481 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021482 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021483 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021484 return FALSE;
21485
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021486 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021487
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021488 /* Find the name in the list of previously loaded package names. Skip
21489 * "autoload/", it's always the same. */
21490 for (i = 0; i < ga_loaded.ga_len; ++i)
21491 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21492 break;
21493 if (!reload && i < ga_loaded.ga_len)
21494 ret = FALSE; /* was loaded already */
21495 else
21496 {
21497 /* Remember the name if it wasn't loaded already. */
21498 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21499 {
21500 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21501 tofree = NULL;
21502 }
21503
21504 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021505 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021506 ret = TRUE;
21507 }
21508
21509 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021510 return ret;
21511}
21512
21513/*
21514 * Return the autoload script name for a function or variable name.
21515 * Returns NULL when out of memory.
21516 */
21517 static char_u *
21518autoload_name(name)
21519 char_u *name;
21520{
21521 char_u *p;
21522 char_u *scriptname;
21523
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021524 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021525 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21526 if (scriptname == NULL)
21527 return FALSE;
21528 STRCPY(scriptname, "autoload/");
21529 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021530 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021531 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021532 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021533 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021534 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021535}
21536
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21538
21539/*
21540 * Function given to ExpandGeneric() to obtain the list of user defined
21541 * function names.
21542 */
21543 char_u *
21544get_user_func_name(xp, idx)
21545 expand_T *xp;
21546 int idx;
21547{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021548 static long_u done;
21549 static hashitem_T *hi;
21550 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551
21552 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021554 done = 0;
21555 hi = func_hashtab.ht_array;
21556 }
21557 if (done < func_hashtab.ht_used)
21558 {
21559 if (done++ > 0)
21560 ++hi;
21561 while (HASHITEM_EMPTY(hi))
21562 ++hi;
21563 fp = HI2UF(hi);
21564
21565 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21566 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567
21568 cat_func_name(IObuff, fp);
21569 if (xp->xp_context != EXPAND_USER_FUNC)
21570 {
21571 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021572 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 STRCAT(IObuff, ")");
21574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 return IObuff;
21576 }
21577 return NULL;
21578}
21579
21580#endif /* FEAT_CMDL_COMPL */
21581
21582/*
21583 * Copy the function name of "fp" to buffer "buf".
21584 * "buf" must be able to hold the function name plus three bytes.
21585 * Takes care of script-local function names.
21586 */
21587 static void
21588cat_func_name(buf, fp)
21589 char_u *buf;
21590 ufunc_T *fp;
21591{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021592 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 {
21594 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021595 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 }
21597 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021598 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599}
21600
21601/*
21602 * ":delfunction {name}"
21603 */
21604 void
21605ex_delfunction(eap)
21606 exarg_T *eap;
21607{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021608 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609 char_u *p;
21610 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021611 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612
21613 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021614 name = trans_function_name(&p, eap->skip, 0, &fudi);
21615 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021617 {
21618 if (fudi.fd_dict != NULL && !eap->skip)
21619 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 if (!ends_excmd(*skipwhite(p)))
21623 {
21624 vim_free(name);
21625 EMSG(_(e_trailing));
21626 return;
21627 }
21628 eap->nextcmd = check_nextcmd(p);
21629 if (eap->nextcmd != NULL)
21630 *p = NUL;
21631
21632 if (!eap->skip)
21633 fp = find_func(name);
21634 vim_free(name);
21635
21636 if (!eap->skip)
21637 {
21638 if (fp == NULL)
21639 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021640 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 return;
21642 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021643 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 {
21645 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21646 return;
21647 }
21648
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021649 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021650 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651 /* Delete the dict item that refers to the function, it will
21652 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021653 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021655 else
21656 func_free(fp);
21657 }
21658}
21659
21660/*
21661 * Free a function and remove it from the list of functions.
21662 */
21663 static void
21664func_free(fp)
21665 ufunc_T *fp;
21666{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021667 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021668
21669 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021670 ga_clear_strings(&(fp->uf_args));
21671 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021672#ifdef FEAT_PROFILE
21673 vim_free(fp->uf_tml_count);
21674 vim_free(fp->uf_tml_total);
21675 vim_free(fp->uf_tml_self);
21676#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021677
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021678 /* remove the function from the function hashtable */
21679 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21680 if (HASHITEM_EMPTY(hi))
21681 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021682 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021683 hash_remove(&func_hashtab, hi);
21684
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021685 vim_free(fp);
21686}
21687
21688/*
21689 * Unreference a Function: decrement the reference count and free it when it
21690 * becomes zero. Only for numbered functions.
21691 */
21692 static void
21693func_unref(name)
21694 char_u *name;
21695{
21696 ufunc_T *fp;
21697
21698 if (name != NULL && isdigit(*name))
21699 {
21700 fp = find_func(name);
21701 if (fp == NULL)
21702 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021703 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021704 {
21705 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021706 * when "uf_calls" becomes zero. */
21707 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708 func_free(fp);
21709 }
21710 }
21711}
21712
21713/*
21714 * Count a reference to a Function.
21715 */
21716 static void
21717func_ref(name)
21718 char_u *name;
21719{
21720 ufunc_T *fp;
21721
21722 if (name != NULL && isdigit(*name))
21723 {
21724 fp = find_func(name);
21725 if (fp == NULL)
21726 EMSG2(_(e_intern2), "func_ref()");
21727 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021728 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 }
21730}
21731
21732/*
21733 * Call a user function.
21734 */
21735 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021736call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021737 ufunc_T *fp; /* pointer to function */
21738 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021739 typval_T *argvars; /* arguments */
21740 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021741 linenr_T firstline; /* first line of range */
21742 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021743 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744{
Bram Moolenaar33570922005-01-25 22:26:29 +000021745 char_u *save_sourcing_name;
21746 linenr_T save_sourcing_lnum;
21747 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021748 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021749 int save_did_emsg;
21750 static int depth = 0;
21751 dictitem_T *v;
21752 int fixvar_idx = 0; /* index in fixvar[] */
21753 int i;
21754 int ai;
21755 char_u numbuf[NUMBUFLEN];
21756 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021757#ifdef FEAT_PROFILE
21758 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021759 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761
21762 /* If depth of calling is getting too high, don't execute the function */
21763 if (depth >= p_mfd)
21764 {
21765 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021766 rettv->v_type = VAR_NUMBER;
21767 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 return;
21769 }
21770 ++depth;
21771
21772 line_breakcheck(); /* check for CTRL-C hit */
21773
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021774 fc = (funccall_T *)alloc(sizeof(funccall_T));
21775 fc->caller = current_funccal;
21776 current_funccal = fc;
21777 fc->func = fp;
21778 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021779 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021780 fc->linenr = 0;
21781 fc->returned = FALSE;
21782 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021784 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21785 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786
Bram Moolenaar33570922005-01-25 22:26:29 +000021787 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021788 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021789 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21790 * each argument variable and saves a lot of time.
21791 */
21792 /*
21793 * Init l: variables.
21794 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021795 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021796 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021797 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021798 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21799 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021800 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021801 name = v->di_key;
21802 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021803 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021804 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021806 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 v->di_tv.vval.v_dict = selfdict;
21808 ++selfdict->dv_refcount;
21809 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021810
Bram Moolenaar33570922005-01-25 22:26:29 +000021811 /*
21812 * Init a: variables.
21813 * Set a:0 to "argcount".
21814 * Set a:000 to a list with room for the "..." arguments.
21815 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021816 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21817 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021818 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021819 /* Use "name" to avoid a warning from some compiler that checks the
21820 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021821 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021822 name = v->di_key;
21823 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021824 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021825 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021826 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021827 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021828 v->di_tv.vval.v_list = &fc->l_varlist;
21829 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21830 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21831 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021832
21833 /*
21834 * Set a:firstline to "firstline" and a:lastline to "lastline".
21835 * Set a:name to named arguments.
21836 * Set a:N to the "..." arguments.
21837 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021838 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021839 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021840 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021841 (varnumber_T)lastline);
21842 for (i = 0; i < argcount; ++i)
21843 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021844 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 if (ai < 0)
21846 /* named argument a:name */
21847 name = FUNCARG(fp, i);
21848 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021849 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021850 /* "..." argument a:1, a:2, etc. */
21851 sprintf((char *)numbuf, "%d", ai + 1);
21852 name = numbuf;
21853 }
21854 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21855 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021856 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021857 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21858 }
21859 else
21860 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021861 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21862 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021863 if (v == NULL)
21864 break;
21865 v->di_flags = DI_FLAGS_RO;
21866 }
21867 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021868 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021869
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021870 /* Note: the values are copied directly to avoid alloc/free.
21871 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021872 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021873 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021874
21875 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21876 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021877 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21878 fc->l_listitems[ai].li_tv = argvars[i];
21879 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021880 }
21881 }
21882
Bram Moolenaar071d4272004-06-13 20:20:40 +000021883 /* Don't redraw while executing the function. */
21884 ++RedrawingDisabled;
21885 save_sourcing_name = sourcing_name;
21886 save_sourcing_lnum = sourcing_lnum;
21887 sourcing_lnum = 1;
21888 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021889 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021890 if (sourcing_name != NULL)
21891 {
21892 if (save_sourcing_name != NULL
21893 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21894 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21895 else
21896 STRCPY(sourcing_name, "function ");
21897 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21898
21899 if (p_verbose >= 12)
21900 {
21901 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021902 verbose_enter_scroll();
21903
Bram Moolenaar555b2802005-05-19 21:08:39 +000021904 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021905 if (p_verbose >= 14)
21906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021908 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021909 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021910 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021911
21912 msg_puts((char_u *)"(");
21913 for (i = 0; i < argcount; ++i)
21914 {
21915 if (i > 0)
21916 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021917 if (argvars[i].v_type == VAR_NUMBER)
21918 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021919 else
21920 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021921 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21922 if (s != NULL)
21923 {
21924 trunc_string(s, buf, MSG_BUF_CLEN);
21925 msg_puts(buf);
21926 vim_free(tofree);
21927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928 }
21929 }
21930 msg_puts((char_u *)")");
21931 }
21932 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021933
21934 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935 --no_wait_return;
21936 }
21937 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021938#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021939 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021940 {
21941 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21942 func_do_profile(fp);
21943 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021944 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021945 {
21946 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021947 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021948 profile_zero(&fp->uf_tm_children);
21949 }
21950 script_prof_save(&wait_start);
21951 }
21952#endif
21953
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021955 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 save_did_emsg = did_emsg;
21957 did_emsg = FALSE;
21958
21959 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021960 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21962
21963 --RedrawingDisabled;
21964
21965 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021966 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021968 clear_tv(rettv);
21969 rettv->v_type = VAR_NUMBER;
21970 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 }
21972
Bram Moolenaar05159a02005-02-26 23:04:13 +000021973#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021974 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021975 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021976 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021977 profile_end(&call_start);
21978 profile_sub_wait(&wait_start, &call_start);
21979 profile_add(&fp->uf_tm_total, &call_start);
21980 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021981 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021982 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021983 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21984 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021985 }
21986 }
21987#endif
21988
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 /* when being verbose, mention the return value */
21990 if (p_verbose >= 12)
21991 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021993 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021996 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021997 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021998 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021999 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022000 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022002 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022003 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022004 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022005 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022006
Bram Moolenaar555b2802005-05-19 21:08:39 +000022007 /* The value may be very long. Skip the middle part, so that we
22008 * have some idea how it starts and ends. smsg() would always
22009 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022010 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022011 if (s != NULL)
22012 {
22013 trunc_string(s, buf, MSG_BUF_CLEN);
22014 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22015 vim_free(tofree);
22016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 }
22018 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022019
22020 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 --no_wait_return;
22022 }
22023
22024 vim_free(sourcing_name);
22025 sourcing_name = save_sourcing_name;
22026 sourcing_lnum = save_sourcing_lnum;
22027 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022028#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022029 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022030 script_prof_restore(&wait_start);
22031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032
22033 if (p_verbose >= 12 && sourcing_name != NULL)
22034 {
22035 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022036 verbose_enter_scroll();
22037
Bram Moolenaar555b2802005-05-19 21:08:39 +000022038 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022039 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022040
22041 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042 --no_wait_return;
22043 }
22044
22045 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022046 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022048
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022049 /* If the a:000 list and the l: and a: dicts are not referenced we can
22050 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022051 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22052 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22053 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22054 {
22055 free_funccal(fc, FALSE);
22056 }
22057 else
22058 {
22059 hashitem_T *hi;
22060 listitem_T *li;
22061 int todo;
22062
22063 /* "fc" is still in use. This can happen when returning "a:000" or
22064 * assigning "l:" to a global variable.
22065 * Link "fc" in the list for garbage collection later. */
22066 fc->caller = previous_funccal;
22067 previous_funccal = fc;
22068
22069 /* Make a copy of the a: variables, since we didn't do that above. */
22070 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22071 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22072 {
22073 if (!HASHITEM_EMPTY(hi))
22074 {
22075 --todo;
22076 v = HI2DI(hi);
22077 copy_tv(&v->di_tv, &v->di_tv);
22078 }
22079 }
22080
22081 /* Make a copy of the a:000 items, since we didn't do that above. */
22082 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22083 copy_tv(&li->li_tv, &li->li_tv);
22084 }
22085}
22086
22087/*
22088 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022089 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022090 */
22091 static int
22092can_free_funccal(fc, copyID)
22093 funccall_T *fc;
22094 int copyID;
22095{
22096 return (fc->l_varlist.lv_copyID != copyID
22097 && fc->l_vars.dv_copyID != copyID
22098 && fc->l_avars.dv_copyID != copyID);
22099}
22100
22101/*
22102 * Free "fc" and what it contains.
22103 */
22104 static void
22105free_funccal(fc, free_val)
22106 funccall_T *fc;
22107 int free_val; /* a: vars were allocated */
22108{
22109 listitem_T *li;
22110
22111 /* The a: variables typevals may not have been allocated, only free the
22112 * allocated variables. */
22113 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22114
22115 /* free all l: variables */
22116 vars_clear(&fc->l_vars.dv_hashtab);
22117
22118 /* Free the a:000 variables if they were allocated. */
22119 if (free_val)
22120 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22121 clear_tv(&li->li_tv);
22122
22123 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124}
22125
22126/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022127 * Add a number variable "name" to dict "dp" with value "nr".
22128 */
22129 static void
22130add_nr_var(dp, v, name, nr)
22131 dict_T *dp;
22132 dictitem_T *v;
22133 char *name;
22134 varnumber_T nr;
22135{
22136 STRCPY(v->di_key, name);
22137 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22138 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22139 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022140 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022141 v->di_tv.vval.v_number = nr;
22142}
22143
22144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 * ":return [expr]"
22146 */
22147 void
22148ex_return(eap)
22149 exarg_T *eap;
22150{
22151 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022152 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153 int returning = FALSE;
22154
22155 if (current_funccal == NULL)
22156 {
22157 EMSG(_("E133: :return not inside a function"));
22158 return;
22159 }
22160
22161 if (eap->skip)
22162 ++emsg_skip;
22163
22164 eap->nextcmd = NULL;
22165 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022166 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 {
22168 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022169 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022171 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 }
22173 /* It's safer to return also on error. */
22174 else if (!eap->skip)
22175 {
22176 /*
22177 * Return unless the expression evaluation has been cancelled due to an
22178 * aborting error, an interrupt, or an exception.
22179 */
22180 if (!aborting())
22181 returning = do_return(eap, FALSE, TRUE, NULL);
22182 }
22183
22184 /* When skipping or the return gets pending, advance to the next command
22185 * in this line (!returning). Otherwise, ignore the rest of the line.
22186 * Following lines will be ignored by get_func_line(). */
22187 if (returning)
22188 eap->nextcmd = NULL;
22189 else if (eap->nextcmd == NULL) /* no argument */
22190 eap->nextcmd = check_nextcmd(arg);
22191
22192 if (eap->skip)
22193 --emsg_skip;
22194}
22195
22196/*
22197 * Return from a function. Possibly makes the return pending. Also called
22198 * for a pending return at the ":endtry" or after returning from an extra
22199 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022200 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022201 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022202 * FALSE when the return gets pending.
22203 */
22204 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022205do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 exarg_T *eap;
22207 int reanimate;
22208 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022209 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210{
22211 int idx;
22212 struct condstack *cstack = eap->cstack;
22213
22214 if (reanimate)
22215 /* Undo the return. */
22216 current_funccal->returned = FALSE;
22217
22218 /*
22219 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22220 * not in its finally clause (which then is to be executed next) is found.
22221 * In this case, make the ":return" pending for execution at the ":endtry".
22222 * Otherwise, return normally.
22223 */
22224 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22225 if (idx >= 0)
22226 {
22227 cstack->cs_pending[idx] = CSTP_RETURN;
22228
22229 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022230 /* A pending return again gets pending. "rettv" points to an
22231 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022233 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 else
22235 {
22236 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022237 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022239 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022241 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 {
22243 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022244 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022245 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 else
22247 EMSG(_(e_outofmem));
22248 }
22249 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022250 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022251
22252 if (reanimate)
22253 {
22254 /* The pending return value could be overwritten by a ":return"
22255 * without argument in a finally clause; reset the default
22256 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022257 current_funccal->rettv->v_type = VAR_NUMBER;
22258 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 }
22260 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022261 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022262 }
22263 else
22264 {
22265 current_funccal->returned = TRUE;
22266
22267 /* If the return is carried out now, store the return value. For
22268 * a return immediately after reanimation, the value is already
22269 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022270 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022271 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022272 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022273 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022275 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 }
22277 }
22278
22279 return idx < 0;
22280}
22281
22282/*
22283 * Free the variable with a pending return value.
22284 */
22285 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022286discard_pending_return(rettv)
22287 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288{
Bram Moolenaar33570922005-01-25 22:26:29 +000022289 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290}
22291
22292/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022293 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 * is an allocated string. Used by report_pending() for verbose messages.
22295 */
22296 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022297get_return_cmd(rettv)
22298 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022300 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022301 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022302 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022303
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022304 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022305 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022306 if (s == NULL)
22307 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022308
22309 STRCPY(IObuff, ":return ");
22310 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22311 if (STRLEN(s) + 8 >= IOSIZE)
22312 STRCPY(IObuff + IOSIZE - 4, "...");
22313 vim_free(tofree);
22314 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022315}
22316
22317/*
22318 * Get next function line.
22319 * Called by do_cmdline() to get the next line.
22320 * Returns allocated string, or NULL for end of function.
22321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022322 char_u *
22323get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022324 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022325 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022326 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327{
Bram Moolenaar33570922005-01-25 22:26:29 +000022328 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022329 ufunc_T *fp = fcp->func;
22330 char_u *retval;
22331 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332
22333 /* If breakpoints have been added/deleted need to check for it. */
22334 if (fcp->dbg_tick != debug_tick)
22335 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022336 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 sourcing_lnum);
22338 fcp->dbg_tick = debug_tick;
22339 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022340#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022341 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022342 func_line_end(cookie);
22343#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344
Bram Moolenaar05159a02005-02-26 23:04:13 +000022345 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022346 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22347 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 retval = NULL;
22349 else
22350 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022351 /* Skip NULL lines (continuation lines). */
22352 while (fcp->linenr < gap->ga_len
22353 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22354 ++fcp->linenr;
22355 if (fcp->linenr >= gap->ga_len)
22356 retval = NULL;
22357 else
22358 {
22359 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22360 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022361#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022362 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022363 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022364#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022366 }
22367
22368 /* Did we encounter a breakpoint? */
22369 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22370 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022371 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022373 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 sourcing_lnum);
22375 fcp->dbg_tick = debug_tick;
22376 }
22377
22378 return retval;
22379}
22380
Bram Moolenaar05159a02005-02-26 23:04:13 +000022381#if defined(FEAT_PROFILE) || defined(PROTO)
22382/*
22383 * Called when starting to read a function line.
22384 * "sourcing_lnum" must be correct!
22385 * When skipping lines it may not actually be executed, but we won't find out
22386 * until later and we need to store the time now.
22387 */
22388 void
22389func_line_start(cookie)
22390 void *cookie;
22391{
22392 funccall_T *fcp = (funccall_T *)cookie;
22393 ufunc_T *fp = fcp->func;
22394
22395 if (fp->uf_profiling && sourcing_lnum >= 1
22396 && sourcing_lnum <= fp->uf_lines.ga_len)
22397 {
22398 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022399 /* Skip continuation lines. */
22400 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22401 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022402 fp->uf_tml_execed = FALSE;
22403 profile_start(&fp->uf_tml_start);
22404 profile_zero(&fp->uf_tml_children);
22405 profile_get_wait(&fp->uf_tml_wait);
22406 }
22407}
22408
22409/*
22410 * Called when actually executing a function line.
22411 */
22412 void
22413func_line_exec(cookie)
22414 void *cookie;
22415{
22416 funccall_T *fcp = (funccall_T *)cookie;
22417 ufunc_T *fp = fcp->func;
22418
22419 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22420 fp->uf_tml_execed = TRUE;
22421}
22422
22423/*
22424 * Called when done with a function line.
22425 */
22426 void
22427func_line_end(cookie)
22428 void *cookie;
22429{
22430 funccall_T *fcp = (funccall_T *)cookie;
22431 ufunc_T *fp = fcp->func;
22432
22433 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22434 {
22435 if (fp->uf_tml_execed)
22436 {
22437 ++fp->uf_tml_count[fp->uf_tml_idx];
22438 profile_end(&fp->uf_tml_start);
22439 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022440 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022441 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22442 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022443 }
22444 fp->uf_tml_idx = -1;
22445 }
22446}
22447#endif
22448
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449/*
22450 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022451 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 */
22453 int
22454func_has_ended(cookie)
22455 void *cookie;
22456{
Bram Moolenaar33570922005-01-25 22:26:29 +000022457 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458
22459 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22460 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022461 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 || fcp->returned);
22463}
22464
22465/*
22466 * return TRUE if cookie indicates a function which "abort"s on errors.
22467 */
22468 int
22469func_has_abort(cookie)
22470 void *cookie;
22471{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022472 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473}
22474
22475#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22476typedef enum
22477{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022478 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22479 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22480 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481} var_flavour_T;
22482
22483static var_flavour_T var_flavour __ARGS((char_u *varname));
22484
22485 static var_flavour_T
22486var_flavour(varname)
22487 char_u *varname;
22488{
22489 char_u *p = varname;
22490
22491 if (ASCII_ISUPPER(*p))
22492 {
22493 while (*(++p))
22494 if (ASCII_ISLOWER(*p))
22495 return VAR_FLAVOUR_SESSION;
22496 return VAR_FLAVOUR_VIMINFO;
22497 }
22498 else
22499 return VAR_FLAVOUR_DEFAULT;
22500}
22501#endif
22502
22503#if defined(FEAT_VIMINFO) || defined(PROTO)
22504/*
22505 * Restore global vars that start with a capital from the viminfo file
22506 */
22507 int
22508read_viminfo_varlist(virp, writing)
22509 vir_T *virp;
22510 int writing;
22511{
22512 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022513 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022514 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515
22516 if (!writing && (find_viminfo_parameter('!') != NULL))
22517 {
22518 tab = vim_strchr(virp->vir_line + 1, '\t');
22519 if (tab != NULL)
22520 {
22521 *tab++ = '\0'; /* isolate the variable name */
22522 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022523 type = VAR_STRING;
22524#ifdef FEAT_FLOAT
22525 else if (*tab == 'F')
22526 type = VAR_FLOAT;
22527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528
22529 tab = vim_strchr(tab, '\t');
22530 if (tab != NULL)
22531 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022532 tv.v_type = type;
22533 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022534 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022536#ifdef FEAT_FLOAT
22537 else if (type == VAR_FLOAT)
22538 (void)string2float(tab + 1, &tv.vval.v_float);
22539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022541 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022542 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022543 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022544 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 }
22546 }
22547 }
22548
22549 return viminfo_readline(virp);
22550}
22551
22552/*
22553 * Write global vars that start with a capital to the viminfo file
22554 */
22555 void
22556write_viminfo_varlist(fp)
22557 FILE *fp;
22558{
Bram Moolenaar33570922005-01-25 22:26:29 +000022559 hashitem_T *hi;
22560 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022561 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022562 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022563 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022564 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022565 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022566
22567 if (find_viminfo_parameter('!') == NULL)
22568 return;
22569
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022570 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022571
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022572 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022573 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022575 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022577 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 this_var = HI2DI(hi);
22579 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022580 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022581 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022582 {
22583 case VAR_STRING: s = "STR"; break;
22584 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022585#ifdef FEAT_FLOAT
22586 case VAR_FLOAT: s = "FLO"; break;
22587#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022588 default: continue;
22589 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022590 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022591 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022592 if (p != NULL)
22593 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022594 vim_free(tofree);
22595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 }
22597 }
22598}
22599#endif
22600
22601#if defined(FEAT_SESSION) || defined(PROTO)
22602 int
22603store_session_globals(fd)
22604 FILE *fd;
22605{
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 hashitem_T *hi;
22607 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022608 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 char_u *p, *t;
22610
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022611 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022612 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022614 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022616 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022617 this_var = HI2DI(hi);
22618 if ((this_var->di_tv.v_type == VAR_NUMBER
22619 || this_var->di_tv.v_type == VAR_STRING)
22620 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022621 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022622 /* Escape special characters with a backslash. Turn a LF and
22623 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022624 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022625 (char_u *)"\\\"\n\r");
22626 if (p == NULL) /* out of memory */
22627 break;
22628 for (t = p; *t != NUL; ++t)
22629 if (*t == '\n')
22630 *t = 'n';
22631 else if (*t == '\r')
22632 *t = 'r';
22633 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 this_var->di_key,
22635 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22636 : ' ',
22637 p,
22638 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22639 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022640 || put_eol(fd) == FAIL)
22641 {
22642 vim_free(p);
22643 return FAIL;
22644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 vim_free(p);
22646 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022647#ifdef FEAT_FLOAT
22648 else if (this_var->di_tv.v_type == VAR_FLOAT
22649 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22650 {
22651 float_T f = this_var->di_tv.vval.v_float;
22652 int sign = ' ';
22653
22654 if (f < 0)
22655 {
22656 f = -f;
22657 sign = '-';
22658 }
22659 if ((fprintf(fd, "let %s = %c&%f",
22660 this_var->di_key, sign, f) < 0)
22661 || put_eol(fd) == FAIL)
22662 return FAIL;
22663 }
22664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 }
22666 }
22667 return OK;
22668}
22669#endif
22670
Bram Moolenaar661b1822005-07-28 22:36:45 +000022671/*
22672 * Display script name where an item was last set.
22673 * Should only be invoked when 'verbose' is non-zero.
22674 */
22675 void
22676last_set_msg(scriptID)
22677 scid_T scriptID;
22678{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022679 char_u *p;
22680
Bram Moolenaar661b1822005-07-28 22:36:45 +000022681 if (scriptID != 0)
22682 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022683 p = home_replace_save(NULL, get_scriptname(scriptID));
22684 if (p != NULL)
22685 {
22686 verbose_enter();
22687 MSG_PUTS(_("\n\tLast set from "));
22688 MSG_PUTS(p);
22689 vim_free(p);
22690 verbose_leave();
22691 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022692 }
22693}
22694
Bram Moolenaard812df62008-11-09 12:46:09 +000022695/*
22696 * List v:oldfiles in a nice way.
22697 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022698 void
22699ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022700 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022701{
22702 list_T *l = vimvars[VV_OLDFILES].vv_list;
22703 listitem_T *li;
22704 int nr = 0;
22705
22706 if (l == NULL)
22707 msg((char_u *)_("No old files"));
22708 else
22709 {
22710 msg_start();
22711 msg_scroll = TRUE;
22712 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22713 {
22714 msg_outnum((long)++nr);
22715 MSG_PUTS(": ");
22716 msg_outtrans(get_tv_string(&li->li_tv));
22717 msg_putchar('\n');
22718 out_flush(); /* output one line at a time */
22719 ui_breakcheck();
22720 }
22721 /* Assume "got_int" was set to truncate the listing. */
22722 got_int = FALSE;
22723
22724#ifdef FEAT_BROWSE_CMD
22725 if (cmdmod.browse)
22726 {
22727 quit_more = FALSE;
22728 nr = prompt_for_number(FALSE);
22729 msg_starthere();
22730 if (nr > 0)
22731 {
22732 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22733 (long)nr);
22734
22735 if (p != NULL)
22736 {
22737 p = expand_env_save(p);
22738 eap->arg = p;
22739 eap->cmdidx = CMD_edit;
22740 cmdmod.browse = FALSE;
22741 do_exedit(eap, NULL);
22742 vim_free(p);
22743 }
22744 }
22745 }
22746#endif
22747 }
22748}
22749
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750#endif /* FEAT_EVAL */
22751
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022753#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754
22755#ifdef WIN3264
22756/*
22757 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22758 */
22759static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22760static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22761static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22762
22763/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022764 * Get the short path (8.3) for the filename in "fnamep".
22765 * Only works for a valid file name.
22766 * When the path gets longer "fnamep" is changed and the allocated buffer
22767 * is put in "bufp".
22768 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22769 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 */
22771 static int
22772get_short_pathname(fnamep, bufp, fnamelen)
22773 char_u **fnamep;
22774 char_u **bufp;
22775 int *fnamelen;
22776{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022777 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 char_u *newbuf;
22779
22780 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 l = GetShortPathName(*fnamep, *fnamep, len);
22782 if (l > len - 1)
22783 {
22784 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022785 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022786 newbuf = vim_strnsave(*fnamep, l);
22787 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022788 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789
22790 vim_free(*bufp);
22791 *fnamep = *bufp = newbuf;
22792
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022793 /* Really should always succeed, as the buffer is big enough. */
22794 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 }
22796
22797 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022798 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799}
22800
22801/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022802 * Get the short path (8.3) for the filename in "fname". The converted
22803 * path is returned in "bufp".
22804 *
22805 * Some of the directories specified in "fname" may not exist. This function
22806 * will shorten the existing directories at the beginning of the path and then
22807 * append the remaining non-existing path.
22808 *
22809 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022810 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022811 * bufp - Pointer to an allocated buffer for the filename.
22812 * fnamelen - Length of the filename pointed to by fname
22813 *
22814 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815 */
22816 static int
22817shortpath_for_invalid_fname(fname, bufp, fnamelen)
22818 char_u **fname;
22819 char_u **bufp;
22820 int *fnamelen;
22821{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022822 char_u *short_fname, *save_fname, *pbuf_unused;
22823 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022825 int old_len, len;
22826 int new_len, sfx_len;
22827 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022828
22829 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022830 old_len = *fnamelen;
22831 save_fname = vim_strnsave(*fname, old_len);
22832 pbuf_unused = NULL;
22833 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022835 endp = save_fname + old_len - 1; /* Find the end of the copy */
22836 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022838 /*
22839 * Try shortening the supplied path till it succeeds by removing one
22840 * directory at a time from the tail of the path.
22841 */
22842 len = 0;
22843 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022845 /* go back one path-separator */
22846 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22847 --endp;
22848 if (endp <= save_fname)
22849 break; /* processed the complete path */
22850
22851 /*
22852 * Replace the path separator with a NUL and try to shorten the
22853 * resulting path.
22854 */
22855 ch = *endp;
22856 *endp = 0;
22857 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022858 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022859 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22860 {
22861 retval = FAIL;
22862 goto theend;
22863 }
22864 *endp = ch; /* preserve the string */
22865
22866 if (len > 0)
22867 break; /* successfully shortened the path */
22868
22869 /* failed to shorten the path. Skip the path separator */
22870 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 }
22872
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022873 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022875 /*
22876 * Succeeded in shortening the path. Now concatenate the shortened
22877 * path with the remaining path at the tail.
22878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022879
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022880 /* Compute the length of the new path. */
22881 sfx_len = (int)(save_endp - endp) + 1;
22882 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022883
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022884 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022886 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022888 /* There is not enough space in the currently allocated string,
22889 * copy it to a buffer big enough. */
22890 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022892 {
22893 retval = FAIL;
22894 goto theend;
22895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 }
22897 else
22898 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022899 /* Transfer short_fname to the main buffer (it's big enough),
22900 * unless get_short_pathname() did its work in-place. */
22901 *fname = *bufp = save_fname;
22902 if (short_fname != save_fname)
22903 vim_strncpy(save_fname, short_fname, len);
22904 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022906
22907 /* concat the not-shortened part of the path */
22908 vim_strncpy(*fname + len, endp, sfx_len);
22909 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022911
22912theend:
22913 vim_free(pbuf_unused);
22914 vim_free(save_fname);
22915
22916 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917}
22918
22919/*
22920 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022921 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 */
22923 static int
22924shortpath_for_partial(fnamep, bufp, fnamelen)
22925 char_u **fnamep;
22926 char_u **bufp;
22927 int *fnamelen;
22928{
22929 int sepcount, len, tflen;
22930 char_u *p;
22931 char_u *pbuf, *tfname;
22932 int hasTilde;
22933
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022934 /* Count up the path separators from the RHS.. so we know which part
22935 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022937 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 if (vim_ispathsep(*p))
22939 ++sepcount;
22940
22941 /* Need full path first (use expand_env() to remove a "~/") */
22942 hasTilde = (**fnamep == '~');
22943 if (hasTilde)
22944 pbuf = tfname = expand_env_save(*fnamep);
22945 else
22946 pbuf = tfname = FullName_save(*fnamep, FALSE);
22947
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022948 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022950 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22951 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952
22953 if (len == 0)
22954 {
22955 /* Don't have a valid filename, so shorten the rest of the
22956 * path if we can. This CAN give us invalid 8.3 filenames, but
22957 * there's not a lot of point in guessing what it might be.
22958 */
22959 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022960 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22961 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 }
22963
22964 /* Count the paths backward to find the beginning of the desired string. */
22965 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022966 {
22967#ifdef FEAT_MBYTE
22968 if (has_mbyte)
22969 p -= mb_head_off(tfname, p);
22970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 if (vim_ispathsep(*p))
22972 {
22973 if (sepcount == 0 || (hasTilde && sepcount == 1))
22974 break;
22975 else
22976 sepcount --;
22977 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979 if (hasTilde)
22980 {
22981 --p;
22982 if (p >= tfname)
22983 *p = '~';
22984 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022985 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 }
22987 else
22988 ++p;
22989
22990 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22991 vim_free(*bufp);
22992 *fnamelen = (int)STRLEN(p);
22993 *bufp = pbuf;
22994 *fnamep = p;
22995
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022996 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022997}
22998#endif /* WIN3264 */
22999
23000/*
23001 * Adjust a filename, according to a string of modifiers.
23002 * *fnamep must be NUL terminated when called. When returning, the length is
23003 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023004 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 * When there is an error, *fnamep is set to NULL.
23006 */
23007 int
23008modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23009 char_u *src; /* string with modifiers */
23010 int *usedlen; /* characters after src that are used */
23011 char_u **fnamep; /* file name so far */
23012 char_u **bufp; /* buffer for allocated file name or NULL */
23013 int *fnamelen; /* length of fnamep */
23014{
23015 int valid = 0;
23016 char_u *tail;
23017 char_u *s, *p, *pbuf;
23018 char_u dirname[MAXPATHL];
23019 int c;
23020 int has_fullname = 0;
23021#ifdef WIN3264
23022 int has_shortname = 0;
23023#endif
23024
23025repeat:
23026 /* ":p" - full path/file_name */
23027 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23028 {
23029 has_fullname = 1;
23030
23031 valid |= VALID_PATH;
23032 *usedlen += 2;
23033
23034 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23035 if ((*fnamep)[0] == '~'
23036#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23037 && ((*fnamep)[1] == '/'
23038# ifdef BACKSLASH_IN_FILENAME
23039 || (*fnamep)[1] == '\\'
23040# endif
23041 || (*fnamep)[1] == NUL)
23042
23043#endif
23044 )
23045 {
23046 *fnamep = expand_env_save(*fnamep);
23047 vim_free(*bufp); /* free any allocated file name */
23048 *bufp = *fnamep;
23049 if (*fnamep == NULL)
23050 return -1;
23051 }
23052
23053 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023054 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 {
23056 if (vim_ispathsep(*p)
23057 && p[1] == '.'
23058 && (p[2] == NUL
23059 || vim_ispathsep(p[2])
23060 || (p[2] == '.'
23061 && (p[3] == NUL || vim_ispathsep(p[3])))))
23062 break;
23063 }
23064
23065 /* FullName_save() is slow, don't use it when not needed. */
23066 if (*p != NUL || !vim_isAbsName(*fnamep))
23067 {
23068 *fnamep = FullName_save(*fnamep, *p != NUL);
23069 vim_free(*bufp); /* free any allocated file name */
23070 *bufp = *fnamep;
23071 if (*fnamep == NULL)
23072 return -1;
23073 }
23074
23075 /* Append a path separator to a directory. */
23076 if (mch_isdir(*fnamep))
23077 {
23078 /* Make room for one or two extra characters. */
23079 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23080 vim_free(*bufp); /* free any allocated file name */
23081 *bufp = *fnamep;
23082 if (*fnamep == NULL)
23083 return -1;
23084 add_pathsep(*fnamep);
23085 }
23086 }
23087
23088 /* ":." - path relative to the current directory */
23089 /* ":~" - path relative to the home directory */
23090 /* ":8" - shortname path - postponed till after */
23091 while (src[*usedlen] == ':'
23092 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23093 {
23094 *usedlen += 2;
23095 if (c == '8')
23096 {
23097#ifdef WIN3264
23098 has_shortname = 1; /* Postpone this. */
23099#endif
23100 continue;
23101 }
23102 pbuf = NULL;
23103 /* Need full path first (use expand_env() to remove a "~/") */
23104 if (!has_fullname)
23105 {
23106 if (c == '.' && **fnamep == '~')
23107 p = pbuf = expand_env_save(*fnamep);
23108 else
23109 p = pbuf = FullName_save(*fnamep, FALSE);
23110 }
23111 else
23112 p = *fnamep;
23113
23114 has_fullname = 0;
23115
23116 if (p != NULL)
23117 {
23118 if (c == '.')
23119 {
23120 mch_dirname(dirname, MAXPATHL);
23121 s = shorten_fname(p, dirname);
23122 if (s != NULL)
23123 {
23124 *fnamep = s;
23125 if (pbuf != NULL)
23126 {
23127 vim_free(*bufp); /* free any allocated file name */
23128 *bufp = pbuf;
23129 pbuf = NULL;
23130 }
23131 }
23132 }
23133 else
23134 {
23135 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23136 /* Only replace it when it starts with '~' */
23137 if (*dirname == '~')
23138 {
23139 s = vim_strsave(dirname);
23140 if (s != NULL)
23141 {
23142 *fnamep = s;
23143 vim_free(*bufp);
23144 *bufp = s;
23145 }
23146 }
23147 }
23148 vim_free(pbuf);
23149 }
23150 }
23151
23152 tail = gettail(*fnamep);
23153 *fnamelen = (int)STRLEN(*fnamep);
23154
23155 /* ":h" - head, remove "/file_name", can be repeated */
23156 /* Don't remove the first "/" or "c:\" */
23157 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23158 {
23159 valid |= VALID_HEAD;
23160 *usedlen += 2;
23161 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023162 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023163 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 *fnamelen = (int)(tail - *fnamep);
23165#ifdef VMS
23166 if (*fnamelen > 0)
23167 *fnamelen += 1; /* the path separator is part of the path */
23168#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023169 if (*fnamelen == 0)
23170 {
23171 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23172 p = vim_strsave((char_u *)".");
23173 if (p == NULL)
23174 return -1;
23175 vim_free(*bufp);
23176 *bufp = *fnamep = tail = p;
23177 *fnamelen = 1;
23178 }
23179 else
23180 {
23181 while (tail > s && !after_pathsep(s, tail))
23182 mb_ptr_back(*fnamep, tail);
23183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 }
23185
23186 /* ":8" - shortname */
23187 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23188 {
23189 *usedlen += 2;
23190#ifdef WIN3264
23191 has_shortname = 1;
23192#endif
23193 }
23194
23195#ifdef WIN3264
23196 /* Check shortname after we have done 'heads' and before we do 'tails'
23197 */
23198 if (has_shortname)
23199 {
23200 pbuf = NULL;
23201 /* Copy the string if it is shortened by :h */
23202 if (*fnamelen < (int)STRLEN(*fnamep))
23203 {
23204 p = vim_strnsave(*fnamep, *fnamelen);
23205 if (p == 0)
23206 return -1;
23207 vim_free(*bufp);
23208 *bufp = *fnamep = p;
23209 }
23210
23211 /* Split into two implementations - makes it easier. First is where
23212 * there isn't a full name already, second is where there is.
23213 */
23214 if (!has_fullname && !vim_isAbsName(*fnamep))
23215 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023216 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 return -1;
23218 }
23219 else
23220 {
23221 int l;
23222
23223 /* Simple case, already have the full-name
23224 * Nearly always shorter, so try first time. */
23225 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023226 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 return -1;
23228
23229 if (l == 0)
23230 {
23231 /* Couldn't find the filename.. search the paths.
23232 */
23233 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023234 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023235 return -1;
23236 }
23237 *fnamelen = l;
23238 }
23239 }
23240#endif /* WIN3264 */
23241
23242 /* ":t" - tail, just the basename */
23243 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23244 {
23245 *usedlen += 2;
23246 *fnamelen -= (int)(tail - *fnamep);
23247 *fnamep = tail;
23248 }
23249
23250 /* ":e" - extension, can be repeated */
23251 /* ":r" - root, without extension, can be repeated */
23252 while (src[*usedlen] == ':'
23253 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23254 {
23255 /* find a '.' in the tail:
23256 * - for second :e: before the current fname
23257 * - otherwise: The last '.'
23258 */
23259 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23260 s = *fnamep - 2;
23261 else
23262 s = *fnamep + *fnamelen - 1;
23263 for ( ; s > tail; --s)
23264 if (s[0] == '.')
23265 break;
23266 if (src[*usedlen + 1] == 'e') /* :e */
23267 {
23268 if (s > tail)
23269 {
23270 *fnamelen += (int)(*fnamep - (s + 1));
23271 *fnamep = s + 1;
23272#ifdef VMS
23273 /* cut version from the extension */
23274 s = *fnamep + *fnamelen - 1;
23275 for ( ; s > *fnamep; --s)
23276 if (s[0] == ';')
23277 break;
23278 if (s > *fnamep)
23279 *fnamelen = s - *fnamep;
23280#endif
23281 }
23282 else if (*fnamep <= tail)
23283 *fnamelen = 0;
23284 }
23285 else /* :r */
23286 {
23287 if (s > tail) /* remove one extension */
23288 *fnamelen = (int)(s - *fnamep);
23289 }
23290 *usedlen += 2;
23291 }
23292
23293 /* ":s?pat?foo?" - substitute */
23294 /* ":gs?pat?foo?" - global substitute */
23295 if (src[*usedlen] == ':'
23296 && (src[*usedlen + 1] == 's'
23297 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23298 {
23299 char_u *str;
23300 char_u *pat;
23301 char_u *sub;
23302 int sep;
23303 char_u *flags;
23304 int didit = FALSE;
23305
23306 flags = (char_u *)"";
23307 s = src + *usedlen + 2;
23308 if (src[*usedlen + 1] == 'g')
23309 {
23310 flags = (char_u *)"g";
23311 ++s;
23312 }
23313
23314 sep = *s++;
23315 if (sep)
23316 {
23317 /* find end of pattern */
23318 p = vim_strchr(s, sep);
23319 if (p != NULL)
23320 {
23321 pat = vim_strnsave(s, (int)(p - s));
23322 if (pat != NULL)
23323 {
23324 s = p + 1;
23325 /* find end of substitution */
23326 p = vim_strchr(s, sep);
23327 if (p != NULL)
23328 {
23329 sub = vim_strnsave(s, (int)(p - s));
23330 str = vim_strnsave(*fnamep, *fnamelen);
23331 if (sub != NULL && str != NULL)
23332 {
23333 *usedlen = (int)(p + 1 - src);
23334 s = do_string_sub(str, pat, sub, flags);
23335 if (s != NULL)
23336 {
23337 *fnamep = s;
23338 *fnamelen = (int)STRLEN(s);
23339 vim_free(*bufp);
23340 *bufp = s;
23341 didit = TRUE;
23342 }
23343 }
23344 vim_free(sub);
23345 vim_free(str);
23346 }
23347 vim_free(pat);
23348 }
23349 }
23350 /* after using ":s", repeat all the modifiers */
23351 if (didit)
23352 goto repeat;
23353 }
23354 }
23355
23356 return valid;
23357}
23358
23359/*
23360 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23361 * "flags" can be "g" to do a global substitute.
23362 * Returns an allocated string, NULL for error.
23363 */
23364 char_u *
23365do_string_sub(str, pat, sub, flags)
23366 char_u *str;
23367 char_u *pat;
23368 char_u *sub;
23369 char_u *flags;
23370{
23371 int sublen;
23372 regmatch_T regmatch;
23373 int i;
23374 int do_all;
23375 char_u *tail;
23376 garray_T ga;
23377 char_u *ret;
23378 char_u *save_cpo;
23379
23380 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23381 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023382 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023383
23384 ga_init2(&ga, 1, 200);
23385
23386 do_all = (flags[0] == 'g');
23387
23388 regmatch.rm_ic = p_ic;
23389 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23390 if (regmatch.regprog != NULL)
23391 {
23392 tail = str;
23393 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23394 {
23395 /*
23396 * Get some space for a temporary buffer to do the substitution
23397 * into. It will contain:
23398 * - The text up to where the match is.
23399 * - The substituted text.
23400 * - The text after the match.
23401 */
23402 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23403 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23404 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23405 {
23406 ga_clear(&ga);
23407 break;
23408 }
23409
23410 /* copy the text up to where the match is */
23411 i = (int)(regmatch.startp[0] - tail);
23412 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23413 /* add the substituted text */
23414 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23415 + ga.ga_len + i, TRUE, TRUE, FALSE);
23416 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417 /* avoid getting stuck on a match with an empty string */
23418 if (tail == regmatch.endp[0])
23419 {
23420 if (*tail == NUL)
23421 break;
23422 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23423 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 }
23425 else
23426 {
23427 tail = regmatch.endp[0];
23428 if (*tail == NUL)
23429 break;
23430 }
23431 if (!do_all)
23432 break;
23433 }
23434
23435 if (ga.ga_data != NULL)
23436 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23437
23438 vim_free(regmatch.regprog);
23439 }
23440
23441 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23442 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023443 if (p_cpo == empty_option)
23444 p_cpo = save_cpo;
23445 else
23446 /* Darn, evaluating {sub} expression changed the value. */
23447 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448
23449 return ret;
23450}
23451
23452#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */