blob: 1216dc67298b094d6238815ce8f09f07003aa503 [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 Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000366};
367
368/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_type vv_di.di_tv.v_type
370#define vv_nr vv_di.di_tv.vval.v_number
371#define vv_float vv_di.di_tv.vval.v_float
372#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000373#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000374#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000375
376/*
377 * The v: variables are stored in dictionary "vimvardict".
378 * "vimvars_var" is the variable that is used for the "l:" scope.
379 */
380static dict_T vimvardict;
381static dictitem_T vimvars_var;
382#define vimvarht vimvardict.dv_hashtab
383
Bram Moolenaara40058a2005-07-11 22:42:07 +0000384static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
385static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
386#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
387static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
388#endif
389static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
390static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
391static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
393static void list_glob_vars __ARGS((int *first));
394static void list_buf_vars __ARGS((int *first));
395static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000397static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000398#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000399static void list_vim_vars __ARGS((int *first));
400static void list_script_vars __ARGS((int *first));
401static void list_func_vars __ARGS((int *first));
402static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000403static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
404static int check_changedtick __ARGS((char_u *arg));
405static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
406static void clear_lval __ARGS((lval_T *lp));
407static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
408static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
409static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
410static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
411static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
412static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
413static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
414static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
415static void item_lock __ARGS((typval_T *tv, int deep, int lock));
416static int tv_islocked __ARGS((typval_T *tv));
417
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
419static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000424static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
425static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000426
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000427static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
429static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
430static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
431static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000432static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static listitem_T *listitem_alloc __ARGS((void));
434static void listitem_free __ARGS((listitem_T *item));
435static void listitem_remove __ARGS((list_T *l, listitem_T *item));
436static long list_len __ARGS((list_T *l));
437static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
438static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
439static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000441static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000444static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
446static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
447static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000448static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *list2string __ARGS((typval_T *tv, int copyID));
451static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000452static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000453static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
454static void set_ref_in_list __ARGS((list_T *l, int copyID));
455static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200456static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000458static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
460static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000461static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000463static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000465static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
466static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000467static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#ifdef FEAT_FLOAT
469static int string2float __ARGS((char_u *text, float_T *value));
470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
472static int find_internal_func __ARGS((char_u *name));
473static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
474static 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 +0200475static 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 +0000476static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000477static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000478
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
480static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200481static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200489static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200491static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#ifdef FEAT_FLOAT
505static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
506#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000507static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000510static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000512#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000513static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000514static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
516#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000519#ifdef FEAT_FLOAT
520static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200521static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000522#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
526static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537#ifdef FEAT_FLOAT
538static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
539#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000542static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000548#ifdef FEAT_FLOAT
549static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000552#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000553static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000562static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000564static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000570static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000578static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000579static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000580static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000581static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200584static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000585static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000593static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000607static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000613static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000625#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200626static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000627static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
628#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000633static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000634static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000635static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000637static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000641#ifdef vim_mkdir
642static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
643#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100645#ifdef FEAT_MZSCHEME
646static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
652static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000655static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000675static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000677static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000684static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000685static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000686static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000687static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200689static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000690static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200696static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000699static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000700static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
704static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000707static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709#ifdef HAVE_STRFTIME
710static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
711#endif
712static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200718static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200719static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000725static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200726static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000728static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000729static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000731static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000732static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000734static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200735#ifdef FEAT_FLOAT
736static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#ifdef FEAT_FLOAT
743static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
744#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200746static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200747static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000762static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000763static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000764static int get_env_len __ARGS((char_u **arg));
765static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000766static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000767static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
768#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
769#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
770 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000771static 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 +0000772static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000773static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000774static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
775static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static typval_T *alloc_tv __ARGS((void));
777static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static void init_tv __ARGS((typval_T *varp));
779static long get_tv_number __ARGS((typval_T *varp));
780static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000781static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static char_u *get_tv_string __ARGS((typval_T *varp));
783static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000784static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000786static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
788static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
789static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000790static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
791static 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 +0000792static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
793static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000794static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000795static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000796static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
798static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
799static int eval_fname_script __ARGS((char_u *p));
800static int eval_fname_sid __ARGS((char_u *p));
801static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static ufunc_T *find_func __ARGS((char_u *name));
803static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000804static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#ifdef FEAT_PROFILE
806static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000807static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
808static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_total_cmp __ARGS((const void *s1, const void *s2));
814static int
815# ifdef __BORLANDC__
816 _RTLENTRYF
817# endif
818 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000820static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000821static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000822static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void func_free __ARGS((ufunc_T *fp));
824static void func_unref __ARGS((char_u *name));
825static void func_ref __ARGS((char_u *name));
826static 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 +0000827static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
828static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000830static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
831static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000832static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000833static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000834static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000835
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200836
837#ifdef EBCDIC
838static int compare_func_name __ARGS((const void *s1, const void *s2));
839static void sortFunctions __ARGS(());
840#endif
841
842
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000843/* Character used as separated in autoload function/variable names. */
844#define AUTOLOAD_CHAR '#'
845
Bram Moolenaar33570922005-01-25 22:26:29 +0000846/*
847 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000848 */
849 void
850eval_init()
851{
Bram Moolenaar33570922005-01-25 22:26:29 +0000852 int i;
853 struct vimvar *p;
854
855 init_var_dict(&globvardict, &globvars_var);
856 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000857 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
860 for (i = 0; i < VV_LEN; ++i)
861 {
862 p = &vimvars[i];
863 STRCPY(p->vv_di.di_key, p->vv_name);
864 if (p->vv_flags & VV_RO)
865 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
866 else if (p->vv_flags & VV_RO_SBX)
867 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
868 else
869 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000870
871 /* add to v: scope dict, unless the value is not always available */
872 if (p->vv_type != VAR_UNKNOWN)
873 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 /* add to compat scope dict */
876 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000878 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
882 * Sort the function table, to enable binary sort.
883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914
915 /* global variables */
916 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000917
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000918 /* autoloaded script names */
919 ga_clear_strings(&ga_loaded);
920
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200921 /* script-local variables */
922 for (i = 1; i <= ga_scripts.ga_len; ++i)
923 {
924 vars_clear(&SCRIPT_VARS(i));
925 vim_free(SCRIPT_SV(i));
926 }
927 ga_clear(&ga_scripts);
928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000929 /* unreferenced lists and dicts */
930 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000931
932 /* functions */
933 free_all_functions();
934 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000935}
936#endif
937
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * Return the name of the executed function.
940 */
941 char_u *
942func_name(cookie)
943 void *cookie;
944{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946}
947
948/*
949 * Return the address holding the next breakpoint line for a funccall cookie.
950 */
951 linenr_T *
952func_breakpoint(cookie)
953 void *cookie;
954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the debug tick for a funccall cookie.
960 */
961 int *
962func_dbg_tick(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the nesting level for a funccall cookie.
970 */
971 int
972func_level(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000979funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000981/* pointer to list of previously used funccal, still around because some
982 * item in it is still being used. */
983funccall_T *previous_funccal = NULL;
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
986 * Return TRUE when a function was ended by a ":return" command.
987 */
988 int
989current_func_returned()
990{
991 return current_funccal->returned;
992}
993
994
995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * Set an internal variable to a string value. Creates the variable if it does
997 * not already exist.
998 */
999 void
1000set_internal_string_var(name, value)
1001 char_u *name;
1002 char_u *value;
1003{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 val = vim_strsave(value);
1008 if (val != NULL)
1009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 }
1017}
1018
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001020static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static char_u *redir_endp = NULL;
1022static char_u *redir_varname = NULL;
1023
1024/*
1025 * Start recording command output to a variable
1026 * Returns OK if successfully completed the setup. FAIL otherwise.
1027 */
1028 int
1029var_redir_start(name, append)
1030 char_u *name;
1031 int append; /* append to an existing variable */
1032{
1033 int save_emsg;
1034 int err;
1035 typval_T tv;
1036
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001037 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001038 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 {
1040 EMSG(_(e_invarg));
1041 return FAIL;
1042 }
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 redir_varname = vim_strsave(name);
1046 if (redir_varname == NULL)
1047 return FAIL;
1048
1049 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1050 if (redir_lval == NULL)
1051 {
1052 var_redir_stop();
1053 return FAIL;
1054 }
1055
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001056 /* The output is stored in growarray "redir_ga" until redirection ends. */
1057 ga_init2(&redir_ga, (int)sizeof(char), 500);
1058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001060 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1061 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1063 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001064 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 if (redir_endp != NULL && *redir_endp != NUL)
1066 /* Trailing characters are present after the variable name */
1067 EMSG(_(e_trailing));
1068 else
1069 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001070 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 var_redir_stop();
1072 return FAIL;
1073 }
1074
1075 /* check if we can write to the variable: set it to or append an empty
1076 * string */
1077 save_emsg = did_emsg;
1078 did_emsg = FALSE;
1079 tv.v_type = VAR_STRING;
1080 tv.vval.v_string = (char_u *)"";
1081 if (append)
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1083 else
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001085 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001087 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (err)
1089 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001090 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 var_redir_stop();
1092 return FAIL;
1093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 * Append "value[value_len]" to the variable set by var_redir_start().
1100 * The actual appending is postponed until redirection ends, because the value
1101 * appended may in fact be the string we write to, changing it may cause freed
1102 * memory to be used:
1103 * :redir => foo
1104 * :let foo
1105 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 */
1107 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
1114 if (redir_lval == NULL)
1115 return;
1116
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 {
1124 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 }
1127 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129}
1130
1131/*
1132 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 */
1135 void
1136var_redir_stop()
1137{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138 typval_T tv;
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_lval != NULL)
1141 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* If there was no error: assign the text to the variable. */
1143 if (redir_endp != NULL)
1144 {
1145 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1146 tv.v_type = VAR_STRING;
1147 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001148 /* Call get_lval() again, if it's inside a Dict or List it may
1149 * have changed. */
1150 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1151 FALSE, FALSE, FALSE, FNE_CHECK_START);
1152 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1153 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1154 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* free the collected output */
1158 vim_free(redir_ga.ga_data);
1159 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 vim_free(redir_lval);
1162 redir_lval = NULL;
1163 }
1164 vim_free(redir_varname);
1165 redir_varname = NULL;
1166}
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168# if defined(FEAT_MBYTE) || defined(PROTO)
1169 int
1170eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1171 char_u *enc_from;
1172 char_u *enc_to;
1173 char_u *fname_from;
1174 char_u *fname_to;
1175{
1176 int err = FALSE;
1177
1178 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1179 set_vim_var_string(VV_CC_TO, enc_to, -1);
1180 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1181 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1182 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1183 err = TRUE;
1184 set_vim_var_string(VV_CC_FROM, NULL, -1);
1185 set_vim_var_string(VV_CC_TO, NULL, -1);
1186 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1187 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1188
1189 if (err)
1190 return FAIL;
1191 return OK;
1192}
1193# endif
1194
1195# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1196 int
1197eval_printexpr(fname, args)
1198 char_u *fname;
1199 char_u *args;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_FNAME_IN, fname, -1);
1204 set_vim_var_string(VV_CMDARG, args, -1);
1205 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1206 err = TRUE;
1207 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1208 set_vim_var_string(VV_CMDARG, NULL, -1);
1209
1210 if (err)
1211 {
1212 mch_remove(fname);
1213 return FAIL;
1214 }
1215 return OK;
1216}
1217# endif
1218
1219# if defined(FEAT_DIFF) || defined(PROTO)
1220 void
1221eval_diff(origfile, newfile, outfile)
1222 char_u *origfile;
1223 char_u *newfile;
1224 char_u *outfile;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1229 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1230 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1231 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1234 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1235}
1236
1237 void
1238eval_patch(origfile, difffile, outfile)
1239 char_u *origfile;
1240 char_u *difffile;
1241 char_u *outfile;
1242{
1243 int err;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253# endif
1254
1255/*
1256 * Top level evaluation function, returning a boolean.
1257 * Sets "error" to TRUE if there was an error.
1258 * Return TRUE or FALSE.
1259 */
1260 int
1261eval_to_bool(arg, error, nextcmd, skip)
1262 char_u *arg;
1263 int *error;
1264 char_u **nextcmd;
1265 int skip; /* only parse, don't execute */
1266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 int retval = FALSE;
1269
1270 if (skip)
1271 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
1275 {
1276 *error = FALSE;
1277 if (!skip)
1278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001279 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 }
1283 if (skip)
1284 --emsg_skip;
1285
1286 return retval;
1287}
1288
1289/*
1290 * Top level evaluation function, returning a string. If "skip" is TRUE,
1291 * only parsing to "nextcmd" is done, without reporting errors. Return
1292 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1293 */
1294 char_u *
1295eval_to_string_skip(arg, nextcmd, skip)
1296 char_u *arg;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 char_u *retval;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 retval = NULL;
1307 else
1308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 retval = vim_strsave(get_tv_string(&tv));
1310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 if (skip)
1313 --emsg_skip;
1314
1315 return retval;
1316}
1317
1318/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319 * Skip over an expression at "*pp".
1320 * Return FAIL for an error, OK otherwise.
1321 */
1322 int
1323skip_expr(pp)
1324 char_u **pp;
1325{
Bram Moolenaar33570922005-01-25 22:26:29 +00001326 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327
1328 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001330}
1331
1332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 * When "convert" is TRUE convert a List into a sequence of lines and convert
1335 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 * Return pointer to allocated memory, or NULL for failure.
1337 */
1338 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *arg;
1341 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 retval = NULL;
1353 else
1354 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 {
1357 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 if (tv.vval.v_list != NULL)
1359 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 ga_append(&ga, NUL);
1361 retval = (char_u *)ga.ga_data;
1362 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363#ifdef FEAT_FLOAT
1364 else if (convert && tv.v_type == VAR_FLOAT)
1365 {
1366 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1367 retval = vim_strsave(numbuf);
1368 }
1369#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001370 else
1371 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374
1375 return retval;
1376}
1377
1378/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379 * Call eval_to_string() without using current local variables and using
1380 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 */
1382 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 char_u *arg;
1385 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 char_u *retval;
1389 void *save_funccalp;
1390
1391 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 if (use_sandbox)
1393 ++sandbox;
1394 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001395 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 if (use_sandbox)
1397 --sandbox;
1398 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 restore_funccal(save_funccalp);
1400 return retval;
1401}
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403/*
1404 * Top level evaluation function, returning a number.
1405 * Evaluates "expr" silently.
1406 * Returns -1 for an error.
1407 */
1408 int
1409eval_to_number(expr)
1410 char_u *expr;
1411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001414 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
1416 ++emsg_off;
1417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 retval = -1;
1420 else
1421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001422 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 --emsg_off;
1426
1427 return retval;
1428}
1429
Bram Moolenaara40058a2005-07-11 22:42:07 +00001430/*
1431 * Prepare v: variable "idx" to be used.
1432 * Save the current typeval in "save_tv".
1433 * When not used yet add the variable to the v: hashtable.
1434 */
1435 static void
1436prepare_vimvar(idx, save_tv)
1437 int idx;
1438 typval_T *save_tv;
1439{
1440 *save_tv = vimvars[idx].vv_tv;
1441 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1442 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1443}
1444
1445/*
1446 * Restore v: variable "idx" to typeval "save_tv".
1447 * When no longer defined, remove the variable from the v: hashtable.
1448 */
1449 static void
1450restore_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 hashitem_T *hi;
1455
Bram Moolenaara40058a2005-07-11 22:42:07 +00001456 vimvars[idx].vv_tv = *save_tv;
1457 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1458 {
1459 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1460 if (HASHITEM_EMPTY(hi))
1461 EMSG2(_(e_intern2), "restore_vimvar()");
1462 else
1463 hash_remove(&vimvarht, hi);
1464 }
1465}
1466
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001467#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001468/*
1469 * Evaluate an expression to a list with suggestions.
1470 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001471 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001472 */
1473 list_T *
1474eval_spell_expr(badword, expr)
1475 char_u *badword;
1476 char_u *expr;
1477{
1478 typval_T save_val;
1479 typval_T rettv;
1480 list_T *list = NULL;
1481 char_u *p = skipwhite(expr);
1482
1483 /* Set "v:val" to the bad word. */
1484 prepare_vimvar(VV_VAL, &save_val);
1485 vimvars[VV_VAL].vv_type = VAR_STRING;
1486 vimvars[VV_VAL].vv_str = badword;
1487 if (p_verbose == 0)
1488 ++emsg_off;
1489
1490 if (eval1(&p, &rettv, TRUE) == OK)
1491 {
1492 if (rettv.v_type != VAR_LIST)
1493 clear_tv(&rettv);
1494 else
1495 list = rettv.vval.v_list;
1496 }
1497
1498 if (p_verbose == 0)
1499 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001500 restore_vimvar(VV_VAL, &save_val);
1501
1502 return list;
1503}
1504
1505/*
1506 * "list" is supposed to contain two items: a word and a number. Return the
1507 * word in "pp" and the number as the return value.
1508 * Return -1 if anything isn't right.
1509 * Used to get the good word and score from the eval_spell_expr() result.
1510 */
1511 int
1512get_spellword(list, pp)
1513 list_T *list;
1514 char_u **pp;
1515{
1516 listitem_T *li;
1517
1518 li = list->lv_first;
1519 if (li == NULL)
1520 return -1;
1521 *pp = get_tv_string(&li->li_tv);
1522
1523 li = li->li_next;
1524 if (li == NULL)
1525 return -1;
1526 return get_tv_number(&li->li_tv);
1527}
1528#endif
1529
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001531 * Top level evaluation function.
1532 * Returns an allocated typval_T with the result.
1533 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534 */
1535 typval_T *
1536eval_expr(arg, nextcmd)
1537 char_u *arg;
1538 char_u **nextcmd;
1539{
1540 typval_T *tv;
1541
1542 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 {
1545 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001546 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001547 }
1548
1549 return tv;
1550}
1551
1552
Bram Moolenaar4f688582007-07-24 12:34:30 +00001553#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1554 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 * Uses argv[argc] for the function arguments. Only Number and String
1558 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 static int
1562call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 char_u *func;
1564 int argc;
1565 char_u **argv;
1566 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 long n;
1571 int len;
1572 int i;
1573 int doesrange;
1574 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 for (i = 0; i < argc; i++)
1582 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 /* Pass a NULL or empty argument as an empty string */
1584 if (argv[i] == NULL || *argv[i] == NUL)
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 continue;
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* Recognize a number argument, the others must be strings. */
1592 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1593 if (len != 0 && len == (int)STRLEN(argv[i]))
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_NUMBER;
1596 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 else
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_STRING;
1601 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 }
1604
1605 if (safe)
1606 {
1607 save_funccalp = save_funccal();
1608 ++sandbox;
1609 }
1610
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1612 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (safe)
1616 {
1617 --sandbox;
1618 restore_funccal(save_funccalp);
1619 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 vim_free(argvars);
1621
1622 if (ret == FAIL)
1623 clear_tv(rettv);
1624
1625 return ret;
1626}
1627
Bram Moolenaar4f688582007-07-24 12:34:30 +00001628# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001630 * Call vimL function "func" and return the result as a string.
1631 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 * Uses argv[argc] for the function arguments.
1633 */
1634 void *
1635call_func_retstr(func, argc, argv, safe)
1636 char_u *func;
1637 int argc;
1638 char_u **argv;
1639 int safe; /* use the sandbox */
1640{
1641 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001642 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643
1644 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1645 return NULL;
1646
1647 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 return retval;
1650}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001651# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652
Bram Moolenaar4f688582007-07-24 12:34:30 +00001653# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001655 * Call vimL function "func" and return the result as a number.
1656 * Returns -1 when calling the function fails.
1657 * Uses argv[argc] for the function arguments.
1658 */
1659 long
1660call_func_retnr(func, argc, argv, safe)
1661 char_u *func;
1662 int argc;
1663 char_u **argv;
1664 int safe; /* use the sandbox */
1665{
1666 typval_T rettv;
1667 long retval;
1668
1669 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1670 return -1;
1671
1672 retval = get_tv_number_chk(&rettv, NULL);
1673 clear_tv(&rettv);
1674 return retval;
1675}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001676# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001677
1678/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001679 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001681 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 */
1683 void *
1684call_func_retlist(func, argc, argv, safe)
1685 char_u *func;
1686 int argc;
1687 char_u **argv;
1688 int safe; /* use the sandbox */
1689{
1690 typval_T rettv;
1691
1692 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1693 return NULL;
1694
1695 if (rettv.v_type != VAR_LIST)
1696 {
1697 clear_tv(&rettv);
1698 return NULL;
1699 }
1700
1701 return rettv.vval.v_list;
1702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
1704
Bram Moolenaar4f688582007-07-24 12:34:30 +00001705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706/*
1707 * Save the current function call pointer, and set it to NULL.
1708 * Used when executing autocommands and for ":source".
1709 */
1710 void *
1711save_funccal()
1712{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001713 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 current_funccal = NULL;
1716 return (void *)fc;
1717}
1718
1719 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720restore_funccal(vfc)
1721 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001723 funccall_T *fc = (funccall_T *)vfc;
1724
1725 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726}
1727
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728#if defined(FEAT_PROFILE) || defined(PROTO)
1729/*
1730 * Prepare profiling for entering a child or something else that is not
1731 * counted for the script/function itself.
1732 * Should always be called in pair with prof_child_exit().
1733 */
1734 void
1735prof_child_enter(tm)
1736 proftime_T *tm; /* place to store waittime */
1737{
1738 funccall_T *fc = current_funccal;
1739
1740 if (fc != NULL && fc->func->uf_profiling)
1741 profile_start(&fc->prof_child);
1742 script_prof_save(tm);
1743}
1744
1745/*
1746 * Take care of time spent in a child.
1747 * Should always be called after prof_child_enter().
1748 */
1749 void
1750prof_child_exit(tm)
1751 proftime_T *tm; /* where waittime was stored */
1752{
1753 funccall_T *fc = current_funccal;
1754
1755 if (fc != NULL && fc->func->uf_profiling)
1756 {
1757 profile_end(&fc->prof_child);
1758 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1759 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1760 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1761 }
1762 script_prof_restore(tm);
1763}
1764#endif
1765
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#ifdef FEAT_FOLDING
1768/*
1769 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1770 * it in "*cp". Doesn't give error messages.
1771 */
1772 int
1773eval_foldexpr(arg, cp)
1774 char_u *arg;
1775 int *cp;
1776{
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 int retval;
1779 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001780 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1781 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001784 if (use_sandbox)
1785 ++sandbox;
1786 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 retval = 0;
1790 else
1791 {
1792 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (tv.v_type == VAR_NUMBER)
1794 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001795 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 retval = 0;
1797 else
1798 {
1799 /* If the result is a string, check if there is a non-digit before
1800 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (!VIM_ISDIGIT(*s) && *s != '-')
1803 *cp = *s++;
1804 retval = atol((char *)s);
1805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001809 if (use_sandbox)
1810 --sandbox;
1811 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813 return retval;
1814}
1815#endif
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 * ":let" list all variable values
1819 * ":let var1 var2" list variable values
1820 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 * ":let var += expr" assignment command.
1822 * ":let var -= expr" assignment command.
1823 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 */
1826 void
1827ex_let(eap)
1828 exarg_T *eap;
1829{
1830 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001832 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 int var_count = 0;
1835 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001838 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaardb552d602006-03-23 22:59:57 +00001840 argend = skip_var_list(arg, &var_count, &semicolon);
1841 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001843 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1844 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001848 /*
1849 * ":let" without "=": list variables
1850 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (*arg == '[')
1852 EMSG(_(e_invarg));
1853 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001855 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001859 list_glob_vars(&first);
1860 list_buf_vars(&first);
1861 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001862#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001863 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001864#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_script_vars(&first);
1866 list_func_vars(&first);
1867 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 eap->nextcmd = check_nextcmd(arg);
1870 }
1871 else
1872 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 op[0] = '=';
1874 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001875 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 {
1877 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1878 op[0] = expr[-1]; /* +=, -= or .= */
1879 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (eap->skip)
1883 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (eap->skip)
1886 {
1887 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 --emsg_skip;
1890 }
1891 else if (i != FAIL)
1892 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 }
1898}
1899
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900/*
1901 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1902 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 * When "nextchars" is not NULL it points to a string with characters that
1904 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1905 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 * Returns OK or FAIL;
1907 */
1908 static int
1909ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1910 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 int copy; /* copy values from "tv", don't move */
1913 int semicolon; /* from skip_var_list() */
1914 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916{
1917 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 listitem_T *item;
1921 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922
1923 if (*arg != '[')
1924 {
1925 /*
1926 * ":let var = expr" or ":for var in list"
1927 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 return FAIL;
1930 return OK;
1931 }
1932
1933 /*
1934 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1935 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001936 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 {
1938 EMSG(_(e_listreq));
1939 return FAIL;
1940 }
1941
1942 i = list_len(l);
1943 if (semicolon == 0 && var_count < i)
1944 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001945 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 return FAIL;
1947 }
1948 if (var_count - semicolon > i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953
1954 item = l->lv_first;
1955 while (*arg != ']')
1956 {
1957 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 item = item->li_next;
1960 if (arg == NULL)
1961 return FAIL;
1962
1963 arg = skipwhite(arg);
1964 if (*arg == ';')
1965 {
1966 /* Put the rest of the list (may be empty) in the var after ';'.
1967 * Create a new list for this. */
1968 l = list_alloc();
1969 if (l == NULL)
1970 return FAIL;
1971 while (item != NULL)
1972 {
1973 list_append_tv(l, &item->li_tv);
1974 item = item->li_next;
1975 }
1976
1977 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001978 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 ltv.vval.v_list = l;
1980 l->lv_refcount = 1;
1981
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1983 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 clear_tv(&ltv);
1985 if (arg == NULL)
1986 return FAIL;
1987 break;
1988 }
1989 else if (*arg != ',' && *arg != ']')
1990 {
1991 EMSG2(_(e_intern2), "ex_let_vars()");
1992 return FAIL;
1993 }
1994 }
1995
1996 return OK;
1997}
1998
1999/*
2000 * Skip over assignable variable "var" or list of variables "[var, var]".
2001 * Used for ":let varvar = expr" and ":for varvar in expr".
2002 * For "[var, var]" increment "*var_count" for each variable.
2003 * for "[var, var; var]" set "semicolon".
2004 * Return NULL for an error.
2005 */
2006 static char_u *
2007skip_var_list(arg, var_count, semicolon)
2008 char_u *arg;
2009 int *var_count;
2010 int *semicolon;
2011{
2012 char_u *p, *s;
2013
2014 if (*arg == '[')
2015 {
2016 /* "[var, var]": find the matching ']'. */
2017 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002018 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 {
2020 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2021 s = skip_var_one(p);
2022 if (s == p)
2023 {
2024 EMSG2(_(e_invarg2), p);
2025 return NULL;
2026 }
2027 ++*var_count;
2028
2029 p = skipwhite(s);
2030 if (*p == ']')
2031 break;
2032 else if (*p == ';')
2033 {
2034 if (*semicolon == 1)
2035 {
2036 EMSG(_("Double ; in list of variables"));
2037 return NULL;
2038 }
2039 *semicolon = 1;
2040 }
2041 else if (*p != ',')
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 }
2047 return p + 1;
2048 }
2049 else
2050 return skip_var_one(arg);
2051}
2052
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002054 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002055 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 static char_u *
2058skip_var_one(arg)
2059 char_u *arg;
2060{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 if (*arg == '@' && arg[1] != NUL)
2062 return arg + 2;
2063 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2064 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065}
2066
Bram Moolenaara7043832005-01-21 11:56:39 +00002067/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 * List variables for hashtab "ht" with prefix "prefix".
2069 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002072list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002076 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077{
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashitem_T *hi;
2079 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 int todo;
2081
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002082 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2084 {
2085 if (!HASHITEM_EMPTY(hi))
2086 {
2087 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 di = HI2DI(hi);
2089 if (empty || di->di_tv.v_type != VAR_STRING
2090 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 }
2093 }
2094}
2095
2096/*
2097 * List global variables.
2098 */
2099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_glob_vars(first)
2101 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002103 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104}
2105
2106/*
2107 * List buffer variables.
2108 */
2109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110list_buf_vars(first)
2111 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113 char_u numbuf[NUMBUFLEN];
2114
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2116 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117
2118 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2120 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121}
2122
2123/*
2124 * List window variables.
2125 */
2126 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127list_win_vars(first)
2128 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002129{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2131 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002132}
2133
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002134#ifdef FEAT_WINDOWS
2135/*
2136 * List tab page variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_tab_vars(first)
2140 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2143 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144}
2145#endif
2146
Bram Moolenaara7043832005-01-21 11:56:39 +00002147/*
2148 * List Vim variables.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_vim_vars(first)
2152 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155}
2156
2157/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002158 * List script-local variables, if there is a script.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_script_vars(first)
2162 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163{
2164 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2166 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167}
2168
2169/*
2170 * List function variables, if there is a function.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_func_vars(first)
2174 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002175{
2176 if (current_funccal != NULL)
2177 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002179}
2180
2181/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 * List variables in "arg".
2183 */
2184 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 exarg_T *eap;
2187 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189{
2190 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002191 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 char_u *name_start;
2194 char_u *arg_subsc;
2195 char_u *tofree;
2196 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197
2198 while (!ends_excmd(*arg) && !got_int)
2199 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002202 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2204 {
2205 emsg_severe = TRUE;
2206 EMSG(_(e_trailing));
2207 break;
2208 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 /* get_name_len() takes care of expanding curly braces */
2213 name_start = name = arg;
2214 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2215 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* This is mainly to keep test 49 working: when expanding
2218 * curly braces fails overrule the exception error message. */
2219 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 emsg_severe = TRUE;
2222 EMSG2(_(e_invarg2), arg);
2223 break;
2224 }
2225 error = TRUE;
2226 }
2227 else
2228 {
2229 if (tofree != NULL)
2230 name = tofree;
2231 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 else
2234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 /* handle d.key, l[idx], f(expr) */
2236 arg_subsc = arg;
2237 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 case 'g': list_glob_vars(first); break;
2246 case 'b': list_buf_vars(first); break;
2247 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002248#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002249 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002250#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'v': list_vim_vars(first); break;
2252 case 's': list_script_vars(first); break;
2253 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 default:
2255 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
2258 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 {
2260 char_u numbuf[NUMBUFLEN];
2261 char_u *tf;
2262 int c;
2263 char_u *s;
2264
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002265 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 c = *arg;
2267 *arg = NUL;
2268 list_one_var_a((char_u *)"",
2269 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 tv.v_type,
2271 s == NULL ? (char_u *)"" : s,
2272 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 *arg = c;
2274 vim_free(tf);
2275 }
2276 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 }
2279 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280
2281 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283
2284 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286
2287 return arg;
2288}
2289
2290/*
2291 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2292 * Returns a pointer to the char just after the var name.
2293 * Returns NULL if there is an error.
2294 */
2295 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002298 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002300 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302{
2303 int c1;
2304 char_u *name;
2305 char_u *p;
2306 char_u *arg_end = NULL;
2307 int len;
2308 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310
2311 /*
2312 * ":let $VAR = expr": Set environment variable.
2313 */
2314 if (*arg == '$')
2315 {
2316 /* Find the end of the name. */
2317 ++arg;
2318 name = arg;
2319 len = get_env_len(&arg);
2320 if (len == 0)
2321 EMSG2(_(e_invarg2), name - 1);
2322 else
2323 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324 if (op != NULL && (*op == '+' || *op == '-'))
2325 EMSG2(_(e_letwrong), op);
2326 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002327 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 EMSG(_(e_letunexp));
2329 else
2330 {
2331 c1 = name[len];
2332 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 p = get_tv_string_chk(tv);
2334 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 {
2336 int mustfree = FALSE;
2337 char_u *s = vim_getenv(name, &mustfree);
2338
2339 if (s != NULL)
2340 {
2341 p = tofree = concat_str(s, p);
2342 if (mustfree)
2343 vim_free(s);
2344 }
2345 }
2346 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002347 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002349 if (STRICMP(name, "HOME") == 0)
2350 init_homedir();
2351 else if (didset_vim && STRICMP(name, "VIM") == 0)
2352 didset_vim = FALSE;
2353 else if (didset_vimruntime
2354 && STRICMP(name, "VIMRUNTIME") == 0)
2355 didset_vimruntime = FALSE;
2356 arg_end = arg;
2357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
2361 }
2362 }
2363
2364 /*
2365 * ":let &option = expr": Set option value.
2366 * ":let &l:option = expr": Set local option value.
2367 * ":let &g:option = expr": Set global option value.
2368 */
2369 else if (*arg == '&')
2370 {
2371 /* Find the end of the name. */
2372 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 if (p == NULL || (endchars != NULL
2374 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 EMSG(_(e_letunexp));
2376 else
2377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 long n;
2379 int opt_type;
2380 long numval;
2381 char_u *stringval = NULL;
2382 char_u *s;
2383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 c1 = *p;
2385 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386
2387 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 s = get_tv_string_chk(tv); /* != NULL if number or string */
2389 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 {
2391 opt_type = get_option_value(arg, &numval,
2392 &stringval, opt_flags);
2393 if ((opt_type == 1 && *op == '.')
2394 || (opt_type == 0 && *op != '.'))
2395 EMSG2(_(e_letwrong), op);
2396 else
2397 {
2398 if (opt_type == 1) /* number */
2399 {
2400 if (*op == '+')
2401 n = numval + n;
2402 else
2403 n = numval - n;
2404 }
2405 else if (opt_type == 0 && stringval != NULL) /* string */
2406 {
2407 s = concat_str(stringval, s);
2408 vim_free(stringval);
2409 stringval = s;
2410 }
2411 }
2412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 if (s != NULL)
2414 {
2415 set_option_value(arg, n, s, opt_flags);
2416 arg_end = p;
2417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 }
2421 }
2422
2423 /*
2424 * ":let @r = expr": Set register contents.
2425 */
2426 else if (*arg == '@')
2427 {
2428 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 if (op != NULL && (*op == '+' || *op == '-'))
2430 EMSG2(_(e_letwrong), op);
2431 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 EMSG(_(e_letunexp));
2434 else
2435 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002436 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 char_u *s;
2438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 p = get_tv_string_chk(tv);
2440 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002442 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 if (s != NULL)
2444 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 vim_free(s);
2447 }
2448 }
2449 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 arg_end = arg + 1;
2453 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 }
2456 }
2457
2458 /*
2459 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002462 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002464 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002466 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 arg_end = p;
2475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 }
2479
2480 else
2481 EMSG2(_(e_invarg2), arg);
2482
2483 return arg_end;
2484}
2485
2486/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2488 */
2489 static int
2490check_changedtick(arg)
2491 char_u *arg;
2492{
2493 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2494 {
2495 EMSG2(_(e_readonlyvar), arg);
2496 return TRUE;
2497 }
2498 return FALSE;
2499}
2500
2501/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * Get an lval: variable, Dict item or List item that can be assigned a value
2503 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2504 * "name.key", "name.key[expr]" etc.
2505 * Indexing only works if "name" is an existing List or Dictionary.
2506 * "name" points to the start of the name.
2507 * If "rettv" is not NULL it points to the value to be assigned.
2508 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2509 * wrong; must end in space or cmd separator.
2510 *
2511 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002512 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
2515 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 typval_T *rettv;
2519 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 int unlet;
2521 int skip;
2522 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 char_u *expr_start, *expr_end;
2527 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 dictitem_T *v;
2529 typval_T var1;
2530 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539
2540 if (skip)
2541 {
2542 /* When skipping just find the end of the name. */
2543 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002544 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 }
2546
2547 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002548 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (expr_start != NULL)
2550 {
2551 /* Don't expand the name when we already know there is an error. */
2552 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2553 && *p != '[' && *p != '.')
2554 {
2555 EMSG(_(e_trailing));
2556 return NULL;
2557 }
2558
2559 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2560 if (lp->ll_exp_name == NULL)
2561 {
2562 /* Report an invalid expression in braces, unless the
2563 * expression evaluation has been cancelled due to an
2564 * aborting error, an interrupt, or an exception. */
2565 if (!aborting() && !quiet)
2566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002567 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 EMSG2(_(e_invarg2), name);
2569 return NULL;
2570 }
2571 }
2572 lp->ll_name = lp->ll_exp_name;
2573 }
2574 else
2575 lp->ll_name = name;
2576
2577 /* Without [idx] or .key we are done. */
2578 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2579 return p;
2580
2581 cc = *p;
2582 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002583 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (v == NULL && !quiet)
2585 EMSG2(_(e_undefvar), lp->ll_name);
2586 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 if (v == NULL)
2588 return NULL;
2589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /*
2591 * Loop until no more [idx] or .key is following.
2592 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2597 && !(lp->ll_tv->v_type == VAR_DICT
2598 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!quiet)
2601 EMSG(_("E689: Can only index a List or Dictionary"));
2602 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E708: [:] must come last"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 len = -1;
2612 if (*p == '.')
2613 {
2614 key = p + 1;
2615 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2616 ;
2617 if (len == 0)
2618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_(e_emptykey));
2621 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623 p = key + len;
2624 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 else
2626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (*p == ':')
2630 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 empty1 = FALSE;
2634 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 if (get_tv_string_chk(&var1) == NULL)
2637 {
2638 /* not a number or string */
2639 clear_tv(&var1);
2640 return NULL;
2641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 }
2643
2644 /* Optionally get the second index [ :expr]. */
2645 if (*p == ':')
2646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 if (!empty1)
2652 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (rettv != NULL && (rettv->v_type != VAR_LIST
2656 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 if (!empty1)
2661 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 }
2664 p = skipwhite(p + 1);
2665 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 else
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (!empty1)
2673 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 if (get_tv_string_chk(&var2) == NULL)
2677 {
2678 /* not a number or string */
2679 if (!empty1)
2680 clear_tv(&var1);
2681 clear_tv(&var2);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700
2701 /* Skip to past ']'. */
2702 ++p;
2703 }
2704
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
2707 if (len == -1)
2708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (*key == NUL)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
2714 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 lp->ll_list = NULL;
2720 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002721 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002724 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (len == -1)
2730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (len == -1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 p = NULL;
2741 break;
2742 }
2743 if (len == -1)
2744 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747 else
2748 {
2749 /*
2750 * Get the number and item for the only or first index of the List.
2751 */
2752 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 else
2755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var1);
2758 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 lp->ll_list = lp->ll_tv->vval.v_list;
2761 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2762 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002764 if (lp->ll_n1 < 0)
2765 {
2766 lp->ll_n1 = 0;
2767 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2768 }
2769 }
2770 if (lp->ll_li == NULL)
2771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776
2777 /*
2778 * May need to find the item or absolute index for the second
2779 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 * When no index given: "lp->ll_empty2" is TRUE.
2781 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 }
2794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2796 if (lp->ll_n1 < 0)
2797 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2798 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002800 }
2801
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002804 }
2805
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return p;
2807}
2808
2809/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002810 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 */
2812 static void
2813clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002814 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815{
2816 vim_free(lp->ll_exp_name);
2817 vim_free(lp->ll_newkey);
2818}
2819
2820/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002821 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 */
2825 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002826set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002831 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832{
2833 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002834 listitem_T *ri;
2835 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836
2837 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002838 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002840 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 cc = *endp;
2842 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 if (op != NULL && *op != '=')
2844 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002847 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002848 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002849 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002850 {
2851 if (tv_op(&tv, rettv, op) == OK)
2852 set_var(lp->ll_name, &tv, FALSE);
2853 clear_tv(&tv);
2854 }
2855 }
2856 else
2857 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002859 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002861 else if (tv_check_lock(lp->ll_newkey == NULL
2862 ? lp->ll_tv->v_lock
2863 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2864 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 else if (lp->ll_range)
2866 {
2867 /*
2868 * Assign the List values to the list items.
2869 */
2870 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002871 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002872 if (op != NULL && *op != '=')
2873 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2874 else
2875 {
2876 clear_tv(&lp->ll_li->li_tv);
2877 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2878 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 ri = ri->li_next;
2880 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2881 break;
2882 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002885 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002886 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 ri = NULL;
2888 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002889 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002890 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_li = lp->ll_li->li_next;
2892 ++lp->ll_n1;
2893 }
2894 if (ri != NULL)
2895 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 else if (lp->ll_empty2
2897 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 : lp->ll_n1 != lp->ll_n2)
2899 EMSG(_("E711: List value has not enough items"));
2900 }
2901 else
2902 {
2903 /*
2904 * Assign to a List or Dictionary item.
2905 */
2906 if (lp->ll_newkey != NULL)
2907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 if (op != NULL && *op != '=')
2909 {
2910 EMSG2(_(e_letwrong), op);
2911 return;
2912 }
2913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002915 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (di == NULL)
2917 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002918 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2919 {
2920 vim_free(di);
2921 return;
2922 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 else if (op != NULL && *op != '=')
2926 {
2927 tv_op(lp->ll_tv, rettv, op);
2928 return;
2929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 /*
2934 * Assign the value to the variable or list item.
2935 */
2936 if (copy)
2937 copy_tv(rettv, lp->ll_tv);
2938 else
2939 {
2940 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002941 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 }
2944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945}
2946
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002947/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2949 * Returns OK or FAIL.
2950 */
2951 static int
2952tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 typval_T *tv1;
2954 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 char_u *op;
2956{
2957 long n;
2958 char_u numbuf[NUMBUFLEN];
2959 char_u *s;
2960
2961 /* Can't do anything with a Funcref or a Dict on the right. */
2962 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2963 {
2964 switch (tv1->v_type)
2965 {
2966 case VAR_DICT:
2967 case VAR_FUNC:
2968 break;
2969
2970 case VAR_LIST:
2971 if (*op != '+' || tv2->v_type != VAR_LIST)
2972 break;
2973 /* List += List */
2974 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2975 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2976 return OK;
2977
2978 case VAR_NUMBER:
2979 case VAR_STRING:
2980 if (tv2->v_type == VAR_LIST)
2981 break;
2982 if (*op == '+' || *op == '-')
2983 {
2984 /* nr += nr or nr -= nr*/
2985 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986#ifdef FEAT_FLOAT
2987 if (tv2->v_type == VAR_FLOAT)
2988 {
2989 float_T f = n;
2990
2991 if (*op == '+')
2992 f += tv2->vval.v_float;
2993 else
2994 f -= tv2->vval.v_float;
2995 clear_tv(tv1);
2996 tv1->v_type = VAR_FLOAT;
2997 tv1->vval.v_float = f;
2998 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000#endif
3001 {
3002 if (*op == '+')
3003 n += get_tv_number(tv2);
3004 else
3005 n -= get_tv_number(tv2);
3006 clear_tv(tv1);
3007 tv1->v_type = VAR_NUMBER;
3008 tv1->vval.v_number = n;
3009 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003010 }
3011 else
3012 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003013 if (tv2->v_type == VAR_FLOAT)
3014 break;
3015
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 /* str .= str */
3017 s = get_tv_string(tv1);
3018 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3019 clear_tv(tv1);
3020 tv1->v_type = VAR_STRING;
3021 tv1->vval.v_string = s;
3022 }
3023 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024
3025#ifdef FEAT_FLOAT
3026 case VAR_FLOAT:
3027 {
3028 float_T f;
3029
3030 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3031 && tv2->v_type != VAR_NUMBER
3032 && tv2->v_type != VAR_STRING))
3033 break;
3034 if (tv2->v_type == VAR_FLOAT)
3035 f = tv2->vval.v_float;
3036 else
3037 f = get_tv_number(tv2);
3038 if (*op == '+')
3039 tv1->vval.v_float += f;
3040 else
3041 tv1->vval.v_float -= f;
3042 }
3043 return OK;
3044#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003045 }
3046 }
3047
3048 EMSG2(_(e_letwrong), op);
3049 return FAIL;
3050}
3051
3052/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053 * Add a watcher to a list.
3054 */
3055 static void
3056list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 list_T *l;
3058 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059{
3060 lw->lw_next = l->lv_watch;
3061 l->lv_watch = lw;
3062}
3063
3064/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003065 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066 * No warning when it isn't found...
3067 */
3068 static void
3069list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 list_T *l;
3071 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003072{
Bram Moolenaar33570922005-01-25 22:26:29 +00003073 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003074
3075 lwp = &l->lv_watch;
3076 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3077 {
3078 if (lw == lwrem)
3079 {
3080 *lwp = lw->lw_next;
3081 break;
3082 }
3083 lwp = &lw->lw_next;
3084 }
3085}
3086
3087/*
3088 * Just before removing an item from a list: advance watchers to the next
3089 * item.
3090 */
3091 static void
3092list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 list_T *l;
3094 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003095{
Bram Moolenaar33570922005-01-25 22:26:29 +00003096 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097
3098 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3099 if (lw->lw_item == item)
3100 lw->lw_item = item->li_next;
3101}
3102
3103/*
3104 * Evaluate the expression used in a ":for var in expr" command.
3105 * "arg" points to "var".
3106 * Set "*errp" to TRUE for an error, FALSE otherwise;
3107 * Return a pointer that holds the info. Null when there is an error.
3108 */
3109 void *
3110eval_for_line(arg, errp, nextcmdp, skip)
3111 char_u *arg;
3112 int *errp;
3113 char_u **nextcmdp;
3114 int skip;
3115{
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 typval_T tv;
3119 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120
3121 *errp = TRUE; /* default: there is an error */
3122
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 if (fi == NULL)
3125 return NULL;
3126
3127 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3128 if (expr == NULL)
3129 return fi;
3130
3131 expr = skipwhite(expr);
3132 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3133 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003134 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135 return fi;
3136 }
3137
3138 if (skip)
3139 ++emsg_skip;
3140 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3141 {
3142 *errp = FALSE;
3143 if (!skip)
3144 {
3145 l = tv.vval.v_list;
3146 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003147 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003149 clear_tv(&tv);
3150 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 else
3152 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003153 /* No need to increment the refcount, it's already set for the
3154 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155 fi->fi_list = l;
3156 list_add_watch(l, &fi->fi_lw);
3157 fi->fi_lw.lw_item = l->lv_first;
3158 }
3159 }
3160 }
3161 if (skip)
3162 --emsg_skip;
3163
3164 return fi;
3165}
3166
3167/*
3168 * Use the first item in a ":for" list. Advance to the next.
3169 * Assign the values to the variable (list). "arg" points to the first one.
3170 * Return TRUE when a valid item was found, FALSE when at end of list or
3171 * something wrong.
3172 */
3173 int
3174next_for_item(fi_void, arg)
3175 void *fi_void;
3176 char_u *arg;
3177{
Bram Moolenaar33570922005-01-25 22:26:29 +00003178 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003179 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003180 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181
3182 item = fi->fi_lw.lw_item;
3183 if (item == NULL)
3184 result = FALSE;
3185 else
3186 {
3187 fi->fi_lw.lw_item = item->li_next;
3188 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3189 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3190 }
3191 return result;
3192}
3193
3194/*
3195 * Free the structure used to store info used by ":for".
3196 */
3197 void
3198free_for_info(fi_void)
3199 void *fi_void;
3200{
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003203 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003204 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003206 list_unref(fi->fi_list);
3207 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 vim_free(fi);
3209}
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3212
3213 void
3214set_context_for_expression(xp, arg, cmdidx)
3215 expand_T *xp;
3216 char_u *arg;
3217 cmdidx_T cmdidx;
3218{
3219 int got_eq = FALSE;
3220 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 if (cmdidx == CMD_let)
3224 {
3225 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003226 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 {
3228 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003229 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 {
3231 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 if (vim_iswhite(*p))
3234 break;
3235 }
3236 return;
3237 }
3238 }
3239 else
3240 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3241 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 while ((xp->xp_pattern = vim_strpbrk(arg,
3243 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3244 {
3245 c = *xp->xp_pattern;
3246 if (c == '&')
3247 {
3248 c = xp->xp_pattern[1];
3249 if (c == '&')
3250 {
3251 ++xp->xp_pattern;
3252 xp->xp_context = cmdidx != CMD_let || got_eq
3253 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3254 }
3255 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003258 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3259 xp->xp_pattern += 2;
3260
3261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 else if (c == '$')
3264 {
3265 /* environment variable */
3266 xp->xp_context = EXPAND_ENV_VARS;
3267 }
3268 else if (c == '=')
3269 {
3270 got_eq = TRUE;
3271 xp->xp_context = EXPAND_EXPRESSION;
3272 }
3273 else if (c == '<'
3274 && xp->xp_context == EXPAND_FUNCTIONS
3275 && vim_strchr(xp->xp_pattern, '(') == NULL)
3276 {
3277 /* Function name can start with "<SNR>" */
3278 break;
3279 }
3280 else if (cmdidx != CMD_let || got_eq)
3281 {
3282 if (c == '"') /* string */
3283 {
3284 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3285 if (c == '\\' && xp->xp_pattern[1] != NUL)
3286 ++xp->xp_pattern;
3287 xp->xp_context = EXPAND_NOTHING;
3288 }
3289 else if (c == '\'') /* literal string */
3290 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003291 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3293 /* skip */ ;
3294 xp->xp_context = EXPAND_NOTHING;
3295 }
3296 else if (c == '|')
3297 {
3298 if (xp->xp_pattern[1] == '|')
3299 {
3300 ++xp->xp_pattern;
3301 xp->xp_context = EXPAND_EXPRESSION;
3302 }
3303 else
3304 xp->xp_context = EXPAND_COMMANDS;
3305 }
3306 else
3307 xp->xp_context = EXPAND_EXPRESSION;
3308 }
3309 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310 /* Doesn't look like something valid, expand as an expression
3311 * anyway. */
3312 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 arg = xp->xp_pattern;
3314 if (*arg != NUL)
3315 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3316 /* skip */ ;
3317 }
3318 xp->xp_pattern = arg;
3319}
3320
3321#endif /* FEAT_CMDL_COMPL */
3322
3323/*
3324 * ":1,25call func(arg1, arg2)" function call.
3325 */
3326 void
3327ex_call(eap)
3328 exarg_T *eap;
3329{
3330 char_u *arg = eap->arg;
3331 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003333 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003335 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 linenr_T lnum;
3337 int doesrange;
3338 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003339 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003341 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003342 if (fudi.fd_newkey != NULL)
3343 {
3344 /* Still need to give an error message for missing key. */
3345 EMSG2(_(e_dictkey), fudi.fd_newkey);
3346 vim_free(fudi.fd_newkey);
3347 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003348 if (tofree == NULL)
3349 return;
3350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003351 /* Increase refcount on dictionary, it could get deleted when evaluating
3352 * the arguments. */
3353 if (fudi.fd_dict != NULL)
3354 ++fudi.fd_dict->dv_refcount;
3355
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003356 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003357 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003358 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
Bram Moolenaar532c7802005-01-27 14:44:31 +00003360 /* Skip white space to allow ":call func ()". Not good, but required for
3361 * backward compatibility. */
3362 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365 if (*startarg != '(')
3366 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003367 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 goto end;
3369 }
3370
3371 /*
3372 * When skipping, evaluate the function once, to find the end of the
3373 * arguments.
3374 * When the function takes a range, this is discovered after the first
3375 * call, and the loop is broken.
3376 */
3377 if (eap->skip)
3378 {
3379 ++emsg_skip;
3380 lnum = eap->line2; /* do it once, also with an invalid range */
3381 }
3382 else
3383 lnum = eap->line1;
3384 for ( ; lnum <= eap->line2; ++lnum)
3385 {
3386 if (!eap->skip && eap->addr_count > 0)
3387 {
3388 curwin->w_cursor.lnum = lnum;
3389 curwin->w_cursor.col = 0;
3390 }
3391 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003392 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003393 eap->line1, eap->line2, &doesrange,
3394 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 {
3396 failed = TRUE;
3397 break;
3398 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003399
3400 /* Handle a function returning a Funcref, Dictionary or List. */
3401 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3402 {
3403 failed = TRUE;
3404 break;
3405 }
3406
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (doesrange || eap->skip)
3409 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003412 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003414 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (aborting())
3416 break;
3417 }
3418 if (eap->skip)
3419 --emsg_skip;
3420
3421 if (!failed)
3422 {
3423 /* Check for trailing illegal characters and a following command. */
3424 if (!ends_excmd(*arg))
3425 {
3426 emsg_severe = TRUE;
3427 EMSG(_(e_trailing));
3428 }
3429 else
3430 eap->nextcmd = check_nextcmd(arg);
3431 }
3432
3433end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003434 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003435 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436}
3437
3438/*
3439 * ":unlet[!] var1 ... " command.
3440 */
3441 void
3442ex_unlet(eap)
3443 exarg_T *eap;
3444{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003445 ex_unletlock(eap, eap->arg, 0);
3446}
3447
3448/*
3449 * ":lockvar" and ":unlockvar" commands
3450 */
3451 void
3452ex_lockvar(eap)
3453 exarg_T *eap;
3454{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003456 int deep = 2;
3457
3458 if (eap->forceit)
3459 deep = -1;
3460 else if (vim_isdigit(*arg))
3461 {
3462 deep = getdigits(&arg);
3463 arg = skipwhite(arg);
3464 }
3465
3466 ex_unletlock(eap, arg, deep);
3467}
3468
3469/*
3470 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3471 */
3472 static void
3473ex_unletlock(eap, argstart, deep)
3474 exarg_T *eap;
3475 char_u *argstart;
3476 int deep;
3477{
3478 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003481 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483 do
3484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003485 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003486 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3487 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003488 if (lv.ll_name == NULL)
3489 error = TRUE; /* error but continue parsing */
3490 if (name_end == NULL || (!vim_iswhite(*name_end)
3491 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003493 if (name_end != NULL)
3494 {
3495 emsg_severe = TRUE;
3496 EMSG(_(e_trailing));
3497 }
3498 if (!(eap->skip || error))
3499 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 break;
3501 }
3502
3503 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003504 {
3505 if (eap->cmdidx == CMD_unlet)
3506 {
3507 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3508 error = TRUE;
3509 }
3510 else
3511 {
3512 if (do_lock_var(&lv, name_end, deep,
3513 eap->cmdidx == CMD_lockvar) == FAIL)
3514 error = TRUE;
3515 }
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003518 if (!eap->skip)
3519 clear_lval(&lv);
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 arg = skipwhite(name_end);
3522 } while (!ends_excmd(*arg));
3523
3524 eap->nextcmd = check_nextcmd(arg);
3525}
3526
Bram Moolenaar8c711452005-01-14 21:53:12 +00003527 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003528do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003529 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003530 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003531 int forceit;
3532{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003533 int ret = OK;
3534 int cc;
3535
3536 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538 cc = *name_end;
3539 *name_end = NUL;
3540
3541 /* Normal name or expanded name. */
3542 if (check_changedtick(lp->ll_name))
3543 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003544 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003545 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003546 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003547 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003548 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3549 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003550 else if (lp->ll_range)
3551 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553
3554 /* Delete a range of List items. */
3555 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3556 {
3557 li = lp->ll_li->li_next;
3558 listitem_remove(lp->ll_list, lp->ll_li);
3559 lp->ll_li = li;
3560 ++lp->ll_n1;
3561 }
3562 }
3563 else
3564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003566 /* unlet a List item. */
3567 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003569 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003570 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 }
3572
3573 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003574}
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576/*
3577 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003578 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 */
3580 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003581do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 hashtab_T *ht;
3586 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003587 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003588 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 ht = find_var_ht(name, &varname);
3591 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 hi = hash_find(ht, varname);
3594 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003595 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003596 di = HI2DI(hi);
3597 if (var_check_fixed(di->di_flags, name)
3598 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 return FAIL;
3600 delete_var(ht, hi);
3601 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003604 if (forceit)
3605 return OK;
3606 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 return FAIL;
3608}
3609
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610/*
3611 * Lock or unlock variable indicated by "lp".
3612 * "deep" is the levels to go (-1 for unlimited);
3613 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3614 */
3615 static int
3616do_lock_var(lp, name_end, deep, lock)
3617 lval_T *lp;
3618 char_u *name_end;
3619 int deep;
3620 int lock;
3621{
3622 int ret = OK;
3623 int cc;
3624 dictitem_T *di;
3625
3626 if (deep == 0) /* nothing to do */
3627 return OK;
3628
3629 if (lp->ll_tv == NULL)
3630 {
3631 cc = *name_end;
3632 *name_end = NUL;
3633
3634 /* Normal name or expanded name. */
3635 if (check_changedtick(lp->ll_name))
3636 ret = FAIL;
3637 else
3638 {
3639 di = find_var(lp->ll_name, NULL);
3640 if (di == NULL)
3641 ret = FAIL;
3642 else
3643 {
3644 if (lock)
3645 di->di_flags |= DI_FLAGS_LOCK;
3646 else
3647 di->di_flags &= ~DI_FLAGS_LOCK;
3648 item_lock(&di->di_tv, deep, lock);
3649 }
3650 }
3651 *name_end = cc;
3652 }
3653 else if (lp->ll_range)
3654 {
3655 listitem_T *li = lp->ll_li;
3656
3657 /* (un)lock a range of List items. */
3658 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3659 {
3660 item_lock(&li->li_tv, deep, lock);
3661 li = li->li_next;
3662 ++lp->ll_n1;
3663 }
3664 }
3665 else if (lp->ll_list != NULL)
3666 /* (un)lock a List item. */
3667 item_lock(&lp->ll_li->li_tv, deep, lock);
3668 else
3669 /* un(lock) a Dictionary item. */
3670 item_lock(&lp->ll_di->di_tv, deep, lock);
3671
3672 return ret;
3673}
3674
3675/*
3676 * Lock or unlock an item. "deep" is nr of levels to go.
3677 */
3678 static void
3679item_lock(tv, deep, lock)
3680 typval_T *tv;
3681 int deep;
3682 int lock;
3683{
3684 static int recurse = 0;
3685 list_T *l;
3686 listitem_T *li;
3687 dict_T *d;
3688 hashitem_T *hi;
3689 int todo;
3690
3691 if (recurse >= DICT_MAXNEST)
3692 {
3693 EMSG(_("E743: variable nested too deep for (un)lock"));
3694 return;
3695 }
3696 if (deep == 0)
3697 return;
3698 ++recurse;
3699
3700 /* lock/unlock the item itself */
3701 if (lock)
3702 tv->v_lock |= VAR_LOCKED;
3703 else
3704 tv->v_lock &= ~VAR_LOCKED;
3705
3706 switch (tv->v_type)
3707 {
3708 case VAR_LIST:
3709 if ((l = tv->vval.v_list) != NULL)
3710 {
3711 if (lock)
3712 l->lv_lock |= VAR_LOCKED;
3713 else
3714 l->lv_lock &= ~VAR_LOCKED;
3715 if (deep < 0 || deep > 1)
3716 /* recursive: lock/unlock the items the List contains */
3717 for (li = l->lv_first; li != NULL; li = li->li_next)
3718 item_lock(&li->li_tv, deep - 1, lock);
3719 }
3720 break;
3721 case VAR_DICT:
3722 if ((d = tv->vval.v_dict) != NULL)
3723 {
3724 if (lock)
3725 d->dv_lock |= VAR_LOCKED;
3726 else
3727 d->dv_lock &= ~VAR_LOCKED;
3728 if (deep < 0 || deep > 1)
3729 {
3730 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003731 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003732 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3733 {
3734 if (!HASHITEM_EMPTY(hi))
3735 {
3736 --todo;
3737 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3738 }
3739 }
3740 }
3741 }
3742 }
3743 --recurse;
3744}
3745
Bram Moolenaara40058a2005-07-11 22:42:07 +00003746/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003747 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3748 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003749 */
3750 static int
3751tv_islocked(tv)
3752 typval_T *tv;
3753{
3754 return (tv->v_lock & VAR_LOCKED)
3755 || (tv->v_type == VAR_LIST
3756 && tv->vval.v_list != NULL
3757 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3758 || (tv->v_type == VAR_DICT
3759 && tv->vval.v_dict != NULL
3760 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3761}
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3764/*
3765 * Delete all "menutrans_" variables.
3766 */
3767 void
3768del_menutrans_vars()
3769{
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003771 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Bram Moolenaar33570922005-01-25 22:26:29 +00003773 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003774 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003775 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003776 {
3777 if (!HASHITEM_EMPTY(hi))
3778 {
3779 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3781 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 }
3783 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785}
3786#endif
3787
3788#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3789
3790/*
3791 * Local string buffer for the next two functions to store a variable name
3792 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3793 * get_user_var_name().
3794 */
3795
3796static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3797
3798static char_u *varnamebuf = NULL;
3799static int varnamebuflen = 0;
3800
3801/*
3802 * Function to concatenate a prefix and a variable name.
3803 */
3804 static char_u *
3805cat_prefix_varname(prefix, name)
3806 int prefix;
3807 char_u *name;
3808{
3809 int len;
3810
3811 len = (int)STRLEN(name) + 3;
3812 if (len > varnamebuflen)
3813 {
3814 vim_free(varnamebuf);
3815 len += 10; /* some additional space */
3816 varnamebuf = alloc(len);
3817 if (varnamebuf == NULL)
3818 {
3819 varnamebuflen = 0;
3820 return NULL;
3821 }
3822 varnamebuflen = len;
3823 }
3824 *varnamebuf = prefix;
3825 varnamebuf[1] = ':';
3826 STRCPY(varnamebuf + 2, name);
3827 return varnamebuf;
3828}
3829
3830/*
3831 * Function given to ExpandGeneric() to obtain the list of user defined
3832 * (global/buffer/window/built-in) variable names.
3833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 char_u *
3835get_user_var_name(xp, idx)
3836 expand_T *xp;
3837 int idx;
3838{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003839 static long_u gdone;
3840 static long_u bdone;
3841 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003842#ifdef FEAT_WINDOWS
3843 static long_u tdone;
3844#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003845 static int vidx;
3846 static hashitem_T *hi;
3847 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003850 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003852#ifdef FEAT_WINDOWS
3853 tdone = 0;
3854#endif
3855 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003856
3857 /* Global variables */
3858 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003861 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003862 else
3863 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003864 while (HASHITEM_EMPTY(hi))
3865 ++hi;
3866 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3867 return cat_prefix_varname('g', hi->hi_key);
3868 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003870
3871 /* b: variables */
3872 ht = &curbuf->b_vars.dv_hashtab;
3873 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003875 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003876 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003877 else
3878 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003879 while (HASHITEM_EMPTY(hi))
3880 ++hi;
3881 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003883 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003885 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 return (char_u *)"b:changedtick";
3887 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003888
3889 /* w: variables */
3890 ht = &curwin->w_vars.dv_hashtab;
3891 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003893 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003894 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003895 else
3896 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003897 while (HASHITEM_EMPTY(hi))
3898 ++hi;
3899 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003902#ifdef FEAT_WINDOWS
3903 /* t: variables */
3904 ht = &curtab->tp_vars.dv_hashtab;
3905 if (tdone < ht->ht_used)
3906 {
3907 if (tdone++ == 0)
3908 hi = ht->ht_array;
3909 else
3910 ++hi;
3911 while (HASHITEM_EMPTY(hi))
3912 ++hi;
3913 return cat_prefix_varname('t', hi->hi_key);
3914 }
3915#endif
3916
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 /* v: variables */
3918 if (vidx < VV_LEN)
3919 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 vim_free(varnamebuf);
3922 varnamebuf = NULL;
3923 varnamebuflen = 0;
3924 return NULL;
3925}
3926
3927#endif /* FEAT_CMDL_COMPL */
3928
3929/*
3930 * types for expressions.
3931 */
3932typedef enum
3933{
3934 TYPE_UNKNOWN = 0
3935 , TYPE_EQUAL /* == */
3936 , TYPE_NEQUAL /* != */
3937 , TYPE_GREATER /* > */
3938 , TYPE_GEQUAL /* >= */
3939 , TYPE_SMALLER /* < */
3940 , TYPE_SEQUAL /* <= */
3941 , TYPE_MATCH /* =~ */
3942 , TYPE_NOMATCH /* !~ */
3943} exptype_T;
3944
3945/*
3946 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3949 */
3950
3951/*
3952 * Handle zero level expression.
3953 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003955 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 * Return OK or FAIL.
3957 */
3958 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 char_u **nextcmd;
3963 int evaluate;
3964{
3965 int ret;
3966 char_u *p;
3967
3968 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (ret == FAIL || !ends_excmd(*p))
3971 {
3972 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003973 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 /*
3975 * Report the invalid expression unless the expression evaluation has
3976 * been cancelled due to an aborting error, an interrupt, or an
3977 * exception.
3978 */
3979 if (!aborting())
3980 EMSG2(_(e_invexpr2), arg);
3981 ret = FAIL;
3982 }
3983 if (nextcmd != NULL)
3984 *nextcmd = check_nextcmd(p);
3985
3986 return ret;
3987}
3988
3989/*
3990 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003991 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 *
3993 * "arg" must point to the first non-white of the expression.
3994 * "arg" is advanced to the next non-white after the recognized expression.
3995 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003996 * Note: "rettv.v_lock" is not set.
3997 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * Return OK or FAIL.
3999 */
4000 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 int evaluate;
4005{
4006 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 /*
4010 * Get the first variable.
4011 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return FAIL;
4014
4015 if ((*arg)[0] == '?')
4016 {
4017 result = FALSE;
4018 if (evaluate)
4019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004020 int error = FALSE;
4021
4022 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004024 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004025 if (error)
4026 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028
4029 /*
4030 * Get the second variable.
4031 */
4032 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return FAIL;
4035
4036 /*
4037 * Check for the ":".
4038 */
4039 if ((*arg)[0] != ':')
4040 {
4041 EMSG(_("E109: Missing ':' after '?'"));
4042 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return FAIL;
4045 }
4046
4047 /*
4048 * Get the third variable.
4049 */
4050 *arg = skipwhite(*arg + 1);
4051 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4052 {
4053 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return FAIL;
4056 }
4057 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060
4061 return OK;
4062}
4063
4064/*
4065 * Handle first level expression:
4066 * expr2 || expr2 || expr2 logical OR
4067 *
4068 * "arg" must point to the first non-white of the expression.
4069 * "arg" is advanced to the next non-white after the recognized expression.
4070 *
4071 * Return OK or FAIL.
4072 */
4073 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 int evaluate;
4078{
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 long result;
4081 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004082 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /*
4085 * Get the first variable.
4086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return FAIL;
4089
4090 /*
4091 * Repeat until there is no following "||".
4092 */
4093 first = TRUE;
4094 result = FALSE;
4095 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4096 {
4097 if (evaluate && first)
4098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004102 if (error)
4103 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 first = FALSE;
4105 }
4106
4107 /*
4108 * Get the second variable.
4109 */
4110 *arg = skipwhite(*arg + 2);
4111 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4112 return FAIL;
4113
4114 /*
4115 * Compute the result.
4116 */
4117 if (evaluate && !result)
4118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004119 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004122 if (error)
4123 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125 if (evaluate)
4126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 rettv->v_type = VAR_NUMBER;
4128 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 }
4131
4132 return OK;
4133}
4134
4135/*
4136 * Handle second level expression:
4137 * expr3 && expr3 && expr3 logical AND
4138 *
4139 * "arg" must point to the first non-white of the expression.
4140 * "arg" is advanced to the next non-white after the recognized expression.
4141 *
4142 * Return OK or FAIL.
4143 */
4144 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int evaluate;
4149{
Bram Moolenaar33570922005-01-25 22:26:29 +00004150 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 long result;
4152 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
4155 /*
4156 * Get the first variable.
4157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 return FAIL;
4160
4161 /*
4162 * Repeat until there is no following "&&".
4163 */
4164 first = TRUE;
4165 result = TRUE;
4166 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4167 {
4168 if (evaluate && first)
4169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 if (error)
4174 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 first = FALSE;
4176 }
4177
4178 /*
4179 * Get the second variable.
4180 */
4181 *arg = skipwhite(*arg + 2);
4182 if (eval4(arg, &var2, evaluate && result) == FAIL)
4183 return FAIL;
4184
4185 /*
4186 * Compute the result.
4187 */
4188 if (evaluate && result)
4189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 if (error)
4194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 if (evaluate)
4197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 rettv->v_type = VAR_NUMBER;
4199 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202
4203 return OK;
4204}
4205
4206/*
4207 * Handle third level expression:
4208 * var1 == var2
4209 * var1 =~ var2
4210 * var1 != var2
4211 * var1 !~ var2
4212 * var1 > var2
4213 * var1 >= var2
4214 * var1 < var2
4215 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 * var1 is var2
4217 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 *
4219 * "arg" must point to the first non-white of the expression.
4220 * "arg" is advanced to the next non-white after the recognized expression.
4221 *
4222 * Return OK or FAIL.
4223 */
4224 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int evaluate;
4229{
Bram Moolenaar33570922005-01-25 22:26:29 +00004230 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 char_u *p;
4232 int i;
4233 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004234 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 int len = 2;
4236 long n1, n2;
4237 char_u *s1, *s2;
4238 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4239 regmatch_T regmatch;
4240 int ic;
4241 char_u *save_cpo;
4242
4243 /*
4244 * Get the first variable.
4245 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 return FAIL;
4248
4249 p = *arg;
4250 switch (p[0])
4251 {
4252 case '=': if (p[1] == '=')
4253 type = TYPE_EQUAL;
4254 else if (p[1] == '~')
4255 type = TYPE_MATCH;
4256 break;
4257 case '!': if (p[1] == '=')
4258 type = TYPE_NEQUAL;
4259 else if (p[1] == '~')
4260 type = TYPE_NOMATCH;
4261 break;
4262 case '>': if (p[1] != '=')
4263 {
4264 type = TYPE_GREATER;
4265 len = 1;
4266 }
4267 else
4268 type = TYPE_GEQUAL;
4269 break;
4270 case '<': if (p[1] != '=')
4271 {
4272 type = TYPE_SMALLER;
4273 len = 1;
4274 }
4275 else
4276 type = TYPE_SEQUAL;
4277 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004278 case 'i': if (p[1] == 's')
4279 {
4280 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4281 len = 5;
4282 if (!vim_isIDc(p[len]))
4283 {
4284 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4285 type_is = TRUE;
4286 }
4287 }
4288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290
4291 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004292 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 */
4294 if (type != TYPE_UNKNOWN)
4295 {
4296 /* extra question mark appended: ignore case */
4297 if (p[len] == '?')
4298 {
4299 ic = TRUE;
4300 ++len;
4301 }
4302 /* extra '#' appended: match case */
4303 else if (p[len] == '#')
4304 {
4305 ic = FALSE;
4306 ++len;
4307 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004308 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 else
4310 ic = p_ic;
4311
4312 /*
4313 * Get the second variable.
4314 */
4315 *arg = skipwhite(p + len);
4316 if (eval5(arg, &var2, evaluate) == FAIL)
4317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return FAIL;
4320 }
4321
4322 if (evaluate)
4323 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004324 if (type_is && rettv->v_type != var2.v_type)
4325 {
4326 /* For "is" a different type always means FALSE, for "notis"
4327 * it means TRUE. */
4328 n1 = (type == TYPE_NEQUAL);
4329 }
4330 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4331 {
4332 if (type_is)
4333 {
4334 n1 = (rettv->v_type == var2.v_type
4335 && rettv->vval.v_list == var2.vval.v_list);
4336 if (type == TYPE_NEQUAL)
4337 n1 = !n1;
4338 }
4339 else if (rettv->v_type != var2.v_type
4340 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4341 {
4342 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004343 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004344 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004345 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004346 clear_tv(rettv);
4347 clear_tv(&var2);
4348 return FAIL;
4349 }
4350 else
4351 {
4352 /* Compare two Lists for being equal or unequal. */
4353 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4354 if (type == TYPE_NEQUAL)
4355 n1 = !n1;
4356 }
4357 }
4358
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004359 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4360 {
4361 if (type_is)
4362 {
4363 n1 = (rettv->v_type == var2.v_type
4364 && rettv->vval.v_dict == var2.vval.v_dict);
4365 if (type == TYPE_NEQUAL)
4366 n1 = !n1;
4367 }
4368 else if (rettv->v_type != var2.v_type
4369 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4370 {
4371 if (rettv->v_type != var2.v_type)
4372 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4373 else
4374 EMSG(_("E736: Invalid operation for Dictionary"));
4375 clear_tv(rettv);
4376 clear_tv(&var2);
4377 return FAIL;
4378 }
4379 else
4380 {
4381 /* Compare two Dictionaries for being equal or unequal. */
4382 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4383 if (type == TYPE_NEQUAL)
4384 n1 = !n1;
4385 }
4386 }
4387
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004388 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4389 {
4390 if (rettv->v_type != var2.v_type
4391 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4392 {
4393 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004394 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004396 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 clear_tv(rettv);
4398 clear_tv(&var2);
4399 return FAIL;
4400 }
4401 else
4402 {
4403 /* Compare two Funcrefs for being equal or unequal. */
4404 if (rettv->vval.v_string == NULL
4405 || var2.vval.v_string == NULL)
4406 n1 = FALSE;
4407 else
4408 n1 = STRCMP(rettv->vval.v_string,
4409 var2.vval.v_string) == 0;
4410 if (type == TYPE_NEQUAL)
4411 n1 = !n1;
4412 }
4413 }
4414
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004415#ifdef FEAT_FLOAT
4416 /*
4417 * If one of the two variables is a float, compare as a float.
4418 * When using "=~" or "!~", always compare as string.
4419 */
4420 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4421 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4422 {
4423 float_T f1, f2;
4424
4425 if (rettv->v_type == VAR_FLOAT)
4426 f1 = rettv->vval.v_float;
4427 else
4428 f1 = get_tv_number(rettv);
4429 if (var2.v_type == VAR_FLOAT)
4430 f2 = var2.vval.v_float;
4431 else
4432 f2 = get_tv_number(&var2);
4433 n1 = FALSE;
4434 switch (type)
4435 {
4436 case TYPE_EQUAL: n1 = (f1 == f2); break;
4437 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4438 case TYPE_GREATER: n1 = (f1 > f2); break;
4439 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4440 case TYPE_SMALLER: n1 = (f1 < f2); break;
4441 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4442 case TYPE_UNKNOWN:
4443 case TYPE_MATCH:
4444 case TYPE_NOMATCH: break; /* avoid gcc warning */
4445 }
4446 }
4447#endif
4448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 /*
4450 * If one of the two variables is a number, compare as a number.
4451 * When using "=~" or "!~", always compare as string.
4452 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004453 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004456 n1 = get_tv_number(rettv);
4457 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 switch (type)
4459 {
4460 case TYPE_EQUAL: n1 = (n1 == n2); break;
4461 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4462 case TYPE_GREATER: n1 = (n1 > n2); break;
4463 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4464 case TYPE_SMALLER: n1 = (n1 < n2); break;
4465 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4466 case TYPE_UNKNOWN:
4467 case TYPE_MATCH:
4468 case TYPE_NOMATCH: break; /* avoid gcc warning */
4469 }
4470 }
4471 else
4472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004473 s1 = get_tv_string_buf(rettv, buf1);
4474 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4476 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4477 else
4478 i = 0;
4479 n1 = FALSE;
4480 switch (type)
4481 {
4482 case TYPE_EQUAL: n1 = (i == 0); break;
4483 case TYPE_NEQUAL: n1 = (i != 0); break;
4484 case TYPE_GREATER: n1 = (i > 0); break;
4485 case TYPE_GEQUAL: n1 = (i >= 0); break;
4486 case TYPE_SMALLER: n1 = (i < 0); break;
4487 case TYPE_SEQUAL: n1 = (i <= 0); break;
4488
4489 case TYPE_MATCH:
4490 case TYPE_NOMATCH:
4491 /* avoid 'l' flag in 'cpoptions' */
4492 save_cpo = p_cpo;
4493 p_cpo = (char_u *)"";
4494 regmatch.regprog = vim_regcomp(s2,
4495 RE_MAGIC + RE_STRING);
4496 regmatch.rm_ic = ic;
4497 if (regmatch.regprog != NULL)
4498 {
4499 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4500 vim_free(regmatch.regprog);
4501 if (type == TYPE_NOMATCH)
4502 n1 = !n1;
4503 }
4504 p_cpo = save_cpo;
4505 break;
4506
4507 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4508 }
4509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004510 clear_tv(rettv);
4511 clear_tv(&var2);
4512 rettv->v_type = VAR_NUMBER;
4513 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 }
4515 }
4516
4517 return OK;
4518}
4519
4520/*
4521 * Handle fourth level expression:
4522 * + number addition
4523 * - number subtraction
4524 * . string concatenation
4525 *
4526 * "arg" must point to the first non-white of the expression.
4527 * "arg" is advanced to the next non-white after the recognized expression.
4528 *
4529 * Return OK or FAIL.
4530 */
4531 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004532eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 int evaluate;
4536{
Bram Moolenaar33570922005-01-25 22:26:29 +00004537 typval_T var2;
4538 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 int op;
4540 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004541#ifdef FEAT_FLOAT
4542 float_T f1 = 0, f2 = 0;
4543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 char_u *s1, *s2;
4545 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4546 char_u *p;
4547
4548 /*
4549 * Get the first variable.
4550 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004551 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 return FAIL;
4553
4554 /*
4555 * Repeat computing, until no '+', '-' or '.' is following.
4556 */
4557 for (;;)
4558 {
4559 op = **arg;
4560 if (op != '+' && op != '-' && op != '.')
4561 break;
4562
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004563 if ((op != '+' || rettv->v_type != VAR_LIST)
4564#ifdef FEAT_FLOAT
4565 && (op == '.' || rettv->v_type != VAR_FLOAT)
4566#endif
4567 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004568 {
4569 /* For "list + ...", an illegal use of the first operand as
4570 * a number cannot be determined before evaluating the 2nd
4571 * operand: if this is also a list, all is ok.
4572 * For "something . ...", "something - ..." or "non-list + ...",
4573 * we know that the first operand needs to be a string or number
4574 * without evaluating the 2nd operand. So check before to avoid
4575 * side effects after an error. */
4576 if (evaluate && get_tv_string_chk(rettv) == NULL)
4577 {
4578 clear_tv(rettv);
4579 return FAIL;
4580 }
4581 }
4582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 /*
4584 * Get the second variable.
4585 */
4586 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004587 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004589 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 return FAIL;
4591 }
4592
4593 if (evaluate)
4594 {
4595 /*
4596 * Compute the result.
4597 */
4598 if (op == '.')
4599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004600 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4601 s2 = get_tv_string_buf_chk(&var2, buf2);
4602 if (s2 == NULL) /* type error ? */
4603 {
4604 clear_tv(rettv);
4605 clear_tv(&var2);
4606 return FAIL;
4607 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004608 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004609 clear_tv(rettv);
4610 rettv->v_type = VAR_STRING;
4611 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004613 else if (op == '+' && rettv->v_type == VAR_LIST
4614 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004615 {
4616 /* concatenate Lists */
4617 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4618 &var3) == FAIL)
4619 {
4620 clear_tv(rettv);
4621 clear_tv(&var2);
4622 return FAIL;
4623 }
4624 clear_tv(rettv);
4625 *rettv = var3;
4626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 else
4628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004629 int error = FALSE;
4630
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004631#ifdef FEAT_FLOAT
4632 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004633 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634 f1 = rettv->vval.v_float;
4635 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004636 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004637 else
4638#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004639 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004640 n1 = get_tv_number_chk(rettv, &error);
4641 if (error)
4642 {
4643 /* This can only happen for "list + non-list". For
4644 * "non-list + ..." or "something - ...", we returned
4645 * before evaluating the 2nd operand. */
4646 clear_tv(rettv);
4647 return FAIL;
4648 }
4649#ifdef FEAT_FLOAT
4650 if (var2.v_type == VAR_FLOAT)
4651 f1 = n1;
4652#endif
4653 }
4654#ifdef FEAT_FLOAT
4655 if (var2.v_type == VAR_FLOAT)
4656 {
4657 f2 = var2.vval.v_float;
4658 n2 = 0;
4659 }
4660 else
4661#endif
4662 {
4663 n2 = get_tv_number_chk(&var2, &error);
4664 if (error)
4665 {
4666 clear_tv(rettv);
4667 clear_tv(&var2);
4668 return FAIL;
4669 }
4670#ifdef FEAT_FLOAT
4671 if (rettv->v_type == VAR_FLOAT)
4672 f2 = n2;
4673#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004674 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004676
4677#ifdef FEAT_FLOAT
4678 /* If there is a float on either side the result is a float. */
4679 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4680 {
4681 if (op == '+')
4682 f1 = f1 + f2;
4683 else
4684 f1 = f1 - f2;
4685 rettv->v_type = VAR_FLOAT;
4686 rettv->vval.v_float = f1;
4687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004689#endif
4690 {
4691 if (op == '+')
4692 n1 = n1 + n2;
4693 else
4694 n1 = n1 - n2;
4695 rettv->v_type = VAR_NUMBER;
4696 rettv->vval.v_number = n1;
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
4701 }
4702 return OK;
4703}
4704
4705/*
4706 * Handle fifth level expression:
4707 * * number multiplication
4708 * / number division
4709 * % number modulo
4710 *
4711 * "arg" must point to the first non-white of the expression.
4712 * "arg" is advanced to the next non-white after the recognized expression.
4713 *
4714 * Return OK or FAIL.
4715 */
4716 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004717eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004721 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722{
Bram Moolenaar33570922005-01-25 22:26:29 +00004723 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 int op;
4725 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004726#ifdef FEAT_FLOAT
4727 int use_float = FALSE;
4728 float_T f1 = 0, f2;
4729#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004730 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731
4732 /*
4733 * Get the first variable.
4734 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004735 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 return FAIL;
4737
4738 /*
4739 * Repeat computing, until no '*', '/' or '%' is following.
4740 */
4741 for (;;)
4742 {
4743 op = **arg;
4744 if (op != '*' && op != '/' && op != '%')
4745 break;
4746
4747 if (evaluate)
4748 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004749#ifdef FEAT_FLOAT
4750 if (rettv->v_type == VAR_FLOAT)
4751 {
4752 f1 = rettv->vval.v_float;
4753 use_float = TRUE;
4754 n1 = 0;
4755 }
4756 else
4757#endif
4758 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004759 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004760 if (error)
4761 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 }
4763 else
4764 n1 = 0;
4765
4766 /*
4767 * Get the second variable.
4768 */
4769 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004770 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 return FAIL;
4772
4773 if (evaluate)
4774 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004775#ifdef FEAT_FLOAT
4776 if (var2.v_type == VAR_FLOAT)
4777 {
4778 if (!use_float)
4779 {
4780 f1 = n1;
4781 use_float = TRUE;
4782 }
4783 f2 = var2.vval.v_float;
4784 n2 = 0;
4785 }
4786 else
4787#endif
4788 {
4789 n2 = get_tv_number_chk(&var2, &error);
4790 clear_tv(&var2);
4791 if (error)
4792 return FAIL;
4793#ifdef FEAT_FLOAT
4794 if (use_float)
4795 f2 = n2;
4796#endif
4797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 /*
4800 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803#ifdef FEAT_FLOAT
4804 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004806 if (op == '*')
4807 f1 = f1 * f2;
4808 else if (op == '/')
4809 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004810# ifdef VMS
4811 /* VMS crashes on divide by zero, work around it */
4812 if (f2 == 0.0)
4813 {
4814 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004815 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004816 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004817 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004818 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004819 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004820 }
4821 else
4822 f1 = f1 / f2;
4823# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004824 /* We rely on the floating point library to handle divide
4825 * by zero to result in "inf" and not a crash. */
4826 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004827# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004831 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832 return FAIL;
4833 }
4834 rettv->v_type = VAR_FLOAT;
4835 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
4837 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840 if (op == '*')
4841 n1 = n1 * n2;
4842 else if (op == '/')
4843 {
4844 if (n2 == 0) /* give an error message? */
4845 {
4846 if (n1 == 0)
4847 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4848 else if (n1 < 0)
4849 n1 = -0x7fffffffL;
4850 else
4851 n1 = 0x7fffffffL;
4852 }
4853 else
4854 n1 = n1 / n2;
4855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004857 {
4858 if (n2 == 0) /* give an error message? */
4859 n1 = 0;
4860 else
4861 n1 = n1 % n2;
4862 }
4863 rettv->v_type = VAR_NUMBER;
4864 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867 }
4868
4869 return OK;
4870}
4871
4872/*
4873 * Handle sixth level expression:
4874 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004875 * "string" string constant
4876 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 * &option-name option value
4878 * @r register contents
4879 * identifier variable value
4880 * function() function call
4881 * $VAR environment variable
4882 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004883 * [expr, expr] List
4884 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 *
4886 * Also handle:
4887 * ! in front logical NOT
4888 * - in front unary minus
4889 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004890 * trailing [] subscript in String or List
4891 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 *
4893 * "arg" must point to the first non-white of the expression.
4894 * "arg" is advanced to the next non-white after the recognized expression.
4895 *
4896 * Return OK or FAIL.
4897 */
4898 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004899eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004903 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 long n;
4906 int len;
4907 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 char_u *start_leader, *end_leader;
4909 int ret = OK;
4910 char_u *alias;
4911
4912 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004913 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004914 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004916 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
4918 /*
4919 * Skip '!' and '-' characters. They are handled later.
4920 */
4921 start_leader = *arg;
4922 while (**arg == '!' || **arg == '-' || **arg == '+')
4923 *arg = skipwhite(*arg + 1);
4924 end_leader = *arg;
4925
4926 switch (**arg)
4927 {
4928 /*
4929 * Number constant.
4930 */
4931 case '0':
4932 case '1':
4933 case '2':
4934 case '3':
4935 case '4':
4936 case '5':
4937 case '6':
4938 case '7':
4939 case '8':
4940 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004941 {
4942#ifdef FEAT_FLOAT
4943 char_u *p = skipdigits(*arg + 1);
4944 int get_float = FALSE;
4945
4946 /* We accept a float when the format matches
4947 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004948 * strict to avoid backwards compatibility problems.
4949 * Don't look for a float after the "." operator, so that
4950 * ":let vers = 1.2.3" doesn't fail. */
4951 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004953 get_float = TRUE;
4954 p = skipdigits(p + 2);
4955 if (*p == 'e' || *p == 'E')
4956 {
4957 ++p;
4958 if (*p == '-' || *p == '+')
4959 ++p;
4960 if (!vim_isdigit(*p))
4961 get_float = FALSE;
4962 else
4963 p = skipdigits(p + 1);
4964 }
4965 if (ASCII_ISALPHA(*p) || *p == '.')
4966 get_float = FALSE;
4967 }
4968 if (get_float)
4969 {
4970 float_T f;
4971
4972 *arg += string2float(*arg, &f);
4973 if (evaluate)
4974 {
4975 rettv->v_type = VAR_FLOAT;
4976 rettv->vval.v_float = f;
4977 }
4978 }
4979 else
4980#endif
4981 {
4982 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4983 *arg += len;
4984 if (evaluate)
4985 {
4986 rettv->v_type = VAR_NUMBER;
4987 rettv->vval.v_number = n;
4988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 /*
4994 * String constant: "string".
4995 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004996 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 break;
4998
4999 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005000 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005002 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005003 break;
5004
5005 /*
5006 * List: [expr, expr]
5007 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005008 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 break;
5010
5011 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005012 * Dictionary: {key: val, key: val}
5013 */
5014 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5015 break;
5016
5017 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005018 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005020 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 break;
5022
5023 /*
5024 * Environment variable: $VAR.
5025 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005026 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 break;
5028
5029 /*
5030 * Register contents: @r.
5031 */
5032 case '@': ++*arg;
5033 if (evaluate)
5034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005035 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005036 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 if (**arg != NUL)
5039 ++*arg;
5040 break;
5041
5042 /*
5043 * nested expression: (expression).
5044 */
5045 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005046 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 if (**arg == ')')
5048 ++*arg;
5049 else if (ret == OK)
5050 {
5051 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 ret = FAIL;
5054 }
5055 break;
5056
Bram Moolenaar8c711452005-01-14 21:53:12 +00005057 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 break;
5059 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005060
5061 if (ret == NOTDONE)
5062 {
5063 /*
5064 * Must be a variable or function name.
5065 * Can also be a curly-braces kind of name: {expr}.
5066 */
5067 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005068 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005069 if (alias != NULL)
5070 s = alias;
5071
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005072 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005073 ret = FAIL;
5074 else
5075 {
5076 if (**arg == '(') /* recursive! */
5077 {
5078 /* If "s" is the name of a variable of type VAR_FUNC
5079 * use its contents. */
5080 s = deref_func_name(s, &len);
5081
5082 /* Invoke the function. */
5083 ret = get_func_tv(s, len, rettv, arg,
5084 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005085 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 /* Stop the expression evaluation when immediately
5087 * aborting on error, or when an interrupt occurred or
5088 * an exception was thrown but not caught. */
5089 if (aborting())
5090 {
5091 if (ret == OK)
5092 clear_tv(rettv);
5093 ret = FAIL;
5094 }
5095 }
5096 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005097 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005098 else
5099 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 }
5101
5102 if (alias != NULL)
5103 vim_free(alias);
5104 }
5105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 *arg = skipwhite(*arg);
5107
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005108 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5109 * expr(expr). */
5110 if (ret == OK)
5111 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112
5113 /*
5114 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5115 */
5116 if (ret == OK && evaluate && end_leader > start_leader)
5117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005118 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005119 int val = 0;
5120#ifdef FEAT_FLOAT
5121 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005122
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005123 if (rettv->v_type == VAR_FLOAT)
5124 f = rettv->vval.v_float;
5125 else
5126#endif
5127 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005128 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005130 clear_tv(rettv);
5131 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005133 else
5134 {
5135 while (end_leader > start_leader)
5136 {
5137 --end_leader;
5138 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005139 {
5140#ifdef FEAT_FLOAT
5141 if (rettv->v_type == VAR_FLOAT)
5142 f = !f;
5143 else
5144#endif
5145 val = !val;
5146 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005147 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005148 {
5149#ifdef FEAT_FLOAT
5150 if (rettv->v_type == VAR_FLOAT)
5151 f = -f;
5152 else
5153#endif
5154 val = -val;
5155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005156 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005157#ifdef FEAT_FLOAT
5158 if (rettv->v_type == VAR_FLOAT)
5159 {
5160 clear_tv(rettv);
5161 rettv->vval.v_float = f;
5162 }
5163 else
5164#endif
5165 {
5166 clear_tv(rettv);
5167 rettv->v_type = VAR_NUMBER;
5168 rettv->vval.v_number = val;
5169 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172
5173 return ret;
5174}
5175
5176/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005177 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5178 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5180 */
5181 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005182eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005184 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005186 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187{
5188 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005189 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191 long len = -1;
5192 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005194 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 if (rettv->v_type == VAR_FUNC
5197#ifdef FEAT_FLOAT
5198 || rettv->v_type == VAR_FLOAT
5199#endif
5200 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005202 if (verbose)
5203 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005204 return FAIL;
5205 }
5206
Bram Moolenaar8c711452005-01-14 21:53:12 +00005207 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 /*
5210 * dict.name
5211 */
5212 key = *arg + 1;
5213 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5214 ;
5215 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005216 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218 }
5219 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005220 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 /*
5222 * something[idx]
5223 *
5224 * Get the (first) variable from inside the [].
5225 */
5226 *arg = skipwhite(*arg + 1);
5227 if (**arg == ':')
5228 empty1 = TRUE;
5229 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5230 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005231 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5232 {
5233 /* not a number or string */
5234 clear_tv(&var1);
5235 return FAIL;
5236 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237
5238 /*
5239 * Get the second variable from inside the [:].
5240 */
5241 if (**arg == ':')
5242 {
5243 range = TRUE;
5244 *arg = skipwhite(*arg + 1);
5245 if (**arg == ']')
5246 empty2 = TRUE;
5247 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5248 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005249 if (!empty1)
5250 clear_tv(&var1);
5251 return FAIL;
5252 }
5253 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5254 {
5255 /* not a number or string */
5256 if (!empty1)
5257 clear_tv(&var1);
5258 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005259 return FAIL;
5260 }
5261 }
5262
5263 /* Check for the ']'. */
5264 if (**arg != ']')
5265 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005266 if (verbose)
5267 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 clear_tv(&var1);
5269 if (range)
5270 clear_tv(&var2);
5271 return FAIL;
5272 }
5273 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 }
5275
5276 if (evaluate)
5277 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005278 n1 = 0;
5279 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005281 n1 = get_tv_number(&var1);
5282 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283 }
5284 if (range)
5285 {
5286 if (empty2)
5287 n2 = -1;
5288 else
5289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005290 n2 = get_tv_number(&var2);
5291 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005292 }
5293 }
5294
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005295 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005296 {
5297 case VAR_NUMBER:
5298 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005299 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 len = (long)STRLEN(s);
5301 if (range)
5302 {
5303 /* The resulting variable is a substring. If the indexes
5304 * are out of range the result is empty. */
5305 if (n1 < 0)
5306 {
5307 n1 = len + n1;
5308 if (n1 < 0)
5309 n1 = 0;
5310 }
5311 if (n2 < 0)
5312 n2 = len + n2;
5313 else if (n2 >= len)
5314 n2 = len;
5315 if (n1 >= len || n2 < 0 || n1 > n2)
5316 s = NULL;
5317 else
5318 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5319 }
5320 else
5321 {
5322 /* The resulting variable is a string of a single
5323 * character. If the index is too big or negative the
5324 * result is empty. */
5325 if (n1 >= len || n1 < 0)
5326 s = NULL;
5327 else
5328 s = vim_strnsave(s + n1, 1);
5329 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005330 clear_tv(rettv);
5331 rettv->v_type = VAR_STRING;
5332 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005333 break;
5334
5335 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005336 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 if (n1 < 0)
5338 n1 = len + n1;
5339 if (!empty1 && (n1 < 0 || n1 >= len))
5340 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005341 /* For a range we allow invalid values and return an empty
5342 * list. A list index out of range is an error. */
5343 if (!range)
5344 {
5345 if (verbose)
5346 EMSGN(_(e_listidx), n1);
5347 return FAIL;
5348 }
5349 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005350 }
5351 if (range)
5352 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 list_T *l;
5354 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355
5356 if (n2 < 0)
5357 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005358 else if (n2 >= len)
5359 n2 = len - 1;
5360 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005361 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 l = list_alloc();
5363 if (l == NULL)
5364 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 n1 <= n2; ++n1)
5367 {
5368 if (list_append_tv(l, &item->li_tv) == FAIL)
5369 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005370 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371 return FAIL;
5372 }
5373 item = item->li_next;
5374 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 clear_tv(rettv);
5376 rettv->v_type = VAR_LIST;
5377 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005378 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 }
5380 else
5381 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005382 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005383 clear_tv(rettv);
5384 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 }
5386 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005387
5388 case VAR_DICT:
5389 if (range)
5390 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005391 if (verbose)
5392 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 if (len == -1)
5394 clear_tv(&var1);
5395 return FAIL;
5396 }
5397 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005398 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399
5400 if (len == -1)
5401 {
5402 key = get_tv_string(&var1);
5403 if (*key == NUL)
5404 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005405 if (verbose)
5406 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 clear_tv(&var1);
5408 return FAIL;
5409 }
5410 }
5411
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005412 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005413
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005414 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005415 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416 if (len == -1)
5417 clear_tv(&var1);
5418 if (item == NULL)
5419 return FAIL;
5420
5421 copy_tv(&item->di_tv, &var1);
5422 clear_tv(rettv);
5423 *rettv = var1;
5424 }
5425 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 }
5427 }
5428
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 return OK;
5430}
5431
5432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 * Get an option value.
5434 * "arg" points to the '&' or '+' before the option name.
5435 * "arg" is advanced to character after the option name.
5436 * Return OK or FAIL.
5437 */
5438 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005439get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005441 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 int evaluate;
5443{
5444 char_u *option_end;
5445 long numval;
5446 char_u *stringval;
5447 int opt_type;
5448 int c;
5449 int working = (**arg == '+'); /* has("+option") */
5450 int ret = OK;
5451 int opt_flags;
5452
5453 /*
5454 * Isolate the option name and find its value.
5455 */
5456 option_end = find_option_end(arg, &opt_flags);
5457 if (option_end == NULL)
5458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005459 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 EMSG2(_("E112: Option name missing: %s"), *arg);
5461 return FAIL;
5462 }
5463
5464 if (!evaluate)
5465 {
5466 *arg = option_end;
5467 return OK;
5468 }
5469
5470 c = *option_end;
5471 *option_end = NUL;
5472 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005473 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 if (opt_type == -3) /* invalid name */
5476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005477 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 EMSG2(_("E113: Unknown option: %s"), *arg);
5479 ret = FAIL;
5480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
5483 if (opt_type == -2) /* hidden string option */
5484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 rettv->v_type = VAR_STRING;
5486 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 }
5488 else if (opt_type == -1) /* hidden number option */
5489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 rettv->v_type = VAR_NUMBER;
5491 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493 else if (opt_type == 1) /* number option */
5494 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005495 rettv->v_type = VAR_NUMBER;
5496 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 }
5498 else /* string option */
5499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 rettv->v_type = VAR_STRING;
5501 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 }
5503 }
5504 else if (working && (opt_type == -2 || opt_type == -1))
5505 ret = FAIL;
5506
5507 *option_end = c; /* put back for error messages */
5508 *arg = option_end;
5509
5510 return ret;
5511}
5512
5513/*
5514 * Allocate a variable for a string constant.
5515 * Return OK or FAIL.
5516 */
5517 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005520 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 int evaluate;
5522{
5523 char_u *p;
5524 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 int extra = 0;
5526
5527 /*
5528 * Find the end of the string, skipping backslashed characters.
5529 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005530 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 if (*p == '\\' && p[1] != NUL)
5533 {
5534 ++p;
5535 /* A "\<x>" form occupies at least 4 characters, and produces up
5536 * to 6 characters: reserve space for 2 extra */
5537 if (*p == '<')
5538 extra += 2;
5539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541
5542 if (*p != '"')
5543 {
5544 EMSG2(_("E114: Missing quote: %s"), *arg);
5545 return FAIL;
5546 }
5547
5548 /* If only parsing, set *arg and return here */
5549 if (!evaluate)
5550 {
5551 *arg = p + 1;
5552 return OK;
5553 }
5554
5555 /*
5556 * Copy the string into allocated memory, handling backslashed
5557 * characters.
5558 */
5559 name = alloc((unsigned)(p - *arg + extra));
5560 if (name == NULL)
5561 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005562 rettv->v_type = VAR_STRING;
5563 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
Bram Moolenaar8c711452005-01-14 21:53:12 +00005565 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
5567 if (*p == '\\')
5568 {
5569 switch (*++p)
5570 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005571 case 'b': *name++ = BS; ++p; break;
5572 case 'e': *name++ = ESC; ++p; break;
5573 case 'f': *name++ = FF; ++p; break;
5574 case 'n': *name++ = NL; ++p; break;
5575 case 'r': *name++ = CAR; ++p; break;
5576 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
5578 case 'X': /* hex: "\x1", "\x12" */
5579 case 'x':
5580 case 'u': /* Unicode: "\u0023" */
5581 case 'U':
5582 if (vim_isxdigit(p[1]))
5583 {
5584 int n, nr;
5585 int c = toupper(*p);
5586
5587 if (c == 'X')
5588 n = 2;
5589 else
5590 n = 4;
5591 nr = 0;
5592 while (--n >= 0 && vim_isxdigit(p[1]))
5593 {
5594 ++p;
5595 nr = (nr << 4) + hex2nr(*p);
5596 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005597 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598#ifdef FEAT_MBYTE
5599 /* For "\u" store the number according to
5600 * 'encoding'. */
5601 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005602 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 else
5604#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005605 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 break;
5608
5609 /* octal: "\1", "\12", "\123" */
5610 case '0':
5611 case '1':
5612 case '2':
5613 case '3':
5614 case '4':
5615 case '5':
5616 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005617 case '7': *name = *p++ - '0';
5618 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620 *name = (*name << 3) + *p++ - '0';
5621 if (*p >= '0' && *p <= '7')
5622 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005624 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 break;
5626
5627 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005628 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 if (extra != 0)
5630 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005631 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 break;
5633 }
5634 /* FALLTHROUGH */
5635
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 break;
5638 }
5639 }
5640 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005641 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 *arg = p + 1;
5646
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 return OK;
5648}
5649
5650/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 * Return OK or FAIL.
5653 */
5654 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005655get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 int evaluate;
5659{
5660 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005662 int reduce = 0;
5663
5664 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005665 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005666 */
5667 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5668 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005669 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005672 break;
5673 ++reduce;
5674 ++p;
5675 }
5676 }
5677
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005681 return FAIL;
5682 }
5683
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005685 if (!evaluate)
5686 {
5687 *arg = p + 1;
5688 return OK;
5689 }
5690
5691 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005693 */
5694 str = alloc((unsigned)((p - *arg) - reduce));
5695 if (str == NULL)
5696 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 rettv->v_type = VAR_STRING;
5698 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005699
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005705 break;
5706 ++p;
5707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005708 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711 *arg = p + 1;
5712
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 return OK;
5714}
5715
5716/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005717 * Allocate a variable for a List and fill it from "*arg".
5718 * Return OK or FAIL.
5719 */
5720 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005721get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005722 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724 int evaluate;
5725{
Bram Moolenaar33570922005-01-25 22:26:29 +00005726 list_T *l = NULL;
5727 typval_T tv;
5728 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729
5730 if (evaluate)
5731 {
5732 l = list_alloc();
5733 if (l == NULL)
5734 return FAIL;
5735 }
5736
5737 *arg = skipwhite(*arg + 1);
5738 while (**arg != ']' && **arg != NUL)
5739 {
5740 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5741 goto failret;
5742 if (evaluate)
5743 {
5744 item = listitem_alloc();
5745 if (item != NULL)
5746 {
5747 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005748 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 list_append(l, item);
5750 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005751 else
5752 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 }
5754
5755 if (**arg == ']')
5756 break;
5757 if (**arg != ',')
5758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005760 goto failret;
5761 }
5762 *arg = skipwhite(*arg + 1);
5763 }
5764
5765 if (**arg != ']')
5766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768failret:
5769 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005770 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 return FAIL;
5772 }
5773
5774 *arg = skipwhite(*arg + 1);
5775 if (evaluate)
5776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005777 rettv->v_type = VAR_LIST;
5778 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005779 ++l->lv_refcount;
5780 }
5781
5782 return OK;
5783}
5784
5785/*
5786 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005787 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005788 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005789 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790list_alloc()
5791{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005792 list_T *l;
5793
5794 l = (list_T *)alloc_clear(sizeof(list_T));
5795 if (l != NULL)
5796 {
5797 /* Prepend the list to the list of lists for garbage collection. */
5798 if (first_list != NULL)
5799 first_list->lv_used_prev = l;
5800 l->lv_used_prev = NULL;
5801 l->lv_used_next = first_list;
5802 first_list = l;
5803 }
5804 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805}
5806
5807/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005808 * Allocate an empty list for a return value.
5809 * Returns OK or FAIL.
5810 */
5811 static int
5812rettv_list_alloc(rettv)
5813 typval_T *rettv;
5814{
5815 list_T *l = list_alloc();
5816
5817 if (l == NULL)
5818 return FAIL;
5819
5820 rettv->vval.v_list = l;
5821 rettv->v_type = VAR_LIST;
5822 ++l->lv_refcount;
5823 return OK;
5824}
5825
5826/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005827 * Unreference a list: decrement the reference count and free it when it
5828 * becomes zero.
5829 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005830 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005832 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005834 if (l != NULL && --l->lv_refcount <= 0)
5835 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836}
5837
5838/*
5839 * Free a list, including all items it points to.
5840 * Ignores the reference count.
5841 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005842 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005843list_free(l, recurse)
5844 list_T *l;
5845 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846{
Bram Moolenaar33570922005-01-25 22:26:29 +00005847 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005849 /* Remove the list from the list of lists for garbage collection. */
5850 if (l->lv_used_prev == NULL)
5851 first_list = l->lv_used_next;
5852 else
5853 l->lv_used_prev->lv_used_next = l->lv_used_next;
5854 if (l->lv_used_next != NULL)
5855 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5856
Bram Moolenaard9fba312005-06-26 22:34:35 +00005857 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005859 /* Remove the item before deleting it. */
5860 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005861 if (recurse || (item->li_tv.v_type != VAR_LIST
5862 && item->li_tv.v_type != VAR_DICT))
5863 clear_tv(&item->li_tv);
5864 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 }
5866 vim_free(l);
5867}
5868
5869/*
5870 * Allocate a list item.
5871 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005872 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873listitem_alloc()
5874{
Bram Moolenaar33570922005-01-25 22:26:29 +00005875 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005876}
5877
5878/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005879 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005880 */
5881 static void
5882listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005883 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005884{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005885 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886 vim_free(item);
5887}
5888
5889/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005890 * Remove a list item from a List and free it. Also clears the value.
5891 */
5892 static void
5893listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005894 list_T *l;
5895 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005896{
5897 list_remove(l, item, item);
5898 listitem_free(item);
5899}
5900
5901/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 * Get the number of items in a list.
5903 */
5904 static long
5905list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005908 if (l == NULL)
5909 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005910 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911}
5912
5913/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005914 * Return TRUE when two lists have exactly the same values.
5915 */
5916 static int
5917list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 list_T *l1;
5919 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005920 int ic; /* ignore case for strings */
5921{
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005923
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005924 if (l1 == NULL || l2 == NULL)
5925 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005926 if (l1 == l2)
5927 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005928 if (list_len(l1) != list_len(l2))
5929 return FALSE;
5930
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005931 for (item1 = l1->lv_first, item2 = l2->lv_first;
5932 item1 != NULL && item2 != NULL;
5933 item1 = item1->li_next, item2 = item2->li_next)
5934 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5935 return FALSE;
5936 return item1 == NULL && item2 == NULL;
5937}
5938
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005939#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5940 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005941/*
5942 * Return the dictitem that an entry in a hashtable points to.
5943 */
5944 dictitem_T *
5945dict_lookup(hi)
5946 hashitem_T *hi;
5947{
5948 return HI2DI(hi);
5949}
5950#endif
5951
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005952/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005953 * Return TRUE when two dictionaries have exactly the same key/values.
5954 */
5955 static int
5956dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 dict_T *d1;
5958 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005959 int ic; /* ignore case for strings */
5960{
Bram Moolenaar33570922005-01-25 22:26:29 +00005961 hashitem_T *hi;
5962 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005963 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005964
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005965 if (d1 == NULL || d2 == NULL)
5966 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005967 if (d1 == d2)
5968 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005969 if (dict_len(d1) != dict_len(d2))
5970 return FALSE;
5971
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005972 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005973 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005975 if (!HASHITEM_EMPTY(hi))
5976 {
5977 item2 = dict_find(d2, hi->hi_key, -1);
5978 if (item2 == NULL)
5979 return FALSE;
5980 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5981 return FALSE;
5982 --todo;
5983 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005984 }
5985 return TRUE;
5986}
5987
5988/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005990 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005991 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005992 */
5993 static int
5994tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 typval_T *tv1;
5996 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 int ic; /* ignore case */
5998{
5999 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006000 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006001 static int recursive = 0; /* cach recursive loops */
6002 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006004 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006005 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006006 /* Catch lists and dicts that have an endless loop by limiting
6007 * recursiveness to 1000. We guess they are equal then. */
6008 if (recursive >= 1000)
6009 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006010
6011 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006012 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006013 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006014 ++recursive;
6015 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
6016 --recursive;
6017 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006018
6019 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006020 ++recursive;
6021 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
6022 --recursive;
6023 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006024
6025 case VAR_FUNC:
6026 return (tv1->vval.v_string != NULL
6027 && tv2->vval.v_string != NULL
6028 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6029
6030 case VAR_NUMBER:
6031 return tv1->vval.v_number == tv2->vval.v_number;
6032
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006033#ifdef FEAT_FLOAT
6034 case VAR_FLOAT:
6035 return tv1->vval.v_float == tv2->vval.v_float;
6036#endif
6037
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006038 case VAR_STRING:
6039 s1 = get_tv_string_buf(tv1, buf1);
6040 s2 = get_tv_string_buf(tv2, buf2);
6041 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006042 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006043
6044 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006045 return TRUE;
6046}
6047
6048/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 * Locate item with index "n" in list "l" and return it.
6050 * A negative index is counted from the end; -1 is the last item.
6051 * Returns NULL when "n" is out of range.
6052 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 long n;
6057{
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 long idx;
6060
6061 if (l == NULL)
6062 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006063
6064 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006066 n = l->lv_len + n;
6067
6068 /* Check for index out of range. */
6069 if (n < 0 || n >= l->lv_len)
6070 return NULL;
6071
6072 /* When there is a cached index may start search from there. */
6073 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006075 if (n < l->lv_idx / 2)
6076 {
6077 /* closest to the start of the list */
6078 item = l->lv_first;
6079 idx = 0;
6080 }
6081 else if (n > (l->lv_idx + l->lv_len) / 2)
6082 {
6083 /* closest to the end of the list */
6084 item = l->lv_last;
6085 idx = l->lv_len - 1;
6086 }
6087 else
6088 {
6089 /* closest to the cached index */
6090 item = l->lv_idx_item;
6091 idx = l->lv_idx;
6092 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 }
6094 else
6095 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006096 if (n < l->lv_len / 2)
6097 {
6098 /* closest to the start of the list */
6099 item = l->lv_first;
6100 idx = 0;
6101 }
6102 else
6103 {
6104 /* closest to the end of the list */
6105 item = l->lv_last;
6106 idx = l->lv_len - 1;
6107 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006109
6110 while (n > idx)
6111 {
6112 /* search forward */
6113 item = item->li_next;
6114 ++idx;
6115 }
6116 while (n < idx)
6117 {
6118 /* search backward */
6119 item = item->li_prev;
6120 --idx;
6121 }
6122
6123 /* cache the used index */
6124 l->lv_idx = idx;
6125 l->lv_idx_item = item;
6126
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006127 return item;
6128}
6129
6130/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006131 * Get list item "l[idx]" as a number.
6132 */
6133 static long
6134list_find_nr(l, idx, errorp)
6135 list_T *l;
6136 long idx;
6137 int *errorp; /* set to TRUE when something wrong */
6138{
6139 listitem_T *li;
6140
6141 li = list_find(l, idx);
6142 if (li == NULL)
6143 {
6144 if (errorp != NULL)
6145 *errorp = TRUE;
6146 return -1L;
6147 }
6148 return get_tv_number_chk(&li->li_tv, errorp);
6149}
6150
6151/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006152 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6153 */
6154 char_u *
6155list_find_str(l, idx)
6156 list_T *l;
6157 long idx;
6158{
6159 listitem_T *li;
6160
6161 li = list_find(l, idx - 1);
6162 if (li == NULL)
6163 {
6164 EMSGN(_(e_listidx), idx);
6165 return NULL;
6166 }
6167 return get_tv_string(&li->li_tv);
6168}
6169
6170/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006171 * Locate "item" list "l" and return its index.
6172 * Returns -1 when "item" is not in the list.
6173 */
6174 static long
6175list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 list_T *l;
6177 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006178{
6179 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006180 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006181
6182 if (l == NULL)
6183 return -1;
6184 idx = 0;
6185 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6186 ++idx;
6187 if (li == NULL)
6188 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006189 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006190}
6191
6192/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 * Append item "item" to the end of list "l".
6194 */
6195 static void
6196list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006197 list_T *l;
6198 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006199{
6200 if (l->lv_last == NULL)
6201 {
6202 /* empty list */
6203 l->lv_first = item;
6204 l->lv_last = item;
6205 item->li_prev = NULL;
6206 }
6207 else
6208 {
6209 l->lv_last->li_next = item;
6210 item->li_prev = l->lv_last;
6211 l->lv_last = item;
6212 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006213 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006214 item->li_next = NULL;
6215}
6216
6217/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006218 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006220 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006221 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006222list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006223 list_T *l;
6224 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006225{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006226 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227
Bram Moolenaar05159a02005-02-26 23:04:13 +00006228 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006229 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006230 copy_tv(tv, &li->li_tv);
6231 list_append(l, li);
6232 return OK;
6233}
6234
6235/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006236 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006237 * Return FAIL when out of memory.
6238 */
6239 int
6240list_append_dict(list, dict)
6241 list_T *list;
6242 dict_T *dict;
6243{
6244 listitem_T *li = listitem_alloc();
6245
6246 if (li == NULL)
6247 return FAIL;
6248 li->li_tv.v_type = VAR_DICT;
6249 li->li_tv.v_lock = 0;
6250 li->li_tv.vval.v_dict = dict;
6251 list_append(list, li);
6252 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006253 return OK;
6254}
6255
6256/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006257 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006258 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006259 * Returns FAIL when out of memory.
6260 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006261 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006262list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006263 list_T *l;
6264 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006265 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006266{
6267 listitem_T *li = listitem_alloc();
6268
6269 if (li == NULL)
6270 return FAIL;
6271 list_append(l, li);
6272 li->li_tv.v_type = VAR_STRING;
6273 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006274 if (str == NULL)
6275 li->li_tv.vval.v_string = NULL;
6276 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006277 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006278 return FAIL;
6279 return OK;
6280}
6281
6282/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006283 * Append "n" to list "l".
6284 * Returns FAIL when out of memory.
6285 */
6286 static int
6287list_append_number(l, n)
6288 list_T *l;
6289 varnumber_T n;
6290{
6291 listitem_T *li;
6292
6293 li = listitem_alloc();
6294 if (li == NULL)
6295 return FAIL;
6296 li->li_tv.v_type = VAR_NUMBER;
6297 li->li_tv.v_lock = 0;
6298 li->li_tv.vval.v_number = n;
6299 list_append(l, li);
6300 return OK;
6301}
6302
6303/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006304 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006305 * If "item" is NULL append at the end.
6306 * Return FAIL when out of memory.
6307 */
6308 static int
6309list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006310 list_T *l;
6311 typval_T *tv;
6312 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006313{
Bram Moolenaar33570922005-01-25 22:26:29 +00006314 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006315
6316 if (ni == NULL)
6317 return FAIL;
6318 copy_tv(tv, &ni->li_tv);
6319 if (item == NULL)
6320 /* Append new item at end of list. */
6321 list_append(l, ni);
6322 else
6323 {
6324 /* Insert new item before existing item. */
6325 ni->li_prev = item->li_prev;
6326 ni->li_next = item;
6327 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006328 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006329 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006330 ++l->lv_idx;
6331 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006333 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006334 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006335 l->lv_idx_item = NULL;
6336 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006338 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006339 }
6340 return OK;
6341}
6342
6343/*
6344 * Extend "l1" with "l2".
6345 * If "bef" is NULL append at the end, otherwise insert before this item.
6346 * Returns FAIL when out of memory.
6347 */
6348 static int
6349list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 list_T *l1;
6351 list_T *l2;
6352 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006353{
Bram Moolenaar33570922005-01-25 22:26:29 +00006354 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006355 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006356
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006357 /* We also quit the loop when we have inserted the original item count of
6358 * the list, avoid a hang when we extend a list with itself. */
6359 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006360 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6361 return FAIL;
6362 return OK;
6363}
6364
6365/*
6366 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6367 * Return FAIL when out of memory.
6368 */
6369 static int
6370list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 list_T *l1;
6372 list_T *l2;
6373 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006374{
Bram Moolenaar33570922005-01-25 22:26:29 +00006375 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006376
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006377 if (l1 == NULL || l2 == NULL)
6378 return FAIL;
6379
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006380 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006381 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006382 if (l == NULL)
6383 return FAIL;
6384 tv->v_type = VAR_LIST;
6385 tv->vval.v_list = l;
6386
6387 /* append all items from the second list */
6388 return list_extend(l, l2, NULL);
6389}
6390
6391/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006392 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006394 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006395 * Returns NULL when out of memory.
6396 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006398list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006399 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006400 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006401 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402{
Bram Moolenaar33570922005-01-25 22:26:29 +00006403 list_T *copy;
6404 listitem_T *item;
6405 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006406
6407 if (orig == NULL)
6408 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006409
6410 copy = list_alloc();
6411 if (copy != NULL)
6412 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006413 if (copyID != 0)
6414 {
6415 /* Do this before adding the items, because one of the items may
6416 * refer back to this list. */
6417 orig->lv_copyID = copyID;
6418 orig->lv_copylist = copy;
6419 }
6420 for (item = orig->lv_first; item != NULL && !got_int;
6421 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006422 {
6423 ni = listitem_alloc();
6424 if (ni == NULL)
6425 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006426 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006427 {
6428 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6429 {
6430 vim_free(ni);
6431 break;
6432 }
6433 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006434 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006435 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006436 list_append(copy, ni);
6437 }
6438 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006439 if (item != NULL)
6440 {
6441 list_unref(copy);
6442 copy = NULL;
6443 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006444 }
6445
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446 return copy;
6447}
6448
6449/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006451 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006454list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006455 list_T *l;
6456 listitem_T *item;
6457 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458{
Bram Moolenaar33570922005-01-25 22:26:29 +00006459 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006461 /* notify watchers */
6462 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006464 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006465 list_fix_watch(l, ip);
6466 if (ip == item2)
6467 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006468 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006469
6470 if (item2->li_next == NULL)
6471 l->lv_last = item->li_prev;
6472 else
6473 item2->li_next->li_prev = item->li_prev;
6474 if (item->li_prev == NULL)
6475 l->lv_first = item2->li_next;
6476 else
6477 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006478 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006479}
6480
6481/*
6482 * Return an allocated string with the string representation of a list.
6483 * May return NULL.
6484 */
6485 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006486list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006487 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006488 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006489{
6490 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491
6492 if (tv->vval.v_list == NULL)
6493 return NULL;
6494 ga_init2(&ga, (int)sizeof(char), 80);
6495 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006496 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006497 {
6498 vim_free(ga.ga_data);
6499 return NULL;
6500 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006501 ga_append(&ga, ']');
6502 ga_append(&ga, NUL);
6503 return (char_u *)ga.ga_data;
6504}
6505
6506/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006507 * Join list "l" into a string in "*gap", using separator "sep".
6508 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006509 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006510 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006511 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006512list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006513 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006515 char_u *sep;
6516 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006517 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006518{
6519 int first = TRUE;
6520 char_u *tofree;
6521 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006522 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006523 char_u *s;
6524
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006525 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006526 {
6527 if (first)
6528 first = FALSE;
6529 else
6530 ga_concat(gap, sep);
6531
6532 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006533 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006534 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006535 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006536 if (s != NULL)
6537 ga_concat(gap, s);
6538 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006539 if (s == NULL)
6540 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006541 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006542 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006543 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006544}
6545
6546/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006547 * Garbage collection for lists and dictionaries.
6548 *
6549 * We use reference counts to be able to free most items right away when they
6550 * are no longer used. But for composite items it's possible that it becomes
6551 * unused while the reference count is > 0: When there is a recursive
6552 * reference. Example:
6553 * :let l = [1, 2, 3]
6554 * :let d = {9: l}
6555 * :let l[1] = d
6556 *
6557 * Since this is quite unusual we handle this with garbage collection: every
6558 * once in a while find out which lists and dicts are not referenced from any
6559 * variable.
6560 *
6561 * Here is a good reference text about garbage collection (refers to Python
6562 * but it applies to all reference-counting mechanisms):
6563 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006564 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006565
6566/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006567 * Do garbage collection for lists and dicts.
6568 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006569 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006570 int
6571garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006572{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006573 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006574 buf_T *buf;
6575 win_T *wp;
6576 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006577 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006578 int did_free;
6579 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006580#ifdef FEAT_WINDOWS
6581 tabpage_T *tp;
6582#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006583
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006584 /* Only do this once. */
6585 want_garbage_collect = FALSE;
6586 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006587 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006588
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006589 /* We advance by two because we add one for items referenced through
6590 * previous_funccal. */
6591 current_copyID += COPYID_INC;
6592 copyID = current_copyID;
6593
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006594 /*
6595 * 1. Go through all accessible variables and mark all lists and dicts
6596 * with copyID.
6597 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006598
6599 /* Don't free variables in the previous_funccal list unless they are only
6600 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006601 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006602 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6603 {
6604 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6605 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6606 }
6607
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006608 /* script-local variables */
6609 for (i = 1; i <= ga_scripts.ga_len; ++i)
6610 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6611
6612 /* buffer-local variables */
6613 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6614 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6615
6616 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006617 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006618 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6619
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006620#ifdef FEAT_WINDOWS
6621 /* tabpage-local variables */
6622 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6623 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6624#endif
6625
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006626 /* global variables */
6627 set_ref_in_ht(&globvarht, copyID);
6628
6629 /* function-local variables */
6630 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6631 {
6632 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6633 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6634 }
6635
Bram Moolenaard812df62008-11-09 12:46:09 +00006636 /* v: vars */
6637 set_ref_in_ht(&vimvarht, copyID);
6638
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006639 /*
6640 * 2. Free lists and dictionaries that are not referenced.
6641 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006642 did_free = free_unref_items(copyID);
6643
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006644 /*
6645 * 3. Check if any funccal can be freed now.
6646 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006647 for (pfc = &previous_funccal; *pfc != NULL; )
6648 {
6649 if (can_free_funccal(*pfc, copyID))
6650 {
6651 fc = *pfc;
6652 *pfc = fc->caller;
6653 free_funccal(fc, TRUE);
6654 did_free = TRUE;
6655 did_free_funccal = TRUE;
6656 }
6657 else
6658 pfc = &(*pfc)->caller;
6659 }
6660 if (did_free_funccal)
6661 /* When a funccal was freed some more items might be garbage
6662 * collected, so run again. */
6663 (void)garbage_collect();
6664
6665 return did_free;
6666}
6667
6668/*
6669 * Free lists and dictionaries that are no longer referenced.
6670 */
6671 static int
6672free_unref_items(copyID)
6673 int copyID;
6674{
6675 dict_T *dd;
6676 list_T *ll;
6677 int did_free = FALSE;
6678
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006679 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006680 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006681 */
6682 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006683 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006684 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006685 /* Free the Dictionary and ordinary items it contains, but don't
6686 * recurse into Lists and Dictionaries, they will be in the list
6687 * of dicts or list of lists. */
6688 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006689 did_free = TRUE;
6690
6691 /* restart, next dict may also have been freed */
6692 dd = first_dict;
6693 }
6694 else
6695 dd = dd->dv_used_next;
6696
6697 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006698 * Go through the list of lists and free items without the copyID.
6699 * But don't free a list that has a watcher (used in a for loop), these
6700 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006701 */
6702 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006703 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6704 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006705 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006706 /* Free the List and ordinary items it contains, but don't recurse
6707 * into Lists and Dictionaries, they will be in the list of dicts
6708 * or list of lists. */
6709 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006710 did_free = TRUE;
6711
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006712 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006713 ll = first_list;
6714 }
6715 else
6716 ll = ll->lv_used_next;
6717
6718 return did_free;
6719}
6720
6721/*
6722 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6723 */
6724 static void
6725set_ref_in_ht(ht, copyID)
6726 hashtab_T *ht;
6727 int copyID;
6728{
6729 int todo;
6730 hashitem_T *hi;
6731
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006732 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006733 for (hi = ht->ht_array; todo > 0; ++hi)
6734 if (!HASHITEM_EMPTY(hi))
6735 {
6736 --todo;
6737 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6738 }
6739}
6740
6741/*
6742 * Mark all lists and dicts referenced through list "l" with "copyID".
6743 */
6744 static void
6745set_ref_in_list(l, copyID)
6746 list_T *l;
6747 int copyID;
6748{
6749 listitem_T *li;
6750
6751 for (li = l->lv_first; li != NULL; li = li->li_next)
6752 set_ref_in_item(&li->li_tv, copyID);
6753}
6754
6755/*
6756 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6757 */
6758 static void
6759set_ref_in_item(tv, copyID)
6760 typval_T *tv;
6761 int copyID;
6762{
6763 dict_T *dd;
6764 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006765
6766 switch (tv->v_type)
6767 {
6768 case VAR_DICT:
6769 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006770 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006771 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006772 /* Didn't see this dict yet. */
6773 dd->dv_copyID = copyID;
6774 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006775 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006776 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006777
6778 case VAR_LIST:
6779 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006780 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006781 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006782 /* Didn't see this list yet. */
6783 ll->lv_copyID = copyID;
6784 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006785 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006786 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006787 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006788 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006789}
6790
6791/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006792 * Allocate an empty header for a dictionary.
6793 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006794 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006795dict_alloc()
6796{
Bram Moolenaar33570922005-01-25 22:26:29 +00006797 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006798
Bram Moolenaar33570922005-01-25 22:26:29 +00006799 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006800 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006801 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006802 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006803 if (first_dict != NULL)
6804 first_dict->dv_used_prev = d;
6805 d->dv_used_next = first_dict;
6806 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006807 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006808
Bram Moolenaar33570922005-01-25 22:26:29 +00006809 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006810 d->dv_lock = 0;
6811 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006812 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006813 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006814 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006815}
6816
6817/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006818 * Allocate an empty dict for a return value.
6819 * Returns OK or FAIL.
6820 */
6821 static int
6822rettv_dict_alloc(rettv)
6823 typval_T *rettv;
6824{
6825 dict_T *d = dict_alloc();
6826
6827 if (d == NULL)
6828 return FAIL;
6829
6830 rettv->vval.v_dict = d;
6831 rettv->v_type = VAR_DICT;
6832 ++d->dv_refcount;
6833 return OK;
6834}
6835
6836
6837/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006838 * Unreference a Dictionary: decrement the reference count and free it when it
6839 * becomes zero.
6840 */
6841 static void
6842dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006843 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006844{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006845 if (d != NULL && --d->dv_refcount <= 0)
6846 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006847}
6848
6849/*
6850 * Free a Dictionary, including all items it contains.
6851 * Ignores the reference count.
6852 */
6853 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006854dict_free(d, recurse)
6855 dict_T *d;
6856 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006857{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006858 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006859 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006860 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006861
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006862 /* Remove the dict from the list of dicts for garbage collection. */
6863 if (d->dv_used_prev == NULL)
6864 first_dict = d->dv_used_next;
6865 else
6866 d->dv_used_prev->dv_used_next = d->dv_used_next;
6867 if (d->dv_used_next != NULL)
6868 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6869
6870 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006871 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006872 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006873 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006874 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006875 if (!HASHITEM_EMPTY(hi))
6876 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006877 /* Remove the item before deleting it, just in case there is
6878 * something recursive causing trouble. */
6879 di = HI2DI(hi);
6880 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006881 if (recurse || (di->di_tv.v_type != VAR_LIST
6882 && di->di_tv.v_type != VAR_DICT))
6883 clear_tv(&di->di_tv);
6884 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006885 --todo;
6886 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006887 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006888 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006889 vim_free(d);
6890}
6891
6892/*
6893 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006894 * The "key" is copied to the new item.
6895 * Note that the value of the item "di_tv" still needs to be initialized!
6896 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006897 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006898 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006899dictitem_alloc(key)
6900 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006901{
Bram Moolenaar33570922005-01-25 22:26:29 +00006902 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006903
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006904 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006905 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006906 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006907 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006908 di->di_flags = 0;
6909 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006910 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006911}
6912
6913/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006914 * Make a copy of a Dictionary item.
6915 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006916 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006917dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006918 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006919{
Bram Moolenaar33570922005-01-25 22:26:29 +00006920 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006921
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006922 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6923 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006924 if (di != NULL)
6925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006926 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006927 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006928 copy_tv(&org->di_tv, &di->di_tv);
6929 }
6930 return di;
6931}
6932
6933/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006934 * Remove item "item" from Dictionary "dict" and free it.
6935 */
6936 static void
6937dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006938 dict_T *dict;
6939 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006940{
Bram Moolenaar33570922005-01-25 22:26:29 +00006941 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006942
Bram Moolenaar33570922005-01-25 22:26:29 +00006943 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006944 if (HASHITEM_EMPTY(hi))
6945 EMSG2(_(e_intern2), "dictitem_remove()");
6946 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006947 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006948 dictitem_free(item);
6949}
6950
6951/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006952 * Free a dict item. Also clears the value.
6953 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006954 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006955dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006956 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006957{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006958 clear_tv(&item->di_tv);
6959 vim_free(item);
6960}
6961
6962/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006963 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6964 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006965 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006966 * Returns NULL when out of memory.
6967 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006969dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006971 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006972 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006973{
Bram Moolenaar33570922005-01-25 22:26:29 +00006974 dict_T *copy;
6975 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006976 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006977 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006978
6979 if (orig == NULL)
6980 return NULL;
6981
6982 copy = dict_alloc();
6983 if (copy != NULL)
6984 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006985 if (copyID != 0)
6986 {
6987 orig->dv_copyID = copyID;
6988 orig->dv_copydict = copy;
6989 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006990 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006991 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006992 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006993 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006994 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006995 --todo;
6996
6997 di = dictitem_alloc(hi->hi_key);
6998 if (di == NULL)
6999 break;
7000 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007001 {
7002 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7003 copyID) == FAIL)
7004 {
7005 vim_free(di);
7006 break;
7007 }
7008 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007009 else
7010 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7011 if (dict_add(copy, di) == FAIL)
7012 {
7013 dictitem_free(di);
7014 break;
7015 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007016 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007017 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007018
Bram Moolenaare9a41262005-01-15 22:18:47 +00007019 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007020 if (todo > 0)
7021 {
7022 dict_unref(copy);
7023 copy = NULL;
7024 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007025 }
7026
7027 return copy;
7028}
7029
7030/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007032 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007034 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007035dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007036 dict_T *d;
7037 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007038{
Bram Moolenaar33570922005-01-25 22:26:29 +00007039 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007040}
7041
Bram Moolenaar8c711452005-01-14 21:53:12 +00007042/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007043 * Add a number or string entry to dictionary "d".
7044 * When "str" is NULL use number "nr", otherwise use "str".
7045 * Returns FAIL when out of memory and when key already exists.
7046 */
7047 int
7048dict_add_nr_str(d, key, nr, str)
7049 dict_T *d;
7050 char *key;
7051 long nr;
7052 char_u *str;
7053{
7054 dictitem_T *item;
7055
7056 item = dictitem_alloc((char_u *)key);
7057 if (item == NULL)
7058 return FAIL;
7059 item->di_tv.v_lock = 0;
7060 if (str == NULL)
7061 {
7062 item->di_tv.v_type = VAR_NUMBER;
7063 item->di_tv.vval.v_number = nr;
7064 }
7065 else
7066 {
7067 item->di_tv.v_type = VAR_STRING;
7068 item->di_tv.vval.v_string = vim_strsave(str);
7069 }
7070 if (dict_add(d, item) == FAIL)
7071 {
7072 dictitem_free(item);
7073 return FAIL;
7074 }
7075 return OK;
7076}
7077
7078/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007079 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007080 * Returns FAIL when out of memory and when key already exists.
7081 */
7082 int
7083dict_add_list(d, key, list)
7084 dict_T *d;
7085 char *key;
7086 list_T *list;
7087{
7088 dictitem_T *item;
7089
7090 item = dictitem_alloc((char_u *)key);
7091 if (item == NULL)
7092 return FAIL;
7093 item->di_tv.v_lock = 0;
7094 item->di_tv.v_type = VAR_LIST;
7095 item->di_tv.vval.v_list = list;
7096 if (dict_add(d, item) == FAIL)
7097 {
7098 dictitem_free(item);
7099 return FAIL;
7100 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007101 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007102 return OK;
7103}
7104
7105/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106 * Get the number of items in a Dictionary.
7107 */
7108 static long
7109dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007110 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007111{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007112 if (d == NULL)
7113 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007114 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007115}
7116
7117/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007118 * Find item "key[len]" in Dictionary "d".
7119 * If "len" is negative use strlen(key).
7120 * Returns NULL when not found.
7121 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007122 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125 char_u *key;
7126 int len;
7127{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128#define AKEYLEN 200
7129 char_u buf[AKEYLEN];
7130 char_u *akey;
7131 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007133
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007134 if (len < 0)
7135 akey = key;
7136 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007137 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007138 tofree = akey = vim_strnsave(key, len);
7139 if (akey == NULL)
7140 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007141 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007142 else
7143 {
7144 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007145 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007146 akey = buf;
7147 }
7148
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007150 vim_free(tofree);
7151 if (HASHITEM_EMPTY(hi))
7152 return NULL;
7153 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007154}
7155
7156/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007157 * Get a string item from a dictionary.
7158 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007159 * Returns NULL if the entry doesn't exist or out of memory.
7160 */
7161 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007162get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007163 dict_T *d;
7164 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007165 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007166{
7167 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007168 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007169
7170 di = dict_find(d, key, -1);
7171 if (di == NULL)
7172 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007173 s = get_tv_string(&di->di_tv);
7174 if (save && s != NULL)
7175 s = vim_strsave(s);
7176 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007177}
7178
7179/*
7180 * Get a number item from a dictionary.
7181 * Returns 0 if the entry doesn't exist or out of memory.
7182 */
7183 long
7184get_dict_number(d, key)
7185 dict_T *d;
7186 char_u *key;
7187{
7188 dictitem_T *di;
7189
7190 di = dict_find(d, key, -1);
7191 if (di == NULL)
7192 return 0;
7193 return get_tv_number(&di->di_tv);
7194}
7195
7196/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197 * Return an allocated string with the string representation of a Dictionary.
7198 * May return NULL.
7199 */
7200 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007201dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007202 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007203 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007204{
7205 garray_T ga;
7206 int first = TRUE;
7207 char_u *tofree;
7208 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007209 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007213
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007215 return NULL;
7216 ga_init2(&ga, (int)sizeof(char), 80);
7217 ga_append(&ga, '{');
7218
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007219 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007220 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007222 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007223 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007224 --todo;
7225
7226 if (first)
7227 first = FALSE;
7228 else
7229 ga_concat(&ga, (char_u *)", ");
7230
7231 tofree = string_quote(hi->hi_key, FALSE);
7232 if (tofree != NULL)
7233 {
7234 ga_concat(&ga, tofree);
7235 vim_free(tofree);
7236 }
7237 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007238 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 if (s != NULL)
7240 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007242 if (s == NULL)
7243 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007246 if (todo > 0)
7247 {
7248 vim_free(ga.ga_data);
7249 return NULL;
7250 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251
7252 ga_append(&ga, '}');
7253 ga_append(&ga, NUL);
7254 return (char_u *)ga.ga_data;
7255}
7256
7257/*
7258 * Allocate a variable for a Dictionary and fill it from "*arg".
7259 * Return OK or FAIL. Returns NOTDONE for {expr}.
7260 */
7261 static int
7262get_dict_tv(arg, rettv, evaluate)
7263 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 int evaluate;
7266{
Bram Moolenaar33570922005-01-25 22:26:29 +00007267 dict_T *d = NULL;
7268 typval_T tvkey;
7269 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007270 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007271 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007273 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274
7275 /*
7276 * First check if it's not a curly-braces thing: {expr}.
7277 * Must do this without evaluating, otherwise a function may be called
7278 * twice. Unfortunately this means we need to call eval1() twice for the
7279 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007280 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007282 if (*start != '}')
7283 {
7284 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7285 return FAIL;
7286 if (*start == '}')
7287 return NOTDONE;
7288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289
7290 if (evaluate)
7291 {
7292 d = dict_alloc();
7293 if (d == NULL)
7294 return FAIL;
7295 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007296 tvkey.v_type = VAR_UNKNOWN;
7297 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298
7299 *arg = skipwhite(*arg + 1);
7300 while (**arg != '}' && **arg != NUL)
7301 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007302 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007303 goto failret;
7304 if (**arg != ':')
7305 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007306 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308 goto failret;
7309 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007310 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007312 key = get_tv_string_buf_chk(&tvkey, buf);
7313 if (key == NULL || *key == NUL)
7314 {
7315 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7316 if (key != NULL)
7317 EMSG(_(e_emptykey));
7318 clear_tv(&tvkey);
7319 goto failret;
7320 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322
7323 *arg = skipwhite(*arg + 1);
7324 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7325 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007326 if (evaluate)
7327 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 goto failret;
7329 }
7330 if (evaluate)
7331 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007333 if (item != NULL)
7334 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007335 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 clear_tv(&tv);
7338 goto failret;
7339 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007340 item = dictitem_alloc(key);
7341 clear_tv(&tvkey);
7342 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007345 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007346 if (dict_add(d, item) == FAIL)
7347 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007348 }
7349 }
7350
7351 if (**arg == '}')
7352 break;
7353 if (**arg != ',')
7354 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007355 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007356 goto failret;
7357 }
7358 *arg = skipwhite(*arg + 1);
7359 }
7360
7361 if (**arg != '}')
7362 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007363 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007364failret:
7365 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007366 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007367 return FAIL;
7368 }
7369
7370 *arg = skipwhite(*arg + 1);
7371 if (evaluate)
7372 {
7373 rettv->v_type = VAR_DICT;
7374 rettv->vval.v_dict = d;
7375 ++d->dv_refcount;
7376 }
7377
7378 return OK;
7379}
7380
Bram Moolenaar8c711452005-01-14 21:53:12 +00007381/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007382 * Return a string with the string representation of a variable.
7383 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007384 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007385 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007386 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007387 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007388 */
7389 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007390echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007392 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007393 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007394 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007395{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007396 static int recurse = 0;
7397 char_u *r = NULL;
7398
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007401 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007402 *tofree = NULL;
7403 return NULL;
7404 }
7405 ++recurse;
7406
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007407 switch (tv->v_type)
7408 {
7409 case VAR_FUNC:
7410 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007411 r = tv->vval.v_string;
7412 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007413
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007414 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007415 if (tv->vval.v_list == NULL)
7416 {
7417 *tofree = NULL;
7418 r = NULL;
7419 }
7420 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7421 {
7422 *tofree = NULL;
7423 r = (char_u *)"[...]";
7424 }
7425 else
7426 {
7427 tv->vval.v_list->lv_copyID = copyID;
7428 *tofree = list2string(tv, copyID);
7429 r = *tofree;
7430 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007431 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007432
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007434 if (tv->vval.v_dict == NULL)
7435 {
7436 *tofree = NULL;
7437 r = NULL;
7438 }
7439 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7440 {
7441 *tofree = NULL;
7442 r = (char_u *)"{...}";
7443 }
7444 else
7445 {
7446 tv->vval.v_dict->dv_copyID = copyID;
7447 *tofree = dict2string(tv, copyID);
7448 r = *tofree;
7449 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007450 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007451
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007452 case VAR_STRING:
7453 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007454 *tofree = NULL;
7455 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007456 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007457
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007458#ifdef FEAT_FLOAT
7459 case VAR_FLOAT:
7460 *tofree = NULL;
7461 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7462 r = numbuf;
7463 break;
7464#endif
7465
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007466 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007467 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007469 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470
7471 --recurse;
7472 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007473}
7474
7475/*
7476 * Return a string with the string representation of a variable.
7477 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7478 * "numbuf" is used for a number.
7479 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007480 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007481 */
7482 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007483tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007485 char_u **tofree;
7486 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007487 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007488{
7489 switch (tv->v_type)
7490 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007491 case VAR_FUNC:
7492 *tofree = string_quote(tv->vval.v_string, TRUE);
7493 return *tofree;
7494 case VAR_STRING:
7495 *tofree = string_quote(tv->vval.v_string, FALSE);
7496 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007497#ifdef FEAT_FLOAT
7498 case VAR_FLOAT:
7499 *tofree = NULL;
7500 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7501 return numbuf;
7502#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007503 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007504 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007505 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007507 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007509 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007510 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007511}
7512
7513/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 * Return string "str" in ' quotes, doubling ' characters.
7515 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007516 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007517 */
7518 static char_u *
7519string_quote(str, function)
7520 char_u *str;
7521 int function;
7522{
Bram Moolenaar33570922005-01-25 22:26:29 +00007523 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007524 char_u *p, *r, *s;
7525
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 len = (function ? 13 : 3);
7527 if (str != NULL)
7528 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007529 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007530 for (p = str; *p != NUL; mb_ptr_adv(p))
7531 if (*p == '\'')
7532 ++len;
7533 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007534 s = r = alloc(len);
7535 if (r != NULL)
7536 {
7537 if (function)
7538 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007540 r += 10;
7541 }
7542 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007544 if (str != NULL)
7545 for (p = str; *p != NUL; )
7546 {
7547 if (*p == '\'')
7548 *r++ = '\'';
7549 MB_COPY_CHAR(p, r);
7550 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007552 if (function)
7553 *r++ = ')';
7554 *r++ = NUL;
7555 }
7556 return s;
7557}
7558
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007559#ifdef FEAT_FLOAT
7560/*
7561 * Convert the string "text" to a floating point number.
7562 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7563 * this always uses a decimal point.
7564 * Returns the length of the text that was consumed.
7565 */
7566 static int
7567string2float(text, value)
7568 char_u *text;
7569 float_T *value; /* result stored here */
7570{
7571 char *s = (char *)text;
7572 float_T f;
7573
7574 f = strtod(s, &s);
7575 *value = f;
7576 return (int)((char_u *)s - text);
7577}
7578#endif
7579
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 * Get the value of an environment variable.
7582 * "arg" is pointing to the '$'. It is advanced to after the name.
7583 * If the environment variable was not set, silently assume it is empty.
7584 * Always return OK.
7585 */
7586 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007587get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 int evaluate;
7591{
7592 char_u *string = NULL;
7593 int len;
7594 int cc;
7595 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007596 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597
7598 ++*arg;
7599 name = *arg;
7600 len = get_env_len(arg);
7601 if (evaluate)
7602 {
7603 if (len != 0)
7604 {
7605 cc = name[len];
7606 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007607 /* first try vim_getenv(), fast for normal environment vars */
7608 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007610 {
7611 if (!mustfree)
7612 string = vim_strsave(string);
7613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 else
7615 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007616 if (mustfree)
7617 vim_free(string);
7618
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 /* next try expanding things like $VIM and ${HOME} */
7620 string = expand_env_save(name - 1);
7621 if (string != NULL && *string == '$')
7622 {
7623 vim_free(string);
7624 string = NULL;
7625 }
7626 }
7627 name[len] = cc;
7628 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007629 rettv->v_type = VAR_STRING;
7630 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632
7633 return OK;
7634}
7635
7636/*
7637 * Array with names and number of arguments of all internal functions
7638 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7639 */
7640static struct fst
7641{
7642 char *f_name; /* function name */
7643 char f_min_argc; /* minimal number of arguments */
7644 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007645 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007646 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647} functions[] =
7648{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007649#ifdef FEAT_FLOAT
7650 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007651 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007652#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007653 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 {"append", 2, 2, f_append},
7655 {"argc", 0, 0, f_argc},
7656 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007657 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007658#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007659 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007660 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007661 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007664 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 {"bufexists", 1, 1, f_bufexists},
7666 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7667 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7668 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7669 {"buflisted", 1, 1, f_buflisted},
7670 {"bufloaded", 1, 1, f_bufloaded},
7671 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007672 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 {"bufwinnr", 1, 1, f_bufwinnr},
7674 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007675 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007676 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007677#ifdef FEAT_FLOAT
7678 {"ceil", 1, 1, f_ceil},
7679#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007680 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {"char2nr", 1, 1, f_char2nr},
7682 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007683 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007685#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007686 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007687 {"complete_add", 1, 1, f_complete_add},
7688 {"complete_check", 0, 0, f_complete_check},
7689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007691 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007692#ifdef FEAT_FLOAT
7693 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007694 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007695#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007698 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007699 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 {"delete", 1, 1, f_delete},
7701 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007702 {"diff_filler", 1, 1, f_diff_filler},
7703 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007704 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007706 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {"eventhandler", 0, 0, f_eventhandler},
7708 {"executable", 1, 1, f_executable},
7709 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007710#ifdef FEAT_FLOAT
7711 {"exp", 1, 1, f_exp},
7712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007714 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007715 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7717 {"filereadable", 1, 1, f_filereadable},
7718 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007720 {"finddir", 1, 3, f_finddir},
7721 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007722#ifdef FEAT_FLOAT
7723 {"float2nr", 1, 1, f_float2nr},
7724 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007725 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007726#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007727 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {"fnamemodify", 2, 2, f_fnamemodify},
7729 {"foldclosed", 1, 1, f_foldclosed},
7730 {"foldclosedend", 1, 1, f_foldclosedend},
7731 {"foldlevel", 1, 1, f_foldlevel},
7732 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007733 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007735 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007736 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007737 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007738 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 {"getbufvar", 2, 2, f_getbufvar},
7740 {"getchar", 0, 1, f_getchar},
7741 {"getcharmod", 0, 0, f_getcharmod},
7742 {"getcmdline", 0, 0, f_getcmdline},
7743 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007744 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007746 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007747 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {"getfsize", 1, 1, f_getfsize},
7749 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007750 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007751 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007752 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007753 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007754 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007755 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007756 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007757 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007759 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007760 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {"getwinposx", 0, 0, f_getwinposx},
7762 {"getwinposy", 0, 0, f_getwinposy},
7763 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007764 {"glob", 1, 2, f_glob},
7765 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007767 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007768 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007769 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7771 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7772 {"histadd", 2, 2, f_histadd},
7773 {"histdel", 1, 2, f_histdel},
7774 {"histget", 1, 2, f_histget},
7775 {"histnr", 1, 1, f_histnr},
7776 {"hlID", 1, 1, f_hlID},
7777 {"hlexists", 1, 1, f_hlexists},
7778 {"hostname", 0, 0, f_hostname},
7779 {"iconv", 3, 3, f_iconv},
7780 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007781 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007782 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007784 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 {"inputrestore", 0, 0, f_inputrestore},
7786 {"inputsave", 0, 0, f_inputsave},
7787 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007788 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007790 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007791 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007792 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007793 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007795 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 {"libcall", 3, 3, f_libcall},
7797 {"libcallnr", 3, 3, f_libcallnr},
7798 {"line", 1, 1, f_line},
7799 {"line2byte", 1, 1, f_line2byte},
7800 {"lispindent", 1, 1, f_lispindent},
7801 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007802#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007803 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007804 {"log10", 1, 1, f_log10},
7805#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007806 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007807 {"maparg", 1, 3, f_maparg},
7808 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007809 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007810 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007811 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007812 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007813 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007814 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007815 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007816 {"max", 1, 1, f_max},
7817 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007818#ifdef vim_mkdir
7819 {"mkdir", 1, 3, f_mkdir},
7820#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007821 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007822#ifdef FEAT_MZSCHEME
7823 {"mzeval", 1, 1, f_mzeval},
7824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 {"nextnonblank", 1, 1, f_nextnonblank},
7826 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007827 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007828#ifdef FEAT_FLOAT
7829 {"pow", 2, 2, f_pow},
7830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007832 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007833 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007834 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007835 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007836 {"reltime", 0, 2, f_reltime},
7837 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {"remote_expr", 2, 3, f_remote_expr},
7839 {"remote_foreground", 1, 1, f_remote_foreground},
7840 {"remote_peek", 1, 2, f_remote_peek},
7841 {"remote_read", 1, 1, f_remote_read},
7842 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007843 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007845 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007847 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848#ifdef FEAT_FLOAT
7849 {"round", 1, 1, f_round},
7850#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007851 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007852 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007853 {"searchpair", 3, 7, f_searchpair},
7854 {"searchpairpos", 3, 7, f_searchpairpos},
7855 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 {"server2client", 2, 2, f_server2client},
7857 {"serverlist", 0, 0, f_serverlist},
7858 {"setbufvar", 3, 3, f_setbufvar},
7859 {"setcmdpos", 1, 1, f_setcmdpos},
7860 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007861 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007862 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007863 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007864 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007866 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007867 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007869 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007871#ifdef FEAT_FLOAT
7872 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007873 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007874#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007875 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007876 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007877 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007878 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007879 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880#ifdef FEAT_FLOAT
7881 {"sqrt", 1, 1, f_sqrt},
7882 {"str2float", 1, 1, f_str2float},
7883#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007884 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007885 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007886 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887#ifdef HAVE_STRFTIME
7888 {"strftime", 1, 2, f_strftime},
7889#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007890 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007891 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"strlen", 1, 1, f_strlen},
7893 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007894 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007896 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 {"submatch", 1, 1, f_submatch},
7898 {"substitute", 4, 4, f_substitute},
7899 {"synID", 3, 3, f_synID},
7900 {"synIDattr", 2, 3, f_synIDattr},
7901 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007902 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007903 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007904 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007905 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007906 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007907 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007908 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007909 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007910#ifdef FEAT_FLOAT
7911 {"tan", 1, 1, f_tan},
7912 {"tanh", 1, 1, f_tanh},
7913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007915 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"tolower", 1, 1, f_tolower},
7917 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007918 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007919#ifdef FEAT_FLOAT
7920 {"trunc", 1, 1, f_trunc},
7921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007923 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007924 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 {"virtcol", 1, 1, f_virtcol},
7927 {"visualmode", 0, 1, f_visualmode},
7928 {"winbufnr", 1, 1, f_winbufnr},
7929 {"wincol", 0, 0, f_wincol},
7930 {"winheight", 1, 1, f_winheight},
7931 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007932 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007934 {"winrestview", 1, 1, f_winrestview},
7935 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007937 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938};
7939
7940#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7941
7942/*
7943 * Function given to ExpandGeneric() to obtain the list of internal
7944 * or user defined function names.
7945 */
7946 char_u *
7947get_function_name(xp, idx)
7948 expand_T *xp;
7949 int idx;
7950{
7951 static int intidx = -1;
7952 char_u *name;
7953
7954 if (idx == 0)
7955 intidx = -1;
7956 if (intidx < 0)
7957 {
7958 name = get_user_func_name(xp, idx);
7959 if (name != NULL)
7960 return name;
7961 }
7962 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7963 {
7964 STRCPY(IObuff, functions[intidx].f_name);
7965 STRCAT(IObuff, "(");
7966 if (functions[intidx].f_max_argc == 0)
7967 STRCAT(IObuff, ")");
7968 return IObuff;
7969 }
7970
7971 return NULL;
7972}
7973
7974/*
7975 * Function given to ExpandGeneric() to obtain the list of internal or
7976 * user defined variable or function names.
7977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 char_u *
7979get_expr_name(xp, idx)
7980 expand_T *xp;
7981 int idx;
7982{
7983 static int intidx = -1;
7984 char_u *name;
7985
7986 if (idx == 0)
7987 intidx = -1;
7988 if (intidx < 0)
7989 {
7990 name = get_function_name(xp, idx);
7991 if (name != NULL)
7992 return name;
7993 }
7994 return get_user_var_name(xp, ++intidx);
7995}
7996
7997#endif /* FEAT_CMDL_COMPL */
7998
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007999#if defined(EBCDIC) || defined(PROTO)
8000/*
8001 * Compare struct fst by function name.
8002 */
8003 static int
8004compare_func_name(s1, s2)
8005 const void *s1;
8006 const void *s2;
8007{
8008 struct fst *p1 = (struct fst *)s1;
8009 struct fst *p2 = (struct fst *)s2;
8010
8011 return STRCMP(p1->f_name, p2->f_name);
8012}
8013
8014/*
8015 * Sort the function table by function name.
8016 * The sorting of the table above is ASCII dependant.
8017 * On machines using EBCDIC we have to sort it.
8018 */
8019 static void
8020sortFunctions()
8021{
8022 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8023
8024 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8025}
8026#endif
8027
8028
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029/*
8030 * Find internal function in table above.
8031 * Return index, or -1 if not found
8032 */
8033 static int
8034find_internal_func(name)
8035 char_u *name; /* name of the function */
8036{
8037 int first = 0;
8038 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8039 int cmp;
8040 int x;
8041
8042 /*
8043 * Find the function name in the table. Binary search.
8044 */
8045 while (first <= last)
8046 {
8047 x = first + ((unsigned)(last - first) >> 1);
8048 cmp = STRCMP(name, functions[x].f_name);
8049 if (cmp < 0)
8050 last = x - 1;
8051 else if (cmp > 0)
8052 first = x + 1;
8053 else
8054 return x;
8055 }
8056 return -1;
8057}
8058
8059/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008060 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8061 * name it contains, otherwise return "name".
8062 */
8063 static char_u *
8064deref_func_name(name, lenp)
8065 char_u *name;
8066 int *lenp;
8067{
Bram Moolenaar33570922005-01-25 22:26:29 +00008068 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008069 int cc;
8070
8071 cc = name[*lenp];
8072 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008073 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008074 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008075 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008076 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008077 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008078 {
8079 *lenp = 0;
8080 return (char_u *)""; /* just in case */
8081 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008082 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008083 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008084 }
8085
8086 return name;
8087}
8088
8089/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 * Allocate a variable for the result of a function.
8091 * Return OK or FAIL.
8092 */
8093 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008094get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8095 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 char_u *name; /* name of the function */
8097 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 char_u **arg; /* argument, pointing to the '(' */
8100 linenr_T firstline; /* first line of range */
8101 linenr_T lastline; /* last line of range */
8102 int *doesrange; /* return: function handled range */
8103 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008104 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 char_u *argp;
8107 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008108 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 int argcount = 0; /* number of arguments found */
8110
8111 /*
8112 * Get the arguments.
8113 */
8114 argp = *arg;
8115 while (argcount < MAX_FUNC_ARGS)
8116 {
8117 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8118 if (*argp == ')' || *argp == ',' || *argp == NUL)
8119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8121 {
8122 ret = FAIL;
8123 break;
8124 }
8125 ++argcount;
8126 if (*argp != ',')
8127 break;
8128 }
8129 if (*argp == ')')
8130 ++argp;
8131 else
8132 ret = FAIL;
8133
8134 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008135 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008136 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008138 {
8139 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008140 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008141 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008142 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144
8145 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008146 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147
8148 *arg = skipwhite(argp);
8149 return ret;
8150}
8151
8152
8153/*
8154 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008155 * Return OK when the function can't be called, FAIL otherwise.
8156 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 */
8158 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008159call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008160 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008161 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008163 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008165 typval_T *argvars; /* vars for arguments, must have "argcount"
8166 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 linenr_T firstline; /* first line of range */
8168 linenr_T lastline; /* last line of range */
8169 int *doesrange; /* return: function handled range */
8170 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008171 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
8173 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174#define ERROR_UNKNOWN 0
8175#define ERROR_TOOMANY 1
8176#define ERROR_TOOFEW 2
8177#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008178#define ERROR_DICT 4
8179#define ERROR_NONE 5
8180#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 int error = ERROR_NONE;
8182 int i;
8183 int llen;
8184 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185#define FLEN_FIXED 40
8186 char_u fname_buf[FLEN_FIXED + 1];
8187 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008188 char_u *name;
8189
8190 /* Make a copy of the name, if it comes from a funcref variable it could
8191 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008192 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008193 if (name == NULL)
8194 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195
8196 /*
8197 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8198 * Change <SNR>123_name() to K_SNR 123_name().
8199 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 llen = eval_fname_script(name);
8202 if (llen > 0)
8203 {
8204 fname_buf[0] = K_SPECIAL;
8205 fname_buf[1] = KS_EXTRA;
8206 fname_buf[2] = (int)KE_SNR;
8207 i = 3;
8208 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8209 {
8210 if (current_SID <= 0)
8211 error = ERROR_SCRIPT;
8212 else
8213 {
8214 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8215 i = (int)STRLEN(fname_buf);
8216 }
8217 }
8218 if (i + STRLEN(name + llen) < FLEN_FIXED)
8219 {
8220 STRCPY(fname_buf + i, name + llen);
8221 fname = fname_buf;
8222 }
8223 else
8224 {
8225 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8226 if (fname == NULL)
8227 error = ERROR_OTHER;
8228 else
8229 {
8230 mch_memmove(fname, fname_buf, (size_t)i);
8231 STRCPY(fname + i, name + llen);
8232 }
8233 }
8234 }
8235 else
8236 fname = name;
8237
8238 *doesrange = FALSE;
8239
8240
8241 /* execute the function if no errors detected and executing */
8242 if (evaluate && error == ERROR_NONE)
8243 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008244 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8245 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 error = ERROR_UNKNOWN;
8247
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008248 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {
8250 /*
8251 * User defined function.
8252 */
8253 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008254
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008256 /* Trigger FuncUndefined event, may load the function. */
8257 if (fp == NULL
8258 && apply_autocmds(EVENT_FUNCUNDEFINED,
8259 fname, fname, TRUE, NULL)
8260 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008262 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 fp = find_func(fname);
8264 }
8265#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008266 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008267 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008268 {
8269 /* loaded a package, search for the function again */
8270 fp = find_func(fname);
8271 }
8272
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 if (fp != NULL)
8274 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008275 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008277 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008279 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008281 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008282 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 else
8284 {
8285 /*
8286 * Call the user function.
8287 * Save and restore search patterns, script variables and
8288 * redo buffer.
8289 */
8290 save_search_patterns();
8291 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008292 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008293 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008294 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008295 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8296 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8297 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008298 /* Function was unreferenced while being used, free it
8299 * now. */
8300 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 restoreRedobuff();
8302 restore_search_patterns();
8303 error = ERROR_NONE;
8304 }
8305 }
8306 }
8307 else
8308 {
8309 /*
8310 * Find the function name in the table, call its implementation.
8311 */
8312 i = find_internal_func(fname);
8313 if (i >= 0)
8314 {
8315 if (argcount < functions[i].f_min_argc)
8316 error = ERROR_TOOFEW;
8317 else if (argcount > functions[i].f_max_argc)
8318 error = ERROR_TOOMANY;
8319 else
8320 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 error = ERROR_NONE;
8324 }
8325 }
8326 }
8327 /*
8328 * The function call (or "FuncUndefined" autocommand sequence) might
8329 * have been aborted by an error, an interrupt, or an explicitly thrown
8330 * exception that has not been caught so far. This situation can be
8331 * tested for by calling aborting(). For an error in an internal
8332 * function or for the "E132" error in call_user_func(), however, the
8333 * throw point at which the "force_abort" flag (temporarily reset by
8334 * emsg()) is normally updated has not been reached yet. We need to
8335 * update that flag first to make aborting() reliable.
8336 */
8337 update_force_abort();
8338 }
8339 if (error == ERROR_NONE)
8340 ret = OK;
8341
8342 /*
8343 * Report an error unless the argument evaluation or function call has been
8344 * cancelled due to an aborting error, an interrupt, or an exception.
8345 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 if (!aborting())
8347 {
8348 switch (error)
8349 {
8350 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008351 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008352 break;
8353 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008354 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008355 break;
8356 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008357 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008358 name);
8359 break;
8360 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008361 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008362 name);
8363 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008364 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008365 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008366 name);
8367 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008368 }
8369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 if (fname != name && fname != fname_buf)
8372 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008373 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374
8375 return ret;
8376}
8377
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008378/*
8379 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008380 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008381 */
8382 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008383emsg_funcname(ermsg, name)
8384 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008385 char_u *name;
8386{
8387 char_u *p;
8388
8389 if (*name == K_SPECIAL)
8390 p = concat_str((char_u *)"<SNR>", name + 3);
8391 else
8392 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008393 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008394 if (p != name)
8395 vim_free(p);
8396}
8397
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008398/*
8399 * Return TRUE for a non-zero Number and a non-empty String.
8400 */
8401 static int
8402non_zero_arg(argvars)
8403 typval_T *argvars;
8404{
8405 return ((argvars[0].v_type == VAR_NUMBER
8406 && argvars[0].vval.v_number != 0)
8407 || (argvars[0].v_type == VAR_STRING
8408 && argvars[0].vval.v_string != NULL
8409 && *argvars[0].vval.v_string != NUL));
8410}
8411
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412/*********************************************
8413 * Implementation of the built-in functions
8414 */
8415
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008416#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008417static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8418
8419/*
8420 * Get the float value of "argvars[0]" into "f".
8421 * Returns FAIL when the argument is not a Number or Float.
8422 */
8423 static int
8424get_float_arg(argvars, f)
8425 typval_T *argvars;
8426 float_T *f;
8427{
8428 if (argvars[0].v_type == VAR_FLOAT)
8429 {
8430 *f = argvars[0].vval.v_float;
8431 return OK;
8432 }
8433 if (argvars[0].v_type == VAR_NUMBER)
8434 {
8435 *f = (float_T)argvars[0].vval.v_number;
8436 return OK;
8437 }
8438 EMSG(_("E808: Number or Float required"));
8439 return FAIL;
8440}
8441
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008442/*
8443 * "abs(expr)" function
8444 */
8445 static void
8446f_abs(argvars, rettv)
8447 typval_T *argvars;
8448 typval_T *rettv;
8449{
8450 if (argvars[0].v_type == VAR_FLOAT)
8451 {
8452 rettv->v_type = VAR_FLOAT;
8453 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8454 }
8455 else
8456 {
8457 varnumber_T n;
8458 int error = FALSE;
8459
8460 n = get_tv_number_chk(&argvars[0], &error);
8461 if (error)
8462 rettv->vval.v_number = -1;
8463 else if (n > 0)
8464 rettv->vval.v_number = n;
8465 else
8466 rettv->vval.v_number = -n;
8467 }
8468}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008469
8470/*
8471 * "acos()" function
8472 */
8473 static void
8474f_acos(argvars, rettv)
8475 typval_T *argvars;
8476 typval_T *rettv;
8477{
8478 float_T f;
8479
8480 rettv->v_type = VAR_FLOAT;
8481 if (get_float_arg(argvars, &f) == OK)
8482 rettv->vval.v_float = acos(f);
8483 else
8484 rettv->vval.v_float = 0.0;
8485}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008486#endif
8487
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008489 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 */
8491 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008492f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008493 typval_T *argvars;
8494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495{
Bram Moolenaar33570922005-01-25 22:26:29 +00008496 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008498 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008499 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008501 if ((l = argvars[0].vval.v_list) != NULL
8502 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8503 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008505 }
8506 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008507 EMSG(_(e_listreq));
8508}
8509
8510/*
8511 * "append(lnum, string/list)" function
8512 */
8513 static void
8514f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008515 typval_T *argvars;
8516 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008517{
8518 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008519 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008520 list_T *l = NULL;
8521 listitem_T *li = NULL;
8522 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008523 long added = 0;
8524
Bram Moolenaar0d660222005-01-07 21:51:51 +00008525 lnum = get_tv_lnum(argvars);
8526 if (lnum >= 0
8527 && lnum <= curbuf->b_ml.ml_line_count
8528 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008530 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008531 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008532 l = argvars[1].vval.v_list;
8533 if (l == NULL)
8534 return;
8535 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008536 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008537 for (;;)
8538 {
8539 if (l == NULL)
8540 tv = &argvars[1]; /* append a string */
8541 else if (li == NULL)
8542 break; /* end of list */
8543 else
8544 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008545 line = get_tv_string_chk(tv);
8546 if (line == NULL) /* type error */
8547 {
8548 rettv->vval.v_number = 1; /* Failed */
8549 break;
8550 }
8551 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008552 ++added;
8553 if (l == NULL)
8554 break;
8555 li = li->li_next;
8556 }
8557
8558 appended_lines_mark(lnum, added);
8559 if (curwin->w_cursor.lnum > lnum)
8560 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008562 else
8563 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564}
8565
8566/*
8567 * "argc()" function
8568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008571 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008574 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575}
8576
8577/*
8578 * "argidx()" function
8579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008582 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008583 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008585 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586}
8587
8588/*
8589 * "argv(nr)" function
8590 */
8591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008592f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008593 typval_T *argvars;
8594 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595{
8596 int idx;
8597
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008598 if (argvars[0].v_type != VAR_UNKNOWN)
8599 {
8600 idx = get_tv_number_chk(&argvars[0], NULL);
8601 if (idx >= 0 && idx < ARGCOUNT)
8602 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8603 else
8604 rettv->vval.v_string = NULL;
8605 rettv->v_type = VAR_STRING;
8606 }
8607 else if (rettv_list_alloc(rettv) == OK)
8608 for (idx = 0; idx < ARGCOUNT; ++idx)
8609 list_append_string(rettv->vval.v_list,
8610 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611}
8612
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008613#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008614/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008615 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008616 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008617 static void
8618f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008619 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008620 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008621{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008622 float_T f;
8623
8624 rettv->v_type = VAR_FLOAT;
8625 if (get_float_arg(argvars, &f) == OK)
8626 rettv->vval.v_float = asin(f);
8627 else
8628 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008629}
8630
8631/*
8632 * "atan()" function
8633 */
8634 static void
8635f_atan(argvars, rettv)
8636 typval_T *argvars;
8637 typval_T *rettv;
8638{
8639 float_T f;
8640
8641 rettv->v_type = VAR_FLOAT;
8642 if (get_float_arg(argvars, &f) == OK)
8643 rettv->vval.v_float = atan(f);
8644 else
8645 rettv->vval.v_float = 0.0;
8646}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008647
8648/*
8649 * "atan2()" function
8650 */
8651 static void
8652f_atan2(argvars, rettv)
8653 typval_T *argvars;
8654 typval_T *rettv;
8655{
8656 float_T fx, fy;
8657
8658 rettv->v_type = VAR_FLOAT;
8659 if (get_float_arg(argvars, &fx) == OK
8660 && get_float_arg(&argvars[1], &fy) == OK)
8661 rettv->vval.v_float = atan2(fx, fy);
8662 else
8663 rettv->vval.v_float = 0.0;
8664}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008665#endif
8666
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667/*
8668 * "browse(save, title, initdir, default)" function
8669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008671f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008672 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
8675#ifdef FEAT_BROWSE
8676 int save;
8677 char_u *title;
8678 char_u *initdir;
8679 char_u *defname;
8680 char_u buf[NUMBUFLEN];
8681 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008682 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008684 save = get_tv_number_chk(&argvars[0], &error);
8685 title = get_tv_string_chk(&argvars[1]);
8686 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8687 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008689 if (error || title == NULL || initdir == NULL || defname == NULL)
8690 rettv->vval.v_string = NULL;
8691 else
8692 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008693 do_browse(save ? BROWSE_SAVE : 0,
8694 title, defname, NULL, initdir, NULL, curbuf);
8695#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008696 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008697#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008698 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008699}
8700
8701/*
8702 * "browsedir(title, initdir)" function
8703 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008705f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008706 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008707 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008708{
8709#ifdef FEAT_BROWSE
8710 char_u *title;
8711 char_u *initdir;
8712 char_u buf[NUMBUFLEN];
8713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008714 title = get_tv_string_chk(&argvars[0]);
8715 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008716
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008717 if (title == NULL || initdir == NULL)
8718 rettv->vval.v_string = NULL;
8719 else
8720 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008721 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008723 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008725 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726}
8727
Bram Moolenaar33570922005-01-25 22:26:29 +00008728static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008729
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730/*
8731 * Find a buffer by number or exact name.
8732 */
8733 static buf_T *
8734find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736{
8737 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008739 if (avar->v_type == VAR_NUMBER)
8740 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008741 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008743 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008744 if (buf == NULL)
8745 {
8746 /* No full path name match, try a match with a URL or a "nofile"
8747 * buffer, these don't use the full path. */
8748 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8749 if (buf->b_fname != NULL
8750 && (path_with_url(buf->b_fname)
8751#ifdef FEAT_QUICKFIX
8752 || bt_nofile(buf)
8753#endif
8754 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008755 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008756 break;
8757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 }
8759 return buf;
8760}
8761
8762/*
8763 * "bufexists(expr)" function
8764 */
8765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008766f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008767 typval_T *argvars;
8768 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771}
8772
8773/*
8774 * "buflisted(expr)" function
8775 */
8776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008777f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008778 typval_T *argvars;
8779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
8781 buf_T *buf;
8782
8783 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008784 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785}
8786
8787/*
8788 * "bufloaded(expr)" function
8789 */
8790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *argvars;
8793 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
8795 buf_T *buf;
8796
8797 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799}
8800
Bram Moolenaar33570922005-01-25 22:26:29 +00008801static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008802
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803/*
8804 * Get buffer by number or pattern.
8805 */
8806 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008807get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008808 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 int save_magic;
8812 char_u *save_cpo;
8813 buf_T *buf;
8814
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008815 if (tv->v_type == VAR_NUMBER)
8816 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008817 if (tv->v_type != VAR_STRING)
8818 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 if (name == NULL || *name == NUL)
8820 return curbuf;
8821 if (name[0] == '$' && name[1] == NUL)
8822 return lastbuf;
8823
8824 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8825 save_magic = p_magic;
8826 p_magic = TRUE;
8827 save_cpo = p_cpo;
8828 p_cpo = (char_u *)"";
8829
8830 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8831 TRUE, FALSE));
8832
8833 p_magic = save_magic;
8834 p_cpo = save_cpo;
8835
8836 /* If not found, try expanding the name, like done for bufexists(). */
8837 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008838 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
8840 return buf;
8841}
8842
8843/*
8844 * "bufname(expr)" function
8845 */
8846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008847f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008848 typval_T *argvars;
8849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850{
8851 buf_T *buf;
8852
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008853 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 buf = get_buf_tv(&argvars[0]);
8856 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008858 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 --emsg_off;
8862}
8863
8864/*
8865 * "bufnr(expr)" function
8866 */
8867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008869 typval_T *argvars;
8870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871{
8872 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008873 int error = FALSE;
8874 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008876 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008878 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008879 --emsg_off;
8880
8881 /* If the buffer isn't found and the second argument is not zero create a
8882 * new buffer. */
8883 if (buf == NULL
8884 && argvars[1].v_type != VAR_UNKNOWN
8885 && get_tv_number_chk(&argvars[1], &error) != 0
8886 && !error
8887 && (name = get_tv_string_chk(&argvars[0])) != NULL
8888 && !error)
8889 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8890
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008892 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895}
8896
8897/*
8898 * "bufwinnr(nr)" function
8899 */
8900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008901f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 typval_T *argvars;
8903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904{
8905#ifdef FEAT_WINDOWS
8906 win_T *wp;
8907 int winnr = 0;
8908#endif
8909 buf_T *buf;
8910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008913 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914#ifdef FEAT_WINDOWS
8915 for (wp = firstwin; wp; wp = wp->w_next)
8916 {
8917 ++winnr;
8918 if (wp->w_buffer == buf)
8919 break;
8920 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924#endif
8925 --emsg_off;
8926}
8927
8928/*
8929 * "byte2line(byte)" function
8930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008932f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008933 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935{
8936#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938#else
8939 long boff = 0;
8940
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008941 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 (linenr_T)0, &boff);
8947#endif
8948}
8949
8950/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008951 * "byteidx()" function
8952 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008953 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 typval_T *argvars;
8956 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008957{
8958#ifdef FEAT_MBYTE
8959 char_u *t;
8960#endif
8961 char_u *str;
8962 long idx;
8963
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008964 str = get_tv_string_chk(&argvars[0]);
8965 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008966 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008967 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008968 return;
8969
8970#ifdef FEAT_MBYTE
8971 t = str;
8972 for ( ; idx > 0; idx--)
8973 {
8974 if (*t == NUL) /* EOL reached */
8975 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008976 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008977 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008978 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008979#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008980 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008982#endif
8983}
8984
8985/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008986 * "call(func, arglist)" function
8987 */
8988 static void
8989f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008990 typval_T *argvars;
8991 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008992{
8993 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008994 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008995 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008997 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008999
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009000 if (argvars[1].v_type != VAR_LIST)
9001 {
9002 EMSG(_(e_listreq));
9003 return;
9004 }
9005 if (argvars[1].vval.v_list == NULL)
9006 return;
9007
9008 if (argvars[0].v_type == VAR_FUNC)
9009 func = argvars[0].vval.v_string;
9010 else
9011 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009012 if (*func == NUL)
9013 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 if (argvars[2].v_type != VAR_UNKNOWN)
9016 {
9017 if (argvars[2].v_type != VAR_DICT)
9018 {
9019 EMSG(_(e_dictreq));
9020 return;
9021 }
9022 selfdict = argvars[2].vval.v_dict;
9023 }
9024
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009025 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9026 item = item->li_next)
9027 {
9028 if (argc == MAX_FUNC_ARGS)
9029 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009030 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009031 break;
9032 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009033 /* Make a copy of each argument. This is needed to be able to set
9034 * v_lock to VAR_FIXED in the copy without changing the original list.
9035 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009036 copy_tv(&item->li_tv, &argv[argc++]);
9037 }
9038
9039 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009040 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9042 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009043
9044 /* Free the arguments. */
9045 while (argc > 0)
9046 clear_tv(&argv[--argc]);
9047}
9048
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009049#ifdef FEAT_FLOAT
9050/*
9051 * "ceil({float})" function
9052 */
9053 static void
9054f_ceil(argvars, rettv)
9055 typval_T *argvars;
9056 typval_T *rettv;
9057{
9058 float_T f;
9059
9060 rettv->v_type = VAR_FLOAT;
9061 if (get_float_arg(argvars, &f) == OK)
9062 rettv->vval.v_float = ceil(f);
9063 else
9064 rettv->vval.v_float = 0.0;
9065}
9066#endif
9067
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009068/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009069 * "changenr()" function
9070 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009071 static void
9072f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009073 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009074 typval_T *rettv;
9075{
9076 rettv->vval.v_number = curbuf->b_u_seq_cur;
9077}
9078
9079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 * "char2nr(string)" function
9081 */
9082 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009083f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 typval_T *argvars;
9085 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086{
9087#ifdef FEAT_MBYTE
9088 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009089 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 else
9091#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093}
9094
9095/*
9096 * "cindent(lnum)" function
9097 */
9098 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009099f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009100 typval_T *argvars;
9101 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102{
9103#ifdef FEAT_CINDENT
9104 pos_T pos;
9105 linenr_T lnum;
9106
9107 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9110 {
9111 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113 curwin->w_cursor = pos;
9114 }
9115 else
9116#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009117 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118}
9119
9120/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009121 * "clearmatches()" function
9122 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009123 static void
9124f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009125 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009126 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009127{
9128#ifdef FEAT_SEARCH_EXTRA
9129 clear_matches(curwin);
9130#endif
9131}
9132
9133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 * "col(string)" function
9135 */
9136 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009138 typval_T *argvars;
9139 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140{
9141 colnr_T col = 0;
9142 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009143 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009145 fp = var2fpos(&argvars[0], FALSE, &fnum);
9146 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 {
9148 if (fp->col == MAXCOL)
9149 {
9150 /* '> can be MAXCOL, get the length of the line then */
9151 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009152 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 else
9154 col = MAXCOL;
9155 }
9156 else
9157 {
9158 col = fp->col + 1;
9159#ifdef FEAT_VIRTUALEDIT
9160 /* col(".") when the cursor is on the NUL at the end of the line
9161 * because of "coladd" can be seen as an extra column. */
9162 if (virtual_active() && fp == &curwin->w_cursor)
9163 {
9164 char_u *p = ml_get_cursor();
9165
9166 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9167 curwin->w_virtcol - curwin->w_cursor.coladd))
9168 {
9169# ifdef FEAT_MBYTE
9170 int l;
9171
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009172 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 col += l;
9174# else
9175 if (*p != NUL && p[1] == NUL)
9176 ++col;
9177# endif
9178 }
9179 }
9180#endif
9181 }
9182 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184}
9185
Bram Moolenaar572cb562005-08-05 21:35:02 +00009186#if defined(FEAT_INS_EXPAND)
9187/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009188 * "complete()" function
9189 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009190 static void
9191f_complete(argvars, rettv)
9192 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009193 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009194{
9195 int startcol;
9196
9197 if ((State & INSERT) == 0)
9198 {
9199 EMSG(_("E785: complete() can only be used in Insert mode"));
9200 return;
9201 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009202
9203 /* Check for undo allowed here, because if something was already inserted
9204 * the line was already saved for undo and this check isn't done. */
9205 if (!undo_allowed())
9206 return;
9207
Bram Moolenaarade00832006-03-10 21:46:58 +00009208 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9209 {
9210 EMSG(_(e_invarg));
9211 return;
9212 }
9213
9214 startcol = get_tv_number_chk(&argvars[0], NULL);
9215 if (startcol <= 0)
9216 return;
9217
9218 set_completion(startcol - 1, argvars[1].vval.v_list);
9219}
9220
9221/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009222 * "complete_add()" function
9223 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009224 static void
9225f_complete_add(argvars, rettv)
9226 typval_T *argvars;
9227 typval_T *rettv;
9228{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009229 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009230}
9231
9232/*
9233 * "complete_check()" function
9234 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009235 static void
9236f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009237 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009238 typval_T *rettv;
9239{
9240 int saved = RedrawingDisabled;
9241
9242 RedrawingDisabled = 0;
9243 ins_compl_check_keys(0);
9244 rettv->vval.v_number = compl_interrupted;
9245 RedrawingDisabled = saved;
9246}
9247#endif
9248
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249/*
9250 * "confirm(message, buttons[, default [, type]])" function
9251 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009254 typval_T *argvars UNUSED;
9255 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009256{
9257#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9258 char_u *message;
9259 char_u *buttons = NULL;
9260 char_u buf[NUMBUFLEN];
9261 char_u buf2[NUMBUFLEN];
9262 int def = 1;
9263 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009264 char_u *typestr;
9265 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009267 message = get_tv_string_chk(&argvars[0]);
9268 if (message == NULL)
9269 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009270 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009272 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9273 if (buttons == NULL)
9274 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009275 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009277 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009278 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009280 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9281 if (typestr == NULL)
9282 error = TRUE;
9283 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009285 switch (TOUPPER_ASC(*typestr))
9286 {
9287 case 'E': type = VIM_ERROR; break;
9288 case 'Q': type = VIM_QUESTION; break;
9289 case 'I': type = VIM_INFO; break;
9290 case 'W': type = VIM_WARNING; break;
9291 case 'G': type = VIM_GENERIC; break;
9292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 }
9294 }
9295 }
9296 }
9297
9298 if (buttons == NULL || *buttons == NUL)
9299 buttons = (char_u *)_("&Ok");
9300
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009301 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009302 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304#endif
9305}
9306
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009307/*
9308 * "copy()" function
9309 */
9310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009311f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009312 typval_T *argvars;
9313 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009314{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009315 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009316}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009318#ifdef FEAT_FLOAT
9319/*
9320 * "cos()" function
9321 */
9322 static void
9323f_cos(argvars, rettv)
9324 typval_T *argvars;
9325 typval_T *rettv;
9326{
9327 float_T f;
9328
9329 rettv->v_type = VAR_FLOAT;
9330 if (get_float_arg(argvars, &f) == OK)
9331 rettv->vval.v_float = cos(f);
9332 else
9333 rettv->vval.v_float = 0.0;
9334}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009335
9336/*
9337 * "cosh()" function
9338 */
9339 static void
9340f_cosh(argvars, rettv)
9341 typval_T *argvars;
9342 typval_T *rettv;
9343{
9344 float_T f;
9345
9346 rettv->v_type = VAR_FLOAT;
9347 if (get_float_arg(argvars, &f) == OK)
9348 rettv->vval.v_float = cosh(f);
9349 else
9350 rettv->vval.v_float = 0.0;
9351}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009352#endif
9353
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009355 * "count()" function
9356 */
9357 static void
9358f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009359 typval_T *argvars;
9360 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009361{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009362 long n = 0;
9363 int ic = FALSE;
9364
Bram Moolenaare9a41262005-01-15 22:18:47 +00009365 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009366 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009367 listitem_T *li;
9368 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009369 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009370
Bram Moolenaare9a41262005-01-15 22:18:47 +00009371 if ((l = argvars[0].vval.v_list) != NULL)
9372 {
9373 li = l->lv_first;
9374 if (argvars[2].v_type != VAR_UNKNOWN)
9375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009376 int error = FALSE;
9377
9378 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009379 if (argvars[3].v_type != VAR_UNKNOWN)
9380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009381 idx = get_tv_number_chk(&argvars[3], &error);
9382 if (!error)
9383 {
9384 li = list_find(l, idx);
9385 if (li == NULL)
9386 EMSGN(_(e_listidx), idx);
9387 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009388 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009389 if (error)
9390 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009391 }
9392
9393 for ( ; li != NULL; li = li->li_next)
9394 if (tv_equal(&li->li_tv, &argvars[1], ic))
9395 ++n;
9396 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009398 else if (argvars[0].v_type == VAR_DICT)
9399 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009400 int todo;
9401 dict_T *d;
9402 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009403
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009404 if ((d = argvars[0].vval.v_dict) != NULL)
9405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009406 int error = FALSE;
9407
Bram Moolenaare9a41262005-01-15 22:18:47 +00009408 if (argvars[2].v_type != VAR_UNKNOWN)
9409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009410 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009411 if (argvars[3].v_type != VAR_UNKNOWN)
9412 EMSG(_(e_invarg));
9413 }
9414
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009415 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009416 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009417 {
9418 if (!HASHITEM_EMPTY(hi))
9419 {
9420 --todo;
9421 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9422 ++n;
9423 }
9424 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009425 }
9426 }
9427 else
9428 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009429 rettv->vval.v_number = n;
9430}
9431
9432/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9434 *
9435 * Checks the existence of a cscope connection.
9436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009438f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009439 typval_T *argvars UNUSED;
9440 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441{
9442#ifdef FEAT_CSCOPE
9443 int num = 0;
9444 char_u *dbpath = NULL;
9445 char_u *prepend = NULL;
9446 char_u buf[NUMBUFLEN];
9447
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009448 if (argvars[0].v_type != VAR_UNKNOWN
9449 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 num = (int)get_tv_number(&argvars[0]);
9452 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009453 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 }
9456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009457 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458#endif
9459}
9460
9461/*
9462 * "cursor(lnum, col)" function
9463 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009464 * Moves the cursor to the specified line and column.
9465 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009469 typval_T *argvars;
9470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471{
9472 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009473#ifdef FEAT_VIRTUALEDIT
9474 long coladd = 0;
9475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009477 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009478 if (argvars[1].v_type == VAR_UNKNOWN)
9479 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009480 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009481
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009482 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009483 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009484 line = pos.lnum;
9485 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009486#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009487 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009488#endif
9489 }
9490 else
9491 {
9492 line = get_tv_lnum(argvars);
9493 col = get_tv_number_chk(&argvars[1], NULL);
9494#ifdef FEAT_VIRTUALEDIT
9495 if (argvars[2].v_type != VAR_UNKNOWN)
9496 coladd = get_tv_number_chk(&argvars[2], NULL);
9497#endif
9498 }
9499 if (line < 0 || col < 0
9500#ifdef FEAT_VIRTUALEDIT
9501 || coladd < 0
9502#endif
9503 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009504 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 if (line > 0)
9506 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 if (col > 0)
9508 curwin->w_cursor.col = col - 1;
9509#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009510 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511#endif
9512
9513 /* Make sure the cursor is in a valid position. */
9514 check_cursor();
9515#ifdef FEAT_MBYTE
9516 /* Correct cursor for multi-byte character. */
9517 if (has_mbyte)
9518 mb_adjust_cursor();
9519#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009520
9521 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009522 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523}
9524
9525/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009526 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 */
9528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009530 typval_T *argvars;
9531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009533 int noref = 0;
9534
9535 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009536 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009537 if (noref < 0 || noref > 1)
9538 EMSG(_(e_invarg));
9539 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009540 {
9541 current_copyID += COPYID_INC;
9542 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544}
9545
9546/*
9547 * "delete()" function
9548 */
9549 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009550f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009551 typval_T *argvars;
9552 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558}
9559
9560/*
9561 * "did_filetype()" function
9562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009565 typval_T *argvars UNUSED;
9566 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567{
9568#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570#endif
9571}
9572
9573/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009574 * "diff_filler()" function
9575 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009578 typval_T *argvars UNUSED;
9579 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009580{
9581#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009583#endif
9584}
9585
9586/*
9587 * "diff_hlID()" function
9588 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009591 typval_T *argvars UNUSED;
9592 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009593{
9594#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009595 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009596 static linenr_T prev_lnum = 0;
9597 static int changedtick = 0;
9598 static int fnum = 0;
9599 static int change_start = 0;
9600 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009601 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009602 int filler_lines;
9603 int col;
9604
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009605 if (lnum < 0) /* ignore type error in {lnum} arg */
9606 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009607 if (lnum != prev_lnum
9608 || changedtick != curbuf->b_changedtick
9609 || fnum != curbuf->b_fnum)
9610 {
9611 /* New line, buffer, change: need to get the values. */
9612 filler_lines = diff_check(curwin, lnum);
9613 if (filler_lines < 0)
9614 {
9615 if (filler_lines == -1)
9616 {
9617 change_start = MAXCOL;
9618 change_end = -1;
9619 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9620 hlID = HLF_ADD; /* added line */
9621 else
9622 hlID = HLF_CHD; /* changed line */
9623 }
9624 else
9625 hlID = HLF_ADD; /* added line */
9626 }
9627 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009628 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009629 prev_lnum = lnum;
9630 changedtick = curbuf->b_changedtick;
9631 fnum = curbuf->b_fnum;
9632 }
9633
9634 if (hlID == HLF_CHD || hlID == HLF_TXD)
9635 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009636 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009637 if (col >= change_start && col <= change_end)
9638 hlID = HLF_TXD; /* changed text */
9639 else
9640 hlID = HLF_CHD; /* changed line */
9641 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009642 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009643#endif
9644}
9645
9646/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009647 * "empty({expr})" function
9648 */
9649 static void
9650f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009651 typval_T *argvars;
9652 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009653{
9654 int n;
9655
9656 switch (argvars[0].v_type)
9657 {
9658 case VAR_STRING:
9659 case VAR_FUNC:
9660 n = argvars[0].vval.v_string == NULL
9661 || *argvars[0].vval.v_string == NUL;
9662 break;
9663 case VAR_NUMBER:
9664 n = argvars[0].vval.v_number == 0;
9665 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009666#ifdef FEAT_FLOAT
9667 case VAR_FLOAT:
9668 n = argvars[0].vval.v_float == 0.0;
9669 break;
9670#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009671 case VAR_LIST:
9672 n = argvars[0].vval.v_list == NULL
9673 || argvars[0].vval.v_list->lv_first == NULL;
9674 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009675 case VAR_DICT:
9676 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009679 default:
9680 EMSG2(_(e_intern2), "f_empty()");
9681 n = 0;
9682 }
9683
9684 rettv->vval.v_number = n;
9685}
9686
9687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 * "escape({string}, {chars})" function
9689 */
9690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009692 typval_T *argvars;
9693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695 char_u buf[NUMBUFLEN];
9696
Bram Moolenaar758711c2005-02-02 23:11:38 +00009697 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9698 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700}
9701
9702/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009703 * "eval()" function
9704 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009705 static void
9706f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009707 typval_T *argvars;
9708 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009709{
9710 char_u *s;
9711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009712 s = get_tv_string_chk(&argvars[0]);
9713 if (s != NULL)
9714 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009716 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9717 {
9718 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009719 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009720 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009721 else if (*s != NUL)
9722 EMSG(_(e_trailing));
9723}
9724
9725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 * "eventhandler()" function
9727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009729f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009730 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009731 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009733 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734}
9735
9736/*
9737 * "executable()" function
9738 */
9739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009741 typval_T *argvars;
9742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745}
9746
9747/*
9748 * "exists()" function
9749 */
9750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009752 typval_T *argvars;
9753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754{
9755 char_u *p;
9756 char_u *name;
9757 int n = FALSE;
9758 int len = 0;
9759
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009760 no_autoload = TRUE;
9761
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 if (*p == '$') /* environment variable */
9764 {
9765 /* first try "normal" environment variables (fast) */
9766 if (mch_getenv(p + 1) != NULL)
9767 n = TRUE;
9768 else
9769 {
9770 /* try expanding things like $VIM and ${HOME} */
9771 p = expand_env_save(p);
9772 if (p != NULL && *p != '$')
9773 n = TRUE;
9774 vim_free(p);
9775 }
9776 }
9777 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009780 if (*skipwhite(p) != NUL)
9781 n = FALSE; /* trailing garbage */
9782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 else if (*p == '*') /* internal or user defined function */
9784 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009785 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 }
9787 else if (*p == ':')
9788 {
9789 n = cmd_exists(p + 1);
9790 }
9791 else if (*p == '#')
9792 {
9793#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009794 if (p[1] == '#')
9795 n = autocmd_supported(p + 2);
9796 else
9797 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798#endif
9799 }
9800 else /* internal variable */
9801 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009802 char_u *tofree;
9803 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009805 /* get_name_len() takes care of expanding curly braces */
9806 name = p;
9807 len = get_name_len(&p, &tofree, TRUE, FALSE);
9808 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009810 if (tofree != NULL)
9811 name = tofree;
9812 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9813 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009815 /* handle d.key, l[idx], f(expr) */
9816 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9817 if (n)
9818 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 }
9820 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009821 if (*p != NUL)
9822 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009824 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 }
9826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009828
9829 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830}
9831
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009832#ifdef FEAT_FLOAT
9833/*
9834 * "exp()" function
9835 */
9836 static void
9837f_exp(argvars, rettv)
9838 typval_T *argvars;
9839 typval_T *rettv;
9840{
9841 float_T f;
9842
9843 rettv->v_type = VAR_FLOAT;
9844 if (get_float_arg(argvars, &f) == OK)
9845 rettv->vval.v_float = exp(f);
9846 else
9847 rettv->vval.v_float = 0.0;
9848}
9849#endif
9850
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851/*
9852 * "expand()" function
9853 */
9854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009855f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009856 typval_T *argvars;
9857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858{
9859 char_u *s;
9860 int len;
9861 char_u *errormsg;
9862 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9863 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009864 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009866 rettv->v_type = VAR_STRING;
9867 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868 if (*s == '%' || *s == '#' || *s == '<')
9869 {
9870 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009871 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872 --emsg_off;
9873 }
9874 else
9875 {
9876 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009877 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 if (argvars[1].v_type != VAR_UNKNOWN
9879 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 if (!error)
9882 {
9883 ExpandInit(&xpc);
9884 xpc.xp_context = EXPAND_FILES;
9885 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009886 }
9887 else
9888 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 }
9890}
9891
9892/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009893 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009894 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009895 */
9896 static void
9897f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 typval_T *argvars;
9899 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009900{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009901 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009902 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009903 list_T *l1, *l2;
9904 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009905 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009906 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009907
Bram Moolenaare9a41262005-01-15 22:18:47 +00009908 l1 = argvars[0].vval.v_list;
9909 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009910 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9911 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009912 {
9913 if (argvars[2].v_type != VAR_UNKNOWN)
9914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009915 before = get_tv_number_chk(&argvars[2], &error);
9916 if (error)
9917 return; /* type error; errmsg already given */
9918
Bram Moolenaar758711c2005-02-02 23:11:38 +00009919 if (before == l1->lv_len)
9920 item = NULL;
9921 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009922 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009923 item = list_find(l1, before);
9924 if (item == NULL)
9925 {
9926 EMSGN(_(e_listidx), before);
9927 return;
9928 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 }
9930 }
9931 else
9932 item = NULL;
9933 list_extend(l1, l2, item);
9934
Bram Moolenaare9a41262005-01-15 22:18:47 +00009935 copy_tv(&argvars[0], rettv);
9936 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009937 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009938 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9939 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009940 dict_T *d1, *d2;
9941 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009942 char_u *action;
9943 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009944 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009945 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009946
9947 d1 = argvars[0].vval.v_dict;
9948 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009949 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9950 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009951 {
9952 /* Check the third argument. */
9953 if (argvars[2].v_type != VAR_UNKNOWN)
9954 {
9955 static char *(av[]) = {"keep", "force", "error"};
9956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 action = get_tv_string_chk(&argvars[2]);
9958 if (action == NULL)
9959 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009960 for (i = 0; i < 3; ++i)
9961 if (STRCMP(action, av[i]) == 0)
9962 break;
9963 if (i == 3)
9964 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009965 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009966 return;
9967 }
9968 }
9969 else
9970 action = (char_u *)"force";
9971
9972 /* Go over all entries in the second dict and add them to the
9973 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009974 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009975 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009976 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009977 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009978 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009979 --todo;
9980 di1 = dict_find(d1, hi2->hi_key, -1);
9981 if (di1 == NULL)
9982 {
9983 di1 = dictitem_copy(HI2DI(hi2));
9984 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9985 dictitem_free(di1);
9986 }
9987 else if (*action == 'e')
9988 {
9989 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9990 break;
9991 }
9992 else if (*action == 'f')
9993 {
9994 clear_tv(&di1->di_tv);
9995 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009997 }
9998 }
9999
Bram Moolenaare9a41262005-01-15 22:18:47 +000010000 copy_tv(&argvars[0], rettv);
10001 }
10002 }
10003 else
10004 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010005}
10006
10007/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010008 * "feedkeys()" function
10009 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010010 static void
10011f_feedkeys(argvars, rettv)
10012 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010013 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010014{
10015 int remap = TRUE;
10016 char_u *keys, *flags;
10017 char_u nbuf[NUMBUFLEN];
10018 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010019 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010020
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010021 /* This is not allowed in the sandbox. If the commands would still be
10022 * executed in the sandbox it would be OK, but it probably happens later,
10023 * when "sandbox" is no longer set. */
10024 if (check_secure())
10025 return;
10026
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010027 keys = get_tv_string(&argvars[0]);
10028 if (*keys != NUL)
10029 {
10030 if (argvars[1].v_type != VAR_UNKNOWN)
10031 {
10032 flags = get_tv_string_buf(&argvars[1], nbuf);
10033 for ( ; *flags != NUL; ++flags)
10034 {
10035 switch (*flags)
10036 {
10037 case 'n': remap = FALSE; break;
10038 case 'm': remap = TRUE; break;
10039 case 't': typed = TRUE; break;
10040 }
10041 }
10042 }
10043
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010044 /* Need to escape K_SPECIAL and CSI before putting the string in the
10045 * typeahead buffer. */
10046 keys_esc = vim_strsave_escape_csi(keys);
10047 if (keys_esc != NULL)
10048 {
10049 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010050 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010051 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010052 if (vgetc_busy)
10053 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010054 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010055 }
10056}
10057
10058/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 * "filereadable()" function
10060 */
10061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010063 typval_T *argvars;
10064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010066 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 char_u *p;
10068 int n;
10069
Bram Moolenaarc236c162008-07-13 17:41:49 +000010070#ifndef O_NONBLOCK
10071# define O_NONBLOCK 0
10072#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010074 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10075 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 {
10077 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010078 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 }
10080 else
10081 n = FALSE;
10082
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
10086/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010087 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 * rights to write into.
10089 */
10090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010092 typval_T *argvars;
10093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010095 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096}
10097
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010098static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010099
10100 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010101findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010102 typval_T *argvars;
10103 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010104 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010105{
10106#ifdef FEAT_SEARCHPATH
10107 char_u *fname;
10108 char_u *fresult = NULL;
10109 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10110 char_u *p;
10111 char_u pathbuf[NUMBUFLEN];
10112 int count = 1;
10113 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010114 int error = FALSE;
10115#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010116
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010117 rettv->vval.v_string = NULL;
10118 rettv->v_type = VAR_STRING;
10119
10120#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010122
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010123 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010124 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010125 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10126 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010127 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010128 else
10129 {
10130 if (*p != NUL)
10131 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010132
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010133 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010134 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010135 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010136 }
10137
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010138 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10139 error = TRUE;
10140
10141 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010142 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010143 do
10144 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010145 if (rettv->v_type == VAR_STRING)
10146 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010147 fresult = find_file_in_path_option(first ? fname : NULL,
10148 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010149 0, first, path,
10150 find_what,
10151 curbuf->b_ffname,
10152 find_what == FINDFILE_DIR
10153 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010154 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010155
10156 if (fresult != NULL && rettv->v_type == VAR_LIST)
10157 list_append_string(rettv->vval.v_list, fresult, -1);
10158
10159 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010161
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010162 if (rettv->v_type == VAR_STRING)
10163 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010164#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010165}
10166
Bram Moolenaar33570922005-01-25 22:26:29 +000010167static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10168static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010169
10170/*
10171 * Implementation of map() and filter().
10172 */
10173 static void
10174filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010175 typval_T *argvars;
10176 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010177 int map;
10178{
10179 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010180 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010181 listitem_T *li, *nli;
10182 list_T *l = NULL;
10183 dictitem_T *di;
10184 hashtab_T *ht;
10185 hashitem_T *hi;
10186 dict_T *d = NULL;
10187 typval_T save_val;
10188 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010189 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010190 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010191 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010192 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010193 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010194
Bram Moolenaare9a41262005-01-15 22:18:47 +000010195 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010196 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010197 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010198 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010199 return;
10200 }
10201 else if (argvars[0].v_type == VAR_DICT)
10202 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010203 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010204 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010205 return;
10206 }
10207 else
10208 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010209 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010210 return;
10211 }
10212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010213 expr = get_tv_string_buf_chk(&argvars[1], buf);
10214 /* On type errors, the preceding call has already displayed an error
10215 * message. Avoid a misleading error message for an empty string that
10216 * was not passed as argument. */
10217 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010219 prepare_vimvar(VV_VAL, &save_val);
10220 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010221
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010222 /* We reset "did_emsg" to be able to detect whether an error
10223 * occurred during evaluation of the expression. */
10224 save_did_emsg = did_emsg;
10225 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010226
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010227 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010228 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 vimvars[VV_KEY].vv_type = VAR_STRING;
10231
10232 ht = &d->dv_hashtab;
10233 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010234 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010235 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010237 if (!HASHITEM_EMPTY(hi))
10238 {
10239 --todo;
10240 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010241 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010242 break;
10243 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010244 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010245 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010246 break;
10247 if (!map && rem)
10248 dictitem_remove(d, di);
10249 clear_tv(&vimvars[VV_KEY].vv_tv);
10250 }
10251 }
10252 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010253 }
10254 else
10255 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010256 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10257
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010258 for (li = l->lv_first; li != NULL; li = nli)
10259 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010260 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010261 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010262 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010263 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010264 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010265 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010266 break;
10267 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010268 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010269 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010270 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010271 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010272
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010273 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010274 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010275
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010276 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010277 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278
10279 copy_tv(&argvars[0], rettv);
10280}
10281
10282 static int
10283filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010284 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010285 char_u *expr;
10286 int map;
10287 int *remp;
10288{
Bram Moolenaar33570922005-01-25 22:26:29 +000010289 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010290 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010291 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292
Bram Moolenaar33570922005-01-25 22:26:29 +000010293 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294 s = expr;
10295 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010296 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010297 if (*s != NUL) /* check for trailing chars after expr */
10298 {
10299 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010300 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010301 }
10302 if (map)
10303 {
10304 /* map(): replace the list item value */
10305 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010306 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 *tv = rettv;
10308 }
10309 else
10310 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010311 int error = FALSE;
10312
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010314 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010316 /* On type error, nothing has been removed; return FAIL to stop the
10317 * loop. The error message was given by get_tv_number_chk(). */
10318 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010319 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010320 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010321 retval = OK;
10322theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010323 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010324 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010325}
10326
10327/*
10328 * "filter()" function
10329 */
10330 static void
10331f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010332 typval_T *argvars;
10333 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010334{
10335 filter_map(argvars, rettv, FALSE);
10336}
10337
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010338/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010339 * "finddir({fname}[, {path}[, {count}]])" function
10340 */
10341 static void
10342f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010343 typval_T *argvars;
10344 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010345{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010346 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010347}
10348
10349/*
10350 * "findfile({fname}[, {path}[, {count}]])" function
10351 */
10352 static void
10353f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010354 typval_T *argvars;
10355 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010356{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010357 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010358}
10359
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010360#ifdef FEAT_FLOAT
10361/*
10362 * "float2nr({float})" function
10363 */
10364 static void
10365f_float2nr(argvars, rettv)
10366 typval_T *argvars;
10367 typval_T *rettv;
10368{
10369 float_T f;
10370
10371 if (get_float_arg(argvars, &f) == OK)
10372 {
10373 if (f < -0x7fffffff)
10374 rettv->vval.v_number = -0x7fffffff;
10375 else if (f > 0x7fffffff)
10376 rettv->vval.v_number = 0x7fffffff;
10377 else
10378 rettv->vval.v_number = (varnumber_T)f;
10379 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010380}
10381
10382/*
10383 * "floor({float})" function
10384 */
10385 static void
10386f_floor(argvars, rettv)
10387 typval_T *argvars;
10388 typval_T *rettv;
10389{
10390 float_T f;
10391
10392 rettv->v_type = VAR_FLOAT;
10393 if (get_float_arg(argvars, &f) == OK)
10394 rettv->vval.v_float = floor(f);
10395 else
10396 rettv->vval.v_float = 0.0;
10397}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010398
10399/*
10400 * "fmod()" function
10401 */
10402 static void
10403f_fmod(argvars, rettv)
10404 typval_T *argvars;
10405 typval_T *rettv;
10406{
10407 float_T fx, fy;
10408
10409 rettv->v_type = VAR_FLOAT;
10410 if (get_float_arg(argvars, &fx) == OK
10411 && get_float_arg(&argvars[1], &fy) == OK)
10412 rettv->vval.v_float = fmod(fx, fy);
10413 else
10414 rettv->vval.v_float = 0.0;
10415}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010416#endif
10417
Bram Moolenaar0d660222005-01-07 21:51:51 +000010418/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010419 * "fnameescape({string})" function
10420 */
10421 static void
10422f_fnameescape(argvars, rettv)
10423 typval_T *argvars;
10424 typval_T *rettv;
10425{
10426 rettv->vval.v_string = vim_strsave_fnameescape(
10427 get_tv_string(&argvars[0]), FALSE);
10428 rettv->v_type = VAR_STRING;
10429}
10430
10431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 * "fnamemodify({fname}, {mods})" function
10433 */
10434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010436 typval_T *argvars;
10437 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438{
10439 char_u *fname;
10440 char_u *mods;
10441 int usedlen = 0;
10442 int len;
10443 char_u *fbuf = NULL;
10444 char_u buf[NUMBUFLEN];
10445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 fname = get_tv_string_chk(&argvars[0]);
10447 mods = get_tv_string_buf_chk(&argvars[1], buf);
10448 if (fname == NULL || mods == NULL)
10449 fname = NULL;
10450 else
10451 {
10452 len = (int)STRLEN(fname);
10453 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010456 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 vim_free(fbuf);
10462}
10463
Bram Moolenaar33570922005-01-25 22:26:29 +000010464static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465
10466/*
10467 * "foldclosed()" function
10468 */
10469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010471 typval_T *argvars;
10472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 int end;
10474{
10475#ifdef FEAT_FOLDING
10476 linenr_T lnum;
10477 linenr_T first, last;
10478
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010479 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10481 {
10482 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10483 {
10484 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010485 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010487 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010488 return;
10489 }
10490 }
10491#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010492 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493}
10494
10495/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010496 * "foldclosed()" function
10497 */
10498 static void
10499f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010500 typval_T *argvars;
10501 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010502{
10503 foldclosed_both(argvars, rettv, FALSE);
10504}
10505
10506/*
10507 * "foldclosedend()" function
10508 */
10509 static void
10510f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010511 typval_T *argvars;
10512 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010513{
10514 foldclosed_both(argvars, rettv, TRUE);
10515}
10516
10517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 * "foldlevel()" function
10519 */
10520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010521f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010522 typval_T *argvars;
10523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010524{
10525#ifdef FEAT_FOLDING
10526 linenr_T lnum;
10527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010528 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532}
10533
10534/*
10535 * "foldtext()" function
10536 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010539 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541{
10542#ifdef FEAT_FOLDING
10543 linenr_T lnum;
10544 char_u *s;
10545 char_u *r;
10546 int len;
10547 char *txt;
10548#endif
10549
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010550 rettv->v_type = VAR_STRING;
10551 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10554 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10555 <= curbuf->b_ml.ml_line_count
10556 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557 {
10558 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10560 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010561 {
10562 if (!linewhite(lnum))
10563 break;
10564 ++lnum;
10565 }
10566
10567 /* Find interesting text in this line. */
10568 s = skipwhite(ml_get(lnum));
10569 /* skip C comment-start */
10570 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010573 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010575 {
10576 s = skipwhite(ml_get(lnum + 1));
10577 if (*s == '*')
10578 s = skipwhite(s + 1);
10579 }
10580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 txt = _("+-%s%3ld lines: ");
10582 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 + 20 /* for %3ld */
10585 + STRLEN(s))); /* concatenated */
10586 if (r != NULL)
10587 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10589 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10590 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 len = (int)STRLEN(r);
10592 STRCAT(r, s);
10593 /* remove 'foldmarker' and 'commentstring' */
10594 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010595 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596 }
10597 }
10598#endif
10599}
10600
10601/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010602 * "foldtextresult(lnum)" function
10603 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010604 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010605f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010606 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010607 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010608{
10609#ifdef FEAT_FOLDING
10610 linenr_T lnum;
10611 char_u *text;
10612 char_u buf[51];
10613 foldinfo_T foldinfo;
10614 int fold_count;
10615#endif
10616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010617 rettv->v_type = VAR_STRING;
10618 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010619#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010621 /* treat illegal types and illegal string values for {lnum} the same */
10622 if (lnum < 0)
10623 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010624 fold_count = foldedCount(curwin, lnum, &foldinfo);
10625 if (fold_count > 0)
10626 {
10627 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10628 &foldinfo, buf);
10629 if (text == buf)
10630 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010632 }
10633#endif
10634}
10635
10636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 * "foreground()" function
10638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010641 typval_T *argvars UNUSED;
10642 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644#ifdef FEAT_GUI
10645 if (gui.in_use)
10646 gui_mch_set_foreground();
10647#else
10648# ifdef WIN32
10649 win32_set_foreground();
10650# endif
10651#endif
10652}
10653
10654/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010655 * "function()" function
10656 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010658f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010659 typval_T *argvars;
10660 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010661{
10662 char_u *s;
10663
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010665 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010666 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010667 /* Don't check an autoload name for existence here. */
10668 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010669 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010670 else
10671 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010672 rettv->vval.v_string = vim_strsave(s);
10673 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010674 }
10675}
10676
10677/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010678 * "garbagecollect()" function
10679 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010680 static void
10681f_garbagecollect(argvars, rettv)
10682 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010683 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010684{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010685 /* This is postponed until we are back at the toplevel, because we may be
10686 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10687 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010688
10689 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10690 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010691}
10692
10693/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010694 * "get()" function
10695 */
10696 static void
10697f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010698 typval_T *argvars;
10699 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010700{
Bram Moolenaar33570922005-01-25 22:26:29 +000010701 listitem_T *li;
10702 list_T *l;
10703 dictitem_T *di;
10704 dict_T *d;
10705 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010706
Bram Moolenaare9a41262005-01-15 22:18:47 +000010707 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010708 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010709 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 int error = FALSE;
10712
10713 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10714 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010715 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010717 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010718 else if (argvars[0].v_type == VAR_DICT)
10719 {
10720 if ((d = argvars[0].vval.v_dict) != NULL)
10721 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010722 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010723 if (di != NULL)
10724 tv = &di->di_tv;
10725 }
10726 }
10727 else
10728 EMSG2(_(e_listdictarg), "get()");
10729
10730 if (tv == NULL)
10731 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010732 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010733 copy_tv(&argvars[2], rettv);
10734 }
10735 else
10736 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010737}
10738
Bram Moolenaar342337a2005-07-21 21:11:17 +000010739static 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 +000010740
10741/*
10742 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010743 * Return a range (from start to end) of lines in rettv from the specified
10744 * buffer.
10745 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010746 */
10747 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010748get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010749 buf_T *buf;
10750 linenr_T start;
10751 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010752 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010753 typval_T *rettv;
10754{
10755 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010756
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010757 if (retlist && rettv_list_alloc(rettv) == FAIL)
10758 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010759
10760 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10761 return;
10762
10763 if (!retlist)
10764 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010765 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10766 p = ml_get_buf(buf, start, FALSE);
10767 else
10768 p = (char_u *)"";
10769
10770 rettv->v_type = VAR_STRING;
10771 rettv->vval.v_string = vim_strsave(p);
10772 }
10773 else
10774 {
10775 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010776 return;
10777
10778 if (start < 1)
10779 start = 1;
10780 if (end > buf->b_ml.ml_line_count)
10781 end = buf->b_ml.ml_line_count;
10782 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010783 if (list_append_string(rettv->vval.v_list,
10784 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010785 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010786 }
10787}
10788
10789/*
10790 * "getbufline()" function
10791 */
10792 static void
10793f_getbufline(argvars, rettv)
10794 typval_T *argvars;
10795 typval_T *rettv;
10796{
10797 linenr_T lnum;
10798 linenr_T end;
10799 buf_T *buf;
10800
10801 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10802 ++emsg_off;
10803 buf = get_buf_tv(&argvars[0]);
10804 --emsg_off;
10805
Bram Moolenaar661b1822005-07-28 22:36:45 +000010806 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010807 if (argvars[2].v_type == VAR_UNKNOWN)
10808 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010809 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010810 end = get_tv_lnum_buf(&argvars[2], buf);
10811
Bram Moolenaar342337a2005-07-21 21:11:17 +000010812 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010813}
10814
Bram Moolenaar0d660222005-01-07 21:51:51 +000010815/*
10816 * "getbufvar()" function
10817 */
10818 static void
10819f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 typval_T *argvars;
10821 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010822{
10823 buf_T *buf;
10824 buf_T *save_curbuf;
10825 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010826 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10829 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010830 ++emsg_off;
10831 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010832
10833 rettv->v_type = VAR_STRING;
10834 rettv->vval.v_string = NULL;
10835
10836 if (buf != NULL && varname != NULL)
10837 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010838 /* set curbuf to be our buf, temporarily */
10839 save_curbuf = curbuf;
10840 curbuf = buf;
10841
Bram Moolenaar0d660222005-01-07 21:51:51 +000010842 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010843 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010844 else
10845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010846 if (*varname == NUL)
10847 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10848 * scope prefix before the NUL byte is required by
10849 * find_var_in_ht(). */
10850 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010851 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010852 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010853 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010855 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010856
10857 /* restore previous notion of curbuf */
10858 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010859 }
10860
10861 --emsg_off;
10862}
10863
10864/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865 * "getchar()" function
10866 */
10867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010869 typval_T *argvars;
10870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871{
10872 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010873 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010875 /* Position the cursor. Needed after a message that ends in a space. */
10876 windgoto(msg_row, msg_col);
10877
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 ++no_mapping;
10879 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010880 for (;;)
10881 {
10882 if (argvars[0].v_type == VAR_UNKNOWN)
10883 /* getchar(): blocking wait. */
10884 n = safe_vgetc();
10885 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10886 /* getchar(1): only check if char avail */
10887 n = vpeekc();
10888 else if (error || vpeekc() == NUL)
10889 /* illegal argument or getchar(0) and no char avail: return zero */
10890 n = 0;
10891 else
10892 /* getchar(0) and char avail: return char */
10893 n = safe_vgetc();
10894 if (n == K_IGNORE)
10895 continue;
10896 break;
10897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 --no_mapping;
10899 --allow_keys;
10900
Bram Moolenaar219b8702006-11-01 14:32:36 +000010901 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10902 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10903 vimvars[VV_MOUSE_COL].vv_nr = 0;
10904
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 if (IS_SPECIAL(n) || mod_mask != 0)
10907 {
10908 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10909 int i = 0;
10910
10911 /* Turn a special key into three bytes, plus modifier. */
10912 if (mod_mask != 0)
10913 {
10914 temp[i++] = K_SPECIAL;
10915 temp[i++] = KS_MODIFIER;
10916 temp[i++] = mod_mask;
10917 }
10918 if (IS_SPECIAL(n))
10919 {
10920 temp[i++] = K_SPECIAL;
10921 temp[i++] = K_SECOND(n);
10922 temp[i++] = K_THIRD(n);
10923 }
10924#ifdef FEAT_MBYTE
10925 else if (has_mbyte)
10926 i += (*mb_char2bytes)(n, temp + i);
10927#endif
10928 else
10929 temp[i++] = n;
10930 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931 rettv->v_type = VAR_STRING;
10932 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010933
10934#ifdef FEAT_MOUSE
10935 if (n == K_LEFTMOUSE
10936 || n == K_LEFTMOUSE_NM
10937 || n == K_LEFTDRAG
10938 || n == K_LEFTRELEASE
10939 || n == K_LEFTRELEASE_NM
10940 || n == K_MIDDLEMOUSE
10941 || n == K_MIDDLEDRAG
10942 || n == K_MIDDLERELEASE
10943 || n == K_RIGHTMOUSE
10944 || n == K_RIGHTDRAG
10945 || n == K_RIGHTRELEASE
10946 || n == K_X1MOUSE
10947 || n == K_X1DRAG
10948 || n == K_X1RELEASE
10949 || n == K_X2MOUSE
10950 || n == K_X2DRAG
10951 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010952 || n == K_MOUSELEFT
10953 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010954 || n == K_MOUSEDOWN
10955 || n == K_MOUSEUP)
10956 {
10957 int row = mouse_row;
10958 int col = mouse_col;
10959 win_T *win;
10960 linenr_T lnum;
10961# ifdef FEAT_WINDOWS
10962 win_T *wp;
10963# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010964 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010965
10966 if (row >= 0 && col >= 0)
10967 {
10968 /* Find the window at the mouse coordinates and compute the
10969 * text position. */
10970 win = mouse_find_win(&row, &col);
10971 (void)mouse_comp_pos(win, &row, &col, &lnum);
10972# ifdef FEAT_WINDOWS
10973 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010974 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010975# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010976 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010977 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10978 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10979 }
10980 }
10981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 }
10983}
10984
10985/*
10986 * "getcharmod()" function
10987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010989f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010990 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010993 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994}
10995
10996/*
10997 * "getcmdline()" function
10998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011000f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011001 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011004 rettv->v_type = VAR_STRING;
11005 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006}
11007
11008/*
11009 * "getcmdpos()" function
11010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011012f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011013 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017}
11018
11019/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011020 * "getcmdtype()" function
11021 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011022 static void
11023f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011024 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011025 typval_T *rettv;
11026{
11027 rettv->v_type = VAR_STRING;
11028 rettv->vval.v_string = alloc(2);
11029 if (rettv->vval.v_string != NULL)
11030 {
11031 rettv->vval.v_string[0] = get_cmdline_type();
11032 rettv->vval.v_string[1] = NUL;
11033 }
11034}
11035
11036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 * "getcwd()" function
11038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011041 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043{
11044 char_u cwd[MAXPATHL];
11045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 else
11050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011053 if (rettv->vval.v_string != NULL)
11054 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055#endif
11056 }
11057}
11058
11059/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011060 * "getfontname()" function
11061 */
11062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011064 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011065 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067 rettv->v_type = VAR_STRING;
11068 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011069#ifdef FEAT_GUI
11070 if (gui.in_use)
11071 {
11072 GuiFont font;
11073 char_u *name = NULL;
11074
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011075 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011076 {
11077 /* Get the "Normal" font. Either the name saved by
11078 * hl_set_font_name() or from the font ID. */
11079 font = gui.norm_font;
11080 name = hl_get_font_name();
11081 }
11082 else
11083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011084 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011085 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11086 return;
11087 font = gui_mch_get_font(name, FALSE);
11088 if (font == NOFONT)
11089 return; /* Invalid font name, return empty string. */
11090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011091 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011092 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011093 gui_mch_free_font(font);
11094 }
11095#endif
11096}
11097
11098/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011099 * "getfperm({fname})" function
11100 */
11101 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011102f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 typval_T *argvars;
11104 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011105{
11106 char_u *fname;
11107 struct stat st;
11108 char_u *perm = NULL;
11109 char_u flags[] = "rwx";
11110 int i;
11111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011112 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011115 if (mch_stat((char *)fname, &st) >= 0)
11116 {
11117 perm = vim_strsave((char_u *)"---------");
11118 if (perm != NULL)
11119 {
11120 for (i = 0; i < 9; i++)
11121 {
11122 if (st.st_mode & (1 << (8 - i)))
11123 perm[i] = flags[i % 3];
11124 }
11125 }
11126 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011128}
11129
11130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 * "getfsize({fname})" function
11132 */
11133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011134f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011135 typval_T *argvars;
11136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011137{
11138 char_u *fname;
11139 struct stat st;
11140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011141 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011143 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011144
11145 if (mch_stat((char *)fname, &st) >= 0)
11146 {
11147 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011152
11153 /* non-perfect check for overflow */
11154 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11155 rettv->vval.v_number = -2;
11156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011157 }
11158 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011159 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160}
11161
11162/*
11163 * "getftime({fname})" function
11164 */
11165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 typval_T *argvars;
11168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169{
11170 char_u *fname;
11171 struct stat st;
11172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011173 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174
11175 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011179}
11180
11181/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011182 * "getftype({fname})" function
11183 */
11184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011185f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011186 typval_T *argvars;
11187 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011188{
11189 char_u *fname;
11190 struct stat st;
11191 char_u *type = NULL;
11192 char *t;
11193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011194 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011197 if (mch_lstat((char *)fname, &st) >= 0)
11198 {
11199#ifdef S_ISREG
11200 if (S_ISREG(st.st_mode))
11201 t = "file";
11202 else if (S_ISDIR(st.st_mode))
11203 t = "dir";
11204# ifdef S_ISLNK
11205 else if (S_ISLNK(st.st_mode))
11206 t = "link";
11207# endif
11208# ifdef S_ISBLK
11209 else if (S_ISBLK(st.st_mode))
11210 t = "bdev";
11211# endif
11212# ifdef S_ISCHR
11213 else if (S_ISCHR(st.st_mode))
11214 t = "cdev";
11215# endif
11216# ifdef S_ISFIFO
11217 else if (S_ISFIFO(st.st_mode))
11218 t = "fifo";
11219# endif
11220# ifdef S_ISSOCK
11221 else if (S_ISSOCK(st.st_mode))
11222 t = "fifo";
11223# endif
11224 else
11225 t = "other";
11226#else
11227# ifdef S_IFMT
11228 switch (st.st_mode & S_IFMT)
11229 {
11230 case S_IFREG: t = "file"; break;
11231 case S_IFDIR: t = "dir"; break;
11232# ifdef S_IFLNK
11233 case S_IFLNK: t = "link"; break;
11234# endif
11235# ifdef S_IFBLK
11236 case S_IFBLK: t = "bdev"; break;
11237# endif
11238# ifdef S_IFCHR
11239 case S_IFCHR: t = "cdev"; break;
11240# endif
11241# ifdef S_IFIFO
11242 case S_IFIFO: t = "fifo"; break;
11243# endif
11244# ifdef S_IFSOCK
11245 case S_IFSOCK: t = "socket"; break;
11246# endif
11247 default: t = "other";
11248 }
11249# else
11250 if (mch_isdir(fname))
11251 t = "dir";
11252 else
11253 t = "file";
11254# endif
11255#endif
11256 type = vim_strsave((char_u *)t);
11257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011259}
11260
11261/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011262 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011263 */
11264 static void
11265f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011266 typval_T *argvars;
11267 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011268{
11269 linenr_T lnum;
11270 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011271 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011272
11273 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011274 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011275 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011276 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011277 retlist = FALSE;
11278 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011279 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011281 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011282 retlist = TRUE;
11283 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011284
Bram Moolenaar342337a2005-07-21 21:11:17 +000011285 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011286}
11287
11288/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011289 * "getmatches()" function
11290 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011291 static void
11292f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011293 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011294 typval_T *rettv;
11295{
11296#ifdef FEAT_SEARCH_EXTRA
11297 dict_T *dict;
11298 matchitem_T *cur = curwin->w_match_head;
11299
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011300 if (rettv_list_alloc(rettv) == OK)
11301 {
11302 while (cur != NULL)
11303 {
11304 dict = dict_alloc();
11305 if (dict == NULL)
11306 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011307 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11308 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11309 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11310 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11311 list_append_dict(rettv->vval.v_list, dict);
11312 cur = cur->next;
11313 }
11314 }
11315#endif
11316}
11317
11318/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011319 * "getpid()" function
11320 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011321 static void
11322f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011323 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011324 typval_T *rettv;
11325{
11326 rettv->vval.v_number = mch_get_pid();
11327}
11328
11329/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011330 * "getpos(string)" function
11331 */
11332 static void
11333f_getpos(argvars, rettv)
11334 typval_T *argvars;
11335 typval_T *rettv;
11336{
11337 pos_T *fp;
11338 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011339 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011340
11341 if (rettv_list_alloc(rettv) == OK)
11342 {
11343 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011344 fp = var2fpos(&argvars[0], TRUE, &fnum);
11345 if (fnum != -1)
11346 list_append_number(l, (varnumber_T)fnum);
11347 else
11348 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011349 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11350 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011351 list_append_number(l, (fp != NULL)
11352 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011353 : (varnumber_T)0);
11354 list_append_number(l,
11355#ifdef FEAT_VIRTUALEDIT
11356 (fp != NULL) ? (varnumber_T)fp->coladd :
11357#endif
11358 (varnumber_T)0);
11359 }
11360 else
11361 rettv->vval.v_number = FALSE;
11362}
11363
11364/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011365 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011366 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011367 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011368f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011369 typval_T *argvars UNUSED;
11370 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011371{
11372#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011373 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011374#endif
11375
Bram Moolenaar2641f772005-03-25 21:58:17 +000011376#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011377 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011378 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011379 wp = NULL;
11380 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11381 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011382 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011383 if (wp == NULL)
11384 return;
11385 }
11386
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011387 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011388 }
11389#endif
11390}
11391
11392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 * "getreg()" function
11394 */
11395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011396f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011397 typval_T *argvars;
11398 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
11400 char_u *strregname;
11401 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011402 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011404
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011405 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011407 strregname = get_tv_string_chk(&argvars[0]);
11408 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011409 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011413 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 regname = (strregname == NULL ? '"' : *strregname);
11415 if (regname == 0)
11416 regname = '"';
11417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011419 rettv->vval.v_string = error ? NULL :
11420 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421}
11422
11423/*
11424 * "getregtype()" function
11425 */
11426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011428 typval_T *argvars;
11429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430{
11431 char_u *strregname;
11432 int regname;
11433 char_u buf[NUMBUFLEN + 2];
11434 long reglen = 0;
11435
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011436 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011437 {
11438 strregname = get_tv_string_chk(&argvars[0]);
11439 if (strregname == NULL) /* type error; errmsg already given */
11440 {
11441 rettv->v_type = VAR_STRING;
11442 rettv->vval.v_string = NULL;
11443 return;
11444 }
11445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 else
11447 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011448 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449
11450 regname = (strregname == NULL ? '"' : *strregname);
11451 if (regname == 0)
11452 regname = '"';
11453
11454 buf[0] = NUL;
11455 buf[1] = NUL;
11456 switch (get_reg_type(regname, &reglen))
11457 {
11458 case MLINE: buf[0] = 'V'; break;
11459 case MCHAR: buf[0] = 'v'; break;
11460#ifdef FEAT_VISUAL
11461 case MBLOCK:
11462 buf[0] = Ctrl_V;
11463 sprintf((char *)buf + 1, "%ld", reglen + 1);
11464 break;
11465#endif
11466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->v_type = VAR_STRING;
11468 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011469}
11470
11471/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011472 * "gettabvar()" function
11473 */
11474 static void
11475f_gettabvar(argvars, rettv)
11476 typval_T *argvars;
11477 typval_T *rettv;
11478{
11479 tabpage_T *tp;
11480 dictitem_T *v;
11481 char_u *varname;
11482
11483 rettv->v_type = VAR_STRING;
11484 rettv->vval.v_string = NULL;
11485
11486 varname = get_tv_string_chk(&argvars[1]);
11487 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11488 if (tp != NULL && varname != NULL)
11489 {
11490 /* look up the variable */
11491 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11492 if (v != NULL)
11493 copy_tv(&v->di_tv, rettv);
11494 }
11495}
11496
11497/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011498 * "gettabwinvar()" function
11499 */
11500 static void
11501f_gettabwinvar(argvars, rettv)
11502 typval_T *argvars;
11503 typval_T *rettv;
11504{
11505 getwinvar(argvars, rettv, 1);
11506}
11507
11508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509 * "getwinposx()" function
11510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011513 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011514 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011516 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517#ifdef FEAT_GUI
11518 if (gui.in_use)
11519 {
11520 int x, y;
11521
11522 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011523 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011524 }
11525#endif
11526}
11527
11528/*
11529 * "getwinposy()" function
11530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011532f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011533 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011536 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011537#ifdef FEAT_GUI
11538 if (gui.in_use)
11539 {
11540 int x, y;
11541
11542 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011543 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 }
11545#endif
11546}
11547
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011548/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011549 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011550 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011551 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011552find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011553 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011554 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011555{
11556#ifdef FEAT_WINDOWS
11557 win_T *wp;
11558#endif
11559 int nr;
11560
11561 nr = get_tv_number_chk(vp, NULL);
11562
11563#ifdef FEAT_WINDOWS
11564 if (nr < 0)
11565 return NULL;
11566 if (nr == 0)
11567 return curwin;
11568
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011569 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11570 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011571 if (--nr <= 0)
11572 break;
11573 return wp;
11574#else
11575 if (nr == 0 || nr == 1)
11576 return curwin;
11577 return NULL;
11578#endif
11579}
11580
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581/*
11582 * "getwinvar()" function
11583 */
11584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011586 typval_T *argvars;
11587 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011588{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011589 getwinvar(argvars, rettv, 0);
11590}
11591
11592/*
11593 * getwinvar() and gettabwinvar()
11594 */
11595 static void
11596getwinvar(argvars, rettv, off)
11597 typval_T *argvars;
11598 typval_T *rettv;
11599 int off; /* 1 for gettabwinvar() */
11600{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011601 win_T *win, *oldcurwin;
11602 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011603 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011604 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011606#ifdef FEAT_WINDOWS
11607 if (off == 1)
11608 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11609 else
11610 tp = curtab;
11611#endif
11612 win = find_win_by_nr(&argvars[off], tp);
11613 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011614 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 rettv->v_type = VAR_STRING;
11617 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618
11619 if (win != NULL && varname != NULL)
11620 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011621 /* Set curwin to be our win, temporarily. Also set curbuf, so
11622 * that we can get buffer-local options. */
11623 oldcurwin = curwin;
11624 curwin = win;
11625 curbuf = win->w_buffer;
11626
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011628 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 else
11630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011631 if (*varname == NUL)
11632 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11633 * scope prefix before the NUL byte is required by
11634 * find_var_in_ht(). */
11635 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011636 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011637 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011639 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011641
11642 /* restore previous notion of curwin */
11643 curwin = oldcurwin;
11644 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645 }
11646
11647 --emsg_off;
11648}
11649
11650/*
11651 * "glob()" function
11652 */
11653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011654f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011655 typval_T *argvars;
11656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011658 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011660 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011661
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011662 /* When the optional second argument is non-zero, don't remove matches
11663 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11664 if (argvars[1].v_type != VAR_UNKNOWN
11665 && get_tv_number_chk(&argvars[1], &error))
11666 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011667 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011668 if (!error)
11669 {
11670 ExpandInit(&xpc);
11671 xpc.xp_context = EXPAND_FILES;
11672 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11673 NULL, flags, WILD_ALL);
11674 }
11675 else
11676 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677}
11678
11679/*
11680 * "globpath()" function
11681 */
11682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011684 typval_T *argvars;
11685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011686{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011687 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011689 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011690 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011692 /* When the optional second argument is non-zero, don't remove matches
11693 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11694 if (argvars[2].v_type != VAR_UNKNOWN
11695 && get_tv_number_chk(&argvars[2], &error))
11696 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011697 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011698 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699 rettv->vval.v_string = NULL;
11700 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011701 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11702 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703}
11704
11705/*
11706 * "has()" function
11707 */
11708 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011710 typval_T *argvars;
11711 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011712{
11713 int i;
11714 char_u *name;
11715 int n = FALSE;
11716 static char *(has_list[]) =
11717 {
11718#ifdef AMIGA
11719 "amiga",
11720# ifdef FEAT_ARP
11721 "arp",
11722# endif
11723#endif
11724#ifdef __BEOS__
11725 "beos",
11726#endif
11727#ifdef MSDOS
11728# ifdef DJGPP
11729 "dos32",
11730# else
11731 "dos16",
11732# endif
11733#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011734#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735 "mac",
11736#endif
11737#if defined(MACOS_X_UNIX)
11738 "macunix",
11739#endif
11740#ifdef OS2
11741 "os2",
11742#endif
11743#ifdef __QNX__
11744 "qnx",
11745#endif
11746#ifdef RISCOS
11747 "riscos",
11748#endif
11749#ifdef UNIX
11750 "unix",
11751#endif
11752#ifdef VMS
11753 "vms",
11754#endif
11755#ifdef WIN16
11756 "win16",
11757#endif
11758#ifdef WIN32
11759 "win32",
11760#endif
11761#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11762 "win32unix",
11763#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011764#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765 "win64",
11766#endif
11767#ifdef EBCDIC
11768 "ebcdic",
11769#endif
11770#ifndef CASE_INSENSITIVE_FILENAME
11771 "fname_case",
11772#endif
11773#ifdef FEAT_ARABIC
11774 "arabic",
11775#endif
11776#ifdef FEAT_AUTOCMD
11777 "autocmd",
11778#endif
11779#ifdef FEAT_BEVAL
11780 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011781# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11782 "balloon_multiline",
11783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784#endif
11785#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11786 "builtin_terms",
11787# ifdef ALL_BUILTIN_TCAPS
11788 "all_builtin_terms",
11789# endif
11790#endif
11791#ifdef FEAT_BYTEOFF
11792 "byte_offset",
11793#endif
11794#ifdef FEAT_CINDENT
11795 "cindent",
11796#endif
11797#ifdef FEAT_CLIENTSERVER
11798 "clientserver",
11799#endif
11800#ifdef FEAT_CLIPBOARD
11801 "clipboard",
11802#endif
11803#ifdef FEAT_CMDL_COMPL
11804 "cmdline_compl",
11805#endif
11806#ifdef FEAT_CMDHIST
11807 "cmdline_hist",
11808#endif
11809#ifdef FEAT_COMMENTS
11810 "comments",
11811#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011812#ifdef FEAT_CONCEAL
11813 "conceal",
11814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815#ifdef FEAT_CRYPT
11816 "cryptv",
11817#endif
11818#ifdef FEAT_CSCOPE
11819 "cscope",
11820#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011821#ifdef FEAT_CURSORBIND
11822 "cursorbind",
11823#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011824#ifdef CURSOR_SHAPE
11825 "cursorshape",
11826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827#ifdef DEBUG
11828 "debug",
11829#endif
11830#ifdef FEAT_CON_DIALOG
11831 "dialog_con",
11832#endif
11833#ifdef FEAT_GUI_DIALOG
11834 "dialog_gui",
11835#endif
11836#ifdef FEAT_DIFF
11837 "diff",
11838#endif
11839#ifdef FEAT_DIGRAPHS
11840 "digraphs",
11841#endif
11842#ifdef FEAT_DND
11843 "dnd",
11844#endif
11845#ifdef FEAT_EMACS_TAGS
11846 "emacs_tags",
11847#endif
11848 "eval", /* always present, of course! */
11849#ifdef FEAT_EX_EXTRA
11850 "ex_extra",
11851#endif
11852#ifdef FEAT_SEARCH_EXTRA
11853 "extra_search",
11854#endif
11855#ifdef FEAT_FKMAP
11856 "farsi",
11857#endif
11858#ifdef FEAT_SEARCHPATH
11859 "file_in_path",
11860#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011861#if defined(UNIX) && !defined(USE_SYSTEM)
11862 "filterpipe",
11863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864#ifdef FEAT_FIND_ID
11865 "find_in_path",
11866#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011867#ifdef FEAT_FLOAT
11868 "float",
11869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870#ifdef FEAT_FOLDING
11871 "folding",
11872#endif
11873#ifdef FEAT_FOOTER
11874 "footer",
11875#endif
11876#if !defined(USE_SYSTEM) && defined(UNIX)
11877 "fork",
11878#endif
11879#ifdef FEAT_GETTEXT
11880 "gettext",
11881#endif
11882#ifdef FEAT_GUI
11883 "gui",
11884#endif
11885#ifdef FEAT_GUI_ATHENA
11886# ifdef FEAT_GUI_NEXTAW
11887 "gui_neXtaw",
11888# else
11889 "gui_athena",
11890# endif
11891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892#ifdef FEAT_GUI_GTK
11893 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011895#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011896#ifdef FEAT_GUI_GNOME
11897 "gui_gnome",
11898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899#ifdef FEAT_GUI_MAC
11900 "gui_mac",
11901#endif
11902#ifdef FEAT_GUI_MOTIF
11903 "gui_motif",
11904#endif
11905#ifdef FEAT_GUI_PHOTON
11906 "gui_photon",
11907#endif
11908#ifdef FEAT_GUI_W16
11909 "gui_win16",
11910#endif
11911#ifdef FEAT_GUI_W32
11912 "gui_win32",
11913#endif
11914#ifdef FEAT_HANGULIN
11915 "hangul_input",
11916#endif
11917#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11918 "iconv",
11919#endif
11920#ifdef FEAT_INS_EXPAND
11921 "insert_expand",
11922#endif
11923#ifdef FEAT_JUMPLIST
11924 "jumplist",
11925#endif
11926#ifdef FEAT_KEYMAP
11927 "keymap",
11928#endif
11929#ifdef FEAT_LANGMAP
11930 "langmap",
11931#endif
11932#ifdef FEAT_LIBCALL
11933 "libcall",
11934#endif
11935#ifdef FEAT_LINEBREAK
11936 "linebreak",
11937#endif
11938#ifdef FEAT_LISP
11939 "lispindent",
11940#endif
11941#ifdef FEAT_LISTCMDS
11942 "listcmds",
11943#endif
11944#ifdef FEAT_LOCALMAP
11945 "localmap",
11946#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011947#ifdef FEAT_LUA
11948# ifndef DYNAMIC_LUA
11949 "lua",
11950# endif
11951#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011952#ifdef FEAT_MENU
11953 "menu",
11954#endif
11955#ifdef FEAT_SESSION
11956 "mksession",
11957#endif
11958#ifdef FEAT_MODIFY_FNAME
11959 "modify_fname",
11960#endif
11961#ifdef FEAT_MOUSE
11962 "mouse",
11963#endif
11964#ifdef FEAT_MOUSESHAPE
11965 "mouseshape",
11966#endif
11967#if defined(UNIX) || defined(VMS)
11968# ifdef FEAT_MOUSE_DEC
11969 "mouse_dec",
11970# endif
11971# ifdef FEAT_MOUSE_GPM
11972 "mouse_gpm",
11973# endif
11974# ifdef FEAT_MOUSE_JSB
11975 "mouse_jsbterm",
11976# endif
11977# ifdef FEAT_MOUSE_NET
11978 "mouse_netterm",
11979# endif
11980# ifdef FEAT_MOUSE_PTERM
11981 "mouse_pterm",
11982# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011983# ifdef FEAT_SYSMOUSE
11984 "mouse_sysmouse",
11985# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011986# ifdef FEAT_MOUSE_XTERM
11987 "mouse_xterm",
11988# endif
11989#endif
11990#ifdef FEAT_MBYTE
11991 "multi_byte",
11992#endif
11993#ifdef FEAT_MBYTE_IME
11994 "multi_byte_ime",
11995#endif
11996#ifdef FEAT_MULTI_LANG
11997 "multi_lang",
11998#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011999#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012000#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012001 "mzscheme",
12002#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004#ifdef FEAT_OLE
12005 "ole",
12006#endif
12007#ifdef FEAT_OSFILETYPE
12008 "osfiletype",
12009#endif
12010#ifdef FEAT_PATH_EXTRA
12011 "path_extra",
12012#endif
12013#ifdef FEAT_PERL
12014#ifndef DYNAMIC_PERL
12015 "perl",
12016#endif
12017#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012018#ifdef FEAT_PERSISTENT_UNDO
12019 "persistent_undo",
12020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021#ifdef FEAT_PYTHON
12022#ifndef DYNAMIC_PYTHON
12023 "python",
12024#endif
12025#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012026#ifdef FEAT_PYTHON3
12027#ifndef DYNAMIC_PYTHON3
12028 "python3",
12029#endif
12030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012031#ifdef FEAT_POSTSCRIPT
12032 "postscript",
12033#endif
12034#ifdef FEAT_PRINTER
12035 "printer",
12036#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012037#ifdef FEAT_PROFILE
12038 "profile",
12039#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012040#ifdef FEAT_RELTIME
12041 "reltime",
12042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043#ifdef FEAT_QUICKFIX
12044 "quickfix",
12045#endif
12046#ifdef FEAT_RIGHTLEFT
12047 "rightleft",
12048#endif
12049#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12050 "ruby",
12051#endif
12052#ifdef FEAT_SCROLLBIND
12053 "scrollbind",
12054#endif
12055#ifdef FEAT_CMDL_INFO
12056 "showcmd",
12057 "cmdline_info",
12058#endif
12059#ifdef FEAT_SIGNS
12060 "signs",
12061#endif
12062#ifdef FEAT_SMARTINDENT
12063 "smartindent",
12064#endif
12065#ifdef FEAT_SNIFF
12066 "sniff",
12067#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012068#ifdef STARTUPTIME
12069 "startuptime",
12070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071#ifdef FEAT_STL_OPT
12072 "statusline",
12073#endif
12074#ifdef FEAT_SUN_WORKSHOP
12075 "sun_workshop",
12076#endif
12077#ifdef FEAT_NETBEANS_INTG
12078 "netbeans_intg",
12079#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012080#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012081 "spell",
12082#endif
12083#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012084 "syntax",
12085#endif
12086#if defined(USE_SYSTEM) || !defined(UNIX)
12087 "system",
12088#endif
12089#ifdef FEAT_TAG_BINS
12090 "tag_binary",
12091#endif
12092#ifdef FEAT_TAG_OLDSTATIC
12093 "tag_old_static",
12094#endif
12095#ifdef FEAT_TAG_ANYWHITE
12096 "tag_any_white",
12097#endif
12098#ifdef FEAT_TCL
12099# ifndef DYNAMIC_TCL
12100 "tcl",
12101# endif
12102#endif
12103#ifdef TERMINFO
12104 "terminfo",
12105#endif
12106#ifdef FEAT_TERMRESPONSE
12107 "termresponse",
12108#endif
12109#ifdef FEAT_TEXTOBJ
12110 "textobjects",
12111#endif
12112#ifdef HAVE_TGETENT
12113 "tgetent",
12114#endif
12115#ifdef FEAT_TITLE
12116 "title",
12117#endif
12118#ifdef FEAT_TOOLBAR
12119 "toolbar",
12120#endif
12121#ifdef FEAT_USR_CMDS
12122 "user-commands", /* was accidentally included in 5.4 */
12123 "user_commands",
12124#endif
12125#ifdef FEAT_VIMINFO
12126 "viminfo",
12127#endif
12128#ifdef FEAT_VERTSPLIT
12129 "vertsplit",
12130#endif
12131#ifdef FEAT_VIRTUALEDIT
12132 "virtualedit",
12133#endif
12134#ifdef FEAT_VISUAL
12135 "visual",
12136#endif
12137#ifdef FEAT_VISUALEXTRA
12138 "visualextra",
12139#endif
12140#ifdef FEAT_VREPLACE
12141 "vreplace",
12142#endif
12143#ifdef FEAT_WILDIGN
12144 "wildignore",
12145#endif
12146#ifdef FEAT_WILDMENU
12147 "wildmenu",
12148#endif
12149#ifdef FEAT_WINDOWS
12150 "windows",
12151#endif
12152#ifdef FEAT_WAK
12153 "winaltkeys",
12154#endif
12155#ifdef FEAT_WRITEBACKUP
12156 "writebackup",
12157#endif
12158#ifdef FEAT_XIM
12159 "xim",
12160#endif
12161#ifdef FEAT_XFONTSET
12162 "xfontset",
12163#endif
12164#ifdef USE_XSMP
12165 "xsmp",
12166#endif
12167#ifdef USE_XSMP_INTERACT
12168 "xsmp_interact",
12169#endif
12170#ifdef FEAT_XCLIPBOARD
12171 "xterm_clipboard",
12172#endif
12173#ifdef FEAT_XTERM_SAVE
12174 "xterm_save",
12175#endif
12176#if defined(UNIX) && defined(FEAT_X11)
12177 "X11",
12178#endif
12179 NULL
12180 };
12181
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012182 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012183 for (i = 0; has_list[i] != NULL; ++i)
12184 if (STRICMP(name, has_list[i]) == 0)
12185 {
12186 n = TRUE;
12187 break;
12188 }
12189
12190 if (n == FALSE)
12191 {
12192 if (STRNICMP(name, "patch", 5) == 0)
12193 n = has_patch(atoi((char *)name + 5));
12194 else if (STRICMP(name, "vim_starting") == 0)
12195 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012196#ifdef FEAT_MBYTE
12197 else if (STRICMP(name, "multi_byte_encoding") == 0)
12198 n = has_mbyte;
12199#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012200#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12201 else if (STRICMP(name, "balloon_multiline") == 0)
12202 n = multiline_balloon_available();
12203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204#ifdef DYNAMIC_TCL
12205 else if (STRICMP(name, "tcl") == 0)
12206 n = tcl_enabled(FALSE);
12207#endif
12208#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12209 else if (STRICMP(name, "iconv") == 0)
12210 n = iconv_enabled(FALSE);
12211#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012212#ifdef DYNAMIC_LUA
12213 else if (STRICMP(name, "lua") == 0)
12214 n = lua_enabled(FALSE);
12215#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012216#ifdef DYNAMIC_MZSCHEME
12217 else if (STRICMP(name, "mzscheme") == 0)
12218 n = mzscheme_enabled(FALSE);
12219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012220#ifdef DYNAMIC_RUBY
12221 else if (STRICMP(name, "ruby") == 0)
12222 n = ruby_enabled(FALSE);
12223#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012224#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225#ifdef DYNAMIC_PYTHON
12226 else if (STRICMP(name, "python") == 0)
12227 n = python_enabled(FALSE);
12228#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012229#endif
12230#ifdef FEAT_PYTHON3
12231#ifdef DYNAMIC_PYTHON3
12232 else if (STRICMP(name, "python3") == 0)
12233 n = python3_enabled(FALSE);
12234#endif
12235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012236#ifdef DYNAMIC_PERL
12237 else if (STRICMP(name, "perl") == 0)
12238 n = perl_enabled(FALSE);
12239#endif
12240#ifdef FEAT_GUI
12241 else if (STRICMP(name, "gui_running") == 0)
12242 n = (gui.in_use || gui.starting);
12243# ifdef FEAT_GUI_W32
12244 else if (STRICMP(name, "gui_win32s") == 0)
12245 n = gui_is_win32s();
12246# endif
12247# ifdef FEAT_BROWSE
12248 else if (STRICMP(name, "browse") == 0)
12249 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12250# endif
12251#endif
12252#ifdef FEAT_SYN_HL
12253 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012254 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012255#endif
12256#if defined(WIN3264)
12257 else if (STRICMP(name, "win95") == 0)
12258 n = mch_windows95();
12259#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012260#ifdef FEAT_NETBEANS_INTG
12261 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012262 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 }
12265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012266 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267}
12268
12269/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012270 * "has_key()" function
12271 */
12272 static void
12273f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012274 typval_T *argvars;
12275 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012276{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012277 if (argvars[0].v_type != VAR_DICT)
12278 {
12279 EMSG(_(e_dictreq));
12280 return;
12281 }
12282 if (argvars[0].vval.v_dict == NULL)
12283 return;
12284
12285 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012286 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012287}
12288
12289/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012290 * "haslocaldir()" function
12291 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012292 static void
12293f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012294 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012295 typval_T *rettv;
12296{
12297 rettv->vval.v_number = (curwin->w_localdir != NULL);
12298}
12299
12300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 * "hasmapto()" function
12302 */
12303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012304f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012305 typval_T *argvars;
12306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012307{
12308 char_u *name;
12309 char_u *mode;
12310 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012311 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012313 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012314 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 mode = (char_u *)"nvo";
12316 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012318 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012319 if (argvars[2].v_type != VAR_UNKNOWN)
12320 abbr = get_tv_number(&argvars[2]);
12321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322
Bram Moolenaar2c932302006-03-18 21:42:09 +000012323 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012326 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327}
12328
12329/*
12330 * "histadd()" function
12331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012333f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012334 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336{
12337#ifdef FEAT_CMDHIST
12338 int histype;
12339 char_u *str;
12340 char_u buf[NUMBUFLEN];
12341#endif
12342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344 if (check_restricted() || check_secure())
12345 return;
12346#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012347 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12348 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 if (histype >= 0)
12350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012351 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 if (*str != NUL)
12353 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012354 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012355 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012356 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012357 return;
12358 }
12359 }
12360#endif
12361}
12362
12363/*
12364 * "histdel()" function
12365 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012367f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012368 typval_T *argvars UNUSED;
12369 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370{
12371#ifdef FEAT_CMDHIST
12372 int n;
12373 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012374 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012376 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12377 if (str == NULL)
12378 n = 0;
12379 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012381 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012382 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012384 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012385 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 else
12387 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012388 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012389 get_tv_string_buf(&argvars[1], buf));
12390 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391#endif
12392}
12393
12394/*
12395 * "histget()" function
12396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012398f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401{
12402#ifdef FEAT_CMDHIST
12403 int type;
12404 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012407 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12408 if (str == NULL)
12409 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012411 {
12412 type = get_histtype(str);
12413 if (argvars[1].v_type == VAR_UNKNOWN)
12414 idx = get_history_idx(type);
12415 else
12416 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12417 /* -1 on type error */
12418 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012422#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012423 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424}
12425
12426/*
12427 * "histnr()" function
12428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012431 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433{
12434 int i;
12435
12436#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012437 char_u *history = get_tv_string_chk(&argvars[0]);
12438
12439 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440 if (i >= HIST_CMD && i < HIST_COUNT)
12441 i = get_history_idx(i);
12442 else
12443#endif
12444 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012445 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446}
12447
12448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 * "highlightID(name)" function
12450 */
12451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012452f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012453 typval_T *argvars;
12454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012455{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457}
12458
12459/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012460 * "highlight_exists()" function
12461 */
12462 static void
12463f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012464 typval_T *argvars;
12465 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012466{
12467 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12468}
12469
12470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012471 * "hostname()" function
12472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012474f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012475 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477{
12478 char_u hostname[256];
12479
12480 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481 rettv->v_type = VAR_STRING;
12482 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483}
12484
12485/*
12486 * iconv() function
12487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012489f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012490 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012491 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012492{
12493#ifdef FEAT_MBYTE
12494 char_u buf1[NUMBUFLEN];
12495 char_u buf2[NUMBUFLEN];
12496 char_u *from, *to, *str;
12497 vimconv_T vimconv;
12498#endif
12499
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012500 rettv->v_type = VAR_STRING;
12501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502
12503#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012504 str = get_tv_string(&argvars[0]);
12505 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12506 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012507 vimconv.vc_type = CONV_NONE;
12508 convert_setup(&vimconv, from, to);
12509
12510 /* If the encodings are equal, no conversion needed. */
12511 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012512 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012514 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012515
12516 convert_setup(&vimconv, NULL, NULL);
12517 vim_free(from);
12518 vim_free(to);
12519#endif
12520}
12521
12522/*
12523 * "indent()" function
12524 */
12525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012526f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012527 typval_T *argvars;
12528 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529{
12530 linenr_T lnum;
12531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012536 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012537}
12538
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012539/*
12540 * "index()" function
12541 */
12542 static void
12543f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012544 typval_T *argvars;
12545 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012546{
Bram Moolenaar33570922005-01-25 22:26:29 +000012547 list_T *l;
12548 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012549 long idx = 0;
12550 int ic = FALSE;
12551
12552 rettv->vval.v_number = -1;
12553 if (argvars[0].v_type != VAR_LIST)
12554 {
12555 EMSG(_(e_listreq));
12556 return;
12557 }
12558 l = argvars[0].vval.v_list;
12559 if (l != NULL)
12560 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012561 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012562 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012563 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012564 int error = FALSE;
12565
Bram Moolenaar758711c2005-02-02 23:11:38 +000012566 /* Start at specified item. Use the cached index that list_find()
12567 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012568 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012569 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012570 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012571 ic = get_tv_number_chk(&argvars[3], &error);
12572 if (error)
12573 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012574 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012575
Bram Moolenaar758711c2005-02-02 23:11:38 +000012576 for ( ; item != NULL; item = item->li_next, ++idx)
12577 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012578 {
12579 rettv->vval.v_number = idx;
12580 break;
12581 }
12582 }
12583}
12584
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585static int inputsecret_flag = 0;
12586
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012587static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12588
Bram Moolenaar071d4272004-06-13 20:20:40 +000012589/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012590 * This function is used by f_input() and f_inputdialog() functions. The third
12591 * argument to f_input() specifies the type of completion to use at the
12592 * prompt. The third argument to f_inputdialog() specifies the value to return
12593 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594 */
12595 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012596get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012597 typval_T *argvars;
12598 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012599 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012600{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012601 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602 char_u *p = NULL;
12603 int c;
12604 char_u buf[NUMBUFLEN];
12605 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012606 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012607 int xp_type = EXPAND_NOTHING;
12608 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012610 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012611 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612
12613#ifdef NO_CONSOLE_INPUT
12614 /* While starting up, there is no place to enter text. */
12615 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617#endif
12618
12619 cmd_silent = FALSE; /* Want to see the prompt. */
12620 if (prompt != NULL)
12621 {
12622 /* Only the part of the message after the last NL is considered as
12623 * prompt for the command line */
12624 p = vim_strrchr(prompt, '\n');
12625 if (p == NULL)
12626 p = prompt;
12627 else
12628 {
12629 ++p;
12630 c = *p;
12631 *p = NUL;
12632 msg_start();
12633 msg_clr_eos();
12634 msg_puts_attr(prompt, echo_attr);
12635 msg_didout = FALSE;
12636 msg_starthere();
12637 *p = c;
12638 }
12639 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012641 if (argvars[1].v_type != VAR_UNKNOWN)
12642 {
12643 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12644 if (defstr != NULL)
12645 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012647 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012648 {
12649 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012650 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012651 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012652
Bram Moolenaar4463f292005-09-25 22:20:24 +000012653 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012654
Bram Moolenaar4463f292005-09-25 22:20:24 +000012655 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12656 if (xp_name == NULL)
12657 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012658
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012659 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012660
Bram Moolenaar4463f292005-09-25 22:20:24 +000012661 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12662 &xp_arg) == FAIL)
12663 return;
12664 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012665 }
12666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012667 if (defstr != NULL)
12668 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012669 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12670 xp_type, xp_arg);
12671
12672 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012674 /* since the user typed this, no need to wait for return */
12675 need_wait_return = FALSE;
12676 msg_didout = FALSE;
12677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 cmd_silent = cmd_silent_save;
12679}
12680
12681/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012682 * "input()" function
12683 * Also handles inputsecret() when inputsecret is set.
12684 */
12685 static void
12686f_input(argvars, rettv)
12687 typval_T *argvars;
12688 typval_T *rettv;
12689{
12690 get_user_input(argvars, rettv, FALSE);
12691}
12692
12693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 * "inputdialog()" function
12695 */
12696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012698 typval_T *argvars;
12699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700{
12701#if defined(FEAT_GUI_TEXTDIALOG)
12702 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12703 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12704 {
12705 char_u *message;
12706 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012707 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 message = get_tv_string_chk(&argvars[0]);
12710 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012711 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012712 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 else
12714 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012715 if (message != NULL && defstr != NULL
12716 && do_dialog(VIM_QUESTION, NULL, message,
12717 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 else
12720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012721 if (message != NULL && defstr != NULL
12722 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012723 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012724 rettv->vval.v_string = vim_strsave(
12725 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012729 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 }
12731 else
12732#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012733 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734}
12735
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012736/*
12737 * "inputlist()" function
12738 */
12739 static void
12740f_inputlist(argvars, rettv)
12741 typval_T *argvars;
12742 typval_T *rettv;
12743{
12744 listitem_T *li;
12745 int selected;
12746 int mouse_used;
12747
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012748#ifdef NO_CONSOLE_INPUT
12749 /* While starting up, there is no place to enter text. */
12750 if (no_console_input())
12751 return;
12752#endif
12753 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12754 {
12755 EMSG2(_(e_listarg), "inputlist()");
12756 return;
12757 }
12758
12759 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012760 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012761 lines_left = Rows; /* avoid more prompt */
12762 msg_scroll = TRUE;
12763 msg_clr_eos();
12764
12765 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12766 {
12767 msg_puts(get_tv_string(&li->li_tv));
12768 msg_putchar('\n');
12769 }
12770
12771 /* Ask for choice. */
12772 selected = prompt_for_number(&mouse_used);
12773 if (mouse_used)
12774 selected -= lines_left;
12775
12776 rettv->vval.v_number = selected;
12777}
12778
12779
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12781
12782/*
12783 * "inputrestore()" function
12784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012787 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789{
12790 if (ga_userinput.ga_len > 0)
12791 {
12792 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12794 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012795 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012796 }
12797 else if (p_verbose > 1)
12798 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012799 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801 }
12802}
12803
12804/*
12805 * "inputsave()" function
12806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012809 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012812 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 if (ga_grow(&ga_userinput, 1) == OK)
12814 {
12815 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12816 + ga_userinput.ga_len);
12817 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012818 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 }
12820 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012821 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012822}
12823
12824/*
12825 * "inputsecret()" function
12826 */
12827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012829 typval_T *argvars;
12830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831{
12832 ++cmdline_star;
12833 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012834 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835 --cmdline_star;
12836 --inputsecret_flag;
12837}
12838
12839/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012840 * "insert()" function
12841 */
12842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012844 typval_T *argvars;
12845 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012846{
12847 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012848 listitem_T *item;
12849 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012850 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012851
12852 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012854 else if ((l = argvars[0].vval.v_list) != NULL
12855 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012856 {
12857 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012858 before = get_tv_number_chk(&argvars[2], &error);
12859 if (error)
12860 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012861
Bram Moolenaar758711c2005-02-02 23:11:38 +000012862 if (before == l->lv_len)
12863 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012864 else
12865 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012866 item = list_find(l, before);
12867 if (item == NULL)
12868 {
12869 EMSGN(_(e_listidx), before);
12870 l = NULL;
12871 }
12872 }
12873 if (l != NULL)
12874 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012875 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012876 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012877 }
12878 }
12879}
12880
12881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 * "isdirectory()" function
12883 */
12884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012886 typval_T *argvars;
12887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890}
12891
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012892/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012893 * "islocked()" function
12894 */
12895 static void
12896f_islocked(argvars, rettv)
12897 typval_T *argvars;
12898 typval_T *rettv;
12899{
12900 lval_T lv;
12901 char_u *end;
12902 dictitem_T *di;
12903
12904 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012905 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12906 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012907 if (end != NULL && lv.ll_name != NULL)
12908 {
12909 if (*end != NUL)
12910 EMSG(_(e_trailing));
12911 else
12912 {
12913 if (lv.ll_tv == NULL)
12914 {
12915 if (check_changedtick(lv.ll_name))
12916 rettv->vval.v_number = 1; /* always locked */
12917 else
12918 {
12919 di = find_var(lv.ll_name, NULL);
12920 if (di != NULL)
12921 {
12922 /* Consider a variable locked when:
12923 * 1. the variable itself is locked
12924 * 2. the value of the variable is locked.
12925 * 3. the List or Dict value is locked.
12926 */
12927 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12928 || tv_islocked(&di->di_tv));
12929 }
12930 }
12931 }
12932 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012933 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012934 else if (lv.ll_newkey != NULL)
12935 EMSG2(_(e_dictkey), lv.ll_newkey);
12936 else if (lv.ll_list != NULL)
12937 /* List item. */
12938 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12939 else
12940 /* Dictionary item. */
12941 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12942 }
12943 }
12944
12945 clear_lval(&lv);
12946}
12947
Bram Moolenaar33570922005-01-25 22:26:29 +000012948static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012949
12950/*
12951 * Turn a dict into a list:
12952 * "what" == 0: list of keys
12953 * "what" == 1: list of values
12954 * "what" == 2: list of items
12955 */
12956 static void
12957dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012958 typval_T *argvars;
12959 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012960 int what;
12961{
Bram Moolenaar33570922005-01-25 22:26:29 +000012962 list_T *l2;
12963 dictitem_T *di;
12964 hashitem_T *hi;
12965 listitem_T *li;
12966 listitem_T *li2;
12967 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012968 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012969
Bram Moolenaar8c711452005-01-14 21:53:12 +000012970 if (argvars[0].v_type != VAR_DICT)
12971 {
12972 EMSG(_(e_dictreq));
12973 return;
12974 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012975 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012976 return;
12977
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012978 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012979 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012980
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012981 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012982 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012983 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012984 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012985 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012986 --todo;
12987 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012988
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012989 li = listitem_alloc();
12990 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012991 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012992 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012993
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012994 if (what == 0)
12995 {
12996 /* keys() */
12997 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012998 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012999 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13000 }
13001 else if (what == 1)
13002 {
13003 /* values() */
13004 copy_tv(&di->di_tv, &li->li_tv);
13005 }
13006 else
13007 {
13008 /* items() */
13009 l2 = list_alloc();
13010 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013011 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013012 li->li_tv.vval.v_list = l2;
13013 if (l2 == NULL)
13014 break;
13015 ++l2->lv_refcount;
13016
13017 li2 = listitem_alloc();
13018 if (li2 == NULL)
13019 break;
13020 list_append(l2, li2);
13021 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013022 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013023 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13024
13025 li2 = listitem_alloc();
13026 if (li2 == NULL)
13027 break;
13028 list_append(l2, li2);
13029 copy_tv(&di->di_tv, &li2->li_tv);
13030 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013031 }
13032 }
13033}
13034
13035/*
13036 * "items(dict)" function
13037 */
13038 static void
13039f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013040 typval_T *argvars;
13041 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013042{
13043 dict_list(argvars, rettv, 2);
13044}
13045
Bram Moolenaar071d4272004-06-13 20:20:40 +000013046/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013047 * "join()" function
13048 */
13049 static void
13050f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013051 typval_T *argvars;
13052 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013053{
13054 garray_T ga;
13055 char_u *sep;
13056
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013057 if (argvars[0].v_type != VAR_LIST)
13058 {
13059 EMSG(_(e_listreq));
13060 return;
13061 }
13062 if (argvars[0].vval.v_list == NULL)
13063 return;
13064 if (argvars[1].v_type == VAR_UNKNOWN)
13065 sep = (char_u *)" ";
13066 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013067 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013068
13069 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013070
13071 if (sep != NULL)
13072 {
13073 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013074 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013075 ga_append(&ga, NUL);
13076 rettv->vval.v_string = (char_u *)ga.ga_data;
13077 }
13078 else
13079 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013080}
13081
13082/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013083 * "keys()" function
13084 */
13085 static void
13086f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013087 typval_T *argvars;
13088 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013089{
13090 dict_list(argvars, rettv, 0);
13091}
13092
13093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094 * "last_buffer_nr()" function.
13095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013097f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013098 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100{
13101 int n = 0;
13102 buf_T *buf;
13103
13104 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13105 if (n < buf->b_fnum)
13106 n = buf->b_fnum;
13107
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013108 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013109}
13110
13111/*
13112 * "len()" function
13113 */
13114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013116 typval_T *argvars;
13117 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013118{
13119 switch (argvars[0].v_type)
13120 {
13121 case VAR_STRING:
13122 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013123 rettv->vval.v_number = (varnumber_T)STRLEN(
13124 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013125 break;
13126 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013127 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013128 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013129 case VAR_DICT:
13130 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13131 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013132 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013133 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013134 break;
13135 }
13136}
13137
Bram Moolenaar33570922005-01-25 22:26:29 +000013138static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013139
13140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013141libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013142 typval_T *argvars;
13143 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013144 int type;
13145{
13146#ifdef FEAT_LIBCALL
13147 char_u *string_in;
13148 char_u **string_result;
13149 int nr_result;
13150#endif
13151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013152 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013153 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013154 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013155
13156 if (check_restricted() || check_secure())
13157 return;
13158
13159#ifdef FEAT_LIBCALL
13160 /* The first two args must be strings, otherwise its meaningless */
13161 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13162 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013163 string_in = NULL;
13164 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013165 string_in = argvars[2].vval.v_string;
13166 if (type == VAR_NUMBER)
13167 string_result = NULL;
13168 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013170 if (mch_libcall(argvars[0].vval.v_string,
13171 argvars[1].vval.v_string,
13172 string_in,
13173 argvars[2].vval.v_number,
13174 string_result,
13175 &nr_result) == OK
13176 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013178 }
13179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180}
13181
13182/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013183 * "libcall()" function
13184 */
13185 static void
13186f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 typval_T *argvars;
13188 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013189{
13190 libcall_common(argvars, rettv, VAR_STRING);
13191}
13192
13193/*
13194 * "libcallnr()" function
13195 */
13196 static void
13197f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013198 typval_T *argvars;
13199 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013200{
13201 libcall_common(argvars, rettv, VAR_NUMBER);
13202}
13203
13204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 * "line(string)" function
13206 */
13207 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013209 typval_T *argvars;
13210 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211{
13212 linenr_T lnum = 0;
13213 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013214 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013216 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 if (fp != NULL)
13218 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013219 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220}
13221
13222/*
13223 * "line2byte(lnum)" function
13224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013226f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013227 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013228 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229{
13230#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013231 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232#else
13233 linenr_T lnum;
13234
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013239 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13240 if (rettv->vval.v_number >= 0)
13241 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242#endif
13243}
13244
13245/*
13246 * "lispindent(lnum)" function
13247 */
13248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013250 typval_T *argvars;
13251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252{
13253#ifdef FEAT_LISP
13254 pos_T pos;
13255 linenr_T lnum;
13256
13257 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013258 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13260 {
13261 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 curwin->w_cursor = pos;
13264 }
13265 else
13266#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013267 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013268}
13269
13270/*
13271 * "localtime()" function
13272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013274f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013275 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279}
13280
Bram Moolenaar33570922005-01-25 22:26:29 +000013281static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282
13283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013284get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013285 typval_T *argvars;
13286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 int exact;
13288{
13289 char_u *keys;
13290 char_u *which;
13291 char_u buf[NUMBUFLEN];
13292 char_u *keys_buf = NULL;
13293 char_u *rhs;
13294 int mode;
13295 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013296 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297
13298 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013299 rettv->v_type = VAR_STRING;
13300 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013302 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013303 if (*keys == NUL)
13304 return;
13305
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013306 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013308 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013309 if (argvars[2].v_type != VAR_UNKNOWN)
13310 abbr = get_tv_number(&argvars[2]);
13311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312 else
13313 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013314 if (which == NULL)
13315 return;
13316
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 mode = get_map_mode(&which, 0);
13318
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013319 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013320 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 vim_free(keys_buf);
13322 if (rhs != NULL)
13323 {
13324 ga_init(&ga);
13325 ga.ga_itemsize = 1;
13326 ga.ga_growsize = 40;
13327
13328 while (*rhs != NUL)
13329 ga_concat(&ga, str2special(&rhs, FALSE));
13330
13331 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013332 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 }
13334}
13335
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013336#ifdef FEAT_FLOAT
13337/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013338 * "log()" function
13339 */
13340 static void
13341f_log(argvars, rettv)
13342 typval_T *argvars;
13343 typval_T *rettv;
13344{
13345 float_T f;
13346
13347 rettv->v_type = VAR_FLOAT;
13348 if (get_float_arg(argvars, &f) == OK)
13349 rettv->vval.v_float = log(f);
13350 else
13351 rettv->vval.v_float = 0.0;
13352}
13353
13354/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013355 * "log10()" function
13356 */
13357 static void
13358f_log10(argvars, rettv)
13359 typval_T *argvars;
13360 typval_T *rettv;
13361{
13362 float_T f;
13363
13364 rettv->v_type = VAR_FLOAT;
13365 if (get_float_arg(argvars, &f) == OK)
13366 rettv->vval.v_float = log10(f);
13367 else
13368 rettv->vval.v_float = 0.0;
13369}
13370#endif
13371
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013373 * "map()" function
13374 */
13375 static void
13376f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013377 typval_T *argvars;
13378 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013379{
13380 filter_map(argvars, rettv, TRUE);
13381}
13382
13383/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013384 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013385 */
13386 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013387f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013388 typval_T *argvars;
13389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013391 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392}
13393
13394/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013395 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013396 */
13397 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013398f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013399 typval_T *argvars;
13400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013402 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403}
13404
Bram Moolenaar33570922005-01-25 22:26:29 +000013405static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406
13407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013408find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013409 typval_T *argvars;
13410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 int type;
13412{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013413 char_u *str = NULL;
13414 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 char_u *pat;
13416 regmatch_T regmatch;
13417 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013418 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419 char_u *save_cpo;
13420 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013421 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013422 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013423 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013424 list_T *l = NULL;
13425 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013426 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013427 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428
13429 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13430 save_cpo = p_cpo;
13431 p_cpo = (char_u *)"";
13432
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013433 rettv->vval.v_number = -1;
13434 if (type == 3)
13435 {
13436 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013437 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013438 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013439 }
13440 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013442 rettv->v_type = VAR_STRING;
13443 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013446 if (argvars[0].v_type == VAR_LIST)
13447 {
13448 if ((l = argvars[0].vval.v_list) == NULL)
13449 goto theend;
13450 li = l->lv_first;
13451 }
13452 else
13453 expr = str = get_tv_string(&argvars[0]);
13454
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13456 if (pat == NULL)
13457 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013458
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013459 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013461 int error = FALSE;
13462
13463 start = get_tv_number_chk(&argvars[2], &error);
13464 if (error)
13465 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013466 if (l != NULL)
13467 {
13468 li = list_find(l, start);
13469 if (li == NULL)
13470 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013471 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013472 }
13473 else
13474 {
13475 if (start < 0)
13476 start = 0;
13477 if (start > (long)STRLEN(str))
13478 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013479 /* When "count" argument is there ignore matches before "start",
13480 * otherwise skip part of the string. Differs when pattern is "^"
13481 * or "\<". */
13482 if (argvars[3].v_type != VAR_UNKNOWN)
13483 startcol = start;
13484 else
13485 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013486 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013487
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013488 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013489 nth = get_tv_number_chk(&argvars[3], &error);
13490 if (error)
13491 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 }
13493
13494 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13495 if (regmatch.regprog != NULL)
13496 {
13497 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013498
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013499 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013500 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013501 if (l != NULL)
13502 {
13503 if (li == NULL)
13504 {
13505 match = FALSE;
13506 break;
13507 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013508 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013509 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013510 if (str == NULL)
13511 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013512 }
13513
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013514 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013515
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013516 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013517 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013518 if (l == NULL && !match)
13519 break;
13520
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013521 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013522 if (l != NULL)
13523 {
13524 li = li->li_next;
13525 ++idx;
13526 }
13527 else
13528 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013529#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013530 startcol = (colnr_T)(regmatch.startp[0]
13531 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013532#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013533 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013534#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013535 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013536 }
13537
13538 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013539 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013540 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013541 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013542 int i;
13543
13544 /* return list with matched string and submatches */
13545 for (i = 0; i < NSUBEXP; ++i)
13546 {
13547 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013548 {
13549 if (list_append_string(rettv->vval.v_list,
13550 (char_u *)"", 0) == FAIL)
13551 break;
13552 }
13553 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013554 regmatch.startp[i],
13555 (int)(regmatch.endp[i] - regmatch.startp[i]))
13556 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013557 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013558 }
13559 }
13560 else if (type == 2)
13561 {
13562 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013563 if (l != NULL)
13564 copy_tv(&li->li_tv, rettv);
13565 else
13566 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013568 }
13569 else if (l != NULL)
13570 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571 else
13572 {
13573 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013575 (varnumber_T)(regmatch.startp[0] - str);
13576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013577 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013579 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 }
13581 }
13582 vim_free(regmatch.regprog);
13583 }
13584
13585theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013586 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 p_cpo = save_cpo;
13588}
13589
13590/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013591 * "match()" function
13592 */
13593 static void
13594f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013595 typval_T *argvars;
13596 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013597{
13598 find_some_match(argvars, rettv, 1);
13599}
13600
13601/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013602 * "matchadd()" function
13603 */
13604 static void
13605f_matchadd(argvars, rettv)
13606 typval_T *argvars;
13607 typval_T *rettv;
13608{
13609#ifdef FEAT_SEARCH_EXTRA
13610 char_u buf[NUMBUFLEN];
13611 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13612 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13613 int prio = 10; /* default priority */
13614 int id = -1;
13615 int error = FALSE;
13616
13617 rettv->vval.v_number = -1;
13618
13619 if (grp == NULL || pat == NULL)
13620 return;
13621 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013622 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013623 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013624 if (argvars[3].v_type != VAR_UNKNOWN)
13625 id = get_tv_number_chk(&argvars[3], &error);
13626 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013627 if (error == TRUE)
13628 return;
13629 if (id >= 1 && id <= 3)
13630 {
13631 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13632 return;
13633 }
13634
13635 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13636#endif
13637}
13638
13639/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013640 * "matcharg()" function
13641 */
13642 static void
13643f_matcharg(argvars, rettv)
13644 typval_T *argvars;
13645 typval_T *rettv;
13646{
13647 if (rettv_list_alloc(rettv) == OK)
13648 {
13649#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013650 int id = get_tv_number(&argvars[0]);
13651 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013652
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013653 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013654 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013655 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13656 {
13657 list_append_string(rettv->vval.v_list,
13658 syn_id2name(m->hlg_id), -1);
13659 list_append_string(rettv->vval.v_list, m->pattern, -1);
13660 }
13661 else
13662 {
13663 list_append_string(rettv->vval.v_list, NUL, -1);
13664 list_append_string(rettv->vval.v_list, NUL, -1);
13665 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013666 }
13667#endif
13668 }
13669}
13670
13671/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013672 * "matchdelete()" function
13673 */
13674 static void
13675f_matchdelete(argvars, rettv)
13676 typval_T *argvars;
13677 typval_T *rettv;
13678{
13679#ifdef FEAT_SEARCH_EXTRA
13680 rettv->vval.v_number = match_delete(curwin,
13681 (int)get_tv_number(&argvars[0]), TRUE);
13682#endif
13683}
13684
13685/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013686 * "matchend()" function
13687 */
13688 static void
13689f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *argvars;
13691 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013692{
13693 find_some_match(argvars, rettv, 0);
13694}
13695
13696/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013697 * "matchlist()" function
13698 */
13699 static void
13700f_matchlist(argvars, rettv)
13701 typval_T *argvars;
13702 typval_T *rettv;
13703{
13704 find_some_match(argvars, rettv, 3);
13705}
13706
13707/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013708 * "matchstr()" function
13709 */
13710 static void
13711f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013712 typval_T *argvars;
13713 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013714{
13715 find_some_match(argvars, rettv, 2);
13716}
13717
Bram Moolenaar33570922005-01-25 22:26:29 +000013718static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013719
13720 static void
13721max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013722 typval_T *argvars;
13723 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013724 int domax;
13725{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013726 long n = 0;
13727 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013728 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013729
13730 if (argvars[0].v_type == VAR_LIST)
13731 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013732 list_T *l;
13733 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013734
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013735 l = argvars[0].vval.v_list;
13736 if (l != NULL)
13737 {
13738 li = l->lv_first;
13739 if (li != NULL)
13740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013741 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013742 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013743 {
13744 li = li->li_next;
13745 if (li == NULL)
13746 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013747 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013748 if (domax ? i > n : i < n)
13749 n = i;
13750 }
13751 }
13752 }
13753 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013754 else if (argvars[0].v_type == VAR_DICT)
13755 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013756 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013757 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013758 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013759 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013760
13761 d = argvars[0].vval.v_dict;
13762 if (d != NULL)
13763 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013764 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013765 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013766 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013767 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013768 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013769 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013770 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013771 if (first)
13772 {
13773 n = i;
13774 first = FALSE;
13775 }
13776 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013777 n = i;
13778 }
13779 }
13780 }
13781 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013782 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013783 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013784 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013785}
13786
13787/*
13788 * "max()" function
13789 */
13790 static void
13791f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013792 typval_T *argvars;
13793 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013794{
13795 max_min(argvars, rettv, TRUE);
13796}
13797
13798/*
13799 * "min()" function
13800 */
13801 static void
13802f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013803 typval_T *argvars;
13804 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013805{
13806 max_min(argvars, rettv, FALSE);
13807}
13808
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013809static int mkdir_recurse __ARGS((char_u *dir, int prot));
13810
13811/*
13812 * Create the directory in which "dir" is located, and higher levels when
13813 * needed.
13814 */
13815 static int
13816mkdir_recurse(dir, prot)
13817 char_u *dir;
13818 int prot;
13819{
13820 char_u *p;
13821 char_u *updir;
13822 int r = FAIL;
13823
13824 /* Get end of directory name in "dir".
13825 * We're done when it's "/" or "c:/". */
13826 p = gettail_sep(dir);
13827 if (p <= get_past_head(dir))
13828 return OK;
13829
13830 /* If the directory exists we're done. Otherwise: create it.*/
13831 updir = vim_strnsave(dir, (int)(p - dir));
13832 if (updir == NULL)
13833 return FAIL;
13834 if (mch_isdir(updir))
13835 r = OK;
13836 else if (mkdir_recurse(updir, prot) == OK)
13837 r = vim_mkdir_emsg(updir, prot);
13838 vim_free(updir);
13839 return r;
13840}
13841
13842#ifdef vim_mkdir
13843/*
13844 * "mkdir()" function
13845 */
13846 static void
13847f_mkdir(argvars, rettv)
13848 typval_T *argvars;
13849 typval_T *rettv;
13850{
13851 char_u *dir;
13852 char_u buf[NUMBUFLEN];
13853 int prot = 0755;
13854
13855 rettv->vval.v_number = FAIL;
13856 if (check_restricted() || check_secure())
13857 return;
13858
13859 dir = get_tv_string_buf(&argvars[0], buf);
13860 if (argvars[1].v_type != VAR_UNKNOWN)
13861 {
13862 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013863 prot = get_tv_number_chk(&argvars[2], NULL);
13864 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013865 mkdir_recurse(dir, prot);
13866 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013867 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013868}
13869#endif
13870
Bram Moolenaar0d660222005-01-07 21:51:51 +000013871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872 * "mode()" function
13873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013875f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013876 typval_T *argvars;
13877 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013879 char_u buf[3];
13880
13881 buf[1] = NUL;
13882 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013883
13884#ifdef FEAT_VISUAL
13885 if (VIsual_active)
13886 {
13887 if (VIsual_select)
13888 buf[0] = VIsual_mode + 's' - 'v';
13889 else
13890 buf[0] = VIsual_mode;
13891 }
13892 else
13893#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013894 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13895 || State == CONFIRM)
13896 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013898 if (State == ASKMORE)
13899 buf[1] = 'm';
13900 else if (State == CONFIRM)
13901 buf[1] = '?';
13902 }
13903 else if (State == EXTERNCMD)
13904 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013905 else if (State & INSERT)
13906 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013907#ifdef FEAT_VREPLACE
13908 if (State & VREPLACE_FLAG)
13909 {
13910 buf[0] = 'R';
13911 buf[1] = 'v';
13912 }
13913 else
13914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 if (State & REPLACE_FLAG)
13916 buf[0] = 'R';
13917 else
13918 buf[0] = 'i';
13919 }
13920 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013923 if (exmode_active)
13924 buf[1] = 'v';
13925 }
13926 else if (exmode_active)
13927 {
13928 buf[0] = 'c';
13929 buf[1] = 'e';
13930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013932 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013934 if (finish_op)
13935 buf[1] = 'o';
13936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013938 /* Clear out the minor mode when the argument is not a non-zero number or
13939 * non-empty string. */
13940 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013941 buf[1] = NUL;
13942
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013943 rettv->vval.v_string = vim_strsave(buf);
13944 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945}
13946
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013947#ifdef FEAT_MZSCHEME
13948/*
13949 * "mzeval()" function
13950 */
13951 static void
13952f_mzeval(argvars, rettv)
13953 typval_T *argvars;
13954 typval_T *rettv;
13955{
13956 char_u *str;
13957 char_u buf[NUMBUFLEN];
13958
13959 str = get_tv_string_buf(&argvars[0], buf);
13960 do_mzeval(str, rettv);
13961}
13962#endif
13963
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013965 * "nextnonblank()" function
13966 */
13967 static void
13968f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013969 typval_T *argvars;
13970 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013971{
13972 linenr_T lnum;
13973
13974 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013976 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013977 {
13978 lnum = 0;
13979 break;
13980 }
13981 if (*skipwhite(ml_get(lnum)) != NUL)
13982 break;
13983 }
13984 rettv->vval.v_number = lnum;
13985}
13986
13987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013988 * "nr2char()" function
13989 */
13990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013992 typval_T *argvars;
13993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994{
13995 char_u buf[NUMBUFLEN];
13996
13997#ifdef FEAT_MBYTE
13998 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013999 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000 else
14001#endif
14002 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014003 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004 buf[1] = NUL;
14005 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014006 rettv->v_type = VAR_STRING;
14007 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014008}
14009
14010/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014011 * "pathshorten()" function
14012 */
14013 static void
14014f_pathshorten(argvars, rettv)
14015 typval_T *argvars;
14016 typval_T *rettv;
14017{
14018 char_u *p;
14019
14020 rettv->v_type = VAR_STRING;
14021 p = get_tv_string_chk(&argvars[0]);
14022 if (p == NULL)
14023 rettv->vval.v_string = NULL;
14024 else
14025 {
14026 p = vim_strsave(p);
14027 rettv->vval.v_string = p;
14028 if (p != NULL)
14029 shorten_dir(p);
14030 }
14031}
14032
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014033#ifdef FEAT_FLOAT
14034/*
14035 * "pow()" function
14036 */
14037 static void
14038f_pow(argvars, rettv)
14039 typval_T *argvars;
14040 typval_T *rettv;
14041{
14042 float_T fx, fy;
14043
14044 rettv->v_type = VAR_FLOAT;
14045 if (get_float_arg(argvars, &fx) == OK
14046 && get_float_arg(&argvars[1], &fy) == OK)
14047 rettv->vval.v_float = pow(fx, fy);
14048 else
14049 rettv->vval.v_float = 0.0;
14050}
14051#endif
14052
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014053/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014054 * "prevnonblank()" function
14055 */
14056 static void
14057f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014058 typval_T *argvars;
14059 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014060{
14061 linenr_T lnum;
14062
14063 lnum = get_tv_lnum(argvars);
14064 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14065 lnum = 0;
14066 else
14067 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14068 --lnum;
14069 rettv->vval.v_number = lnum;
14070}
14071
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014072#ifdef HAVE_STDARG_H
14073/* This dummy va_list is here because:
14074 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14075 * - locally in the function results in a "used before set" warning
14076 * - using va_start() to initialize it gives "function with fixed args" error */
14077static va_list ap;
14078#endif
14079
Bram Moolenaar8c711452005-01-14 21:53:12 +000014080/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014081 * "printf()" function
14082 */
14083 static void
14084f_printf(argvars, rettv)
14085 typval_T *argvars;
14086 typval_T *rettv;
14087{
14088 rettv->v_type = VAR_STRING;
14089 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014090#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014091 {
14092 char_u buf[NUMBUFLEN];
14093 int len;
14094 char_u *s;
14095 int saved_did_emsg = did_emsg;
14096 char *fmt;
14097
14098 /* Get the required length, allocate the buffer and do it for real. */
14099 did_emsg = FALSE;
14100 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014101 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014102 if (!did_emsg)
14103 {
14104 s = alloc(len + 1);
14105 if (s != NULL)
14106 {
14107 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014108 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014109 }
14110 }
14111 did_emsg |= saved_did_emsg;
14112 }
14113#endif
14114}
14115
14116/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014117 * "pumvisible()" function
14118 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014119 static void
14120f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014121 typval_T *argvars UNUSED;
14122 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014123{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014124#ifdef FEAT_INS_EXPAND
14125 if (pum_visible())
14126 rettv->vval.v_number = 1;
14127#endif
14128}
14129
14130/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014131 * "range()" function
14132 */
14133 static void
14134f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014135 typval_T *argvars;
14136 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014137{
14138 long start;
14139 long end;
14140 long stride = 1;
14141 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014142 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014145 if (argvars[1].v_type == VAR_UNKNOWN)
14146 {
14147 end = start - 1;
14148 start = 0;
14149 }
14150 else
14151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014153 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014155 }
14156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014157 if (error)
14158 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014159 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014160 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014161 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014162 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014163 else
14164 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014165 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014166 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014167 if (list_append_number(rettv->vval.v_list,
14168 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014169 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014170 }
14171}
14172
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014173/*
14174 * "readfile()" function
14175 */
14176 static void
14177f_readfile(argvars, rettv)
14178 typval_T *argvars;
14179 typval_T *rettv;
14180{
14181 int binary = FALSE;
14182 char_u *fname;
14183 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014184 listitem_T *li;
14185#define FREAD_SIZE 200 /* optimized for text lines */
14186 char_u buf[FREAD_SIZE];
14187 int readlen; /* size of last fread() */
14188 int buflen; /* nr of valid chars in buf[] */
14189 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14190 int tolist; /* first byte in buf[] still to be put in list */
14191 int chop; /* how many CR to chop off */
14192 char_u *prev = NULL; /* previously read bytes, if any */
14193 int prevlen = 0; /* length of "prev" if not NULL */
14194 char_u *s;
14195 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014196 long maxline = MAXLNUM;
14197 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014198
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014199 if (argvars[1].v_type != VAR_UNKNOWN)
14200 {
14201 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14202 binary = TRUE;
14203 if (argvars[2].v_type != VAR_UNKNOWN)
14204 maxline = get_tv_number(&argvars[2]);
14205 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014206
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014207 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014208 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014209
14210 /* Always open the file in binary mode, library functions have a mind of
14211 * their own about CR-LF conversion. */
14212 fname = get_tv_string(&argvars[0]);
14213 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14214 {
14215 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14216 return;
14217 }
14218
14219 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014220 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014221 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014222 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014223 buflen = filtd + readlen;
14224 tolist = 0;
14225 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14226 {
14227 if (buf[filtd] == '\n' || readlen <= 0)
14228 {
14229 /* Only when in binary mode add an empty list item when the
14230 * last line ends in a '\n'. */
14231 if (!binary && readlen == 0 && filtd == 0)
14232 break;
14233
14234 /* Found end-of-line or end-of-file: add a text line to the
14235 * list. */
14236 chop = 0;
14237 if (!binary)
14238 while (filtd - chop - 1 >= tolist
14239 && buf[filtd - chop - 1] == '\r')
14240 ++chop;
14241 len = filtd - tolist - chop;
14242 if (prev == NULL)
14243 s = vim_strnsave(buf + tolist, len);
14244 else
14245 {
14246 s = alloc((unsigned)(prevlen + len + 1));
14247 if (s != NULL)
14248 {
14249 mch_memmove(s, prev, prevlen);
14250 vim_free(prev);
14251 prev = NULL;
14252 mch_memmove(s + prevlen, buf + tolist, len);
14253 s[prevlen + len] = NUL;
14254 }
14255 }
14256 tolist = filtd + 1;
14257
14258 li = listitem_alloc();
14259 if (li == NULL)
14260 {
14261 vim_free(s);
14262 break;
14263 }
14264 li->li_tv.v_type = VAR_STRING;
14265 li->li_tv.v_lock = 0;
14266 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014267 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014268
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014269 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014270 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014271 if (readlen <= 0)
14272 break;
14273 }
14274 else if (buf[filtd] == NUL)
14275 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014276#ifdef FEAT_MBYTE
14277 else if (buf[filtd] == 0xef
14278 && enc_utf8
14279 && filtd + 2 < buflen
14280 && !binary
14281 && buf[filtd + 1] == 0xbb
14282 && buf[filtd + 2] == 0xbf)
14283 {
14284 /* remove utf-8 byte order mark */
14285 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14286 --filtd;
14287 buflen -= 3;
14288 }
14289#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014290 }
14291 if (readlen <= 0)
14292 break;
14293
14294 if (tolist == 0)
14295 {
14296 /* "buf" is full, need to move text to an allocated buffer */
14297 if (prev == NULL)
14298 {
14299 prev = vim_strnsave(buf, buflen);
14300 prevlen = buflen;
14301 }
14302 else
14303 {
14304 s = alloc((unsigned)(prevlen + buflen));
14305 if (s != NULL)
14306 {
14307 mch_memmove(s, prev, prevlen);
14308 mch_memmove(s + prevlen, buf, buflen);
14309 vim_free(prev);
14310 prev = s;
14311 prevlen += buflen;
14312 }
14313 }
14314 filtd = 0;
14315 }
14316 else
14317 {
14318 mch_memmove(buf, buf + tolist, buflen - tolist);
14319 filtd -= tolist;
14320 }
14321 }
14322
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014323 /*
14324 * For a negative line count use only the lines at the end of the file,
14325 * free the rest.
14326 */
14327 if (maxline < 0)
14328 while (cnt > -maxline)
14329 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014330 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014331 --cnt;
14332 }
14333
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014334 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014335 fclose(fd);
14336}
14337
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014338#if defined(FEAT_RELTIME)
14339static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14340
14341/*
14342 * Convert a List to proftime_T.
14343 * Return FAIL when there is something wrong.
14344 */
14345 static int
14346list2proftime(arg, tm)
14347 typval_T *arg;
14348 proftime_T *tm;
14349{
14350 long n1, n2;
14351 int error = FALSE;
14352
14353 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14354 || arg->vval.v_list->lv_len != 2)
14355 return FAIL;
14356 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14357 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14358# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014359 tm->HighPart = n1;
14360 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014361# else
14362 tm->tv_sec = n1;
14363 tm->tv_usec = n2;
14364# endif
14365 return error ? FAIL : OK;
14366}
14367#endif /* FEAT_RELTIME */
14368
14369/*
14370 * "reltime()" function
14371 */
14372 static void
14373f_reltime(argvars, rettv)
14374 typval_T *argvars;
14375 typval_T *rettv;
14376{
14377#ifdef FEAT_RELTIME
14378 proftime_T res;
14379 proftime_T start;
14380
14381 if (argvars[0].v_type == VAR_UNKNOWN)
14382 {
14383 /* No arguments: get current time. */
14384 profile_start(&res);
14385 }
14386 else if (argvars[1].v_type == VAR_UNKNOWN)
14387 {
14388 if (list2proftime(&argvars[0], &res) == FAIL)
14389 return;
14390 profile_end(&res);
14391 }
14392 else
14393 {
14394 /* Two arguments: compute the difference. */
14395 if (list2proftime(&argvars[0], &start) == FAIL
14396 || list2proftime(&argvars[1], &res) == FAIL)
14397 return;
14398 profile_sub(&res, &start);
14399 }
14400
14401 if (rettv_list_alloc(rettv) == OK)
14402 {
14403 long n1, n2;
14404
14405# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014406 n1 = res.HighPart;
14407 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014408# else
14409 n1 = res.tv_sec;
14410 n2 = res.tv_usec;
14411# endif
14412 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14413 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14414 }
14415#endif
14416}
14417
14418/*
14419 * "reltimestr()" function
14420 */
14421 static void
14422f_reltimestr(argvars, rettv)
14423 typval_T *argvars;
14424 typval_T *rettv;
14425{
14426#ifdef FEAT_RELTIME
14427 proftime_T tm;
14428#endif
14429
14430 rettv->v_type = VAR_STRING;
14431 rettv->vval.v_string = NULL;
14432#ifdef FEAT_RELTIME
14433 if (list2proftime(&argvars[0], &tm) == OK)
14434 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14435#endif
14436}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014437
Bram Moolenaar0d660222005-01-07 21:51:51 +000014438#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14439static void make_connection __ARGS((void));
14440static int check_connection __ARGS((void));
14441
14442 static void
14443make_connection()
14444{
14445 if (X_DISPLAY == NULL
14446# ifdef FEAT_GUI
14447 && !gui.in_use
14448# endif
14449 )
14450 {
14451 x_force_connect = TRUE;
14452 setup_term_clip();
14453 x_force_connect = FALSE;
14454 }
14455}
14456
14457 static int
14458check_connection()
14459{
14460 make_connection();
14461 if (X_DISPLAY == NULL)
14462 {
14463 EMSG(_("E240: No connection to Vim server"));
14464 return FAIL;
14465 }
14466 return OK;
14467}
14468#endif
14469
14470#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014471static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014472
14473 static void
14474remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014475 typval_T *argvars;
14476 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014477 int expr;
14478{
14479 char_u *server_name;
14480 char_u *keys;
14481 char_u *r = NULL;
14482 char_u buf[NUMBUFLEN];
14483# ifdef WIN32
14484 HWND w;
14485# else
14486 Window w;
14487# endif
14488
14489 if (check_restricted() || check_secure())
14490 return;
14491
14492# ifdef FEAT_X11
14493 if (check_connection() == FAIL)
14494 return;
14495# endif
14496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 server_name = get_tv_string_chk(&argvars[0]);
14498 if (server_name == NULL)
14499 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014500 keys = get_tv_string_buf(&argvars[1], buf);
14501# ifdef WIN32
14502 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14503# else
14504 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14505 < 0)
14506# endif
14507 {
14508 if (r != NULL)
14509 EMSG(r); /* sending worked but evaluation failed */
14510 else
14511 EMSG2(_("E241: Unable to send to %s"), server_name);
14512 return;
14513 }
14514
14515 rettv->vval.v_string = r;
14516
14517 if (argvars[2].v_type != VAR_UNKNOWN)
14518 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014519 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014520 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014522
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014523 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014524 v.di_tv.v_type = VAR_STRING;
14525 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 idvar = get_tv_string_chk(&argvars[2]);
14527 if (idvar != NULL)
14528 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014529 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014530 }
14531}
14532#endif
14533
14534/*
14535 * "remote_expr()" function
14536 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014537 static void
14538f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014539 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014540 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541{
14542 rettv->v_type = VAR_STRING;
14543 rettv->vval.v_string = NULL;
14544#ifdef FEAT_CLIENTSERVER
14545 remote_common(argvars, rettv, TRUE);
14546#endif
14547}
14548
14549/*
14550 * "remote_foreground()" function
14551 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014552 static void
14553f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014554 typval_T *argvars UNUSED;
14555 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014557#ifdef FEAT_CLIENTSERVER
14558# ifdef WIN32
14559 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 {
14561 char_u *server_name = get_tv_string_chk(&argvars[0]);
14562
14563 if (server_name != NULL)
14564 serverForeground(server_name);
14565 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014566# else
14567 /* Send a foreground() expression to the server. */
14568 argvars[1].v_type = VAR_STRING;
14569 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14570 argvars[2].v_type = VAR_UNKNOWN;
14571 remote_common(argvars, rettv, TRUE);
14572 vim_free(argvars[1].vval.v_string);
14573# endif
14574#endif
14575}
14576
Bram Moolenaar0d660222005-01-07 21:51:51 +000014577 static void
14578f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014579 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014580 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014581{
14582#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014584 char_u *s = NULL;
14585# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014586 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014587# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014588 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014589
14590 if (check_restricted() || check_secure())
14591 {
14592 rettv->vval.v_number = -1;
14593 return;
14594 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014595 serverid = get_tv_string_chk(&argvars[0]);
14596 if (serverid == NULL)
14597 {
14598 rettv->vval.v_number = -1;
14599 return; /* type error; errmsg already given */
14600 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014601# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014602 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014603 if (n == 0)
14604 rettv->vval.v_number = -1;
14605 else
14606 {
14607 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14608 rettv->vval.v_number = (s != NULL);
14609 }
14610# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014611 if (check_connection() == FAIL)
14612 return;
14613
14614 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014615 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014616# endif
14617
14618 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014620 char_u *retvar;
14621
Bram Moolenaar33570922005-01-25 22:26:29 +000014622 v.di_tv.v_type = VAR_STRING;
14623 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 retvar = get_tv_string_chk(&argvars[1]);
14625 if (retvar != NULL)
14626 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014627 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014628 }
14629#else
14630 rettv->vval.v_number = -1;
14631#endif
14632}
14633
Bram Moolenaar0d660222005-01-07 21:51:51 +000014634 static void
14635f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014636 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014638{
14639 char_u *r = NULL;
14640
14641#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642 char_u *serverid = get_tv_string_chk(&argvars[0]);
14643
14644 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014645 {
14646# ifdef WIN32
14647 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014648 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014650 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014651 if (n != 0)
14652 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14653 if (r == NULL)
14654# else
14655 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014657# endif
14658 EMSG(_("E277: Unable to read a server reply"));
14659 }
14660#endif
14661 rettv->v_type = VAR_STRING;
14662 rettv->vval.v_string = r;
14663}
14664
14665/*
14666 * "remote_send()" function
14667 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014668 static void
14669f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014670 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014671 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672{
14673 rettv->v_type = VAR_STRING;
14674 rettv->vval.v_string = NULL;
14675#ifdef FEAT_CLIENTSERVER
14676 remote_common(argvars, rettv, FALSE);
14677#endif
14678}
14679
14680/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014681 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014682 */
14683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014684f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014685 typval_T *argvars;
14686 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014687{
Bram Moolenaar33570922005-01-25 22:26:29 +000014688 list_T *l;
14689 listitem_T *item, *item2;
14690 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014691 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014692 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014693 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014694 dict_T *d;
14695 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014696
Bram Moolenaar8c711452005-01-14 21:53:12 +000014697 if (argvars[0].v_type == VAR_DICT)
14698 {
14699 if (argvars[2].v_type != VAR_UNKNOWN)
14700 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014701 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014702 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014704 key = get_tv_string_chk(&argvars[1]);
14705 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014707 di = dict_find(d, key, -1);
14708 if (di == NULL)
14709 EMSG2(_(e_dictkey), key);
14710 else
14711 {
14712 *rettv = di->di_tv;
14713 init_tv(&di->di_tv);
14714 dictitem_remove(d, di);
14715 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014716 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014717 }
14718 }
14719 else if (argvars[0].v_type != VAR_LIST)
14720 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014721 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014722 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014724 int error = FALSE;
14725
14726 idx = get_tv_number_chk(&argvars[1], &error);
14727 if (error)
14728 ; /* type error: do nothing, errmsg already given */
14729 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014730 EMSGN(_(e_listidx), idx);
14731 else
14732 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014733 if (argvars[2].v_type == VAR_UNKNOWN)
14734 {
14735 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014736 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014737 *rettv = item->li_tv;
14738 vim_free(item);
14739 }
14740 else
14741 {
14742 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014743 end = get_tv_number_chk(&argvars[2], &error);
14744 if (error)
14745 ; /* type error: do nothing */
14746 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014747 EMSGN(_(e_listidx), end);
14748 else
14749 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014750 int cnt = 0;
14751
14752 for (li = item; li != NULL; li = li->li_next)
14753 {
14754 ++cnt;
14755 if (li == item2)
14756 break;
14757 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014758 if (li == NULL) /* didn't find "item2" after "item" */
14759 EMSG(_(e_invrange));
14760 else
14761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014762 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014763 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014764 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014765 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014766 l->lv_first = item;
14767 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014768 item->li_prev = NULL;
14769 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014770 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014771 }
14772 }
14773 }
14774 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014775 }
14776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014777}
14778
14779/*
14780 * "rename({from}, {to})" function
14781 */
14782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014783f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014784 typval_T *argvars;
14785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786{
14787 char_u buf[NUMBUFLEN];
14788
14789 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014792 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14793 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794}
14795
14796/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014797 * "repeat()" function
14798 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014799 static void
14800f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014801 typval_T *argvars;
14802 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014803{
14804 char_u *p;
14805 int n;
14806 int slen;
14807 int len;
14808 char_u *r;
14809 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014810
14811 n = get_tv_number(&argvars[1]);
14812 if (argvars[0].v_type == VAR_LIST)
14813 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014814 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014815 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014816 if (list_extend(rettv->vval.v_list,
14817 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014818 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014819 }
14820 else
14821 {
14822 p = get_tv_string(&argvars[0]);
14823 rettv->v_type = VAR_STRING;
14824 rettv->vval.v_string = NULL;
14825
14826 slen = (int)STRLEN(p);
14827 len = slen * n;
14828 if (len <= 0)
14829 return;
14830
14831 r = alloc(len + 1);
14832 if (r != NULL)
14833 {
14834 for (i = 0; i < n; i++)
14835 mch_memmove(r + i * slen, p, (size_t)slen);
14836 r[len] = NUL;
14837 }
14838
14839 rettv->vval.v_string = r;
14840 }
14841}
14842
14843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 * "resolve()" function
14845 */
14846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014847f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014848 typval_T *argvars;
14849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850{
14851 char_u *p;
14852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854#ifdef FEAT_SHORTCUT
14855 {
14856 char_u *v = NULL;
14857
14858 v = mch_resolve_shortcut(p);
14859 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014860 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014862 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 }
14864#else
14865# ifdef HAVE_READLINK
14866 {
14867 char_u buf[MAXPATHL + 1];
14868 char_u *cpy;
14869 int len;
14870 char_u *remain = NULL;
14871 char_u *q;
14872 int is_relative_to_current = FALSE;
14873 int has_trailing_pathsep = FALSE;
14874 int limit = 100;
14875
14876 p = vim_strsave(p);
14877
14878 if (p[0] == '.' && (vim_ispathsep(p[1])
14879 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14880 is_relative_to_current = TRUE;
14881
14882 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014883 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014884 has_trailing_pathsep = TRUE;
14885
14886 q = getnextcomp(p);
14887 if (*q != NUL)
14888 {
14889 /* Separate the first path component in "p", and keep the
14890 * remainder (beginning with the path separator). */
14891 remain = vim_strsave(q - 1);
14892 q[-1] = NUL;
14893 }
14894
14895 for (;;)
14896 {
14897 for (;;)
14898 {
14899 len = readlink((char *)p, (char *)buf, MAXPATHL);
14900 if (len <= 0)
14901 break;
14902 buf[len] = NUL;
14903
14904 if (limit-- == 0)
14905 {
14906 vim_free(p);
14907 vim_free(remain);
14908 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014909 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 goto fail;
14911 }
14912
14913 /* Ensure that the result will have a trailing path separator
14914 * if the argument has one. */
14915 if (remain == NULL && has_trailing_pathsep)
14916 add_pathsep(buf);
14917
14918 /* Separate the first path component in the link value and
14919 * concatenate the remainders. */
14920 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14921 if (*q != NUL)
14922 {
14923 if (remain == NULL)
14924 remain = vim_strsave(q - 1);
14925 else
14926 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014927 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928 if (cpy != NULL)
14929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 vim_free(remain);
14931 remain = cpy;
14932 }
14933 }
14934 q[-1] = NUL;
14935 }
14936
14937 q = gettail(p);
14938 if (q > p && *q == NUL)
14939 {
14940 /* Ignore trailing path separator. */
14941 q[-1] = NUL;
14942 q = gettail(p);
14943 }
14944 if (q > p && !mch_isFullName(buf))
14945 {
14946 /* symlink is relative to directory of argument */
14947 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14948 if (cpy != NULL)
14949 {
14950 STRCPY(cpy, p);
14951 STRCPY(gettail(cpy), buf);
14952 vim_free(p);
14953 p = cpy;
14954 }
14955 }
14956 else
14957 {
14958 vim_free(p);
14959 p = vim_strsave(buf);
14960 }
14961 }
14962
14963 if (remain == NULL)
14964 break;
14965
14966 /* Append the first path component of "remain" to "p". */
14967 q = getnextcomp(remain + 1);
14968 len = q - remain - (*q != NUL);
14969 cpy = vim_strnsave(p, STRLEN(p) + len);
14970 if (cpy != NULL)
14971 {
14972 STRNCAT(cpy, remain, len);
14973 vim_free(p);
14974 p = cpy;
14975 }
14976 /* Shorten "remain". */
14977 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014978 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979 else
14980 {
14981 vim_free(remain);
14982 remain = NULL;
14983 }
14984 }
14985
14986 /* If the result is a relative path name, make it explicitly relative to
14987 * the current directory if and only if the argument had this form. */
14988 if (!vim_ispathsep(*p))
14989 {
14990 if (is_relative_to_current
14991 && *p != NUL
14992 && !(p[0] == '.'
14993 && (p[1] == NUL
14994 || vim_ispathsep(p[1])
14995 || (p[1] == '.'
14996 && (p[2] == NUL
14997 || vim_ispathsep(p[2]))))))
14998 {
14999 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015000 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 if (cpy != NULL)
15002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003 vim_free(p);
15004 p = cpy;
15005 }
15006 }
15007 else if (!is_relative_to_current)
15008 {
15009 /* Strip leading "./". */
15010 q = p;
15011 while (q[0] == '.' && vim_ispathsep(q[1]))
15012 q += 2;
15013 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015014 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015 }
15016 }
15017
15018 /* Ensure that the result will have no trailing path separator
15019 * if the argument had none. But keep "/" or "//". */
15020 if (!has_trailing_pathsep)
15021 {
15022 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015023 if (after_pathsep(p, q))
15024 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015025 }
15026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015027 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028 }
15029# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015030 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015031# endif
15032#endif
15033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035
15036#ifdef HAVE_READLINK
15037fail:
15038#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015039 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015040}
15041
15042/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015043 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 */
15045 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015047 typval_T *argvars;
15048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015049{
Bram Moolenaar33570922005-01-25 22:26:29 +000015050 list_T *l;
15051 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052
Bram Moolenaar0d660222005-01-07 21:51:51 +000015053 if (argvars[0].v_type != VAR_LIST)
15054 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015055 else if ((l = argvars[0].vval.v_list) != NULL
15056 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015057 {
15058 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015059 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015060 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015061 while (li != NULL)
15062 {
15063 ni = li->li_prev;
15064 list_append(l, li);
15065 li = ni;
15066 }
15067 rettv->vval.v_list = l;
15068 rettv->v_type = VAR_LIST;
15069 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015070 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072}
15073
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015074#define SP_NOMOVE 0x01 /* don't move cursor */
15075#define SP_REPEAT 0x02 /* repeat to find outer pair */
15076#define SP_RETCOUNT 0x04 /* return matchcount */
15077#define SP_SETPCMARK 0x08 /* set previous context mark */
15078#define SP_START 0x10 /* accept match at start position */
15079#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15080#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015081
Bram Moolenaar33570922005-01-25 22:26:29 +000015082static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083
15084/*
15085 * Get flags for a search function.
15086 * Possibly sets "p_ws".
15087 * Returns BACKWARD, FORWARD or zero (for an error).
15088 */
15089 static int
15090get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092 int *flagsp;
15093{
15094 int dir = FORWARD;
15095 char_u *flags;
15096 char_u nbuf[NUMBUFLEN];
15097 int mask;
15098
15099 if (varp->v_type != VAR_UNKNOWN)
15100 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015101 flags = get_tv_string_buf_chk(varp, nbuf);
15102 if (flags == NULL)
15103 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015104 while (*flags != NUL)
15105 {
15106 switch (*flags)
15107 {
15108 case 'b': dir = BACKWARD; break;
15109 case 'w': p_ws = TRUE; break;
15110 case 'W': p_ws = FALSE; break;
15111 default: mask = 0;
15112 if (flagsp != NULL)
15113 switch (*flags)
15114 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015115 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015116 case 'e': mask = SP_END; break;
15117 case 'm': mask = SP_RETCOUNT; break;
15118 case 'n': mask = SP_NOMOVE; break;
15119 case 'p': mask = SP_SUBPAT; break;
15120 case 'r': mask = SP_REPEAT; break;
15121 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015122 }
15123 if (mask == 0)
15124 {
15125 EMSG2(_(e_invarg2), flags);
15126 dir = 0;
15127 }
15128 else
15129 *flagsp |= mask;
15130 }
15131 if (dir == 0)
15132 break;
15133 ++flags;
15134 }
15135 }
15136 return dir;
15137}
15138
Bram Moolenaar071d4272004-06-13 20:20:40 +000015139/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015140 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015142 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015143search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015144 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015145 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015146 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015148 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 char_u *pat;
15150 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015151 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152 int save_p_ws = p_ws;
15153 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015154 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015155 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015156 proftime_T tm;
15157#ifdef FEAT_RELTIME
15158 long time_limit = 0;
15159#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015160 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015161 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015163 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015164 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015165 if (dir == 0)
15166 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015167 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015168 if (flags & SP_START)
15169 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015170 if (flags & SP_END)
15171 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015172
Bram Moolenaar76929292008-01-06 19:07:36 +000015173 /* Optional arguments: line number to stop searching and timeout. */
15174 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015175 {
15176 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15177 if (lnum_stop < 0)
15178 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015179#ifdef FEAT_RELTIME
15180 if (argvars[3].v_type != VAR_UNKNOWN)
15181 {
15182 time_limit = get_tv_number_chk(&argvars[3], NULL);
15183 if (time_limit < 0)
15184 goto theend;
15185 }
15186#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015187 }
15188
Bram Moolenaar76929292008-01-06 19:07:36 +000015189#ifdef FEAT_RELTIME
15190 /* Set the time limit, if there is one. */
15191 profile_setlimit(time_limit, &tm);
15192#endif
15193
Bram Moolenaar231334e2005-07-25 20:46:57 +000015194 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015195 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015196 * Check to make sure only those flags are set.
15197 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15198 * flags cannot be set. Check for that condition also.
15199 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015200 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015201 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015202 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015203 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015204 goto theend;
15205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015207 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015208 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015209 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015210 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015212 if (flags & SP_SUBPAT)
15213 retval = subpatnum;
15214 else
15215 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015216 if (flags & SP_SETPCMARK)
15217 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015219 if (match_pos != NULL)
15220 {
15221 /* Store the match cursor position */
15222 match_pos->lnum = pos.lnum;
15223 match_pos->col = pos.col + 1;
15224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015225 /* "/$" will put the cursor after the end of the line, may need to
15226 * correct that here */
15227 check_cursor();
15228 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015229
15230 /* If 'n' flag is used: restore cursor position. */
15231 if (flags & SP_NOMOVE)
15232 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015233 else
15234 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015235theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015237
15238 return retval;
15239}
15240
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015241#ifdef FEAT_FLOAT
15242/*
15243 * "round({float})" function
15244 */
15245 static void
15246f_round(argvars, rettv)
15247 typval_T *argvars;
15248 typval_T *rettv;
15249{
15250 float_T f;
15251
15252 rettv->v_type = VAR_FLOAT;
15253 if (get_float_arg(argvars, &f) == OK)
15254 /* round() is not in C90, use ceil() or floor() instead. */
15255 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15256 else
15257 rettv->vval.v_float = 0.0;
15258}
15259#endif
15260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015261/*
15262 * "search()" function
15263 */
15264 static void
15265f_search(argvars, rettv)
15266 typval_T *argvars;
15267 typval_T *rettv;
15268{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015269 int flags = 0;
15270
15271 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272}
15273
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015275 * "searchdecl()" function
15276 */
15277 static void
15278f_searchdecl(argvars, rettv)
15279 typval_T *argvars;
15280 typval_T *rettv;
15281{
15282 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015283 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015284 int error = FALSE;
15285 char_u *name;
15286
15287 rettv->vval.v_number = 1; /* default: FAIL */
15288
15289 name = get_tv_string_chk(&argvars[0]);
15290 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015291 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015292 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015293 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15294 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15295 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015296 if (!error && name != NULL)
15297 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015298 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015299}
15300
15301/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015302 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015304 static int
15305searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015306 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015307 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308{
15309 char_u *spat, *mpat, *epat;
15310 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 int dir;
15313 int flags = 0;
15314 char_u nbuf1[NUMBUFLEN];
15315 char_u nbuf2[NUMBUFLEN];
15316 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015317 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015318 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015319 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320
Bram Moolenaar071d4272004-06-13 20:20:40 +000015321 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015322 spat = get_tv_string_chk(&argvars[0]);
15323 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15324 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15325 if (spat == NULL || mpat == NULL || epat == NULL)
15326 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327
Bram Moolenaar071d4272004-06-13 20:20:40 +000015328 /* Handle the optional fourth argument: flags */
15329 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015330 if (dir == 0)
15331 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015332
15333 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015334 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15335 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015336 if ((flags & (SP_END | SP_SUBPAT)) != 0
15337 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015338 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015339 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015340 goto theend;
15341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015343 /* Using 'r' implies 'W', otherwise it doesn't work. */
15344 if (flags & SP_REPEAT)
15345 p_ws = FALSE;
15346
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015347 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015348 if (argvars[3].v_type == VAR_UNKNOWN
15349 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 skip = (char_u *)"";
15351 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015353 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015354 if (argvars[5].v_type != VAR_UNKNOWN)
15355 {
15356 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15357 if (lnum_stop < 0)
15358 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015359#ifdef FEAT_RELTIME
15360 if (argvars[6].v_type != VAR_UNKNOWN)
15361 {
15362 time_limit = get_tv_number_chk(&argvars[6], NULL);
15363 if (time_limit < 0)
15364 goto theend;
15365 }
15366#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015367 }
15368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015369 if (skip == NULL)
15370 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015371
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015372 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015373 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015374
15375theend:
15376 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015377
15378 return retval;
15379}
15380
15381/*
15382 * "searchpair()" function
15383 */
15384 static void
15385f_searchpair(argvars, rettv)
15386 typval_T *argvars;
15387 typval_T *rettv;
15388{
15389 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15390}
15391
15392/*
15393 * "searchpairpos()" function
15394 */
15395 static void
15396f_searchpairpos(argvars, rettv)
15397 typval_T *argvars;
15398 typval_T *rettv;
15399{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015400 pos_T match_pos;
15401 int lnum = 0;
15402 int col = 0;
15403
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015404 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015405 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015406
15407 if (searchpair_cmn(argvars, &match_pos) > 0)
15408 {
15409 lnum = match_pos.lnum;
15410 col = match_pos.col;
15411 }
15412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015413 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15414 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015415}
15416
15417/*
15418 * Search for a start/middle/end thing.
15419 * Used by searchpair(), see its documentation for the details.
15420 * Returns 0 or -1 for no match,
15421 */
15422 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015423do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15424 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015425 char_u *spat; /* start pattern */
15426 char_u *mpat; /* middle pattern */
15427 char_u *epat; /* end pattern */
15428 int dir; /* BACKWARD or FORWARD */
15429 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015430 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015431 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015432 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015433 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015434{
15435 char_u *save_cpo;
15436 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15437 long retval = 0;
15438 pos_T pos;
15439 pos_T firstpos;
15440 pos_T foundpos;
15441 pos_T save_cursor;
15442 pos_T save_pos;
15443 int n;
15444 int r;
15445 int nest = 1;
15446 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015447 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015448 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015449
15450 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15451 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015452 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015453
Bram Moolenaar76929292008-01-06 19:07:36 +000015454#ifdef FEAT_RELTIME
15455 /* Set the time limit, if there is one. */
15456 profile_setlimit(time_limit, &tm);
15457#endif
15458
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015459 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15460 * start/middle/end (pat3, for the top pair). */
15461 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15462 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15463 if (pat2 == NULL || pat3 == NULL)
15464 goto theend;
15465 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15466 if (*mpat == NUL)
15467 STRCPY(pat3, pat2);
15468 else
15469 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15470 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015471 if (flags & SP_START)
15472 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015473
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474 save_cursor = curwin->w_cursor;
15475 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015476 clearpos(&firstpos);
15477 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478 pat = pat3;
15479 for (;;)
15480 {
15481 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015482 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15484 /* didn't find it or found the first match again: FAIL */
15485 break;
15486
15487 if (firstpos.lnum == 0)
15488 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015489 if (equalpos(pos, foundpos))
15490 {
15491 /* Found the same position again. Can happen with a pattern that
15492 * has "\zs" at the end and searching backwards. Advance one
15493 * character and try again. */
15494 if (dir == BACKWARD)
15495 decl(&pos);
15496 else
15497 incl(&pos);
15498 }
15499 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015501 /* clear the start flag to avoid getting stuck here */
15502 options &= ~SEARCH_START;
15503
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504 /* If the skip pattern matches, ignore this match. */
15505 if (*skip != NUL)
15506 {
15507 save_pos = curwin->w_cursor;
15508 curwin->w_cursor = pos;
15509 r = eval_to_bool(skip, &err, NULL, FALSE);
15510 curwin->w_cursor = save_pos;
15511 if (err)
15512 {
15513 /* Evaluating {skip} caused an error, break here. */
15514 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015515 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 break;
15517 }
15518 if (r)
15519 continue;
15520 }
15521
15522 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15523 {
15524 /* Found end when searching backwards or start when searching
15525 * forward: nested pair. */
15526 ++nest;
15527 pat = pat2; /* nested, don't search for middle */
15528 }
15529 else
15530 {
15531 /* Found end when searching forward or start when searching
15532 * backward: end of (nested) pair; or found middle in outer pair. */
15533 if (--nest == 1)
15534 pat = pat3; /* outer level, search for middle */
15535 }
15536
15537 if (nest == 0)
15538 {
15539 /* Found the match: return matchcount or line number. */
15540 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015541 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015543 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015544 if (flags & SP_SETPCMARK)
15545 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 curwin->w_cursor = pos;
15547 if (!(flags & SP_REPEAT))
15548 break;
15549 nest = 1; /* search for next unmatched */
15550 }
15551 }
15552
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015553 if (match_pos != NULL)
15554 {
15555 /* Store the match cursor position */
15556 match_pos->lnum = curwin->w_cursor.lnum;
15557 match_pos->col = curwin->w_cursor.col + 1;
15558 }
15559
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015561 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 curwin->w_cursor = save_cursor;
15563
15564theend:
15565 vim_free(pat2);
15566 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015567 if (p_cpo == empty_option)
15568 p_cpo = save_cpo;
15569 else
15570 /* Darn, evaluating the {skip} expression changed the value. */
15571 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015572
15573 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574}
15575
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015576/*
15577 * "searchpos()" function
15578 */
15579 static void
15580f_searchpos(argvars, rettv)
15581 typval_T *argvars;
15582 typval_T *rettv;
15583{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015584 pos_T match_pos;
15585 int lnum = 0;
15586 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015587 int n;
15588 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015589
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015590 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015591 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015592
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015593 n = search_cmn(argvars, &match_pos, &flags);
15594 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015595 {
15596 lnum = match_pos.lnum;
15597 col = match_pos.col;
15598 }
15599
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015600 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15601 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015602 if (flags & SP_SUBPAT)
15603 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015604}
15605
15606
Bram Moolenaar0d660222005-01-07 21:51:51 +000015607 static void
15608f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015609 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015610 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015612#ifdef FEAT_CLIENTSERVER
15613 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015614 char_u *server = get_tv_string_chk(&argvars[0]);
15615 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015616
Bram Moolenaar0d660222005-01-07 21:51:51 +000015617 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015618 if (server == NULL || reply == NULL)
15619 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015620 if (check_restricted() || check_secure())
15621 return;
15622# ifdef FEAT_X11
15623 if (check_connection() == FAIL)
15624 return;
15625# endif
15626
15627 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015629 EMSG(_("E258: Unable to send to client"));
15630 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015631 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015632 rettv->vval.v_number = 0;
15633#else
15634 rettv->vval.v_number = -1;
15635#endif
15636}
15637
Bram Moolenaar0d660222005-01-07 21:51:51 +000015638 static void
15639f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015640 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015641 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015642{
15643 char_u *r = NULL;
15644
15645#ifdef FEAT_CLIENTSERVER
15646# ifdef WIN32
15647 r = serverGetVimNames();
15648# else
15649 make_connection();
15650 if (X_DISPLAY != NULL)
15651 r = serverGetVimNames(X_DISPLAY);
15652# endif
15653#endif
15654 rettv->v_type = VAR_STRING;
15655 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656}
15657
15658/*
15659 * "setbufvar()" function
15660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015662f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015663 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015664 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015665{
15666 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015669 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 char_u nbuf[NUMBUFLEN];
15671
15672 if (check_restricted() || check_secure())
15673 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015674 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15675 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015676 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677 varp = &argvars[2];
15678
15679 if (buf != NULL && varname != NULL && varp != NULL)
15680 {
15681 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683
15684 if (*varname == '&')
15685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015686 long numval;
15687 char_u *strval;
15688 int error = FALSE;
15689
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015691 numval = get_tv_number_chk(varp, &error);
15692 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015693 if (!error && strval != NULL)
15694 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015695 }
15696 else
15697 {
15698 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15699 if (bufvarname != NULL)
15700 {
15701 STRCPY(bufvarname, "b:");
15702 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015703 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 vim_free(bufvarname);
15705 }
15706 }
15707
15708 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711}
15712
15713/*
15714 * "setcmdpos()" function
15715 */
15716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015717f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015718 typval_T *argvars;
15719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015721 int pos = (int)get_tv_number(&argvars[0]) - 1;
15722
15723 if (pos >= 0)
15724 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725}
15726
15727/*
15728 * "setline()" function
15729 */
15730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015731f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015732 typval_T *argvars;
15733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734{
15735 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015736 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015737 list_T *l = NULL;
15738 listitem_T *li = NULL;
15739 long added = 0;
15740 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015741
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015742 lnum = get_tv_lnum(&argvars[0]);
15743 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015745 l = argvars[1].vval.v_list;
15746 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015748 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015749 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015750
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015751 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015752 for (;;)
15753 {
15754 if (l != NULL)
15755 {
15756 /* list argument, get next string */
15757 if (li == NULL)
15758 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015759 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015760 li = li->li_next;
15761 }
15762
15763 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015764 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015765 break;
15766 if (lnum <= curbuf->b_ml.ml_line_count)
15767 {
15768 /* existing line, replace it */
15769 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15770 {
15771 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015772 if (lnum == curwin->w_cursor.lnum)
15773 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015774 rettv->vval.v_number = 0; /* OK */
15775 }
15776 }
15777 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15778 {
15779 /* lnum is one past the last line, append the line */
15780 ++added;
15781 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15782 rettv->vval.v_number = 0; /* OK */
15783 }
15784
15785 if (l == NULL) /* only one string argument */
15786 break;
15787 ++lnum;
15788 }
15789
15790 if (added > 0)
15791 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792}
15793
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015794static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15795
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015797 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015798 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015799 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015800set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015801 win_T *wp UNUSED;
15802 typval_T *list_arg UNUSED;
15803 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015804 typval_T *rettv;
15805{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015806#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015807 char_u *act;
15808 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015809#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015810
Bram Moolenaar2641f772005-03-25 21:58:17 +000015811 rettv->vval.v_number = -1;
15812
15813#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015814 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015815 EMSG(_(e_listreq));
15816 else
15817 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015818 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015819
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015820 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015821 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015822 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015823 if (act == NULL)
15824 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015825 if (*act == 'a' || *act == 'r')
15826 action = *act;
15827 }
15828
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015829 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015830 rettv->vval.v_number = 0;
15831 }
15832#endif
15833}
15834
15835/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015836 * "setloclist()" function
15837 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015838 static void
15839f_setloclist(argvars, rettv)
15840 typval_T *argvars;
15841 typval_T *rettv;
15842{
15843 win_T *win;
15844
15845 rettv->vval.v_number = -1;
15846
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015847 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015848 if (win != NULL)
15849 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15850}
15851
15852/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015853 * "setmatches()" function
15854 */
15855 static void
15856f_setmatches(argvars, rettv)
15857 typval_T *argvars;
15858 typval_T *rettv;
15859{
15860#ifdef FEAT_SEARCH_EXTRA
15861 list_T *l;
15862 listitem_T *li;
15863 dict_T *d;
15864
15865 rettv->vval.v_number = -1;
15866 if (argvars[0].v_type != VAR_LIST)
15867 {
15868 EMSG(_(e_listreq));
15869 return;
15870 }
15871 if ((l = argvars[0].vval.v_list) != NULL)
15872 {
15873
15874 /* To some extent make sure that we are dealing with a list from
15875 * "getmatches()". */
15876 li = l->lv_first;
15877 while (li != NULL)
15878 {
15879 if (li->li_tv.v_type != VAR_DICT
15880 || (d = li->li_tv.vval.v_dict) == NULL)
15881 {
15882 EMSG(_(e_invarg));
15883 return;
15884 }
15885 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15886 && dict_find(d, (char_u *)"pattern", -1) != NULL
15887 && dict_find(d, (char_u *)"priority", -1) != NULL
15888 && dict_find(d, (char_u *)"id", -1) != NULL))
15889 {
15890 EMSG(_(e_invarg));
15891 return;
15892 }
15893 li = li->li_next;
15894 }
15895
15896 clear_matches(curwin);
15897 li = l->lv_first;
15898 while (li != NULL)
15899 {
15900 d = li->li_tv.vval.v_dict;
15901 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15902 get_dict_string(d, (char_u *)"pattern", FALSE),
15903 (int)get_dict_number(d, (char_u *)"priority"),
15904 (int)get_dict_number(d, (char_u *)"id"));
15905 li = li->li_next;
15906 }
15907 rettv->vval.v_number = 0;
15908 }
15909#endif
15910}
15911
15912/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015913 * "setpos()" function
15914 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015915 static void
15916f_setpos(argvars, rettv)
15917 typval_T *argvars;
15918 typval_T *rettv;
15919{
15920 pos_T pos;
15921 int fnum;
15922 char_u *name;
15923
Bram Moolenaar08250432008-02-13 11:42:46 +000015924 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015925 name = get_tv_string_chk(argvars);
15926 if (name != NULL)
15927 {
15928 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15929 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015930 if (--pos.col < 0)
15931 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015932 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015933 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015934 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015935 if (fnum == curbuf->b_fnum)
15936 {
15937 curwin->w_cursor = pos;
15938 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015939 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015940 }
15941 else
15942 EMSG(_(e_invarg));
15943 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015944 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15945 {
15946 /* set mark */
15947 if (setmark_pos(name[1], &pos, fnum) == OK)
15948 rettv->vval.v_number = 0;
15949 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015950 else
15951 EMSG(_(e_invarg));
15952 }
15953 }
15954}
15955
15956/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015957 * "setqflist()" function
15958 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015959 static void
15960f_setqflist(argvars, rettv)
15961 typval_T *argvars;
15962 typval_T *rettv;
15963{
15964 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15965}
15966
15967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015968 * "setreg()" function
15969 */
15970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015971f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015972 typval_T *argvars;
15973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974{
15975 int regname;
15976 char_u *strregname;
15977 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015978 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979 int append;
15980 char_u yank_type;
15981 long block_len;
15982
15983 block_len = -1;
15984 yank_type = MAUTO;
15985 append = FALSE;
15986
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015987 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015988 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015989
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015990 if (strregname == NULL)
15991 return; /* type error; errmsg already given */
15992 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015993 if (regname == 0 || regname == '@')
15994 regname = '"';
15995 else if (regname == '=')
15996 return;
15997
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016000 stropt = get_tv_string_chk(&argvars[2]);
16001 if (stropt == NULL)
16002 return; /* type error */
16003 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016004 switch (*stropt)
16005 {
16006 case 'a': case 'A': /* append */
16007 append = TRUE;
16008 break;
16009 case 'v': case 'c': /* character-wise selection */
16010 yank_type = MCHAR;
16011 break;
16012 case 'V': case 'l': /* line-wise selection */
16013 yank_type = MLINE;
16014 break;
16015#ifdef FEAT_VISUAL
16016 case 'b': case Ctrl_V: /* block-wise selection */
16017 yank_type = MBLOCK;
16018 if (VIM_ISDIGIT(stropt[1]))
16019 {
16020 ++stropt;
16021 block_len = getdigits(&stropt) - 1;
16022 --stropt;
16023 }
16024 break;
16025#endif
16026 }
16027 }
16028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016029 strval = get_tv_string_chk(&argvars[1]);
16030 if (strval != NULL)
16031 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016032 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016033 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034}
16035
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016036/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016037 * "settabvar()" function
16038 */
16039 static void
16040f_settabvar(argvars, rettv)
16041 typval_T *argvars;
16042 typval_T *rettv;
16043{
16044 tabpage_T *save_curtab;
16045 char_u *varname, *tabvarname;
16046 typval_T *varp;
16047 tabpage_T *tp;
16048
16049 rettv->vval.v_number = 0;
16050
16051 if (check_restricted() || check_secure())
16052 return;
16053
16054 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16055 varname = get_tv_string_chk(&argvars[1]);
16056 varp = &argvars[2];
16057
16058 if (tp != NULL && varname != NULL && varp != NULL)
16059 {
16060 save_curtab = curtab;
16061 goto_tabpage_tp(tp);
16062
16063 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16064 if (tabvarname != NULL)
16065 {
16066 STRCPY(tabvarname, "t:");
16067 STRCPY(tabvarname + 2, varname);
16068 set_var(tabvarname, varp, TRUE);
16069 vim_free(tabvarname);
16070 }
16071
16072 /* Restore current tabpage */
16073 if (valid_tabpage(save_curtab))
16074 goto_tabpage_tp(save_curtab);
16075 }
16076}
16077
16078/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016079 * "settabwinvar()" function
16080 */
16081 static void
16082f_settabwinvar(argvars, rettv)
16083 typval_T *argvars;
16084 typval_T *rettv;
16085{
16086 setwinvar(argvars, rettv, 1);
16087}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016088
16089/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016090 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016092 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016093f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016094 typval_T *argvars;
16095 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016096{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016097 setwinvar(argvars, rettv, 0);
16098}
16099
16100/*
16101 * "setwinvar()" and "settabwinvar()" functions
16102 */
16103 static void
16104setwinvar(argvars, rettv, off)
16105 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016106 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016107 int off;
16108{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016109 win_T *win;
16110#ifdef FEAT_WINDOWS
16111 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016112 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016113#endif
16114 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016115 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016116 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016117 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118
16119 if (check_restricted() || check_secure())
16120 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016121
16122#ifdef FEAT_WINDOWS
16123 if (off == 1)
16124 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16125 else
16126 tp = curtab;
16127#endif
16128 win = find_win_by_nr(&argvars[off], tp);
16129 varname = get_tv_string_chk(&argvars[off + 1]);
16130 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131
16132 if (win != NULL && varname != NULL && varp != NULL)
16133 {
16134#ifdef FEAT_WINDOWS
16135 /* set curwin to be our win, temporarily */
16136 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016137 save_curtab = curtab;
16138 goto_tabpage_tp(tp);
16139 if (!win_valid(win))
16140 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141 curwin = win;
16142 curbuf = curwin->w_buffer;
16143#endif
16144
16145 if (*varname == '&')
16146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016147 long numval;
16148 char_u *strval;
16149 int error = FALSE;
16150
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016152 numval = get_tv_number_chk(varp, &error);
16153 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016154 if (!error && strval != NULL)
16155 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 }
16157 else
16158 {
16159 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16160 if (winvarname != NULL)
16161 {
16162 STRCPY(winvarname, "w:");
16163 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016164 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 vim_free(winvarname);
16166 }
16167 }
16168
16169#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016170 /* Restore current tabpage and window, if still valid (autocomands can
16171 * make them invalid). */
16172 if (valid_tabpage(save_curtab))
16173 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 if (win_valid(save_curwin))
16175 {
16176 curwin = save_curwin;
16177 curbuf = curwin->w_buffer;
16178 }
16179#endif
16180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181}
16182
16183/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016184 * "shellescape({string})" function
16185 */
16186 static void
16187f_shellescape(argvars, rettv)
16188 typval_T *argvars;
16189 typval_T *rettv;
16190{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016191 rettv->vval.v_string = vim_strsave_shellescape(
16192 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016193 rettv->v_type = VAR_STRING;
16194}
16195
16196/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 */
16199 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016201 typval_T *argvars;
16202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016204 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205
Bram Moolenaar0d660222005-01-07 21:51:51 +000016206 p = get_tv_string(&argvars[0]);
16207 rettv->vval.v_string = vim_strsave(p);
16208 simplify_filename(rettv->vval.v_string); /* simplify in place */
16209 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210}
16211
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016212#ifdef FEAT_FLOAT
16213/*
16214 * "sin()" function
16215 */
16216 static void
16217f_sin(argvars, rettv)
16218 typval_T *argvars;
16219 typval_T *rettv;
16220{
16221 float_T f;
16222
16223 rettv->v_type = VAR_FLOAT;
16224 if (get_float_arg(argvars, &f) == OK)
16225 rettv->vval.v_float = sin(f);
16226 else
16227 rettv->vval.v_float = 0.0;
16228}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016229
16230/*
16231 * "sinh()" function
16232 */
16233 static void
16234f_sinh(argvars, rettv)
16235 typval_T *argvars;
16236 typval_T *rettv;
16237{
16238 float_T f;
16239
16240 rettv->v_type = VAR_FLOAT;
16241 if (get_float_arg(argvars, &f) == OK)
16242 rettv->vval.v_float = sinh(f);
16243 else
16244 rettv->vval.v_float = 0.0;
16245}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016246#endif
16247
Bram Moolenaar0d660222005-01-07 21:51:51 +000016248static int
16249#ifdef __BORLANDC__
16250 _RTLENTRYF
16251#endif
16252 item_compare __ARGS((const void *s1, const void *s2));
16253static int
16254#ifdef __BORLANDC__
16255 _RTLENTRYF
16256#endif
16257 item_compare2 __ARGS((const void *s1, const void *s2));
16258
16259static int item_compare_ic;
16260static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016261static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016262#define ITEM_COMPARE_FAIL 999
16263
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016267 static int
16268#ifdef __BORLANDC__
16269_RTLENTRYF
16270#endif
16271item_compare(s1, s2)
16272 const void *s1;
16273 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016275 char_u *p1, *p2;
16276 char_u *tofree1, *tofree2;
16277 int res;
16278 char_u numbuf1[NUMBUFLEN];
16279 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016281 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16282 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016283 if (p1 == NULL)
16284 p1 = (char_u *)"";
16285 if (p2 == NULL)
16286 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287 if (item_compare_ic)
16288 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290 res = STRCMP(p1, p2);
16291 vim_free(tofree1);
16292 vim_free(tofree2);
16293 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294}
16295
16296 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297#ifdef __BORLANDC__
16298_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300item_compare2(s1, s2)
16301 const void *s1;
16302 const void *s2;
16303{
16304 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016305 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016306 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016307 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016309 /* shortcut after failure in previous call; compare all items equal */
16310 if (item_compare_func_err)
16311 return 0;
16312
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016313 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16314 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016315 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16316 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016317
16318 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016319 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016320 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016321 clear_tv(&argv[0]);
16322 clear_tv(&argv[1]);
16323
16324 if (res == FAIL)
16325 res = ITEM_COMPARE_FAIL;
16326 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016327 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16328 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016329 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016330 clear_tv(&rettv);
16331 return res;
16332}
16333
16334/*
16335 * "sort({list})" function
16336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016338f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016339 typval_T *argvars;
16340 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341{
Bram Moolenaar33570922005-01-25 22:26:29 +000016342 list_T *l;
16343 listitem_T *li;
16344 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016345 long len;
16346 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348 if (argvars[0].v_type != VAR_LIST)
16349 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016350 else
16351 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016352 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016353 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016354 return;
16355 rettv->vval.v_list = l;
16356 rettv->v_type = VAR_LIST;
16357 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358
Bram Moolenaar0d660222005-01-07 21:51:51 +000016359 len = list_len(l);
16360 if (len <= 1)
16361 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363 item_compare_ic = FALSE;
16364 item_compare_func = NULL;
16365 if (argvars[1].v_type != VAR_UNKNOWN)
16366 {
16367 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016369 else
16370 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016371 int error = FALSE;
16372
16373 i = get_tv_number_chk(&argvars[1], &error);
16374 if (error)
16375 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376 if (i == 1)
16377 item_compare_ic = TRUE;
16378 else
16379 item_compare_func = get_tv_string(&argvars[1]);
16380 }
16381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016384 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016385 if (ptrs == NULL)
16386 return;
16387 i = 0;
16388 for (li = l->lv_first; li != NULL; li = li->li_next)
16389 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016391 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392 /* test the compare function */
16393 if (item_compare_func != NULL
16394 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16395 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016396 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016398 {
16399 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016400 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401 item_compare_func == NULL ? item_compare : item_compare2);
16402
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016403 if (!item_compare_func_err)
16404 {
16405 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016406 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016407 l->lv_len = 0;
16408 for (i = 0; i < len; ++i)
16409 list_append(l, ptrs[i]);
16410 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016411 }
16412
16413 vim_free(ptrs);
16414 }
16415}
16416
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016417/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016418 * "soundfold({word})" function
16419 */
16420 static void
16421f_soundfold(argvars, rettv)
16422 typval_T *argvars;
16423 typval_T *rettv;
16424{
16425 char_u *s;
16426
16427 rettv->v_type = VAR_STRING;
16428 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016429#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016430 rettv->vval.v_string = eval_soundfold(s);
16431#else
16432 rettv->vval.v_string = vim_strsave(s);
16433#endif
16434}
16435
16436/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016437 * "spellbadword()" function
16438 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016439 static void
16440f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016441 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016442 typval_T *rettv;
16443{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016444 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016445 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016446 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016447
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016448 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016449 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016450
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016451#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016452 if (argvars[0].v_type == VAR_UNKNOWN)
16453 {
16454 /* Find the start and length of the badly spelled word. */
16455 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16456 if (len != 0)
16457 word = ml_get_cursor();
16458 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016459 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016460 {
16461 char_u *str = get_tv_string_chk(&argvars[0]);
16462 int capcol = -1;
16463
16464 if (str != NULL)
16465 {
16466 /* Check the argument for spelling. */
16467 while (*str != NUL)
16468 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016469 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016470 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016471 {
16472 word = str;
16473 break;
16474 }
16475 str += len;
16476 }
16477 }
16478 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016479#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016480
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016481 list_append_string(rettv->vval.v_list, word, len);
16482 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016483 attr == HLF_SPB ? "bad" :
16484 attr == HLF_SPR ? "rare" :
16485 attr == HLF_SPL ? "local" :
16486 attr == HLF_SPC ? "caps" :
16487 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016488}
16489
16490/*
16491 * "spellsuggest()" function
16492 */
16493 static void
16494f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016495 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016496 typval_T *rettv;
16497{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016498#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016499 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016500 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016501 int maxcount;
16502 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016503 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016504 listitem_T *li;
16505 int need_capital = FALSE;
16506#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016507
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016508 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016509 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016510
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016511#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016512 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016513 {
16514 str = get_tv_string(&argvars[0]);
16515 if (argvars[1].v_type != VAR_UNKNOWN)
16516 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016517 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016518 if (maxcount <= 0)
16519 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016520 if (argvars[2].v_type != VAR_UNKNOWN)
16521 {
16522 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16523 if (typeerr)
16524 return;
16525 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016526 }
16527 else
16528 maxcount = 25;
16529
Bram Moolenaar4770d092006-01-12 23:22:24 +000016530 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016531
16532 for (i = 0; i < ga.ga_len; ++i)
16533 {
16534 str = ((char_u **)ga.ga_data)[i];
16535
16536 li = listitem_alloc();
16537 if (li == NULL)
16538 vim_free(str);
16539 else
16540 {
16541 li->li_tv.v_type = VAR_STRING;
16542 li->li_tv.v_lock = 0;
16543 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016544 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016545 }
16546 }
16547 ga_clear(&ga);
16548 }
16549#endif
16550}
16551
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016553f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016554 typval_T *argvars;
16555 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016556{
16557 char_u *str;
16558 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016559 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016560 regmatch_T regmatch;
16561 char_u patbuf[NUMBUFLEN];
16562 char_u *save_cpo;
16563 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016564 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016565 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016566 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016567
16568 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16569 save_cpo = p_cpo;
16570 p_cpo = (char_u *)"";
16571
16572 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016573 if (argvars[1].v_type != VAR_UNKNOWN)
16574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016575 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16576 if (pat == NULL)
16577 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016578 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016579 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016580 }
16581 if (pat == NULL || *pat == NUL)
16582 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016583
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016584 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016586 if (typeerr)
16587 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016588
Bram Moolenaar0d660222005-01-07 21:51:51 +000016589 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16590 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016591 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016592 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016593 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016594 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016595 if (*str == NUL)
16596 match = FALSE; /* empty item at the end */
16597 else
16598 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016599 if (match)
16600 end = regmatch.startp[0];
16601 else
16602 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016603 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16604 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016605 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016606 if (list_append_string(rettv->vval.v_list, str,
16607 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016608 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016609 }
16610 if (!match)
16611 break;
16612 /* Advance to just after the match. */
16613 if (regmatch.endp[0] > str)
16614 col = 0;
16615 else
16616 {
16617 /* Don't get stuck at the same match. */
16618#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016619 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016620#else
16621 col = 1;
16622#endif
16623 }
16624 str = regmatch.endp[0];
16625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626
Bram Moolenaar0d660222005-01-07 21:51:51 +000016627 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629
Bram Moolenaar0d660222005-01-07 21:51:51 +000016630 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631}
16632
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016633#ifdef FEAT_FLOAT
16634/*
16635 * "sqrt()" function
16636 */
16637 static void
16638f_sqrt(argvars, rettv)
16639 typval_T *argvars;
16640 typval_T *rettv;
16641{
16642 float_T f;
16643
16644 rettv->v_type = VAR_FLOAT;
16645 if (get_float_arg(argvars, &f) == OK)
16646 rettv->vval.v_float = sqrt(f);
16647 else
16648 rettv->vval.v_float = 0.0;
16649}
16650
16651/*
16652 * "str2float()" function
16653 */
16654 static void
16655f_str2float(argvars, rettv)
16656 typval_T *argvars;
16657 typval_T *rettv;
16658{
16659 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16660
16661 if (*p == '+')
16662 p = skipwhite(p + 1);
16663 (void)string2float(p, &rettv->vval.v_float);
16664 rettv->v_type = VAR_FLOAT;
16665}
16666#endif
16667
Bram Moolenaar2c932302006-03-18 21:42:09 +000016668/*
16669 * "str2nr()" function
16670 */
16671 static void
16672f_str2nr(argvars, rettv)
16673 typval_T *argvars;
16674 typval_T *rettv;
16675{
16676 int base = 10;
16677 char_u *p;
16678 long n;
16679
16680 if (argvars[1].v_type != VAR_UNKNOWN)
16681 {
16682 base = get_tv_number(&argvars[1]);
16683 if (base != 8 && base != 10 && base != 16)
16684 {
16685 EMSG(_(e_invarg));
16686 return;
16687 }
16688 }
16689
16690 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016691 if (*p == '+')
16692 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016693 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16694 rettv->vval.v_number = n;
16695}
16696
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697#ifdef HAVE_STRFTIME
16698/*
16699 * "strftime({format}[, {time}])" function
16700 */
16701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016702f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016703 typval_T *argvars;
16704 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705{
16706 char_u result_buf[256];
16707 struct tm *curtime;
16708 time_t seconds;
16709 char_u *p;
16710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016713 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016714 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 seconds = time(NULL);
16716 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016717 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016718 curtime = localtime(&seconds);
16719 /* MSVC returns NULL for an invalid value of seconds. */
16720 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016721 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 else
16723 {
16724# ifdef FEAT_MBYTE
16725 vimconv_T conv;
16726 char_u *enc;
16727
16728 conv.vc_type = CONV_NONE;
16729 enc = enc_locale();
16730 convert_setup(&conv, p_enc, enc);
16731 if (conv.vc_type != CONV_NONE)
16732 p = string_convert(&conv, p, NULL);
16733# endif
16734 if (p != NULL)
16735 (void)strftime((char *)result_buf, sizeof(result_buf),
16736 (char *)p, curtime);
16737 else
16738 result_buf[0] = NUL;
16739
16740# ifdef FEAT_MBYTE
16741 if (conv.vc_type != CONV_NONE)
16742 vim_free(p);
16743 convert_setup(&conv, enc, p_enc);
16744 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016745 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 else
16747# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016748 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749
16750# ifdef FEAT_MBYTE
16751 /* Release conversion descriptors */
16752 convert_setup(&conv, NULL, NULL);
16753 vim_free(enc);
16754# endif
16755 }
16756}
16757#endif
16758
16759/*
16760 * "stridx()" function
16761 */
16762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016763f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016764 typval_T *argvars;
16765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766{
16767 char_u buf[NUMBUFLEN];
16768 char_u *needle;
16769 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016770 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016772 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016774 needle = get_tv_string_chk(&argvars[1]);
16775 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016776 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016777 if (needle == NULL || haystack == NULL)
16778 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016779
Bram Moolenaar33570922005-01-25 22:26:29 +000016780 if (argvars[2].v_type != VAR_UNKNOWN)
16781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016782 int error = FALSE;
16783
16784 start_idx = get_tv_number_chk(&argvars[2], &error);
16785 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016786 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016787 if (start_idx >= 0)
16788 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016789 }
16790
16791 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16792 if (pos != NULL)
16793 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794}
16795
16796/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016797 * "string()" function
16798 */
16799 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016800f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016801 typval_T *argvars;
16802 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016803{
16804 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016805 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016807 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016808 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016809 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016810 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016811 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812}
16813
16814/*
16815 * "strlen()" function
16816 */
16817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016818f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016819 typval_T *argvars;
16820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016822 rettv->vval.v_number = (varnumber_T)(STRLEN(
16823 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824}
16825
16826/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016827 * "strchars()" function
16828 */
16829 static void
16830f_strchars(argvars, rettv)
16831 typval_T *argvars;
16832 typval_T *rettv;
16833{
16834 char_u *s = get_tv_string(&argvars[0]);
16835#ifdef FEAT_MBYTE
16836 varnumber_T len = 0;
16837
16838 while (*s != NUL)
16839 {
16840 mb_cptr2char_adv(&s);
16841 ++len;
16842 }
16843 rettv->vval.v_number = len;
16844#else
16845 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16846#endif
16847}
16848
16849/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016850 * "strdisplaywidth()" function
16851 */
16852 static void
16853f_strdisplaywidth(argvars, rettv)
16854 typval_T *argvars;
16855 typval_T *rettv;
16856{
16857 char_u *s = get_tv_string(&argvars[0]);
16858 int col = 0;
16859
16860 if (argvars[1].v_type != VAR_UNKNOWN)
16861 col = get_tv_number(&argvars[1]);
16862
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016863 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016864}
16865
16866/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016867 * "strwidth()" function
16868 */
16869 static void
16870f_strwidth(argvars, rettv)
16871 typval_T *argvars;
16872 typval_T *rettv;
16873{
16874 char_u *s = get_tv_string(&argvars[0]);
16875
16876 rettv->vval.v_number = (varnumber_T)(
16877#ifdef FEAT_MBYTE
16878 mb_string2cells(s, -1)
16879#else
16880 STRLEN(s)
16881#endif
16882 );
16883}
16884
16885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 * "strpart()" function
16887 */
16888 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016889f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 typval_T *argvars;
16891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016892{
16893 char_u *p;
16894 int n;
16895 int len;
16896 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016897 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016899 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 slen = (int)STRLEN(p);
16901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016902 n = get_tv_number_chk(&argvars[1], &error);
16903 if (error)
16904 len = 0;
16905 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016906 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 else
16908 len = slen - n; /* default len: all bytes that are available. */
16909
16910 /*
16911 * Only return the overlap between the specified part and the actual
16912 * string.
16913 */
16914 if (n < 0)
16915 {
16916 len += n;
16917 n = 0;
16918 }
16919 else if (n > slen)
16920 n = slen;
16921 if (len < 0)
16922 len = 0;
16923 else if (n + len > slen)
16924 len = slen - n;
16925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016926 rettv->v_type = VAR_STRING;
16927 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928}
16929
16930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931 * "strridx()" function
16932 */
16933 static void
16934f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016935 typval_T *argvars;
16936 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016937{
16938 char_u buf[NUMBUFLEN];
16939 char_u *needle;
16940 char_u *haystack;
16941 char_u *rest;
16942 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016943 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016945 needle = get_tv_string_chk(&argvars[1]);
16946 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016947
16948 rettv->vval.v_number = -1;
16949 if (needle == NULL || haystack == NULL)
16950 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016951
16952 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016953 if (argvars[2].v_type != VAR_UNKNOWN)
16954 {
16955 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016956 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016957 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016958 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016959 }
16960 else
16961 end_idx = haystack_len;
16962
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016964 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016966 lastmatch = haystack + end_idx;
16967 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016968 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016969 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 for (rest = haystack; *rest != '\0'; ++rest)
16971 {
16972 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016973 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 break;
16975 lastmatch = rest;
16976 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016977 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978
16979 if (lastmatch == NULL)
16980 rettv->vval.v_number = -1;
16981 else
16982 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16983}
16984
16985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986 * "strtrans()" function
16987 */
16988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016989f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016990 typval_T *argvars;
16991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016993 rettv->v_type = VAR_STRING;
16994 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995}
16996
16997/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 * "submatch()" function
16999 */
17000 static void
17001f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017002 typval_T *argvars;
17003 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004{
17005 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006 rettv->vval.v_string =
17007 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017008}
17009
17010/*
17011 * "substitute()" function
17012 */
17013 static void
17014f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017015 typval_T *argvars;
17016 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017{
17018 char_u patbuf[NUMBUFLEN];
17019 char_u subbuf[NUMBUFLEN];
17020 char_u flagsbuf[NUMBUFLEN];
17021
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017022 char_u *str = get_tv_string_chk(&argvars[0]);
17023 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17024 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17025 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17026
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017028 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17029 rettv->vval.v_string = NULL;
17030 else
17031 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032}
17033
17034/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017035 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017038f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017039 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017040 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041{
17042 int id = 0;
17043#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017044 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017045 long col;
17046 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017047 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017049 lnum = get_tv_lnum(argvars); /* -1 on type error */
17050 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17051 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017054 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017055 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056#endif
17057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017058 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017059}
17060
17061/*
17062 * "synIDattr(id, what [, mode])" function
17063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017065f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017066 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017067 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017068{
17069 char_u *p = NULL;
17070#ifdef FEAT_SYN_HL
17071 int id;
17072 char_u *what;
17073 char_u *mode;
17074 char_u modebuf[NUMBUFLEN];
17075 int modec;
17076
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017077 id = get_tv_number(&argvars[0]);
17078 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017079 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017081 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017083 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 modec = 0; /* replace invalid with current */
17085 }
17086 else
17087 {
17088#ifdef FEAT_GUI
17089 if (gui.in_use)
17090 modec = 'g';
17091 else
17092#endif
17093 if (t_colors > 1)
17094 modec = 'c';
17095 else
17096 modec = 't';
17097 }
17098
17099
17100 switch (TOLOWER_ASC(what[0]))
17101 {
17102 case 'b':
17103 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17104 p = highlight_color(id, what, modec);
17105 else /* bold */
17106 p = highlight_has_attr(id, HL_BOLD, modec);
17107 break;
17108
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017109 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 p = highlight_color(id, what, modec);
17111 break;
17112
17113 case 'i':
17114 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17115 p = highlight_has_attr(id, HL_INVERSE, modec);
17116 else /* italic */
17117 p = highlight_has_attr(id, HL_ITALIC, modec);
17118 break;
17119
17120 case 'n': /* name */
17121 p = get_highlight_name(NULL, id - 1);
17122 break;
17123
17124 case 'r': /* reverse */
17125 p = highlight_has_attr(id, HL_INVERSE, modec);
17126 break;
17127
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017128 case 's':
17129 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17130 p = highlight_color(id, what, modec);
17131 else /* standout */
17132 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133 break;
17134
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017135 case 'u':
17136 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17137 /* underline */
17138 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17139 else
17140 /* undercurl */
17141 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142 break;
17143 }
17144
17145 if (p != NULL)
17146 p = vim_strsave(p);
17147#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017148 rettv->v_type = VAR_STRING;
17149 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150}
17151
17152/*
17153 * "synIDtrans(id)" function
17154 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017156f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017157 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017158 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159{
17160 int id;
17161
17162#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017163 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017164
17165 if (id > 0)
17166 id = syn_get_final_id(id);
17167 else
17168#endif
17169 id = 0;
17170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017171 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017172}
17173
17174/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017175 * "synconcealed(lnum, col)" function
17176 */
17177 static void
17178f_synconcealed(argvars, rettv)
17179 typval_T *argvars UNUSED;
17180 typval_T *rettv;
17181{
17182#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17183 long lnum;
17184 long col;
17185 int syntax_flags = 0;
17186 int cchar;
17187 int matchid = 0;
17188 char_u str[NUMBUFLEN];
17189#endif
17190
17191 rettv->v_type = VAR_LIST;
17192 rettv->vval.v_list = NULL;
17193
17194#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17195 lnum = get_tv_lnum(argvars); /* -1 on type error */
17196 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17197
17198 vim_memset(str, NUL, sizeof(str));
17199
17200 if (rettv_list_alloc(rettv) != FAIL)
17201 {
17202 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17203 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17204 && curwin->w_p_cole > 0)
17205 {
17206 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17207 syntax_flags = get_syntax_info(&matchid);
17208
17209 /* get the conceal character */
17210 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17211 {
17212 cchar = syn_get_sub_char();
17213 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17214 cchar = lcs_conceal;
17215 if (cchar != NUL)
17216 {
17217# ifdef FEAT_MBYTE
17218 if (has_mbyte)
17219 (*mb_char2bytes)(cchar, str);
17220 else
17221# endif
17222 str[0] = cchar;
17223 }
17224 }
17225 }
17226
17227 list_append_number(rettv->vval.v_list,
17228 (syntax_flags & HL_CONCEAL) != 0);
17229 /* -1 to auto-determine strlen */
17230 list_append_string(rettv->vval.v_list, str, -1);
17231 list_append_number(rettv->vval.v_list, matchid);
17232 }
17233#endif
17234}
17235
17236/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017237 * "synstack(lnum, col)" function
17238 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017239 static void
17240f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017241 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017242 typval_T *rettv;
17243{
17244#ifdef FEAT_SYN_HL
17245 long lnum;
17246 long col;
17247 int i;
17248 int id;
17249#endif
17250
17251 rettv->v_type = VAR_LIST;
17252 rettv->vval.v_list = NULL;
17253
17254#ifdef FEAT_SYN_HL
17255 lnum = get_tv_lnum(argvars); /* -1 on type error */
17256 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17257
17258 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017259 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017260 && rettv_list_alloc(rettv) != FAIL)
17261 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017262 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017263 for (i = 0; ; ++i)
17264 {
17265 id = syn_get_stack_item(i);
17266 if (id < 0)
17267 break;
17268 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17269 break;
17270 }
17271 }
17272#endif
17273}
17274
17275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 * "system()" function
17277 */
17278 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017279f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017280 typval_T *argvars;
17281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017283 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017285 char_u *infile = NULL;
17286 char_u buf[NUMBUFLEN];
17287 int err = FALSE;
17288 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017290 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017291 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017292
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017293 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017294 {
17295 /*
17296 * Write the string to a temp file, to be used for input of the shell
17297 * command.
17298 */
17299 if ((infile = vim_tempname('i')) == NULL)
17300 {
17301 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017302 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017303 }
17304
17305 fd = mch_fopen((char *)infile, WRITEBIN);
17306 if (fd == NULL)
17307 {
17308 EMSG2(_(e_notopen), infile);
17309 goto done;
17310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017311 p = get_tv_string_buf_chk(&argvars[1], buf);
17312 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017313 {
17314 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017315 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017316 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017317 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17318 err = TRUE;
17319 if (fclose(fd) != 0)
17320 err = TRUE;
17321 if (err)
17322 {
17323 EMSG(_("E677: Error writing temp file"));
17324 goto done;
17325 }
17326 }
17327
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017328 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17329 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017330
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331#ifdef USE_CR
17332 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017333 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334 {
17335 char_u *s;
17336
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017337 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338 {
17339 if (*s == CAR)
17340 *s = NL;
17341 }
17342 }
17343#else
17344# ifdef USE_CRNL
17345 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017346 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017347 {
17348 char_u *s, *d;
17349
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017350 d = res;
17351 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 {
17353 if (s[0] == CAR && s[1] == NL)
17354 ++s;
17355 *d++ = *s;
17356 }
17357 *d = NUL;
17358 }
17359# endif
17360#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017361
17362done:
17363 if (infile != NULL)
17364 {
17365 mch_remove(infile);
17366 vim_free(infile);
17367 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017368 rettv->v_type = VAR_STRING;
17369 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017370}
17371
17372/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017373 * "tabpagebuflist()" function
17374 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017375 static void
17376f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017377 typval_T *argvars UNUSED;
17378 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017379{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017380#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017381 tabpage_T *tp;
17382 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017383
17384 if (argvars[0].v_type == VAR_UNKNOWN)
17385 wp = firstwin;
17386 else
17387 {
17388 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17389 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017390 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017391 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017392 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017393 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017394 for (; wp != NULL; wp = wp->w_next)
17395 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017396 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017397 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017398 }
17399#endif
17400}
17401
17402
17403/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017404 * "tabpagenr()" function
17405 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017406 static void
17407f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017408 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017409 typval_T *rettv;
17410{
17411 int nr = 1;
17412#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017413 char_u *arg;
17414
17415 if (argvars[0].v_type != VAR_UNKNOWN)
17416 {
17417 arg = get_tv_string_chk(&argvars[0]);
17418 nr = 0;
17419 if (arg != NULL)
17420 {
17421 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017422 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017423 else
17424 EMSG2(_(e_invexpr2), arg);
17425 }
17426 }
17427 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017428 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017429#endif
17430 rettv->vval.v_number = nr;
17431}
17432
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017433
17434#ifdef FEAT_WINDOWS
17435static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17436
17437/*
17438 * Common code for tabpagewinnr() and winnr().
17439 */
17440 static int
17441get_winnr(tp, argvar)
17442 tabpage_T *tp;
17443 typval_T *argvar;
17444{
17445 win_T *twin;
17446 int nr = 1;
17447 win_T *wp;
17448 char_u *arg;
17449
17450 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17451 if (argvar->v_type != VAR_UNKNOWN)
17452 {
17453 arg = get_tv_string_chk(argvar);
17454 if (arg == NULL)
17455 nr = 0; /* type error; errmsg already given */
17456 else if (STRCMP(arg, "$") == 0)
17457 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17458 else if (STRCMP(arg, "#") == 0)
17459 {
17460 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17461 if (twin == NULL)
17462 nr = 0;
17463 }
17464 else
17465 {
17466 EMSG2(_(e_invexpr2), arg);
17467 nr = 0;
17468 }
17469 }
17470
17471 if (nr > 0)
17472 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17473 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017474 {
17475 if (wp == NULL)
17476 {
17477 /* didn't find it in this tabpage */
17478 nr = 0;
17479 break;
17480 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017481 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017482 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017483 return nr;
17484}
17485#endif
17486
17487/*
17488 * "tabpagewinnr()" function
17489 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017490 static void
17491f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017492 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017493 typval_T *rettv;
17494{
17495 int nr = 1;
17496#ifdef FEAT_WINDOWS
17497 tabpage_T *tp;
17498
17499 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17500 if (tp == NULL)
17501 nr = 0;
17502 else
17503 nr = get_winnr(tp, &argvars[1]);
17504#endif
17505 rettv->vval.v_number = nr;
17506}
17507
17508
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017509/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017510 * "tagfiles()" function
17511 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017512 static void
17513f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017514 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017515 typval_T *rettv;
17516{
17517 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017518 tagname_T tn;
17519 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017520
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017521 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017522 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017523
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017524 for (first = TRUE; ; first = FALSE)
17525 if (get_tagfname(&tn, first, fname) == FAIL
17526 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017527 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017528 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017529}
17530
17531/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017532 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017533 */
17534 static void
17535f_taglist(argvars, rettv)
17536 typval_T *argvars;
17537 typval_T *rettv;
17538{
17539 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017540
17541 tag_pattern = get_tv_string(&argvars[0]);
17542
17543 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017544 if (*tag_pattern == NUL)
17545 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017546
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017547 if (rettv_list_alloc(rettv) == OK)
17548 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017549}
17550
17551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552 * "tempname()" function
17553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017555f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017556 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558{
17559 static int x = 'A';
17560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017561 rettv->v_type = VAR_STRING;
17562 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017563
17564 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17565 * names. Skip 'I' and 'O', they are used for shell redirection. */
17566 do
17567 {
17568 if (x == 'Z')
17569 x = '0';
17570 else if (x == '9')
17571 x = 'A';
17572 else
17573 {
17574#ifdef EBCDIC
17575 if (x == 'I')
17576 x = 'J';
17577 else if (x == 'R')
17578 x = 'S';
17579 else
17580#endif
17581 ++x;
17582 }
17583 } while (x == 'I' || x == 'O');
17584}
17585
17586/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017587 * "test(list)" function: Just checking the walls...
17588 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017589 static void
17590f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017591 typval_T *argvars UNUSED;
17592 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017593{
17594 /* Used for unit testing. Change the code below to your liking. */
17595#if 0
17596 listitem_T *li;
17597 list_T *l;
17598 char_u *bad, *good;
17599
17600 if (argvars[0].v_type != VAR_LIST)
17601 return;
17602 l = argvars[0].vval.v_list;
17603 if (l == NULL)
17604 return;
17605 li = l->lv_first;
17606 if (li == NULL)
17607 return;
17608 bad = get_tv_string(&li->li_tv);
17609 li = li->li_next;
17610 if (li == NULL)
17611 return;
17612 good = get_tv_string(&li->li_tv);
17613 rettv->vval.v_number = test_edit_score(bad, good);
17614#endif
17615}
17616
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017617#ifdef FEAT_FLOAT
17618/*
17619 * "tan()" function
17620 */
17621 static void
17622f_tan(argvars, rettv)
17623 typval_T *argvars;
17624 typval_T *rettv;
17625{
17626 float_T f;
17627
17628 rettv->v_type = VAR_FLOAT;
17629 if (get_float_arg(argvars, &f) == OK)
17630 rettv->vval.v_float = tan(f);
17631 else
17632 rettv->vval.v_float = 0.0;
17633}
17634
17635/*
17636 * "tanh()" function
17637 */
17638 static void
17639f_tanh(argvars, rettv)
17640 typval_T *argvars;
17641 typval_T *rettv;
17642{
17643 float_T f;
17644
17645 rettv->v_type = VAR_FLOAT;
17646 if (get_float_arg(argvars, &f) == OK)
17647 rettv->vval.v_float = tanh(f);
17648 else
17649 rettv->vval.v_float = 0.0;
17650}
17651#endif
17652
Bram Moolenaard52d9742005-08-21 22:20:28 +000017653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017654 * "tolower(string)" function
17655 */
17656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017657f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017658 typval_T *argvars;
17659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660{
17661 char_u *p;
17662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017663 p = vim_strsave(get_tv_string(&argvars[0]));
17664 rettv->v_type = VAR_STRING;
17665 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017666
17667 if (p != NULL)
17668 while (*p != NUL)
17669 {
17670#ifdef FEAT_MBYTE
17671 int l;
17672
17673 if (enc_utf8)
17674 {
17675 int c, lc;
17676
17677 c = utf_ptr2char(p);
17678 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017679 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 /* TODO: reallocate string when byte count changes. */
17681 if (utf_char2len(lc) == l)
17682 utf_char2bytes(lc, p);
17683 p += l;
17684 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017685 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017686 p += l; /* skip multi-byte character */
17687 else
17688#endif
17689 {
17690 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17691 ++p;
17692 }
17693 }
17694}
17695
17696/*
17697 * "toupper(string)" function
17698 */
17699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017701 typval_T *argvars;
17702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017704 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017705 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706}
17707
17708/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017709 * "tr(string, fromstr, tostr)" function
17710 */
17711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017712f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017713 typval_T *argvars;
17714 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017715{
17716 char_u *instr;
17717 char_u *fromstr;
17718 char_u *tostr;
17719 char_u *p;
17720#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017721 int inlen;
17722 int fromlen;
17723 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017724 int idx;
17725 char_u *cpstr;
17726 int cplen;
17727 int first = TRUE;
17728#endif
17729 char_u buf[NUMBUFLEN];
17730 char_u buf2[NUMBUFLEN];
17731 garray_T ga;
17732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017733 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017734 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17735 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017736
17737 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738 rettv->v_type = VAR_STRING;
17739 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017740 if (fromstr == NULL || tostr == NULL)
17741 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017742 ga_init2(&ga, (int)sizeof(char), 80);
17743
17744#ifdef FEAT_MBYTE
17745 if (!has_mbyte)
17746#endif
17747 /* not multi-byte: fromstr and tostr must be the same length */
17748 if (STRLEN(fromstr) != STRLEN(tostr))
17749 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017750#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017751error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017752#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017753 EMSG2(_(e_invarg2), fromstr);
17754 ga_clear(&ga);
17755 return;
17756 }
17757
17758 /* fromstr and tostr have to contain the same number of chars */
17759 while (*instr != NUL)
17760 {
17761#ifdef FEAT_MBYTE
17762 if (has_mbyte)
17763 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017764 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017765 cpstr = instr;
17766 cplen = inlen;
17767 idx = 0;
17768 for (p = fromstr; *p != NUL; p += fromlen)
17769 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017770 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017771 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17772 {
17773 for (p = tostr; *p != NUL; p += tolen)
17774 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017775 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017776 if (idx-- == 0)
17777 {
17778 cplen = tolen;
17779 cpstr = p;
17780 break;
17781 }
17782 }
17783 if (*p == NUL) /* tostr is shorter than fromstr */
17784 goto error;
17785 break;
17786 }
17787 ++idx;
17788 }
17789
17790 if (first && cpstr == instr)
17791 {
17792 /* Check that fromstr and tostr have the same number of
17793 * (multi-byte) characters. Done only once when a character
17794 * of instr doesn't appear in fromstr. */
17795 first = FALSE;
17796 for (p = tostr; *p != NUL; p += tolen)
17797 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017798 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017799 --idx;
17800 }
17801 if (idx != 0)
17802 goto error;
17803 }
17804
17805 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017806 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017807 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017808
17809 instr += inlen;
17810 }
17811 else
17812#endif
17813 {
17814 /* When not using multi-byte chars we can do it faster. */
17815 p = vim_strchr(fromstr, *instr);
17816 if (p != NULL)
17817 ga_append(&ga, tostr[p - fromstr]);
17818 else
17819 ga_append(&ga, *instr);
17820 ++instr;
17821 }
17822 }
17823
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017824 /* add a terminating NUL */
17825 ga_grow(&ga, 1);
17826 ga_append(&ga, NUL);
17827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017828 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017829}
17830
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017831#ifdef FEAT_FLOAT
17832/*
17833 * "trunc({float})" function
17834 */
17835 static void
17836f_trunc(argvars, rettv)
17837 typval_T *argvars;
17838 typval_T *rettv;
17839{
17840 float_T f;
17841
17842 rettv->v_type = VAR_FLOAT;
17843 if (get_float_arg(argvars, &f) == OK)
17844 /* trunc() is not in C90, use floor() or ceil() instead. */
17845 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17846 else
17847 rettv->vval.v_float = 0.0;
17848}
17849#endif
17850
Bram Moolenaar8299df92004-07-10 09:47:34 +000017851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 * "type(expr)" function
17853 */
17854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017855f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017856 typval_T *argvars;
17857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017859 int n;
17860
17861 switch (argvars[0].v_type)
17862 {
17863 case VAR_NUMBER: n = 0; break;
17864 case VAR_STRING: n = 1; break;
17865 case VAR_FUNC: n = 2; break;
17866 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017867 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017868#ifdef FEAT_FLOAT
17869 case VAR_FLOAT: n = 5; break;
17870#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017871 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17872 }
17873 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874}
17875
17876/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017877 * "undofile(name)" function
17878 */
17879 static void
17880f_undofile(argvars, rettv)
17881 typval_T *argvars;
17882 typval_T *rettv;
17883{
17884 rettv->v_type = VAR_STRING;
17885#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017886 {
17887 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17888
17889 if (ffname != NULL)
17890 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17891 vim_free(ffname);
17892 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017893#else
17894 rettv->vval.v_string = NULL;
17895#endif
17896}
17897
17898/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017899 * "undotree()" function
17900 */
17901 static void
17902f_undotree(argvars, rettv)
17903 typval_T *argvars UNUSED;
17904 typval_T *rettv;
17905{
17906 if (rettv_dict_alloc(rettv) == OK)
17907 {
17908 dict_T *dict = rettv->vval.v_dict;
17909 list_T *list;
17910
Bram Moolenaar730cde92010-06-27 05:18:54 +020017911 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017912 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017913 dict_add_nr_str(dict, "save_last",
17914 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017915 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17916 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017917 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017918
17919 list = list_alloc();
17920 if (list != NULL)
17921 {
17922 u_eval_tree(curbuf->b_u_oldhead, list);
17923 dict_add_list(dict, "entries", list);
17924 }
17925 }
17926}
17927
17928/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017929 * "values(dict)" function
17930 */
17931 static void
17932f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017933 typval_T *argvars;
17934 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017935{
17936 dict_list(argvars, rettv, 1);
17937}
17938
17939/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017940 * "virtcol(string)" function
17941 */
17942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017943f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017944 typval_T *argvars;
17945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017946{
17947 colnr_T vcol = 0;
17948 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017949 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017951 fp = var2fpos(&argvars[0], FALSE, &fnum);
17952 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17953 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 {
17955 getvvcol(curwin, fp, NULL, NULL, &vcol);
17956 ++vcol;
17957 }
17958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017959 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960}
17961
17962/*
17963 * "visualmode()" function
17964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017966f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017967 typval_T *argvars UNUSED;
17968 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969{
17970#ifdef FEAT_VISUAL
17971 char_u str[2];
17972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017973 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 str[0] = curbuf->b_visual_mode_eval;
17975 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017976 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977
17978 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017979 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981#endif
17982}
17983
17984/*
17985 * "winbufnr(nr)" function
17986 */
17987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017988f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017989 typval_T *argvars;
17990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991{
17992 win_T *wp;
17993
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017994 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017996 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017998 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999}
18000
18001/*
18002 * "wincol()" function
18003 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018005f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018006 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008{
18009 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018010 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018011}
18012
18013/*
18014 * "winheight(nr)" function
18015 */
18016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018017f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018018 typval_T *argvars;
18019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020{
18021 win_T *wp;
18022
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018023 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018025 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018027 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028}
18029
18030/*
18031 * "winline()" function
18032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018034f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018035 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018036 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037{
18038 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018039 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040}
18041
18042/*
18043 * "winnr()" function
18044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018046f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018047 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049{
18050 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018051
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018053 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018055 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056}
18057
18058/*
18059 * "winrestcmd()" function
18060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018062f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018063 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065{
18066#ifdef FEAT_WINDOWS
18067 win_T *wp;
18068 int winnr = 1;
18069 garray_T ga;
18070 char_u buf[50];
18071
18072 ga_init2(&ga, (int)sizeof(char), 70);
18073 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18074 {
18075 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18076 ga_concat(&ga, buf);
18077# ifdef FEAT_VERTSPLIT
18078 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18079 ga_concat(&ga, buf);
18080# endif
18081 ++winnr;
18082 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018083 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018085 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018087 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018089 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090}
18091
18092/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018093 * "winrestview()" function
18094 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018095 static void
18096f_winrestview(argvars, rettv)
18097 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018098 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018099{
18100 dict_T *dict;
18101
18102 if (argvars[0].v_type != VAR_DICT
18103 || (dict = argvars[0].vval.v_dict) == NULL)
18104 EMSG(_(e_invarg));
18105 else
18106 {
18107 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18108 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18109#ifdef FEAT_VIRTUALEDIT
18110 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18111#endif
18112 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018113 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018114
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018115 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018116#ifdef FEAT_DIFF
18117 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18118#endif
18119 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18120 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18121
18122 check_cursor();
18123 changed_cline_bef_curs();
18124 invalidate_botline();
18125 redraw_later(VALID);
18126
18127 if (curwin->w_topline == 0)
18128 curwin->w_topline = 1;
18129 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18130 curwin->w_topline = curbuf->b_ml.ml_line_count;
18131#ifdef FEAT_DIFF
18132 check_topfill(curwin, TRUE);
18133#endif
18134 }
18135}
18136
18137/*
18138 * "winsaveview()" function
18139 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018140 static void
18141f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018142 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018143 typval_T *rettv;
18144{
18145 dict_T *dict;
18146
Bram Moolenaara800b422010-06-27 01:15:55 +020018147 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018148 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018149 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018150
18151 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18152 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18153#ifdef FEAT_VIRTUALEDIT
18154 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18155#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018156 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018157 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18158
18159 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18160#ifdef FEAT_DIFF
18161 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18162#endif
18163 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18164 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18165}
18166
18167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168 * "winwidth(nr)" function
18169 */
18170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018171f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018172 typval_T *argvars;
18173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174{
18175 win_T *wp;
18176
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018177 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018179 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 else
18181#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018182 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018184 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185#endif
18186}
18187
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018189 * "writefile()" function
18190 */
18191 static void
18192f_writefile(argvars, rettv)
18193 typval_T *argvars;
18194 typval_T *rettv;
18195{
18196 int binary = FALSE;
18197 char_u *fname;
18198 FILE *fd;
18199 listitem_T *li;
18200 char_u *s;
18201 int ret = 0;
18202 int c;
18203
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018204 if (check_restricted() || check_secure())
18205 return;
18206
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018207 if (argvars[0].v_type != VAR_LIST)
18208 {
18209 EMSG2(_(e_listarg), "writefile()");
18210 return;
18211 }
18212 if (argvars[0].vval.v_list == NULL)
18213 return;
18214
18215 if (argvars[2].v_type != VAR_UNKNOWN
18216 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18217 binary = TRUE;
18218
18219 /* Always open the file in binary mode, library functions have a mind of
18220 * their own about CR-LF conversion. */
18221 fname = get_tv_string(&argvars[1]);
18222 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18223 {
18224 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18225 ret = -1;
18226 }
18227 else
18228 {
18229 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18230 li = li->li_next)
18231 {
18232 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18233 {
18234 if (*s == '\n')
18235 c = putc(NUL, fd);
18236 else
18237 c = putc(*s, fd);
18238 if (c == EOF)
18239 {
18240 ret = -1;
18241 break;
18242 }
18243 }
18244 if (!binary || li->li_next != NULL)
18245 if (putc('\n', fd) == EOF)
18246 {
18247 ret = -1;
18248 break;
18249 }
18250 if (ret < 0)
18251 {
18252 EMSG(_(e_write));
18253 break;
18254 }
18255 }
18256 fclose(fd);
18257 }
18258
18259 rettv->vval.v_number = ret;
18260}
18261
18262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018264 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 */
18266 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018267var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018268 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018269 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018270 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018272 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018273 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018274 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275
Bram Moolenaara5525202006-03-02 22:52:09 +000018276 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018277 if (varp->v_type == VAR_LIST)
18278 {
18279 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018280 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018281 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018282 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018283
18284 l = varp->vval.v_list;
18285 if (l == NULL)
18286 return NULL;
18287
18288 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018289 pos.lnum = list_find_nr(l, 0L, &error);
18290 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018291 return NULL; /* invalid line number */
18292
18293 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018294 pos.col = list_find_nr(l, 1L, &error);
18295 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018296 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018297 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018298
18299 /* We accept "$" for the column number: last column. */
18300 li = list_find(l, 1L);
18301 if (li != NULL && li->li_tv.v_type == VAR_STRING
18302 && li->li_tv.vval.v_string != NULL
18303 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18304 pos.col = len + 1;
18305
Bram Moolenaara5525202006-03-02 22:52:09 +000018306 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018307 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018308 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018309 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018310
Bram Moolenaara5525202006-03-02 22:52:09 +000018311#ifdef FEAT_VIRTUALEDIT
18312 /* Get the virtual offset. Defaults to zero. */
18313 pos.coladd = list_find_nr(l, 2L, &error);
18314 if (error)
18315 pos.coladd = 0;
18316#endif
18317
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018318 return &pos;
18319 }
18320
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018321 name = get_tv_string_chk(varp);
18322 if (name == NULL)
18323 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018324 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018326#ifdef FEAT_VISUAL
18327 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18328 {
18329 if (VIsual_active)
18330 return &VIsual;
18331 return &curwin->w_cursor;
18332 }
18333#endif
18334 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018335 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018336 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18338 return NULL;
18339 return pp;
18340 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018341
18342#ifdef FEAT_VIRTUALEDIT
18343 pos.coladd = 0;
18344#endif
18345
Bram Moolenaar477933c2007-07-17 14:32:23 +000018346 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018347 {
18348 pos.col = 0;
18349 if (name[1] == '0') /* "w0": first visible line */
18350 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018351 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018352 pos.lnum = curwin->w_topline;
18353 return &pos;
18354 }
18355 else if (name[1] == '$') /* "w$": last visible line */
18356 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018357 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018358 pos.lnum = curwin->w_botline - 1;
18359 return &pos;
18360 }
18361 }
18362 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018364 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 {
18366 pos.lnum = curbuf->b_ml.ml_line_count;
18367 pos.col = 0;
18368 }
18369 else
18370 {
18371 pos.lnum = curwin->w_cursor.lnum;
18372 pos.col = (colnr_T)STRLEN(ml_get_curline());
18373 }
18374 return &pos;
18375 }
18376 return NULL;
18377}
18378
18379/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018380 * Convert list in "arg" into a position and optional file number.
18381 * When "fnump" is NULL there is no file number, only 3 items.
18382 * Note that the column is passed on as-is, the caller may want to decrement
18383 * it to use 1 for the first column.
18384 * Return FAIL when conversion is not possible, doesn't check the position for
18385 * validity.
18386 */
18387 static int
18388list2fpos(arg, posp, fnump)
18389 typval_T *arg;
18390 pos_T *posp;
18391 int *fnump;
18392{
18393 list_T *l = arg->vval.v_list;
18394 long i = 0;
18395 long n;
18396
Bram Moolenaarbde35262006-07-23 20:12:24 +000018397 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18398 * when "fnump" isn't NULL and "coladd" is optional. */
18399 if (arg->v_type != VAR_LIST
18400 || l == NULL
18401 || l->lv_len < (fnump == NULL ? 2 : 3)
18402 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018403 return FAIL;
18404
18405 if (fnump != NULL)
18406 {
18407 n = list_find_nr(l, i++, NULL); /* fnum */
18408 if (n < 0)
18409 return FAIL;
18410 if (n == 0)
18411 n = curbuf->b_fnum; /* current buffer */
18412 *fnump = n;
18413 }
18414
18415 n = list_find_nr(l, i++, NULL); /* lnum */
18416 if (n < 0)
18417 return FAIL;
18418 posp->lnum = n;
18419
18420 n = list_find_nr(l, i++, NULL); /* col */
18421 if (n < 0)
18422 return FAIL;
18423 posp->col = n;
18424
18425#ifdef FEAT_VIRTUALEDIT
18426 n = list_find_nr(l, i, NULL);
18427 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018428 posp->coladd = 0;
18429 else
18430 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018431#endif
18432
18433 return OK;
18434}
18435
18436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018437 * Get the length of an environment variable name.
18438 * Advance "arg" to the first character after the name.
18439 * Return 0 for error.
18440 */
18441 static int
18442get_env_len(arg)
18443 char_u **arg;
18444{
18445 char_u *p;
18446 int len;
18447
18448 for (p = *arg; vim_isIDc(*p); ++p)
18449 ;
18450 if (p == *arg) /* no name found */
18451 return 0;
18452
18453 len = (int)(p - *arg);
18454 *arg = p;
18455 return len;
18456}
18457
18458/*
18459 * Get the length of the name of a function or internal variable.
18460 * "arg" is advanced to the first non-white character after the name.
18461 * Return 0 if something is wrong.
18462 */
18463 static int
18464get_id_len(arg)
18465 char_u **arg;
18466{
18467 char_u *p;
18468 int len;
18469
18470 /* Find the end of the name. */
18471 for (p = *arg; eval_isnamec(*p); ++p)
18472 ;
18473 if (p == *arg) /* no name found */
18474 return 0;
18475
18476 len = (int)(p - *arg);
18477 *arg = skipwhite(p);
18478
18479 return len;
18480}
18481
18482/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018483 * Get the length of the name of a variable or function.
18484 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018486 * Return -1 if curly braces expansion failed.
18487 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488 * If the name contains 'magic' {}'s, expand them and return the
18489 * expanded name in an allocated string via 'alias' - caller must free.
18490 */
18491 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018492get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 char_u **arg;
18494 char_u **alias;
18495 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018496 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497{
18498 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499 char_u *p;
18500 char_u *expr_start;
18501 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018502
18503 *alias = NULL; /* default to no alias */
18504
18505 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18506 && (*arg)[2] == (int)KE_SNR)
18507 {
18508 /* hard coded <SNR>, already translated */
18509 *arg += 3;
18510 return get_id_len(arg) + 3;
18511 }
18512 len = eval_fname_script(*arg);
18513 if (len > 0)
18514 {
18515 /* literal "<SID>", "s:" or "<SNR>" */
18516 *arg += len;
18517 }
18518
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018520 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018522 p = find_name_end(*arg, &expr_start, &expr_end,
18523 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018524 if (expr_start != NULL)
18525 {
18526 char_u *temp_string;
18527
18528 if (!evaluate)
18529 {
18530 len += (int)(p - *arg);
18531 *arg = skipwhite(p);
18532 return len;
18533 }
18534
18535 /*
18536 * Include any <SID> etc in the expanded string:
18537 * Thus the -len here.
18538 */
18539 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18540 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018541 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 *alias = temp_string;
18543 *arg = skipwhite(p);
18544 return (int)STRLEN(temp_string);
18545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546
18547 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018548 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018549 EMSG2(_(e_invexpr2), *arg);
18550
18551 return len;
18552}
18553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018554/*
18555 * Find the end of a variable or function name, taking care of magic braces.
18556 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18557 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018558 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018559 * Return a pointer to just after the name. Equal to "arg" if there is no
18560 * valid name.
18561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018563find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018564 char_u *arg;
18565 char_u **expr_start;
18566 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018567 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018569 int mb_nest = 0;
18570 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018571 char_u *p;
18572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018573 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575 *expr_start = NULL;
18576 *expr_end = NULL;
18577 }
18578
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018579 /* Quick check for valid starting character. */
18580 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18581 return arg;
18582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583 for (p = arg; *p != NUL
18584 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018585 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018586 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018587 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018588 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018589 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018590 if (*p == '\'')
18591 {
18592 /* skip over 'string' to avoid counting [ and ] inside it. */
18593 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18594 ;
18595 if (*p == NUL)
18596 break;
18597 }
18598 else if (*p == '"')
18599 {
18600 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18601 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18602 if (*p == '\\' && p[1] != NUL)
18603 ++p;
18604 if (*p == NUL)
18605 break;
18606 }
18607
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018608 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 if (*p == '[')
18611 ++br_nest;
18612 else if (*p == ']')
18613 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018617 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018618 if (*p == '{')
18619 {
18620 mb_nest++;
18621 if (expr_start != NULL && *expr_start == NULL)
18622 *expr_start = p;
18623 }
18624 else if (*p == '}')
18625 {
18626 mb_nest--;
18627 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18628 *expr_end = p;
18629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 }
18632
18633 return p;
18634}
18635
18636/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018637 * Expands out the 'magic' {}'s in a variable/function name.
18638 * Note that this can call itself recursively, to deal with
18639 * constructs like foo{bar}{baz}{bam}
18640 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18641 * "in_start" ^
18642 * "expr_start" ^
18643 * "expr_end" ^
18644 * "in_end" ^
18645 *
18646 * Returns a new allocated string, which the caller must free.
18647 * Returns NULL for failure.
18648 */
18649 static char_u *
18650make_expanded_name(in_start, expr_start, expr_end, in_end)
18651 char_u *in_start;
18652 char_u *expr_start;
18653 char_u *expr_end;
18654 char_u *in_end;
18655{
18656 char_u c1;
18657 char_u *retval = NULL;
18658 char_u *temp_result;
18659 char_u *nextcmd = NULL;
18660
18661 if (expr_end == NULL || in_end == NULL)
18662 return NULL;
18663 *expr_start = NUL;
18664 *expr_end = NUL;
18665 c1 = *in_end;
18666 *in_end = NUL;
18667
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018668 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018669 if (temp_result != NULL && nextcmd == NULL)
18670 {
18671 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18672 + (in_end - expr_end) + 1));
18673 if (retval != NULL)
18674 {
18675 STRCPY(retval, in_start);
18676 STRCAT(retval, temp_result);
18677 STRCAT(retval, expr_end + 1);
18678 }
18679 }
18680 vim_free(temp_result);
18681
18682 *in_end = c1; /* put char back for error messages */
18683 *expr_start = '{';
18684 *expr_end = '}';
18685
18686 if (retval != NULL)
18687 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018688 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018689 if (expr_start != NULL)
18690 {
18691 /* Further expansion! */
18692 temp_result = make_expanded_name(retval, expr_start,
18693 expr_end, temp_result);
18694 vim_free(retval);
18695 retval = temp_result;
18696 }
18697 }
18698
18699 return retval;
18700}
18701
18702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018704 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 */
18706 static int
18707eval_isnamec(c)
18708 int c;
18709{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018710 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18711}
18712
18713/*
18714 * Return TRUE if character "c" can be used as the first character in a
18715 * variable or function name (excluding '{' and '}').
18716 */
18717 static int
18718eval_isnamec1(c)
18719 int c;
18720{
18721 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722}
18723
18724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 * Set number v: variable to "val".
18726 */
18727 void
18728set_vim_var_nr(idx, val)
18729 int idx;
18730 long val;
18731{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018732 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733}
18734
18735/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018736 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 */
18738 long
18739get_vim_var_nr(idx)
18740 int idx;
18741{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018742 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743}
18744
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018745/*
18746 * Get string v: variable value. Uses a static buffer, can only be used once.
18747 */
18748 char_u *
18749get_vim_var_str(idx)
18750 int idx;
18751{
18752 return get_tv_string(&vimvars[idx].vv_tv);
18753}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018754
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018756 * Get List v: variable value. Caller must take care of reference count when
18757 * needed.
18758 */
18759 list_T *
18760get_vim_var_list(idx)
18761 int idx;
18762{
18763 return vimvars[idx].vv_list;
18764}
18765
18766/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018767 * Set v:char to character "c".
18768 */
18769 void
18770set_vim_var_char(c)
18771 int c;
18772{
18773#ifdef FEAT_MBYTE
18774 char_u buf[MB_MAXBYTES];
18775#else
18776 char_u buf[2];
18777#endif
18778
18779#ifdef FEAT_MBYTE
18780 if (has_mbyte)
18781 buf[(*mb_char2bytes)(c, buf)] = NUL;
18782 else
18783#endif
18784 {
18785 buf[0] = c;
18786 buf[1] = NUL;
18787 }
18788 set_vim_var_string(VV_CHAR, buf, -1);
18789}
18790
18791/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018792 * Set v:count to "count" and v:count1 to "count1".
18793 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794 */
18795 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018796set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 long count;
18798 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018799 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018801 if (set_prevcount)
18802 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018803 vimvars[VV_COUNT].vv_nr = count;
18804 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805}
18806
18807/*
18808 * Set string v: variable to a copy of "val".
18809 */
18810 void
18811set_vim_var_string(idx, val, len)
18812 int idx;
18813 char_u *val;
18814 int len; /* length of "val" to use or -1 (whole string) */
18815{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018816 /* Need to do this (at least) once, since we can't initialize a union.
18817 * Will always be invoked when "v:progname" is set. */
18818 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18819
Bram Moolenaare9a41262005-01-15 22:18:47 +000018820 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018822 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018824 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018826 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018827}
18828
18829/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018830 * Set List v: variable to "val".
18831 */
18832 void
18833set_vim_var_list(idx, val)
18834 int idx;
18835 list_T *val;
18836{
18837 list_unref(vimvars[idx].vv_list);
18838 vimvars[idx].vv_list = val;
18839 if (val != NULL)
18840 ++val->lv_refcount;
18841}
18842
18843/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844 * Set v:register if needed.
18845 */
18846 void
18847set_reg_var(c)
18848 int c;
18849{
18850 char_u regname;
18851
18852 if (c == 0 || c == ' ')
18853 regname = '"';
18854 else
18855 regname = c;
18856 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018857 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018858 set_vim_var_string(VV_REG, &regname, 1);
18859}
18860
18861/*
18862 * Get or set v:exception. If "oldval" == NULL, return the current value.
18863 * Otherwise, restore the value to "oldval" and return NULL.
18864 * Must always be called in pairs to save and restore v:exception! Does not
18865 * take care of memory allocations.
18866 */
18867 char_u *
18868v_exception(oldval)
18869 char_u *oldval;
18870{
18871 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018872 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018873
Bram Moolenaare9a41262005-01-15 22:18:47 +000018874 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875 return NULL;
18876}
18877
18878/*
18879 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18880 * Otherwise, restore the value to "oldval" and return NULL.
18881 * Must always be called in pairs to save and restore v:throwpoint! Does not
18882 * take care of memory allocations.
18883 */
18884 char_u *
18885v_throwpoint(oldval)
18886 char_u *oldval;
18887{
18888 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018889 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890
Bram Moolenaare9a41262005-01-15 22:18:47 +000018891 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 return NULL;
18893}
18894
18895#if defined(FEAT_AUTOCMD) || defined(PROTO)
18896/*
18897 * Set v:cmdarg.
18898 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18899 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18900 * Must always be called in pairs!
18901 */
18902 char_u *
18903set_cmdarg(eap, oldarg)
18904 exarg_T *eap;
18905 char_u *oldarg;
18906{
18907 char_u *oldval;
18908 char_u *newval;
18909 unsigned len;
18910
Bram Moolenaare9a41262005-01-15 22:18:47 +000018911 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018912 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018914 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018915 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018916 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917 }
18918
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018919 if (eap->force_bin == FORCE_BIN)
18920 len = 6;
18921 else if (eap->force_bin == FORCE_NOBIN)
18922 len = 8;
18923 else
18924 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018925
18926 if (eap->read_edit)
18927 len += 7;
18928
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018929 if (eap->force_ff != 0)
18930 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18931# ifdef FEAT_MBYTE
18932 if (eap->force_enc != 0)
18933 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018934 if (eap->bad_char != 0)
18935 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018936# endif
18937
18938 newval = alloc(len + 1);
18939 if (newval == NULL)
18940 return NULL;
18941
18942 if (eap->force_bin == FORCE_BIN)
18943 sprintf((char *)newval, " ++bin");
18944 else if (eap->force_bin == FORCE_NOBIN)
18945 sprintf((char *)newval, " ++nobin");
18946 else
18947 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018948
18949 if (eap->read_edit)
18950 STRCAT(newval, " ++edit");
18951
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018952 if (eap->force_ff != 0)
18953 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18954 eap->cmd + eap->force_ff);
18955# ifdef FEAT_MBYTE
18956 if (eap->force_enc != 0)
18957 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18958 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018959 if (eap->bad_char == BAD_KEEP)
18960 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18961 else if (eap->bad_char == BAD_DROP)
18962 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18963 else if (eap->bad_char != 0)
18964 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018965# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018966 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018967 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968}
18969#endif
18970
18971/*
18972 * Get the value of internal variable "name".
18973 * Return OK or FAIL.
18974 */
18975 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018976get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018977 char_u *name;
18978 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018979 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018980 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981{
18982 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018983 typval_T *tv = NULL;
18984 typval_T atv;
18985 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018987
18988 /* truncate the name, so that we can use strcmp() */
18989 cc = name[len];
18990 name[len] = NUL;
18991
18992 /*
18993 * Check for "b:changedtick".
18994 */
18995 if (STRCMP(name, "b:changedtick") == 0)
18996 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018997 atv.v_type = VAR_NUMBER;
18998 atv.vval.v_number = curbuf->b_changedtick;
18999 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000 }
19001
19002 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 * Check for user-defined variables.
19004 */
19005 else
19006 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019007 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019008 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019009 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019010 }
19011
Bram Moolenaare9a41262005-01-15 22:18:47 +000019012 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019014 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019015 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019016 ret = FAIL;
19017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019018 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019019 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020
19021 name[len] = cc;
19022
19023 return ret;
19024}
19025
19026/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019027 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19028 * Also handle function call with Funcref variable: func(expr)
19029 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19030 */
19031 static int
19032handle_subscript(arg, rettv, evaluate, verbose)
19033 char_u **arg;
19034 typval_T *rettv;
19035 int evaluate; /* do more than finding the end */
19036 int verbose; /* give error messages */
19037{
19038 int ret = OK;
19039 dict_T *selfdict = NULL;
19040 char_u *s;
19041 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019042 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019043
19044 while (ret == OK
19045 && (**arg == '['
19046 || (**arg == '.' && rettv->v_type == VAR_DICT)
19047 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19048 && !vim_iswhite(*(*arg - 1)))
19049 {
19050 if (**arg == '(')
19051 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019052 /* need to copy the funcref so that we can clear rettv */
19053 functv = *rettv;
19054 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019055
19056 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019057 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019058 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019059 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19060 &len, evaluate, selfdict);
19061
19062 /* Clear the funcref afterwards, so that deleting it while
19063 * evaluating the arguments is possible (see test55). */
19064 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019065
19066 /* Stop the expression evaluation when immediately aborting on
19067 * error, or when an interrupt occurred or an exception was thrown
19068 * but not caught. */
19069 if (aborting())
19070 {
19071 if (ret == OK)
19072 clear_tv(rettv);
19073 ret = FAIL;
19074 }
19075 dict_unref(selfdict);
19076 selfdict = NULL;
19077 }
19078 else /* **arg == '[' || **arg == '.' */
19079 {
19080 dict_unref(selfdict);
19081 if (rettv->v_type == VAR_DICT)
19082 {
19083 selfdict = rettv->vval.v_dict;
19084 if (selfdict != NULL)
19085 ++selfdict->dv_refcount;
19086 }
19087 else
19088 selfdict = NULL;
19089 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19090 {
19091 clear_tv(rettv);
19092 ret = FAIL;
19093 }
19094 }
19095 }
19096 dict_unref(selfdict);
19097 return ret;
19098}
19099
19100/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019101 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019102 * value).
19103 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019104 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019105alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019106{
Bram Moolenaar33570922005-01-25 22:26:29 +000019107 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019108}
19109
19110/*
19111 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112 * The string "s" must have been allocated, it is consumed.
19113 * Return NULL for out of memory, the variable otherwise.
19114 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019115 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019116alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019117 char_u *s;
19118{
Bram Moolenaar33570922005-01-25 22:26:29 +000019119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019121 rettv = alloc_tv();
19122 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019124 rettv->v_type = VAR_STRING;
19125 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019126 }
19127 else
19128 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019129 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130}
19131
19132/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019133 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019135 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019136free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019137 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138{
19139 if (varp != NULL)
19140 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019141 switch (varp->v_type)
19142 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019143 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019144 func_unref(varp->vval.v_string);
19145 /*FALLTHROUGH*/
19146 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019147 vim_free(varp->vval.v_string);
19148 break;
19149 case VAR_LIST:
19150 list_unref(varp->vval.v_list);
19151 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019152 case VAR_DICT:
19153 dict_unref(varp->vval.v_dict);
19154 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019155 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019156#ifdef FEAT_FLOAT
19157 case VAR_FLOAT:
19158#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019159 case VAR_UNKNOWN:
19160 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019161 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019162 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019163 break;
19164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019165 vim_free(varp);
19166 }
19167}
19168
19169/*
19170 * Free the memory for a variable value and set the value to NULL or 0.
19171 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019172 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019173clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019174 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175{
19176 if (varp != NULL)
19177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019178 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019180 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019181 func_unref(varp->vval.v_string);
19182 /*FALLTHROUGH*/
19183 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019184 vim_free(varp->vval.v_string);
19185 varp->vval.v_string = NULL;
19186 break;
19187 case VAR_LIST:
19188 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019189 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019190 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019191 case VAR_DICT:
19192 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019193 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019194 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019195 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019196 varp->vval.v_number = 0;
19197 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019198#ifdef FEAT_FLOAT
19199 case VAR_FLOAT:
19200 varp->vval.v_float = 0.0;
19201 break;
19202#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 case VAR_UNKNOWN:
19204 break;
19205 default:
19206 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019207 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019208 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209 }
19210}
19211
19212/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019213 * Set the value of a variable to NULL without freeing items.
19214 */
19215 static void
19216init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019217 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019218{
19219 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019220 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019221}
19222
19223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 * Get the number value of a variable.
19225 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019226 * For incompatible types, return 0.
19227 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19228 * caller of incompatible types: it sets *denote to TRUE if "denote"
19229 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019230 */
19231 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019232get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019233 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019235 int error = FALSE;
19236
19237 return get_tv_number_chk(varp, &error); /* return 0L on error */
19238}
19239
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019240 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019241get_tv_number_chk(varp, denote)
19242 typval_T *varp;
19243 int *denote;
19244{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019245 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019246
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019247 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019249 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019250 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019251#ifdef FEAT_FLOAT
19252 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019253 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019254 break;
19255#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019256 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019257 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019258 break;
19259 case VAR_STRING:
19260 if (varp->vval.v_string != NULL)
19261 vim_str2nr(varp->vval.v_string, NULL, NULL,
19262 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019263 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019264 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019265 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019266 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019267 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019268 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019269 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019270 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019271 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019274 if (denote == NULL) /* useful for values that must be unsigned */
19275 n = -1;
19276 else
19277 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019278 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019279}
19280
19281/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019282 * Get the lnum from the first argument.
19283 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019284 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019285 */
19286 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019287get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019288 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289{
Bram Moolenaar33570922005-01-25 22:26:29 +000019290 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 linenr_T lnum;
19292
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019293 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 if (lnum == 0) /* no valid number, try using line() */
19295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019296 rettv.v_type = VAR_NUMBER;
19297 f_line(argvars, &rettv);
19298 lnum = rettv.vval.v_number;
19299 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300 }
19301 return lnum;
19302}
19303
19304/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019305 * Get the lnum from the first argument.
19306 * Also accepts "$", then "buf" is used.
19307 * Returns 0 on error.
19308 */
19309 static linenr_T
19310get_tv_lnum_buf(argvars, buf)
19311 typval_T *argvars;
19312 buf_T *buf;
19313{
19314 if (argvars[0].v_type == VAR_STRING
19315 && argvars[0].vval.v_string != NULL
19316 && argvars[0].vval.v_string[0] == '$'
19317 && buf != NULL)
19318 return buf->b_ml.ml_line_count;
19319 return get_tv_number_chk(&argvars[0], NULL);
19320}
19321
19322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 * Get the string value of a variable.
19324 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019325 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19326 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 * If the String variable has never been set, return an empty string.
19328 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019329 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19330 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331 */
19332 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019333get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019334 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335{
19336 static char_u mybuf[NUMBUFLEN];
19337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019338 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339}
19340
19341 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019342get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019343 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019344 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019346 char_u *res = get_tv_string_buf_chk(varp, buf);
19347
19348 return res != NULL ? res : (char_u *)"";
19349}
19350
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019351 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019352get_tv_string_chk(varp)
19353 typval_T *varp;
19354{
19355 static char_u mybuf[NUMBUFLEN];
19356
19357 return get_tv_string_buf_chk(varp, mybuf);
19358}
19359
19360 static char_u *
19361get_tv_string_buf_chk(varp, buf)
19362 typval_T *varp;
19363 char_u *buf;
19364{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019365 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019366 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019367 case VAR_NUMBER:
19368 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19369 return buf;
19370 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019371 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019372 break;
19373 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019374 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019375 break;
19376 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019377 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019378 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019379#ifdef FEAT_FLOAT
19380 case VAR_FLOAT:
19381 EMSG(_("E806: using Float as a String"));
19382 break;
19383#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019384 case VAR_STRING:
19385 if (varp->vval.v_string != NULL)
19386 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019387 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019388 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019389 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019390 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019392 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393}
19394
19395/*
19396 * Find variable "name" in the list of variables.
19397 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019398 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019399 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019400 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019402 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019403find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019405 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019408 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019409
Bram Moolenaara7043832005-01-21 11:56:39 +000019410 ht = find_var_ht(name, &varname);
19411 if (htp != NULL)
19412 *htp = ht;
19413 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019415 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019416}
19417
19418/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019419 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019420 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019421 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019422 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019423find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019424 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019425 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019426 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019427{
Bram Moolenaar33570922005-01-25 22:26:29 +000019428 hashitem_T *hi;
19429
19430 if (*varname == NUL)
19431 {
19432 /* Must be something like "s:", otherwise "ht" would be NULL. */
19433 switch (varname[-2])
19434 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019435 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019436 case 'g': return &globvars_var;
19437 case 'v': return &vimvars_var;
19438 case 'b': return &curbuf->b_bufvar;
19439 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019440#ifdef FEAT_WINDOWS
19441 case 't': return &curtab->tp_winvar;
19442#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019443 case 'l': return current_funccal == NULL
19444 ? NULL : &current_funccal->l_vars_var;
19445 case 'a': return current_funccal == NULL
19446 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019447 }
19448 return NULL;
19449 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019450
19451 hi = hash_find(ht, varname);
19452 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019453 {
19454 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019455 * worked find the variable again. Don't auto-load a script if it was
19456 * loaded already, otherwise it would be loaded every time when
19457 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019458 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019459 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019460 hi = hash_find(ht, varname);
19461 if (HASHITEM_EMPTY(hi))
19462 return NULL;
19463 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019464 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019465}
19466
19467/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019468 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019469 * Set "varname" to the start of name without ':'.
19470 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019471 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019472find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 char_u *name;
19474 char_u **varname;
19475{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019476 hashitem_T *hi;
19477
Bram Moolenaar071d4272004-06-13 20:20:40 +000019478 if (name[1] != ':')
19479 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019480 /* The name must not start with a colon or #. */
19481 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019482 return NULL;
19483 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019484
19485 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019486 hi = hash_find(&compat_hashtab, name);
19487 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019488 return &compat_hashtab;
19489
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019491 return &globvarht; /* global variable */
19492 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493 }
19494 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019495 if (*name == 'g') /* global variable */
19496 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019497 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19498 */
19499 if (vim_strchr(name + 2, ':') != NULL
19500 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019501 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019503 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019505 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019506#ifdef FEAT_WINDOWS
19507 if (*name == 't') /* tab page variable */
19508 return &curtab->tp_vars.dv_hashtab;
19509#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019510 if (*name == 'v') /* v: variable */
19511 return &vimvarht;
19512 if (*name == 'a' && current_funccal != NULL) /* function argument */
19513 return &current_funccal->l_avars.dv_hashtab;
19514 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19515 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 if (*name == 's' /* script variable */
19517 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19518 return &SCRIPT_VARS(current_SID);
19519 return NULL;
19520}
19521
19522/*
19523 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019524 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 * Returns NULL when it doesn't exist.
19526 */
19527 char_u *
19528get_var_value(name)
19529 char_u *name;
19530{
Bram Moolenaar33570922005-01-25 22:26:29 +000019531 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019532
Bram Moolenaara7043832005-01-21 11:56:39 +000019533 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 if (v == NULL)
19535 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019536 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537}
19538
19539/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019540 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 * sourcing this script and when executing functions defined in the script.
19542 */
19543 void
19544new_script_vars(id)
19545 scid_T id;
19546{
Bram Moolenaara7043832005-01-21 11:56:39 +000019547 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019548 hashtab_T *ht;
19549 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019550
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19552 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019553 /* Re-allocating ga_data means that an ht_array pointing to
19554 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019555 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019556 for (i = 1; i <= ga_scripts.ga_len; ++i)
19557 {
19558 ht = &SCRIPT_VARS(i);
19559 if (ht->ht_mask == HT_INIT_SIZE - 1)
19560 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019561 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019562 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019563 }
19564
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565 while (ga_scripts.ga_len < id)
19566 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019567 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019568 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019569 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 }
19572 }
19573}
19574
19575/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019576 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19577 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578 */
19579 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019580init_var_dict(dict, dict_var)
19581 dict_T *dict;
19582 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583{
Bram Moolenaar33570922005-01-25 22:26:29 +000019584 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019585 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019586 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019587 dict_var->di_tv.vval.v_dict = dict;
19588 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019589 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019590 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19591 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592}
19593
19594/*
19595 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019596 * Frees all allocated variables and the value they contain.
19597 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598 */
19599 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019600vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019601 hashtab_T *ht;
19602{
19603 vars_clear_ext(ht, TRUE);
19604}
19605
19606/*
19607 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19608 */
19609 static void
19610vars_clear_ext(ht, free_val)
19611 hashtab_T *ht;
19612 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613{
Bram Moolenaara7043832005-01-21 11:56:39 +000019614 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019615 hashitem_T *hi;
19616 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617
Bram Moolenaar33570922005-01-25 22:26:29 +000019618 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019619 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019620 for (hi = ht->ht_array; todo > 0; ++hi)
19621 {
19622 if (!HASHITEM_EMPTY(hi))
19623 {
19624 --todo;
19625
Bram Moolenaar33570922005-01-25 22:26:29 +000019626 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019627 * ht_array might change then. hash_clear() takes care of it
19628 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019629 v = HI2DI(hi);
19630 if (free_val)
19631 clear_tv(&v->di_tv);
19632 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19633 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019634 }
19635 }
19636 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019637 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638}
19639
Bram Moolenaara7043832005-01-21 11:56:39 +000019640/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019641 * Delete a variable from hashtab "ht" at item "hi".
19642 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019645delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019646 hashtab_T *ht;
19647 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019648{
Bram Moolenaar33570922005-01-25 22:26:29 +000019649 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019650
19651 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019652 clear_tv(&di->di_tv);
19653 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654}
19655
19656/*
19657 * List the value of one internal variable.
19658 */
19659 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019660list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019661 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019662 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019663 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019665 char_u *tofree;
19666 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019667 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019668
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019669 current_copyID += COPYID_INC;
19670 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019671 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019672 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019673 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674}
19675
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019677list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019678 char_u *prefix;
19679 char_u *name;
19680 int type;
19681 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019682 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683{
Bram Moolenaar31859182007-08-14 20:41:13 +000019684 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19685 msg_start();
19686 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 if (name != NULL) /* "a:" vars don't have a name stored */
19688 msg_puts(name);
19689 msg_putchar(' ');
19690 msg_advance(22);
19691 if (type == VAR_NUMBER)
19692 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019693 else if (type == VAR_FUNC)
19694 msg_putchar('*');
19695 else if (type == VAR_LIST)
19696 {
19697 msg_putchar('[');
19698 if (*string == '[')
19699 ++string;
19700 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019701 else if (type == VAR_DICT)
19702 {
19703 msg_putchar('{');
19704 if (*string == '{')
19705 ++string;
19706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019707 else
19708 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019709
Bram Moolenaar071d4272004-06-13 20:20:40 +000019710 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019711
19712 if (type == VAR_FUNC)
19713 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019714 if (*first)
19715 {
19716 msg_clr_eos();
19717 *first = FALSE;
19718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719}
19720
19721/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019722 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 * If the variable already exists, the value is updated.
19724 * Otherwise the variable is created.
19725 */
19726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019727set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019729 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019730 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731{
Bram Moolenaar33570922005-01-25 22:26:29 +000019732 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019734 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019735 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019737 ht = find_var_ht(name, &varname);
19738 if (ht == NULL || *varname == NUL)
19739 {
19740 EMSG2(_(e_illvar), name);
19741 return;
19742 }
19743 v = find_var_in_ht(ht, varname, TRUE);
19744
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019745 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019746 {
19747 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19748 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19749 ? name[2] : name[0]))
19750 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019751 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019752 return;
19753 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019754 /* Don't allow hiding a function. When "v" is not NULL we migth be
19755 * assigning another function to the same var, the type is checked
19756 * below. */
19757 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019758 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019759 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019760 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019761 return;
19762 }
19763 }
19764
Bram Moolenaar33570922005-01-25 22:26:29 +000019765 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019767 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019768 if (var_check_ro(v->di_flags, name)
19769 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019770 return;
19771 if (v->di_tv.v_type != tv->v_type
19772 && !((v->di_tv.v_type == VAR_STRING
19773 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019774 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019775 || tv->v_type == VAR_NUMBER))
19776#ifdef FEAT_FLOAT
19777 && !((v->di_tv.v_type == VAR_NUMBER
19778 || v->di_tv.v_type == VAR_FLOAT)
19779 && (tv->v_type == VAR_NUMBER
19780 || tv->v_type == VAR_FLOAT))
19781#endif
19782 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019783 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019784 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019785 return;
19786 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019787
19788 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019789 * Handle setting internal v: variables separately: we don't change
19790 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019791 */
19792 if (ht == &vimvarht)
19793 {
19794 if (v->di_tv.v_type == VAR_STRING)
19795 {
19796 vim_free(v->di_tv.vval.v_string);
19797 if (copy || tv->v_type != VAR_STRING)
19798 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19799 else
19800 {
19801 /* Take over the string to avoid an extra alloc/free. */
19802 v->di_tv.vval.v_string = tv->vval.v_string;
19803 tv->vval.v_string = NULL;
19804 }
19805 }
19806 else if (v->di_tv.v_type != VAR_NUMBER)
19807 EMSG2(_(e_intern2), "set_var()");
19808 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019809 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019810 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019811 if (STRCMP(varname, "searchforward") == 0)
19812 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19813 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019814 return;
19815 }
19816
19817 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818 }
19819 else /* add a new variable */
19820 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019821 /* Can't add "v:" variable. */
19822 if (ht == &vimvarht)
19823 {
19824 EMSG2(_(e_illvar), name);
19825 return;
19826 }
19827
Bram Moolenaar92124a32005-06-17 22:03:40 +000019828 /* Make sure the variable name is valid. */
19829 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019830 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19831 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019832 {
19833 EMSG2(_(e_illvar), varname);
19834 return;
19835 }
19836
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019837 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19838 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019839 if (v == NULL)
19840 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019841 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019842 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019844 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 return;
19846 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019847 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019849
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019850 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019851 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019852 else
19853 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019854 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019855 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019856 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858}
19859
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019860/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019861 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019862 * Also give an error message.
19863 */
19864 static int
19865var_check_ro(flags, name)
19866 int flags;
19867 char_u *name;
19868{
19869 if (flags & DI_FLAGS_RO)
19870 {
19871 EMSG2(_(e_readonlyvar), name);
19872 return TRUE;
19873 }
19874 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19875 {
19876 EMSG2(_(e_readonlysbx), name);
19877 return TRUE;
19878 }
19879 return FALSE;
19880}
19881
19882/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019883 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19884 * Also give an error message.
19885 */
19886 static int
19887var_check_fixed(flags, name)
19888 int flags;
19889 char_u *name;
19890{
19891 if (flags & DI_FLAGS_FIX)
19892 {
19893 EMSG2(_("E795: Cannot delete variable %s"), name);
19894 return TRUE;
19895 }
19896 return FALSE;
19897}
19898
19899/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019900 * Return TRUE if typeval "tv" is set to be locked (immutable).
19901 * Also give an error message, using "name".
19902 */
19903 static int
19904tv_check_lock(lock, name)
19905 int lock;
19906 char_u *name;
19907{
19908 if (lock & VAR_LOCKED)
19909 {
19910 EMSG2(_("E741: Value is locked: %s"),
19911 name == NULL ? (char_u *)_("Unknown") : name);
19912 return TRUE;
19913 }
19914 if (lock & VAR_FIXED)
19915 {
19916 EMSG2(_("E742: Cannot change value of %s"),
19917 name == NULL ? (char_u *)_("Unknown") : name);
19918 return TRUE;
19919 }
19920 return FALSE;
19921}
19922
19923/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019924 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019925 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019926 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019927 * It is OK for "from" and "to" to point to the same item. This is used to
19928 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019929 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019930 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019931copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019932 typval_T *from;
19933 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019935 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019936 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019937 switch (from->v_type)
19938 {
19939 case VAR_NUMBER:
19940 to->vval.v_number = from->vval.v_number;
19941 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019942#ifdef FEAT_FLOAT
19943 case VAR_FLOAT:
19944 to->vval.v_float = from->vval.v_float;
19945 break;
19946#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019947 case VAR_STRING:
19948 case VAR_FUNC:
19949 if (from->vval.v_string == NULL)
19950 to->vval.v_string = NULL;
19951 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019952 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019953 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019954 if (from->v_type == VAR_FUNC)
19955 func_ref(to->vval.v_string);
19956 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019957 break;
19958 case VAR_LIST:
19959 if (from->vval.v_list == NULL)
19960 to->vval.v_list = NULL;
19961 else
19962 {
19963 to->vval.v_list = from->vval.v_list;
19964 ++to->vval.v_list->lv_refcount;
19965 }
19966 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019967 case VAR_DICT:
19968 if (from->vval.v_dict == NULL)
19969 to->vval.v_dict = NULL;
19970 else
19971 {
19972 to->vval.v_dict = from->vval.v_dict;
19973 ++to->vval.v_dict->dv_refcount;
19974 }
19975 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019976 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019977 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019978 break;
19979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980}
19981
19982/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019983 * Make a copy of an item.
19984 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019985 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19986 * reference to an already copied list/dict can be used.
19987 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019988 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019989 static int
19990item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019991 typval_T *from;
19992 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019993 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019994 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019995{
19996 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019997 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019998
Bram Moolenaar33570922005-01-25 22:26:29 +000019999 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020000 {
20001 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020002 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020003 }
20004 ++recurse;
20005
20006 switch (from->v_type)
20007 {
20008 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020009#ifdef FEAT_FLOAT
20010 case VAR_FLOAT:
20011#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020012 case VAR_STRING:
20013 case VAR_FUNC:
20014 copy_tv(from, to);
20015 break;
20016 case VAR_LIST:
20017 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020018 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020019 if (from->vval.v_list == NULL)
20020 to->vval.v_list = NULL;
20021 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20022 {
20023 /* use the copy made earlier */
20024 to->vval.v_list = from->vval.v_list->lv_copylist;
20025 ++to->vval.v_list->lv_refcount;
20026 }
20027 else
20028 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20029 if (to->vval.v_list == NULL)
20030 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020031 break;
20032 case VAR_DICT:
20033 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020034 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020035 if (from->vval.v_dict == NULL)
20036 to->vval.v_dict = NULL;
20037 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20038 {
20039 /* use the copy made earlier */
20040 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20041 ++to->vval.v_dict->dv_refcount;
20042 }
20043 else
20044 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20045 if (to->vval.v_dict == NULL)
20046 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020047 break;
20048 default:
20049 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020050 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020051 }
20052 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020053 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020054}
20055
20056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 * ":echo expr1 ..." print each argument separated with a space, add a
20058 * newline at the end.
20059 * ":echon expr1 ..." print each argument plain.
20060 */
20061 void
20062ex_echo(eap)
20063 exarg_T *eap;
20064{
20065 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020066 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020067 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020068 char_u *p;
20069 int needclr = TRUE;
20070 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020071 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072
20073 if (eap->skip)
20074 ++emsg_skip;
20075 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20076 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020077 /* If eval1() causes an error message the text from the command may
20078 * still need to be cleared. E.g., "echo 22,44". */
20079 need_clr_eos = needclr;
20080
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020082 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 {
20084 /*
20085 * Report the invalid expression unless the expression evaluation
20086 * has been cancelled due to an aborting error, an interrupt, or an
20087 * exception.
20088 */
20089 if (!aborting())
20090 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020091 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020092 break;
20093 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020094 need_clr_eos = FALSE;
20095
Bram Moolenaar071d4272004-06-13 20:20:40 +000020096 if (!eap->skip)
20097 {
20098 if (atstart)
20099 {
20100 atstart = FALSE;
20101 /* Call msg_start() after eval1(), evaluating the expression
20102 * may cause a message to appear. */
20103 if (eap->cmdidx == CMD_echo)
20104 msg_start();
20105 }
20106 else if (eap->cmdidx == CMD_echo)
20107 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020108 current_copyID += COPYID_INC;
20109 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020110 if (p != NULL)
20111 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020113 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020115 if (*p != TAB && needclr)
20116 {
20117 /* remove any text still there from the command */
20118 msg_clr_eos();
20119 needclr = FALSE;
20120 }
20121 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 }
20123 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020124 {
20125#ifdef FEAT_MBYTE
20126 if (has_mbyte)
20127 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020128 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020129
20130 (void)msg_outtrans_len_attr(p, i, echo_attr);
20131 p += i - 1;
20132 }
20133 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020135 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020138 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020140 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 arg = skipwhite(arg);
20142 }
20143 eap->nextcmd = check_nextcmd(arg);
20144
20145 if (eap->skip)
20146 --emsg_skip;
20147 else
20148 {
20149 /* remove text that may still be there from the command */
20150 if (needclr)
20151 msg_clr_eos();
20152 if (eap->cmdidx == CMD_echo)
20153 msg_end();
20154 }
20155}
20156
20157/*
20158 * ":echohl {name}".
20159 */
20160 void
20161ex_echohl(eap)
20162 exarg_T *eap;
20163{
20164 int id;
20165
20166 id = syn_name2id(eap->arg);
20167 if (id == 0)
20168 echo_attr = 0;
20169 else
20170 echo_attr = syn_id2attr(id);
20171}
20172
20173/*
20174 * ":execute expr1 ..." execute the result of an expression.
20175 * ":echomsg expr1 ..." Print a message
20176 * ":echoerr expr1 ..." Print an error
20177 * Each gets spaces around each argument and a newline at the end for
20178 * echo commands
20179 */
20180 void
20181ex_execute(eap)
20182 exarg_T *eap;
20183{
20184 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020185 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186 int ret = OK;
20187 char_u *p;
20188 garray_T ga;
20189 int len;
20190 int save_did_emsg;
20191
20192 ga_init2(&ga, 1, 80);
20193
20194 if (eap->skip)
20195 ++emsg_skip;
20196 while (*arg != NUL && *arg != '|' && *arg != '\n')
20197 {
20198 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020199 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 {
20201 /*
20202 * Report the invalid expression unless the expression evaluation
20203 * has been cancelled due to an aborting error, an interrupt, or an
20204 * exception.
20205 */
20206 if (!aborting())
20207 EMSG2(_(e_invexpr2), p);
20208 ret = FAIL;
20209 break;
20210 }
20211
20212 if (!eap->skip)
20213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020214 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215 len = (int)STRLEN(p);
20216 if (ga_grow(&ga, len + 2) == FAIL)
20217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020218 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 ret = FAIL;
20220 break;
20221 }
20222 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 ga.ga_len += len;
20226 }
20227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020228 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 arg = skipwhite(arg);
20230 }
20231
20232 if (ret != FAIL && ga.ga_data != NULL)
20233 {
20234 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020237 out_flush();
20238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020239 else if (eap->cmdidx == CMD_echoerr)
20240 {
20241 /* We don't want to abort following commands, restore did_emsg. */
20242 save_did_emsg = did_emsg;
20243 EMSG((char_u *)ga.ga_data);
20244 if (!force_abort)
20245 did_emsg = save_did_emsg;
20246 }
20247 else if (eap->cmdidx == CMD_execute)
20248 do_cmdline((char_u *)ga.ga_data,
20249 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20250 }
20251
20252 ga_clear(&ga);
20253
20254 if (eap->skip)
20255 --emsg_skip;
20256
20257 eap->nextcmd = check_nextcmd(arg);
20258}
20259
20260/*
20261 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20262 * "arg" points to the "&" or '+' when called, to "option" when returning.
20263 * Returns NULL when no option name found. Otherwise pointer to the char
20264 * after the option name.
20265 */
20266 static char_u *
20267find_option_end(arg, opt_flags)
20268 char_u **arg;
20269 int *opt_flags;
20270{
20271 char_u *p = *arg;
20272
20273 ++p;
20274 if (*p == 'g' && p[1] == ':')
20275 {
20276 *opt_flags = OPT_GLOBAL;
20277 p += 2;
20278 }
20279 else if (*p == 'l' && p[1] == ':')
20280 {
20281 *opt_flags = OPT_LOCAL;
20282 p += 2;
20283 }
20284 else
20285 *opt_flags = 0;
20286
20287 if (!ASCII_ISALPHA(*p))
20288 return NULL;
20289 *arg = p;
20290
20291 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20292 p += 4; /* termcap option */
20293 else
20294 while (ASCII_ISALPHA(*p))
20295 ++p;
20296 return p;
20297}
20298
20299/*
20300 * ":function"
20301 */
20302 void
20303ex_function(eap)
20304 exarg_T *eap;
20305{
20306 char_u *theline;
20307 int j;
20308 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 char_u *name = NULL;
20311 char_u *p;
20312 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020313 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314 garray_T newargs;
20315 garray_T newlines;
20316 int varargs = FALSE;
20317 int mustend = FALSE;
20318 int flags = 0;
20319 ufunc_T *fp;
20320 int indent;
20321 int nesting;
20322 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020323 dictitem_T *v;
20324 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020325 static int func_nr = 0; /* number for nameless function */
20326 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020327 hashtab_T *ht;
20328 int todo;
20329 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020330 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331
20332 /*
20333 * ":function" without argument: list functions.
20334 */
20335 if (ends_excmd(*eap->arg))
20336 {
20337 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020338 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020339 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020340 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020341 {
20342 if (!HASHITEM_EMPTY(hi))
20343 {
20344 --todo;
20345 fp = HI2UF(hi);
20346 if (!isdigit(*fp->uf_name))
20347 list_func_head(fp, FALSE);
20348 }
20349 }
20350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 eap->nextcmd = check_nextcmd(eap->arg);
20352 return;
20353 }
20354
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020355 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020356 * ":function /pat": list functions matching pattern.
20357 */
20358 if (*eap->arg == '/')
20359 {
20360 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20361 if (!eap->skip)
20362 {
20363 regmatch_T regmatch;
20364
20365 c = *p;
20366 *p = NUL;
20367 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20368 *p = c;
20369 if (regmatch.regprog != NULL)
20370 {
20371 regmatch.rm_ic = p_ic;
20372
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020373 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020374 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20375 {
20376 if (!HASHITEM_EMPTY(hi))
20377 {
20378 --todo;
20379 fp = HI2UF(hi);
20380 if (!isdigit(*fp->uf_name)
20381 && vim_regexec(&regmatch, fp->uf_name, 0))
20382 list_func_head(fp, FALSE);
20383 }
20384 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020385 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020386 }
20387 }
20388 if (*p == '/')
20389 ++p;
20390 eap->nextcmd = check_nextcmd(p);
20391 return;
20392 }
20393
20394 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020395 * Get the function name. There are these situations:
20396 * func normal function name
20397 * "name" == func, "fudi.fd_dict" == NULL
20398 * dict.func new dictionary entry
20399 * "name" == NULL, "fudi.fd_dict" set,
20400 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20401 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020402 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020403 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20404 * dict.func existing dict entry that's not a Funcref
20405 * "name" == NULL, "fudi.fd_dict" set,
20406 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020408 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020409 name = trans_function_name(&p, eap->skip, 0, &fudi);
20410 paren = (vim_strchr(p, '(') != NULL);
20411 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 {
20413 /*
20414 * Return on an invalid expression in braces, unless the expression
20415 * evaluation has been cancelled due to an aborting error, an
20416 * interrupt, or an exception.
20417 */
20418 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020419 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020420 if (!eap->skip && fudi.fd_newkey != NULL)
20421 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020422 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425 else
20426 eap->skip = TRUE;
20427 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020428
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 /* An error in a function call during evaluation of an expression in magic
20430 * braces should not cause the function not to be defined. */
20431 saved_did_emsg = did_emsg;
20432 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433
20434 /*
20435 * ":function func" with only function name: list function.
20436 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020437 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438 {
20439 if (!ends_excmd(*skipwhite(p)))
20440 {
20441 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020442 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020443 }
20444 eap->nextcmd = check_nextcmd(p);
20445 if (eap->nextcmd != NULL)
20446 *p = NUL;
20447 if (!eap->skip && !got_int)
20448 {
20449 fp = find_func(name);
20450 if (fp != NULL)
20451 {
20452 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020453 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020454 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020455 if (FUNCLINE(fp, j) == NULL)
20456 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 msg_putchar('\n');
20458 msg_outnum((long)(j + 1));
20459 if (j < 9)
20460 msg_putchar(' ');
20461 if (j < 99)
20462 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020463 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 out_flush(); /* show a line at a time */
20465 ui_breakcheck();
20466 }
20467 if (!got_int)
20468 {
20469 msg_putchar('\n');
20470 msg_puts((char_u *)" endfunction");
20471 }
20472 }
20473 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020474 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020476 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020477 }
20478
20479 /*
20480 * ":function name(arg1, arg2)" Define function.
20481 */
20482 p = skipwhite(p);
20483 if (*p != '(')
20484 {
20485 if (!eap->skip)
20486 {
20487 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020488 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020489 }
20490 /* attempt to continue by skipping some text */
20491 if (vim_strchr(p, '(') != NULL)
20492 p = vim_strchr(p, '(');
20493 }
20494 p = skipwhite(p + 1);
20495
20496 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20497 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20498
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020499 if (!eap->skip)
20500 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020501 /* Check the name of the function. Unless it's a dictionary function
20502 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020503 if (name != NULL)
20504 arg = name;
20505 else
20506 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020507 if (arg != NULL && (fudi.fd_di == NULL
20508 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020509 {
20510 if (*arg == K_SPECIAL)
20511 j = 3;
20512 else
20513 j = 0;
20514 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20515 : eval_isnamec(arg[j])))
20516 ++j;
20517 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020518 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020519 }
20520 }
20521
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 /*
20523 * Isolate the arguments: "arg1, arg2, ...)"
20524 */
20525 while (*p != ')')
20526 {
20527 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20528 {
20529 varargs = TRUE;
20530 p += 3;
20531 mustend = TRUE;
20532 }
20533 else
20534 {
20535 arg = p;
20536 while (ASCII_ISALNUM(*p) || *p == '_')
20537 ++p;
20538 if (arg == p || isdigit(*arg)
20539 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20540 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20541 {
20542 if (!eap->skip)
20543 EMSG2(_("E125: Illegal argument: %s"), arg);
20544 break;
20545 }
20546 if (ga_grow(&newargs, 1) == FAIL)
20547 goto erret;
20548 c = *p;
20549 *p = NUL;
20550 arg = vim_strsave(arg);
20551 if (arg == NULL)
20552 goto erret;
20553 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20554 *p = c;
20555 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 if (*p == ',')
20557 ++p;
20558 else
20559 mustend = TRUE;
20560 }
20561 p = skipwhite(p);
20562 if (mustend && *p != ')')
20563 {
20564 if (!eap->skip)
20565 EMSG2(_(e_invarg2), eap->arg);
20566 break;
20567 }
20568 }
20569 ++p; /* skip the ')' */
20570
Bram Moolenaare9a41262005-01-15 22:18:47 +000020571 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 for (;;)
20573 {
20574 p = skipwhite(p);
20575 if (STRNCMP(p, "range", 5) == 0)
20576 {
20577 flags |= FC_RANGE;
20578 p += 5;
20579 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020580 else if (STRNCMP(p, "dict", 4) == 0)
20581 {
20582 flags |= FC_DICT;
20583 p += 4;
20584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 else if (STRNCMP(p, "abort", 5) == 0)
20586 {
20587 flags |= FC_ABORT;
20588 p += 5;
20589 }
20590 else
20591 break;
20592 }
20593
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020594 /* When there is a line break use what follows for the function body.
20595 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20596 if (*p == '\n')
20597 line_arg = p + 1;
20598 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020599 EMSG(_(e_trailing));
20600
20601 /*
20602 * Read the body of the function, until ":endfunction" is found.
20603 */
20604 if (KeyTyped)
20605 {
20606 /* Check if the function already exists, don't let the user type the
20607 * whole function before telling him it doesn't work! For a script we
20608 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020609 if (!eap->skip && !eap->forceit)
20610 {
20611 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20612 EMSG(_(e_funcdict));
20613 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020614 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020617 if (!eap->skip && did_emsg)
20618 goto erret;
20619
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 msg_putchar('\n'); /* don't overwrite the function name */
20621 cmdline_row = msg_row;
20622 }
20623
20624 indent = 2;
20625 nesting = 0;
20626 for (;;)
20627 {
20628 msg_scroll = TRUE;
20629 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020630 sourcing_lnum_off = sourcing_lnum;
20631
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020632 if (line_arg != NULL)
20633 {
20634 /* Use eap->arg, split up in parts by line breaks. */
20635 theline = line_arg;
20636 p = vim_strchr(theline, '\n');
20637 if (p == NULL)
20638 line_arg += STRLEN(line_arg);
20639 else
20640 {
20641 *p = NUL;
20642 line_arg = p + 1;
20643 }
20644 }
20645 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 theline = getcmdline(':', 0L, indent);
20647 else
20648 theline = eap->getline(':', eap->cookie, indent);
20649 if (KeyTyped)
20650 lines_left = Rows - 1;
20651 if (theline == NULL)
20652 {
20653 EMSG(_("E126: Missing :endfunction"));
20654 goto erret;
20655 }
20656
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020657 /* Detect line continuation: sourcing_lnum increased more than one. */
20658 if (sourcing_lnum > sourcing_lnum_off + 1)
20659 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20660 else
20661 sourcing_lnum_off = 0;
20662
Bram Moolenaar071d4272004-06-13 20:20:40 +000020663 if (skip_until != NULL)
20664 {
20665 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20666 * don't check for ":endfunc". */
20667 if (STRCMP(theline, skip_until) == 0)
20668 {
20669 vim_free(skip_until);
20670 skip_until = NULL;
20671 }
20672 }
20673 else
20674 {
20675 /* skip ':' and blanks*/
20676 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20677 ;
20678
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020679 /* Check for "endfunction". */
20680 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020682 if (line_arg == NULL)
20683 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 break;
20685 }
20686
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020687 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688 * at "end". */
20689 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20690 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020691 else if (STRNCMP(p, "if", 2) == 0
20692 || STRNCMP(p, "wh", 2) == 0
20693 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 || STRNCMP(p, "try", 3) == 0)
20695 indent += 2;
20696
20697 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020698 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020700 if (*p == '!')
20701 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020702 p += eval_fname_script(p);
20703 if (ASCII_ISALPHA(*p))
20704 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020705 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706 if (*skipwhite(p) == '(')
20707 {
20708 ++nesting;
20709 indent += 2;
20710 }
20711 }
20712 }
20713
20714 /* Check for ":append" or ":insert". */
20715 p = skip_range(p, NULL);
20716 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20717 || (p[0] == 'i'
20718 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20719 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20720 skip_until = vim_strsave((char_u *)".");
20721
20722 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20723 arg = skipwhite(skiptowhite(p));
20724 if (arg[0] == '<' && arg[1] =='<'
20725 && ((p[0] == 'p' && p[1] == 'y'
20726 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20727 || (p[0] == 'p' && p[1] == 'e'
20728 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20729 || (p[0] == 't' && p[1] == 'c'
20730 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20731 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20732 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020733 || (p[0] == 'm' && p[1] == 'z'
20734 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 ))
20736 {
20737 /* ":python <<" continues until a dot, like ":append" */
20738 p = skipwhite(arg + 2);
20739 if (*p == NUL)
20740 skip_until = vim_strsave((char_u *)".");
20741 else
20742 skip_until = vim_strsave(p);
20743 }
20744 }
20745
20746 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020747 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020748 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020749 if (line_arg == NULL)
20750 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020751 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020752 }
20753
20754 /* Copy the line to newly allocated memory. get_one_sourceline()
20755 * allocates 250 bytes per line, this saves 80% on average. The cost
20756 * is an extra alloc/free. */
20757 p = vim_strsave(theline);
20758 if (p != NULL)
20759 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020760 if (line_arg == NULL)
20761 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020762 theline = p;
20763 }
20764
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020765 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20766
20767 /* Add NULL lines for continuation lines, so that the line count is
20768 * equal to the index in the growarray. */
20769 while (sourcing_lnum_off-- > 0)
20770 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020771
20772 /* Check for end of eap->arg. */
20773 if (line_arg != NULL && *line_arg == NUL)
20774 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020775 }
20776
20777 /* Don't define the function when skipping commands or when an error was
20778 * detected. */
20779 if (eap->skip || did_emsg)
20780 goto erret;
20781
20782 /*
20783 * If there are no errors, add the function
20784 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020785 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020786 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020787 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020788 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020789 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020790 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020791 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020792 goto erret;
20793 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020794
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020795 fp = find_func(name);
20796 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020798 if (!eap->forceit)
20799 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020800 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020801 goto erret;
20802 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020803 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020804 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020805 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020806 name);
20807 goto erret;
20808 }
20809 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020810 ga_clear_strings(&(fp->uf_args));
20811 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020812 vim_free(name);
20813 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020815 }
20816 else
20817 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020818 char numbuf[20];
20819
20820 fp = NULL;
20821 if (fudi.fd_newkey == NULL && !eap->forceit)
20822 {
20823 EMSG(_(e_funcdict));
20824 goto erret;
20825 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020826 if (fudi.fd_di == NULL)
20827 {
20828 /* Can't add a function to a locked dictionary */
20829 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20830 goto erret;
20831 }
20832 /* Can't change an existing function if it is locked */
20833 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20834 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020835
20836 /* Give the function a sequential number. Can only be used with a
20837 * Funcref! */
20838 vim_free(name);
20839 sprintf(numbuf, "%d", ++func_nr);
20840 name = vim_strsave((char_u *)numbuf);
20841 if (name == NULL)
20842 goto erret;
20843 }
20844
20845 if (fp == NULL)
20846 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020847 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020848 {
20849 int slen, plen;
20850 char_u *scriptname;
20851
20852 /* Check that the autoload name matches the script name. */
20853 j = FAIL;
20854 if (sourcing_name != NULL)
20855 {
20856 scriptname = autoload_name(name);
20857 if (scriptname != NULL)
20858 {
20859 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020860 plen = (int)STRLEN(p);
20861 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020862 if (slen > plen && fnamecmp(p,
20863 sourcing_name + slen - plen) == 0)
20864 j = OK;
20865 vim_free(scriptname);
20866 }
20867 }
20868 if (j == FAIL)
20869 {
20870 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20871 goto erret;
20872 }
20873 }
20874
20875 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 if (fp == NULL)
20877 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020878
20879 if (fudi.fd_dict != NULL)
20880 {
20881 if (fudi.fd_di == NULL)
20882 {
20883 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020884 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020885 if (fudi.fd_di == NULL)
20886 {
20887 vim_free(fp);
20888 goto erret;
20889 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020890 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20891 {
20892 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020893 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020894 goto erret;
20895 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020896 }
20897 else
20898 /* overwrite existing dict entry */
20899 clear_tv(&fudi.fd_di->di_tv);
20900 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020901 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020902 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020903 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020904
20905 /* behave like "dict" was used */
20906 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020907 }
20908
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020910 STRCPY(fp->uf_name, name);
20911 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020913 fp->uf_args = newargs;
20914 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020915#ifdef FEAT_PROFILE
20916 fp->uf_tml_count = NULL;
20917 fp->uf_tml_total = NULL;
20918 fp->uf_tml_self = NULL;
20919 fp->uf_profiling = FALSE;
20920 if (prof_def_func())
20921 func_do_profile(fp);
20922#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020923 fp->uf_varargs = varargs;
20924 fp->uf_flags = flags;
20925 fp->uf_calls = 0;
20926 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020927 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928
20929erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 ga_clear_strings(&newargs);
20931 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020932ret_free:
20933 vim_free(skip_until);
20934 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937}
20938
20939/*
20940 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020941 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020943 * flags:
20944 * TFN_INT: internal function name OK
20945 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 * Advances "pp" to just after the function name (if no error).
20947 */
20948 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020949trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 char_u **pp;
20951 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020952 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020953 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020955 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 char_u *start;
20957 char_u *end;
20958 int lead;
20959 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020961 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020962
20963 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020964 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020966
20967 /* Check for hard coded <SNR>: already translated function ID (from a user
20968 * command). */
20969 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20970 && (*pp)[2] == (int)KE_SNR)
20971 {
20972 *pp += 3;
20973 len = get_id_len(pp) + 3;
20974 return vim_strnsave(start, len);
20975 }
20976
20977 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20978 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020980 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020982
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020983 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20984 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020985 if (end == start)
20986 {
20987 if (!skip)
20988 EMSG(_("E129: Function name required"));
20989 goto theend;
20990 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020991 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020992 {
20993 /*
20994 * Report an invalid expression in braces, unless the expression
20995 * evaluation has been cancelled due to an aborting error, an
20996 * interrupt, or an exception.
20997 */
20998 if (!aborting())
20999 {
21000 if (end != NULL)
21001 EMSG2(_(e_invarg2), start);
21002 }
21003 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021004 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021005 goto theend;
21006 }
21007
21008 if (lv.ll_tv != NULL)
21009 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021010 if (fdp != NULL)
21011 {
21012 fdp->fd_dict = lv.ll_dict;
21013 fdp->fd_newkey = lv.ll_newkey;
21014 lv.ll_newkey = NULL;
21015 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021016 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021017 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21018 {
21019 name = vim_strsave(lv.ll_tv->vval.v_string);
21020 *pp = end;
21021 }
21022 else
21023 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021024 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21025 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021026 EMSG(_(e_funcref));
21027 else
21028 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021029 name = NULL;
21030 }
21031 goto theend;
21032 }
21033
21034 if (lv.ll_name == NULL)
21035 {
21036 /* Error found, but continue after the function name. */
21037 *pp = end;
21038 goto theend;
21039 }
21040
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021041 /* Check if the name is a Funcref. If so, use the value. */
21042 if (lv.ll_exp_name != NULL)
21043 {
21044 len = (int)STRLEN(lv.ll_exp_name);
21045 name = deref_func_name(lv.ll_exp_name, &len);
21046 if (name == lv.ll_exp_name)
21047 name = NULL;
21048 }
21049 else
21050 {
21051 len = (int)(end - *pp);
21052 name = deref_func_name(*pp, &len);
21053 if (name == *pp)
21054 name = NULL;
21055 }
21056 if (name != NULL)
21057 {
21058 name = vim_strsave(name);
21059 *pp = end;
21060 goto theend;
21061 }
21062
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021063 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021064 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021065 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021066 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21067 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21068 {
21069 /* When there was "s:" already or the name expanded to get a
21070 * leading "s:" then remove it. */
21071 lv.ll_name += 2;
21072 len -= 2;
21073 lead = 2;
21074 }
21075 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021076 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021077 {
21078 if (lead == 2) /* skip over "s:" */
21079 lv.ll_name += 2;
21080 len = (int)(end - lv.ll_name);
21081 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021082
21083 /*
21084 * Copy the function name to allocated memory.
21085 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21086 * Accept <SNR>123_name() outside a script.
21087 */
21088 if (skip)
21089 lead = 0; /* do nothing */
21090 else if (lead > 0)
21091 {
21092 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021093 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21094 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021095 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021096 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021097 if (current_SID <= 0)
21098 {
21099 EMSG(_(e_usingsid));
21100 goto theend;
21101 }
21102 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21103 lead += (int)STRLEN(sid_buf);
21104 }
21105 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021106 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021107 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021108 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021109 goto theend;
21110 }
21111 name = alloc((unsigned)(len + lead + 1));
21112 if (name != NULL)
21113 {
21114 if (lead > 0)
21115 {
21116 name[0] = K_SPECIAL;
21117 name[1] = KS_EXTRA;
21118 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021119 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021120 STRCPY(name + 3, sid_buf);
21121 }
21122 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21123 name[len + lead] = NUL;
21124 }
21125 *pp = end;
21126
21127theend:
21128 clear_lval(&lv);
21129 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130}
21131
21132/*
21133 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21134 * Return 2 if "p" starts with "s:".
21135 * Return 0 otherwise.
21136 */
21137 static int
21138eval_fname_script(p)
21139 char_u *p;
21140{
21141 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21142 || STRNICMP(p + 1, "SNR>", 4) == 0))
21143 return 5;
21144 if (p[0] == 's' && p[1] == ':')
21145 return 2;
21146 return 0;
21147}
21148
21149/*
21150 * Return TRUE if "p" starts with "<SID>" or "s:".
21151 * Only works if eval_fname_script() returned non-zero for "p"!
21152 */
21153 static int
21154eval_fname_sid(p)
21155 char_u *p;
21156{
21157 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21158}
21159
21160/*
21161 * List the head of the function: "name(arg1, arg2)".
21162 */
21163 static void
21164list_func_head(fp, indent)
21165 ufunc_T *fp;
21166 int indent;
21167{
21168 int j;
21169
21170 msg_start();
21171 if (indent)
21172 MSG_PUTS(" ");
21173 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021174 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 {
21176 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021177 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 }
21179 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021180 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021182 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183 {
21184 if (j)
21185 MSG_PUTS(", ");
21186 msg_puts(FUNCARG(fp, j));
21187 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021188 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021189 {
21190 if (j)
21191 MSG_PUTS(", ");
21192 MSG_PUTS("...");
21193 }
21194 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021195 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021196 if (p_verbose > 0)
21197 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198}
21199
21200/*
21201 * Find a function by name, return pointer to it in ufuncs.
21202 * Return NULL for unknown function.
21203 */
21204 static ufunc_T *
21205find_func(name)
21206 char_u *name;
21207{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021208 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021210 hi = hash_find(&func_hashtab, name);
21211 if (!HASHITEM_EMPTY(hi))
21212 return HI2UF(hi);
21213 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214}
21215
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021216#if defined(EXITFREE) || defined(PROTO)
21217 void
21218free_all_functions()
21219{
21220 hashitem_T *hi;
21221
21222 /* Need to start all over every time, because func_free() may change the
21223 * hash table. */
21224 while (func_hashtab.ht_used > 0)
21225 for (hi = func_hashtab.ht_array; ; ++hi)
21226 if (!HASHITEM_EMPTY(hi))
21227 {
21228 func_free(HI2UF(hi));
21229 break;
21230 }
21231}
21232#endif
21233
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021234/*
21235 * Return TRUE if a function "name" exists.
21236 */
21237 static int
21238function_exists(name)
21239 char_u *name;
21240{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021241 char_u *nm = name;
21242 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021243 int n = FALSE;
21244
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021245 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021246 nm = skipwhite(nm);
21247
21248 /* Only accept "funcname", "funcname ", "funcname (..." and
21249 * "funcname(...", not "funcname!...". */
21250 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021251 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021252 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021253 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021254 else
21255 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021256 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021257 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021258 return n;
21259}
21260
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021261/*
21262 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021263 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021264 */
21265 static int
21266builtin_function(name)
21267 char_u *name;
21268{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021269 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21270 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021271}
21272
Bram Moolenaar05159a02005-02-26 23:04:13 +000021273#if defined(FEAT_PROFILE) || defined(PROTO)
21274/*
21275 * Start profiling function "fp".
21276 */
21277 static void
21278func_do_profile(fp)
21279 ufunc_T *fp;
21280{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021281 int len = fp->uf_lines.ga_len;
21282
21283 if (len == 0)
21284 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021285 fp->uf_tm_count = 0;
21286 profile_zero(&fp->uf_tm_self);
21287 profile_zero(&fp->uf_tm_total);
21288 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021289 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021290 if (fp->uf_tml_total == NULL)
21291 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021292 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021293 if (fp->uf_tml_self == NULL)
21294 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021295 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021296 fp->uf_tml_idx = -1;
21297 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21298 || fp->uf_tml_self == NULL)
21299 return; /* out of memory */
21300
21301 fp->uf_profiling = TRUE;
21302}
21303
21304/*
21305 * Dump the profiling results for all functions in file "fd".
21306 */
21307 void
21308func_dump_profile(fd)
21309 FILE *fd;
21310{
21311 hashitem_T *hi;
21312 int todo;
21313 ufunc_T *fp;
21314 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021315 ufunc_T **sorttab;
21316 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021317
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021318 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021319 if (todo == 0)
21320 return; /* nothing to dump */
21321
Bram Moolenaar73830342005-02-28 22:48:19 +000021322 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21323
Bram Moolenaar05159a02005-02-26 23:04:13 +000021324 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21325 {
21326 if (!HASHITEM_EMPTY(hi))
21327 {
21328 --todo;
21329 fp = HI2UF(hi);
21330 if (fp->uf_profiling)
21331 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021332 if (sorttab != NULL)
21333 sorttab[st_len++] = fp;
21334
Bram Moolenaar05159a02005-02-26 23:04:13 +000021335 if (fp->uf_name[0] == K_SPECIAL)
21336 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21337 else
21338 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21339 if (fp->uf_tm_count == 1)
21340 fprintf(fd, "Called 1 time\n");
21341 else
21342 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21343 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21344 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21345 fprintf(fd, "\n");
21346 fprintf(fd, "count total (s) self (s)\n");
21347
21348 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21349 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021350 if (FUNCLINE(fp, i) == NULL)
21351 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021352 prof_func_line(fd, fp->uf_tml_count[i],
21353 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021354 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21355 }
21356 fprintf(fd, "\n");
21357 }
21358 }
21359 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021360
21361 if (sorttab != NULL && st_len > 0)
21362 {
21363 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21364 prof_total_cmp);
21365 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21366 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21367 prof_self_cmp);
21368 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21369 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021370
21371 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021372}
Bram Moolenaar73830342005-02-28 22:48:19 +000021373
21374 static void
21375prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21376 FILE *fd;
21377 ufunc_T **sorttab;
21378 int st_len;
21379 char *title;
21380 int prefer_self; /* when equal print only self time */
21381{
21382 int i;
21383 ufunc_T *fp;
21384
21385 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21386 fprintf(fd, "count total (s) self (s) function\n");
21387 for (i = 0; i < 20 && i < st_len; ++i)
21388 {
21389 fp = sorttab[i];
21390 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21391 prefer_self);
21392 if (fp->uf_name[0] == K_SPECIAL)
21393 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21394 else
21395 fprintf(fd, " %s()\n", fp->uf_name);
21396 }
21397 fprintf(fd, "\n");
21398}
21399
21400/*
21401 * Print the count and times for one function or function line.
21402 */
21403 static void
21404prof_func_line(fd, count, total, self, prefer_self)
21405 FILE *fd;
21406 int count;
21407 proftime_T *total;
21408 proftime_T *self;
21409 int prefer_self; /* when equal print only self time */
21410{
21411 if (count > 0)
21412 {
21413 fprintf(fd, "%5d ", count);
21414 if (prefer_self && profile_equal(total, self))
21415 fprintf(fd, " ");
21416 else
21417 fprintf(fd, "%s ", profile_msg(total));
21418 if (!prefer_self && profile_equal(total, self))
21419 fprintf(fd, " ");
21420 else
21421 fprintf(fd, "%s ", profile_msg(self));
21422 }
21423 else
21424 fprintf(fd, " ");
21425}
21426
21427/*
21428 * Compare function for total time sorting.
21429 */
21430 static int
21431#ifdef __BORLANDC__
21432_RTLENTRYF
21433#endif
21434prof_total_cmp(s1, s2)
21435 const void *s1;
21436 const void *s2;
21437{
21438 ufunc_T *p1, *p2;
21439
21440 p1 = *(ufunc_T **)s1;
21441 p2 = *(ufunc_T **)s2;
21442 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21443}
21444
21445/*
21446 * Compare function for self time sorting.
21447 */
21448 static int
21449#ifdef __BORLANDC__
21450_RTLENTRYF
21451#endif
21452prof_self_cmp(s1, s2)
21453 const void *s1;
21454 const void *s2;
21455{
21456 ufunc_T *p1, *p2;
21457
21458 p1 = *(ufunc_T **)s1;
21459 p2 = *(ufunc_T **)s2;
21460 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21461}
21462
Bram Moolenaar05159a02005-02-26 23:04:13 +000021463#endif
21464
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021465/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021466 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021467 * Return TRUE if a package was loaded.
21468 */
21469 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021470script_autoload(name, reload)
21471 char_u *name;
21472 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021473{
21474 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021475 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021476 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021477 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021478
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021479 /* Return quickly when autoload disabled. */
21480 if (no_autoload)
21481 return FALSE;
21482
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021483 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021484 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021485 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021486 return FALSE;
21487
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021488 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021489
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021490 /* Find the name in the list of previously loaded package names. Skip
21491 * "autoload/", it's always the same. */
21492 for (i = 0; i < ga_loaded.ga_len; ++i)
21493 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21494 break;
21495 if (!reload && i < ga_loaded.ga_len)
21496 ret = FALSE; /* was loaded already */
21497 else
21498 {
21499 /* Remember the name if it wasn't loaded already. */
21500 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21501 {
21502 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21503 tofree = NULL;
21504 }
21505
21506 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021507 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021508 ret = TRUE;
21509 }
21510
21511 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021512 return ret;
21513}
21514
21515/*
21516 * Return the autoload script name for a function or variable name.
21517 * Returns NULL when out of memory.
21518 */
21519 static char_u *
21520autoload_name(name)
21521 char_u *name;
21522{
21523 char_u *p;
21524 char_u *scriptname;
21525
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021526 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021527 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21528 if (scriptname == NULL)
21529 return FALSE;
21530 STRCPY(scriptname, "autoload/");
21531 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021532 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021533 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021534 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021535 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021536 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021537}
21538
Bram Moolenaar071d4272004-06-13 20:20:40 +000021539#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21540
21541/*
21542 * Function given to ExpandGeneric() to obtain the list of user defined
21543 * function names.
21544 */
21545 char_u *
21546get_user_func_name(xp, idx)
21547 expand_T *xp;
21548 int idx;
21549{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021550 static long_u done;
21551 static hashitem_T *hi;
21552 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553
21554 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021556 done = 0;
21557 hi = func_hashtab.ht_array;
21558 }
21559 if (done < func_hashtab.ht_used)
21560 {
21561 if (done++ > 0)
21562 ++hi;
21563 while (HASHITEM_EMPTY(hi))
21564 ++hi;
21565 fp = HI2UF(hi);
21566
21567 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21568 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569
21570 cat_func_name(IObuff, fp);
21571 if (xp->xp_context != EXPAND_USER_FUNC)
21572 {
21573 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021574 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 STRCAT(IObuff, ")");
21576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 return IObuff;
21578 }
21579 return NULL;
21580}
21581
21582#endif /* FEAT_CMDL_COMPL */
21583
21584/*
21585 * Copy the function name of "fp" to buffer "buf".
21586 * "buf" must be able to hold the function name plus three bytes.
21587 * Takes care of script-local function names.
21588 */
21589 static void
21590cat_func_name(buf, fp)
21591 char_u *buf;
21592 ufunc_T *fp;
21593{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021594 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 {
21596 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021597 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 }
21599 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021600 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601}
21602
21603/*
21604 * ":delfunction {name}"
21605 */
21606 void
21607ex_delfunction(eap)
21608 exarg_T *eap;
21609{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021610 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 char_u *p;
21612 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021613 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614
21615 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021616 name = trans_function_name(&p, eap->skip, 0, &fudi);
21617 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021619 {
21620 if (fudi.fd_dict != NULL && !eap->skip)
21621 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 if (!ends_excmd(*skipwhite(p)))
21625 {
21626 vim_free(name);
21627 EMSG(_(e_trailing));
21628 return;
21629 }
21630 eap->nextcmd = check_nextcmd(p);
21631 if (eap->nextcmd != NULL)
21632 *p = NUL;
21633
21634 if (!eap->skip)
21635 fp = find_func(name);
21636 vim_free(name);
21637
21638 if (!eap->skip)
21639 {
21640 if (fp == NULL)
21641 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021642 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 return;
21644 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021645 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646 {
21647 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21648 return;
21649 }
21650
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021653 /* Delete the dict item that refers to the function, it will
21654 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021655 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021657 else
21658 func_free(fp);
21659 }
21660}
21661
21662/*
21663 * Free a function and remove it from the list of functions.
21664 */
21665 static void
21666func_free(fp)
21667 ufunc_T *fp;
21668{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021669 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021670
21671 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021672 ga_clear_strings(&(fp->uf_args));
21673 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021674#ifdef FEAT_PROFILE
21675 vim_free(fp->uf_tml_count);
21676 vim_free(fp->uf_tml_total);
21677 vim_free(fp->uf_tml_self);
21678#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021679
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021680 /* remove the function from the function hashtable */
21681 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21682 if (HASHITEM_EMPTY(hi))
21683 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021684 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021685 hash_remove(&func_hashtab, hi);
21686
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021687 vim_free(fp);
21688}
21689
21690/*
21691 * Unreference a Function: decrement the reference count and free it when it
21692 * becomes zero. Only for numbered functions.
21693 */
21694 static void
21695func_unref(name)
21696 char_u *name;
21697{
21698 ufunc_T *fp;
21699
21700 if (name != NULL && isdigit(*name))
21701 {
21702 fp = find_func(name);
21703 if (fp == NULL)
21704 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021705 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021706 {
21707 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021708 * when "uf_calls" becomes zero. */
21709 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021710 func_free(fp);
21711 }
21712 }
21713}
21714
21715/*
21716 * Count a reference to a Function.
21717 */
21718 static void
21719func_ref(name)
21720 char_u *name;
21721{
21722 ufunc_T *fp;
21723
21724 if (name != NULL && isdigit(*name))
21725 {
21726 fp = find_func(name);
21727 if (fp == NULL)
21728 EMSG2(_(e_intern2), "func_ref()");
21729 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021730 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731 }
21732}
21733
21734/*
21735 * Call a user function.
21736 */
21737 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021738call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021739 ufunc_T *fp; /* pointer to function */
21740 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021741 typval_T *argvars; /* arguments */
21742 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 linenr_T firstline; /* first line of range */
21744 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021745 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746{
Bram Moolenaar33570922005-01-25 22:26:29 +000021747 char_u *save_sourcing_name;
21748 linenr_T save_sourcing_lnum;
21749 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021750 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021751 int save_did_emsg;
21752 static int depth = 0;
21753 dictitem_T *v;
21754 int fixvar_idx = 0; /* index in fixvar[] */
21755 int i;
21756 int ai;
21757 char_u numbuf[NUMBUFLEN];
21758 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021759#ifdef FEAT_PROFILE
21760 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021761 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763
21764 /* If depth of calling is getting too high, don't execute the function */
21765 if (depth >= p_mfd)
21766 {
21767 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021768 rettv->v_type = VAR_NUMBER;
21769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770 return;
21771 }
21772 ++depth;
21773
21774 line_breakcheck(); /* check for CTRL-C hit */
21775
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021776 fc = (funccall_T *)alloc(sizeof(funccall_T));
21777 fc->caller = current_funccal;
21778 current_funccal = fc;
21779 fc->func = fp;
21780 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021781 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021782 fc->linenr = 0;
21783 fc->returned = FALSE;
21784 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021786 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21787 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021788
Bram Moolenaar33570922005-01-25 22:26:29 +000021789 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021790 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021791 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21792 * each argument variable and saves a lot of time.
21793 */
21794 /*
21795 * Init l: variables.
21796 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021797 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021798 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021799 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021800 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21801 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021802 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021803 name = v->di_key;
21804 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021805 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021806 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021807 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021808 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021809 v->di_tv.vval.v_dict = selfdict;
21810 ++selfdict->dv_refcount;
21811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021812
Bram Moolenaar33570922005-01-25 22:26:29 +000021813 /*
21814 * Init a: variables.
21815 * Set a:0 to "argcount".
21816 * Set a:000 to a list with room for the "..." arguments.
21817 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021818 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21819 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021820 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021821 /* Use "name" to avoid a warning from some compiler that checks the
21822 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021823 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021824 name = v->di_key;
21825 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021826 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021827 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021828 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021829 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021830 v->di_tv.vval.v_list = &fc->l_varlist;
21831 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21832 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21833 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021834
21835 /*
21836 * Set a:firstline to "firstline" and a:lastline to "lastline".
21837 * Set a:name to named arguments.
21838 * Set a:N to the "..." arguments.
21839 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021840 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021841 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021842 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 (varnumber_T)lastline);
21844 for (i = 0; i < argcount; ++i)
21845 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021846 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021847 if (ai < 0)
21848 /* named argument a:name */
21849 name = FUNCARG(fp, i);
21850 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021851 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021852 /* "..." argument a:1, a:2, etc. */
21853 sprintf((char *)numbuf, "%d", ai + 1);
21854 name = numbuf;
21855 }
21856 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21857 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021858 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021859 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21860 }
21861 else
21862 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021863 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21864 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021865 if (v == NULL)
21866 break;
21867 v->di_flags = DI_FLAGS_RO;
21868 }
21869 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021870 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021871
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021872 /* Note: the values are copied directly to avoid alloc/free.
21873 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021874 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021875 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021876
21877 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21878 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021879 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21880 fc->l_listitems[ai].li_tv = argvars[i];
21881 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021882 }
21883 }
21884
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 /* Don't redraw while executing the function. */
21886 ++RedrawingDisabled;
21887 save_sourcing_name = sourcing_name;
21888 save_sourcing_lnum = sourcing_lnum;
21889 sourcing_lnum = 1;
21890 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021891 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 if (sourcing_name != NULL)
21893 {
21894 if (save_sourcing_name != NULL
21895 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21896 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21897 else
21898 STRCPY(sourcing_name, "function ");
21899 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21900
21901 if (p_verbose >= 12)
21902 {
21903 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021904 verbose_enter_scroll();
21905
Bram Moolenaar555b2802005-05-19 21:08:39 +000021906 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907 if (p_verbose >= 14)
21908 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021910 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021911 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021912 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913
21914 msg_puts((char_u *)"(");
21915 for (i = 0; i < argcount; ++i)
21916 {
21917 if (i > 0)
21918 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021919 if (argvars[i].v_type == VAR_NUMBER)
21920 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 else
21922 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021923 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21924 if (s != NULL)
21925 {
21926 trunc_string(s, buf, MSG_BUF_CLEN);
21927 msg_puts(buf);
21928 vim_free(tofree);
21929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 }
21931 }
21932 msg_puts((char_u *)")");
21933 }
21934 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021935
21936 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 --no_wait_return;
21938 }
21939 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021940#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021941 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021942 {
21943 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21944 func_do_profile(fp);
21945 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021946 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021947 {
21948 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021949 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021950 profile_zero(&fp->uf_tm_children);
21951 }
21952 script_prof_save(&wait_start);
21953 }
21954#endif
21955
Bram Moolenaar071d4272004-06-13 20:20:40 +000021956 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021957 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958 save_did_emsg = did_emsg;
21959 did_emsg = FALSE;
21960
21961 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021962 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21964
21965 --RedrawingDisabled;
21966
21967 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021968 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021970 clear_tv(rettv);
21971 rettv->v_type = VAR_NUMBER;
21972 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 }
21974
Bram Moolenaar05159a02005-02-26 23:04:13 +000021975#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021976 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021977 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021978 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021979 profile_end(&call_start);
21980 profile_sub_wait(&wait_start, &call_start);
21981 profile_add(&fp->uf_tm_total, &call_start);
21982 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021983 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021984 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021985 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21986 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021987 }
21988 }
21989#endif
21990
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 /* when being verbose, mention the return value */
21992 if (p_verbose >= 12)
21993 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021995 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021998 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021999 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022000 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022001 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022004 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022005 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022006 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022007 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022008
Bram Moolenaar555b2802005-05-19 21:08:39 +000022009 /* The value may be very long. Skip the middle part, so that we
22010 * have some idea how it starts and ends. smsg() would always
22011 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022012 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022013 if (s != NULL)
22014 {
22015 trunc_string(s, buf, MSG_BUF_CLEN);
22016 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22017 vim_free(tofree);
22018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 }
22020 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022021
22022 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022023 --no_wait_return;
22024 }
22025
22026 vim_free(sourcing_name);
22027 sourcing_name = save_sourcing_name;
22028 sourcing_lnum = save_sourcing_lnum;
22029 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022030#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022031 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022032 script_prof_restore(&wait_start);
22033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034
22035 if (p_verbose >= 12 && sourcing_name != NULL)
22036 {
22037 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022038 verbose_enter_scroll();
22039
Bram Moolenaar555b2802005-05-19 21:08:39 +000022040 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022042
22043 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 --no_wait_return;
22045 }
22046
22047 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022048 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022050
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022051 /* If the a:000 list and the l: and a: dicts are not referenced we can
22052 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022053 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22054 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22055 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22056 {
22057 free_funccal(fc, FALSE);
22058 }
22059 else
22060 {
22061 hashitem_T *hi;
22062 listitem_T *li;
22063 int todo;
22064
22065 /* "fc" is still in use. This can happen when returning "a:000" or
22066 * assigning "l:" to a global variable.
22067 * Link "fc" in the list for garbage collection later. */
22068 fc->caller = previous_funccal;
22069 previous_funccal = fc;
22070
22071 /* Make a copy of the a: variables, since we didn't do that above. */
22072 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22073 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22074 {
22075 if (!HASHITEM_EMPTY(hi))
22076 {
22077 --todo;
22078 v = HI2DI(hi);
22079 copy_tv(&v->di_tv, &v->di_tv);
22080 }
22081 }
22082
22083 /* Make a copy of the a:000 items, since we didn't do that above. */
22084 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22085 copy_tv(&li->li_tv, &li->li_tv);
22086 }
22087}
22088
22089/*
22090 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022091 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022092 */
22093 static int
22094can_free_funccal(fc, copyID)
22095 funccall_T *fc;
22096 int copyID;
22097{
22098 return (fc->l_varlist.lv_copyID != copyID
22099 && fc->l_vars.dv_copyID != copyID
22100 && fc->l_avars.dv_copyID != copyID);
22101}
22102
22103/*
22104 * Free "fc" and what it contains.
22105 */
22106 static void
22107free_funccal(fc, free_val)
22108 funccall_T *fc;
22109 int free_val; /* a: vars were allocated */
22110{
22111 listitem_T *li;
22112
22113 /* The a: variables typevals may not have been allocated, only free the
22114 * allocated variables. */
22115 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22116
22117 /* free all l: variables */
22118 vars_clear(&fc->l_vars.dv_hashtab);
22119
22120 /* Free the a:000 variables if they were allocated. */
22121 if (free_val)
22122 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22123 clear_tv(&li->li_tv);
22124
22125 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126}
22127
22128/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022129 * Add a number variable "name" to dict "dp" with value "nr".
22130 */
22131 static void
22132add_nr_var(dp, v, name, nr)
22133 dict_T *dp;
22134 dictitem_T *v;
22135 char *name;
22136 varnumber_T nr;
22137{
22138 STRCPY(v->di_key, name);
22139 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22140 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22141 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022142 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022143 v->di_tv.vval.v_number = nr;
22144}
22145
22146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147 * ":return [expr]"
22148 */
22149 void
22150ex_return(eap)
22151 exarg_T *eap;
22152{
22153 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022154 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 int returning = FALSE;
22156
22157 if (current_funccal == NULL)
22158 {
22159 EMSG(_("E133: :return not inside a function"));
22160 return;
22161 }
22162
22163 if (eap->skip)
22164 ++emsg_skip;
22165
22166 eap->nextcmd = NULL;
22167 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022168 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 {
22170 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022171 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022173 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 }
22175 /* It's safer to return also on error. */
22176 else if (!eap->skip)
22177 {
22178 /*
22179 * Return unless the expression evaluation has been cancelled due to an
22180 * aborting error, an interrupt, or an exception.
22181 */
22182 if (!aborting())
22183 returning = do_return(eap, FALSE, TRUE, NULL);
22184 }
22185
22186 /* When skipping or the return gets pending, advance to the next command
22187 * in this line (!returning). Otherwise, ignore the rest of the line.
22188 * Following lines will be ignored by get_func_line(). */
22189 if (returning)
22190 eap->nextcmd = NULL;
22191 else if (eap->nextcmd == NULL) /* no argument */
22192 eap->nextcmd = check_nextcmd(arg);
22193
22194 if (eap->skip)
22195 --emsg_skip;
22196}
22197
22198/*
22199 * Return from a function. Possibly makes the return pending. Also called
22200 * for a pending return at the ":endtry" or after returning from an extra
22201 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022202 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022203 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022204 * FALSE when the return gets pending.
22205 */
22206 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022207do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208 exarg_T *eap;
22209 int reanimate;
22210 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022211 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212{
22213 int idx;
22214 struct condstack *cstack = eap->cstack;
22215
22216 if (reanimate)
22217 /* Undo the return. */
22218 current_funccal->returned = FALSE;
22219
22220 /*
22221 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22222 * not in its finally clause (which then is to be executed next) is found.
22223 * In this case, make the ":return" pending for execution at the ":endtry".
22224 * Otherwise, return normally.
22225 */
22226 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22227 if (idx >= 0)
22228 {
22229 cstack->cs_pending[idx] = CSTP_RETURN;
22230
22231 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022232 /* A pending return again gets pending. "rettv" points to an
22233 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022234 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022235 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022236 else
22237 {
22238 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022239 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022241 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022243 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 {
22245 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022246 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022247 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022248 else
22249 EMSG(_(e_outofmem));
22250 }
22251 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022252 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253
22254 if (reanimate)
22255 {
22256 /* The pending return value could be overwritten by a ":return"
22257 * without argument in a finally clause; reset the default
22258 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022259 current_funccal->rettv->v_type = VAR_NUMBER;
22260 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 }
22262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022263 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022264 }
22265 else
22266 {
22267 current_funccal->returned = TRUE;
22268
22269 /* If the return is carried out now, store the return value. For
22270 * a return immediately after reanimation, the value is already
22271 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022272 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022274 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022275 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022277 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 }
22279 }
22280
22281 return idx < 0;
22282}
22283
22284/*
22285 * Free the variable with a pending return value.
22286 */
22287 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022288discard_pending_return(rettv)
22289 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022290{
Bram Moolenaar33570922005-01-25 22:26:29 +000022291 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292}
22293
22294/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022295 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 * is an allocated string. Used by report_pending() for verbose messages.
22297 */
22298 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022299get_return_cmd(rettv)
22300 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022301{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022302 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022303 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022304 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022306 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022307 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022308 if (s == NULL)
22309 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022310
22311 STRCPY(IObuff, ":return ");
22312 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22313 if (STRLEN(s) + 8 >= IOSIZE)
22314 STRCPY(IObuff + IOSIZE - 4, "...");
22315 vim_free(tofree);
22316 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317}
22318
22319/*
22320 * Get next function line.
22321 * Called by do_cmdline() to get the next line.
22322 * Returns allocated string, or NULL for end of function.
22323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 char_u *
22325get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022326 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022328 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329{
Bram Moolenaar33570922005-01-25 22:26:29 +000022330 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022331 ufunc_T *fp = fcp->func;
22332 char_u *retval;
22333 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334
22335 /* If breakpoints have been added/deleted need to check for it. */
22336 if (fcp->dbg_tick != debug_tick)
22337 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022338 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339 sourcing_lnum);
22340 fcp->dbg_tick = debug_tick;
22341 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022342#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022343 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022344 func_line_end(cookie);
22345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346
Bram Moolenaar05159a02005-02-26 23:04:13 +000022347 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022348 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22349 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 retval = NULL;
22351 else
22352 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022353 /* Skip NULL lines (continuation lines). */
22354 while (fcp->linenr < gap->ga_len
22355 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22356 ++fcp->linenr;
22357 if (fcp->linenr >= gap->ga_len)
22358 retval = NULL;
22359 else
22360 {
22361 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22362 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022363#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022364 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022365 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022366#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 }
22369
22370 /* Did we encounter a breakpoint? */
22371 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22372 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022373 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022374 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022375 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 sourcing_lnum);
22377 fcp->dbg_tick = debug_tick;
22378 }
22379
22380 return retval;
22381}
22382
Bram Moolenaar05159a02005-02-26 23:04:13 +000022383#if defined(FEAT_PROFILE) || defined(PROTO)
22384/*
22385 * Called when starting to read a function line.
22386 * "sourcing_lnum" must be correct!
22387 * When skipping lines it may not actually be executed, but we won't find out
22388 * until later and we need to store the time now.
22389 */
22390 void
22391func_line_start(cookie)
22392 void *cookie;
22393{
22394 funccall_T *fcp = (funccall_T *)cookie;
22395 ufunc_T *fp = fcp->func;
22396
22397 if (fp->uf_profiling && sourcing_lnum >= 1
22398 && sourcing_lnum <= fp->uf_lines.ga_len)
22399 {
22400 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022401 /* Skip continuation lines. */
22402 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22403 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022404 fp->uf_tml_execed = FALSE;
22405 profile_start(&fp->uf_tml_start);
22406 profile_zero(&fp->uf_tml_children);
22407 profile_get_wait(&fp->uf_tml_wait);
22408 }
22409}
22410
22411/*
22412 * Called when actually executing a function line.
22413 */
22414 void
22415func_line_exec(cookie)
22416 void *cookie;
22417{
22418 funccall_T *fcp = (funccall_T *)cookie;
22419 ufunc_T *fp = fcp->func;
22420
22421 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22422 fp->uf_tml_execed = TRUE;
22423}
22424
22425/*
22426 * Called when done with a function line.
22427 */
22428 void
22429func_line_end(cookie)
22430 void *cookie;
22431{
22432 funccall_T *fcp = (funccall_T *)cookie;
22433 ufunc_T *fp = fcp->func;
22434
22435 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22436 {
22437 if (fp->uf_tml_execed)
22438 {
22439 ++fp->uf_tml_count[fp->uf_tml_idx];
22440 profile_end(&fp->uf_tml_start);
22441 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022442 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022443 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22444 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022445 }
22446 fp->uf_tml_idx = -1;
22447 }
22448}
22449#endif
22450
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451/*
22452 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022453 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 */
22455 int
22456func_has_ended(cookie)
22457 void *cookie;
22458{
Bram Moolenaar33570922005-01-25 22:26:29 +000022459 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460
22461 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22462 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022463 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 || fcp->returned);
22465}
22466
22467/*
22468 * return TRUE if cookie indicates a function which "abort"s on errors.
22469 */
22470 int
22471func_has_abort(cookie)
22472 void *cookie;
22473{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022474 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475}
22476
22477#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22478typedef enum
22479{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022480 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22481 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22482 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483} var_flavour_T;
22484
22485static var_flavour_T var_flavour __ARGS((char_u *varname));
22486
22487 static var_flavour_T
22488var_flavour(varname)
22489 char_u *varname;
22490{
22491 char_u *p = varname;
22492
22493 if (ASCII_ISUPPER(*p))
22494 {
22495 while (*(++p))
22496 if (ASCII_ISLOWER(*p))
22497 return VAR_FLAVOUR_SESSION;
22498 return VAR_FLAVOUR_VIMINFO;
22499 }
22500 else
22501 return VAR_FLAVOUR_DEFAULT;
22502}
22503#endif
22504
22505#if defined(FEAT_VIMINFO) || defined(PROTO)
22506/*
22507 * Restore global vars that start with a capital from the viminfo file
22508 */
22509 int
22510read_viminfo_varlist(virp, writing)
22511 vir_T *virp;
22512 int writing;
22513{
22514 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022515 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022516 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517
22518 if (!writing && (find_viminfo_parameter('!') != NULL))
22519 {
22520 tab = vim_strchr(virp->vir_line + 1, '\t');
22521 if (tab != NULL)
22522 {
22523 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022524 switch (*tab)
22525 {
22526 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022527#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022528 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022529#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022530 case 'D': type = VAR_DICT; break;
22531 case 'L': type = VAR_LIST; break;
22532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533
22534 tab = vim_strchr(tab, '\t');
22535 if (tab != NULL)
22536 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022537 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022538 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022539 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022540 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022541#ifdef FEAT_FLOAT
22542 else if (type == VAR_FLOAT)
22543 (void)string2float(tab + 1, &tv.vval.v_float);
22544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022545 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022546 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022547 if (type == VAR_DICT || type == VAR_LIST)
22548 {
22549 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22550
22551 if (etv == NULL)
22552 /* Failed to parse back the dict or list, use it as a
22553 * string. */
22554 tv.v_type = VAR_STRING;
22555 else
22556 {
22557 vim_free(tv.vval.v_string);
22558 tv = *etv;
22559 }
22560 }
22561
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022562 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022563
22564 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022565 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022566 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22567 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 }
22569 }
22570 }
22571
22572 return viminfo_readline(virp);
22573}
22574
22575/*
22576 * Write global vars that start with a capital to the viminfo file
22577 */
22578 void
22579write_viminfo_varlist(fp)
22580 FILE *fp;
22581{
Bram Moolenaar33570922005-01-25 22:26:29 +000022582 hashitem_T *hi;
22583 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022584 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022585 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022586 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022587 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022588 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589
22590 if (find_viminfo_parameter('!') == NULL)
22591 return;
22592
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022593 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022594
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022595 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022596 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022598 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022600 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022601 this_var = HI2DI(hi);
22602 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022603 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022604 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022605 {
22606 case VAR_STRING: s = "STR"; break;
22607 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022608#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022609 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022610#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022611 case VAR_DICT: s = "DIC"; break;
22612 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022613 default: continue;
22614 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022615 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022616 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022617 if (p != NULL)
22618 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022619 vim_free(tofree);
22620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621 }
22622 }
22623}
22624#endif
22625
22626#if defined(FEAT_SESSION) || defined(PROTO)
22627 int
22628store_session_globals(fd)
22629 FILE *fd;
22630{
Bram Moolenaar33570922005-01-25 22:26:29 +000022631 hashitem_T *hi;
22632 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022633 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 char_u *p, *t;
22635
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022636 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022637 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022639 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022641 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022642 this_var = HI2DI(hi);
22643 if ((this_var->di_tv.v_type == VAR_NUMBER
22644 || this_var->di_tv.v_type == VAR_STRING)
22645 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022646 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022647 /* Escape special characters with a backslash. Turn a LF and
22648 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022649 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022650 (char_u *)"\\\"\n\r");
22651 if (p == NULL) /* out of memory */
22652 break;
22653 for (t = p; *t != NUL; ++t)
22654 if (*t == '\n')
22655 *t = 'n';
22656 else if (*t == '\r')
22657 *t = 'r';
22658 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022659 this_var->di_key,
22660 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22661 : ' ',
22662 p,
22663 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22664 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022665 || put_eol(fd) == FAIL)
22666 {
22667 vim_free(p);
22668 return FAIL;
22669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 vim_free(p);
22671 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022672#ifdef FEAT_FLOAT
22673 else if (this_var->di_tv.v_type == VAR_FLOAT
22674 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22675 {
22676 float_T f = this_var->di_tv.vval.v_float;
22677 int sign = ' ';
22678
22679 if (f < 0)
22680 {
22681 f = -f;
22682 sign = '-';
22683 }
22684 if ((fprintf(fd, "let %s = %c&%f",
22685 this_var->di_key, sign, f) < 0)
22686 || put_eol(fd) == FAIL)
22687 return FAIL;
22688 }
22689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 }
22691 }
22692 return OK;
22693}
22694#endif
22695
Bram Moolenaar661b1822005-07-28 22:36:45 +000022696/*
22697 * Display script name where an item was last set.
22698 * Should only be invoked when 'verbose' is non-zero.
22699 */
22700 void
22701last_set_msg(scriptID)
22702 scid_T scriptID;
22703{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022704 char_u *p;
22705
Bram Moolenaar661b1822005-07-28 22:36:45 +000022706 if (scriptID != 0)
22707 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022708 p = home_replace_save(NULL, get_scriptname(scriptID));
22709 if (p != NULL)
22710 {
22711 verbose_enter();
22712 MSG_PUTS(_("\n\tLast set from "));
22713 MSG_PUTS(p);
22714 vim_free(p);
22715 verbose_leave();
22716 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022717 }
22718}
22719
Bram Moolenaard812df62008-11-09 12:46:09 +000022720/*
22721 * List v:oldfiles in a nice way.
22722 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022723 void
22724ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022725 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022726{
22727 list_T *l = vimvars[VV_OLDFILES].vv_list;
22728 listitem_T *li;
22729 int nr = 0;
22730
22731 if (l == NULL)
22732 msg((char_u *)_("No old files"));
22733 else
22734 {
22735 msg_start();
22736 msg_scroll = TRUE;
22737 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22738 {
22739 msg_outnum((long)++nr);
22740 MSG_PUTS(": ");
22741 msg_outtrans(get_tv_string(&li->li_tv));
22742 msg_putchar('\n');
22743 out_flush(); /* output one line at a time */
22744 ui_breakcheck();
22745 }
22746 /* Assume "got_int" was set to truncate the listing. */
22747 got_int = FALSE;
22748
22749#ifdef FEAT_BROWSE_CMD
22750 if (cmdmod.browse)
22751 {
22752 quit_more = FALSE;
22753 nr = prompt_for_number(FALSE);
22754 msg_starthere();
22755 if (nr > 0)
22756 {
22757 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22758 (long)nr);
22759
22760 if (p != NULL)
22761 {
22762 p = expand_env_save(p);
22763 eap->arg = p;
22764 eap->cmdidx = CMD_edit;
22765 cmdmod.browse = FALSE;
22766 do_exedit(eap, NULL);
22767 vim_free(p);
22768 }
22769 }
22770 }
22771#endif
22772 }
22773}
22774
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775#endif /* FEAT_EVAL */
22776
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022778#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779
22780#ifdef WIN3264
22781/*
22782 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22783 */
22784static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22785static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22786static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22787
22788/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022789 * Get the short path (8.3) for the filename in "fnamep".
22790 * Only works for a valid file name.
22791 * When the path gets longer "fnamep" is changed and the allocated buffer
22792 * is put in "bufp".
22793 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22794 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 */
22796 static int
22797get_short_pathname(fnamep, bufp, fnamelen)
22798 char_u **fnamep;
22799 char_u **bufp;
22800 int *fnamelen;
22801{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022802 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 char_u *newbuf;
22804
22805 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 l = GetShortPathName(*fnamep, *fnamep, len);
22807 if (l > len - 1)
22808 {
22809 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022810 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 newbuf = vim_strnsave(*fnamep, l);
22812 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022813 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814
22815 vim_free(*bufp);
22816 *fnamep = *bufp = newbuf;
22817
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022818 /* Really should always succeed, as the buffer is big enough. */
22819 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 }
22821
22822 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022823 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824}
22825
22826/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022827 * Get the short path (8.3) for the filename in "fname". The converted
22828 * path is returned in "bufp".
22829 *
22830 * Some of the directories specified in "fname" may not exist. This function
22831 * will shorten the existing directories at the beginning of the path and then
22832 * append the remaining non-existing path.
22833 *
22834 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022835 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022836 * bufp - Pointer to an allocated buffer for the filename.
22837 * fnamelen - Length of the filename pointed to by fname
22838 *
22839 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840 */
22841 static int
22842shortpath_for_invalid_fname(fname, bufp, fnamelen)
22843 char_u **fname;
22844 char_u **bufp;
22845 int *fnamelen;
22846{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022847 char_u *short_fname, *save_fname, *pbuf_unused;
22848 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022850 int old_len, len;
22851 int new_len, sfx_len;
22852 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853
22854 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022855 old_len = *fnamelen;
22856 save_fname = vim_strnsave(*fname, old_len);
22857 pbuf_unused = NULL;
22858 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022860 endp = save_fname + old_len - 1; /* Find the end of the copy */
22861 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022863 /*
22864 * Try shortening the supplied path till it succeeds by removing one
22865 * directory at a time from the tail of the path.
22866 */
22867 len = 0;
22868 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022870 /* go back one path-separator */
22871 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22872 --endp;
22873 if (endp <= save_fname)
22874 break; /* processed the complete path */
22875
22876 /*
22877 * Replace the path separator with a NUL and try to shorten the
22878 * resulting path.
22879 */
22880 ch = *endp;
22881 *endp = 0;
22882 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022883 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022884 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22885 {
22886 retval = FAIL;
22887 goto theend;
22888 }
22889 *endp = ch; /* preserve the string */
22890
22891 if (len > 0)
22892 break; /* successfully shortened the path */
22893
22894 /* failed to shorten the path. Skip the path separator */
22895 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 }
22897
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022898 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022900 /*
22901 * Succeeded in shortening the path. Now concatenate the shortened
22902 * path with the remaining path at the tail.
22903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022905 /* Compute the length of the new path. */
22906 sfx_len = (int)(save_endp - endp) + 1;
22907 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022909 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022911 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022913 /* There is not enough space in the currently allocated string,
22914 * copy it to a buffer big enough. */
22915 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022916 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022917 {
22918 retval = FAIL;
22919 goto theend;
22920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 }
22922 else
22923 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022924 /* Transfer short_fname to the main buffer (it's big enough),
22925 * unless get_short_pathname() did its work in-place. */
22926 *fname = *bufp = save_fname;
22927 if (short_fname != save_fname)
22928 vim_strncpy(save_fname, short_fname, len);
22929 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022931
22932 /* concat the not-shortened part of the path */
22933 vim_strncpy(*fname + len, endp, sfx_len);
22934 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022936
22937theend:
22938 vim_free(pbuf_unused);
22939 vim_free(save_fname);
22940
22941 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942}
22943
22944/*
22945 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022946 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 */
22948 static int
22949shortpath_for_partial(fnamep, bufp, fnamelen)
22950 char_u **fnamep;
22951 char_u **bufp;
22952 int *fnamelen;
22953{
22954 int sepcount, len, tflen;
22955 char_u *p;
22956 char_u *pbuf, *tfname;
22957 int hasTilde;
22958
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022959 /* Count up the path separators from the RHS.. so we know which part
22960 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022961 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022962 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963 if (vim_ispathsep(*p))
22964 ++sepcount;
22965
22966 /* Need full path first (use expand_env() to remove a "~/") */
22967 hasTilde = (**fnamep == '~');
22968 if (hasTilde)
22969 pbuf = tfname = expand_env_save(*fnamep);
22970 else
22971 pbuf = tfname = FullName_save(*fnamep, FALSE);
22972
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022973 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022974
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022975 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22976 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977
22978 if (len == 0)
22979 {
22980 /* Don't have a valid filename, so shorten the rest of the
22981 * path if we can. This CAN give us invalid 8.3 filenames, but
22982 * there's not a lot of point in guessing what it might be.
22983 */
22984 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022985 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22986 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 }
22988
22989 /* Count the paths backward to find the beginning of the desired string. */
22990 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022991 {
22992#ifdef FEAT_MBYTE
22993 if (has_mbyte)
22994 p -= mb_head_off(tfname, p);
22995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 if (vim_ispathsep(*p))
22997 {
22998 if (sepcount == 0 || (hasTilde && sepcount == 1))
22999 break;
23000 else
23001 sepcount --;
23002 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 if (hasTilde)
23005 {
23006 --p;
23007 if (p >= tfname)
23008 *p = '~';
23009 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023010 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011 }
23012 else
23013 ++p;
23014
23015 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23016 vim_free(*bufp);
23017 *fnamelen = (int)STRLEN(p);
23018 *bufp = pbuf;
23019 *fnamep = p;
23020
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023021 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022}
23023#endif /* WIN3264 */
23024
23025/*
23026 * Adjust a filename, according to a string of modifiers.
23027 * *fnamep must be NUL terminated when called. When returning, the length is
23028 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023029 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030 * When there is an error, *fnamep is set to NULL.
23031 */
23032 int
23033modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23034 char_u *src; /* string with modifiers */
23035 int *usedlen; /* characters after src that are used */
23036 char_u **fnamep; /* file name so far */
23037 char_u **bufp; /* buffer for allocated file name or NULL */
23038 int *fnamelen; /* length of fnamep */
23039{
23040 int valid = 0;
23041 char_u *tail;
23042 char_u *s, *p, *pbuf;
23043 char_u dirname[MAXPATHL];
23044 int c;
23045 int has_fullname = 0;
23046#ifdef WIN3264
23047 int has_shortname = 0;
23048#endif
23049
23050repeat:
23051 /* ":p" - full path/file_name */
23052 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23053 {
23054 has_fullname = 1;
23055
23056 valid |= VALID_PATH;
23057 *usedlen += 2;
23058
23059 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23060 if ((*fnamep)[0] == '~'
23061#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23062 && ((*fnamep)[1] == '/'
23063# ifdef BACKSLASH_IN_FILENAME
23064 || (*fnamep)[1] == '\\'
23065# endif
23066 || (*fnamep)[1] == NUL)
23067
23068#endif
23069 )
23070 {
23071 *fnamep = expand_env_save(*fnamep);
23072 vim_free(*bufp); /* free any allocated file name */
23073 *bufp = *fnamep;
23074 if (*fnamep == NULL)
23075 return -1;
23076 }
23077
23078 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023079 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 {
23081 if (vim_ispathsep(*p)
23082 && p[1] == '.'
23083 && (p[2] == NUL
23084 || vim_ispathsep(p[2])
23085 || (p[2] == '.'
23086 && (p[3] == NUL || vim_ispathsep(p[3])))))
23087 break;
23088 }
23089
23090 /* FullName_save() is slow, don't use it when not needed. */
23091 if (*p != NUL || !vim_isAbsName(*fnamep))
23092 {
23093 *fnamep = FullName_save(*fnamep, *p != NUL);
23094 vim_free(*bufp); /* free any allocated file name */
23095 *bufp = *fnamep;
23096 if (*fnamep == NULL)
23097 return -1;
23098 }
23099
23100 /* Append a path separator to a directory. */
23101 if (mch_isdir(*fnamep))
23102 {
23103 /* Make room for one or two extra characters. */
23104 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23105 vim_free(*bufp); /* free any allocated file name */
23106 *bufp = *fnamep;
23107 if (*fnamep == NULL)
23108 return -1;
23109 add_pathsep(*fnamep);
23110 }
23111 }
23112
23113 /* ":." - path relative to the current directory */
23114 /* ":~" - path relative to the home directory */
23115 /* ":8" - shortname path - postponed till after */
23116 while (src[*usedlen] == ':'
23117 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23118 {
23119 *usedlen += 2;
23120 if (c == '8')
23121 {
23122#ifdef WIN3264
23123 has_shortname = 1; /* Postpone this. */
23124#endif
23125 continue;
23126 }
23127 pbuf = NULL;
23128 /* Need full path first (use expand_env() to remove a "~/") */
23129 if (!has_fullname)
23130 {
23131 if (c == '.' && **fnamep == '~')
23132 p = pbuf = expand_env_save(*fnamep);
23133 else
23134 p = pbuf = FullName_save(*fnamep, FALSE);
23135 }
23136 else
23137 p = *fnamep;
23138
23139 has_fullname = 0;
23140
23141 if (p != NULL)
23142 {
23143 if (c == '.')
23144 {
23145 mch_dirname(dirname, MAXPATHL);
23146 s = shorten_fname(p, dirname);
23147 if (s != NULL)
23148 {
23149 *fnamep = s;
23150 if (pbuf != NULL)
23151 {
23152 vim_free(*bufp); /* free any allocated file name */
23153 *bufp = pbuf;
23154 pbuf = NULL;
23155 }
23156 }
23157 }
23158 else
23159 {
23160 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23161 /* Only replace it when it starts with '~' */
23162 if (*dirname == '~')
23163 {
23164 s = vim_strsave(dirname);
23165 if (s != NULL)
23166 {
23167 *fnamep = s;
23168 vim_free(*bufp);
23169 *bufp = s;
23170 }
23171 }
23172 }
23173 vim_free(pbuf);
23174 }
23175 }
23176
23177 tail = gettail(*fnamep);
23178 *fnamelen = (int)STRLEN(*fnamep);
23179
23180 /* ":h" - head, remove "/file_name", can be repeated */
23181 /* Don't remove the first "/" or "c:\" */
23182 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23183 {
23184 valid |= VALID_HEAD;
23185 *usedlen += 2;
23186 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023187 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023188 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 *fnamelen = (int)(tail - *fnamep);
23190#ifdef VMS
23191 if (*fnamelen > 0)
23192 *fnamelen += 1; /* the path separator is part of the path */
23193#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023194 if (*fnamelen == 0)
23195 {
23196 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23197 p = vim_strsave((char_u *)".");
23198 if (p == NULL)
23199 return -1;
23200 vim_free(*bufp);
23201 *bufp = *fnamep = tail = p;
23202 *fnamelen = 1;
23203 }
23204 else
23205 {
23206 while (tail > s && !after_pathsep(s, tail))
23207 mb_ptr_back(*fnamep, tail);
23208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 }
23210
23211 /* ":8" - shortname */
23212 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23213 {
23214 *usedlen += 2;
23215#ifdef WIN3264
23216 has_shortname = 1;
23217#endif
23218 }
23219
23220#ifdef WIN3264
23221 /* Check shortname after we have done 'heads' and before we do 'tails'
23222 */
23223 if (has_shortname)
23224 {
23225 pbuf = NULL;
23226 /* Copy the string if it is shortened by :h */
23227 if (*fnamelen < (int)STRLEN(*fnamep))
23228 {
23229 p = vim_strnsave(*fnamep, *fnamelen);
23230 if (p == 0)
23231 return -1;
23232 vim_free(*bufp);
23233 *bufp = *fnamep = p;
23234 }
23235
23236 /* Split into two implementations - makes it easier. First is where
23237 * there isn't a full name already, second is where there is.
23238 */
23239 if (!has_fullname && !vim_isAbsName(*fnamep))
23240 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023241 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 return -1;
23243 }
23244 else
23245 {
23246 int l;
23247
23248 /* Simple case, already have the full-name
23249 * Nearly always shorter, so try first time. */
23250 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023251 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 return -1;
23253
23254 if (l == 0)
23255 {
23256 /* Couldn't find the filename.. search the paths.
23257 */
23258 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023259 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 return -1;
23261 }
23262 *fnamelen = l;
23263 }
23264 }
23265#endif /* WIN3264 */
23266
23267 /* ":t" - tail, just the basename */
23268 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23269 {
23270 *usedlen += 2;
23271 *fnamelen -= (int)(tail - *fnamep);
23272 *fnamep = tail;
23273 }
23274
23275 /* ":e" - extension, can be repeated */
23276 /* ":r" - root, without extension, can be repeated */
23277 while (src[*usedlen] == ':'
23278 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23279 {
23280 /* find a '.' in the tail:
23281 * - for second :e: before the current fname
23282 * - otherwise: The last '.'
23283 */
23284 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23285 s = *fnamep - 2;
23286 else
23287 s = *fnamep + *fnamelen - 1;
23288 for ( ; s > tail; --s)
23289 if (s[0] == '.')
23290 break;
23291 if (src[*usedlen + 1] == 'e') /* :e */
23292 {
23293 if (s > tail)
23294 {
23295 *fnamelen += (int)(*fnamep - (s + 1));
23296 *fnamep = s + 1;
23297#ifdef VMS
23298 /* cut version from the extension */
23299 s = *fnamep + *fnamelen - 1;
23300 for ( ; s > *fnamep; --s)
23301 if (s[0] == ';')
23302 break;
23303 if (s > *fnamep)
23304 *fnamelen = s - *fnamep;
23305#endif
23306 }
23307 else if (*fnamep <= tail)
23308 *fnamelen = 0;
23309 }
23310 else /* :r */
23311 {
23312 if (s > tail) /* remove one extension */
23313 *fnamelen = (int)(s - *fnamep);
23314 }
23315 *usedlen += 2;
23316 }
23317
23318 /* ":s?pat?foo?" - substitute */
23319 /* ":gs?pat?foo?" - global substitute */
23320 if (src[*usedlen] == ':'
23321 && (src[*usedlen + 1] == 's'
23322 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23323 {
23324 char_u *str;
23325 char_u *pat;
23326 char_u *sub;
23327 int sep;
23328 char_u *flags;
23329 int didit = FALSE;
23330
23331 flags = (char_u *)"";
23332 s = src + *usedlen + 2;
23333 if (src[*usedlen + 1] == 'g')
23334 {
23335 flags = (char_u *)"g";
23336 ++s;
23337 }
23338
23339 sep = *s++;
23340 if (sep)
23341 {
23342 /* find end of pattern */
23343 p = vim_strchr(s, sep);
23344 if (p != NULL)
23345 {
23346 pat = vim_strnsave(s, (int)(p - s));
23347 if (pat != NULL)
23348 {
23349 s = p + 1;
23350 /* find end of substitution */
23351 p = vim_strchr(s, sep);
23352 if (p != NULL)
23353 {
23354 sub = vim_strnsave(s, (int)(p - s));
23355 str = vim_strnsave(*fnamep, *fnamelen);
23356 if (sub != NULL && str != NULL)
23357 {
23358 *usedlen = (int)(p + 1 - src);
23359 s = do_string_sub(str, pat, sub, flags);
23360 if (s != NULL)
23361 {
23362 *fnamep = s;
23363 *fnamelen = (int)STRLEN(s);
23364 vim_free(*bufp);
23365 *bufp = s;
23366 didit = TRUE;
23367 }
23368 }
23369 vim_free(sub);
23370 vim_free(str);
23371 }
23372 vim_free(pat);
23373 }
23374 }
23375 /* after using ":s", repeat all the modifiers */
23376 if (didit)
23377 goto repeat;
23378 }
23379 }
23380
23381 return valid;
23382}
23383
23384/*
23385 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23386 * "flags" can be "g" to do a global substitute.
23387 * Returns an allocated string, NULL for error.
23388 */
23389 char_u *
23390do_string_sub(str, pat, sub, flags)
23391 char_u *str;
23392 char_u *pat;
23393 char_u *sub;
23394 char_u *flags;
23395{
23396 int sublen;
23397 regmatch_T regmatch;
23398 int i;
23399 int do_all;
23400 char_u *tail;
23401 garray_T ga;
23402 char_u *ret;
23403 char_u *save_cpo;
23404
23405 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23406 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023407 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408
23409 ga_init2(&ga, 1, 200);
23410
23411 do_all = (flags[0] == 'g');
23412
23413 regmatch.rm_ic = p_ic;
23414 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23415 if (regmatch.regprog != NULL)
23416 {
23417 tail = str;
23418 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23419 {
23420 /*
23421 * Get some space for a temporary buffer to do the substitution
23422 * into. It will contain:
23423 * - The text up to where the match is.
23424 * - The substituted text.
23425 * - The text after the match.
23426 */
23427 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23428 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23429 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23430 {
23431 ga_clear(&ga);
23432 break;
23433 }
23434
23435 /* copy the text up to where the match is */
23436 i = (int)(regmatch.startp[0] - tail);
23437 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23438 /* add the substituted text */
23439 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23440 + ga.ga_len + i, TRUE, TRUE, FALSE);
23441 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023442 /* avoid getting stuck on a match with an empty string */
23443 if (tail == regmatch.endp[0])
23444 {
23445 if (*tail == NUL)
23446 break;
23447 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23448 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 }
23450 else
23451 {
23452 tail = regmatch.endp[0];
23453 if (*tail == NUL)
23454 break;
23455 }
23456 if (!do_all)
23457 break;
23458 }
23459
23460 if (ga.ga_data != NULL)
23461 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23462
23463 vim_free(regmatch.regprog);
23464 }
23465
23466 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23467 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023468 if (p_cpo == empty_option)
23469 p_cpo = save_cpo;
23470 else
23471 /* Darn, evaluating {sub} expression changed the value. */
23472 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473
23474 return ret;
23475}
23476
23477#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */