blob: 0f306d46ffd6070deae9b4a60a62ae7683fe0e7f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
Bram Moolenaar314f11d2010-08-09 22:07:08 +020025#ifdef VMS
26# include <float.h>
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef MACOS
30# include <time.h> /* for time_t */
31#endif
32
Bram Moolenaar8c8de832008-06-24 22:58:06 +000033#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
34# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
36
Bram Moolenaar33570922005-01-25 22:26:29 +000037#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000039#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
40 be freed. */
41
Bram Moolenaar071d4272004-06-13 20:20:40 +000042/*
Bram Moolenaar33570922005-01-25 22:26:29 +000043 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
44 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000045 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
46 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
47 * HI2DI() converts a hashitem pointer to a dictitem pointer.
48 */
Bram Moolenaar33570922005-01-25 22:26:29 +000049static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000050#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000051#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000052#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000053
54/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000055 * Structure returned by get_lval() and used by set_var_lval().
56 * For a plain name:
57 * "name" points to the variable name.
58 * "exp_name" is NULL.
59 * "tv" is NULL
60 * For a magic braces name:
61 * "name" points to the expanded variable name.
62 * "exp_name" is non-NULL, to be freed later.
63 * "tv" is NULL
64 * For an index in a list:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the (first) list item value
68 * "li" points to the (first) list item
69 * "range", "n1", "n2" and "empty2" indicate what items are used.
70 * For an existing Dict item:
71 * "name" points to the (expanded) variable name.
72 * "exp_name" NULL or non-NULL, to be freed later.
73 * "tv" points to the dict item value
74 * "newkey" is NULL
75 * For a non-existing Dict item:
76 * "name" points to the (expanded) variable name.
77 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000078 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 * "newkey" is the key for the new item.
80 */
81typedef struct lval_S
82{
83 char_u *ll_name; /* start of variable name (can be NULL) */
84 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 isn't NULL it's the Dict to which to add
87 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 listitem_T *ll_li; /* The list item or NULL. */
89 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000090 int ll_range; /* TRUE when a [i:j] range was used */
91 long ll_n1; /* First index for list */
92 long ll_n2; /* Second index for list range */
93 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000094 dict_T *ll_dict; /* The Dictionary or NULL */
95 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000096 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000097} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000098
Bram Moolenaar8c711452005-01-14 21:53:12 +000099
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000100static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000101static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000102static char *e_undefvar = N_("E121: Undefined variable: %s");
103static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000105static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000106static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_listreq = N_("E714: List required");
108static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000109static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000110static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
111static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
112static char *e_funcdict = N_("E717: Dictionary entry already exists");
113static char *e_funcref = N_("E718: Funcref required");
114static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
115static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000116static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000117static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000119/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000120 * All user-defined global variables are stored in dictionary "globvardict".
121 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000123static dict_T globvardict;
124static dictitem_T globvars_var;
125#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128 * Old Vim variables such as "v:version" are also available without the "v:".
129 * Also in functions. We need a special hashtable for them.
130 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000131static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000132
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200133/* When using exists() don't auto-load a script. */
134static int no_autoload = FALSE;
135
Bram Moolenaar532c7802005-01-27 14:44:31 +0000136/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 * When recursively copying lists and dicts we need to remember which ones we
138 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140 */
141static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000142#define COPYID_INC 2
143#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000144
145/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000146 * Array to hold the hashtab with variables local to each sourced script.
147 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000149typedef struct
150{
151 dictitem_T sv_var;
152 dict_T sv_dict;
153} scriptvar_T;
154
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200155static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
156#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
157#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
159static int echo_attr = 0; /* attributes used for ":echo" */
160
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161/* Values for trans_function_name() argument: */
162#define TFN_INT 1 /* internal function name OK */
163#define TFN_QUIET 2 /* no error messages */
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000358 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000365};
366
367/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000368#define vv_type vv_di.di_tv.v_type
369#define vv_nr vv_di.di_tv.vval.v_number
370#define vv_float vv_di.di_tv.vval.v_float
371#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000372#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000373#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000374
375/*
376 * The v: variables are stored in dictionary "vimvardict".
377 * "vimvars_var" is the variable that is used for the "l:" scope.
378 */
379static dict_T vimvardict;
380static dictitem_T vimvars_var;
381#define vimvarht vimvardict.dv_hashtab
382
Bram Moolenaara40058a2005-07-11 22:42:07 +0000383static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
384static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
385#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
386static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
387#endif
388static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
389static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
390static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000391static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
392static void list_glob_vars __ARGS((int *first));
393static void list_buf_vars __ARGS((int *first));
394static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000395#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000396static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000398static void list_vim_vars __ARGS((int *first));
399static void list_script_vars __ARGS((int *first));
400static void list_func_vars __ARGS((int *first));
401static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000402static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
403static int check_changedtick __ARGS((char_u *arg));
404static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
405static void clear_lval __ARGS((lval_T *lp));
406static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
407static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
408static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
409static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
410static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
411static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
412static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
413static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
414static void item_lock __ARGS((typval_T *tv, int deep, int lock));
415static int tv_islocked __ARGS((typval_T *tv));
416
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
418static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
419static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000423static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
424static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000426static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000427static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
428static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
429static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
430static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000431static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000432static listitem_T *listitem_alloc __ARGS((void));
433static void listitem_free __ARGS((listitem_T *item));
434static void listitem_remove __ARGS((list_T *l, listitem_T *item));
435static long list_len __ARGS((list_T *l));
436static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
437static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
438static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000439static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000440static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000443static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
445static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
446static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000447static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000448static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000449static char_u *list2string __ARGS((typval_T *tv, int copyID));
450static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000451static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000452static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
453static void set_ref_in_list __ARGS((list_T *l, int copyID));
454static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200455static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000456static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000457static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
459static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000460static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000461static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000462static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000463static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000464static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
465static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000466static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#ifdef FEAT_FLOAT
468static int string2float __ARGS((char_u *text, float_T *value));
469#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000470static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
471static int find_internal_func __ARGS((char_u *name));
472static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
473static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200474static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000475static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000476static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000477
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000478#ifdef FEAT_FLOAT
479static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200480static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000482static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200488static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000489static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200490static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000503#ifdef FEAT_FLOAT
504static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
505#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000506static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000507static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000509static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000510static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000511#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000512static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000513static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
515#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000516static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000518#ifdef FEAT_FLOAT
519static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200520static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000521#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000522static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
525static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200536#ifdef FEAT_FLOAT
537static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
538#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000539static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000541static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000547#ifdef FEAT_FLOAT
548static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000551#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000552static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000553static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000561static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000562static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000563static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000564static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000569static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000570static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000577static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000578static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000579static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000580static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000581static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200583static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000584static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000592static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000606static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000607static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000612static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000613static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200625static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000626static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
627#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000632static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000633static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000634static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000637static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
638static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000640#ifdef vim_mkdir
641static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100644#ifdef FEAT_MZSCHEME
645static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
646#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000649static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000650#ifdef FEAT_FLOAT
651static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
652#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000653static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000654static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000655static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000657static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000658static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
659static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#ifdef FEAT_FLOAT
671static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
672#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000673static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000674static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000676static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
677static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
679static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000683static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000684static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000685static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000686static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200688static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000689static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000690static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000691static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#ifdef FEAT_FLOAT
694static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200695static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000696#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000698static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000699static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
700static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000701static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
703static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
704static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000706static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200707static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000708#ifdef HAVE_STRFTIME
709static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
710#endif
711static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200717static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200718static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000719static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000724static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200725static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000726static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000727static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000728static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000729static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000730static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000731static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000732static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000733static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200734#ifdef FEAT_FLOAT
735static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
737#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000738static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
739static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000741#ifdef FEAT_FLOAT
742static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
743#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200745static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200746static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000747static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000756static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
757static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000758static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000759static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000761static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000762static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000763static int get_env_len __ARGS((char_u **arg));
764static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000765static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000766static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
767#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
768#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
769 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000770static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000771static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000772static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000773static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
774static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000775static typval_T *alloc_tv __ARGS((void));
776static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000777static void init_tv __ARGS((typval_T *varp));
778static long get_tv_number __ARGS((typval_T *varp));
779static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000780static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static char_u *get_tv_string __ARGS((typval_T *varp));
782static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000783static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000784static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000785static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000786static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
787static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
788static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000789static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
790static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000791static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
792static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000793static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000794static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000795static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000796static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
797static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
798static int eval_fname_script __ARGS((char_u *p));
799static int eval_fname_sid __ARGS((char_u *p));
800static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000801static ufunc_T *find_func __ARGS((char_u *name));
802static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000803static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
805static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000806static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
807static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
808static int
809# ifdef __BORLANDC__
810 _RTLENTRYF
811# endif
812 prof_total_cmp __ARGS((const void *s1, const void *s2));
813static int
814# ifdef __BORLANDC__
815 _RTLENTRYF
816# endif
817 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000819static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000820static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000821static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void func_free __ARGS((ufunc_T *fp));
823static void func_unref __ARGS((char_u *name));
824static void func_ref __ARGS((char_u *name));
825static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000826static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
827static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000828static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000829static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
830static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000831static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000832static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000833static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200835
836#ifdef EBCDIC
837static int compare_func_name __ARGS((const void *s1, const void *s2));
838static void sortFunctions __ARGS(());
839#endif
840
841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000842/* Character used as separated in autoload function/variable names. */
843#define AUTOLOAD_CHAR '#'
844
Bram Moolenaar33570922005-01-25 22:26:29 +0000845/*
846 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000847 */
848 void
849eval_init()
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 int i;
852 struct vimvar *p;
853
854 init_var_dict(&globvardict, &globvars_var);
855 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000856 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000857 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
859 for (i = 0; i < VV_LEN; ++i)
860 {
861 p = &vimvars[i];
862 STRCPY(p->vv_di.di_key, p->vv_name);
863 if (p->vv_flags & VV_RO)
864 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
865 else if (p->vv_flags & VV_RO_SBX)
866 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
867 else
868 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000869
870 /* add to v: scope dict, unless the value is not always available */
871 if (p->vv_type != VAR_UNKNOWN)
872 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000873 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000874 /* add to compat scope dict */
875 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000876 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000877 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200878
879#ifdef EBCDIC
880 /*
881 * Sort the function table, to enable binary sort.
882 */
883 sortFunctions();
884#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000885}
886
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000887#if defined(EXITFREE) || defined(PROTO)
888 void
889eval_clear()
890{
891 int i;
892 struct vimvar *p;
893
894 for (i = 0; i < VV_LEN; ++i)
895 {
896 p = &vimvars[i];
897 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000898 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000899 vim_free(p->vv_str);
900 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000901 }
902 else if (p->vv_di.di_tv.v_type == VAR_LIST)
903 {
904 list_unref(p->vv_list);
905 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000906 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907 }
908 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000909 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000910 hash_clear(&compat_hashtab);
911
Bram Moolenaard9fba312005-06-26 22:34:35 +0000912 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000913
914 /* global variables */
915 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000916
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000917 /* autoloaded script names */
918 ga_clear_strings(&ga_loaded);
919
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200920 /* script-local variables */
921 for (i = 1; i <= ga_scripts.ga_len; ++i)
922 {
923 vars_clear(&SCRIPT_VARS(i));
924 vim_free(SCRIPT_SV(i));
925 }
926 ga_clear(&ga_scripts);
927
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000928 /* unreferenced lists and dicts */
929 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000930
931 /* functions */
932 free_all_functions();
933 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000934}
935#endif
936
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 * Return the name of the executed function.
939 */
940 char_u *
941func_name(cookie)
942 void *cookie;
943{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000944 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945}
946
947/*
948 * Return the address holding the next breakpoint line for a funccall cookie.
949 */
950 linenr_T *
951func_breakpoint(cookie)
952 void *cookie;
953{
Bram Moolenaar33570922005-01-25 22:26:29 +0000954 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955}
956
957/*
958 * Return the address holding the debug tick for a funccall cookie.
959 */
960 int *
961func_dbg_tick(cookie)
962 void *cookie;
963{
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965}
966
967/*
968 * Return the nesting level for a funccall cookie.
969 */
970 int
971func_level(cookie)
972 void *cookie;
973{
Bram Moolenaar33570922005-01-25 22:26:29 +0000974 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975}
976
977/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000978funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000980/* pointer to list of previously used funccal, still around because some
981 * item in it is still being used. */
982funccall_T *previous_funccal = NULL;
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984/*
985 * Return TRUE when a function was ended by a ":return" command.
986 */
987 int
988current_func_returned()
989{
990 return current_funccal->returned;
991}
992
993
994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 * Set an internal variable to a string value. Creates the variable if it does
996 * not already exist.
997 */
998 void
999set_internal_string_var(name, value)
1000 char_u *name;
1001 char_u *value;
1002{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001003 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001004 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
1006 val = vim_strsave(value);
1007 if (val != NULL)
1008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001012 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001013 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015 }
1016}
1017
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001019static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020static char_u *redir_endp = NULL;
1021static char_u *redir_varname = NULL;
1022
1023/*
1024 * Start recording command output to a variable
1025 * Returns OK if successfully completed the setup. FAIL otherwise.
1026 */
1027 int
1028var_redir_start(name, append)
1029 char_u *name;
1030 int append; /* append to an existing variable */
1031{
1032 int save_emsg;
1033 int err;
1034 typval_T tv;
1035
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001036 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001037 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001038 {
1039 EMSG(_(e_invarg));
1040 return FAIL;
1041 }
1042
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001043 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044 redir_varname = vim_strsave(name);
1045 if (redir_varname == NULL)
1046 return FAIL;
1047
1048 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1049 if (redir_lval == NULL)
1050 {
1051 var_redir_stop();
1052 return FAIL;
1053 }
1054
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001055 /* The output is stored in growarray "redir_ga" until redirection ends. */
1056 ga_init2(&redir_ga, (int)sizeof(char), 500);
1057
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001059 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1060 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1062 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001063 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 if (redir_endp != NULL && *redir_endp != NUL)
1065 /* Trailing characters are present after the variable name */
1066 EMSG(_(e_trailing));
1067 else
1068 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001069 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 var_redir_stop();
1071 return FAIL;
1072 }
1073
1074 /* check if we can write to the variable: set it to or append an empty
1075 * string */
1076 save_emsg = did_emsg;
1077 did_emsg = FALSE;
1078 tv.v_type = VAR_STRING;
1079 tv.vval.v_string = (char_u *)"";
1080 if (append)
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1082 else
1083 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001084 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001086 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001087 if (err)
1088 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001089 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001090 var_redir_stop();
1091 return FAIL;
1092 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001093
1094 return OK;
1095}
1096
1097/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001098 * Append "value[value_len]" to the variable set by var_redir_start().
1099 * The actual appending is postponed until redirection ends, because the value
1100 * appended may in fact be the string we write to, changing it may cause freed
1101 * memory to be used:
1102 * :redir => foo
1103 * :let foo
1104 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105 */
1106 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001111 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001112
1113 if (redir_lval == NULL)
1114 return;
1115
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001116 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001121 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122 {
1123 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001124 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001125 }
1126 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128}
1129
1130/*
1131 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001132 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 */
1134 void
1135var_redir_stop()
1136{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 typval_T tv;
1138
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 if (redir_lval != NULL)
1140 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001141 /* If there was no error: assign the text to the variable. */
1142 if (redir_endp != NULL)
1143 {
1144 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1145 tv.v_type = VAR_STRING;
1146 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001147 /* Call get_lval() again, if it's inside a Dict or List it may
1148 * have changed. */
1149 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1150 FALSE, FALSE, FALSE, FNE_CHECK_START);
1151 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1152 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1153 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001155
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001156 /* free the collected output */
1157 vim_free(redir_ga.ga_data);
1158 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001159
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 vim_free(redir_lval);
1161 redir_lval = NULL;
1162 }
1163 vim_free(redir_varname);
1164 redir_varname = NULL;
1165}
1166
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167# if defined(FEAT_MBYTE) || defined(PROTO)
1168 int
1169eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1170 char_u *enc_from;
1171 char_u *enc_to;
1172 char_u *fname_from;
1173 char_u *fname_to;
1174{
1175 int err = FALSE;
1176
1177 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1178 set_vim_var_string(VV_CC_TO, enc_to, -1);
1179 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1180 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1181 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1182 err = TRUE;
1183 set_vim_var_string(VV_CC_FROM, NULL, -1);
1184 set_vim_var_string(VV_CC_TO, NULL, -1);
1185 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1186 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1187
1188 if (err)
1189 return FAIL;
1190 return OK;
1191}
1192# endif
1193
1194# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1195 int
1196eval_printexpr(fname, args)
1197 char_u *fname;
1198 char_u *args;
1199{
1200 int err = FALSE;
1201
1202 set_vim_var_string(VV_FNAME_IN, fname, -1);
1203 set_vim_var_string(VV_CMDARG, args, -1);
1204 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1205 err = TRUE;
1206 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1207 set_vim_var_string(VV_CMDARG, NULL, -1);
1208
1209 if (err)
1210 {
1211 mch_remove(fname);
1212 return FAIL;
1213 }
1214 return OK;
1215}
1216# endif
1217
1218# if defined(FEAT_DIFF) || defined(PROTO)
1219 void
1220eval_diff(origfile, newfile, outfile)
1221 char_u *origfile;
1222 char_u *newfile;
1223 char_u *outfile;
1224{
1225 int err = FALSE;
1226
1227 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1228 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1229 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1230 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1231 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1232 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1233 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1234}
1235
1236 void
1237eval_patch(origfile, difffile, outfile)
1238 char_u *origfile;
1239 char_u *difffile;
1240 char_u *outfile;
1241{
1242 int err;
1243
1244 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1245 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1246 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1247 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1248 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1249 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1250 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1251}
1252# endif
1253
1254/*
1255 * Top level evaluation function, returning a boolean.
1256 * Sets "error" to TRUE if there was an error.
1257 * Return TRUE or FALSE.
1258 */
1259 int
1260eval_to_bool(arg, error, nextcmd, skip)
1261 char_u *arg;
1262 int *error;
1263 char_u **nextcmd;
1264 int skip; /* only parse, don't execute */
1265{
Bram Moolenaar33570922005-01-25 22:26:29 +00001266 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 int retval = FALSE;
1268
1269 if (skip)
1270 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 else
1274 {
1275 *error = FALSE;
1276 if (!skip)
1277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001278 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001279 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
1281 }
1282 if (skip)
1283 --emsg_skip;
1284
1285 return retval;
1286}
1287
1288/*
1289 * Top level evaluation function, returning a string. If "skip" is TRUE,
1290 * only parsing to "nextcmd" is done, without reporting errors. Return
1291 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1292 */
1293 char_u *
1294eval_to_string_skip(arg, nextcmd, skip)
1295 char_u *arg;
1296 char_u **nextcmd;
1297 int skip; /* only parse, don't execute */
1298{
Bram Moolenaar33570922005-01-25 22:26:29 +00001299 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 char_u *retval;
1301
1302 if (skip)
1303 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001304 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 retval = NULL;
1306 else
1307 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001308 retval = vim_strsave(get_tv_string(&tv));
1309 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 }
1311 if (skip)
1312 --emsg_skip;
1313
1314 return retval;
1315}
1316
1317/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001318 * Skip over an expression at "*pp".
1319 * Return FAIL for an error, OK otherwise.
1320 */
1321 int
1322skip_expr(pp)
1323 char_u **pp;
1324{
Bram Moolenaar33570922005-01-25 22:26:29 +00001325 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001326
1327 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001328 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001329}
1330
1331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001333 * When "convert" is TRUE convert a List into a sequence of lines and convert
1334 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 * Return pointer to allocated memory, or NULL for failure.
1336 */
1337 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001338eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 char_u *arg;
1340 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001341 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342{
Bram Moolenaar33570922005-01-25 22:26:29 +00001343 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001345 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001347 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 retval = NULL;
1352 else
1353 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001354 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001355 {
1356 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001357 if (tv.vval.v_list != NULL)
1358 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001359 ga_append(&ga, NUL);
1360 retval = (char_u *)ga.ga_data;
1361 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001362#ifdef FEAT_FLOAT
1363 else if (convert && tv.v_type == VAR_FLOAT)
1364 {
1365 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1366 retval = vim_strsave(numbuf);
1367 }
1368#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001369 else
1370 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001371 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373
1374 return retval;
1375}
1376
1377/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001378 * Call eval_to_string() without using current local variables and using
1379 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 */
1381 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001382eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 char_u *arg;
1384 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386{
1387 char_u *retval;
1388 void *save_funccalp;
1389
1390 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001391 if (use_sandbox)
1392 ++sandbox;
1393 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001394 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001395 if (use_sandbox)
1396 --sandbox;
1397 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 restore_funccal(save_funccalp);
1399 return retval;
1400}
1401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402/*
1403 * Top level evaluation function, returning a number.
1404 * Evaluates "expr" silently.
1405 * Returns -1 for an error.
1406 */
1407 int
1408eval_to_number(expr)
1409 char_u *expr;
1410{
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001413 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
1415 ++emsg_off;
1416
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 retval = -1;
1419 else
1420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001421 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424 --emsg_off;
1425
1426 return retval;
1427}
1428
Bram Moolenaara40058a2005-07-11 22:42:07 +00001429/*
1430 * Prepare v: variable "idx" to be used.
1431 * Save the current typeval in "save_tv".
1432 * When not used yet add the variable to the v: hashtable.
1433 */
1434 static void
1435prepare_vimvar(idx, save_tv)
1436 int idx;
1437 typval_T *save_tv;
1438{
1439 *save_tv = vimvars[idx].vv_tv;
1440 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1441 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1442}
1443
1444/*
1445 * Restore v: variable "idx" to typeval "save_tv".
1446 * When no longer defined, remove the variable from the v: hashtable.
1447 */
1448 static void
1449restore_vimvar(idx, save_tv)
1450 int idx;
1451 typval_T *save_tv;
1452{
1453 hashitem_T *hi;
1454
Bram Moolenaara40058a2005-07-11 22:42:07 +00001455 vimvars[idx].vv_tv = *save_tv;
1456 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1457 {
1458 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1459 if (HASHITEM_EMPTY(hi))
1460 EMSG2(_(e_intern2), "restore_vimvar()");
1461 else
1462 hash_remove(&vimvarht, hi);
1463 }
1464}
1465
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001466#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001467/*
1468 * Evaluate an expression to a list with suggestions.
1469 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001470 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001471 */
1472 list_T *
1473eval_spell_expr(badword, expr)
1474 char_u *badword;
1475 char_u *expr;
1476{
1477 typval_T save_val;
1478 typval_T rettv;
1479 list_T *list = NULL;
1480 char_u *p = skipwhite(expr);
1481
1482 /* Set "v:val" to the bad word. */
1483 prepare_vimvar(VV_VAL, &save_val);
1484 vimvars[VV_VAL].vv_type = VAR_STRING;
1485 vimvars[VV_VAL].vv_str = badword;
1486 if (p_verbose == 0)
1487 ++emsg_off;
1488
1489 if (eval1(&p, &rettv, TRUE) == OK)
1490 {
1491 if (rettv.v_type != VAR_LIST)
1492 clear_tv(&rettv);
1493 else
1494 list = rettv.vval.v_list;
1495 }
1496
1497 if (p_verbose == 0)
1498 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001499 restore_vimvar(VV_VAL, &save_val);
1500
1501 return list;
1502}
1503
1504/*
1505 * "list" is supposed to contain two items: a word and a number. Return the
1506 * word in "pp" and the number as the return value.
1507 * Return -1 if anything isn't right.
1508 * Used to get the good word and score from the eval_spell_expr() result.
1509 */
1510 int
1511get_spellword(list, pp)
1512 list_T *list;
1513 char_u **pp;
1514{
1515 listitem_T *li;
1516
1517 li = list->lv_first;
1518 if (li == NULL)
1519 return -1;
1520 *pp = get_tv_string(&li->li_tv);
1521
1522 li = li->li_next;
1523 if (li == NULL)
1524 return -1;
1525 return get_tv_number(&li->li_tv);
1526}
1527#endif
1528
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001529/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001530 * Top level evaluation function.
1531 * Returns an allocated typval_T with the result.
1532 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001533 */
1534 typval_T *
1535eval_expr(arg, nextcmd)
1536 char_u *arg;
1537 char_u **nextcmd;
1538{
1539 typval_T *tv;
1540
1541 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001542 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001543 {
1544 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 }
1547
1548 return tv;
1549}
1550
1551
Bram Moolenaar4f688582007-07-24 12:34:30 +00001552#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1553 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001556 * Uses argv[argc] for the function arguments. Only Number and String
1557 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001560 static int
1561call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 char_u *func;
1563 int argc;
1564 char_u **argv;
1565 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567{
Bram Moolenaar33570922005-01-25 22:26:29 +00001568 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 long n;
1570 int len;
1571 int i;
1572 int doesrange;
1573 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001574 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001576 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 for (i = 0; i < argc; i++)
1581 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001582 /* Pass a NULL or empty argument as an empty string */
1583 if (argv[i] == NULL || *argv[i] == NUL)
1584 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001585 argvars[i].v_type = VAR_STRING;
1586 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001587 continue;
1588 }
1589
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 /* Recognize a number argument, the others must be strings. */
1591 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1592 if (len != 0 && len == (int)STRLEN(argv[i]))
1593 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001594 argvars[i].v_type = VAR_NUMBER;
1595 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 else
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_STRING;
1600 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 }
1603
1604 if (safe)
1605 {
1606 save_funccalp = save_funccal();
1607 ++sandbox;
1608 }
1609
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001610 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1611 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001613 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 if (safe)
1615 {
1616 --sandbox;
1617 restore_funccal(save_funccalp);
1618 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001619 vim_free(argvars);
1620
1621 if (ret == FAIL)
1622 clear_tv(rettv);
1623
1624 return ret;
1625}
1626
Bram Moolenaar4f688582007-07-24 12:34:30 +00001627# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001629 * Call vimL function "func" and return the result as a string.
1630 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001631 * Uses argv[argc] for the function arguments.
1632 */
1633 void *
1634call_func_retstr(func, argc, argv, safe)
1635 char_u *func;
1636 int argc;
1637 char_u **argv;
1638 int safe; /* use the sandbox */
1639{
1640 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001641 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001642
1643 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1644 return NULL;
1645
1646 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 return retval;
1649}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001650# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001651
Bram Moolenaar4f688582007-07-24 12:34:30 +00001652# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001654 * Call vimL function "func" and return the result as a number.
1655 * Returns -1 when calling the function fails.
1656 * Uses argv[argc] for the function arguments.
1657 */
1658 long
1659call_func_retnr(func, argc, argv, safe)
1660 char_u *func;
1661 int argc;
1662 char_u **argv;
1663 int safe; /* use the sandbox */
1664{
1665 typval_T rettv;
1666 long retval;
1667
1668 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1669 return -1;
1670
1671 retval = get_tv_number_chk(&rettv, NULL);
1672 clear_tv(&rettv);
1673 return retval;
1674}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001675# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001676
1677/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001678 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001680 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001681 */
1682 void *
1683call_func_retlist(func, argc, argv, safe)
1684 char_u *func;
1685 int argc;
1686 char_u **argv;
1687 int safe; /* use the sandbox */
1688{
1689 typval_T rettv;
1690
1691 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1692 return NULL;
1693
1694 if (rettv.v_type != VAR_LIST)
1695 {
1696 clear_tv(&rettv);
1697 return NULL;
1698 }
1699
1700 return rettv.vval.v_list;
1701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#endif
1703
Bram Moolenaar4f688582007-07-24 12:34:30 +00001704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705/*
1706 * Save the current function call pointer, and set it to NULL.
1707 * Used when executing autocommands and for ":source".
1708 */
1709 void *
1710save_funccal()
1711{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001712 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 current_funccal = NULL;
1715 return (void *)fc;
1716}
1717
1718 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001719restore_funccal(vfc)
1720 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001722 funccall_T *fc = (funccall_T *)vfc;
1723
1724 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725}
1726
Bram Moolenaar05159a02005-02-26 23:04:13 +00001727#if defined(FEAT_PROFILE) || defined(PROTO)
1728/*
1729 * Prepare profiling for entering a child or something else that is not
1730 * counted for the script/function itself.
1731 * Should always be called in pair with prof_child_exit().
1732 */
1733 void
1734prof_child_enter(tm)
1735 proftime_T *tm; /* place to store waittime */
1736{
1737 funccall_T *fc = current_funccal;
1738
1739 if (fc != NULL && fc->func->uf_profiling)
1740 profile_start(&fc->prof_child);
1741 script_prof_save(tm);
1742}
1743
1744/*
1745 * Take care of time spent in a child.
1746 * Should always be called after prof_child_enter().
1747 */
1748 void
1749prof_child_exit(tm)
1750 proftime_T *tm; /* where waittime was stored */
1751{
1752 funccall_T *fc = current_funccal;
1753
1754 if (fc != NULL && fc->func->uf_profiling)
1755 {
1756 profile_end(&fc->prof_child);
1757 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1758 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1759 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1760 }
1761 script_prof_restore(tm);
1762}
1763#endif
1764
1765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_FOLDING
1767/*
1768 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1769 * it in "*cp". Doesn't give error messages.
1770 */
1771 int
1772eval_foldexpr(arg, cp)
1773 char_u *arg;
1774 int *cp;
1775{
Bram Moolenaar33570922005-01-25 22:26:29 +00001776 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 int retval;
1778 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001779 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1780 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001783 if (use_sandbox)
1784 ++sandbox;
1785 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 retval = 0;
1789 else
1790 {
1791 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001792 if (tv.v_type == VAR_NUMBER)
1793 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001794 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval = 0;
1796 else
1797 {
1798 /* If the result is a string, check if there is a non-digit before
1799 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (!VIM_ISDIGIT(*s) && *s != '-')
1802 *cp = *s++;
1803 retval = atol((char *)s);
1804 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
1807 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001808 if (use_sandbox)
1809 --sandbox;
1810 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811
1812 return retval;
1813}
1814#endif
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001817 * ":let" list all variable values
1818 * ":let var1 var2" list variable values
1819 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001820 * ":let var += expr" assignment command.
1821 * ":let var -= expr" assignment command.
1822 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001823 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 */
1825 void
1826ex_let(eap)
1827 exarg_T *eap;
1828{
1829 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001831 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 int var_count = 0;
1834 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001835 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001836 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001837 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
Bram Moolenaardb552d602006-03-23 22:59:57 +00001839 argend = skip_var_list(arg, &var_count, &semicolon);
1840 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001841 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001842 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1843 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001845 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001847 /*
1848 * ":let" without "=": list variables
1849 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001850 if (*arg == '[')
1851 EMSG(_(e_invarg));
1852 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001854 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001856 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001857 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001858 list_glob_vars(&first);
1859 list_buf_vars(&first);
1860 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001861#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001863#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 list_script_vars(&first);
1865 list_func_vars(&first);
1866 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 eap->nextcmd = check_nextcmd(arg);
1869 }
1870 else
1871 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001872 op[0] = '=';
1873 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001874 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875 {
1876 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1877 op[0] = expr[-1]; /* +=, -= or .= */
1878 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001879 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (eap->skip)
1882 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001883 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (eap->skip)
1885 {
1886 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 --emsg_skip;
1889 }
1890 else if (i != FAIL)
1891 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 }
1896 }
1897}
1898
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899/*
1900 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1901 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001902 * When "nextchars" is not NULL it points to a string with characters that
1903 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1904 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001905 * Returns OK or FAIL;
1906 */
1907 static int
1908ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1909 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001910 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 int copy; /* copy values from "tv", don't move */
1912 int semicolon; /* from skip_var_list() */
1913 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915{
1916 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001919 listitem_T *item;
1920 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921
1922 if (*arg != '[')
1923 {
1924 /*
1925 * ":let var = expr" or ":for var in list"
1926 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001927 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 return FAIL;
1929 return OK;
1930 }
1931
1932 /*
1933 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1934 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001935 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 {
1937 EMSG(_(e_listreq));
1938 return FAIL;
1939 }
1940
1941 i = list_len(l);
1942 if (semicolon == 0 && var_count < i)
1943 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001944 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 return FAIL;
1946 }
1947 if (var_count - semicolon > i)
1948 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001949 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001950 return FAIL;
1951 }
1952
1953 item = l->lv_first;
1954 while (*arg != ']')
1955 {
1956 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001957 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 item = item->li_next;
1959 if (arg == NULL)
1960 return FAIL;
1961
1962 arg = skipwhite(arg);
1963 if (*arg == ';')
1964 {
1965 /* Put the rest of the list (may be empty) in the var after ';'.
1966 * Create a new list for this. */
1967 l = list_alloc();
1968 if (l == NULL)
1969 return FAIL;
1970 while (item != NULL)
1971 {
1972 list_append_tv(l, &item->li_tv);
1973 item = item->li_next;
1974 }
1975
1976 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001977 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001978 ltv.vval.v_list = l;
1979 l->lv_refcount = 1;
1980
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1982 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983 clear_tv(&ltv);
1984 if (arg == NULL)
1985 return FAIL;
1986 break;
1987 }
1988 else if (*arg != ',' && *arg != ']')
1989 {
1990 EMSG2(_(e_intern2), "ex_let_vars()");
1991 return FAIL;
1992 }
1993 }
1994
1995 return OK;
1996}
1997
1998/*
1999 * Skip over assignable variable "var" or list of variables "[var, var]".
2000 * Used for ":let varvar = expr" and ":for varvar in expr".
2001 * For "[var, var]" increment "*var_count" for each variable.
2002 * for "[var, var; var]" set "semicolon".
2003 * Return NULL for an error.
2004 */
2005 static char_u *
2006skip_var_list(arg, var_count, semicolon)
2007 char_u *arg;
2008 int *var_count;
2009 int *semicolon;
2010{
2011 char_u *p, *s;
2012
2013 if (*arg == '[')
2014 {
2015 /* "[var, var]": find the matching ']'. */
2016 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002017 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 {
2019 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2020 s = skip_var_one(p);
2021 if (s == p)
2022 {
2023 EMSG2(_(e_invarg2), p);
2024 return NULL;
2025 }
2026 ++*var_count;
2027
2028 p = skipwhite(s);
2029 if (*p == ']')
2030 break;
2031 else if (*p == ';')
2032 {
2033 if (*semicolon == 1)
2034 {
2035 EMSG(_("Double ; in list of variables"));
2036 return NULL;
2037 }
2038 *semicolon = 1;
2039 }
2040 else if (*p != ',')
2041 {
2042 EMSG2(_(e_invarg2), p);
2043 return NULL;
2044 }
2045 }
2046 return p + 1;
2047 }
2048 else
2049 return skip_var_one(arg);
2050}
2051
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002052/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002053 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002054 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002055 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002056 static char_u *
2057skip_var_one(arg)
2058 char_u *arg;
2059{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002060 if (*arg == '@' && arg[1] != NUL)
2061 return arg + 2;
2062 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2063 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064}
2065
Bram Moolenaara7043832005-01-21 11:56:39 +00002066/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002067 * List variables for hashtab "ht" with prefix "prefix".
2068 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002069 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002071list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002072 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002073 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002075 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002076{
Bram Moolenaar33570922005-01-25 22:26:29 +00002077 hashitem_T *hi;
2078 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 int todo;
2080
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002081 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002082 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2083 {
2084 if (!HASHITEM_EMPTY(hi))
2085 {
2086 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 di = HI2DI(hi);
2088 if (empty || di->di_tv.v_type != VAR_STRING
2089 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002090 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002091 }
2092 }
2093}
2094
2095/*
2096 * List global variables.
2097 */
2098 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002099list_glob_vars(first)
2100 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002101{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002102 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002103}
2104
2105/*
2106 * List buffer variables.
2107 */
2108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_buf_vars(first)
2110 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002112 char_u numbuf[NUMBUFLEN];
2113
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002114 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2115 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002116
2117 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002118 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2119 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002120}
2121
2122/*
2123 * List window variables.
2124 */
2125 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126list_win_vars(first)
2127 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002128{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002129 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2130 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002131}
2132
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002133#ifdef FEAT_WINDOWS
2134/*
2135 * List tab page variables.
2136 */
2137 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138list_tab_vars(first)
2139 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002141 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2142 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002143}
2144#endif
2145
Bram Moolenaara7043832005-01-21 11:56:39 +00002146/*
2147 * List Vim variables.
2148 */
2149 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150list_vim_vars(first)
2151 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002153 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002154}
2155
2156/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002157 * List script-local variables, if there is a script.
2158 */
2159 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160list_script_vars(first)
2161 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002162{
2163 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2165 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002166}
2167
2168/*
2169 * List function variables, if there is a function.
2170 */
2171 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172list_func_vars(first)
2173 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002174{
2175 if (current_funccal != NULL)
2176 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002177 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002178}
2179
2180/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 * List variables in "arg".
2182 */
2183 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 exarg_T *eap;
2186 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188{
2189 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002192 char_u *name_start;
2193 char_u *arg_subsc;
2194 char_u *tofree;
2195 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196
2197 while (!ends_excmd(*arg) && !got_int)
2198 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002200 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002201 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2203 {
2204 emsg_severe = TRUE;
2205 EMSG(_(e_trailing));
2206 break;
2207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002211 /* get_name_len() takes care of expanding curly braces */
2212 name_start = name = arg;
2213 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2214 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 /* This is mainly to keep test 49 working: when expanding
2217 * curly braces fails overrule the exception error message. */
2218 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002220 emsg_severe = TRUE;
2221 EMSG2(_(e_invarg2), arg);
2222 break;
2223 }
2224 error = TRUE;
2225 }
2226 else
2227 {
2228 if (tofree != NULL)
2229 name = tofree;
2230 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 else
2233 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002234 /* handle d.key, l[idx], f(expr) */
2235 arg_subsc = arg;
2236 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002237 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002239 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002240 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002244 case 'g': list_glob_vars(first); break;
2245 case 'b': list_buf_vars(first); break;
2246 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002247#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002248 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002249#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 case 'v': list_vim_vars(first); break;
2251 case 's': list_script_vars(first); break;
2252 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 default:
2254 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002255 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 }
2257 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002258 {
2259 char_u numbuf[NUMBUFLEN];
2260 char_u *tf;
2261 int c;
2262 char_u *s;
2263
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002264 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 c = *arg;
2266 *arg = NUL;
2267 list_one_var_a((char_u *)"",
2268 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002269 tv.v_type,
2270 s == NULL ? (char_u *)"" : s,
2271 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 *arg = c;
2273 vim_free(tf);
2274 }
2275 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002276 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 }
2278 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279
2280 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282
2283 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285
2286 return arg;
2287}
2288
2289/*
2290 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2291 * Returns a pointer to the char just after the var name.
2292 * Returns NULL if there is an error.
2293 */
2294 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002295ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002297 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002299 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002300 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301{
2302 int c1;
2303 char_u *name;
2304 char_u *p;
2305 char_u *arg_end = NULL;
2306 int len;
2307 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309
2310 /*
2311 * ":let $VAR = expr": Set environment variable.
2312 */
2313 if (*arg == '$')
2314 {
2315 /* Find the end of the name. */
2316 ++arg;
2317 name = arg;
2318 len = get_env_len(&arg);
2319 if (len == 0)
2320 EMSG2(_(e_invarg2), name - 1);
2321 else
2322 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002323 if (op != NULL && (*op == '+' || *op == '-'))
2324 EMSG2(_(e_letwrong), op);
2325 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002326 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002327 EMSG(_(e_letunexp));
2328 else
2329 {
2330 c1 = name[len];
2331 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002332 p = get_tv_string_chk(tv);
2333 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002334 {
2335 int mustfree = FALSE;
2336 char_u *s = vim_getenv(name, &mustfree);
2337
2338 if (s != NULL)
2339 {
2340 p = tofree = concat_str(s, p);
2341 if (mustfree)
2342 vim_free(s);
2343 }
2344 }
2345 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002346 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002347 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002348 if (STRICMP(name, "HOME") == 0)
2349 init_homedir();
2350 else if (didset_vim && STRICMP(name, "VIM") == 0)
2351 didset_vim = FALSE;
2352 else if (didset_vimruntime
2353 && STRICMP(name, "VIMRUNTIME") == 0)
2354 didset_vimruntime = FALSE;
2355 arg_end = arg;
2356 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002358 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002359 }
2360 }
2361 }
2362
2363 /*
2364 * ":let &option = expr": Set option value.
2365 * ":let &l:option = expr": Set local option value.
2366 * ":let &g:option = expr": Set global option value.
2367 */
2368 else if (*arg == '&')
2369 {
2370 /* Find the end of the name. */
2371 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 if (p == NULL || (endchars != NULL
2373 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 EMSG(_(e_letunexp));
2375 else
2376 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002377 long n;
2378 int opt_type;
2379 long numval;
2380 char_u *stringval = NULL;
2381 char_u *s;
2382
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002383 c1 = *p;
2384 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385
2386 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002387 s = get_tv_string_chk(tv); /* != NULL if number or string */
2388 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002389 {
2390 opt_type = get_option_value(arg, &numval,
2391 &stringval, opt_flags);
2392 if ((opt_type == 1 && *op == '.')
2393 || (opt_type == 0 && *op != '.'))
2394 EMSG2(_(e_letwrong), op);
2395 else
2396 {
2397 if (opt_type == 1) /* number */
2398 {
2399 if (*op == '+')
2400 n = numval + n;
2401 else
2402 n = numval - n;
2403 }
2404 else if (opt_type == 0 && stringval != NULL) /* string */
2405 {
2406 s = concat_str(stringval, s);
2407 vim_free(stringval);
2408 stringval = s;
2409 }
2410 }
2411 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002412 if (s != NULL)
2413 {
2414 set_option_value(arg, n, s, opt_flags);
2415 arg_end = p;
2416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 }
2420 }
2421
2422 /*
2423 * ":let @r = expr": Set register contents.
2424 */
2425 else if (*arg == '@')
2426 {
2427 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 if (op != NULL && (*op == '+' || *op == '-'))
2429 EMSG2(_(e_letwrong), op);
2430 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002431 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 EMSG(_(e_letunexp));
2433 else
2434 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002435 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 char_u *s;
2437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 p = get_tv_string_chk(tv);
2439 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002441 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 if (s != NULL)
2443 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002444 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 vim_free(s);
2446 }
2447 }
2448 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002449 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 arg_end = arg + 1;
2452 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002453 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002454 }
2455 }
2456
2457 /*
2458 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002459 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002461 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002463 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002465 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2469 EMSG(_(e_letunexp));
2470 else
2471 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 arg_end = p;
2474 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 }
2478
2479 else
2480 EMSG2(_(e_invarg2), arg);
2481
2482 return arg_end;
2483}
2484
2485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2487 */
2488 static int
2489check_changedtick(arg)
2490 char_u *arg;
2491{
2492 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2493 {
2494 EMSG2(_(e_readonlyvar), arg);
2495 return TRUE;
2496 }
2497 return FALSE;
2498}
2499
2500/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002501 * Get an lval: variable, Dict item or List item that can be assigned a value
2502 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2503 * "name.key", "name.key[expr]" etc.
2504 * Indexing only works if "name" is an existing List or Dictionary.
2505 * "name" points to the start of the name.
2506 * If "rettv" is not NULL it points to the value to be assigned.
2507 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2508 * wrong; must end in space or cmd separator.
2509 *
2510 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002511 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002513 */
2514 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002515get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002516 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002517 typval_T *rettv;
2518 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 int unlet;
2520 int skip;
2521 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 char_u *expr_start, *expr_end;
2526 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 dictitem_T *v;
2528 typval_T var1;
2529 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002531 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002532 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538
2539 if (skip)
2540 {
2541 /* When skipping just find the end of the name. */
2542 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002543 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 }
2545
2546 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002547 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 if (expr_start != NULL)
2549 {
2550 /* Don't expand the name when we already know there is an error. */
2551 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2552 && *p != '[' && *p != '.')
2553 {
2554 EMSG(_(e_trailing));
2555 return NULL;
2556 }
2557
2558 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2559 if (lp->ll_exp_name == NULL)
2560 {
2561 /* Report an invalid expression in braces, unless the
2562 * expression evaluation has been cancelled due to an
2563 * aborting error, an interrupt, or an exception. */
2564 if (!aborting() && !quiet)
2565 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002566 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002567 EMSG2(_(e_invarg2), name);
2568 return NULL;
2569 }
2570 }
2571 lp->ll_name = lp->ll_exp_name;
2572 }
2573 else
2574 lp->ll_name = name;
2575
2576 /* Without [idx] or .key we are done. */
2577 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2578 return p;
2579
2580 cc = *p;
2581 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002582 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 if (v == NULL && !quiet)
2584 EMSG2(_(e_undefvar), lp->ll_name);
2585 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 if (v == NULL)
2587 return NULL;
2588
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002589 /*
2590 * Loop until no more [idx] or .key is following.
2591 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002592 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002595 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2596 && !(lp->ll_tv->v_type == VAR_DICT
2597 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 if (!quiet)
2600 EMSG(_("E689: Can only index a List or Dictionary"));
2601 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!quiet)
2606 EMSG(_("E708: [:] must come last"));
2607 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002609
Bram Moolenaar8c711452005-01-14 21:53:12 +00002610 len = -1;
2611 if (*p == '.')
2612 {
2613 key = p + 1;
2614 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2615 ;
2616 if (len == 0)
2617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (!quiet)
2619 EMSG(_(e_emptykey));
2620 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002621 }
2622 p = key + len;
2623 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002624 else
2625 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002626 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002627 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 if (*p == ':')
2629 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002630 else
2631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002632 empty1 = FALSE;
2633 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002635 if (get_tv_string_chk(&var1) == NULL)
2636 {
2637 /* not a number or string */
2638 clear_tv(&var1);
2639 return NULL;
2640 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002641 }
2642
2643 /* Optionally get the second index [ :expr]. */
2644 if (*p == ':')
2645 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002646 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002647 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002649 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002650 if (!empty1)
2651 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002653 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (rettv != NULL && (rettv->v_type != VAR_LIST
2655 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (!quiet)
2658 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002659 if (!empty1)
2660 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 }
2663 p = skipwhite(p + 1);
2664 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 else
2667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 if (!empty1)
2672 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002675 if (get_tv_string_chk(&var2) == NULL)
2676 {
2677 /* not a number or string */
2678 if (!empty1)
2679 clear_tv(&var1);
2680 clear_tv(&var2);
2681 return NULL;
2682 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002683 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002684 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002686 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!quiet)
2692 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 if (!empty1)
2694 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002698 }
2699
2700 /* Skip to past ']'. */
2701 ++p;
2702 }
2703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 {
2706 if (len == -1)
2707 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 if (*key == NUL)
2711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
2713 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 }
2717 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 lp->ll_list = NULL;
2719 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002720 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002723 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002727 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (len == -1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 }
2732 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (len == -1)
2737 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002738 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 p = NULL;
2740 break;
2741 }
2742 if (len == -1)
2743 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 }
2746 else
2747 {
2748 /*
2749 * Get the number and item for the only or first index of the List.
2750 */
2751 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 else
2754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002755 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 clear_tv(&var1);
2757 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002758 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 lp->ll_list = lp->ll_tv->vval.v_list;
2760 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2761 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002763 if (lp->ll_n1 < 0)
2764 {
2765 lp->ll_n1 = 0;
2766 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2767 }
2768 }
2769 if (lp->ll_li == NULL)
2770 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775
2776 /*
2777 * May need to find the item or absolute index for the second
2778 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 * When no index given: "lp->ll_empty2" is TRUE.
2780 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002784 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002790 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
2793
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2795 if (lp->ll_n1 < 0)
2796 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2797 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002798 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002803 }
2804
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 return p;
2806}
2807
2808/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002809 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 */
2811 static void
2812clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002813 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814{
2815 vim_free(lp->ll_exp_name);
2816 vim_free(lp->ll_newkey);
2817}
2818
2819/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002820 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002822 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 */
2824 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002825set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002828 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002830 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831{
2832 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002833 listitem_T *ri;
2834 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835
2836 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002837 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002839 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 cc = *endp;
2841 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002842 if (op != NULL && *op != '=')
2843 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002844 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002846 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002847 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002848 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002849 {
2850 if (tv_op(&tv, rettv, op) == OK)
2851 set_var(lp->ll_name, &tv, FALSE);
2852 clear_tv(&tv);
2853 }
2854 }
2855 else
2856 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002858 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002860 else if (tv_check_lock(lp->ll_newkey == NULL
2861 ? lp->ll_tv->v_lock
2862 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2863 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 else if (lp->ll_range)
2865 {
2866 /*
2867 * Assign the List values to the list items.
2868 */
2869 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002870 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 if (op != NULL && *op != '=')
2872 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2873 else
2874 {
2875 clear_tv(&lp->ll_li->li_tv);
2876 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2877 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 ri = ri->li_next;
2879 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2880 break;
2881 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002882 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002884 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002885 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 ri = NULL;
2887 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002888 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002889 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_li = lp->ll_li->li_next;
2891 ++lp->ll_n1;
2892 }
2893 if (ri != NULL)
2894 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002895 else if (lp->ll_empty2
2896 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 : lp->ll_n1 != lp->ll_n2)
2898 EMSG(_("E711: List value has not enough items"));
2899 }
2900 else
2901 {
2902 /*
2903 * Assign to a List or Dictionary item.
2904 */
2905 if (lp->ll_newkey != NULL)
2906 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002907 if (op != NULL && *op != '=')
2908 {
2909 EMSG2(_(e_letwrong), op);
2910 return;
2911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002914 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 if (di == NULL)
2916 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002917 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2918 {
2919 vim_free(di);
2920 return;
2921 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002924 else if (op != NULL && *op != '=')
2925 {
2926 tv_op(lp->ll_tv, rettv, op);
2927 return;
2928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002931
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 /*
2933 * Assign the value to the variable or list item.
2934 */
2935 if (copy)
2936 copy_tv(rettv, lp->ll_tv);
2937 else
2938 {
2939 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002940 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002942 }
2943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944}
2945
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002946/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002947 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2948 * Returns OK or FAIL.
2949 */
2950 static int
2951tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002952 typval_T *tv1;
2953 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002954 char_u *op;
2955{
2956 long n;
2957 char_u numbuf[NUMBUFLEN];
2958 char_u *s;
2959
2960 /* Can't do anything with a Funcref or a Dict on the right. */
2961 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2962 {
2963 switch (tv1->v_type)
2964 {
2965 case VAR_DICT:
2966 case VAR_FUNC:
2967 break;
2968
2969 case VAR_LIST:
2970 if (*op != '+' || tv2->v_type != VAR_LIST)
2971 break;
2972 /* List += List */
2973 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2974 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2975 return OK;
2976
2977 case VAR_NUMBER:
2978 case VAR_STRING:
2979 if (tv2->v_type == VAR_LIST)
2980 break;
2981 if (*op == '+' || *op == '-')
2982 {
2983 /* nr += nr or nr -= nr*/
2984 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002985#ifdef FEAT_FLOAT
2986 if (tv2->v_type == VAR_FLOAT)
2987 {
2988 float_T f = n;
2989
2990 if (*op == '+')
2991 f += tv2->vval.v_float;
2992 else
2993 f -= tv2->vval.v_float;
2994 clear_tv(tv1);
2995 tv1->v_type = VAR_FLOAT;
2996 tv1->vval.v_float = f;
2997 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002998 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002999#endif
3000 {
3001 if (*op == '+')
3002 n += get_tv_number(tv2);
3003 else
3004 n -= get_tv_number(tv2);
3005 clear_tv(tv1);
3006 tv1->v_type = VAR_NUMBER;
3007 tv1->vval.v_number = n;
3008 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 }
3010 else
3011 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003012 if (tv2->v_type == VAR_FLOAT)
3013 break;
3014
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003015 /* str .= str */
3016 s = get_tv_string(tv1);
3017 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3018 clear_tv(tv1);
3019 tv1->v_type = VAR_STRING;
3020 tv1->vval.v_string = s;
3021 }
3022 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003023
3024#ifdef FEAT_FLOAT
3025 case VAR_FLOAT:
3026 {
3027 float_T f;
3028
3029 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3030 && tv2->v_type != VAR_NUMBER
3031 && tv2->v_type != VAR_STRING))
3032 break;
3033 if (tv2->v_type == VAR_FLOAT)
3034 f = tv2->vval.v_float;
3035 else
3036 f = get_tv_number(tv2);
3037 if (*op == '+')
3038 tv1->vval.v_float += f;
3039 else
3040 tv1->vval.v_float -= f;
3041 }
3042 return OK;
3043#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 }
3045 }
3046
3047 EMSG2(_(e_letwrong), op);
3048 return FAIL;
3049}
3050
3051/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003052 * Add a watcher to a list.
3053 */
3054 static void
3055list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003056 list_T *l;
3057 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003058{
3059 lw->lw_next = l->lv_watch;
3060 l->lv_watch = lw;
3061}
3062
3063/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003064 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003065 * No warning when it isn't found...
3066 */
3067 static void
3068list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003069 list_T *l;
3070 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003071{
Bram Moolenaar33570922005-01-25 22:26:29 +00003072 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003073
3074 lwp = &l->lv_watch;
3075 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3076 {
3077 if (lw == lwrem)
3078 {
3079 *lwp = lw->lw_next;
3080 break;
3081 }
3082 lwp = &lw->lw_next;
3083 }
3084}
3085
3086/*
3087 * Just before removing an item from a list: advance watchers to the next
3088 * item.
3089 */
3090 static void
3091list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003092 list_T *l;
3093 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003094{
Bram Moolenaar33570922005-01-25 22:26:29 +00003095 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003096
3097 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3098 if (lw->lw_item == item)
3099 lw->lw_item = item->li_next;
3100}
3101
3102/*
3103 * Evaluate the expression used in a ":for var in expr" command.
3104 * "arg" points to "var".
3105 * Set "*errp" to TRUE for an error, FALSE otherwise;
3106 * Return a pointer that holds the info. Null when there is an error.
3107 */
3108 void *
3109eval_for_line(arg, errp, nextcmdp, skip)
3110 char_u *arg;
3111 int *errp;
3112 char_u **nextcmdp;
3113 int skip;
3114{
Bram Moolenaar33570922005-01-25 22:26:29 +00003115 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003116 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003117 typval_T tv;
3118 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119
3120 *errp = TRUE; /* default: there is an error */
3121
Bram Moolenaar33570922005-01-25 22:26:29 +00003122 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123 if (fi == NULL)
3124 return NULL;
3125
3126 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3127 if (expr == NULL)
3128 return fi;
3129
3130 expr = skipwhite(expr);
3131 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3132 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003133 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003134 return fi;
3135 }
3136
3137 if (skip)
3138 ++emsg_skip;
3139 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3140 {
3141 *errp = FALSE;
3142 if (!skip)
3143 {
3144 l = tv.vval.v_list;
3145 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003146 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003147 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003148 clear_tv(&tv);
3149 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150 else
3151 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003152 /* No need to increment the refcount, it's already set for the
3153 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003154 fi->fi_list = l;
3155 list_add_watch(l, &fi->fi_lw);
3156 fi->fi_lw.lw_item = l->lv_first;
3157 }
3158 }
3159 }
3160 if (skip)
3161 --emsg_skip;
3162
3163 return fi;
3164}
3165
3166/*
3167 * Use the first item in a ":for" list. Advance to the next.
3168 * Assign the values to the variable (list). "arg" points to the first one.
3169 * Return TRUE when a valid item was found, FALSE when at end of list or
3170 * something wrong.
3171 */
3172 int
3173next_for_item(fi_void, arg)
3174 void *fi_void;
3175 char_u *arg;
3176{
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180
3181 item = fi->fi_lw.lw_item;
3182 if (item == NULL)
3183 result = FALSE;
3184 else
3185 {
3186 fi->fi_lw.lw_item = item->li_next;
3187 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3188 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3189 }
3190 return result;
3191}
3192
3193/*
3194 * Free the structure used to store info used by ":for".
3195 */
3196 void
3197free_for_info(fi_void)
3198 void *fi_void;
3199{
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003202 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 list_unref(fi->fi_list);
3206 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207 vim_free(fi);
3208}
3209
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3211
3212 void
3213set_context_for_expression(xp, arg, cmdidx)
3214 expand_T *xp;
3215 char_u *arg;
3216 cmdidx_T cmdidx;
3217{
3218 int got_eq = FALSE;
3219 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003220 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222 if (cmdidx == CMD_let)
3223 {
3224 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003225 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226 {
3227 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003228 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 {
3230 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003231 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 if (vim_iswhite(*p))
3233 break;
3234 }
3235 return;
3236 }
3237 }
3238 else
3239 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3240 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 while ((xp->xp_pattern = vim_strpbrk(arg,
3242 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3243 {
3244 c = *xp->xp_pattern;
3245 if (c == '&')
3246 {
3247 c = xp->xp_pattern[1];
3248 if (c == '&')
3249 {
3250 ++xp->xp_pattern;
3251 xp->xp_context = cmdidx != CMD_let || got_eq
3252 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3253 }
3254 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003257 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3258 xp->xp_pattern += 2;
3259
3260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 else if (c == '$')
3263 {
3264 /* environment variable */
3265 xp->xp_context = EXPAND_ENV_VARS;
3266 }
3267 else if (c == '=')
3268 {
3269 got_eq = TRUE;
3270 xp->xp_context = EXPAND_EXPRESSION;
3271 }
3272 else if (c == '<'
3273 && xp->xp_context == EXPAND_FUNCTIONS
3274 && vim_strchr(xp->xp_pattern, '(') == NULL)
3275 {
3276 /* Function name can start with "<SNR>" */
3277 break;
3278 }
3279 else if (cmdidx != CMD_let || got_eq)
3280 {
3281 if (c == '"') /* string */
3282 {
3283 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3284 if (c == '\\' && xp->xp_pattern[1] != NUL)
3285 ++xp->xp_pattern;
3286 xp->xp_context = EXPAND_NOTHING;
3287 }
3288 else if (c == '\'') /* literal string */
3289 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003290 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3292 /* skip */ ;
3293 xp->xp_context = EXPAND_NOTHING;
3294 }
3295 else if (c == '|')
3296 {
3297 if (xp->xp_pattern[1] == '|')
3298 {
3299 ++xp->xp_pattern;
3300 xp->xp_context = EXPAND_EXPRESSION;
3301 }
3302 else
3303 xp->xp_context = EXPAND_COMMANDS;
3304 }
3305 else
3306 xp->xp_context = EXPAND_EXPRESSION;
3307 }
3308 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309 /* Doesn't look like something valid, expand as an expression
3310 * anyway. */
3311 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 arg = xp->xp_pattern;
3313 if (*arg != NUL)
3314 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3315 /* skip */ ;
3316 }
3317 xp->xp_pattern = arg;
3318}
3319
3320#endif /* FEAT_CMDL_COMPL */
3321
3322/*
3323 * ":1,25call func(arg1, arg2)" function call.
3324 */
3325 void
3326ex_call(eap)
3327 exarg_T *eap;
3328{
3329 char_u *arg = eap->arg;
3330 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003332 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003334 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 linenr_T lnum;
3336 int doesrange;
3337 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003338 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003340 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003341 if (fudi.fd_newkey != NULL)
3342 {
3343 /* Still need to give an error message for missing key. */
3344 EMSG2(_(e_dictkey), fudi.fd_newkey);
3345 vim_free(fudi.fd_newkey);
3346 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003347 if (tofree == NULL)
3348 return;
3349
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003350 /* Increase refcount on dictionary, it could get deleted when evaluating
3351 * the arguments. */
3352 if (fudi.fd_dict != NULL)
3353 ++fudi.fd_dict->dv_refcount;
3354
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003355 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003356 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003357 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaar532c7802005-01-27 14:44:31 +00003359 /* Skip white space to allow ":call func ()". Not good, but required for
3360 * backward compatibility. */
3361 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003362 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
3364 if (*startarg != '(')
3365 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003366 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 goto end;
3368 }
3369
3370 /*
3371 * When skipping, evaluate the function once, to find the end of the
3372 * arguments.
3373 * When the function takes a range, this is discovered after the first
3374 * call, and the loop is broken.
3375 */
3376 if (eap->skip)
3377 {
3378 ++emsg_skip;
3379 lnum = eap->line2; /* do it once, also with an invalid range */
3380 }
3381 else
3382 lnum = eap->line1;
3383 for ( ; lnum <= eap->line2; ++lnum)
3384 {
3385 if (!eap->skip && eap->addr_count > 0)
3386 {
3387 curwin->w_cursor.lnum = lnum;
3388 curwin->w_cursor.col = 0;
3389 }
3390 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003391 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003392 eap->line1, eap->line2, &doesrange,
3393 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
3395 failed = TRUE;
3396 break;
3397 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003398
3399 /* Handle a function returning a Funcref, Dictionary or List. */
3400 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3401 {
3402 failed = TRUE;
3403 break;
3404 }
3405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003406 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 if (doesrange || eap->skip)
3408 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003411 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003412 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003413 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 if (aborting())
3415 break;
3416 }
3417 if (eap->skip)
3418 --emsg_skip;
3419
3420 if (!failed)
3421 {
3422 /* Check for trailing illegal characters and a following command. */
3423 if (!ends_excmd(*arg))
3424 {
3425 emsg_severe = TRUE;
3426 EMSG(_(e_trailing));
3427 }
3428 else
3429 eap->nextcmd = check_nextcmd(arg);
3430 }
3431
3432end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003433 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003434 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435}
3436
3437/*
3438 * ":unlet[!] var1 ... " command.
3439 */
3440 void
3441ex_unlet(eap)
3442 exarg_T *eap;
3443{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003444 ex_unletlock(eap, eap->arg, 0);
3445}
3446
3447/*
3448 * ":lockvar" and ":unlockvar" commands
3449 */
3450 void
3451ex_lockvar(eap)
3452 exarg_T *eap;
3453{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003455 int deep = 2;
3456
3457 if (eap->forceit)
3458 deep = -1;
3459 else if (vim_isdigit(*arg))
3460 {
3461 deep = getdigits(&arg);
3462 arg = skipwhite(arg);
3463 }
3464
3465 ex_unletlock(eap, arg, deep);
3466}
3467
3468/*
3469 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3470 */
3471 static void
3472ex_unletlock(eap, argstart, deep)
3473 exarg_T *eap;
3474 char_u *argstart;
3475 int deep;
3476{
3477 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003480 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
3482 do
3483 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003484 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003485 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3486 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003487 if (lv.ll_name == NULL)
3488 error = TRUE; /* error but continue parsing */
3489 if (name_end == NULL || (!vim_iswhite(*name_end)
3490 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003492 if (name_end != NULL)
3493 {
3494 emsg_severe = TRUE;
3495 EMSG(_(e_trailing));
3496 }
3497 if (!(eap->skip || error))
3498 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 break;
3500 }
3501
3502 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003503 {
3504 if (eap->cmdidx == CMD_unlet)
3505 {
3506 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3507 error = TRUE;
3508 }
3509 else
3510 {
3511 if (do_lock_var(&lv, name_end, deep,
3512 eap->cmdidx == CMD_lockvar) == FAIL)
3513 error = TRUE;
3514 }
3515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003517 if (!eap->skip)
3518 clear_lval(&lv);
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 arg = skipwhite(name_end);
3521 } while (!ends_excmd(*arg));
3522
3523 eap->nextcmd = check_nextcmd(arg);
3524}
3525
Bram Moolenaar8c711452005-01-14 21:53:12 +00003526 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003527do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003528 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003529 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003530 int forceit;
3531{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003532 int ret = OK;
3533 int cc;
3534
3535 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003536 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003537 cc = *name_end;
3538 *name_end = NUL;
3539
3540 /* Normal name or expanded name. */
3541 if (check_changedtick(lp->ll_name))
3542 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003543 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003544 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003545 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003546 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003547 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3548 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003549 else if (lp->ll_range)
3550 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003551 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003552
3553 /* Delete a range of List items. */
3554 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3555 {
3556 li = lp->ll_li->li_next;
3557 listitem_remove(lp->ll_list, lp->ll_li);
3558 lp->ll_li = li;
3559 ++lp->ll_n1;
3560 }
3561 }
3562 else
3563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 /* unlet a List item. */
3566 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003567 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003569 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003570 }
3571
3572 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003573}
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575/*
3576 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003577 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 */
3579 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003580do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003582 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583{
Bram Moolenaar33570922005-01-25 22:26:29 +00003584 hashtab_T *ht;
3585 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003586 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003587 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
Bram Moolenaar33570922005-01-25 22:26:29 +00003589 ht = find_var_ht(name, &varname);
3590 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003592 hi = hash_find(ht, varname);
3593 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003594 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003595 di = HI2DI(hi);
3596 if (var_check_fixed(di->di_flags, name)
3597 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598 return FAIL;
3599 delete_var(ht, hi);
3600 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603 if (forceit)
3604 return OK;
3605 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 return FAIL;
3607}
3608
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003609/*
3610 * Lock or unlock variable indicated by "lp".
3611 * "deep" is the levels to go (-1 for unlimited);
3612 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3613 */
3614 static int
3615do_lock_var(lp, name_end, deep, lock)
3616 lval_T *lp;
3617 char_u *name_end;
3618 int deep;
3619 int lock;
3620{
3621 int ret = OK;
3622 int cc;
3623 dictitem_T *di;
3624
3625 if (deep == 0) /* nothing to do */
3626 return OK;
3627
3628 if (lp->ll_tv == NULL)
3629 {
3630 cc = *name_end;
3631 *name_end = NUL;
3632
3633 /* Normal name or expanded name. */
3634 if (check_changedtick(lp->ll_name))
3635 ret = FAIL;
3636 else
3637 {
3638 di = find_var(lp->ll_name, NULL);
3639 if (di == NULL)
3640 ret = FAIL;
3641 else
3642 {
3643 if (lock)
3644 di->di_flags |= DI_FLAGS_LOCK;
3645 else
3646 di->di_flags &= ~DI_FLAGS_LOCK;
3647 item_lock(&di->di_tv, deep, lock);
3648 }
3649 }
3650 *name_end = cc;
3651 }
3652 else if (lp->ll_range)
3653 {
3654 listitem_T *li = lp->ll_li;
3655
3656 /* (un)lock a range of List items. */
3657 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3658 {
3659 item_lock(&li->li_tv, deep, lock);
3660 li = li->li_next;
3661 ++lp->ll_n1;
3662 }
3663 }
3664 else if (lp->ll_list != NULL)
3665 /* (un)lock a List item. */
3666 item_lock(&lp->ll_li->li_tv, deep, lock);
3667 else
3668 /* un(lock) a Dictionary item. */
3669 item_lock(&lp->ll_di->di_tv, deep, lock);
3670
3671 return ret;
3672}
3673
3674/*
3675 * Lock or unlock an item. "deep" is nr of levels to go.
3676 */
3677 static void
3678item_lock(tv, deep, lock)
3679 typval_T *tv;
3680 int deep;
3681 int lock;
3682{
3683 static int recurse = 0;
3684 list_T *l;
3685 listitem_T *li;
3686 dict_T *d;
3687 hashitem_T *hi;
3688 int todo;
3689
3690 if (recurse >= DICT_MAXNEST)
3691 {
3692 EMSG(_("E743: variable nested too deep for (un)lock"));
3693 return;
3694 }
3695 if (deep == 0)
3696 return;
3697 ++recurse;
3698
3699 /* lock/unlock the item itself */
3700 if (lock)
3701 tv->v_lock |= VAR_LOCKED;
3702 else
3703 tv->v_lock &= ~VAR_LOCKED;
3704
3705 switch (tv->v_type)
3706 {
3707 case VAR_LIST:
3708 if ((l = tv->vval.v_list) != NULL)
3709 {
3710 if (lock)
3711 l->lv_lock |= VAR_LOCKED;
3712 else
3713 l->lv_lock &= ~VAR_LOCKED;
3714 if (deep < 0 || deep > 1)
3715 /* recursive: lock/unlock the items the List contains */
3716 for (li = l->lv_first; li != NULL; li = li->li_next)
3717 item_lock(&li->li_tv, deep - 1, lock);
3718 }
3719 break;
3720 case VAR_DICT:
3721 if ((d = tv->vval.v_dict) != NULL)
3722 {
3723 if (lock)
3724 d->dv_lock |= VAR_LOCKED;
3725 else
3726 d->dv_lock &= ~VAR_LOCKED;
3727 if (deep < 0 || deep > 1)
3728 {
3729 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003730 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3732 {
3733 if (!HASHITEM_EMPTY(hi))
3734 {
3735 --todo;
3736 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3737 }
3738 }
3739 }
3740 }
3741 }
3742 --recurse;
3743}
3744
Bram Moolenaara40058a2005-07-11 22:42:07 +00003745/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003746 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3747 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003748 */
3749 static int
3750tv_islocked(tv)
3751 typval_T *tv;
3752{
3753 return (tv->v_lock & VAR_LOCKED)
3754 || (tv->v_type == VAR_LIST
3755 && tv->vval.v_list != NULL
3756 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3757 || (tv->v_type == VAR_DICT
3758 && tv->vval.v_dict != NULL
3759 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3760}
3761
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3763/*
3764 * Delete all "menutrans_" variables.
3765 */
3766 void
3767del_menutrans_vars()
3768{
Bram Moolenaar33570922005-01-25 22:26:29 +00003769 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003770 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771
Bram Moolenaar33570922005-01-25 22:26:29 +00003772 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003773 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003774 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003775 {
3776 if (!HASHITEM_EMPTY(hi))
3777 {
3778 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3780 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 }
3782 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003783 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784}
3785#endif
3786
3787#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3788
3789/*
3790 * Local string buffer for the next two functions to store a variable name
3791 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3792 * get_user_var_name().
3793 */
3794
3795static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3796
3797static char_u *varnamebuf = NULL;
3798static int varnamebuflen = 0;
3799
3800/*
3801 * Function to concatenate a prefix and a variable name.
3802 */
3803 static char_u *
3804cat_prefix_varname(prefix, name)
3805 int prefix;
3806 char_u *name;
3807{
3808 int len;
3809
3810 len = (int)STRLEN(name) + 3;
3811 if (len > varnamebuflen)
3812 {
3813 vim_free(varnamebuf);
3814 len += 10; /* some additional space */
3815 varnamebuf = alloc(len);
3816 if (varnamebuf == NULL)
3817 {
3818 varnamebuflen = 0;
3819 return NULL;
3820 }
3821 varnamebuflen = len;
3822 }
3823 *varnamebuf = prefix;
3824 varnamebuf[1] = ':';
3825 STRCPY(varnamebuf + 2, name);
3826 return varnamebuf;
3827}
3828
3829/*
3830 * Function given to ExpandGeneric() to obtain the list of user defined
3831 * (global/buffer/window/built-in) variable names.
3832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 char_u *
3834get_user_var_name(xp, idx)
3835 expand_T *xp;
3836 int idx;
3837{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003838 static long_u gdone;
3839 static long_u bdone;
3840 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003841#ifdef FEAT_WINDOWS
3842 static long_u tdone;
3843#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003844 static int vidx;
3845 static hashitem_T *hi;
3846 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847
3848 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003849 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003851#ifdef FEAT_WINDOWS
3852 tdone = 0;
3853#endif
3854 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003855
3856 /* Global variables */
3857 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003859 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003860 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003861 else
3862 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003863 while (HASHITEM_EMPTY(hi))
3864 ++hi;
3865 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3866 return cat_prefix_varname('g', hi->hi_key);
3867 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003869
3870 /* b: variables */
3871 ht = &curbuf->b_vars.dv_hashtab;
3872 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003874 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003875 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003876 else
3877 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003878 while (HASHITEM_EMPTY(hi))
3879 ++hi;
3880 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003882 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003884 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 return (char_u *)"b:changedtick";
3886 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003887
3888 /* w: variables */
3889 ht = &curwin->w_vars.dv_hashtab;
3890 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003892 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003893 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003894 else
3895 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003896 while (HASHITEM_EMPTY(hi))
3897 ++hi;
3898 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003900
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003901#ifdef FEAT_WINDOWS
3902 /* t: variables */
3903 ht = &curtab->tp_vars.dv_hashtab;
3904 if (tdone < ht->ht_used)
3905 {
3906 if (tdone++ == 0)
3907 hi = ht->ht_array;
3908 else
3909 ++hi;
3910 while (HASHITEM_EMPTY(hi))
3911 ++hi;
3912 return cat_prefix_varname('t', hi->hi_key);
3913 }
3914#endif
3915
Bram Moolenaar33570922005-01-25 22:26:29 +00003916 /* v: variables */
3917 if (vidx < VV_LEN)
3918 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
3920 vim_free(varnamebuf);
3921 varnamebuf = NULL;
3922 varnamebuflen = 0;
3923 return NULL;
3924}
3925
3926#endif /* FEAT_CMDL_COMPL */
3927
3928/*
3929 * types for expressions.
3930 */
3931typedef enum
3932{
3933 TYPE_UNKNOWN = 0
3934 , TYPE_EQUAL /* == */
3935 , TYPE_NEQUAL /* != */
3936 , TYPE_GREATER /* > */
3937 , TYPE_GEQUAL /* >= */
3938 , TYPE_SMALLER /* < */
3939 , TYPE_SEQUAL /* <= */
3940 , TYPE_MATCH /* =~ */
3941 , TYPE_NOMATCH /* !~ */
3942} exptype_T;
3943
3944/*
3945 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003946 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3948 */
3949
3950/*
3951 * Handle zero level expression.
3952 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003953 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003954 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 * Return OK or FAIL.
3956 */
3957 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003958eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 char_u **nextcmd;
3962 int evaluate;
3963{
3964 int ret;
3965 char_u *p;
3966
3967 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003968 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (ret == FAIL || !ends_excmd(*p))
3970 {
3971 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003972 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 /*
3974 * Report the invalid expression unless the expression evaluation has
3975 * been cancelled due to an aborting error, an interrupt, or an
3976 * exception.
3977 */
3978 if (!aborting())
3979 EMSG2(_(e_invexpr2), arg);
3980 ret = FAIL;
3981 }
3982 if (nextcmd != NULL)
3983 *nextcmd = check_nextcmd(p);
3984
3985 return ret;
3986}
3987
3988/*
3989 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003990 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 *
3992 * "arg" must point to the first non-white of the expression.
3993 * "arg" is advanced to the next non-white after the recognized expression.
3994 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003995 * Note: "rettv.v_lock" is not set.
3996 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 * Return OK or FAIL.
3998 */
3999 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004000eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 int evaluate;
4004{
4005 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007
4008 /*
4009 * Get the first variable.
4010 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004011 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 return FAIL;
4013
4014 if ((*arg)[0] == '?')
4015 {
4016 result = FALSE;
4017 if (evaluate)
4018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004019 int error = FALSE;
4020
4021 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004024 if (error)
4025 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027
4028 /*
4029 * Get the second variable.
4030 */
4031 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004032 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 return FAIL;
4034
4035 /*
4036 * Check for the ":".
4037 */
4038 if ((*arg)[0] != ':')
4039 {
4040 EMSG(_("E109: Missing ':' after '?'"));
4041 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 return FAIL;
4044 }
4045
4046 /*
4047 * Get the third variable.
4048 */
4049 *arg = skipwhite(*arg + 1);
4050 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4051 {
4052 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004053 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return FAIL;
4055 }
4056 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004057 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059
4060 return OK;
4061}
4062
4063/*
4064 * Handle first level expression:
4065 * expr2 || expr2 || expr2 logical OR
4066 *
4067 * "arg" must point to the first non-white of the expression.
4068 * "arg" is advanced to the next non-white after the recognized expression.
4069 *
4070 * Return OK or FAIL.
4071 */
4072 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004073eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 int evaluate;
4077{
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 long result;
4080 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004081 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082
4083 /*
4084 * Get the first variable.
4085 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 return FAIL;
4088
4089 /*
4090 * Repeat until there is no following "||".
4091 */
4092 first = TRUE;
4093 result = FALSE;
4094 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4095 {
4096 if (evaluate && first)
4097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004098 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004100 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004101 if (error)
4102 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 first = FALSE;
4104 }
4105
4106 /*
4107 * Get the second variable.
4108 */
4109 *arg = skipwhite(*arg + 2);
4110 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4111 return FAIL;
4112
4113 /*
4114 * Compute the result.
4115 */
4116 if (evaluate && !result)
4117 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004118 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004121 if (error)
4122 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 if (evaluate)
4125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 rettv->v_type = VAR_NUMBER;
4127 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 }
4130
4131 return OK;
4132}
4133
4134/*
4135 * Handle second level expression:
4136 * expr3 && expr3 && expr3 logical AND
4137 *
4138 * "arg" must point to the first non-white of the expression.
4139 * "arg" is advanced to the next non-white after the recognized expression.
4140 *
4141 * Return OK or FAIL.
4142 */
4143 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 int evaluate;
4148{
Bram Moolenaar33570922005-01-25 22:26:29 +00004149 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 long result;
4151 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004152 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154 /*
4155 * Get the first variable.
4156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004157 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 return FAIL;
4159
4160 /*
4161 * Repeat until there is no following "&&".
4162 */
4163 first = TRUE;
4164 result = TRUE;
4165 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4166 {
4167 if (evaluate && first)
4168 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004169 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004172 if (error)
4173 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 first = FALSE;
4175 }
4176
4177 /*
4178 * Get the second variable.
4179 */
4180 *arg = skipwhite(*arg + 2);
4181 if (eval4(arg, &var2, evaluate && result) == FAIL)
4182 return FAIL;
4183
4184 /*
4185 * Compute the result.
4186 */
4187 if (evaluate && result)
4188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 if (error)
4193 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 if (evaluate)
4196 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 rettv->v_type = VAR_NUMBER;
4198 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 }
4201
4202 return OK;
4203}
4204
4205/*
4206 * Handle third level expression:
4207 * var1 == var2
4208 * var1 =~ var2
4209 * var1 != var2
4210 * var1 !~ var2
4211 * var1 > var2
4212 * var1 >= var2
4213 * var1 < var2
4214 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004215 * var1 is var2
4216 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 *
4218 * "arg" must point to the first non-white of the expression.
4219 * "arg" is advanced to the next non-white after the recognized expression.
4220 *
4221 * Return OK or FAIL.
4222 */
4223 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004226 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 int evaluate;
4228{
Bram Moolenaar33570922005-01-25 22:26:29 +00004229 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 char_u *p;
4231 int i;
4232 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004233 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 int len = 2;
4235 long n1, n2;
4236 char_u *s1, *s2;
4237 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4238 regmatch_T regmatch;
4239 int ic;
4240 char_u *save_cpo;
4241
4242 /*
4243 * Get the first variable.
4244 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004245 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 return FAIL;
4247
4248 p = *arg;
4249 switch (p[0])
4250 {
4251 case '=': if (p[1] == '=')
4252 type = TYPE_EQUAL;
4253 else if (p[1] == '~')
4254 type = TYPE_MATCH;
4255 break;
4256 case '!': if (p[1] == '=')
4257 type = TYPE_NEQUAL;
4258 else if (p[1] == '~')
4259 type = TYPE_NOMATCH;
4260 break;
4261 case '>': if (p[1] != '=')
4262 {
4263 type = TYPE_GREATER;
4264 len = 1;
4265 }
4266 else
4267 type = TYPE_GEQUAL;
4268 break;
4269 case '<': if (p[1] != '=')
4270 {
4271 type = TYPE_SMALLER;
4272 len = 1;
4273 }
4274 else
4275 type = TYPE_SEQUAL;
4276 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004277 case 'i': if (p[1] == 's')
4278 {
4279 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4280 len = 5;
4281 if (!vim_isIDc(p[len]))
4282 {
4283 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4284 type_is = TRUE;
4285 }
4286 }
4287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289
4290 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004291 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 */
4293 if (type != TYPE_UNKNOWN)
4294 {
4295 /* extra question mark appended: ignore case */
4296 if (p[len] == '?')
4297 {
4298 ic = TRUE;
4299 ++len;
4300 }
4301 /* extra '#' appended: match case */
4302 else if (p[len] == '#')
4303 {
4304 ic = FALSE;
4305 ++len;
4306 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004307 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 else
4309 ic = p_ic;
4310
4311 /*
4312 * Get the second variable.
4313 */
4314 *arg = skipwhite(p + len);
4315 if (eval5(arg, &var2, evaluate) == FAIL)
4316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004317 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return FAIL;
4319 }
4320
4321 if (evaluate)
4322 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004323 if (type_is && rettv->v_type != var2.v_type)
4324 {
4325 /* For "is" a different type always means FALSE, for "notis"
4326 * it means TRUE. */
4327 n1 = (type == TYPE_NEQUAL);
4328 }
4329 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4330 {
4331 if (type_is)
4332 {
4333 n1 = (rettv->v_type == var2.v_type
4334 && rettv->vval.v_list == var2.vval.v_list);
4335 if (type == TYPE_NEQUAL)
4336 n1 = !n1;
4337 }
4338 else if (rettv->v_type != var2.v_type
4339 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4340 {
4341 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004342 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004343 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004344 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004345 clear_tv(rettv);
4346 clear_tv(&var2);
4347 return FAIL;
4348 }
4349 else
4350 {
4351 /* Compare two Lists for being equal or unequal. */
4352 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
4353 if (type == TYPE_NEQUAL)
4354 n1 = !n1;
4355 }
4356 }
4357
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004358 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4359 {
4360 if (type_is)
4361 {
4362 n1 = (rettv->v_type == var2.v_type
4363 && rettv->vval.v_dict == var2.vval.v_dict);
4364 if (type == TYPE_NEQUAL)
4365 n1 = !n1;
4366 }
4367 else if (rettv->v_type != var2.v_type
4368 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4369 {
4370 if (rettv->v_type != var2.v_type)
4371 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4372 else
4373 EMSG(_("E736: Invalid operation for Dictionary"));
4374 clear_tv(rettv);
4375 clear_tv(&var2);
4376 return FAIL;
4377 }
4378 else
4379 {
4380 /* Compare two Dictionaries for being equal or unequal. */
4381 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4382 if (type == TYPE_NEQUAL)
4383 n1 = !n1;
4384 }
4385 }
4386
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004387 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4388 {
4389 if (rettv->v_type != var2.v_type
4390 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4391 {
4392 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004393 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004394 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004395 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004396 clear_tv(rettv);
4397 clear_tv(&var2);
4398 return FAIL;
4399 }
4400 else
4401 {
4402 /* Compare two Funcrefs for being equal or unequal. */
4403 if (rettv->vval.v_string == NULL
4404 || var2.vval.v_string == NULL)
4405 n1 = FALSE;
4406 else
4407 n1 = STRCMP(rettv->vval.v_string,
4408 var2.vval.v_string) == 0;
4409 if (type == TYPE_NEQUAL)
4410 n1 = !n1;
4411 }
4412 }
4413
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004414#ifdef FEAT_FLOAT
4415 /*
4416 * If one of the two variables is a float, compare as a float.
4417 * When using "=~" or "!~", always compare as string.
4418 */
4419 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4420 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4421 {
4422 float_T f1, f2;
4423
4424 if (rettv->v_type == VAR_FLOAT)
4425 f1 = rettv->vval.v_float;
4426 else
4427 f1 = get_tv_number(rettv);
4428 if (var2.v_type == VAR_FLOAT)
4429 f2 = var2.vval.v_float;
4430 else
4431 f2 = get_tv_number(&var2);
4432 n1 = FALSE;
4433 switch (type)
4434 {
4435 case TYPE_EQUAL: n1 = (f1 == f2); break;
4436 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4437 case TYPE_GREATER: n1 = (f1 > f2); break;
4438 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4439 case TYPE_SMALLER: n1 = (f1 < f2); break;
4440 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4441 case TYPE_UNKNOWN:
4442 case TYPE_MATCH:
4443 case TYPE_NOMATCH: break; /* avoid gcc warning */
4444 }
4445 }
4446#endif
4447
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 /*
4449 * If one of the two variables is a number, compare as a number.
4450 * When using "=~" or "!~", always compare as string.
4451 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004452 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004455 n1 = get_tv_number(rettv);
4456 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 switch (type)
4458 {
4459 case TYPE_EQUAL: n1 = (n1 == n2); break;
4460 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4461 case TYPE_GREATER: n1 = (n1 > n2); break;
4462 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4463 case TYPE_SMALLER: n1 = (n1 < n2); break;
4464 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4465 case TYPE_UNKNOWN:
4466 case TYPE_MATCH:
4467 case TYPE_NOMATCH: break; /* avoid gcc warning */
4468 }
4469 }
4470 else
4471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004472 s1 = get_tv_string_buf(rettv, buf1);
4473 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4475 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4476 else
4477 i = 0;
4478 n1 = FALSE;
4479 switch (type)
4480 {
4481 case TYPE_EQUAL: n1 = (i == 0); break;
4482 case TYPE_NEQUAL: n1 = (i != 0); break;
4483 case TYPE_GREATER: n1 = (i > 0); break;
4484 case TYPE_GEQUAL: n1 = (i >= 0); break;
4485 case TYPE_SMALLER: n1 = (i < 0); break;
4486 case TYPE_SEQUAL: n1 = (i <= 0); break;
4487
4488 case TYPE_MATCH:
4489 case TYPE_NOMATCH:
4490 /* avoid 'l' flag in 'cpoptions' */
4491 save_cpo = p_cpo;
4492 p_cpo = (char_u *)"";
4493 regmatch.regprog = vim_regcomp(s2,
4494 RE_MAGIC + RE_STRING);
4495 regmatch.rm_ic = ic;
4496 if (regmatch.regprog != NULL)
4497 {
4498 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4499 vim_free(regmatch.regprog);
4500 if (type == TYPE_NOMATCH)
4501 n1 = !n1;
4502 }
4503 p_cpo = save_cpo;
4504 break;
4505
4506 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4507 }
4508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004509 clear_tv(rettv);
4510 clear_tv(&var2);
4511 rettv->v_type = VAR_NUMBER;
4512 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514 }
4515
4516 return OK;
4517}
4518
4519/*
4520 * Handle fourth level expression:
4521 * + number addition
4522 * - number subtraction
4523 * . string concatenation
4524 *
4525 * "arg" must point to the first non-white of the expression.
4526 * "arg" is advanced to the next non-white after the recognized expression.
4527 *
4528 * Return OK or FAIL.
4529 */
4530 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004531eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 int evaluate;
4535{
Bram Moolenaar33570922005-01-25 22:26:29 +00004536 typval_T var2;
4537 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 int op;
4539 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004540#ifdef FEAT_FLOAT
4541 float_T f1 = 0, f2 = 0;
4542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 char_u *s1, *s2;
4544 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4545 char_u *p;
4546
4547 /*
4548 * Get the first variable.
4549 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004550 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 return FAIL;
4552
4553 /*
4554 * Repeat computing, until no '+', '-' or '.' is following.
4555 */
4556 for (;;)
4557 {
4558 op = **arg;
4559 if (op != '+' && op != '-' && op != '.')
4560 break;
4561
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004562 if ((op != '+' || rettv->v_type != VAR_LIST)
4563#ifdef FEAT_FLOAT
4564 && (op == '.' || rettv->v_type != VAR_FLOAT)
4565#endif
4566 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004567 {
4568 /* For "list + ...", an illegal use of the first operand as
4569 * a number cannot be determined before evaluating the 2nd
4570 * operand: if this is also a list, all is ok.
4571 * For "something . ...", "something - ..." or "non-list + ...",
4572 * we know that the first operand needs to be a string or number
4573 * without evaluating the 2nd operand. So check before to avoid
4574 * side effects after an error. */
4575 if (evaluate && get_tv_string_chk(rettv) == NULL)
4576 {
4577 clear_tv(rettv);
4578 return FAIL;
4579 }
4580 }
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 /*
4583 * Get the second variable.
4584 */
4585 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004586 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004588 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 return FAIL;
4590 }
4591
4592 if (evaluate)
4593 {
4594 /*
4595 * Compute the result.
4596 */
4597 if (op == '.')
4598 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004599 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4600 s2 = get_tv_string_buf_chk(&var2, buf2);
4601 if (s2 == NULL) /* type error ? */
4602 {
4603 clear_tv(rettv);
4604 clear_tv(&var2);
4605 return FAIL;
4606 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004607 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 clear_tv(rettv);
4609 rettv->v_type = VAR_STRING;
4610 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004612 else if (op == '+' && rettv->v_type == VAR_LIST
4613 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004614 {
4615 /* concatenate Lists */
4616 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4617 &var3) == FAIL)
4618 {
4619 clear_tv(rettv);
4620 clear_tv(&var2);
4621 return FAIL;
4622 }
4623 clear_tv(rettv);
4624 *rettv = var3;
4625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 else
4627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004628 int error = FALSE;
4629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004630#ifdef FEAT_FLOAT
4631 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004632 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633 f1 = rettv->vval.v_float;
4634 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004635 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004636 else
4637#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004639 n1 = get_tv_number_chk(rettv, &error);
4640 if (error)
4641 {
4642 /* This can only happen for "list + non-list". For
4643 * "non-list + ..." or "something - ...", we returned
4644 * before evaluating the 2nd operand. */
4645 clear_tv(rettv);
4646 return FAIL;
4647 }
4648#ifdef FEAT_FLOAT
4649 if (var2.v_type == VAR_FLOAT)
4650 f1 = n1;
4651#endif
4652 }
4653#ifdef FEAT_FLOAT
4654 if (var2.v_type == VAR_FLOAT)
4655 {
4656 f2 = var2.vval.v_float;
4657 n2 = 0;
4658 }
4659 else
4660#endif
4661 {
4662 n2 = get_tv_number_chk(&var2, &error);
4663 if (error)
4664 {
4665 clear_tv(rettv);
4666 clear_tv(&var2);
4667 return FAIL;
4668 }
4669#ifdef FEAT_FLOAT
4670 if (rettv->v_type == VAR_FLOAT)
4671 f2 = n2;
4672#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004675
4676#ifdef FEAT_FLOAT
4677 /* If there is a float on either side the result is a float. */
4678 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4679 {
4680 if (op == '+')
4681 f1 = f1 + f2;
4682 else
4683 f1 = f1 - f2;
4684 rettv->v_type = VAR_FLOAT;
4685 rettv->vval.v_float = f1;
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004688#endif
4689 {
4690 if (op == '+')
4691 n1 = n1 + n2;
4692 else
4693 n1 = n1 - n2;
4694 rettv->v_type = VAR_NUMBER;
4695 rettv->vval.v_number = n1;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700 }
4701 return OK;
4702}
4703
4704/*
4705 * Handle fifth level expression:
4706 * * number multiplication
4707 * / number division
4708 * % number modulo
4709 *
4710 * "arg" must point to the first non-white of the expression.
4711 * "arg" is advanced to the next non-white after the recognized expression.
4712 *
4713 * Return OK or FAIL.
4714 */
4715 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004716eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004720 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
Bram Moolenaar33570922005-01-25 22:26:29 +00004722 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 int op;
4724 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004725#ifdef FEAT_FLOAT
4726 int use_float = FALSE;
4727 float_T f1 = 0, f2;
4728#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
4731 /*
4732 * Get the first variable.
4733 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004734 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 return FAIL;
4736
4737 /*
4738 * Repeat computing, until no '*', '/' or '%' is following.
4739 */
4740 for (;;)
4741 {
4742 op = **arg;
4743 if (op != '*' && op != '/' && op != '%')
4744 break;
4745
4746 if (evaluate)
4747 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004748#ifdef FEAT_FLOAT
4749 if (rettv->v_type == VAR_FLOAT)
4750 {
4751 f1 = rettv->vval.v_float;
4752 use_float = TRUE;
4753 n1 = 0;
4754 }
4755 else
4756#endif
4757 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 if (error)
4760 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762 else
4763 n1 = 0;
4764
4765 /*
4766 * Get the second variable.
4767 */
4768 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004769 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 return FAIL;
4771
4772 if (evaluate)
4773 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004774#ifdef FEAT_FLOAT
4775 if (var2.v_type == VAR_FLOAT)
4776 {
4777 if (!use_float)
4778 {
4779 f1 = n1;
4780 use_float = TRUE;
4781 }
4782 f2 = var2.vval.v_float;
4783 n2 = 0;
4784 }
4785 else
4786#endif
4787 {
4788 n2 = get_tv_number_chk(&var2, &error);
4789 clear_tv(&var2);
4790 if (error)
4791 return FAIL;
4792#ifdef FEAT_FLOAT
4793 if (use_float)
4794 f2 = n2;
4795#endif
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
4798 /*
4799 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004802#ifdef FEAT_FLOAT
4803 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805 if (op == '*')
4806 f1 = f1 * f2;
4807 else if (op == '/')
4808 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004809# ifdef VMS
4810 /* VMS crashes on divide by zero, work around it */
4811 if (f2 == 0.0)
4812 {
4813 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004814 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004815 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004816 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004817 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004818 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004819 }
4820 else
4821 f1 = f1 / f2;
4822# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004823 /* We rely on the floating point library to handle divide
4824 * by zero to result in "inf" and not a crash. */
4825 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004826# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004829 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004830 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831 return FAIL;
4832 }
4833 rettv->v_type = VAR_FLOAT;
4834 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839 if (op == '*')
4840 n1 = n1 * n2;
4841 else if (op == '/')
4842 {
4843 if (n2 == 0) /* give an error message? */
4844 {
4845 if (n1 == 0)
4846 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4847 else if (n1 < 0)
4848 n1 = -0x7fffffffL;
4849 else
4850 n1 = 0x7fffffffL;
4851 }
4852 else
4853 n1 = n1 / n2;
4854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856 {
4857 if (n2 == 0) /* give an error message? */
4858 n1 = 0;
4859 else
4860 n1 = n1 % n2;
4861 }
4862 rettv->v_type = VAR_NUMBER;
4863 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 }
4867
4868 return OK;
4869}
4870
4871/*
4872 * Handle sixth level expression:
4873 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004874 * "string" string constant
4875 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 * &option-name option value
4877 * @r register contents
4878 * identifier variable value
4879 * function() function call
4880 * $VAR environment variable
4881 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004882 * [expr, expr] List
4883 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 *
4885 * Also handle:
4886 * ! in front logical NOT
4887 * - in front unary minus
4888 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004889 * trailing [] subscript in String or List
4890 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 *
4892 * "arg" must point to the first non-white of the expression.
4893 * "arg" is advanced to the next non-white after the recognized expression.
4894 *
4895 * Return OK or FAIL.
4896 */
4897 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004898eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004902 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 long n;
4905 int len;
4906 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 char_u *start_leader, *end_leader;
4908 int ret = OK;
4909 char_u *alias;
4910
4911 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004913 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
4917 /*
4918 * Skip '!' and '-' characters. They are handled later.
4919 */
4920 start_leader = *arg;
4921 while (**arg == '!' || **arg == '-' || **arg == '+')
4922 *arg = skipwhite(*arg + 1);
4923 end_leader = *arg;
4924
4925 switch (**arg)
4926 {
4927 /*
4928 * Number constant.
4929 */
4930 case '0':
4931 case '1':
4932 case '2':
4933 case '3':
4934 case '4':
4935 case '5':
4936 case '6':
4937 case '7':
4938 case '8':
4939 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940 {
4941#ifdef FEAT_FLOAT
4942 char_u *p = skipdigits(*arg + 1);
4943 int get_float = FALSE;
4944
4945 /* We accept a float when the format matches
4946 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004947 * strict to avoid backwards compatibility problems.
4948 * Don't look for a float after the "." operator, so that
4949 * ":let vers = 1.2.3" doesn't fail. */
4950 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952 get_float = TRUE;
4953 p = skipdigits(p + 2);
4954 if (*p == 'e' || *p == 'E')
4955 {
4956 ++p;
4957 if (*p == '-' || *p == '+')
4958 ++p;
4959 if (!vim_isdigit(*p))
4960 get_float = FALSE;
4961 else
4962 p = skipdigits(p + 1);
4963 }
4964 if (ASCII_ISALPHA(*p) || *p == '.')
4965 get_float = FALSE;
4966 }
4967 if (get_float)
4968 {
4969 float_T f;
4970
4971 *arg += string2float(*arg, &f);
4972 if (evaluate)
4973 {
4974 rettv->v_type = VAR_FLOAT;
4975 rettv->vval.v_float = f;
4976 }
4977 }
4978 else
4979#endif
4980 {
4981 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4982 *arg += len;
4983 if (evaluate)
4984 {
4985 rettv->v_type = VAR_NUMBER;
4986 rettv->vval.v_number = n;
4987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
4992 /*
4993 * String constant: "string".
4994 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 break;
4997
4998 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005001 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002 break;
5003
5004 /*
5005 * List: [expr, expr]
5006 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005007 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 break;
5009
5010 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005011 * Dictionary: {key: val, key: val}
5012 */
5013 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5014 break;
5015
5016 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005017 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005019 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 break;
5021
5022 /*
5023 * Environment variable: $VAR.
5024 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005025 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 break;
5027
5028 /*
5029 * Register contents: @r.
5030 */
5031 case '@': ++*arg;
5032 if (evaluate)
5033 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005034 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005035 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 if (**arg != NUL)
5038 ++*arg;
5039 break;
5040
5041 /*
5042 * nested expression: (expression).
5043 */
5044 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005045 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (**arg == ')')
5047 ++*arg;
5048 else if (ret == OK)
5049 {
5050 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 ret = FAIL;
5053 }
5054 break;
5055
Bram Moolenaar8c711452005-01-14 21:53:12 +00005056 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 break;
5058 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059
5060 if (ret == NOTDONE)
5061 {
5062 /*
5063 * Must be a variable or function name.
5064 * Can also be a curly-braces kind of name: {expr}.
5065 */
5066 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005067 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 if (alias != NULL)
5069 s = alias;
5070
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005071 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 ret = FAIL;
5073 else
5074 {
5075 if (**arg == '(') /* recursive! */
5076 {
5077 /* If "s" is the name of a variable of type VAR_FUNC
5078 * use its contents. */
5079 s = deref_func_name(s, &len);
5080
5081 /* Invoke the function. */
5082 ret = get_func_tv(s, len, rettv, arg,
5083 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005084 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 /* Stop the expression evaluation when immediately
5086 * aborting on error, or when an interrupt occurred or
5087 * an exception was thrown but not caught. */
5088 if (aborting())
5089 {
5090 if (ret == OK)
5091 clear_tv(rettv);
5092 ret = FAIL;
5093 }
5094 }
5095 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005096 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005097 else
5098 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 }
5100
5101 if (alias != NULL)
5102 vim_free(alias);
5103 }
5104
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 *arg = skipwhite(*arg);
5106
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005107 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5108 * expr(expr). */
5109 if (ret == OK)
5110 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112 /*
5113 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5114 */
5115 if (ret == OK && evaluate && end_leader > start_leader)
5116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005117 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 int val = 0;
5119#ifdef FEAT_FLOAT
5120 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005121
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005122 if (rettv->v_type == VAR_FLOAT)
5123 f = rettv->vval.v_float;
5124 else
5125#endif
5126 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005127 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005129 clear_tv(rettv);
5130 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005132 else
5133 {
5134 while (end_leader > start_leader)
5135 {
5136 --end_leader;
5137 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 {
5139#ifdef FEAT_FLOAT
5140 if (rettv->v_type == VAR_FLOAT)
5141 f = !f;
5142 else
5143#endif
5144 val = !val;
5145 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005146 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005147 {
5148#ifdef FEAT_FLOAT
5149 if (rettv->v_type == VAR_FLOAT)
5150 f = -f;
5151 else
5152#endif
5153 val = -val;
5154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005155 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005156#ifdef FEAT_FLOAT
5157 if (rettv->v_type == VAR_FLOAT)
5158 {
5159 clear_tv(rettv);
5160 rettv->vval.v_float = f;
5161 }
5162 else
5163#endif
5164 {
5165 clear_tv(rettv);
5166 rettv->v_type = VAR_NUMBER;
5167 rettv->vval.v_number = val;
5168 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171
5172 return ret;
5173}
5174
5175/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005176 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5177 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5179 */
5180 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005182 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005183 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005184 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005185 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186{
5187 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005190 long len = -1;
5191 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005192 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005195 if (rettv->v_type == VAR_FUNC
5196#ifdef FEAT_FLOAT
5197 || rettv->v_type == VAR_FLOAT
5198#endif
5199 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005200 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005201 if (verbose)
5202 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203 return FAIL;
5204 }
5205
Bram Moolenaar8c711452005-01-14 21:53:12 +00005206 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005207 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 /*
5209 * dict.name
5210 */
5211 key = *arg + 1;
5212 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5213 ;
5214 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005215 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005217 }
5218 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005220 /*
5221 * something[idx]
5222 *
5223 * Get the (first) variable from inside the [].
5224 */
5225 *arg = skipwhite(*arg + 1);
5226 if (**arg == ':')
5227 empty1 = TRUE;
5228 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5229 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005230 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5231 {
5232 /* not a number or string */
5233 clear_tv(&var1);
5234 return FAIL;
5235 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236
5237 /*
5238 * Get the second variable from inside the [:].
5239 */
5240 if (**arg == ':')
5241 {
5242 range = TRUE;
5243 *arg = skipwhite(*arg + 1);
5244 if (**arg == ']')
5245 empty2 = TRUE;
5246 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5247 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 if (!empty1)
5249 clear_tv(&var1);
5250 return FAIL;
5251 }
5252 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5253 {
5254 /* not a number or string */
5255 if (!empty1)
5256 clear_tv(&var1);
5257 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 return FAIL;
5259 }
5260 }
5261
5262 /* Check for the ']'. */
5263 if (**arg != ']')
5264 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 if (verbose)
5266 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005267 clear_tv(&var1);
5268 if (range)
5269 clear_tv(&var2);
5270 return FAIL;
5271 }
5272 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273 }
5274
5275 if (evaluate)
5276 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 n1 = 0;
5278 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005280 n1 = get_tv_number(&var1);
5281 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 }
5283 if (range)
5284 {
5285 if (empty2)
5286 n2 = -1;
5287 else
5288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005289 n2 = get_tv_number(&var2);
5290 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 }
5292 }
5293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005294 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 {
5296 case VAR_NUMBER:
5297 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005298 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 len = (long)STRLEN(s);
5300 if (range)
5301 {
5302 /* The resulting variable is a substring. If the indexes
5303 * are out of range the result is empty. */
5304 if (n1 < 0)
5305 {
5306 n1 = len + n1;
5307 if (n1 < 0)
5308 n1 = 0;
5309 }
5310 if (n2 < 0)
5311 n2 = len + n2;
5312 else if (n2 >= len)
5313 n2 = len;
5314 if (n1 >= len || n2 < 0 || n1 > n2)
5315 s = NULL;
5316 else
5317 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5318 }
5319 else
5320 {
5321 /* The resulting variable is a string of a single
5322 * character. If the index is too big or negative the
5323 * result is empty. */
5324 if (n1 >= len || n1 < 0)
5325 s = NULL;
5326 else
5327 s = vim_strnsave(s + n1, 1);
5328 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005329 clear_tv(rettv);
5330 rettv->v_type = VAR_STRING;
5331 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005332 break;
5333
5334 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005335 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005336 if (n1 < 0)
5337 n1 = len + n1;
5338 if (!empty1 && (n1 < 0 || n1 >= len))
5339 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005340 /* For a range we allow invalid values and return an empty
5341 * list. A list index out of range is an error. */
5342 if (!range)
5343 {
5344 if (verbose)
5345 EMSGN(_(e_listidx), n1);
5346 return FAIL;
5347 }
5348 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 }
5350 if (range)
5351 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005352 list_T *l;
5353 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354
5355 if (n2 < 0)
5356 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005357 else if (n2 >= len)
5358 n2 = len - 1;
5359 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005360 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 l = list_alloc();
5362 if (l == NULL)
5363 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 n1 <= n2; ++n1)
5366 {
5367 if (list_append_tv(l, &item->li_tv) == FAIL)
5368 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005369 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 return FAIL;
5371 }
5372 item = item->li_next;
5373 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 clear_tv(rettv);
5375 rettv->v_type = VAR_LIST;
5376 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005377 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 }
5379 else
5380 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005381 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 clear_tv(rettv);
5383 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 }
5385 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005386
5387 case VAR_DICT:
5388 if (range)
5389 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005390 if (verbose)
5391 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 if (len == -1)
5393 clear_tv(&var1);
5394 return FAIL;
5395 }
5396 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398
5399 if (len == -1)
5400 {
5401 key = get_tv_string(&var1);
5402 if (*key == NUL)
5403 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005404 if (verbose)
5405 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 clear_tv(&var1);
5407 return FAIL;
5408 }
5409 }
5410
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005411 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005413 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005414 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415 if (len == -1)
5416 clear_tv(&var1);
5417 if (item == NULL)
5418 return FAIL;
5419
5420 copy_tv(&item->di_tv, &var1);
5421 clear_tv(rettv);
5422 *rettv = var1;
5423 }
5424 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 }
5426 }
5427
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 return OK;
5429}
5430
5431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 * Get an option value.
5433 * "arg" points to the '&' or '+' before the option name.
5434 * "arg" is advanced to character after the option name.
5435 * Return OK or FAIL.
5436 */
5437 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005438get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005440 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 int evaluate;
5442{
5443 char_u *option_end;
5444 long numval;
5445 char_u *stringval;
5446 int opt_type;
5447 int c;
5448 int working = (**arg == '+'); /* has("+option") */
5449 int ret = OK;
5450 int opt_flags;
5451
5452 /*
5453 * Isolate the option name and find its value.
5454 */
5455 option_end = find_option_end(arg, &opt_flags);
5456 if (option_end == NULL)
5457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 EMSG2(_("E112: Option name missing: %s"), *arg);
5460 return FAIL;
5461 }
5462
5463 if (!evaluate)
5464 {
5465 *arg = option_end;
5466 return OK;
5467 }
5468
5469 c = *option_end;
5470 *option_end = NUL;
5471 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
5474 if (opt_type == -3) /* invalid name */
5475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005476 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 EMSG2(_("E113: Unknown option: %s"), *arg);
5478 ret = FAIL;
5479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
5482 if (opt_type == -2) /* hidden string option */
5483 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005484 rettv->v_type = VAR_STRING;
5485 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487 else if (opt_type == -1) /* hidden number option */
5488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005489 rettv->v_type = VAR_NUMBER;
5490 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492 else if (opt_type == 1) /* number option */
5493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 rettv->v_type = VAR_NUMBER;
5495 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 }
5497 else /* string option */
5498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005499 rettv->v_type = VAR_STRING;
5500 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502 }
5503 else if (working && (opt_type == -2 || opt_type == -1))
5504 ret = FAIL;
5505
5506 *option_end = c; /* put back for error messages */
5507 *arg = option_end;
5508
5509 return ret;
5510}
5511
5512/*
5513 * Allocate a variable for a string constant.
5514 * Return OK or FAIL.
5515 */
5516 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 int evaluate;
5521{
5522 char_u *p;
5523 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 int extra = 0;
5525
5526 /*
5527 * Find the end of the string, skipping backslashed characters.
5528 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005529 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
5531 if (*p == '\\' && p[1] != NUL)
5532 {
5533 ++p;
5534 /* A "\<x>" form occupies at least 4 characters, and produces up
5535 * to 6 characters: reserve space for 2 extra */
5536 if (*p == '<')
5537 extra += 2;
5538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 }
5540
5541 if (*p != '"')
5542 {
5543 EMSG2(_("E114: Missing quote: %s"), *arg);
5544 return FAIL;
5545 }
5546
5547 /* If only parsing, set *arg and return here */
5548 if (!evaluate)
5549 {
5550 *arg = p + 1;
5551 return OK;
5552 }
5553
5554 /*
5555 * Copy the string into allocated memory, handling backslashed
5556 * characters.
5557 */
5558 name = alloc((unsigned)(p - *arg + extra));
5559 if (name == NULL)
5560 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561 rettv->v_type = VAR_STRING;
5562 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
Bram Moolenaar8c711452005-01-14 21:53:12 +00005564 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
5566 if (*p == '\\')
5567 {
5568 switch (*++p)
5569 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005570 case 'b': *name++ = BS; ++p; break;
5571 case 'e': *name++ = ESC; ++p; break;
5572 case 'f': *name++ = FF; ++p; break;
5573 case 'n': *name++ = NL; ++p; break;
5574 case 'r': *name++ = CAR; ++p; break;
5575 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
5577 case 'X': /* hex: "\x1", "\x12" */
5578 case 'x':
5579 case 'u': /* Unicode: "\u0023" */
5580 case 'U':
5581 if (vim_isxdigit(p[1]))
5582 {
5583 int n, nr;
5584 int c = toupper(*p);
5585
5586 if (c == 'X')
5587 n = 2;
5588 else
5589 n = 4;
5590 nr = 0;
5591 while (--n >= 0 && vim_isxdigit(p[1]))
5592 {
5593 ++p;
5594 nr = (nr << 4) + hex2nr(*p);
5595 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597#ifdef FEAT_MBYTE
5598 /* For "\u" store the number according to
5599 * 'encoding'. */
5600 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005601 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 else
5603#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 break;
5607
5608 /* octal: "\1", "\12", "\123" */
5609 case '0':
5610 case '1':
5611 case '2':
5612 case '3':
5613 case '4':
5614 case '5':
5615 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005616 case '7': *name = *p++ - '0';
5617 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 *name = (*name << 3) + *p++ - '0';
5620 if (*p >= '0' && *p <= '7')
5621 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 break;
5625
5626 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005627 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 if (extra != 0)
5629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 break;
5632 }
5633 /* FALLTHROUGH */
5634
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 break;
5637 }
5638 }
5639 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 *arg = p + 1;
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 return OK;
5647}
5648
5649/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 * Return OK or FAIL.
5652 */
5653 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 int evaluate;
5658{
5659 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005660 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005661 int reduce = 0;
5662
5663 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005664 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 */
5666 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5667 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005668 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005671 break;
5672 ++reduce;
5673 ++p;
5674 }
5675 }
5676
Bram Moolenaar8c711452005-01-14 21:53:12 +00005677 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005680 return FAIL;
5681 }
5682
Bram Moolenaar8c711452005-01-14 21:53:12 +00005683 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005684 if (!evaluate)
5685 {
5686 *arg = p + 1;
5687 return OK;
5688 }
5689
5690 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005691 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005692 */
5693 str = alloc((unsigned)((p - *arg) - reduce));
5694 if (str == NULL)
5695 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005696 rettv->v_type = VAR_STRING;
5697 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005698
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005701 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005704 break;
5705 ++p;
5706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005710 *arg = p + 1;
5711
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 return OK;
5713}
5714
5715/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005716 * Allocate a variable for a List and fill it from "*arg".
5717 * Return OK or FAIL.
5718 */
5719 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005720get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005722 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005723 int evaluate;
5724{
Bram Moolenaar33570922005-01-25 22:26:29 +00005725 list_T *l = NULL;
5726 typval_T tv;
5727 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728
5729 if (evaluate)
5730 {
5731 l = list_alloc();
5732 if (l == NULL)
5733 return FAIL;
5734 }
5735
5736 *arg = skipwhite(*arg + 1);
5737 while (**arg != ']' && **arg != NUL)
5738 {
5739 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5740 goto failret;
5741 if (evaluate)
5742 {
5743 item = listitem_alloc();
5744 if (item != NULL)
5745 {
5746 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005747 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748 list_append(l, item);
5749 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005750 else
5751 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005752 }
5753
5754 if (**arg == ']')
5755 break;
5756 if (**arg != ',')
5757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759 goto failret;
5760 }
5761 *arg = skipwhite(*arg + 1);
5762 }
5763
5764 if (**arg != ']')
5765 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005767failret:
5768 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005769 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770 return FAIL;
5771 }
5772
5773 *arg = skipwhite(*arg + 1);
5774 if (evaluate)
5775 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005776 rettv->v_type = VAR_LIST;
5777 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778 ++l->lv_refcount;
5779 }
5780
5781 return OK;
5782}
5783
5784/*
5785 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005786 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005787 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005788 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789list_alloc()
5790{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005791 list_T *l;
5792
5793 l = (list_T *)alloc_clear(sizeof(list_T));
5794 if (l != NULL)
5795 {
5796 /* Prepend the list to the list of lists for garbage collection. */
5797 if (first_list != NULL)
5798 first_list->lv_used_prev = l;
5799 l->lv_used_prev = NULL;
5800 l->lv_used_next = first_list;
5801 first_list = l;
5802 }
5803 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804}
5805
5806/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005807 * Allocate an empty list for a return value.
5808 * Returns OK or FAIL.
5809 */
5810 static int
5811rettv_list_alloc(rettv)
5812 typval_T *rettv;
5813{
5814 list_T *l = list_alloc();
5815
5816 if (l == NULL)
5817 return FAIL;
5818
5819 rettv->vval.v_list = l;
5820 rettv->v_type = VAR_LIST;
5821 ++l->lv_refcount;
5822 return OK;
5823}
5824
5825/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 * Unreference a list: decrement the reference count and free it when it
5827 * becomes zero.
5828 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005829 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005831 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005833 if (l != NULL && --l->lv_refcount <= 0)
5834 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835}
5836
5837/*
5838 * Free a list, including all items it points to.
5839 * Ignores the reference count.
5840 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005841 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005842list_free(l, recurse)
5843 list_T *l;
5844 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845{
Bram Moolenaar33570922005-01-25 22:26:29 +00005846 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005847
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005848 /* Remove the list from the list of lists for garbage collection. */
5849 if (l->lv_used_prev == NULL)
5850 first_list = l->lv_used_next;
5851 else
5852 l->lv_used_prev->lv_used_next = l->lv_used_next;
5853 if (l->lv_used_next != NULL)
5854 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5855
Bram Moolenaard9fba312005-06-26 22:34:35 +00005856 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005857 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005858 /* Remove the item before deleting it. */
5859 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005860 if (recurse || (item->li_tv.v_type != VAR_LIST
5861 && item->li_tv.v_type != VAR_DICT))
5862 clear_tv(&item->li_tv);
5863 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 }
5865 vim_free(l);
5866}
5867
5868/*
5869 * Allocate a list item.
5870 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005871 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872listitem_alloc()
5873{
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875}
5876
5877/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005878 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005879 */
5880 static void
5881listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005882 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005884 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005885 vim_free(item);
5886}
5887
5888/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005889 * Remove a list item from a List and free it. Also clears the value.
5890 */
5891 static void
5892listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005893 list_T *l;
5894 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005895{
5896 list_remove(l, item, item);
5897 listitem_free(item);
5898}
5899
5900/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 * Get the number of items in a list.
5902 */
5903 static long
5904list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005905 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 if (l == NULL)
5908 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005909 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910}
5911
5912/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005913 * Return TRUE when two lists have exactly the same values.
5914 */
5915 static int
5916list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 list_T *l1;
5918 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005919 int ic; /* ignore case for strings */
5920{
Bram Moolenaar33570922005-01-25 22:26:29 +00005921 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005922
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005923 if (l1 == NULL || l2 == NULL)
5924 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005925 if (l1 == l2)
5926 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005927 if (list_len(l1) != list_len(l2))
5928 return FALSE;
5929
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005930 for (item1 = l1->lv_first, item2 = l2->lv_first;
5931 item1 != NULL && item2 != NULL;
5932 item1 = item1->li_next, item2 = item2->li_next)
5933 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5934 return FALSE;
5935 return item1 == NULL && item2 == NULL;
5936}
5937
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005938#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5939 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005940/*
5941 * Return the dictitem that an entry in a hashtable points to.
5942 */
5943 dictitem_T *
5944dict_lookup(hi)
5945 hashitem_T *hi;
5946{
5947 return HI2DI(hi);
5948}
5949#endif
5950
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005951/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005952 * Return TRUE when two dictionaries have exactly the same key/values.
5953 */
5954 static int
5955dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005956 dict_T *d1;
5957 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005958 int ic; /* ignore case for strings */
5959{
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 hashitem_T *hi;
5961 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005962 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005963
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005964 if (d1 == NULL || d2 == NULL)
5965 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005966 if (d1 == d2)
5967 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005968 if (dict_len(d1) != dict_len(d2))
5969 return FALSE;
5970
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005971 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005972 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005973 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005974 if (!HASHITEM_EMPTY(hi))
5975 {
5976 item2 = dict_find(d2, hi->hi_key, -1);
5977 if (item2 == NULL)
5978 return FALSE;
5979 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5980 return FALSE;
5981 --todo;
5982 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005983 }
5984 return TRUE;
5985}
5986
5987/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005988 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005989 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005990 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005991 */
5992 static int
5993tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005994 typval_T *tv1;
5995 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 int ic; /* ignore case */
5997{
5998 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005999 char_u *s1, *s2;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006000 static int recursive = 0; /* cach recursive loops */
6001 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006003 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006004 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006005 /* Catch lists and dicts that have an endless loop by limiting
6006 * recursiveness to 1000. We guess they are equal then. */
6007 if (recursive >= 1000)
6008 return TRUE;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006009
6010 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006011 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006012 case VAR_LIST:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006013 ++recursive;
6014 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
6015 --recursive;
6016 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006017
6018 case VAR_DICT:
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006019 ++recursive;
6020 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
6021 --recursive;
6022 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006023
6024 case VAR_FUNC:
6025 return (tv1->vval.v_string != NULL
6026 && tv2->vval.v_string != NULL
6027 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6028
6029 case VAR_NUMBER:
6030 return tv1->vval.v_number == tv2->vval.v_number;
6031
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006032#ifdef FEAT_FLOAT
6033 case VAR_FLOAT:
6034 return tv1->vval.v_float == tv2->vval.v_float;
6035#endif
6036
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006037 case VAR_STRING:
6038 s1 = get_tv_string_buf(tv1, buf1);
6039 s2 = get_tv_string_buf(tv2, buf2);
6040 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006041 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006042
6043 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006044 return TRUE;
6045}
6046
6047/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 * Locate item with index "n" in list "l" and return it.
6049 * A negative index is counted from the end; -1 is the last item.
6050 * Returns NULL when "n" is out of range.
6051 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 long n;
6056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058 long idx;
6059
6060 if (l == NULL)
6061 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006062
6063 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006065 n = l->lv_len + n;
6066
6067 /* Check for index out of range. */
6068 if (n < 0 || n >= l->lv_len)
6069 return NULL;
6070
6071 /* When there is a cached index may start search from there. */
6072 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006074 if (n < l->lv_idx / 2)
6075 {
6076 /* closest to the start of the list */
6077 item = l->lv_first;
6078 idx = 0;
6079 }
6080 else if (n > (l->lv_idx + l->lv_len) / 2)
6081 {
6082 /* closest to the end of the list */
6083 item = l->lv_last;
6084 idx = l->lv_len - 1;
6085 }
6086 else
6087 {
6088 /* closest to the cached index */
6089 item = l->lv_idx_item;
6090 idx = l->lv_idx;
6091 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092 }
6093 else
6094 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006095 if (n < l->lv_len / 2)
6096 {
6097 /* closest to the start of the list */
6098 item = l->lv_first;
6099 idx = 0;
6100 }
6101 else
6102 {
6103 /* closest to the end of the list */
6104 item = l->lv_last;
6105 idx = l->lv_len - 1;
6106 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006108
6109 while (n > idx)
6110 {
6111 /* search forward */
6112 item = item->li_next;
6113 ++idx;
6114 }
6115 while (n < idx)
6116 {
6117 /* search backward */
6118 item = item->li_prev;
6119 --idx;
6120 }
6121
6122 /* cache the used index */
6123 l->lv_idx = idx;
6124 l->lv_idx_item = item;
6125
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006126 return item;
6127}
6128
6129/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006130 * Get list item "l[idx]" as a number.
6131 */
6132 static long
6133list_find_nr(l, idx, errorp)
6134 list_T *l;
6135 long idx;
6136 int *errorp; /* set to TRUE when something wrong */
6137{
6138 listitem_T *li;
6139
6140 li = list_find(l, idx);
6141 if (li == NULL)
6142 {
6143 if (errorp != NULL)
6144 *errorp = TRUE;
6145 return -1L;
6146 }
6147 return get_tv_number_chk(&li->li_tv, errorp);
6148}
6149
6150/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006151 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6152 */
6153 char_u *
6154list_find_str(l, idx)
6155 list_T *l;
6156 long idx;
6157{
6158 listitem_T *li;
6159
6160 li = list_find(l, idx - 1);
6161 if (li == NULL)
6162 {
6163 EMSGN(_(e_listidx), idx);
6164 return NULL;
6165 }
6166 return get_tv_string(&li->li_tv);
6167}
6168
6169/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006170 * Locate "item" list "l" and return its index.
6171 * Returns -1 when "item" is not in the list.
6172 */
6173 static long
6174list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 list_T *l;
6176 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006177{
6178 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006180
6181 if (l == NULL)
6182 return -1;
6183 idx = 0;
6184 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6185 ++idx;
6186 if (li == NULL)
6187 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006188 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006189}
6190
6191/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006192 * Append item "item" to the end of list "l".
6193 */
6194 static void
6195list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006196 list_T *l;
6197 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198{
6199 if (l->lv_last == NULL)
6200 {
6201 /* empty list */
6202 l->lv_first = item;
6203 l->lv_last = item;
6204 item->li_prev = NULL;
6205 }
6206 else
6207 {
6208 l->lv_last->li_next = item;
6209 item->li_prev = l->lv_last;
6210 l->lv_last = item;
6211 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006212 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006213 item->li_next = NULL;
6214}
6215
6216/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006217 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006218 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006219 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006220 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006221list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006222 list_T *l;
6223 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006225 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006226
Bram Moolenaar05159a02005-02-26 23:04:13 +00006227 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006228 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006229 copy_tv(tv, &li->li_tv);
6230 list_append(l, li);
6231 return OK;
6232}
6233
6234/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006235 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006236 * Return FAIL when out of memory.
6237 */
6238 int
6239list_append_dict(list, dict)
6240 list_T *list;
6241 dict_T *dict;
6242{
6243 listitem_T *li = listitem_alloc();
6244
6245 if (li == NULL)
6246 return FAIL;
6247 li->li_tv.v_type = VAR_DICT;
6248 li->li_tv.v_lock = 0;
6249 li->li_tv.vval.v_dict = dict;
6250 list_append(list, li);
6251 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006252 return OK;
6253}
6254
6255/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006256 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006257 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006258 * Returns FAIL when out of memory.
6259 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006260 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006261list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006262 list_T *l;
6263 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006264 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006265{
6266 listitem_T *li = listitem_alloc();
6267
6268 if (li == NULL)
6269 return FAIL;
6270 list_append(l, li);
6271 li->li_tv.v_type = VAR_STRING;
6272 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006273 if (str == NULL)
6274 li->li_tv.vval.v_string = NULL;
6275 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006276 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006277 return FAIL;
6278 return OK;
6279}
6280
6281/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006282 * Append "n" to list "l".
6283 * Returns FAIL when out of memory.
6284 */
6285 static int
6286list_append_number(l, n)
6287 list_T *l;
6288 varnumber_T n;
6289{
6290 listitem_T *li;
6291
6292 li = listitem_alloc();
6293 if (li == NULL)
6294 return FAIL;
6295 li->li_tv.v_type = VAR_NUMBER;
6296 li->li_tv.v_lock = 0;
6297 li->li_tv.vval.v_number = n;
6298 list_append(l, li);
6299 return OK;
6300}
6301
6302/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006303 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006304 * If "item" is NULL append at the end.
6305 * Return FAIL when out of memory.
6306 */
6307 static int
6308list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006309 list_T *l;
6310 typval_T *tv;
6311 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006312{
Bram Moolenaar33570922005-01-25 22:26:29 +00006313 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006314
6315 if (ni == NULL)
6316 return FAIL;
6317 copy_tv(tv, &ni->li_tv);
6318 if (item == NULL)
6319 /* Append new item at end of list. */
6320 list_append(l, ni);
6321 else
6322 {
6323 /* Insert new item before existing item. */
6324 ni->li_prev = item->li_prev;
6325 ni->li_next = item;
6326 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006327 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006328 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006329 ++l->lv_idx;
6330 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006331 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006332 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006333 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006334 l->lv_idx_item = NULL;
6335 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006337 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006338 }
6339 return OK;
6340}
6341
6342/*
6343 * Extend "l1" with "l2".
6344 * If "bef" is NULL append at the end, otherwise insert before this item.
6345 * Returns FAIL when out of memory.
6346 */
6347 static int
6348list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 list_T *l1;
6350 list_T *l2;
6351 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006352{
Bram Moolenaar33570922005-01-25 22:26:29 +00006353 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006354 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006355
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006356 /* We also quit the loop when we have inserted the original item count of
6357 * the list, avoid a hang when we extend a list with itself. */
6358 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006359 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6360 return FAIL;
6361 return OK;
6362}
6363
6364/*
6365 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6366 * Return FAIL when out of memory.
6367 */
6368 static int
6369list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006370 list_T *l1;
6371 list_T *l2;
6372 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006373{
Bram Moolenaar33570922005-01-25 22:26:29 +00006374 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006375
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006376 if (l1 == NULL || l2 == NULL)
6377 return FAIL;
6378
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006380 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006381 if (l == NULL)
6382 return FAIL;
6383 tv->v_type = VAR_LIST;
6384 tv->vval.v_list = l;
6385
6386 /* append all items from the second list */
6387 return list_extend(l, l2, NULL);
6388}
6389
6390/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006391 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006392 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006393 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 * Returns NULL when out of memory.
6395 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006396 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006397list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006400 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401{
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 list_T *copy;
6403 listitem_T *item;
6404 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405
6406 if (orig == NULL)
6407 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006408
6409 copy = list_alloc();
6410 if (copy != NULL)
6411 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006412 if (copyID != 0)
6413 {
6414 /* Do this before adding the items, because one of the items may
6415 * refer back to this list. */
6416 orig->lv_copyID = copyID;
6417 orig->lv_copylist = copy;
6418 }
6419 for (item = orig->lv_first; item != NULL && !got_int;
6420 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 {
6422 ni = listitem_alloc();
6423 if (ni == NULL)
6424 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006425 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006426 {
6427 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6428 {
6429 vim_free(ni);
6430 break;
6431 }
6432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006434 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435 list_append(copy, ni);
6436 }
6437 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006438 if (item != NULL)
6439 {
6440 list_unref(copy);
6441 copy = NULL;
6442 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006443 }
6444
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445 return copy;
6446}
6447
6448/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006449 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006450 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006452 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006453list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 list_T *l;
6455 listitem_T *item;
6456 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457{
Bram Moolenaar33570922005-01-25 22:26:29 +00006458 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 /* notify watchers */
6461 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006462 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006463 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006464 list_fix_watch(l, ip);
6465 if (ip == item2)
6466 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006468
6469 if (item2->li_next == NULL)
6470 l->lv_last = item->li_prev;
6471 else
6472 item2->li_next->li_prev = item->li_prev;
6473 if (item->li_prev == NULL)
6474 l->lv_first = item2->li_next;
6475 else
6476 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006477 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478}
6479
6480/*
6481 * Return an allocated string with the string representation of a list.
6482 * May return NULL.
6483 */
6484 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006485list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006487 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006488{
6489 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490
6491 if (tv->vval.v_list == NULL)
6492 return NULL;
6493 ga_init2(&ga, (int)sizeof(char), 80);
6494 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006495 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496 {
6497 vim_free(ga.ga_data);
6498 return NULL;
6499 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500 ga_append(&ga, ']');
6501 ga_append(&ga, NUL);
6502 return (char_u *)ga.ga_data;
6503}
6504
6505/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006506 * Join list "l" into a string in "*gap", using separator "sep".
6507 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006508 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006509 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006511list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006512 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006513 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006514 char_u *sep;
6515 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006516 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006517{
6518 int first = TRUE;
6519 char_u *tofree;
6520 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006522 char_u *s;
6523
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006525 {
6526 if (first)
6527 first = FALSE;
6528 else
6529 ga_concat(gap, sep);
6530
6531 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006532 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006533 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006534 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006535 if (s != NULL)
6536 ga_concat(gap, s);
6537 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006538 if (s == NULL)
6539 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006540 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006541 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006542 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006543}
6544
6545/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006546 * Garbage collection for lists and dictionaries.
6547 *
6548 * We use reference counts to be able to free most items right away when they
6549 * are no longer used. But for composite items it's possible that it becomes
6550 * unused while the reference count is > 0: When there is a recursive
6551 * reference. Example:
6552 * :let l = [1, 2, 3]
6553 * :let d = {9: l}
6554 * :let l[1] = d
6555 *
6556 * Since this is quite unusual we handle this with garbage collection: every
6557 * once in a while find out which lists and dicts are not referenced from any
6558 * variable.
6559 *
6560 * Here is a good reference text about garbage collection (refers to Python
6561 * but it applies to all reference-counting mechanisms):
6562 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006563 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006564
6565/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006566 * Do garbage collection for lists and dicts.
6567 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006568 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006569 int
6570garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006571{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006572 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006573 buf_T *buf;
6574 win_T *wp;
6575 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006576 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006577 int did_free;
6578 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006579#ifdef FEAT_WINDOWS
6580 tabpage_T *tp;
6581#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006582
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006583 /* Only do this once. */
6584 want_garbage_collect = FALSE;
6585 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006586 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006587
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006588 /* We advance by two because we add one for items referenced through
6589 * previous_funccal. */
6590 current_copyID += COPYID_INC;
6591 copyID = current_copyID;
6592
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006593 /*
6594 * 1. Go through all accessible variables and mark all lists and dicts
6595 * with copyID.
6596 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006597
6598 /* Don't free variables in the previous_funccal list unless they are only
6599 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006600 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006601 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6602 {
6603 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6604 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6605 }
6606
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006607 /* script-local variables */
6608 for (i = 1; i <= ga_scripts.ga_len; ++i)
6609 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6610
6611 /* buffer-local variables */
6612 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6613 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6614
6615 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006616 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006617 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6618
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006619#ifdef FEAT_WINDOWS
6620 /* tabpage-local variables */
6621 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6622 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6623#endif
6624
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006625 /* global variables */
6626 set_ref_in_ht(&globvarht, copyID);
6627
6628 /* function-local variables */
6629 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6630 {
6631 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6632 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6633 }
6634
Bram Moolenaard812df62008-11-09 12:46:09 +00006635 /* v: vars */
6636 set_ref_in_ht(&vimvarht, copyID);
6637
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006638 /*
6639 * 2. Free lists and dictionaries that are not referenced.
6640 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006641 did_free = free_unref_items(copyID);
6642
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006643 /*
6644 * 3. Check if any funccal can be freed now.
6645 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006646 for (pfc = &previous_funccal; *pfc != NULL; )
6647 {
6648 if (can_free_funccal(*pfc, copyID))
6649 {
6650 fc = *pfc;
6651 *pfc = fc->caller;
6652 free_funccal(fc, TRUE);
6653 did_free = TRUE;
6654 did_free_funccal = TRUE;
6655 }
6656 else
6657 pfc = &(*pfc)->caller;
6658 }
6659 if (did_free_funccal)
6660 /* When a funccal was freed some more items might be garbage
6661 * collected, so run again. */
6662 (void)garbage_collect();
6663
6664 return did_free;
6665}
6666
6667/*
6668 * Free lists and dictionaries that are no longer referenced.
6669 */
6670 static int
6671free_unref_items(copyID)
6672 int copyID;
6673{
6674 dict_T *dd;
6675 list_T *ll;
6676 int did_free = FALSE;
6677
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006678 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006679 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006680 */
6681 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006682 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006683 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006684 /* Free the Dictionary and ordinary items it contains, but don't
6685 * recurse into Lists and Dictionaries, they will be in the list
6686 * of dicts or list of lists. */
6687 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006688 did_free = TRUE;
6689
6690 /* restart, next dict may also have been freed */
6691 dd = first_dict;
6692 }
6693 else
6694 dd = dd->dv_used_next;
6695
6696 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006697 * Go through the list of lists and free items without the copyID.
6698 * But don't free a list that has a watcher (used in a for loop), these
6699 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006700 */
6701 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006702 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6703 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006704 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006705 /* Free the List and ordinary items it contains, but don't recurse
6706 * into Lists and Dictionaries, they will be in the list of dicts
6707 * or list of lists. */
6708 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006709 did_free = TRUE;
6710
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006711 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006712 ll = first_list;
6713 }
6714 else
6715 ll = ll->lv_used_next;
6716
6717 return did_free;
6718}
6719
6720/*
6721 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6722 */
6723 static void
6724set_ref_in_ht(ht, copyID)
6725 hashtab_T *ht;
6726 int copyID;
6727{
6728 int todo;
6729 hashitem_T *hi;
6730
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006731 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006732 for (hi = ht->ht_array; todo > 0; ++hi)
6733 if (!HASHITEM_EMPTY(hi))
6734 {
6735 --todo;
6736 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6737 }
6738}
6739
6740/*
6741 * Mark all lists and dicts referenced through list "l" with "copyID".
6742 */
6743 static void
6744set_ref_in_list(l, copyID)
6745 list_T *l;
6746 int copyID;
6747{
6748 listitem_T *li;
6749
6750 for (li = l->lv_first; li != NULL; li = li->li_next)
6751 set_ref_in_item(&li->li_tv, copyID);
6752}
6753
6754/*
6755 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6756 */
6757 static void
6758set_ref_in_item(tv, copyID)
6759 typval_T *tv;
6760 int copyID;
6761{
6762 dict_T *dd;
6763 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006764
6765 switch (tv->v_type)
6766 {
6767 case VAR_DICT:
6768 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006769 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006770 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006771 /* Didn't see this dict yet. */
6772 dd->dv_copyID = copyID;
6773 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006774 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006775 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006776
6777 case VAR_LIST:
6778 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006779 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006780 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006781 /* Didn't see this list yet. */
6782 ll->lv_copyID = copyID;
6783 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006784 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006786 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006788}
6789
6790/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006791 * Allocate an empty header for a dictionary.
6792 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006793 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006794dict_alloc()
6795{
Bram Moolenaar33570922005-01-25 22:26:29 +00006796 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006797
Bram Moolenaar33570922005-01-25 22:26:29 +00006798 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006799 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006800 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006801 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006802 if (first_dict != NULL)
6803 first_dict->dv_used_prev = d;
6804 d->dv_used_next = first_dict;
6805 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006806 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006807
Bram Moolenaar33570922005-01-25 22:26:29 +00006808 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006809 d->dv_lock = 0;
6810 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006811 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006812 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006813 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006814}
6815
6816/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006817 * Allocate an empty dict for a return value.
6818 * Returns OK or FAIL.
6819 */
6820 static int
6821rettv_dict_alloc(rettv)
6822 typval_T *rettv;
6823{
6824 dict_T *d = dict_alloc();
6825
6826 if (d == NULL)
6827 return FAIL;
6828
6829 rettv->vval.v_dict = d;
6830 rettv->v_type = VAR_DICT;
6831 ++d->dv_refcount;
6832 return OK;
6833}
6834
6835
6836/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006837 * Unreference a Dictionary: decrement the reference count and free it when it
6838 * becomes zero.
6839 */
6840 static void
6841dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006842 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006843{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006844 if (d != NULL && --d->dv_refcount <= 0)
6845 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006846}
6847
6848/*
6849 * Free a Dictionary, including all items it contains.
6850 * Ignores the reference count.
6851 */
6852 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006853dict_free(d, recurse)
6854 dict_T *d;
6855 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006856{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006857 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006858 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006859 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006860
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 /* Remove the dict from the list of dicts for garbage collection. */
6862 if (d->dv_used_prev == NULL)
6863 first_dict = d->dv_used_next;
6864 else
6865 d->dv_used_prev->dv_used_next = d->dv_used_next;
6866 if (d->dv_used_next != NULL)
6867 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6868
6869 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006870 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006871 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006872 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006873 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006874 if (!HASHITEM_EMPTY(hi))
6875 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006876 /* Remove the item before deleting it, just in case there is
6877 * something recursive causing trouble. */
6878 di = HI2DI(hi);
6879 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006880 if (recurse || (di->di_tv.v_type != VAR_LIST
6881 && di->di_tv.v_type != VAR_DICT))
6882 clear_tv(&di->di_tv);
6883 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006884 --todo;
6885 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006886 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006887 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006888 vim_free(d);
6889}
6890
6891/*
6892 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006893 * The "key" is copied to the new item.
6894 * Note that the value of the item "di_tv" still needs to be initialized!
6895 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006896 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006897 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006898dictitem_alloc(key)
6899 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006900{
Bram Moolenaar33570922005-01-25 22:26:29 +00006901 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006902
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006903 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006904 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006905 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006906 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006907 di->di_flags = 0;
6908 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006909 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006910}
6911
6912/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006913 * Make a copy of a Dictionary item.
6914 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006915 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006916dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006918{
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006920
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006921 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6922 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006923 if (di != NULL)
6924 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006925 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006926 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006927 copy_tv(&org->di_tv, &di->di_tv);
6928 }
6929 return di;
6930}
6931
6932/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006933 * Remove item "item" from Dictionary "dict" and free it.
6934 */
6935 static void
6936dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006937 dict_T *dict;
6938 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006939{
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006941
Bram Moolenaar33570922005-01-25 22:26:29 +00006942 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006943 if (HASHITEM_EMPTY(hi))
6944 EMSG2(_(e_intern2), "dictitem_remove()");
6945 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006947 dictitem_free(item);
6948}
6949
6950/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006951 * Free a dict item. Also clears the value.
6952 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006953 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006954dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006955 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006956{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006957 clear_tv(&item->di_tv);
6958 vim_free(item);
6959}
6960
6961/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006962 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6963 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006964 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006965 * Returns NULL when out of memory.
6966 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006967 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006968dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006970 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006971 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006972{
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 dict_T *copy;
6974 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006975 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006976 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006977
6978 if (orig == NULL)
6979 return NULL;
6980
6981 copy = dict_alloc();
6982 if (copy != NULL)
6983 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006984 if (copyID != 0)
6985 {
6986 orig->dv_copyID = copyID;
6987 orig->dv_copydict = copy;
6988 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006989 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006990 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006991 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006992 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006993 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006994 --todo;
6995
6996 di = dictitem_alloc(hi->hi_key);
6997 if (di == NULL)
6998 break;
6999 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007000 {
7001 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7002 copyID) == FAIL)
7003 {
7004 vim_free(di);
7005 break;
7006 }
7007 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007008 else
7009 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7010 if (dict_add(copy, di) == FAIL)
7011 {
7012 dictitem_free(di);
7013 break;
7014 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007015 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007016 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007017
Bram Moolenaare9a41262005-01-15 22:18:47 +00007018 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007019 if (todo > 0)
7020 {
7021 dict_unref(copy);
7022 copy = NULL;
7023 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007024 }
7025
7026 return copy;
7027}
7028
7029/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007031 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007032 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007033 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007035 dict_T *d;
7036 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007037{
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007039}
7040
Bram Moolenaar8c711452005-01-14 21:53:12 +00007041/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007042 * Add a number or string entry to dictionary "d".
7043 * When "str" is NULL use number "nr", otherwise use "str".
7044 * Returns FAIL when out of memory and when key already exists.
7045 */
7046 int
7047dict_add_nr_str(d, key, nr, str)
7048 dict_T *d;
7049 char *key;
7050 long nr;
7051 char_u *str;
7052{
7053 dictitem_T *item;
7054
7055 item = dictitem_alloc((char_u *)key);
7056 if (item == NULL)
7057 return FAIL;
7058 item->di_tv.v_lock = 0;
7059 if (str == NULL)
7060 {
7061 item->di_tv.v_type = VAR_NUMBER;
7062 item->di_tv.vval.v_number = nr;
7063 }
7064 else
7065 {
7066 item->di_tv.v_type = VAR_STRING;
7067 item->di_tv.vval.v_string = vim_strsave(str);
7068 }
7069 if (dict_add(d, item) == FAIL)
7070 {
7071 dictitem_free(item);
7072 return FAIL;
7073 }
7074 return OK;
7075}
7076
7077/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007078 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007079 * Returns FAIL when out of memory and when key already exists.
7080 */
7081 int
7082dict_add_list(d, key, list)
7083 dict_T *d;
7084 char *key;
7085 list_T *list;
7086{
7087 dictitem_T *item;
7088
7089 item = dictitem_alloc((char_u *)key);
7090 if (item == NULL)
7091 return FAIL;
7092 item->di_tv.v_lock = 0;
7093 item->di_tv.v_type = VAR_LIST;
7094 item->di_tv.vval.v_list = list;
7095 if (dict_add(d, item) == FAIL)
7096 {
7097 dictitem_free(item);
7098 return FAIL;
7099 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007100 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007101 return OK;
7102}
7103
7104/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105 * Get the number of items in a Dictionary.
7106 */
7107 static long
7108dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007111 if (d == NULL)
7112 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007113 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114}
7115
7116/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007117 * Find item "key[len]" in Dictionary "d".
7118 * If "len" is negative use strlen(key).
7119 * Returns NULL when not found.
7120 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007121 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007122dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007123 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007124 char_u *key;
7125 int len;
7126{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127#define AKEYLEN 200
7128 char_u buf[AKEYLEN];
7129 char_u *akey;
7130 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007131 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007132
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 if (len < 0)
7134 akey = key;
7135 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007136 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007137 tofree = akey = vim_strnsave(key, len);
7138 if (akey == NULL)
7139 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007140 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007141 else
7142 {
7143 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007144 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007145 akey = buf;
7146 }
7147
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007149 vim_free(tofree);
7150 if (HASHITEM_EMPTY(hi))
7151 return NULL;
7152 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007153}
7154
7155/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007156 * Get a string item from a dictionary.
7157 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007158 * Returns NULL if the entry doesn't exist or out of memory.
7159 */
7160 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007161get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007162 dict_T *d;
7163 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007164 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007165{
7166 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007167 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007168
7169 di = dict_find(d, key, -1);
7170 if (di == NULL)
7171 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007172 s = get_tv_string(&di->di_tv);
7173 if (save && s != NULL)
7174 s = vim_strsave(s);
7175 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007176}
7177
7178/*
7179 * Get a number item from a dictionary.
7180 * Returns 0 if the entry doesn't exist or out of memory.
7181 */
7182 long
7183get_dict_number(d, key)
7184 dict_T *d;
7185 char_u *key;
7186{
7187 dictitem_T *di;
7188
7189 di = dict_find(d, key, -1);
7190 if (di == NULL)
7191 return 0;
7192 return get_tv_number(&di->di_tv);
7193}
7194
7195/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007196 * Return an allocated string with the string representation of a Dictionary.
7197 * May return NULL.
7198 */
7199 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007200dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007202 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203{
7204 garray_T ga;
7205 int first = TRUE;
7206 char_u *tofree;
7207 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007208 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007209 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007210 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007211 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007212
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007213 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 return NULL;
7215 ga_init2(&ga, (int)sizeof(char), 80);
7216 ga_append(&ga, '{');
7217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007218 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007219 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007221 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007223 --todo;
7224
7225 if (first)
7226 first = FALSE;
7227 else
7228 ga_concat(&ga, (char_u *)", ");
7229
7230 tofree = string_quote(hi->hi_key, FALSE);
7231 if (tofree != NULL)
7232 {
7233 ga_concat(&ga, tofree);
7234 vim_free(tofree);
7235 }
7236 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007237 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007238 if (s != NULL)
7239 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007241 if (s == NULL)
7242 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007243 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007245 if (todo > 0)
7246 {
7247 vim_free(ga.ga_data);
7248 return NULL;
7249 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250
7251 ga_append(&ga, '}');
7252 ga_append(&ga, NUL);
7253 return (char_u *)ga.ga_data;
7254}
7255
7256/*
7257 * Allocate a variable for a Dictionary and fill it from "*arg".
7258 * Return OK or FAIL. Returns NOTDONE for {expr}.
7259 */
7260 static int
7261get_dict_tv(arg, rettv, evaluate)
7262 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007263 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 int evaluate;
7265{
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 dict_T *d = NULL;
7267 typval_T tvkey;
7268 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007269 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007270 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007272 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007273
7274 /*
7275 * First check if it's not a curly-braces thing: {expr}.
7276 * Must do this without evaluating, otherwise a function may be called
7277 * twice. Unfortunately this means we need to call eval1() twice for the
7278 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007279 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007281 if (*start != '}')
7282 {
7283 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7284 return FAIL;
7285 if (*start == '}')
7286 return NOTDONE;
7287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288
7289 if (evaluate)
7290 {
7291 d = dict_alloc();
7292 if (d == NULL)
7293 return FAIL;
7294 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295 tvkey.v_type = VAR_UNKNOWN;
7296 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007297
7298 *arg = skipwhite(*arg + 1);
7299 while (**arg != '}' && **arg != NUL)
7300 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007301 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 goto failret;
7303 if (**arg != ':')
7304 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007305 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007306 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307 goto failret;
7308 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007309 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007311 key = get_tv_string_buf_chk(&tvkey, buf);
7312 if (key == NULL || *key == NUL)
7313 {
7314 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7315 if (key != NULL)
7316 EMSG(_(e_emptykey));
7317 clear_tv(&tvkey);
7318 goto failret;
7319 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321
7322 *arg = skipwhite(*arg + 1);
7323 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7324 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007325 if (evaluate)
7326 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007327 goto failret;
7328 }
7329 if (evaluate)
7330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007332 if (item != NULL)
7333 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007334 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 clear_tv(&tv);
7337 goto failret;
7338 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 item = dictitem_alloc(key);
7340 clear_tv(&tvkey);
7341 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007344 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 if (dict_add(d, item) == FAIL)
7346 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347 }
7348 }
7349
7350 if (**arg == '}')
7351 break;
7352 if (**arg != ',')
7353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007354 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355 goto failret;
7356 }
7357 *arg = skipwhite(*arg + 1);
7358 }
7359
7360 if (**arg != '}')
7361 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007362 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363failret:
7364 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007365 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 return FAIL;
7367 }
7368
7369 *arg = skipwhite(*arg + 1);
7370 if (evaluate)
7371 {
7372 rettv->v_type = VAR_DICT;
7373 rettv->vval.v_dict = d;
7374 ++d->dv_refcount;
7375 }
7376
7377 return OK;
7378}
7379
Bram Moolenaar8c711452005-01-14 21:53:12 +00007380/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007381 * Return a string with the string representation of a variable.
7382 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007383 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007384 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007385 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007386 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007387 */
7388 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007389echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007390 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007392 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007393 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007394{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 static int recurse = 0;
7396 char_u *r = NULL;
7397
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007400 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 *tofree = NULL;
7402 return NULL;
7403 }
7404 ++recurse;
7405
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007406 switch (tv->v_type)
7407 {
7408 case VAR_FUNC:
7409 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410 r = tv->vval.v_string;
7411 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007412
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007413 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007414 if (tv->vval.v_list == NULL)
7415 {
7416 *tofree = NULL;
7417 r = NULL;
7418 }
7419 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7420 {
7421 *tofree = NULL;
7422 r = (char_u *)"[...]";
7423 }
7424 else
7425 {
7426 tv->vval.v_list->lv_copyID = copyID;
7427 *tofree = list2string(tv, copyID);
7428 r = *tofree;
7429 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007430 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007431
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007433 if (tv->vval.v_dict == NULL)
7434 {
7435 *tofree = NULL;
7436 r = NULL;
7437 }
7438 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7439 {
7440 *tofree = NULL;
7441 r = (char_u *)"{...}";
7442 }
7443 else
7444 {
7445 tv->vval.v_dict->dv_copyID = copyID;
7446 *tofree = dict2string(tv, copyID);
7447 r = *tofree;
7448 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007449 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007450
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007451 case VAR_STRING:
7452 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 *tofree = NULL;
7454 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007455 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007456
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007457#ifdef FEAT_FLOAT
7458 case VAR_FLOAT:
7459 *tofree = NULL;
7460 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7461 r = numbuf;
7462 break;
7463#endif
7464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007465 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007466 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007467 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007468 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469
7470 --recurse;
7471 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007472}
7473
7474/*
7475 * Return a string with the string representation of a variable.
7476 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7477 * "numbuf" is used for a number.
7478 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007479 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007480 */
7481 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007482tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007484 char_u **tofree;
7485 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007486 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007487{
7488 switch (tv->v_type)
7489 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007490 case VAR_FUNC:
7491 *tofree = string_quote(tv->vval.v_string, TRUE);
7492 return *tofree;
7493 case VAR_STRING:
7494 *tofree = string_quote(tv->vval.v_string, FALSE);
7495 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007496#ifdef FEAT_FLOAT
7497 case VAR_FLOAT:
7498 *tofree = NULL;
7499 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7500 return numbuf;
7501#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007502 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007503 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007505 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007506 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007508 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007509 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007510}
7511
7512/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007513 * Return string "str" in ' quotes, doubling ' characters.
7514 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007516 */
7517 static char_u *
7518string_quote(str, function)
7519 char_u *str;
7520 int function;
7521{
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007523 char_u *p, *r, *s;
7524
Bram Moolenaar33570922005-01-25 22:26:29 +00007525 len = (function ? 13 : 3);
7526 if (str != NULL)
7527 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007528 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 for (p = str; *p != NUL; mb_ptr_adv(p))
7530 if (*p == '\'')
7531 ++len;
7532 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007533 s = r = alloc(len);
7534 if (r != NULL)
7535 {
7536 if (function)
7537 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007538 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007539 r += 10;
7540 }
7541 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 if (str != NULL)
7544 for (p = str; *p != NUL; )
7545 {
7546 if (*p == '\'')
7547 *r++ = '\'';
7548 MB_COPY_CHAR(p, r);
7549 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007551 if (function)
7552 *r++ = ')';
7553 *r++ = NUL;
7554 }
7555 return s;
7556}
7557
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007558#ifdef FEAT_FLOAT
7559/*
7560 * Convert the string "text" to a floating point number.
7561 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7562 * this always uses a decimal point.
7563 * Returns the length of the text that was consumed.
7564 */
7565 static int
7566string2float(text, value)
7567 char_u *text;
7568 float_T *value; /* result stored here */
7569{
7570 char *s = (char *)text;
7571 float_T f;
7572
7573 f = strtod(s, &s);
7574 *value = f;
7575 return (int)((char_u *)s - text);
7576}
7577#endif
7578
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 * Get the value of an environment variable.
7581 * "arg" is pointing to the '$'. It is advanced to after the name.
7582 * If the environment variable was not set, silently assume it is empty.
7583 * Always return OK.
7584 */
7585 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 int evaluate;
7590{
7591 char_u *string = NULL;
7592 int len;
7593 int cc;
7594 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007595 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596
7597 ++*arg;
7598 name = *arg;
7599 len = get_env_len(arg);
7600 if (evaluate)
7601 {
7602 if (len != 0)
7603 {
7604 cc = name[len];
7605 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007606 /* first try vim_getenv(), fast for normal environment vars */
7607 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007609 {
7610 if (!mustfree)
7611 string = vim_strsave(string);
7612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 else
7614 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007615 if (mustfree)
7616 vim_free(string);
7617
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 /* next try expanding things like $VIM and ${HOME} */
7619 string = expand_env_save(name - 1);
7620 if (string != NULL && *string == '$')
7621 {
7622 vim_free(string);
7623 string = NULL;
7624 }
7625 }
7626 name[len] = cc;
7627 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007628 rettv->v_type = VAR_STRING;
7629 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 }
7631
7632 return OK;
7633}
7634
7635/*
7636 * Array with names and number of arguments of all internal functions
7637 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7638 */
7639static struct fst
7640{
7641 char *f_name; /* function name */
7642 char f_min_argc; /* minimal number of arguments */
7643 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007645 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646} functions[] =
7647{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007648#ifdef FEAT_FLOAT
7649 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007650 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007651#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007652 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 {"append", 2, 2, f_append},
7654 {"argc", 0, 0, f_argc},
7655 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007656 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007657#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007658 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007659 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007660 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007663 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 {"bufexists", 1, 1, f_bufexists},
7665 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7666 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7667 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7668 {"buflisted", 1, 1, f_buflisted},
7669 {"bufloaded", 1, 1, f_bufloaded},
7670 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007671 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 {"bufwinnr", 1, 1, f_bufwinnr},
7673 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007674 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007675 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007676#ifdef FEAT_FLOAT
7677 {"ceil", 1, 1, f_ceil},
7678#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007679 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {"char2nr", 1, 1, f_char2nr},
7681 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007682 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007684#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007685 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007686 {"complete_add", 1, 1, f_complete_add},
7687 {"complete_check", 0, 0, f_complete_check},
7688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007690 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007691#ifdef FEAT_FLOAT
7692 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007693 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007694#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007695 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007697 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007698 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 {"delete", 1, 1, f_delete},
7700 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007701 {"diff_filler", 1, 1, f_diff_filler},
7702 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007703 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007705 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 {"eventhandler", 0, 0, f_eventhandler},
7707 {"executable", 1, 1, f_executable},
7708 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007709#ifdef FEAT_FLOAT
7710 {"exp", 1, 1, f_exp},
7711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007713 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007714 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7716 {"filereadable", 1, 1, f_filereadable},
7717 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007718 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007719 {"finddir", 1, 3, f_finddir},
7720 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007721#ifdef FEAT_FLOAT
7722 {"float2nr", 1, 1, f_float2nr},
7723 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007724 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007725#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007726 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 {"fnamemodify", 2, 2, f_fnamemodify},
7728 {"foldclosed", 1, 1, f_foldclosed},
7729 {"foldclosedend", 1, 1, f_foldclosedend},
7730 {"foldlevel", 1, 1, f_foldlevel},
7731 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007732 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007734 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007735 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007736 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007737 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 {"getbufvar", 2, 2, f_getbufvar},
7739 {"getchar", 0, 1, f_getchar},
7740 {"getcharmod", 0, 0, f_getcharmod},
7741 {"getcmdline", 0, 0, f_getcmdline},
7742 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007743 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007745 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007746 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 {"getfsize", 1, 1, f_getfsize},
7748 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007749 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007750 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007751 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007752 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007753 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007754 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007755 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007756 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007758 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007759 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {"getwinposx", 0, 0, f_getwinposx},
7761 {"getwinposy", 0, 0, f_getwinposy},
7762 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007763 {"glob", 1, 2, f_glob},
7764 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007766 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007767 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007768 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7770 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7771 {"histadd", 2, 2, f_histadd},
7772 {"histdel", 1, 2, f_histdel},
7773 {"histget", 1, 2, f_histget},
7774 {"histnr", 1, 1, f_histnr},
7775 {"hlID", 1, 1, f_hlID},
7776 {"hlexists", 1, 1, f_hlexists},
7777 {"hostname", 0, 0, f_hostname},
7778 {"iconv", 3, 3, f_iconv},
7779 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007780 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007781 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007783 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 {"inputrestore", 0, 0, f_inputrestore},
7785 {"inputsave", 0, 0, f_inputsave},
7786 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007787 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007789 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007790 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007791 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007792 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 {"libcall", 3, 3, f_libcall},
7796 {"libcallnr", 3, 3, f_libcallnr},
7797 {"line", 1, 1, f_line},
7798 {"line2byte", 1, 1, f_line2byte},
7799 {"lispindent", 1, 1, f_lispindent},
7800 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007801#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007802 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007803 {"log10", 1, 1, f_log10},
7804#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007805 {"map", 2, 2, f_map},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007806 {"maparg", 1, 3, f_maparg},
7807 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007808 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007809 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007810 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007811 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007812 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007813 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007814 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007815 {"max", 1, 1, f_max},
7816 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007817#ifdef vim_mkdir
7818 {"mkdir", 1, 3, f_mkdir},
7819#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007820 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007821#ifdef FEAT_MZSCHEME
7822 {"mzeval", 1, 1, f_mzeval},
7823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 {"nextnonblank", 1, 1, f_nextnonblank},
7825 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007826 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007827#ifdef FEAT_FLOAT
7828 {"pow", 2, 2, f_pow},
7829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007831 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007832 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007833 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007834 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007835 {"reltime", 0, 2, f_reltime},
7836 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 {"remote_expr", 2, 3, f_remote_expr},
7838 {"remote_foreground", 1, 1, f_remote_foreground},
7839 {"remote_peek", 1, 2, f_remote_peek},
7840 {"remote_read", 1, 1, f_remote_read},
7841 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007842 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007844 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007846 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007847#ifdef FEAT_FLOAT
7848 {"round", 1, 1, f_round},
7849#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007850 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007851 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007852 {"searchpair", 3, 7, f_searchpair},
7853 {"searchpairpos", 3, 7, f_searchpairpos},
7854 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"server2client", 2, 2, f_server2client},
7856 {"serverlist", 0, 0, f_serverlist},
7857 {"setbufvar", 3, 3, f_setbufvar},
7858 {"setcmdpos", 1, 1, f_setcmdpos},
7859 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007860 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007861 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007862 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007863 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007865 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007866 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007868 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007870#ifdef FEAT_FLOAT
7871 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007872 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007873#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007874 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007875 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007876 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007877 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007878 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#ifdef FEAT_FLOAT
7880 {"sqrt", 1, 1, f_sqrt},
7881 {"str2float", 1, 1, f_str2float},
7882#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007883 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007884 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007885 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886#ifdef HAVE_STRFTIME
7887 {"strftime", 1, 2, f_strftime},
7888#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007889 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007890 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"strlen", 1, 1, f_strlen},
7892 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007893 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007895 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"submatch", 1, 1, f_submatch},
7897 {"substitute", 4, 4, f_substitute},
7898 {"synID", 3, 3, f_synID},
7899 {"synIDattr", 2, 3, f_synIDattr},
7900 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007901 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007902 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007903 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007904 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007905 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007906 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007907 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007908 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007909#ifdef FEAT_FLOAT
7910 {"tan", 1, 1, f_tan},
7911 {"tanh", 1, 1, f_tanh},
7912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007914 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"tolower", 1, 1, f_tolower},
7916 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007917 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007918#ifdef FEAT_FLOAT
7919 {"trunc", 1, 1, f_trunc},
7920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007922 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007923 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007924 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"virtcol", 1, 1, f_virtcol},
7926 {"visualmode", 0, 1, f_visualmode},
7927 {"winbufnr", 1, 1, f_winbufnr},
7928 {"wincol", 0, 0, f_wincol},
7929 {"winheight", 1, 1, f_winheight},
7930 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007931 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007933 {"winrestview", 1, 1, f_winrestview},
7934 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007936 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937};
7938
7939#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7940
7941/*
7942 * Function given to ExpandGeneric() to obtain the list of internal
7943 * or user defined function names.
7944 */
7945 char_u *
7946get_function_name(xp, idx)
7947 expand_T *xp;
7948 int idx;
7949{
7950 static int intidx = -1;
7951 char_u *name;
7952
7953 if (idx == 0)
7954 intidx = -1;
7955 if (intidx < 0)
7956 {
7957 name = get_user_func_name(xp, idx);
7958 if (name != NULL)
7959 return name;
7960 }
7961 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7962 {
7963 STRCPY(IObuff, functions[intidx].f_name);
7964 STRCAT(IObuff, "(");
7965 if (functions[intidx].f_max_argc == 0)
7966 STRCAT(IObuff, ")");
7967 return IObuff;
7968 }
7969
7970 return NULL;
7971}
7972
7973/*
7974 * Function given to ExpandGeneric() to obtain the list of internal or
7975 * user defined variable or function names.
7976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 char_u *
7978get_expr_name(xp, idx)
7979 expand_T *xp;
7980 int idx;
7981{
7982 static int intidx = -1;
7983 char_u *name;
7984
7985 if (idx == 0)
7986 intidx = -1;
7987 if (intidx < 0)
7988 {
7989 name = get_function_name(xp, idx);
7990 if (name != NULL)
7991 return name;
7992 }
7993 return get_user_var_name(xp, ++intidx);
7994}
7995
7996#endif /* FEAT_CMDL_COMPL */
7997
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007998#if defined(EBCDIC) || defined(PROTO)
7999/*
8000 * Compare struct fst by function name.
8001 */
8002 static int
8003compare_func_name(s1, s2)
8004 const void *s1;
8005 const void *s2;
8006{
8007 struct fst *p1 = (struct fst *)s1;
8008 struct fst *p2 = (struct fst *)s2;
8009
8010 return STRCMP(p1->f_name, p2->f_name);
8011}
8012
8013/*
8014 * Sort the function table by function name.
8015 * The sorting of the table above is ASCII dependant.
8016 * On machines using EBCDIC we have to sort it.
8017 */
8018 static void
8019sortFunctions()
8020{
8021 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8022
8023 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8024}
8025#endif
8026
8027
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028/*
8029 * Find internal function in table above.
8030 * Return index, or -1 if not found
8031 */
8032 static int
8033find_internal_func(name)
8034 char_u *name; /* name of the function */
8035{
8036 int first = 0;
8037 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8038 int cmp;
8039 int x;
8040
8041 /*
8042 * Find the function name in the table. Binary search.
8043 */
8044 while (first <= last)
8045 {
8046 x = first + ((unsigned)(last - first) >> 1);
8047 cmp = STRCMP(name, functions[x].f_name);
8048 if (cmp < 0)
8049 last = x - 1;
8050 else if (cmp > 0)
8051 first = x + 1;
8052 else
8053 return x;
8054 }
8055 return -1;
8056}
8057
8058/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008059 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8060 * name it contains, otherwise return "name".
8061 */
8062 static char_u *
8063deref_func_name(name, lenp)
8064 char_u *name;
8065 int *lenp;
8066{
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008068 int cc;
8069
8070 cc = name[*lenp];
8071 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008072 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008073 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008074 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008075 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008076 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008077 {
8078 *lenp = 0;
8079 return (char_u *)""; /* just in case */
8080 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008081 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008083 }
8084
8085 return name;
8086}
8087
8088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089 * Allocate a variable for the result of a function.
8090 * Return OK or FAIL.
8091 */
8092 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008093get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8094 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 char_u *name; /* name of the function */
8096 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008097 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 char_u **arg; /* argument, pointing to the '(' */
8099 linenr_T firstline; /* first line of range */
8100 linenr_T lastline; /* last line of range */
8101 int *doesrange; /* return: function handled range */
8102 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008103 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104{
8105 char_u *argp;
8106 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008107 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 int argcount = 0; /* number of arguments found */
8109
8110 /*
8111 * Get the arguments.
8112 */
8113 argp = *arg;
8114 while (argcount < MAX_FUNC_ARGS)
8115 {
8116 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8117 if (*argp == ')' || *argp == ',' || *argp == NUL)
8118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8120 {
8121 ret = FAIL;
8122 break;
8123 }
8124 ++argcount;
8125 if (*argp != ',')
8126 break;
8127 }
8128 if (*argp == ')')
8129 ++argp;
8130 else
8131 ret = FAIL;
8132
8133 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008134 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008135 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008137 {
8138 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008139 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008140 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008141 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143
8144 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146
8147 *arg = skipwhite(argp);
8148 return ret;
8149}
8150
8151
8152/*
8153 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008154 * Return OK when the function can't be called, FAIL otherwise.
8155 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 */
8157 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008158call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008159 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008160 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008162 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008164 typval_T *argvars; /* vars for arguments, must have "argcount"
8165 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 linenr_T firstline; /* first line of range */
8167 linenr_T lastline; /* last line of range */
8168 int *doesrange; /* return: function handled range */
8169 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008170 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171{
8172 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173#define ERROR_UNKNOWN 0
8174#define ERROR_TOOMANY 1
8175#define ERROR_TOOFEW 2
8176#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008177#define ERROR_DICT 4
8178#define ERROR_NONE 5
8179#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 int error = ERROR_NONE;
8181 int i;
8182 int llen;
8183 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184#define FLEN_FIXED 40
8185 char_u fname_buf[FLEN_FIXED + 1];
8186 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008187 char_u *name;
8188
8189 /* Make a copy of the name, if it comes from a funcref variable it could
8190 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008191 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008192 if (name == NULL)
8193 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194
8195 /*
8196 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8197 * Change <SNR>123_name() to K_SNR 123_name().
8198 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 llen = eval_fname_script(name);
8201 if (llen > 0)
8202 {
8203 fname_buf[0] = K_SPECIAL;
8204 fname_buf[1] = KS_EXTRA;
8205 fname_buf[2] = (int)KE_SNR;
8206 i = 3;
8207 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8208 {
8209 if (current_SID <= 0)
8210 error = ERROR_SCRIPT;
8211 else
8212 {
8213 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8214 i = (int)STRLEN(fname_buf);
8215 }
8216 }
8217 if (i + STRLEN(name + llen) < FLEN_FIXED)
8218 {
8219 STRCPY(fname_buf + i, name + llen);
8220 fname = fname_buf;
8221 }
8222 else
8223 {
8224 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8225 if (fname == NULL)
8226 error = ERROR_OTHER;
8227 else
8228 {
8229 mch_memmove(fname, fname_buf, (size_t)i);
8230 STRCPY(fname + i, name + llen);
8231 }
8232 }
8233 }
8234 else
8235 fname = name;
8236
8237 *doesrange = FALSE;
8238
8239
8240 /* execute the function if no errors detected and executing */
8241 if (evaluate && error == ERROR_NONE)
8242 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008243 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8244 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 error = ERROR_UNKNOWN;
8246
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008247 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
8249 /*
8250 * User defined function.
8251 */
8252 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008253
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008255 /* Trigger FuncUndefined event, may load the function. */
8256 if (fp == NULL
8257 && apply_autocmds(EVENT_FUNCUNDEFINED,
8258 fname, fname, TRUE, NULL)
8259 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008261 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 fp = find_func(fname);
8263 }
8264#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008265 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008266 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008267 {
8268 /* loaded a package, search for the function again */
8269 fp = find_func(fname);
8270 }
8271
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 if (fp != NULL)
8273 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008274 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008276 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008278 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008280 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008281 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 else
8283 {
8284 /*
8285 * Call the user function.
8286 * Save and restore search patterns, script variables and
8287 * redo buffer.
8288 */
8289 save_search_patterns();
8290 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008291 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008292 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008293 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008294 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8295 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8296 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008297 /* Function was unreferenced while being used, free it
8298 * now. */
8299 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 restoreRedobuff();
8301 restore_search_patterns();
8302 error = ERROR_NONE;
8303 }
8304 }
8305 }
8306 else
8307 {
8308 /*
8309 * Find the function name in the table, call its implementation.
8310 */
8311 i = find_internal_func(fname);
8312 if (i >= 0)
8313 {
8314 if (argcount < functions[i].f_min_argc)
8315 error = ERROR_TOOFEW;
8316 else if (argcount > functions[i].f_max_argc)
8317 error = ERROR_TOOMANY;
8318 else
8319 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008321 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 error = ERROR_NONE;
8323 }
8324 }
8325 }
8326 /*
8327 * The function call (or "FuncUndefined" autocommand sequence) might
8328 * have been aborted by an error, an interrupt, or an explicitly thrown
8329 * exception that has not been caught so far. This situation can be
8330 * tested for by calling aborting(). For an error in an internal
8331 * function or for the "E132" error in call_user_func(), however, the
8332 * throw point at which the "force_abort" flag (temporarily reset by
8333 * emsg()) is normally updated has not been reached yet. We need to
8334 * update that flag first to make aborting() reliable.
8335 */
8336 update_force_abort();
8337 }
8338 if (error == ERROR_NONE)
8339 ret = OK;
8340
8341 /*
8342 * Report an error unless the argument evaluation or function call has been
8343 * cancelled due to an aborting error, an interrupt, or an exception.
8344 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008345 if (!aborting())
8346 {
8347 switch (error)
8348 {
8349 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008350 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008351 break;
8352 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008353 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008354 break;
8355 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008356 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008357 name);
8358 break;
8359 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008360 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008361 name);
8362 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008363 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008364 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008365 name);
8366 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008367 }
8368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 if (fname != name && fname != fname_buf)
8371 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008372 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
8374 return ret;
8375}
8376
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008377/*
8378 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008379 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008380 */
8381 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008382emsg_funcname(ermsg, name)
8383 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008384 char_u *name;
8385{
8386 char_u *p;
8387
8388 if (*name == K_SPECIAL)
8389 p = concat_str((char_u *)"<SNR>", name + 3);
8390 else
8391 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008392 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008393 if (p != name)
8394 vim_free(p);
8395}
8396
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008397/*
8398 * Return TRUE for a non-zero Number and a non-empty String.
8399 */
8400 static int
8401non_zero_arg(argvars)
8402 typval_T *argvars;
8403{
8404 return ((argvars[0].v_type == VAR_NUMBER
8405 && argvars[0].vval.v_number != 0)
8406 || (argvars[0].v_type == VAR_STRING
8407 && argvars[0].vval.v_string != NULL
8408 && *argvars[0].vval.v_string != NUL));
8409}
8410
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411/*********************************************
8412 * Implementation of the built-in functions
8413 */
8414
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008415#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008416static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8417
8418/*
8419 * Get the float value of "argvars[0]" into "f".
8420 * Returns FAIL when the argument is not a Number or Float.
8421 */
8422 static int
8423get_float_arg(argvars, f)
8424 typval_T *argvars;
8425 float_T *f;
8426{
8427 if (argvars[0].v_type == VAR_FLOAT)
8428 {
8429 *f = argvars[0].vval.v_float;
8430 return OK;
8431 }
8432 if (argvars[0].v_type == VAR_NUMBER)
8433 {
8434 *f = (float_T)argvars[0].vval.v_number;
8435 return OK;
8436 }
8437 EMSG(_("E808: Number or Float required"));
8438 return FAIL;
8439}
8440
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008441/*
8442 * "abs(expr)" function
8443 */
8444 static void
8445f_abs(argvars, rettv)
8446 typval_T *argvars;
8447 typval_T *rettv;
8448{
8449 if (argvars[0].v_type == VAR_FLOAT)
8450 {
8451 rettv->v_type = VAR_FLOAT;
8452 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8453 }
8454 else
8455 {
8456 varnumber_T n;
8457 int error = FALSE;
8458
8459 n = get_tv_number_chk(&argvars[0], &error);
8460 if (error)
8461 rettv->vval.v_number = -1;
8462 else if (n > 0)
8463 rettv->vval.v_number = n;
8464 else
8465 rettv->vval.v_number = -n;
8466 }
8467}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008468
8469/*
8470 * "acos()" function
8471 */
8472 static void
8473f_acos(argvars, rettv)
8474 typval_T *argvars;
8475 typval_T *rettv;
8476{
8477 float_T f;
8478
8479 rettv->v_type = VAR_FLOAT;
8480 if (get_float_arg(argvars, &f) == OK)
8481 rettv->vval.v_float = acos(f);
8482 else
8483 rettv->vval.v_float = 0.0;
8484}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008485#endif
8486
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008488 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 */
8490 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008491f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008492 typval_T *argvars;
8493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
Bram Moolenaar33570922005-01-25 22:26:29 +00008495 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008497 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008498 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008500 if ((l = argvars[0].vval.v_list) != NULL
8501 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8502 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008503 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008504 }
8505 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008506 EMSG(_(e_listreq));
8507}
8508
8509/*
8510 * "append(lnum, string/list)" function
8511 */
8512 static void
8513f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008514 typval_T *argvars;
8515 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008516{
8517 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008518 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008519 list_T *l = NULL;
8520 listitem_T *li = NULL;
8521 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008522 long added = 0;
8523
Bram Moolenaar0d660222005-01-07 21:51:51 +00008524 lnum = get_tv_lnum(argvars);
8525 if (lnum >= 0
8526 && lnum <= curbuf->b_ml.ml_line_count
8527 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008528 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008529 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008531 l = argvars[1].vval.v_list;
8532 if (l == NULL)
8533 return;
8534 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008536 for (;;)
8537 {
8538 if (l == NULL)
8539 tv = &argvars[1]; /* append a string */
8540 else if (li == NULL)
8541 break; /* end of list */
8542 else
8543 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008544 line = get_tv_string_chk(tv);
8545 if (line == NULL) /* type error */
8546 {
8547 rettv->vval.v_number = 1; /* Failed */
8548 break;
8549 }
8550 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008551 ++added;
8552 if (l == NULL)
8553 break;
8554 li = li->li_next;
8555 }
8556
8557 appended_lines_mark(lnum, added);
8558 if (curwin->w_cursor.lnum > lnum)
8559 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008561 else
8562 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563}
8564
8565/*
8566 * "argc()" function
8567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008570 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008571 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008573 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574}
8575
8576/*
8577 * "argidx()" function
8578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008581 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585}
8586
8587/*
8588 * "argv(nr)" function
8589 */
8590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008592 typval_T *argvars;
8593 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594{
8595 int idx;
8596
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008597 if (argvars[0].v_type != VAR_UNKNOWN)
8598 {
8599 idx = get_tv_number_chk(&argvars[0], NULL);
8600 if (idx >= 0 && idx < ARGCOUNT)
8601 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8602 else
8603 rettv->vval.v_string = NULL;
8604 rettv->v_type = VAR_STRING;
8605 }
8606 else if (rettv_list_alloc(rettv) == OK)
8607 for (idx = 0; idx < ARGCOUNT; ++idx)
8608 list_append_string(rettv->vval.v_list,
8609 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610}
8611
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008612#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008613/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008614 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008615 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008616 static void
8617f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008618 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008619 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008620{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008621 float_T f;
8622
8623 rettv->v_type = VAR_FLOAT;
8624 if (get_float_arg(argvars, &f) == OK)
8625 rettv->vval.v_float = asin(f);
8626 else
8627 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008628}
8629
8630/*
8631 * "atan()" function
8632 */
8633 static void
8634f_atan(argvars, rettv)
8635 typval_T *argvars;
8636 typval_T *rettv;
8637{
8638 float_T f;
8639
8640 rettv->v_type = VAR_FLOAT;
8641 if (get_float_arg(argvars, &f) == OK)
8642 rettv->vval.v_float = atan(f);
8643 else
8644 rettv->vval.v_float = 0.0;
8645}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008646
8647/*
8648 * "atan2()" function
8649 */
8650 static void
8651f_atan2(argvars, rettv)
8652 typval_T *argvars;
8653 typval_T *rettv;
8654{
8655 float_T fx, fy;
8656
8657 rettv->v_type = VAR_FLOAT;
8658 if (get_float_arg(argvars, &fx) == OK
8659 && get_float_arg(&argvars[1], &fy) == OK)
8660 rettv->vval.v_float = atan2(fx, fy);
8661 else
8662 rettv->vval.v_float = 0.0;
8663}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008664#endif
8665
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666/*
8667 * "browse(save, title, initdir, default)" function
8668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008670f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008671 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
8674#ifdef FEAT_BROWSE
8675 int save;
8676 char_u *title;
8677 char_u *initdir;
8678 char_u *defname;
8679 char_u buf[NUMBUFLEN];
8680 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008681 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008683 save = get_tv_number_chk(&argvars[0], &error);
8684 title = get_tv_string_chk(&argvars[1]);
8685 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8686 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008688 if (error || title == NULL || initdir == NULL || defname == NULL)
8689 rettv->vval.v_string = NULL;
8690 else
8691 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008692 do_browse(save ? BROWSE_SAVE : 0,
8693 title, defname, NULL, initdir, NULL, curbuf);
8694#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008696#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008697 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008698}
8699
8700/*
8701 * "browsedir(title, initdir)" function
8702 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008704f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008705 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008706 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008707{
8708#ifdef FEAT_BROWSE
8709 char_u *title;
8710 char_u *initdir;
8711 char_u buf[NUMBUFLEN];
8712
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008713 title = get_tv_string_chk(&argvars[0]);
8714 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008716 if (title == NULL || initdir == NULL)
8717 rettv->vval.v_string = NULL;
8718 else
8719 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008720 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008724 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725}
8726
Bram Moolenaar33570922005-01-25 22:26:29 +00008727static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008728
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729/*
8730 * Find a buffer by number or exact name.
8731 */
8732 static buf_T *
8733find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008734 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735{
8736 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008738 if (avar->v_type == VAR_NUMBER)
8739 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008740 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008742 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008743 if (buf == NULL)
8744 {
8745 /* No full path name match, try a match with a URL or a "nofile"
8746 * buffer, these don't use the full path. */
8747 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8748 if (buf->b_fname != NULL
8749 && (path_with_url(buf->b_fname)
8750#ifdef FEAT_QUICKFIX
8751 || bt_nofile(buf)
8752#endif
8753 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008754 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008755 break;
8756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 }
8758 return buf;
8759}
8760
8761/*
8762 * "bufexists(expr)" function
8763 */
8764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008766 typval_T *argvars;
8767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008769 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770}
8771
8772/*
8773 * "buflisted(expr)" function
8774 */
8775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008776f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008777 typval_T *argvars;
8778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
8780 buf_T *buf;
8781
8782 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784}
8785
8786/*
8787 * "bufloaded(expr)" function
8788 */
8789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008791 typval_T *argvars;
8792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
8794 buf_T *buf;
8795
8796 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008797 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798}
8799
Bram Moolenaar33570922005-01-25 22:26:29 +00008800static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008801
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802/*
8803 * Get buffer by number or pattern.
8804 */
8805 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008807 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008809 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 int save_magic;
8811 char_u *save_cpo;
8812 buf_T *buf;
8813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 if (tv->v_type == VAR_NUMBER)
8815 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008816 if (tv->v_type != VAR_STRING)
8817 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 if (name == NULL || *name == NUL)
8819 return curbuf;
8820 if (name[0] == '$' && name[1] == NUL)
8821 return lastbuf;
8822
8823 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8824 save_magic = p_magic;
8825 p_magic = TRUE;
8826 save_cpo = p_cpo;
8827 p_cpo = (char_u *)"";
8828
8829 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8830 TRUE, FALSE));
8831
8832 p_magic = save_magic;
8833 p_cpo = save_cpo;
8834
8835 /* If not found, try expanding the name, like done for bufexists(). */
8836 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008837 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838
8839 return buf;
8840}
8841
8842/*
8843 * "bufname(expr)" function
8844 */
8845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 typval_T *argvars;
8848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849{
8850 buf_T *buf;
8851
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008852 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008854 buf = get_buf_tv(&argvars[0]);
8855 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008857 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 --emsg_off;
8861}
8862
8863/*
8864 * "bufnr(expr)" function
8865 */
8866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008867f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008868 typval_T *argvars;
8869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870{
8871 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008872 int error = FALSE;
8873 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008875 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008878 --emsg_off;
8879
8880 /* If the buffer isn't found and the second argument is not zero create a
8881 * new buffer. */
8882 if (buf == NULL
8883 && argvars[1].v_type != VAR_UNKNOWN
8884 && get_tv_number_chk(&argvars[1], &error) != 0
8885 && !error
8886 && (name = get_tv_string_chk(&argvars[0])) != NULL
8887 && !error)
8888 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8889
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894}
8895
8896/*
8897 * "bufwinnr(nr)" function
8898 */
8899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008901 typval_T *argvars;
8902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904#ifdef FEAT_WINDOWS
8905 win_T *wp;
8906 int winnr = 0;
8907#endif
8908 buf_T *buf;
8909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008912 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913#ifdef FEAT_WINDOWS
8914 for (wp = firstwin; wp; wp = wp->w_next)
8915 {
8916 ++winnr;
8917 if (wp->w_buffer == buf)
8918 break;
8919 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008922 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923#endif
8924 --emsg_off;
8925}
8926
8927/*
8928 * "byte2line(byte)" function
8929 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008931f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008932 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008936 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937#else
8938 long boff = 0;
8939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008940 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 (linenr_T)0, &boff);
8946#endif
8947}
8948
8949/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008950 * "byteidx()" function
8951 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008954 typval_T *argvars;
8955 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008956{
8957#ifdef FEAT_MBYTE
8958 char_u *t;
8959#endif
8960 char_u *str;
8961 long idx;
8962
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008963 str = get_tv_string_chk(&argvars[0]);
8964 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008965 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008966 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008967 return;
8968
8969#ifdef FEAT_MBYTE
8970 t = str;
8971 for ( ; idx > 0; idx--)
8972 {
8973 if (*t == NUL) /* EOL reached */
8974 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008975 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008976 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008977 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008978#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008979 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008981#endif
8982}
8983
8984/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008985 * "call(func, arglist)" function
8986 */
8987 static void
8988f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008989 typval_T *argvars;
8990 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008991{
8992 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008993 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008994 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00008995 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008996 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00008997 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008998
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008999 if (argvars[1].v_type != VAR_LIST)
9000 {
9001 EMSG(_(e_listreq));
9002 return;
9003 }
9004 if (argvars[1].vval.v_list == NULL)
9005 return;
9006
9007 if (argvars[0].v_type == VAR_FUNC)
9008 func = argvars[0].vval.v_string;
9009 else
9010 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009011 if (*func == NUL)
9012 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009013
Bram Moolenaare9a41262005-01-15 22:18:47 +00009014 if (argvars[2].v_type != VAR_UNKNOWN)
9015 {
9016 if (argvars[2].v_type != VAR_DICT)
9017 {
9018 EMSG(_(e_dictreq));
9019 return;
9020 }
9021 selfdict = argvars[2].vval.v_dict;
9022 }
9023
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009024 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9025 item = item->li_next)
9026 {
9027 if (argc == MAX_FUNC_ARGS)
9028 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009029 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009030 break;
9031 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009032 /* Make a copy of each argument. This is needed to be able to set
9033 * v_lock to VAR_FIXED in the copy without changing the original list.
9034 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009035 copy_tv(&item->li_tv, &argv[argc++]);
9036 }
9037
9038 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009039 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009040 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9041 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009042
9043 /* Free the arguments. */
9044 while (argc > 0)
9045 clear_tv(&argv[--argc]);
9046}
9047
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009048#ifdef FEAT_FLOAT
9049/*
9050 * "ceil({float})" function
9051 */
9052 static void
9053f_ceil(argvars, rettv)
9054 typval_T *argvars;
9055 typval_T *rettv;
9056{
9057 float_T f;
9058
9059 rettv->v_type = VAR_FLOAT;
9060 if (get_float_arg(argvars, &f) == OK)
9061 rettv->vval.v_float = ceil(f);
9062 else
9063 rettv->vval.v_float = 0.0;
9064}
9065#endif
9066
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009067/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009068 * "changenr()" function
9069 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009070 static void
9071f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009072 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009073 typval_T *rettv;
9074{
9075 rettv->vval.v_number = curbuf->b_u_seq_cur;
9076}
9077
9078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 * "char2nr(string)" function
9080 */
9081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 typval_T *argvars;
9084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085{
9086#ifdef FEAT_MBYTE
9087 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009088 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 else
9090#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092}
9093
9094/*
9095 * "cindent(lnum)" function
9096 */
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102#ifdef FEAT_CINDENT
9103 pos_T pos;
9104 linenr_T lnum;
9105
9106 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9109 {
9110 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009111 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 curwin->w_cursor = pos;
9113 }
9114 else
9115#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117}
9118
9119/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009120 * "clearmatches()" function
9121 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009122 static void
9123f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009124 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009125 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009126{
9127#ifdef FEAT_SEARCH_EXTRA
9128 clear_matches(curwin);
9129#endif
9130}
9131
9132/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 * "col(string)" function
9134 */
9135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009136f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009137 typval_T *argvars;
9138 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139{
9140 colnr_T col = 0;
9141 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009142 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009144 fp = var2fpos(&argvars[0], FALSE, &fnum);
9145 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 {
9147 if (fp->col == MAXCOL)
9148 {
9149 /* '> can be MAXCOL, get the length of the line then */
9150 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009151 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 else
9153 col = MAXCOL;
9154 }
9155 else
9156 {
9157 col = fp->col + 1;
9158#ifdef FEAT_VIRTUALEDIT
9159 /* col(".") when the cursor is on the NUL at the end of the line
9160 * because of "coladd" can be seen as an extra column. */
9161 if (virtual_active() && fp == &curwin->w_cursor)
9162 {
9163 char_u *p = ml_get_cursor();
9164
9165 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9166 curwin->w_virtcol - curwin->w_cursor.coladd))
9167 {
9168# ifdef FEAT_MBYTE
9169 int l;
9170
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009171 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 col += l;
9173# else
9174 if (*p != NUL && p[1] == NUL)
9175 ++col;
9176# endif
9177 }
9178 }
9179#endif
9180 }
9181 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009182 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183}
9184
Bram Moolenaar572cb562005-08-05 21:35:02 +00009185#if defined(FEAT_INS_EXPAND)
9186/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009187 * "complete()" function
9188 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009189 static void
9190f_complete(argvars, rettv)
9191 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009192 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009193{
9194 int startcol;
9195
9196 if ((State & INSERT) == 0)
9197 {
9198 EMSG(_("E785: complete() can only be used in Insert mode"));
9199 return;
9200 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009201
9202 /* Check for undo allowed here, because if something was already inserted
9203 * the line was already saved for undo and this check isn't done. */
9204 if (!undo_allowed())
9205 return;
9206
Bram Moolenaarade00832006-03-10 21:46:58 +00009207 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9208 {
9209 EMSG(_(e_invarg));
9210 return;
9211 }
9212
9213 startcol = get_tv_number_chk(&argvars[0], NULL);
9214 if (startcol <= 0)
9215 return;
9216
9217 set_completion(startcol - 1, argvars[1].vval.v_list);
9218}
9219
9220/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009221 * "complete_add()" function
9222 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009223 static void
9224f_complete_add(argvars, rettv)
9225 typval_T *argvars;
9226 typval_T *rettv;
9227{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009228 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009229}
9230
9231/*
9232 * "complete_check()" function
9233 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009234 static void
9235f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009236 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009237 typval_T *rettv;
9238{
9239 int saved = RedrawingDisabled;
9240
9241 RedrawingDisabled = 0;
9242 ins_compl_check_keys(0);
9243 rettv->vval.v_number = compl_interrupted;
9244 RedrawingDisabled = saved;
9245}
9246#endif
9247
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248/*
9249 * "confirm(message, buttons[, default [, type]])" function
9250 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009252f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009253 typval_T *argvars UNUSED;
9254 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255{
9256#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9257 char_u *message;
9258 char_u *buttons = NULL;
9259 char_u buf[NUMBUFLEN];
9260 char_u buf2[NUMBUFLEN];
9261 int def = 1;
9262 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009263 char_u *typestr;
9264 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009266 message = get_tv_string_chk(&argvars[0]);
9267 if (message == NULL)
9268 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009269 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009271 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9272 if (buttons == NULL)
9273 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009274 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009276 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009277 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009279 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9280 if (typestr == NULL)
9281 error = TRUE;
9282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009284 switch (TOUPPER_ASC(*typestr))
9285 {
9286 case 'E': type = VIM_ERROR; break;
9287 case 'Q': type = VIM_QUESTION; break;
9288 case 'I': type = VIM_INFO; break;
9289 case 'W': type = VIM_WARNING; break;
9290 case 'G': type = VIM_GENERIC; break;
9291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 }
9293 }
9294 }
9295 }
9296
9297 if (buttons == NULL || *buttons == NUL)
9298 buttons = (char_u *)_("&Ok");
9299
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009300 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009301 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303#endif
9304}
9305
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009306/*
9307 * "copy()" function
9308 */
9309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009311 typval_T *argvars;
9312 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009313{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009314 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009315}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009317#ifdef FEAT_FLOAT
9318/*
9319 * "cos()" function
9320 */
9321 static void
9322f_cos(argvars, rettv)
9323 typval_T *argvars;
9324 typval_T *rettv;
9325{
9326 float_T f;
9327
9328 rettv->v_type = VAR_FLOAT;
9329 if (get_float_arg(argvars, &f) == OK)
9330 rettv->vval.v_float = cos(f);
9331 else
9332 rettv->vval.v_float = 0.0;
9333}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009334
9335/*
9336 * "cosh()" function
9337 */
9338 static void
9339f_cosh(argvars, rettv)
9340 typval_T *argvars;
9341 typval_T *rettv;
9342{
9343 float_T f;
9344
9345 rettv->v_type = VAR_FLOAT;
9346 if (get_float_arg(argvars, &f) == OK)
9347 rettv->vval.v_float = cosh(f);
9348 else
9349 rettv->vval.v_float = 0.0;
9350}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009351#endif
9352
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009354 * "count()" function
9355 */
9356 static void
9357f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009358 typval_T *argvars;
9359 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009360{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009361 long n = 0;
9362 int ic = FALSE;
9363
Bram Moolenaare9a41262005-01-15 22:18:47 +00009364 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009365 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009366 listitem_T *li;
9367 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009368 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009369
Bram Moolenaare9a41262005-01-15 22:18:47 +00009370 if ((l = argvars[0].vval.v_list) != NULL)
9371 {
9372 li = l->lv_first;
9373 if (argvars[2].v_type != VAR_UNKNOWN)
9374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009375 int error = FALSE;
9376
9377 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009378 if (argvars[3].v_type != VAR_UNKNOWN)
9379 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009380 idx = get_tv_number_chk(&argvars[3], &error);
9381 if (!error)
9382 {
9383 li = list_find(l, idx);
9384 if (li == NULL)
9385 EMSGN(_(e_listidx), idx);
9386 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009387 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009388 if (error)
9389 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009390 }
9391
9392 for ( ; li != NULL; li = li->li_next)
9393 if (tv_equal(&li->li_tv, &argvars[1], ic))
9394 ++n;
9395 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009397 else if (argvars[0].v_type == VAR_DICT)
9398 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009399 int todo;
9400 dict_T *d;
9401 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009402
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009403 if ((d = argvars[0].vval.v_dict) != NULL)
9404 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009405 int error = FALSE;
9406
Bram Moolenaare9a41262005-01-15 22:18:47 +00009407 if (argvars[2].v_type != VAR_UNKNOWN)
9408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009409 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009410 if (argvars[3].v_type != VAR_UNKNOWN)
9411 EMSG(_(e_invarg));
9412 }
9413
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009414 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009415 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009416 {
9417 if (!HASHITEM_EMPTY(hi))
9418 {
9419 --todo;
9420 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
9421 ++n;
9422 }
9423 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009424 }
9425 }
9426 else
9427 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009428 rettv->vval.v_number = n;
9429}
9430
9431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9433 *
9434 * Checks the existence of a cscope connection.
9435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009438 typval_T *argvars UNUSED;
9439 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440{
9441#ifdef FEAT_CSCOPE
9442 int num = 0;
9443 char_u *dbpath = NULL;
9444 char_u *prepend = NULL;
9445 char_u buf[NUMBUFLEN];
9446
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009447 if (argvars[0].v_type != VAR_UNKNOWN
9448 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009450 num = (int)get_tv_number(&argvars[0]);
9451 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009452 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 }
9455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457#endif
9458}
9459
9460/*
9461 * "cursor(lnum, col)" function
9462 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009463 * Moves the cursor to the specified line and column.
9464 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009468 typval_T *argvars;
9469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470{
9471 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009472#ifdef FEAT_VIRTUALEDIT
9473 long coladd = 0;
9474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009476 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009477 if (argvars[1].v_type == VAR_UNKNOWN)
9478 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009479 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009480
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009481 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009482 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009483 line = pos.lnum;
9484 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009485#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009486 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009487#endif
9488 }
9489 else
9490 {
9491 line = get_tv_lnum(argvars);
9492 col = get_tv_number_chk(&argvars[1], NULL);
9493#ifdef FEAT_VIRTUALEDIT
9494 if (argvars[2].v_type != VAR_UNKNOWN)
9495 coladd = get_tv_number_chk(&argvars[2], NULL);
9496#endif
9497 }
9498 if (line < 0 || col < 0
9499#ifdef FEAT_VIRTUALEDIT
9500 || coladd < 0
9501#endif
9502 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009503 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 if (line > 0)
9505 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 if (col > 0)
9507 curwin->w_cursor.col = col - 1;
9508#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009509 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510#endif
9511
9512 /* Make sure the cursor is in a valid position. */
9513 check_cursor();
9514#ifdef FEAT_MBYTE
9515 /* Correct cursor for multi-byte character. */
9516 if (has_mbyte)
9517 mb_adjust_cursor();
9518#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009519
9520 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009521 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522}
9523
9524/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009525 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 */
9527 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009529 typval_T *argvars;
9530 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009532 int noref = 0;
9533
9534 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009535 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009536 if (noref < 0 || noref > 1)
9537 EMSG(_(e_invarg));
9538 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009539 {
9540 current_copyID += COPYID_INC;
9541 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543}
9544
9545/*
9546 * "delete()" function
9547 */
9548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009550 typval_T *argvars;
9551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552{
9553 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557}
9558
9559/*
9560 * "did_filetype()" function
9561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009563f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009564 typval_T *argvars UNUSED;
9565 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566{
9567#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569#endif
9570}
9571
9572/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009573 * "diff_filler()" function
9574 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009577 typval_T *argvars UNUSED;
9578 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009579{
9580#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009582#endif
9583}
9584
9585/*
9586 * "diff_hlID()" function
9587 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009588 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009590 typval_T *argvars UNUSED;
9591 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009592{
9593#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009595 static linenr_T prev_lnum = 0;
9596 static int changedtick = 0;
9597 static int fnum = 0;
9598 static int change_start = 0;
9599 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009600 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009601 int filler_lines;
9602 int col;
9603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009604 if (lnum < 0) /* ignore type error in {lnum} arg */
9605 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009606 if (lnum != prev_lnum
9607 || changedtick != curbuf->b_changedtick
9608 || fnum != curbuf->b_fnum)
9609 {
9610 /* New line, buffer, change: need to get the values. */
9611 filler_lines = diff_check(curwin, lnum);
9612 if (filler_lines < 0)
9613 {
9614 if (filler_lines == -1)
9615 {
9616 change_start = MAXCOL;
9617 change_end = -1;
9618 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9619 hlID = HLF_ADD; /* added line */
9620 else
9621 hlID = HLF_CHD; /* changed line */
9622 }
9623 else
9624 hlID = HLF_ADD; /* added line */
9625 }
9626 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009627 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009628 prev_lnum = lnum;
9629 changedtick = curbuf->b_changedtick;
9630 fnum = curbuf->b_fnum;
9631 }
9632
9633 if (hlID == HLF_CHD || hlID == HLF_TXD)
9634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009636 if (col >= change_start && col <= change_end)
9637 hlID = HLF_TXD; /* changed text */
9638 else
9639 hlID = HLF_CHD; /* changed line */
9640 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009641 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009642#endif
9643}
9644
9645/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009646 * "empty({expr})" function
9647 */
9648 static void
9649f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009650 typval_T *argvars;
9651 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009652{
9653 int n;
9654
9655 switch (argvars[0].v_type)
9656 {
9657 case VAR_STRING:
9658 case VAR_FUNC:
9659 n = argvars[0].vval.v_string == NULL
9660 || *argvars[0].vval.v_string == NUL;
9661 break;
9662 case VAR_NUMBER:
9663 n = argvars[0].vval.v_number == 0;
9664 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009665#ifdef FEAT_FLOAT
9666 case VAR_FLOAT:
9667 n = argvars[0].vval.v_float == 0.0;
9668 break;
9669#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009670 case VAR_LIST:
9671 n = argvars[0].vval.v_list == NULL
9672 || argvars[0].vval.v_list->lv_first == NULL;
9673 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009674 case VAR_DICT:
9675 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009676 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009677 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009678 default:
9679 EMSG2(_(e_intern2), "f_empty()");
9680 n = 0;
9681 }
9682
9683 rettv->vval.v_number = n;
9684}
9685
9686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 * "escape({string}, {chars})" function
9688 */
9689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009690f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009691 typval_T *argvars;
9692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693{
9694 char_u buf[NUMBUFLEN];
9695
Bram Moolenaar758711c2005-02-02 23:11:38 +00009696 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9697 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009698 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699}
9700
9701/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009702 * "eval()" function
9703 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009704 static void
9705f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009706 typval_T *argvars;
9707 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009708{
9709 char_u *s;
9710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009711 s = get_tv_string_chk(&argvars[0]);
9712 if (s != NULL)
9713 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009714
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009715 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9716 {
9717 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009718 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009719 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009720 else if (*s != NUL)
9721 EMSG(_(e_trailing));
9722}
9723
9724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 * "eventhandler()" function
9726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009728f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009729 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009732 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009733}
9734
9735/*
9736 * "executable()" function
9737 */
9738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009740 typval_T *argvars;
9741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009743 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744}
9745
9746/*
9747 * "exists()" function
9748 */
9749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009751 typval_T *argvars;
9752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753{
9754 char_u *p;
9755 char_u *name;
9756 int n = FALSE;
9757 int len = 0;
9758
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009759 no_autoload = TRUE;
9760
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762 if (*p == '$') /* environment variable */
9763 {
9764 /* first try "normal" environment variables (fast) */
9765 if (mch_getenv(p + 1) != NULL)
9766 n = TRUE;
9767 else
9768 {
9769 /* try expanding things like $VIM and ${HOME} */
9770 p = expand_env_save(p);
9771 if (p != NULL && *p != '$')
9772 n = TRUE;
9773 vim_free(p);
9774 }
9775 }
9776 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009779 if (*skipwhite(p) != NUL)
9780 n = FALSE; /* trailing garbage */
9781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 else if (*p == '*') /* internal or user defined function */
9783 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009784 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 }
9786 else if (*p == ':')
9787 {
9788 n = cmd_exists(p + 1);
9789 }
9790 else if (*p == '#')
9791 {
9792#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009793 if (p[1] == '#')
9794 n = autocmd_supported(p + 2);
9795 else
9796 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797#endif
9798 }
9799 else /* internal variable */
9800 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009801 char_u *tofree;
9802 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009804 /* get_name_len() takes care of expanding curly braces */
9805 name = p;
9806 len = get_name_len(&p, &tofree, TRUE, FALSE);
9807 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009809 if (tofree != NULL)
9810 name = tofree;
9811 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9812 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009814 /* handle d.key, l[idx], f(expr) */
9815 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9816 if (n)
9817 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009818 }
9819 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009820 if (*p != NUL)
9821 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009823 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 }
9825
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009826 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009827
9828 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829}
9830
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009831#ifdef FEAT_FLOAT
9832/*
9833 * "exp()" function
9834 */
9835 static void
9836f_exp(argvars, rettv)
9837 typval_T *argvars;
9838 typval_T *rettv;
9839{
9840 float_T f;
9841
9842 rettv->v_type = VAR_FLOAT;
9843 if (get_float_arg(argvars, &f) == OK)
9844 rettv->vval.v_float = exp(f);
9845 else
9846 rettv->vval.v_float = 0.0;
9847}
9848#endif
9849
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850/*
9851 * "expand()" function
9852 */
9853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009855 typval_T *argvars;
9856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857{
9858 char_u *s;
9859 int len;
9860 char_u *errormsg;
9861 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9862 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009863 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 rettv->v_type = VAR_STRING;
9866 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 if (*s == '%' || *s == '#' || *s == '<')
9868 {
9869 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009870 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 --emsg_off;
9872 }
9873 else
9874 {
9875 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009876 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009877 if (argvars[1].v_type != VAR_UNKNOWN
9878 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009880 if (!error)
9881 {
9882 ExpandInit(&xpc);
9883 xpc.xp_context = EXPAND_FILES;
9884 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 }
9886 else
9887 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 }
9889}
9890
9891/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009892 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009893 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009894 */
9895 static void
9896f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 typval_T *argvars;
9898 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009899{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009900 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009901 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009902 list_T *l1, *l2;
9903 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009904 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009905 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009906
Bram Moolenaare9a41262005-01-15 22:18:47 +00009907 l1 = argvars[0].vval.v_list;
9908 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009909 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9910 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009911 {
9912 if (argvars[2].v_type != VAR_UNKNOWN)
9913 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009914 before = get_tv_number_chk(&argvars[2], &error);
9915 if (error)
9916 return; /* type error; errmsg already given */
9917
Bram Moolenaar758711c2005-02-02 23:11:38 +00009918 if (before == l1->lv_len)
9919 item = NULL;
9920 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009921 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009922 item = list_find(l1, before);
9923 if (item == NULL)
9924 {
9925 EMSGN(_(e_listidx), before);
9926 return;
9927 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 }
9929 }
9930 else
9931 item = NULL;
9932 list_extend(l1, l2, item);
9933
Bram Moolenaare9a41262005-01-15 22:18:47 +00009934 copy_tv(&argvars[0], rettv);
9935 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009937 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9938 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009939 dict_T *d1, *d2;
9940 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009941 char_u *action;
9942 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009943 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009944 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009945
9946 d1 = argvars[0].vval.v_dict;
9947 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009948 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9949 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009950 {
9951 /* Check the third argument. */
9952 if (argvars[2].v_type != VAR_UNKNOWN)
9953 {
9954 static char *(av[]) = {"keep", "force", "error"};
9955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009956 action = get_tv_string_chk(&argvars[2]);
9957 if (action == NULL)
9958 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009959 for (i = 0; i < 3; ++i)
9960 if (STRCMP(action, av[i]) == 0)
9961 break;
9962 if (i == 3)
9963 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009964 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009965 return;
9966 }
9967 }
9968 else
9969 action = (char_u *)"force";
9970
9971 /* Go over all entries in the second dict and add them to the
9972 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009973 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009974 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009975 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009976 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009977 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009978 --todo;
9979 di1 = dict_find(d1, hi2->hi_key, -1);
9980 if (di1 == NULL)
9981 {
9982 di1 = dictitem_copy(HI2DI(hi2));
9983 if (di1 != NULL && dict_add(d1, di1) == FAIL)
9984 dictitem_free(di1);
9985 }
9986 else if (*action == 'e')
9987 {
9988 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
9989 break;
9990 }
9991 else if (*action == 'f')
9992 {
9993 clear_tv(&di1->di_tv);
9994 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
9995 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009996 }
9997 }
9998
Bram Moolenaare9a41262005-01-15 22:18:47 +00009999 copy_tv(&argvars[0], rettv);
10000 }
10001 }
10002 else
10003 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010004}
10005
10006/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010007 * "feedkeys()" function
10008 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010009 static void
10010f_feedkeys(argvars, rettv)
10011 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010012 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010013{
10014 int remap = TRUE;
10015 char_u *keys, *flags;
10016 char_u nbuf[NUMBUFLEN];
10017 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010018 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010019
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010020 /* This is not allowed in the sandbox. If the commands would still be
10021 * executed in the sandbox it would be OK, but it probably happens later,
10022 * when "sandbox" is no longer set. */
10023 if (check_secure())
10024 return;
10025
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010026 keys = get_tv_string(&argvars[0]);
10027 if (*keys != NUL)
10028 {
10029 if (argvars[1].v_type != VAR_UNKNOWN)
10030 {
10031 flags = get_tv_string_buf(&argvars[1], nbuf);
10032 for ( ; *flags != NUL; ++flags)
10033 {
10034 switch (*flags)
10035 {
10036 case 'n': remap = FALSE; break;
10037 case 'm': remap = TRUE; break;
10038 case 't': typed = TRUE; break;
10039 }
10040 }
10041 }
10042
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010043 /* Need to escape K_SPECIAL and CSI before putting the string in the
10044 * typeahead buffer. */
10045 keys_esc = vim_strsave_escape_csi(keys);
10046 if (keys_esc != NULL)
10047 {
10048 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010049 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010050 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010051 if (vgetc_busy)
10052 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010053 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010054 }
10055}
10056
10057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058 * "filereadable()" function
10059 */
10060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010061f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010062 typval_T *argvars;
10063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010065 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 char_u *p;
10067 int n;
10068
Bram Moolenaarc236c162008-07-13 17:41:49 +000010069#ifndef O_NONBLOCK
10070# define O_NONBLOCK 0
10071#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010073 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10074 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075 {
10076 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010077 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 }
10079 else
10080 n = FALSE;
10081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010082 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083}
10084
10085/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010086 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 * rights to write into.
10088 */
10089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010091 typval_T *argvars;
10092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010094 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095}
10096
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010097static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010098
10099 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010100findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010101 typval_T *argvars;
10102 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010103 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010104{
10105#ifdef FEAT_SEARCHPATH
10106 char_u *fname;
10107 char_u *fresult = NULL;
10108 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10109 char_u *p;
10110 char_u pathbuf[NUMBUFLEN];
10111 int count = 1;
10112 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010113 int error = FALSE;
10114#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010115
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010116 rettv->vval.v_string = NULL;
10117 rettv->v_type = VAR_STRING;
10118
10119#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010121
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010122 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010123 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010124 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10125 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010126 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010127 else
10128 {
10129 if (*p != NUL)
10130 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010132 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010133 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010134 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010135 }
10136
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010137 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10138 error = TRUE;
10139
10140 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142 do
10143 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010144 if (rettv->v_type == VAR_STRING)
10145 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010146 fresult = find_file_in_path_option(first ? fname : NULL,
10147 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010148 0, first, path,
10149 find_what,
10150 curbuf->b_ffname,
10151 find_what == FINDFILE_DIR
10152 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010154
10155 if (fresult != NULL && rettv->v_type == VAR_LIST)
10156 list_append_string(rettv->vval.v_list, fresult, -1);
10157
10158 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010160
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010161 if (rettv->v_type == VAR_STRING)
10162 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010163#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010164}
10165
Bram Moolenaar33570922005-01-25 22:26:29 +000010166static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10167static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010168
10169/*
10170 * Implementation of map() and filter().
10171 */
10172 static void
10173filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010174 typval_T *argvars;
10175 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010176 int map;
10177{
10178 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010179 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010180 listitem_T *li, *nli;
10181 list_T *l = NULL;
10182 dictitem_T *di;
10183 hashtab_T *ht;
10184 hashitem_T *hi;
10185 dict_T *d = NULL;
10186 typval_T save_val;
10187 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010188 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010189 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010190 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010191 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010192 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010193
Bram Moolenaare9a41262005-01-15 22:18:47 +000010194 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010195 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010196 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010197 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010198 return;
10199 }
10200 else if (argvars[0].v_type == VAR_DICT)
10201 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010202 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010203 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010204 return;
10205 }
10206 else
10207 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010208 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010209 return;
10210 }
10211
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010212 expr = get_tv_string_buf_chk(&argvars[1], buf);
10213 /* On type errors, the preceding call has already displayed an error
10214 * message. Avoid a misleading error message for an empty string that
10215 * was not passed as argument. */
10216 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010217 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010218 prepare_vimvar(VV_VAL, &save_val);
10219 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010220
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010221 /* We reset "did_emsg" to be able to detect whether an error
10222 * occurred during evaluation of the expression. */
10223 save_did_emsg = did_emsg;
10224 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010225
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010226 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010227 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010229 vimvars[VV_KEY].vv_type = VAR_STRING;
10230
10231 ht = &d->dv_hashtab;
10232 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010233 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010234 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 if (!HASHITEM_EMPTY(hi))
10237 {
10238 --todo;
10239 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010240 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 break;
10242 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010243 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010244 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010245 break;
10246 if (!map && rem)
10247 dictitem_remove(d, di);
10248 clear_tv(&vimvars[VV_KEY].vv_tv);
10249 }
10250 }
10251 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010252 }
10253 else
10254 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010255 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10256
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010257 for (li = l->lv_first; li != NULL; li = nli)
10258 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010259 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010260 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010261 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010262 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010263 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010264 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010265 break;
10266 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010267 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010268 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010269 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010270 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010271
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010272 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010273 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010274
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010275 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010276 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010277
10278 copy_tv(&argvars[0], rettv);
10279}
10280
10281 static int
10282filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010283 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010284 char_u *expr;
10285 int map;
10286 int *remp;
10287{
Bram Moolenaar33570922005-01-25 22:26:29 +000010288 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010289 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010290 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291
Bram Moolenaar33570922005-01-25 22:26:29 +000010292 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010293 s = expr;
10294 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010295 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010296 if (*s != NUL) /* check for trailing chars after expr */
10297 {
10298 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010299 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010300 }
10301 if (map)
10302 {
10303 /* map(): replace the list item value */
10304 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010305 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010306 *tv = rettv;
10307 }
10308 else
10309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 int error = FALSE;
10311
Bram Moolenaare9a41262005-01-15 22:18:47 +000010312 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010313 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 /* On type error, nothing has been removed; return FAIL to stop the
10316 * loop. The error message was given by get_tv_number_chk(). */
10317 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010318 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010319 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010320 retval = OK;
10321theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010322 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010323 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010324}
10325
10326/*
10327 * "filter()" function
10328 */
10329 static void
10330f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010331 typval_T *argvars;
10332 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010333{
10334 filter_map(argvars, rettv, FALSE);
10335}
10336
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010337/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010338 * "finddir({fname}[, {path}[, {count}]])" function
10339 */
10340 static void
10341f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010342 typval_T *argvars;
10343 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010344{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010345 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010346}
10347
10348/*
10349 * "findfile({fname}[, {path}[, {count}]])" function
10350 */
10351 static void
10352f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010353 typval_T *argvars;
10354 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010355{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010356 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010357}
10358
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010359#ifdef FEAT_FLOAT
10360/*
10361 * "float2nr({float})" function
10362 */
10363 static void
10364f_float2nr(argvars, rettv)
10365 typval_T *argvars;
10366 typval_T *rettv;
10367{
10368 float_T f;
10369
10370 if (get_float_arg(argvars, &f) == OK)
10371 {
10372 if (f < -0x7fffffff)
10373 rettv->vval.v_number = -0x7fffffff;
10374 else if (f > 0x7fffffff)
10375 rettv->vval.v_number = 0x7fffffff;
10376 else
10377 rettv->vval.v_number = (varnumber_T)f;
10378 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010379}
10380
10381/*
10382 * "floor({float})" function
10383 */
10384 static void
10385f_floor(argvars, rettv)
10386 typval_T *argvars;
10387 typval_T *rettv;
10388{
10389 float_T f;
10390
10391 rettv->v_type = VAR_FLOAT;
10392 if (get_float_arg(argvars, &f) == OK)
10393 rettv->vval.v_float = floor(f);
10394 else
10395 rettv->vval.v_float = 0.0;
10396}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010397
10398/*
10399 * "fmod()" function
10400 */
10401 static void
10402f_fmod(argvars, rettv)
10403 typval_T *argvars;
10404 typval_T *rettv;
10405{
10406 float_T fx, fy;
10407
10408 rettv->v_type = VAR_FLOAT;
10409 if (get_float_arg(argvars, &fx) == OK
10410 && get_float_arg(&argvars[1], &fy) == OK)
10411 rettv->vval.v_float = fmod(fx, fy);
10412 else
10413 rettv->vval.v_float = 0.0;
10414}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010415#endif
10416
Bram Moolenaar0d660222005-01-07 21:51:51 +000010417/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010418 * "fnameescape({string})" function
10419 */
10420 static void
10421f_fnameescape(argvars, rettv)
10422 typval_T *argvars;
10423 typval_T *rettv;
10424{
10425 rettv->vval.v_string = vim_strsave_fnameescape(
10426 get_tv_string(&argvars[0]), FALSE);
10427 rettv->v_type = VAR_STRING;
10428}
10429
10430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431 * "fnamemodify({fname}, {mods})" function
10432 */
10433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010434f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010435 typval_T *argvars;
10436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437{
10438 char_u *fname;
10439 char_u *mods;
10440 int usedlen = 0;
10441 int len;
10442 char_u *fbuf = NULL;
10443 char_u buf[NUMBUFLEN];
10444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 fname = get_tv_string_chk(&argvars[0]);
10446 mods = get_tv_string_buf_chk(&argvars[1], buf);
10447 if (fname == NULL || mods == NULL)
10448 fname = NULL;
10449 else
10450 {
10451 len = (int)STRLEN(fname);
10452 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010455 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010457 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010459 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010460 vim_free(fbuf);
10461}
10462
Bram Moolenaar33570922005-01-25 22:26:29 +000010463static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464
10465/*
10466 * "foldclosed()" function
10467 */
10468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010469foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010470 typval_T *argvars;
10471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 int end;
10473{
10474#ifdef FEAT_FOLDING
10475 linenr_T lnum;
10476 linenr_T first, last;
10477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010478 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10480 {
10481 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10482 {
10483 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010484 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010485 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010486 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 return;
10488 }
10489 }
10490#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010491 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492}
10493
10494/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010495 * "foldclosed()" function
10496 */
10497 static void
10498f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010499 typval_T *argvars;
10500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010501{
10502 foldclosed_both(argvars, rettv, FALSE);
10503}
10504
10505/*
10506 * "foldclosedend()" function
10507 */
10508 static void
10509f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010510 typval_T *argvars;
10511 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010512{
10513 foldclosed_both(argvars, rettv, TRUE);
10514}
10515
10516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 * "foldlevel()" function
10518 */
10519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 typval_T *argvars;
10522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523{
10524#ifdef FEAT_FOLDING
10525 linenr_T lnum;
10526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010527 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010528 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010529 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531}
10532
10533/*
10534 * "foldtext()" function
10535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010537f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010538 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010540{
10541#ifdef FEAT_FOLDING
10542 linenr_T lnum;
10543 char_u *s;
10544 char_u *r;
10545 int len;
10546 char *txt;
10547#endif
10548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 rettv->v_type = VAR_STRING;
10550 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010552 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10553 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10554 <= curbuf->b_ml.ml_line_count
10555 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010556 {
10557 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010558 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10559 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 {
10561 if (!linewhite(lnum))
10562 break;
10563 ++lnum;
10564 }
10565
10566 /* Find interesting text in this line. */
10567 s = skipwhite(ml_get(lnum));
10568 /* skip C comment-start */
10569 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010572 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010574 {
10575 s = skipwhite(ml_get(lnum + 1));
10576 if (*s == '*')
10577 s = skipwhite(s + 1);
10578 }
10579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 txt = _("+-%s%3ld lines: ");
10581 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 + 20 /* for %3ld */
10584 + STRLEN(s))); /* concatenated */
10585 if (r != NULL)
10586 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10588 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10589 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 len = (int)STRLEN(r);
10591 STRCAT(r, s);
10592 /* remove 'foldmarker' and 'commentstring' */
10593 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010594 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595 }
10596 }
10597#endif
10598}
10599
10600/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010601 * "foldtextresult(lnum)" function
10602 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010604f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010605 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010606 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010607{
10608#ifdef FEAT_FOLDING
10609 linenr_T lnum;
10610 char_u *text;
10611 char_u buf[51];
10612 foldinfo_T foldinfo;
10613 int fold_count;
10614#endif
10615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010616 rettv->v_type = VAR_STRING;
10617 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010618#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 /* treat illegal types and illegal string values for {lnum} the same */
10621 if (lnum < 0)
10622 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010623 fold_count = foldedCount(curwin, lnum, &foldinfo);
10624 if (fold_count > 0)
10625 {
10626 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10627 &foldinfo, buf);
10628 if (text == buf)
10629 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010630 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010631 }
10632#endif
10633}
10634
10635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 * "foreground()" function
10637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010640 typval_T *argvars UNUSED;
10641 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010643#ifdef FEAT_GUI
10644 if (gui.in_use)
10645 gui_mch_set_foreground();
10646#else
10647# ifdef WIN32
10648 win32_set_foreground();
10649# endif
10650#endif
10651}
10652
10653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010654 * "function()" function
10655 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010657f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010658 typval_T *argvars;
10659 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010660{
10661 char_u *s;
10662
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010663 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010664 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010665 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010666 /* Don't check an autoload name for existence here. */
10667 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010668 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010669 else
10670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010671 rettv->vval.v_string = vim_strsave(s);
10672 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010673 }
10674}
10675
10676/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010677 * "garbagecollect()" function
10678 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010679 static void
10680f_garbagecollect(argvars, rettv)
10681 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010682 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010683{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010684 /* This is postponed until we are back at the toplevel, because we may be
10685 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10686 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010687
10688 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10689 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010690}
10691
10692/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010693 * "get()" function
10694 */
10695 static void
10696f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010697 typval_T *argvars;
10698 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010699{
Bram Moolenaar33570922005-01-25 22:26:29 +000010700 listitem_T *li;
10701 list_T *l;
10702 dictitem_T *di;
10703 dict_T *d;
10704 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010705
Bram Moolenaare9a41262005-01-15 22:18:47 +000010706 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010707 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010708 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010709 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010710 int error = FALSE;
10711
10712 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10713 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010714 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010715 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010717 else if (argvars[0].v_type == VAR_DICT)
10718 {
10719 if ((d = argvars[0].vval.v_dict) != NULL)
10720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010721 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010722 if (di != NULL)
10723 tv = &di->di_tv;
10724 }
10725 }
10726 else
10727 EMSG2(_(e_listdictarg), "get()");
10728
10729 if (tv == NULL)
10730 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010731 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010732 copy_tv(&argvars[2], rettv);
10733 }
10734 else
10735 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010736}
10737
Bram Moolenaar342337a2005-07-21 21:11:17 +000010738static 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 +000010739
10740/*
10741 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010742 * Return a range (from start to end) of lines in rettv from the specified
10743 * buffer.
10744 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010745 */
10746 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010747get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010748 buf_T *buf;
10749 linenr_T start;
10750 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010751 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010752 typval_T *rettv;
10753{
10754 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010755
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010756 if (retlist && rettv_list_alloc(rettv) == FAIL)
10757 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010758
10759 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10760 return;
10761
10762 if (!retlist)
10763 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010764 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10765 p = ml_get_buf(buf, start, FALSE);
10766 else
10767 p = (char_u *)"";
10768
10769 rettv->v_type = VAR_STRING;
10770 rettv->vval.v_string = vim_strsave(p);
10771 }
10772 else
10773 {
10774 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010775 return;
10776
10777 if (start < 1)
10778 start = 1;
10779 if (end > buf->b_ml.ml_line_count)
10780 end = buf->b_ml.ml_line_count;
10781 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010782 if (list_append_string(rettv->vval.v_list,
10783 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010784 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010785 }
10786}
10787
10788/*
10789 * "getbufline()" function
10790 */
10791 static void
10792f_getbufline(argvars, rettv)
10793 typval_T *argvars;
10794 typval_T *rettv;
10795{
10796 linenr_T lnum;
10797 linenr_T end;
10798 buf_T *buf;
10799
10800 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10801 ++emsg_off;
10802 buf = get_buf_tv(&argvars[0]);
10803 --emsg_off;
10804
Bram Moolenaar661b1822005-07-28 22:36:45 +000010805 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010806 if (argvars[2].v_type == VAR_UNKNOWN)
10807 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010808 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010809 end = get_tv_lnum_buf(&argvars[2], buf);
10810
Bram Moolenaar342337a2005-07-21 21:11:17 +000010811 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010812}
10813
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814/*
10815 * "getbufvar()" function
10816 */
10817 static void
10818f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010819 typval_T *argvars;
10820 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010821{
10822 buf_T *buf;
10823 buf_T *save_curbuf;
10824 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010825 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010826
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010827 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10828 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010829 ++emsg_off;
10830 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010831
10832 rettv->v_type = VAR_STRING;
10833 rettv->vval.v_string = NULL;
10834
10835 if (buf != NULL && varname != NULL)
10836 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010837 /* set curbuf to be our buf, temporarily */
10838 save_curbuf = curbuf;
10839 curbuf = buf;
10840
Bram Moolenaar0d660222005-01-07 21:51:51 +000010841 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010842 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010843 else
10844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 if (*varname == NUL)
10846 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10847 * scope prefix before the NUL byte is required by
10848 * find_var_in_ht(). */
10849 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010850 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010851 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010852 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010853 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010854 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010855
10856 /* restore previous notion of curbuf */
10857 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010858 }
10859
10860 --emsg_off;
10861}
10862
10863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 * "getchar()" function
10865 */
10866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010868 typval_T *argvars;
10869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870{
10871 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010872 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010874 /* Position the cursor. Needed after a message that ends in a space. */
10875 windgoto(msg_row, msg_col);
10876
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877 ++no_mapping;
10878 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010879 for (;;)
10880 {
10881 if (argvars[0].v_type == VAR_UNKNOWN)
10882 /* getchar(): blocking wait. */
10883 n = safe_vgetc();
10884 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10885 /* getchar(1): only check if char avail */
10886 n = vpeekc();
10887 else if (error || vpeekc() == NUL)
10888 /* illegal argument or getchar(0) and no char avail: return zero */
10889 n = 0;
10890 else
10891 /* getchar(0) and char avail: return char */
10892 n = safe_vgetc();
10893 if (n == K_IGNORE)
10894 continue;
10895 break;
10896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 --no_mapping;
10898 --allow_keys;
10899
Bram Moolenaar219b8702006-11-01 14:32:36 +000010900 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10901 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10902 vimvars[VV_MOUSE_COL].vv_nr = 0;
10903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 if (IS_SPECIAL(n) || mod_mask != 0)
10906 {
10907 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10908 int i = 0;
10909
10910 /* Turn a special key into three bytes, plus modifier. */
10911 if (mod_mask != 0)
10912 {
10913 temp[i++] = K_SPECIAL;
10914 temp[i++] = KS_MODIFIER;
10915 temp[i++] = mod_mask;
10916 }
10917 if (IS_SPECIAL(n))
10918 {
10919 temp[i++] = K_SPECIAL;
10920 temp[i++] = K_SECOND(n);
10921 temp[i++] = K_THIRD(n);
10922 }
10923#ifdef FEAT_MBYTE
10924 else if (has_mbyte)
10925 i += (*mb_char2bytes)(n, temp + i);
10926#endif
10927 else
10928 temp[i++] = n;
10929 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930 rettv->v_type = VAR_STRING;
10931 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010932
10933#ifdef FEAT_MOUSE
10934 if (n == K_LEFTMOUSE
10935 || n == K_LEFTMOUSE_NM
10936 || n == K_LEFTDRAG
10937 || n == K_LEFTRELEASE
10938 || n == K_LEFTRELEASE_NM
10939 || n == K_MIDDLEMOUSE
10940 || n == K_MIDDLEDRAG
10941 || n == K_MIDDLERELEASE
10942 || n == K_RIGHTMOUSE
10943 || n == K_RIGHTDRAG
10944 || n == K_RIGHTRELEASE
10945 || n == K_X1MOUSE
10946 || n == K_X1DRAG
10947 || n == K_X1RELEASE
10948 || n == K_X2MOUSE
10949 || n == K_X2DRAG
10950 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010951 || n == K_MOUSELEFT
10952 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010953 || n == K_MOUSEDOWN
10954 || n == K_MOUSEUP)
10955 {
10956 int row = mouse_row;
10957 int col = mouse_col;
10958 win_T *win;
10959 linenr_T lnum;
10960# ifdef FEAT_WINDOWS
10961 win_T *wp;
10962# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010963 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010964
10965 if (row >= 0 && col >= 0)
10966 {
10967 /* Find the window at the mouse coordinates and compute the
10968 * text position. */
10969 win = mouse_find_win(&row, &col);
10970 (void)mouse_comp_pos(win, &row, &col, &lnum);
10971# ifdef FEAT_WINDOWS
10972 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010973 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010974# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010975 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010976 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10977 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10978 }
10979 }
10980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 }
10982}
10983
10984/*
10985 * "getcharmod()" function
10986 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010989 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993}
10994
10995/*
10996 * "getcmdline()" function
10997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010999f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011000 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011001 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011003 rettv->v_type = VAR_STRING;
11004 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005}
11006
11007/*
11008 * "getcmdpos()" function
11009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011012 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011014{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016}
11017
11018/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011019 * "getcmdtype()" function
11020 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011021 static void
11022f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011023 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011024 typval_T *rettv;
11025{
11026 rettv->v_type = VAR_STRING;
11027 rettv->vval.v_string = alloc(2);
11028 if (rettv->vval.v_string != NULL)
11029 {
11030 rettv->vval.v_string[0] = get_cmdline_type();
11031 rettv->vval.v_string[1] = NUL;
11032 }
11033}
11034
11035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 * "getcwd()" function
11037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011040 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042{
11043 char_u cwd[MAXPATHL];
11044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011045 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011047 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 else
11049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011052 if (rettv->vval.v_string != NULL)
11053 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054#endif
11055 }
11056}
11057
11058/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011059 * "getfontname()" function
11060 */
11061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011062f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011063 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011064 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011065{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066 rettv->v_type = VAR_STRING;
11067 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011068#ifdef FEAT_GUI
11069 if (gui.in_use)
11070 {
11071 GuiFont font;
11072 char_u *name = NULL;
11073
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011074 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011075 {
11076 /* Get the "Normal" font. Either the name saved by
11077 * hl_set_font_name() or from the font ID. */
11078 font = gui.norm_font;
11079 name = hl_get_font_name();
11080 }
11081 else
11082 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011083 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011084 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11085 return;
11086 font = gui_mch_get_font(name, FALSE);
11087 if (font == NOFONT)
11088 return; /* Invalid font name, return empty string. */
11089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011090 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011091 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011092 gui_mch_free_font(font);
11093 }
11094#endif
11095}
11096
11097/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011098 * "getfperm({fname})" function
11099 */
11100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011102 typval_T *argvars;
11103 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011104{
11105 char_u *fname;
11106 struct stat st;
11107 char_u *perm = NULL;
11108 char_u flags[] = "rwx";
11109 int i;
11110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011114 if (mch_stat((char *)fname, &st) >= 0)
11115 {
11116 perm = vim_strsave((char_u *)"---------");
11117 if (perm != NULL)
11118 {
11119 for (i = 0; i < 9; i++)
11120 {
11121 if (st.st_mode & (1 << (8 - i)))
11122 perm[i] = flags[i % 3];
11123 }
11124 }
11125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011126 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011127}
11128
11129/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 * "getfsize({fname})" function
11131 */
11132 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011134 typval_T *argvars;
11135 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011136{
11137 char_u *fname;
11138 struct stat st;
11139
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011140 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011142 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143
11144 if (mch_stat((char *)fname, &st) >= 0)
11145 {
11146 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011147 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011149 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011151
11152 /* non-perfect check for overflow */
11153 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11154 rettv->vval.v_number = -2;
11155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011156 }
11157 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159}
11160
11161/*
11162 * "getftime({fname})" function
11163 */
11164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011166 typval_T *argvars;
11167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168{
11169 char_u *fname;
11170 struct stat st;
11171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173
11174 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178}
11179
11180/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011181 * "getftype({fname})" function
11182 */
11183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011184f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011185 typval_T *argvars;
11186 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011187{
11188 char_u *fname;
11189 struct stat st;
11190 char_u *type = NULL;
11191 char *t;
11192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011194
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011196 if (mch_lstat((char *)fname, &st) >= 0)
11197 {
11198#ifdef S_ISREG
11199 if (S_ISREG(st.st_mode))
11200 t = "file";
11201 else if (S_ISDIR(st.st_mode))
11202 t = "dir";
11203# ifdef S_ISLNK
11204 else if (S_ISLNK(st.st_mode))
11205 t = "link";
11206# endif
11207# ifdef S_ISBLK
11208 else if (S_ISBLK(st.st_mode))
11209 t = "bdev";
11210# endif
11211# ifdef S_ISCHR
11212 else if (S_ISCHR(st.st_mode))
11213 t = "cdev";
11214# endif
11215# ifdef S_ISFIFO
11216 else if (S_ISFIFO(st.st_mode))
11217 t = "fifo";
11218# endif
11219# ifdef S_ISSOCK
11220 else if (S_ISSOCK(st.st_mode))
11221 t = "fifo";
11222# endif
11223 else
11224 t = "other";
11225#else
11226# ifdef S_IFMT
11227 switch (st.st_mode & S_IFMT)
11228 {
11229 case S_IFREG: t = "file"; break;
11230 case S_IFDIR: t = "dir"; break;
11231# ifdef S_IFLNK
11232 case S_IFLNK: t = "link"; break;
11233# endif
11234# ifdef S_IFBLK
11235 case S_IFBLK: t = "bdev"; break;
11236# endif
11237# ifdef S_IFCHR
11238 case S_IFCHR: t = "cdev"; break;
11239# endif
11240# ifdef S_IFIFO
11241 case S_IFIFO: t = "fifo"; break;
11242# endif
11243# ifdef S_IFSOCK
11244 case S_IFSOCK: t = "socket"; break;
11245# endif
11246 default: t = "other";
11247 }
11248# else
11249 if (mch_isdir(fname))
11250 t = "dir";
11251 else
11252 t = "file";
11253# endif
11254#endif
11255 type = vim_strsave((char_u *)t);
11256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011257 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011258}
11259
11260/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011261 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011262 */
11263 static void
11264f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011265 typval_T *argvars;
11266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011267{
11268 linenr_T lnum;
11269 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011270 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011271
11272 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011273 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011274 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011275 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011276 retlist = FALSE;
11277 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011278 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011279 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011280 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011281 retlist = TRUE;
11282 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011283
Bram Moolenaar342337a2005-07-21 21:11:17 +000011284 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011285}
11286
11287/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011288 * "getmatches()" function
11289 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011290 static void
11291f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011292 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011293 typval_T *rettv;
11294{
11295#ifdef FEAT_SEARCH_EXTRA
11296 dict_T *dict;
11297 matchitem_T *cur = curwin->w_match_head;
11298
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011299 if (rettv_list_alloc(rettv) == OK)
11300 {
11301 while (cur != NULL)
11302 {
11303 dict = dict_alloc();
11304 if (dict == NULL)
11305 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011306 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11307 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11308 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11309 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11310 list_append_dict(rettv->vval.v_list, dict);
11311 cur = cur->next;
11312 }
11313 }
11314#endif
11315}
11316
11317/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011318 * "getpid()" function
11319 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011320 static void
11321f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011322 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011323 typval_T *rettv;
11324{
11325 rettv->vval.v_number = mch_get_pid();
11326}
11327
11328/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011329 * "getpos(string)" function
11330 */
11331 static void
11332f_getpos(argvars, rettv)
11333 typval_T *argvars;
11334 typval_T *rettv;
11335{
11336 pos_T *fp;
11337 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011338 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011339
11340 if (rettv_list_alloc(rettv) == OK)
11341 {
11342 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011343 fp = var2fpos(&argvars[0], TRUE, &fnum);
11344 if (fnum != -1)
11345 list_append_number(l, (varnumber_T)fnum);
11346 else
11347 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011348 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11349 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011350 list_append_number(l, (fp != NULL)
11351 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011352 : (varnumber_T)0);
11353 list_append_number(l,
11354#ifdef FEAT_VIRTUALEDIT
11355 (fp != NULL) ? (varnumber_T)fp->coladd :
11356#endif
11357 (varnumber_T)0);
11358 }
11359 else
11360 rettv->vval.v_number = FALSE;
11361}
11362
11363/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011364 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011365 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011366 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011367f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011368 typval_T *argvars UNUSED;
11369 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011370{
11371#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011372 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011373#endif
11374
Bram Moolenaar2641f772005-03-25 21:58:17 +000011375#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011376 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011377 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011378 wp = NULL;
11379 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11380 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011381 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011382 if (wp == NULL)
11383 return;
11384 }
11385
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011386 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011387 }
11388#endif
11389}
11390
11391/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011392 * "getreg()" function
11393 */
11394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011396 typval_T *argvars;
11397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
11399 char_u *strregname;
11400 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011401 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011402 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011404 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011406 strregname = get_tv_string_chk(&argvars[0]);
11407 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011408 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011409 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011412 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 regname = (strregname == NULL ? '"' : *strregname);
11414 if (regname == 0)
11415 regname = '"';
11416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011418 rettv->vval.v_string = error ? NULL :
11419 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011420}
11421
11422/*
11423 * "getregtype()" function
11424 */
11425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011427 typval_T *argvars;
11428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429{
11430 char_u *strregname;
11431 int regname;
11432 char_u buf[NUMBUFLEN + 2];
11433 long reglen = 0;
11434
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011435 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 {
11437 strregname = get_tv_string_chk(&argvars[0]);
11438 if (strregname == NULL) /* type error; errmsg already given */
11439 {
11440 rettv->v_type = VAR_STRING;
11441 rettv->vval.v_string = NULL;
11442 return;
11443 }
11444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011445 else
11446 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011447 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448
11449 regname = (strregname == NULL ? '"' : *strregname);
11450 if (regname == 0)
11451 regname = '"';
11452
11453 buf[0] = NUL;
11454 buf[1] = NUL;
11455 switch (get_reg_type(regname, &reglen))
11456 {
11457 case MLINE: buf[0] = 'V'; break;
11458 case MCHAR: buf[0] = 'v'; break;
11459#ifdef FEAT_VISUAL
11460 case MBLOCK:
11461 buf[0] = Ctrl_V;
11462 sprintf((char *)buf + 1, "%ld", reglen + 1);
11463 break;
11464#endif
11465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011466 rettv->v_type = VAR_STRING;
11467 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468}
11469
11470/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011471 * "gettabvar()" function
11472 */
11473 static void
11474f_gettabvar(argvars, rettv)
11475 typval_T *argvars;
11476 typval_T *rettv;
11477{
11478 tabpage_T *tp;
11479 dictitem_T *v;
11480 char_u *varname;
11481
11482 rettv->v_type = VAR_STRING;
11483 rettv->vval.v_string = NULL;
11484
11485 varname = get_tv_string_chk(&argvars[1]);
11486 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11487 if (tp != NULL && varname != NULL)
11488 {
11489 /* look up the variable */
11490 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11491 if (v != NULL)
11492 copy_tv(&v->di_tv, rettv);
11493 }
11494}
11495
11496/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011497 * "gettabwinvar()" function
11498 */
11499 static void
11500f_gettabwinvar(argvars, rettv)
11501 typval_T *argvars;
11502 typval_T *rettv;
11503{
11504 getwinvar(argvars, rettv, 1);
11505}
11506
11507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508 * "getwinposx()" function
11509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011512 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011513 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011515 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011516#ifdef FEAT_GUI
11517 if (gui.in_use)
11518 {
11519 int x, y;
11520
11521 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 }
11524#endif
11525}
11526
11527/*
11528 * "getwinposy()" function
11529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011532 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011535 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011536#ifdef FEAT_GUI
11537 if (gui.in_use)
11538 {
11539 int x, y;
11540
11541 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011542 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011543 }
11544#endif
11545}
11546
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011547/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011548 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011549 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011550 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011551find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011552 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011553 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011554{
11555#ifdef FEAT_WINDOWS
11556 win_T *wp;
11557#endif
11558 int nr;
11559
11560 nr = get_tv_number_chk(vp, NULL);
11561
11562#ifdef FEAT_WINDOWS
11563 if (nr < 0)
11564 return NULL;
11565 if (nr == 0)
11566 return curwin;
11567
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011568 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11569 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011570 if (--nr <= 0)
11571 break;
11572 return wp;
11573#else
11574 if (nr == 0 || nr == 1)
11575 return curwin;
11576 return NULL;
11577#endif
11578}
11579
Bram Moolenaar071d4272004-06-13 20:20:40 +000011580/*
11581 * "getwinvar()" function
11582 */
11583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011585 typval_T *argvars;
11586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011587{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011588 getwinvar(argvars, rettv, 0);
11589}
11590
11591/*
11592 * getwinvar() and gettabwinvar()
11593 */
11594 static void
11595getwinvar(argvars, rettv, off)
11596 typval_T *argvars;
11597 typval_T *rettv;
11598 int off; /* 1 for gettabwinvar() */
11599{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 win_T *win, *oldcurwin;
11601 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011602 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011603 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011605#ifdef FEAT_WINDOWS
11606 if (off == 1)
11607 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11608 else
11609 tp = curtab;
11610#endif
11611 win = find_win_by_nr(&argvars[off], tp);
11612 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011613 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011615 rettv->v_type = VAR_STRING;
11616 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617
11618 if (win != NULL && varname != NULL)
11619 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011620 /* Set curwin to be our win, temporarily. Also set curbuf, so
11621 * that we can get buffer-local options. */
11622 oldcurwin = curwin;
11623 curwin = win;
11624 curbuf = win->w_buffer;
11625
Bram Moolenaar071d4272004-06-13 20:20:40 +000011626 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011627 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011628 else
11629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011630 if (*varname == NUL)
11631 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11632 * scope prefix before the NUL byte is required by
11633 * find_var_in_ht(). */
11634 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011636 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011638 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011640
11641 /* restore previous notion of curwin */
11642 curwin = oldcurwin;
11643 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 }
11645
11646 --emsg_off;
11647}
11648
11649/*
11650 * "glob()" function
11651 */
11652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011654 typval_T *argvars;
11655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011657 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011658 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011659 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011660
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011661 /* When the optional second argument is non-zero, don't remove matches
11662 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11663 if (argvars[1].v_type != VAR_UNKNOWN
11664 && get_tv_number_chk(&argvars[1], &error))
11665 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011667 if (!error)
11668 {
11669 ExpandInit(&xpc);
11670 xpc.xp_context = EXPAND_FILES;
11671 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11672 NULL, flags, WILD_ALL);
11673 }
11674 else
11675 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676}
11677
11678/*
11679 * "globpath()" function
11680 */
11681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011682f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011683 typval_T *argvars;
11684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011686 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011687 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011688 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011689 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011690
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011691 /* When the optional second argument is non-zero, don't remove matches
11692 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11693 if (argvars[2].v_type != VAR_UNKNOWN
11694 && get_tv_number_chk(&argvars[2], &error))
11695 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011697 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011698 rettv->vval.v_string = NULL;
11699 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011700 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11701 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702}
11703
11704/*
11705 * "has()" function
11706 */
11707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011709 typval_T *argvars;
11710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711{
11712 int i;
11713 char_u *name;
11714 int n = FALSE;
11715 static char *(has_list[]) =
11716 {
11717#ifdef AMIGA
11718 "amiga",
11719# ifdef FEAT_ARP
11720 "arp",
11721# endif
11722#endif
11723#ifdef __BEOS__
11724 "beos",
11725#endif
11726#ifdef MSDOS
11727# ifdef DJGPP
11728 "dos32",
11729# else
11730 "dos16",
11731# endif
11732#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011733#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 "mac",
11735#endif
11736#if defined(MACOS_X_UNIX)
11737 "macunix",
11738#endif
11739#ifdef OS2
11740 "os2",
11741#endif
11742#ifdef __QNX__
11743 "qnx",
11744#endif
11745#ifdef RISCOS
11746 "riscos",
11747#endif
11748#ifdef UNIX
11749 "unix",
11750#endif
11751#ifdef VMS
11752 "vms",
11753#endif
11754#ifdef WIN16
11755 "win16",
11756#endif
11757#ifdef WIN32
11758 "win32",
11759#endif
11760#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11761 "win32unix",
11762#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011763#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 "win64",
11765#endif
11766#ifdef EBCDIC
11767 "ebcdic",
11768#endif
11769#ifndef CASE_INSENSITIVE_FILENAME
11770 "fname_case",
11771#endif
11772#ifdef FEAT_ARABIC
11773 "arabic",
11774#endif
11775#ifdef FEAT_AUTOCMD
11776 "autocmd",
11777#endif
11778#ifdef FEAT_BEVAL
11779 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011780# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11781 "balloon_multiline",
11782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783#endif
11784#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11785 "builtin_terms",
11786# ifdef ALL_BUILTIN_TCAPS
11787 "all_builtin_terms",
11788# endif
11789#endif
11790#ifdef FEAT_BYTEOFF
11791 "byte_offset",
11792#endif
11793#ifdef FEAT_CINDENT
11794 "cindent",
11795#endif
11796#ifdef FEAT_CLIENTSERVER
11797 "clientserver",
11798#endif
11799#ifdef FEAT_CLIPBOARD
11800 "clipboard",
11801#endif
11802#ifdef FEAT_CMDL_COMPL
11803 "cmdline_compl",
11804#endif
11805#ifdef FEAT_CMDHIST
11806 "cmdline_hist",
11807#endif
11808#ifdef FEAT_COMMENTS
11809 "comments",
11810#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011811#ifdef FEAT_CONCEAL
11812 "conceal",
11813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814#ifdef FEAT_CRYPT
11815 "cryptv",
11816#endif
11817#ifdef FEAT_CSCOPE
11818 "cscope",
11819#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011820#ifdef FEAT_CURSORBIND
11821 "cursorbind",
11822#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011823#ifdef CURSOR_SHAPE
11824 "cursorshape",
11825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826#ifdef DEBUG
11827 "debug",
11828#endif
11829#ifdef FEAT_CON_DIALOG
11830 "dialog_con",
11831#endif
11832#ifdef FEAT_GUI_DIALOG
11833 "dialog_gui",
11834#endif
11835#ifdef FEAT_DIFF
11836 "diff",
11837#endif
11838#ifdef FEAT_DIGRAPHS
11839 "digraphs",
11840#endif
11841#ifdef FEAT_DND
11842 "dnd",
11843#endif
11844#ifdef FEAT_EMACS_TAGS
11845 "emacs_tags",
11846#endif
11847 "eval", /* always present, of course! */
11848#ifdef FEAT_EX_EXTRA
11849 "ex_extra",
11850#endif
11851#ifdef FEAT_SEARCH_EXTRA
11852 "extra_search",
11853#endif
11854#ifdef FEAT_FKMAP
11855 "farsi",
11856#endif
11857#ifdef FEAT_SEARCHPATH
11858 "file_in_path",
11859#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011860#if defined(UNIX) && !defined(USE_SYSTEM)
11861 "filterpipe",
11862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011863#ifdef FEAT_FIND_ID
11864 "find_in_path",
11865#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011866#ifdef FEAT_FLOAT
11867 "float",
11868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011869#ifdef FEAT_FOLDING
11870 "folding",
11871#endif
11872#ifdef FEAT_FOOTER
11873 "footer",
11874#endif
11875#if !defined(USE_SYSTEM) && defined(UNIX)
11876 "fork",
11877#endif
11878#ifdef FEAT_GETTEXT
11879 "gettext",
11880#endif
11881#ifdef FEAT_GUI
11882 "gui",
11883#endif
11884#ifdef FEAT_GUI_ATHENA
11885# ifdef FEAT_GUI_NEXTAW
11886 "gui_neXtaw",
11887# else
11888 "gui_athena",
11889# endif
11890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891#ifdef FEAT_GUI_GTK
11892 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011893 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011895#ifdef FEAT_GUI_GNOME
11896 "gui_gnome",
11897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011898#ifdef FEAT_GUI_MAC
11899 "gui_mac",
11900#endif
11901#ifdef FEAT_GUI_MOTIF
11902 "gui_motif",
11903#endif
11904#ifdef FEAT_GUI_PHOTON
11905 "gui_photon",
11906#endif
11907#ifdef FEAT_GUI_W16
11908 "gui_win16",
11909#endif
11910#ifdef FEAT_GUI_W32
11911 "gui_win32",
11912#endif
11913#ifdef FEAT_HANGULIN
11914 "hangul_input",
11915#endif
11916#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11917 "iconv",
11918#endif
11919#ifdef FEAT_INS_EXPAND
11920 "insert_expand",
11921#endif
11922#ifdef FEAT_JUMPLIST
11923 "jumplist",
11924#endif
11925#ifdef FEAT_KEYMAP
11926 "keymap",
11927#endif
11928#ifdef FEAT_LANGMAP
11929 "langmap",
11930#endif
11931#ifdef FEAT_LIBCALL
11932 "libcall",
11933#endif
11934#ifdef FEAT_LINEBREAK
11935 "linebreak",
11936#endif
11937#ifdef FEAT_LISP
11938 "lispindent",
11939#endif
11940#ifdef FEAT_LISTCMDS
11941 "listcmds",
11942#endif
11943#ifdef FEAT_LOCALMAP
11944 "localmap",
11945#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011946#ifdef FEAT_LUA
11947# ifndef DYNAMIC_LUA
11948 "lua",
11949# endif
11950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011951#ifdef FEAT_MENU
11952 "menu",
11953#endif
11954#ifdef FEAT_SESSION
11955 "mksession",
11956#endif
11957#ifdef FEAT_MODIFY_FNAME
11958 "modify_fname",
11959#endif
11960#ifdef FEAT_MOUSE
11961 "mouse",
11962#endif
11963#ifdef FEAT_MOUSESHAPE
11964 "mouseshape",
11965#endif
11966#if defined(UNIX) || defined(VMS)
11967# ifdef FEAT_MOUSE_DEC
11968 "mouse_dec",
11969# endif
11970# ifdef FEAT_MOUSE_GPM
11971 "mouse_gpm",
11972# endif
11973# ifdef FEAT_MOUSE_JSB
11974 "mouse_jsbterm",
11975# endif
11976# ifdef FEAT_MOUSE_NET
11977 "mouse_netterm",
11978# endif
11979# ifdef FEAT_MOUSE_PTERM
11980 "mouse_pterm",
11981# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011982# ifdef FEAT_SYSMOUSE
11983 "mouse_sysmouse",
11984# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985# ifdef FEAT_MOUSE_XTERM
11986 "mouse_xterm",
11987# endif
11988#endif
11989#ifdef FEAT_MBYTE
11990 "multi_byte",
11991#endif
11992#ifdef FEAT_MBYTE_IME
11993 "multi_byte_ime",
11994#endif
11995#ifdef FEAT_MULTI_LANG
11996 "multi_lang",
11997#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000011998#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000011999#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012000 "mzscheme",
12001#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003#ifdef FEAT_OLE
12004 "ole",
12005#endif
12006#ifdef FEAT_OSFILETYPE
12007 "osfiletype",
12008#endif
12009#ifdef FEAT_PATH_EXTRA
12010 "path_extra",
12011#endif
12012#ifdef FEAT_PERL
12013#ifndef DYNAMIC_PERL
12014 "perl",
12015#endif
12016#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012017#ifdef FEAT_PERSISTENT_UNDO
12018 "persistent_undo",
12019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020#ifdef FEAT_PYTHON
12021#ifndef DYNAMIC_PYTHON
12022 "python",
12023#endif
12024#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012025#ifdef FEAT_PYTHON3
12026#ifndef DYNAMIC_PYTHON3
12027 "python3",
12028#endif
12029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030#ifdef FEAT_POSTSCRIPT
12031 "postscript",
12032#endif
12033#ifdef FEAT_PRINTER
12034 "printer",
12035#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012036#ifdef FEAT_PROFILE
12037 "profile",
12038#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012039#ifdef FEAT_RELTIME
12040 "reltime",
12041#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012042#ifdef FEAT_QUICKFIX
12043 "quickfix",
12044#endif
12045#ifdef FEAT_RIGHTLEFT
12046 "rightleft",
12047#endif
12048#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12049 "ruby",
12050#endif
12051#ifdef FEAT_SCROLLBIND
12052 "scrollbind",
12053#endif
12054#ifdef FEAT_CMDL_INFO
12055 "showcmd",
12056 "cmdline_info",
12057#endif
12058#ifdef FEAT_SIGNS
12059 "signs",
12060#endif
12061#ifdef FEAT_SMARTINDENT
12062 "smartindent",
12063#endif
12064#ifdef FEAT_SNIFF
12065 "sniff",
12066#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012067#ifdef STARTUPTIME
12068 "startuptime",
12069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070#ifdef FEAT_STL_OPT
12071 "statusline",
12072#endif
12073#ifdef FEAT_SUN_WORKSHOP
12074 "sun_workshop",
12075#endif
12076#ifdef FEAT_NETBEANS_INTG
12077 "netbeans_intg",
12078#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012079#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012080 "spell",
12081#endif
12082#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 "syntax",
12084#endif
12085#if defined(USE_SYSTEM) || !defined(UNIX)
12086 "system",
12087#endif
12088#ifdef FEAT_TAG_BINS
12089 "tag_binary",
12090#endif
12091#ifdef FEAT_TAG_OLDSTATIC
12092 "tag_old_static",
12093#endif
12094#ifdef FEAT_TAG_ANYWHITE
12095 "tag_any_white",
12096#endif
12097#ifdef FEAT_TCL
12098# ifndef DYNAMIC_TCL
12099 "tcl",
12100# endif
12101#endif
12102#ifdef TERMINFO
12103 "terminfo",
12104#endif
12105#ifdef FEAT_TERMRESPONSE
12106 "termresponse",
12107#endif
12108#ifdef FEAT_TEXTOBJ
12109 "textobjects",
12110#endif
12111#ifdef HAVE_TGETENT
12112 "tgetent",
12113#endif
12114#ifdef FEAT_TITLE
12115 "title",
12116#endif
12117#ifdef FEAT_TOOLBAR
12118 "toolbar",
12119#endif
12120#ifdef FEAT_USR_CMDS
12121 "user-commands", /* was accidentally included in 5.4 */
12122 "user_commands",
12123#endif
12124#ifdef FEAT_VIMINFO
12125 "viminfo",
12126#endif
12127#ifdef FEAT_VERTSPLIT
12128 "vertsplit",
12129#endif
12130#ifdef FEAT_VIRTUALEDIT
12131 "virtualedit",
12132#endif
12133#ifdef FEAT_VISUAL
12134 "visual",
12135#endif
12136#ifdef FEAT_VISUALEXTRA
12137 "visualextra",
12138#endif
12139#ifdef FEAT_VREPLACE
12140 "vreplace",
12141#endif
12142#ifdef FEAT_WILDIGN
12143 "wildignore",
12144#endif
12145#ifdef FEAT_WILDMENU
12146 "wildmenu",
12147#endif
12148#ifdef FEAT_WINDOWS
12149 "windows",
12150#endif
12151#ifdef FEAT_WAK
12152 "winaltkeys",
12153#endif
12154#ifdef FEAT_WRITEBACKUP
12155 "writebackup",
12156#endif
12157#ifdef FEAT_XIM
12158 "xim",
12159#endif
12160#ifdef FEAT_XFONTSET
12161 "xfontset",
12162#endif
12163#ifdef USE_XSMP
12164 "xsmp",
12165#endif
12166#ifdef USE_XSMP_INTERACT
12167 "xsmp_interact",
12168#endif
12169#ifdef FEAT_XCLIPBOARD
12170 "xterm_clipboard",
12171#endif
12172#ifdef FEAT_XTERM_SAVE
12173 "xterm_save",
12174#endif
12175#if defined(UNIX) && defined(FEAT_X11)
12176 "X11",
12177#endif
12178 NULL
12179 };
12180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012181 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 for (i = 0; has_list[i] != NULL; ++i)
12183 if (STRICMP(name, has_list[i]) == 0)
12184 {
12185 n = TRUE;
12186 break;
12187 }
12188
12189 if (n == FALSE)
12190 {
12191 if (STRNICMP(name, "patch", 5) == 0)
12192 n = has_patch(atoi((char *)name + 5));
12193 else if (STRICMP(name, "vim_starting") == 0)
12194 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012195#ifdef FEAT_MBYTE
12196 else if (STRICMP(name, "multi_byte_encoding") == 0)
12197 n = has_mbyte;
12198#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012199#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12200 else if (STRICMP(name, "balloon_multiline") == 0)
12201 n = multiline_balloon_available();
12202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203#ifdef DYNAMIC_TCL
12204 else if (STRICMP(name, "tcl") == 0)
12205 n = tcl_enabled(FALSE);
12206#endif
12207#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12208 else if (STRICMP(name, "iconv") == 0)
12209 n = iconv_enabled(FALSE);
12210#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012211#ifdef DYNAMIC_LUA
12212 else if (STRICMP(name, "lua") == 0)
12213 n = lua_enabled(FALSE);
12214#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012215#ifdef DYNAMIC_MZSCHEME
12216 else if (STRICMP(name, "mzscheme") == 0)
12217 n = mzscheme_enabled(FALSE);
12218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219#ifdef DYNAMIC_RUBY
12220 else if (STRICMP(name, "ruby") == 0)
12221 n = ruby_enabled(FALSE);
12222#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012223#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224#ifdef DYNAMIC_PYTHON
12225 else if (STRICMP(name, "python") == 0)
12226 n = python_enabled(FALSE);
12227#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012228#endif
12229#ifdef FEAT_PYTHON3
12230#ifdef DYNAMIC_PYTHON3
12231 else if (STRICMP(name, "python3") == 0)
12232 n = python3_enabled(FALSE);
12233#endif
12234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235#ifdef DYNAMIC_PERL
12236 else if (STRICMP(name, "perl") == 0)
12237 n = perl_enabled(FALSE);
12238#endif
12239#ifdef FEAT_GUI
12240 else if (STRICMP(name, "gui_running") == 0)
12241 n = (gui.in_use || gui.starting);
12242# ifdef FEAT_GUI_W32
12243 else if (STRICMP(name, "gui_win32s") == 0)
12244 n = gui_is_win32s();
12245# endif
12246# ifdef FEAT_BROWSE
12247 else if (STRICMP(name, "browse") == 0)
12248 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12249# endif
12250#endif
12251#ifdef FEAT_SYN_HL
12252 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012253 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254#endif
12255#if defined(WIN3264)
12256 else if (STRICMP(name, "win95") == 0)
12257 n = mch_windows95();
12258#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012259#ifdef FEAT_NETBEANS_INTG
12260 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012261 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 }
12264
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012265 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266}
12267
12268/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012269 * "has_key()" function
12270 */
12271 static void
12272f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012273 typval_T *argvars;
12274 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012275{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012276 if (argvars[0].v_type != VAR_DICT)
12277 {
12278 EMSG(_(e_dictreq));
12279 return;
12280 }
12281 if (argvars[0].vval.v_dict == NULL)
12282 return;
12283
12284 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012285 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012286}
12287
12288/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012289 * "haslocaldir()" function
12290 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012291 static void
12292f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012293 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012294 typval_T *rettv;
12295{
12296 rettv->vval.v_number = (curwin->w_localdir != NULL);
12297}
12298
12299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012300 * "hasmapto()" function
12301 */
12302 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012303f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012304 typval_T *argvars;
12305 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306{
12307 char_u *name;
12308 char_u *mode;
12309 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012310 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012312 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012313 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314 mode = (char_u *)"nvo";
12315 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012318 if (argvars[2].v_type != VAR_UNKNOWN)
12319 abbr = get_tv_number(&argvars[2]);
12320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321
Bram Moolenaar2c932302006-03-18 21:42:09 +000012322 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012323 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012325 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326}
12327
12328/*
12329 * "histadd()" function
12330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012332f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012333 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335{
12336#ifdef FEAT_CMDHIST
12337 int histype;
12338 char_u *str;
12339 char_u buf[NUMBUFLEN];
12340#endif
12341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012342 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012343 if (check_restricted() || check_secure())
12344 return;
12345#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012346 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12347 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 if (histype >= 0)
12349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012350 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351 if (*str != NUL)
12352 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012353 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012355 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 return;
12357 }
12358 }
12359#endif
12360}
12361
12362/*
12363 * "histdel()" function
12364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012367 typval_T *argvars UNUSED;
12368 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369{
12370#ifdef FEAT_CMDHIST
12371 int n;
12372 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012373 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012375 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12376 if (str == NULL)
12377 n = 0;
12378 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012380 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012381 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012382 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012383 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 else
12386 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012387 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012388 get_tv_string_buf(&argvars[1], buf));
12389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390#endif
12391}
12392
12393/*
12394 * "histget()" function
12395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012397f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012398 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400{
12401#ifdef FEAT_CMDHIST
12402 int type;
12403 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012404 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012406 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12407 if (str == NULL)
12408 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012409 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 {
12411 type = get_histtype(str);
12412 if (argvars[1].v_type == VAR_UNKNOWN)
12413 idx = get_history_idx(type);
12414 else
12415 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12416 /* -1 on type error */
12417 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012420 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012422 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423}
12424
12425/*
12426 * "histnr()" function
12427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012429f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012430 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432{
12433 int i;
12434
12435#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012436 char_u *history = get_tv_string_chk(&argvars[0]);
12437
12438 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439 if (i >= HIST_CMD && i < HIST_COUNT)
12440 i = get_history_idx(i);
12441 else
12442#endif
12443 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012444 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012445}
12446
12447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448 * "highlightID(name)" function
12449 */
12450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012451f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012452 typval_T *argvars;
12453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012454{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012455 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012456}
12457
12458/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012459 * "highlight_exists()" function
12460 */
12461 static void
12462f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012463 typval_T *argvars;
12464 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012465{
12466 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12467}
12468
12469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012470 * "hostname()" function
12471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012473f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012474 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476{
12477 char_u hostname[256];
12478
12479 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480 rettv->v_type = VAR_STRING;
12481 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482}
12483
12484/*
12485 * iconv() function
12486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012488f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012489 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491{
12492#ifdef FEAT_MBYTE
12493 char_u buf1[NUMBUFLEN];
12494 char_u buf2[NUMBUFLEN];
12495 char_u *from, *to, *str;
12496 vimconv_T vimconv;
12497#endif
12498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012499 rettv->v_type = VAR_STRING;
12500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501
12502#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012503 str = get_tv_string(&argvars[0]);
12504 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12505 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012506 vimconv.vc_type = CONV_NONE;
12507 convert_setup(&vimconv, from, to);
12508
12509 /* If the encodings are equal, no conversion needed. */
12510 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012511 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514
12515 convert_setup(&vimconv, NULL, NULL);
12516 vim_free(from);
12517 vim_free(to);
12518#endif
12519}
12520
12521/*
12522 * "indent()" function
12523 */
12524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012525f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012526 typval_T *argvars;
12527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012528{
12529 linenr_T lnum;
12530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012533 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012535 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536}
12537
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012538/*
12539 * "index()" function
12540 */
12541 static void
12542f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012543 typval_T *argvars;
12544 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012545{
Bram Moolenaar33570922005-01-25 22:26:29 +000012546 list_T *l;
12547 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012548 long idx = 0;
12549 int ic = FALSE;
12550
12551 rettv->vval.v_number = -1;
12552 if (argvars[0].v_type != VAR_LIST)
12553 {
12554 EMSG(_(e_listreq));
12555 return;
12556 }
12557 l = argvars[0].vval.v_list;
12558 if (l != NULL)
12559 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012560 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012561 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012562 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012563 int error = FALSE;
12564
Bram Moolenaar758711c2005-02-02 23:11:38 +000012565 /* Start at specified item. Use the cached index that list_find()
12566 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012567 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012568 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012569 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012570 ic = get_tv_number_chk(&argvars[3], &error);
12571 if (error)
12572 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012573 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012574
Bram Moolenaar758711c2005-02-02 23:11:38 +000012575 for ( ; item != NULL; item = item->li_next, ++idx)
12576 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012577 {
12578 rettv->vval.v_number = idx;
12579 break;
12580 }
12581 }
12582}
12583
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584static int inputsecret_flag = 0;
12585
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012586static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12587
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012589 * This function is used by f_input() and f_inputdialog() functions. The third
12590 * argument to f_input() specifies the type of completion to use at the
12591 * prompt. The third argument to f_inputdialog() specifies the value to return
12592 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012593 */
12594 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012595get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012596 typval_T *argvars;
12597 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012598 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012600 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601 char_u *p = NULL;
12602 int c;
12603 char_u buf[NUMBUFLEN];
12604 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012605 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012606 int xp_type = EXPAND_NOTHING;
12607 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012610 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611
12612#ifdef NO_CONSOLE_INPUT
12613 /* While starting up, there is no place to enter text. */
12614 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616#endif
12617
12618 cmd_silent = FALSE; /* Want to see the prompt. */
12619 if (prompt != NULL)
12620 {
12621 /* Only the part of the message after the last NL is considered as
12622 * prompt for the command line */
12623 p = vim_strrchr(prompt, '\n');
12624 if (p == NULL)
12625 p = prompt;
12626 else
12627 {
12628 ++p;
12629 c = *p;
12630 *p = NUL;
12631 msg_start();
12632 msg_clr_eos();
12633 msg_puts_attr(prompt, echo_attr);
12634 msg_didout = FALSE;
12635 msg_starthere();
12636 *p = c;
12637 }
12638 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012640 if (argvars[1].v_type != VAR_UNKNOWN)
12641 {
12642 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12643 if (defstr != NULL)
12644 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012646 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012647 {
12648 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012649 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012650 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012651
Bram Moolenaar4463f292005-09-25 22:20:24 +000012652 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012653
Bram Moolenaar4463f292005-09-25 22:20:24 +000012654 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12655 if (xp_name == NULL)
12656 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012657
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012658 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012659
Bram Moolenaar4463f292005-09-25 22:20:24 +000012660 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12661 &xp_arg) == FAIL)
12662 return;
12663 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012664 }
12665
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012666 if (defstr != NULL)
12667 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012668 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12669 xp_type, xp_arg);
12670
12671 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012673 /* since the user typed this, no need to wait for return */
12674 need_wait_return = FALSE;
12675 msg_didout = FALSE;
12676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012677 cmd_silent = cmd_silent_save;
12678}
12679
12680/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012681 * "input()" function
12682 * Also handles inputsecret() when inputsecret is set.
12683 */
12684 static void
12685f_input(argvars, rettv)
12686 typval_T *argvars;
12687 typval_T *rettv;
12688{
12689 get_user_input(argvars, rettv, FALSE);
12690}
12691
12692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693 * "inputdialog()" function
12694 */
12695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012696f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012697 typval_T *argvars;
12698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699{
12700#if defined(FEAT_GUI_TEXTDIALOG)
12701 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12702 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12703 {
12704 char_u *message;
12705 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012706 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012708 message = get_tv_string_chk(&argvars[0]);
12709 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012710 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012711 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 else
12713 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012714 if (message != NULL && defstr != NULL
12715 && do_dialog(VIM_QUESTION, NULL, message,
12716 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 else
12719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012720 if (message != NULL && defstr != NULL
12721 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012722 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 rettv->vval.v_string = vim_strsave(
12724 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012726 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012728 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 }
12730 else
12731#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012732 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733}
12734
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012735/*
12736 * "inputlist()" function
12737 */
12738 static void
12739f_inputlist(argvars, rettv)
12740 typval_T *argvars;
12741 typval_T *rettv;
12742{
12743 listitem_T *li;
12744 int selected;
12745 int mouse_used;
12746
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012747#ifdef NO_CONSOLE_INPUT
12748 /* While starting up, there is no place to enter text. */
12749 if (no_console_input())
12750 return;
12751#endif
12752 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12753 {
12754 EMSG2(_(e_listarg), "inputlist()");
12755 return;
12756 }
12757
12758 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012759 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012760 lines_left = Rows; /* avoid more prompt */
12761 msg_scroll = TRUE;
12762 msg_clr_eos();
12763
12764 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12765 {
12766 msg_puts(get_tv_string(&li->li_tv));
12767 msg_putchar('\n');
12768 }
12769
12770 /* Ask for choice. */
12771 selected = prompt_for_number(&mouse_used);
12772 if (mouse_used)
12773 selected -= lines_left;
12774
12775 rettv->vval.v_number = selected;
12776}
12777
12778
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12780
12781/*
12782 * "inputrestore()" function
12783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012786 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788{
12789 if (ga_userinput.ga_len > 0)
12790 {
12791 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12793 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012794 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795 }
12796 else if (p_verbose > 1)
12797 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012798 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 }
12801}
12802
12803/*
12804 * "inputsave()" function
12805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012808 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012809 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012811 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 if (ga_grow(&ga_userinput, 1) == OK)
12813 {
12814 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12815 + ga_userinput.ga_len);
12816 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012817 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 }
12819 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012820 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821}
12822
12823/*
12824 * "inputsecret()" function
12825 */
12826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012828 typval_T *argvars;
12829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830{
12831 ++cmdline_star;
12832 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 --cmdline_star;
12835 --inputsecret_flag;
12836}
12837
12838/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012839 * "insert()" function
12840 */
12841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012843 typval_T *argvars;
12844 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012845{
12846 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012847 listitem_T *item;
12848 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012849 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012850
12851 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012852 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012853 else if ((l = argvars[0].vval.v_list) != NULL
12854 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012855 {
12856 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012857 before = get_tv_number_chk(&argvars[2], &error);
12858 if (error)
12859 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012860
Bram Moolenaar758711c2005-02-02 23:11:38 +000012861 if (before == l->lv_len)
12862 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012863 else
12864 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012865 item = list_find(l, before);
12866 if (item == NULL)
12867 {
12868 EMSGN(_(e_listidx), before);
12869 l = NULL;
12870 }
12871 }
12872 if (l != NULL)
12873 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012874 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012875 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012876 }
12877 }
12878}
12879
12880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881 * "isdirectory()" function
12882 */
12883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012885 typval_T *argvars;
12886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889}
12890
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012891/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012892 * "islocked()" function
12893 */
12894 static void
12895f_islocked(argvars, rettv)
12896 typval_T *argvars;
12897 typval_T *rettv;
12898{
12899 lval_T lv;
12900 char_u *end;
12901 dictitem_T *di;
12902
12903 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012904 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12905 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012906 if (end != NULL && lv.ll_name != NULL)
12907 {
12908 if (*end != NUL)
12909 EMSG(_(e_trailing));
12910 else
12911 {
12912 if (lv.ll_tv == NULL)
12913 {
12914 if (check_changedtick(lv.ll_name))
12915 rettv->vval.v_number = 1; /* always locked */
12916 else
12917 {
12918 di = find_var(lv.ll_name, NULL);
12919 if (di != NULL)
12920 {
12921 /* Consider a variable locked when:
12922 * 1. the variable itself is locked
12923 * 2. the value of the variable is locked.
12924 * 3. the List or Dict value is locked.
12925 */
12926 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12927 || tv_islocked(&di->di_tv));
12928 }
12929 }
12930 }
12931 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012932 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012933 else if (lv.ll_newkey != NULL)
12934 EMSG2(_(e_dictkey), lv.ll_newkey);
12935 else if (lv.ll_list != NULL)
12936 /* List item. */
12937 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12938 else
12939 /* Dictionary item. */
12940 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12941 }
12942 }
12943
12944 clear_lval(&lv);
12945}
12946
Bram Moolenaar33570922005-01-25 22:26:29 +000012947static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012948
12949/*
12950 * Turn a dict into a list:
12951 * "what" == 0: list of keys
12952 * "what" == 1: list of values
12953 * "what" == 2: list of items
12954 */
12955 static void
12956dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012957 typval_T *argvars;
12958 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012959 int what;
12960{
Bram Moolenaar33570922005-01-25 22:26:29 +000012961 list_T *l2;
12962 dictitem_T *di;
12963 hashitem_T *hi;
12964 listitem_T *li;
12965 listitem_T *li2;
12966 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012967 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012968
Bram Moolenaar8c711452005-01-14 21:53:12 +000012969 if (argvars[0].v_type != VAR_DICT)
12970 {
12971 EMSG(_(e_dictreq));
12972 return;
12973 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012974 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012975 return;
12976
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012977 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012978 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012979
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012980 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012981 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012982 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012983 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012984 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012985 --todo;
12986 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012987
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012988 li = listitem_alloc();
12989 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012990 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012991 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012992
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012993 if (what == 0)
12994 {
12995 /* keys() */
12996 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012997 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012998 li->li_tv.vval.v_string = vim_strsave(di->di_key);
12999 }
13000 else if (what == 1)
13001 {
13002 /* values() */
13003 copy_tv(&di->di_tv, &li->li_tv);
13004 }
13005 else
13006 {
13007 /* items() */
13008 l2 = list_alloc();
13009 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013010 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013011 li->li_tv.vval.v_list = l2;
13012 if (l2 == NULL)
13013 break;
13014 ++l2->lv_refcount;
13015
13016 li2 = listitem_alloc();
13017 if (li2 == NULL)
13018 break;
13019 list_append(l2, li2);
13020 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013021 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013022 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13023
13024 li2 = listitem_alloc();
13025 if (li2 == NULL)
13026 break;
13027 list_append(l2, li2);
13028 copy_tv(&di->di_tv, &li2->li_tv);
13029 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013030 }
13031 }
13032}
13033
13034/*
13035 * "items(dict)" function
13036 */
13037 static void
13038f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013039 typval_T *argvars;
13040 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013041{
13042 dict_list(argvars, rettv, 2);
13043}
13044
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013046 * "join()" function
13047 */
13048 static void
13049f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013050 typval_T *argvars;
13051 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013052{
13053 garray_T ga;
13054 char_u *sep;
13055
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013056 if (argvars[0].v_type != VAR_LIST)
13057 {
13058 EMSG(_(e_listreq));
13059 return;
13060 }
13061 if (argvars[0].vval.v_list == NULL)
13062 return;
13063 if (argvars[1].v_type == VAR_UNKNOWN)
13064 sep = (char_u *)" ";
13065 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013066 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013067
13068 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013069
13070 if (sep != NULL)
13071 {
13072 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013073 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013074 ga_append(&ga, NUL);
13075 rettv->vval.v_string = (char_u *)ga.ga_data;
13076 }
13077 else
13078 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013079}
13080
13081/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013082 * "keys()" function
13083 */
13084 static void
13085f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013086 typval_T *argvars;
13087 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013088{
13089 dict_list(argvars, rettv, 0);
13090}
13091
13092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093 * "last_buffer_nr()" function.
13094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013096f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013097 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013098 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099{
13100 int n = 0;
13101 buf_T *buf;
13102
13103 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13104 if (n < buf->b_fnum)
13105 n = buf->b_fnum;
13106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013107 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013108}
13109
13110/*
13111 * "len()" function
13112 */
13113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013114f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013115 typval_T *argvars;
13116 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013117{
13118 switch (argvars[0].v_type)
13119 {
13120 case VAR_STRING:
13121 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013122 rettv->vval.v_number = (varnumber_T)STRLEN(
13123 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013124 break;
13125 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013127 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013128 case VAR_DICT:
13129 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13130 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013131 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013132 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013133 break;
13134 }
13135}
13136
Bram Moolenaar33570922005-01-25 22:26:29 +000013137static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013138
13139 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013141 typval_T *argvars;
13142 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013143 int type;
13144{
13145#ifdef FEAT_LIBCALL
13146 char_u *string_in;
13147 char_u **string_result;
13148 int nr_result;
13149#endif
13150
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013151 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013152 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013154
13155 if (check_restricted() || check_secure())
13156 return;
13157
13158#ifdef FEAT_LIBCALL
13159 /* The first two args must be strings, otherwise its meaningless */
13160 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13161 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013162 string_in = NULL;
13163 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013164 string_in = argvars[2].vval.v_string;
13165 if (type == VAR_NUMBER)
13166 string_result = NULL;
13167 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013169 if (mch_libcall(argvars[0].vval.v_string,
13170 argvars[1].vval.v_string,
13171 string_in,
13172 argvars[2].vval.v_number,
13173 string_result,
13174 &nr_result) == OK
13175 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013177 }
13178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179}
13180
13181/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013182 * "libcall()" function
13183 */
13184 static void
13185f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013186 typval_T *argvars;
13187 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013188{
13189 libcall_common(argvars, rettv, VAR_STRING);
13190}
13191
13192/*
13193 * "libcallnr()" function
13194 */
13195 static void
13196f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013197 typval_T *argvars;
13198 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013199{
13200 libcall_common(argvars, rettv, VAR_NUMBER);
13201}
13202
13203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 * "line(string)" function
13205 */
13206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013207f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013208 typval_T *argvars;
13209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210{
13211 linenr_T lnum = 0;
13212 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013213 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013215 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 if (fp != NULL)
13217 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013218 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219}
13220
13221/*
13222 * "line2byte(lnum)" function
13223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013226 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228{
13229#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013230 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231#else
13232 linenr_T lnum;
13233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013234 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013238 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13239 if (rettv->vval.v_number >= 0)
13240 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241#endif
13242}
13243
13244/*
13245 * "lispindent(lnum)" function
13246 */
13247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013249 typval_T *argvars;
13250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251{
13252#ifdef FEAT_LISP
13253 pos_T pos;
13254 linenr_T lnum;
13255
13256 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13259 {
13260 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013261 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 curwin->w_cursor = pos;
13263 }
13264 else
13265#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013266 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267}
13268
13269/*
13270 * "localtime()" function
13271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013274 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278}
13279
Bram Moolenaar33570922005-01-25 22:26:29 +000013280static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281
13282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013283get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013284 typval_T *argvars;
13285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 int exact;
13287{
13288 char_u *keys;
13289 char_u *which;
13290 char_u buf[NUMBUFLEN];
13291 char_u *keys_buf = NULL;
13292 char_u *rhs;
13293 int mode;
13294 garray_T ga;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013295 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296
13297 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013298 rettv->v_type = VAR_STRING;
13299 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013301 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 if (*keys == NUL)
13303 return;
13304
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013305 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013306 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013307 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013308 if (argvars[2].v_type != VAR_UNKNOWN)
13309 abbr = get_tv_number(&argvars[2]);
13310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013311 else
13312 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013313 if (which == NULL)
13314 return;
13315
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 mode = get_map_mode(&which, 0);
13317
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013318 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013319 rhs = check_map(keys, mode, exact, FALSE, abbr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320 vim_free(keys_buf);
13321 if (rhs != NULL)
13322 {
13323 ga_init(&ga);
13324 ga.ga_itemsize = 1;
13325 ga.ga_growsize = 40;
13326
13327 while (*rhs != NUL)
13328 ga_concat(&ga, str2special(&rhs, FALSE));
13329
13330 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013331 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013332 }
13333}
13334
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013335#ifdef FEAT_FLOAT
13336/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013337 * "log()" function
13338 */
13339 static void
13340f_log(argvars, rettv)
13341 typval_T *argvars;
13342 typval_T *rettv;
13343{
13344 float_T f;
13345
13346 rettv->v_type = VAR_FLOAT;
13347 if (get_float_arg(argvars, &f) == OK)
13348 rettv->vval.v_float = log(f);
13349 else
13350 rettv->vval.v_float = 0.0;
13351}
13352
13353/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013354 * "log10()" function
13355 */
13356 static void
13357f_log10(argvars, rettv)
13358 typval_T *argvars;
13359 typval_T *rettv;
13360{
13361 float_T f;
13362
13363 rettv->v_type = VAR_FLOAT;
13364 if (get_float_arg(argvars, &f) == OK)
13365 rettv->vval.v_float = log10(f);
13366 else
13367 rettv->vval.v_float = 0.0;
13368}
13369#endif
13370
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013372 * "map()" function
13373 */
13374 static void
13375f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013376 typval_T *argvars;
13377 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013378{
13379 filter_map(argvars, rettv, TRUE);
13380}
13381
13382/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013383 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 */
13385 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013386f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 typval_T *argvars;
13388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391}
13392
13393/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013394 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 */
13396 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013397f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013401 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013402}
13403
Bram Moolenaar33570922005-01-25 22:26:29 +000013404static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405
13406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013407find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013408 typval_T *argvars;
13409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 int type;
13411{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013412 char_u *str = NULL;
13413 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414 char_u *pat;
13415 regmatch_T regmatch;
13416 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013417 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 char_u *save_cpo;
13419 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013420 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013421 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013422 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013423 list_T *l = NULL;
13424 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013425 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013426 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427
13428 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13429 save_cpo = p_cpo;
13430 p_cpo = (char_u *)"";
13431
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013432 rettv->vval.v_number = -1;
13433 if (type == 3)
13434 {
13435 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013436 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013437 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013438 }
13439 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 rettv->v_type = VAR_STRING;
13442 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013445 if (argvars[0].v_type == VAR_LIST)
13446 {
13447 if ((l = argvars[0].vval.v_list) == NULL)
13448 goto theend;
13449 li = l->lv_first;
13450 }
13451 else
13452 expr = str = get_tv_string(&argvars[0]);
13453
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013454 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13455 if (pat == NULL)
13456 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013457
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013458 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013460 int error = FALSE;
13461
13462 start = get_tv_number_chk(&argvars[2], &error);
13463 if (error)
13464 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013465 if (l != NULL)
13466 {
13467 li = list_find(l, start);
13468 if (li == NULL)
13469 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013470 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013471 }
13472 else
13473 {
13474 if (start < 0)
13475 start = 0;
13476 if (start > (long)STRLEN(str))
13477 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013478 /* When "count" argument is there ignore matches before "start",
13479 * otherwise skip part of the string. Differs when pattern is "^"
13480 * or "\<". */
13481 if (argvars[3].v_type != VAR_UNKNOWN)
13482 startcol = start;
13483 else
13484 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013485 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013486
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013487 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013488 nth = get_tv_number_chk(&argvars[3], &error);
13489 if (error)
13490 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 }
13492
13493 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13494 if (regmatch.regprog != NULL)
13495 {
13496 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013497
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013498 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013499 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013500 if (l != NULL)
13501 {
13502 if (li == NULL)
13503 {
13504 match = FALSE;
13505 break;
13506 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013507 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013508 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013509 if (str == NULL)
13510 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013511 }
13512
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013513 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013514
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013515 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013516 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013517 if (l == NULL && !match)
13518 break;
13519
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013520 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013521 if (l != NULL)
13522 {
13523 li = li->li_next;
13524 ++idx;
13525 }
13526 else
13527 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013528#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013529 startcol = (colnr_T)(regmatch.startp[0]
13530 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013531#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013532 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013533#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013534 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013535 }
13536
13537 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013539 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013540 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013541 int i;
13542
13543 /* return list with matched string and submatches */
13544 for (i = 0; i < NSUBEXP; ++i)
13545 {
13546 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013547 {
13548 if (list_append_string(rettv->vval.v_list,
13549 (char_u *)"", 0) == FAIL)
13550 break;
13551 }
13552 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013553 regmatch.startp[i],
13554 (int)(regmatch.endp[i] - regmatch.startp[i]))
13555 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013556 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013557 }
13558 }
13559 else if (type == 2)
13560 {
13561 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013562 if (l != NULL)
13563 copy_tv(&li->li_tv, rettv);
13564 else
13565 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013567 }
13568 else if (l != NULL)
13569 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570 else
13571 {
13572 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013574 (varnumber_T)(regmatch.startp[0] - str);
13575 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013578 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 }
13580 }
13581 vim_free(regmatch.regprog);
13582 }
13583
13584theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013585 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 p_cpo = save_cpo;
13587}
13588
13589/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013590 * "match()" function
13591 */
13592 static void
13593f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013594 typval_T *argvars;
13595 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013596{
13597 find_some_match(argvars, rettv, 1);
13598}
13599
13600/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013601 * "matchadd()" function
13602 */
13603 static void
13604f_matchadd(argvars, rettv)
13605 typval_T *argvars;
13606 typval_T *rettv;
13607{
13608#ifdef FEAT_SEARCH_EXTRA
13609 char_u buf[NUMBUFLEN];
13610 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13611 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13612 int prio = 10; /* default priority */
13613 int id = -1;
13614 int error = FALSE;
13615
13616 rettv->vval.v_number = -1;
13617
13618 if (grp == NULL || pat == NULL)
13619 return;
13620 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013621 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013622 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013623 if (argvars[3].v_type != VAR_UNKNOWN)
13624 id = get_tv_number_chk(&argvars[3], &error);
13625 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013626 if (error == TRUE)
13627 return;
13628 if (id >= 1 && id <= 3)
13629 {
13630 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13631 return;
13632 }
13633
13634 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13635#endif
13636}
13637
13638/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013639 * "matcharg()" function
13640 */
13641 static void
13642f_matcharg(argvars, rettv)
13643 typval_T *argvars;
13644 typval_T *rettv;
13645{
13646 if (rettv_list_alloc(rettv) == OK)
13647 {
13648#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013649 int id = get_tv_number(&argvars[0]);
13650 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013651
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013652 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013653 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013654 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13655 {
13656 list_append_string(rettv->vval.v_list,
13657 syn_id2name(m->hlg_id), -1);
13658 list_append_string(rettv->vval.v_list, m->pattern, -1);
13659 }
13660 else
13661 {
13662 list_append_string(rettv->vval.v_list, NUL, -1);
13663 list_append_string(rettv->vval.v_list, NUL, -1);
13664 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013665 }
13666#endif
13667 }
13668}
13669
13670/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013671 * "matchdelete()" function
13672 */
13673 static void
13674f_matchdelete(argvars, rettv)
13675 typval_T *argvars;
13676 typval_T *rettv;
13677{
13678#ifdef FEAT_SEARCH_EXTRA
13679 rettv->vval.v_number = match_delete(curwin,
13680 (int)get_tv_number(&argvars[0]), TRUE);
13681#endif
13682}
13683
13684/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013685 * "matchend()" function
13686 */
13687 static void
13688f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013689 typval_T *argvars;
13690 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013691{
13692 find_some_match(argvars, rettv, 0);
13693}
13694
13695/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013696 * "matchlist()" function
13697 */
13698 static void
13699f_matchlist(argvars, rettv)
13700 typval_T *argvars;
13701 typval_T *rettv;
13702{
13703 find_some_match(argvars, rettv, 3);
13704}
13705
13706/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013707 * "matchstr()" function
13708 */
13709 static void
13710f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013711 typval_T *argvars;
13712 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013713{
13714 find_some_match(argvars, rettv, 2);
13715}
13716
Bram Moolenaar33570922005-01-25 22:26:29 +000013717static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013718
13719 static void
13720max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013721 typval_T *argvars;
13722 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013723 int domax;
13724{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013725 long n = 0;
13726 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013727 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013728
13729 if (argvars[0].v_type == VAR_LIST)
13730 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 list_T *l;
13732 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013733
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013734 l = argvars[0].vval.v_list;
13735 if (l != NULL)
13736 {
13737 li = l->lv_first;
13738 if (li != NULL)
13739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013740 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013741 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013742 {
13743 li = li->li_next;
13744 if (li == NULL)
13745 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013746 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013747 if (domax ? i > n : i < n)
13748 n = i;
13749 }
13750 }
13751 }
13752 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013753 else if (argvars[0].v_type == VAR_DICT)
13754 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013755 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013756 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013757 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013758 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013759
13760 d = argvars[0].vval.v_dict;
13761 if (d != NULL)
13762 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013763 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013764 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013765 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013766 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013767 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013768 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013770 if (first)
13771 {
13772 n = i;
13773 first = FALSE;
13774 }
13775 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013776 n = i;
13777 }
13778 }
13779 }
13780 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013781 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013782 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013783 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013784}
13785
13786/*
13787 * "max()" function
13788 */
13789 static void
13790f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013791 typval_T *argvars;
13792 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013793{
13794 max_min(argvars, rettv, TRUE);
13795}
13796
13797/*
13798 * "min()" function
13799 */
13800 static void
13801f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013802 typval_T *argvars;
13803 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013804{
13805 max_min(argvars, rettv, FALSE);
13806}
13807
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013808static int mkdir_recurse __ARGS((char_u *dir, int prot));
13809
13810/*
13811 * Create the directory in which "dir" is located, and higher levels when
13812 * needed.
13813 */
13814 static int
13815mkdir_recurse(dir, prot)
13816 char_u *dir;
13817 int prot;
13818{
13819 char_u *p;
13820 char_u *updir;
13821 int r = FAIL;
13822
13823 /* Get end of directory name in "dir".
13824 * We're done when it's "/" or "c:/". */
13825 p = gettail_sep(dir);
13826 if (p <= get_past_head(dir))
13827 return OK;
13828
13829 /* If the directory exists we're done. Otherwise: create it.*/
13830 updir = vim_strnsave(dir, (int)(p - dir));
13831 if (updir == NULL)
13832 return FAIL;
13833 if (mch_isdir(updir))
13834 r = OK;
13835 else if (mkdir_recurse(updir, prot) == OK)
13836 r = vim_mkdir_emsg(updir, prot);
13837 vim_free(updir);
13838 return r;
13839}
13840
13841#ifdef vim_mkdir
13842/*
13843 * "mkdir()" function
13844 */
13845 static void
13846f_mkdir(argvars, rettv)
13847 typval_T *argvars;
13848 typval_T *rettv;
13849{
13850 char_u *dir;
13851 char_u buf[NUMBUFLEN];
13852 int prot = 0755;
13853
13854 rettv->vval.v_number = FAIL;
13855 if (check_restricted() || check_secure())
13856 return;
13857
13858 dir = get_tv_string_buf(&argvars[0], buf);
13859 if (argvars[1].v_type != VAR_UNKNOWN)
13860 {
13861 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013862 prot = get_tv_number_chk(&argvars[2], NULL);
13863 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013864 mkdir_recurse(dir, prot);
13865 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013866 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013867}
13868#endif
13869
Bram Moolenaar0d660222005-01-07 21:51:51 +000013870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871 * "mode()" function
13872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013874f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013875 typval_T *argvars;
13876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013877{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013878 char_u buf[3];
13879
13880 buf[1] = NUL;
13881 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882
13883#ifdef FEAT_VISUAL
13884 if (VIsual_active)
13885 {
13886 if (VIsual_select)
13887 buf[0] = VIsual_mode + 's' - 'v';
13888 else
13889 buf[0] = VIsual_mode;
13890 }
13891 else
13892#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013893 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13894 || State == CONFIRM)
13895 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013897 if (State == ASKMORE)
13898 buf[1] = 'm';
13899 else if (State == CONFIRM)
13900 buf[1] = '?';
13901 }
13902 else if (State == EXTERNCMD)
13903 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 else if (State & INSERT)
13905 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013906#ifdef FEAT_VREPLACE
13907 if (State & VREPLACE_FLAG)
13908 {
13909 buf[0] = 'R';
13910 buf[1] = 'v';
13911 }
13912 else
13913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914 if (State & REPLACE_FLAG)
13915 buf[0] = 'R';
13916 else
13917 buf[0] = 'i';
13918 }
13919 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013920 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013922 if (exmode_active)
13923 buf[1] = 'v';
13924 }
13925 else if (exmode_active)
13926 {
13927 buf[0] = 'c';
13928 buf[1] = 'e';
13929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013933 if (finish_op)
13934 buf[1] = 'o';
13935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013937 /* Clear out the minor mode when the argument is not a non-zero number or
13938 * non-empty string. */
13939 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013940 buf[1] = NUL;
13941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013942 rettv->vval.v_string = vim_strsave(buf);
13943 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013944}
13945
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013946#ifdef FEAT_MZSCHEME
13947/*
13948 * "mzeval()" function
13949 */
13950 static void
13951f_mzeval(argvars, rettv)
13952 typval_T *argvars;
13953 typval_T *rettv;
13954{
13955 char_u *str;
13956 char_u buf[NUMBUFLEN];
13957
13958 str = get_tv_string_buf(&argvars[0], buf);
13959 do_mzeval(str, rettv);
13960}
13961#endif
13962
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013964 * "nextnonblank()" function
13965 */
13966 static void
13967f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013968 typval_T *argvars;
13969 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013970{
13971 linenr_T lnum;
13972
13973 for (lnum = get_tv_lnum(argvars); ; ++lnum)
13974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013975 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013976 {
13977 lnum = 0;
13978 break;
13979 }
13980 if (*skipwhite(ml_get(lnum)) != NUL)
13981 break;
13982 }
13983 rettv->vval.v_number = lnum;
13984}
13985
13986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 * "nr2char()" function
13988 */
13989 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013990f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013991 typval_T *argvars;
13992 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993{
13994 char_u buf[NUMBUFLEN];
13995
13996#ifdef FEAT_MBYTE
13997 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013998 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999 else
14000#endif
14001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014002 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003 buf[1] = NUL;
14004 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014005 rettv->v_type = VAR_STRING;
14006 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014007}
14008
14009/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014010 * "pathshorten()" function
14011 */
14012 static void
14013f_pathshorten(argvars, rettv)
14014 typval_T *argvars;
14015 typval_T *rettv;
14016{
14017 char_u *p;
14018
14019 rettv->v_type = VAR_STRING;
14020 p = get_tv_string_chk(&argvars[0]);
14021 if (p == NULL)
14022 rettv->vval.v_string = NULL;
14023 else
14024 {
14025 p = vim_strsave(p);
14026 rettv->vval.v_string = p;
14027 if (p != NULL)
14028 shorten_dir(p);
14029 }
14030}
14031
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014032#ifdef FEAT_FLOAT
14033/*
14034 * "pow()" function
14035 */
14036 static void
14037f_pow(argvars, rettv)
14038 typval_T *argvars;
14039 typval_T *rettv;
14040{
14041 float_T fx, fy;
14042
14043 rettv->v_type = VAR_FLOAT;
14044 if (get_float_arg(argvars, &fx) == OK
14045 && get_float_arg(&argvars[1], &fy) == OK)
14046 rettv->vval.v_float = pow(fx, fy);
14047 else
14048 rettv->vval.v_float = 0.0;
14049}
14050#endif
14051
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014052/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014053 * "prevnonblank()" function
14054 */
14055 static void
14056f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014057 typval_T *argvars;
14058 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014059{
14060 linenr_T lnum;
14061
14062 lnum = get_tv_lnum(argvars);
14063 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14064 lnum = 0;
14065 else
14066 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14067 --lnum;
14068 rettv->vval.v_number = lnum;
14069}
14070
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014071#ifdef HAVE_STDARG_H
14072/* This dummy va_list is here because:
14073 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14074 * - locally in the function results in a "used before set" warning
14075 * - using va_start() to initialize it gives "function with fixed args" error */
14076static va_list ap;
14077#endif
14078
Bram Moolenaar8c711452005-01-14 21:53:12 +000014079/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014080 * "printf()" function
14081 */
14082 static void
14083f_printf(argvars, rettv)
14084 typval_T *argvars;
14085 typval_T *rettv;
14086{
14087 rettv->v_type = VAR_STRING;
14088 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014089#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014090 {
14091 char_u buf[NUMBUFLEN];
14092 int len;
14093 char_u *s;
14094 int saved_did_emsg = did_emsg;
14095 char *fmt;
14096
14097 /* Get the required length, allocate the buffer and do it for real. */
14098 did_emsg = FALSE;
14099 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014100 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014101 if (!did_emsg)
14102 {
14103 s = alloc(len + 1);
14104 if (s != NULL)
14105 {
14106 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014107 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014108 }
14109 }
14110 did_emsg |= saved_did_emsg;
14111 }
14112#endif
14113}
14114
14115/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014116 * "pumvisible()" function
14117 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014118 static void
14119f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014120 typval_T *argvars UNUSED;
14121 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014122{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014123#ifdef FEAT_INS_EXPAND
14124 if (pum_visible())
14125 rettv->vval.v_number = 1;
14126#endif
14127}
14128
14129/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014130 * "range()" function
14131 */
14132 static void
14133f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *argvars;
14135 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014136{
14137 long start;
14138 long end;
14139 long stride = 1;
14140 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014142
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014144 if (argvars[1].v_type == VAR_UNKNOWN)
14145 {
14146 end = start - 1;
14147 start = 0;
14148 }
14149 else
14150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014151 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014152 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014153 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014154 }
14155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 if (error)
14157 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014158 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014159 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014160 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014161 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014162 else
14163 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014164 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014165 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014166 if (list_append_number(rettv->vval.v_list,
14167 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014168 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014169 }
14170}
14171
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014172/*
14173 * "readfile()" function
14174 */
14175 static void
14176f_readfile(argvars, rettv)
14177 typval_T *argvars;
14178 typval_T *rettv;
14179{
14180 int binary = FALSE;
14181 char_u *fname;
14182 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014183 listitem_T *li;
14184#define FREAD_SIZE 200 /* optimized for text lines */
14185 char_u buf[FREAD_SIZE];
14186 int readlen; /* size of last fread() */
14187 int buflen; /* nr of valid chars in buf[] */
14188 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14189 int tolist; /* first byte in buf[] still to be put in list */
14190 int chop; /* how many CR to chop off */
14191 char_u *prev = NULL; /* previously read bytes, if any */
14192 int prevlen = 0; /* length of "prev" if not NULL */
14193 char_u *s;
14194 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014195 long maxline = MAXLNUM;
14196 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014197
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014198 if (argvars[1].v_type != VAR_UNKNOWN)
14199 {
14200 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14201 binary = TRUE;
14202 if (argvars[2].v_type != VAR_UNKNOWN)
14203 maxline = get_tv_number(&argvars[2]);
14204 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014205
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014206 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014207 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014208
14209 /* Always open the file in binary mode, library functions have a mind of
14210 * their own about CR-LF conversion. */
14211 fname = get_tv_string(&argvars[0]);
14212 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14213 {
14214 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14215 return;
14216 }
14217
14218 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014219 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014220 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014221 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014222 buflen = filtd + readlen;
14223 tolist = 0;
14224 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14225 {
14226 if (buf[filtd] == '\n' || readlen <= 0)
14227 {
14228 /* Only when in binary mode add an empty list item when the
14229 * last line ends in a '\n'. */
14230 if (!binary && readlen == 0 && filtd == 0)
14231 break;
14232
14233 /* Found end-of-line or end-of-file: add a text line to the
14234 * list. */
14235 chop = 0;
14236 if (!binary)
14237 while (filtd - chop - 1 >= tolist
14238 && buf[filtd - chop - 1] == '\r')
14239 ++chop;
14240 len = filtd - tolist - chop;
14241 if (prev == NULL)
14242 s = vim_strnsave(buf + tolist, len);
14243 else
14244 {
14245 s = alloc((unsigned)(prevlen + len + 1));
14246 if (s != NULL)
14247 {
14248 mch_memmove(s, prev, prevlen);
14249 vim_free(prev);
14250 prev = NULL;
14251 mch_memmove(s + prevlen, buf + tolist, len);
14252 s[prevlen + len] = NUL;
14253 }
14254 }
14255 tolist = filtd + 1;
14256
14257 li = listitem_alloc();
14258 if (li == NULL)
14259 {
14260 vim_free(s);
14261 break;
14262 }
14263 li->li_tv.v_type = VAR_STRING;
14264 li->li_tv.v_lock = 0;
14265 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014266 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014267
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014268 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014269 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014270 if (readlen <= 0)
14271 break;
14272 }
14273 else if (buf[filtd] == NUL)
14274 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014275#ifdef FEAT_MBYTE
14276 else if (buf[filtd] == 0xef
14277 && enc_utf8
14278 && filtd + 2 < buflen
14279 && !binary
14280 && buf[filtd + 1] == 0xbb
14281 && buf[filtd + 2] == 0xbf)
14282 {
14283 /* remove utf-8 byte order mark */
14284 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14285 --filtd;
14286 buflen -= 3;
14287 }
14288#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014289 }
14290 if (readlen <= 0)
14291 break;
14292
14293 if (tolist == 0)
14294 {
14295 /* "buf" is full, need to move text to an allocated buffer */
14296 if (prev == NULL)
14297 {
14298 prev = vim_strnsave(buf, buflen);
14299 prevlen = buflen;
14300 }
14301 else
14302 {
14303 s = alloc((unsigned)(prevlen + buflen));
14304 if (s != NULL)
14305 {
14306 mch_memmove(s, prev, prevlen);
14307 mch_memmove(s + prevlen, buf, buflen);
14308 vim_free(prev);
14309 prev = s;
14310 prevlen += buflen;
14311 }
14312 }
14313 filtd = 0;
14314 }
14315 else
14316 {
14317 mch_memmove(buf, buf + tolist, buflen - tolist);
14318 filtd -= tolist;
14319 }
14320 }
14321
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014322 /*
14323 * For a negative line count use only the lines at the end of the file,
14324 * free the rest.
14325 */
14326 if (maxline < 0)
14327 while (cnt > -maxline)
14328 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014329 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014330 --cnt;
14331 }
14332
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014333 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014334 fclose(fd);
14335}
14336
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014337#if defined(FEAT_RELTIME)
14338static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14339
14340/*
14341 * Convert a List to proftime_T.
14342 * Return FAIL when there is something wrong.
14343 */
14344 static int
14345list2proftime(arg, tm)
14346 typval_T *arg;
14347 proftime_T *tm;
14348{
14349 long n1, n2;
14350 int error = FALSE;
14351
14352 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14353 || arg->vval.v_list->lv_len != 2)
14354 return FAIL;
14355 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14356 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14357# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014358 tm->HighPart = n1;
14359 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014360# else
14361 tm->tv_sec = n1;
14362 tm->tv_usec = n2;
14363# endif
14364 return error ? FAIL : OK;
14365}
14366#endif /* FEAT_RELTIME */
14367
14368/*
14369 * "reltime()" function
14370 */
14371 static void
14372f_reltime(argvars, rettv)
14373 typval_T *argvars;
14374 typval_T *rettv;
14375{
14376#ifdef FEAT_RELTIME
14377 proftime_T res;
14378 proftime_T start;
14379
14380 if (argvars[0].v_type == VAR_UNKNOWN)
14381 {
14382 /* No arguments: get current time. */
14383 profile_start(&res);
14384 }
14385 else if (argvars[1].v_type == VAR_UNKNOWN)
14386 {
14387 if (list2proftime(&argvars[0], &res) == FAIL)
14388 return;
14389 profile_end(&res);
14390 }
14391 else
14392 {
14393 /* Two arguments: compute the difference. */
14394 if (list2proftime(&argvars[0], &start) == FAIL
14395 || list2proftime(&argvars[1], &res) == FAIL)
14396 return;
14397 profile_sub(&res, &start);
14398 }
14399
14400 if (rettv_list_alloc(rettv) == OK)
14401 {
14402 long n1, n2;
14403
14404# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014405 n1 = res.HighPart;
14406 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014407# else
14408 n1 = res.tv_sec;
14409 n2 = res.tv_usec;
14410# endif
14411 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14412 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14413 }
14414#endif
14415}
14416
14417/*
14418 * "reltimestr()" function
14419 */
14420 static void
14421f_reltimestr(argvars, rettv)
14422 typval_T *argvars;
14423 typval_T *rettv;
14424{
14425#ifdef FEAT_RELTIME
14426 proftime_T tm;
14427#endif
14428
14429 rettv->v_type = VAR_STRING;
14430 rettv->vval.v_string = NULL;
14431#ifdef FEAT_RELTIME
14432 if (list2proftime(&argvars[0], &tm) == OK)
14433 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14434#endif
14435}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014436
Bram Moolenaar0d660222005-01-07 21:51:51 +000014437#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14438static void make_connection __ARGS((void));
14439static int check_connection __ARGS((void));
14440
14441 static void
14442make_connection()
14443{
14444 if (X_DISPLAY == NULL
14445# ifdef FEAT_GUI
14446 && !gui.in_use
14447# endif
14448 )
14449 {
14450 x_force_connect = TRUE;
14451 setup_term_clip();
14452 x_force_connect = FALSE;
14453 }
14454}
14455
14456 static int
14457check_connection()
14458{
14459 make_connection();
14460 if (X_DISPLAY == NULL)
14461 {
14462 EMSG(_("E240: No connection to Vim server"));
14463 return FAIL;
14464 }
14465 return OK;
14466}
14467#endif
14468
14469#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014470static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014471
14472 static void
14473remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014474 typval_T *argvars;
14475 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476 int expr;
14477{
14478 char_u *server_name;
14479 char_u *keys;
14480 char_u *r = NULL;
14481 char_u buf[NUMBUFLEN];
14482# ifdef WIN32
14483 HWND w;
14484# else
14485 Window w;
14486# endif
14487
14488 if (check_restricted() || check_secure())
14489 return;
14490
14491# ifdef FEAT_X11
14492 if (check_connection() == FAIL)
14493 return;
14494# endif
14495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014496 server_name = get_tv_string_chk(&argvars[0]);
14497 if (server_name == NULL)
14498 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014499 keys = get_tv_string_buf(&argvars[1], buf);
14500# ifdef WIN32
14501 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14502# else
14503 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14504 < 0)
14505# endif
14506 {
14507 if (r != NULL)
14508 EMSG(r); /* sending worked but evaluation failed */
14509 else
14510 EMSG2(_("E241: Unable to send to %s"), server_name);
14511 return;
14512 }
14513
14514 rettv->vval.v_string = r;
14515
14516 if (argvars[2].v_type != VAR_UNKNOWN)
14517 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014518 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014519 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014520 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014521
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014522 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014523 v.di_tv.v_type = VAR_STRING;
14524 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014525 idvar = get_tv_string_chk(&argvars[2]);
14526 if (idvar != NULL)
14527 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014528 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014529 }
14530}
14531#endif
14532
14533/*
14534 * "remote_expr()" function
14535 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014536 static void
14537f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014538 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014539 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014540{
14541 rettv->v_type = VAR_STRING;
14542 rettv->vval.v_string = NULL;
14543#ifdef FEAT_CLIENTSERVER
14544 remote_common(argvars, rettv, TRUE);
14545#endif
14546}
14547
14548/*
14549 * "remote_foreground()" function
14550 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014551 static void
14552f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014553 typval_T *argvars UNUSED;
14554 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014555{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014556#ifdef FEAT_CLIENTSERVER
14557# ifdef WIN32
14558 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 {
14560 char_u *server_name = get_tv_string_chk(&argvars[0]);
14561
14562 if (server_name != NULL)
14563 serverForeground(server_name);
14564 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014565# else
14566 /* Send a foreground() expression to the server. */
14567 argvars[1].v_type = VAR_STRING;
14568 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14569 argvars[2].v_type = VAR_UNKNOWN;
14570 remote_common(argvars, rettv, TRUE);
14571 vim_free(argvars[1].vval.v_string);
14572# endif
14573#endif
14574}
14575
Bram Moolenaar0d660222005-01-07 21:51:51 +000014576 static void
14577f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014578 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014579 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014580{
14581#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014582 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014583 char_u *s = NULL;
14584# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014585 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014586# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014587 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014588
14589 if (check_restricted() || check_secure())
14590 {
14591 rettv->vval.v_number = -1;
14592 return;
14593 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594 serverid = get_tv_string_chk(&argvars[0]);
14595 if (serverid == NULL)
14596 {
14597 rettv->vval.v_number = -1;
14598 return; /* type error; errmsg already given */
14599 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014600# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014601 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014602 if (n == 0)
14603 rettv->vval.v_number = -1;
14604 else
14605 {
14606 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14607 rettv->vval.v_number = (s != NULL);
14608 }
14609# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014610 if (check_connection() == FAIL)
14611 return;
14612
14613 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014614 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014615# endif
14616
14617 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014619 char_u *retvar;
14620
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 v.di_tv.v_type = VAR_STRING;
14622 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 retvar = get_tv_string_chk(&argvars[1]);
14624 if (retvar != NULL)
14625 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014626 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014627 }
14628#else
14629 rettv->vval.v_number = -1;
14630#endif
14631}
14632
Bram Moolenaar0d660222005-01-07 21:51:51 +000014633 static void
14634f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014635 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014636 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014637{
14638 char_u *r = NULL;
14639
14640#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014641 char_u *serverid = get_tv_string_chk(&argvars[0]);
14642
14643 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644 {
14645# ifdef WIN32
14646 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014647 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014648
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014649 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014650 if (n != 0)
14651 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14652 if (r == NULL)
14653# else
14654 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014655 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014656# endif
14657 EMSG(_("E277: Unable to read a server reply"));
14658 }
14659#endif
14660 rettv->v_type = VAR_STRING;
14661 rettv->vval.v_string = r;
14662}
14663
14664/*
14665 * "remote_send()" function
14666 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014667 static void
14668f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014669 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014670 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014671{
14672 rettv->v_type = VAR_STRING;
14673 rettv->vval.v_string = NULL;
14674#ifdef FEAT_CLIENTSERVER
14675 remote_common(argvars, rettv, FALSE);
14676#endif
14677}
14678
14679/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014680 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014681 */
14682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014684 typval_T *argvars;
14685 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014686{
Bram Moolenaar33570922005-01-25 22:26:29 +000014687 list_T *l;
14688 listitem_T *item, *item2;
14689 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014690 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014691 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014692 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014693 dict_T *d;
14694 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014695
Bram Moolenaar8c711452005-01-14 21:53:12 +000014696 if (argvars[0].v_type == VAR_DICT)
14697 {
14698 if (argvars[2].v_type != VAR_UNKNOWN)
14699 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014700 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014701 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014702 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014703 key = get_tv_string_chk(&argvars[1]);
14704 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014706 di = dict_find(d, key, -1);
14707 if (di == NULL)
14708 EMSG2(_(e_dictkey), key);
14709 else
14710 {
14711 *rettv = di->di_tv;
14712 init_tv(&di->di_tv);
14713 dictitem_remove(d, di);
14714 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014715 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014716 }
14717 }
14718 else if (argvars[0].v_type != VAR_LIST)
14719 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014720 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014721 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014722 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014723 int error = FALSE;
14724
14725 idx = get_tv_number_chk(&argvars[1], &error);
14726 if (error)
14727 ; /* type error: do nothing, errmsg already given */
14728 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014729 EMSGN(_(e_listidx), idx);
14730 else
14731 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014732 if (argvars[2].v_type == VAR_UNKNOWN)
14733 {
14734 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014735 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014736 *rettv = item->li_tv;
14737 vim_free(item);
14738 }
14739 else
14740 {
14741 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 end = get_tv_number_chk(&argvars[2], &error);
14743 if (error)
14744 ; /* type error: do nothing */
14745 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014746 EMSGN(_(e_listidx), end);
14747 else
14748 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014749 int cnt = 0;
14750
14751 for (li = item; li != NULL; li = li->li_next)
14752 {
14753 ++cnt;
14754 if (li == item2)
14755 break;
14756 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014757 if (li == NULL) /* didn't find "item2" after "item" */
14758 EMSG(_(e_invrange));
14759 else
14760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014761 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014762 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014763 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014764 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014765 l->lv_first = item;
14766 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014767 item->li_prev = NULL;
14768 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014769 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014770 }
14771 }
14772 }
14773 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014774 }
14775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776}
14777
14778/*
14779 * "rename({from}, {to})" function
14780 */
14781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014782f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014783 typval_T *argvars;
14784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785{
14786 char_u buf[NUMBUFLEN];
14787
14788 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014791 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14792 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014793}
14794
14795/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014796 * "repeat()" function
14797 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014798 static void
14799f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014800 typval_T *argvars;
14801 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014802{
14803 char_u *p;
14804 int n;
14805 int slen;
14806 int len;
14807 char_u *r;
14808 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014809
14810 n = get_tv_number(&argvars[1]);
14811 if (argvars[0].v_type == VAR_LIST)
14812 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014813 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014814 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014815 if (list_extend(rettv->vval.v_list,
14816 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014817 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014818 }
14819 else
14820 {
14821 p = get_tv_string(&argvars[0]);
14822 rettv->v_type = VAR_STRING;
14823 rettv->vval.v_string = NULL;
14824
14825 slen = (int)STRLEN(p);
14826 len = slen * n;
14827 if (len <= 0)
14828 return;
14829
14830 r = alloc(len + 1);
14831 if (r != NULL)
14832 {
14833 for (i = 0; i < n; i++)
14834 mch_memmove(r + i * slen, p, (size_t)slen);
14835 r[len] = NUL;
14836 }
14837
14838 rettv->vval.v_string = r;
14839 }
14840}
14841
14842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843 * "resolve()" function
14844 */
14845 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014847 typval_T *argvars;
14848 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014849{
14850 char_u *p;
14851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014852 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853#ifdef FEAT_SHORTCUT
14854 {
14855 char_u *v = NULL;
14856
14857 v = mch_resolve_shortcut(p);
14858 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014859 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014861 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014862 }
14863#else
14864# ifdef HAVE_READLINK
14865 {
14866 char_u buf[MAXPATHL + 1];
14867 char_u *cpy;
14868 int len;
14869 char_u *remain = NULL;
14870 char_u *q;
14871 int is_relative_to_current = FALSE;
14872 int has_trailing_pathsep = FALSE;
14873 int limit = 100;
14874
14875 p = vim_strsave(p);
14876
14877 if (p[0] == '.' && (vim_ispathsep(p[1])
14878 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14879 is_relative_to_current = TRUE;
14880
14881 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014882 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 has_trailing_pathsep = TRUE;
14884
14885 q = getnextcomp(p);
14886 if (*q != NUL)
14887 {
14888 /* Separate the first path component in "p", and keep the
14889 * remainder (beginning with the path separator). */
14890 remain = vim_strsave(q - 1);
14891 q[-1] = NUL;
14892 }
14893
14894 for (;;)
14895 {
14896 for (;;)
14897 {
14898 len = readlink((char *)p, (char *)buf, MAXPATHL);
14899 if (len <= 0)
14900 break;
14901 buf[len] = NUL;
14902
14903 if (limit-- == 0)
14904 {
14905 vim_free(p);
14906 vim_free(remain);
14907 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014908 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014909 goto fail;
14910 }
14911
14912 /* Ensure that the result will have a trailing path separator
14913 * if the argument has one. */
14914 if (remain == NULL && has_trailing_pathsep)
14915 add_pathsep(buf);
14916
14917 /* Separate the first path component in the link value and
14918 * concatenate the remainders. */
14919 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14920 if (*q != NUL)
14921 {
14922 if (remain == NULL)
14923 remain = vim_strsave(q - 1);
14924 else
14925 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014926 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927 if (cpy != NULL)
14928 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929 vim_free(remain);
14930 remain = cpy;
14931 }
14932 }
14933 q[-1] = NUL;
14934 }
14935
14936 q = gettail(p);
14937 if (q > p && *q == NUL)
14938 {
14939 /* Ignore trailing path separator. */
14940 q[-1] = NUL;
14941 q = gettail(p);
14942 }
14943 if (q > p && !mch_isFullName(buf))
14944 {
14945 /* symlink is relative to directory of argument */
14946 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14947 if (cpy != NULL)
14948 {
14949 STRCPY(cpy, p);
14950 STRCPY(gettail(cpy), buf);
14951 vim_free(p);
14952 p = cpy;
14953 }
14954 }
14955 else
14956 {
14957 vim_free(p);
14958 p = vim_strsave(buf);
14959 }
14960 }
14961
14962 if (remain == NULL)
14963 break;
14964
14965 /* Append the first path component of "remain" to "p". */
14966 q = getnextcomp(remain + 1);
14967 len = q - remain - (*q != NUL);
14968 cpy = vim_strnsave(p, STRLEN(p) + len);
14969 if (cpy != NULL)
14970 {
14971 STRNCAT(cpy, remain, len);
14972 vim_free(p);
14973 p = cpy;
14974 }
14975 /* Shorten "remain". */
14976 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014977 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978 else
14979 {
14980 vim_free(remain);
14981 remain = NULL;
14982 }
14983 }
14984
14985 /* If the result is a relative path name, make it explicitly relative to
14986 * the current directory if and only if the argument had this form. */
14987 if (!vim_ispathsep(*p))
14988 {
14989 if (is_relative_to_current
14990 && *p != NUL
14991 && !(p[0] == '.'
14992 && (p[1] == NUL
14993 || vim_ispathsep(p[1])
14994 || (p[1] == '.'
14995 && (p[2] == NUL
14996 || vim_ispathsep(p[2]))))))
14997 {
14998 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014999 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000 if (cpy != NULL)
15001 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015002 vim_free(p);
15003 p = cpy;
15004 }
15005 }
15006 else if (!is_relative_to_current)
15007 {
15008 /* Strip leading "./". */
15009 q = p;
15010 while (q[0] == '.' && vim_ispathsep(q[1]))
15011 q += 2;
15012 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015013 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014 }
15015 }
15016
15017 /* Ensure that the result will have no trailing path separator
15018 * if the argument had none. But keep "/" or "//". */
15019 if (!has_trailing_pathsep)
15020 {
15021 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015022 if (after_pathsep(p, q))
15023 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 }
15025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015026 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 }
15028# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015029 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030# endif
15031#endif
15032
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015033 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034
15035#ifdef HAVE_READLINK
15036fail:
15037#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015038 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039}
15040
15041/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015042 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 */
15044 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015045f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015046 typval_T *argvars;
15047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015048{
Bram Moolenaar33570922005-01-25 22:26:29 +000015049 list_T *l;
15050 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051
Bram Moolenaar0d660222005-01-07 21:51:51 +000015052 if (argvars[0].v_type != VAR_LIST)
15053 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015054 else if ((l = argvars[0].vval.v_list) != NULL
15055 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015056 {
15057 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015058 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015059 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015060 while (li != NULL)
15061 {
15062 ni = li->li_prev;
15063 list_append(l, li);
15064 li = ni;
15065 }
15066 rettv->vval.v_list = l;
15067 rettv->v_type = VAR_LIST;
15068 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015069 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071}
15072
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015073#define SP_NOMOVE 0x01 /* don't move cursor */
15074#define SP_REPEAT 0x02 /* repeat to find outer pair */
15075#define SP_RETCOUNT 0x04 /* return matchcount */
15076#define SP_SETPCMARK 0x08 /* set previous context mark */
15077#define SP_START 0x10 /* accept match at start position */
15078#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15079#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015080
Bram Moolenaar33570922005-01-25 22:26:29 +000015081static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015082
15083/*
15084 * Get flags for a search function.
15085 * Possibly sets "p_ws".
15086 * Returns BACKWARD, FORWARD or zero (for an error).
15087 */
15088 static int
15089get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015090 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091 int *flagsp;
15092{
15093 int dir = FORWARD;
15094 char_u *flags;
15095 char_u nbuf[NUMBUFLEN];
15096 int mask;
15097
15098 if (varp->v_type != VAR_UNKNOWN)
15099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015100 flags = get_tv_string_buf_chk(varp, nbuf);
15101 if (flags == NULL)
15102 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015103 while (*flags != NUL)
15104 {
15105 switch (*flags)
15106 {
15107 case 'b': dir = BACKWARD; break;
15108 case 'w': p_ws = TRUE; break;
15109 case 'W': p_ws = FALSE; break;
15110 default: mask = 0;
15111 if (flagsp != NULL)
15112 switch (*flags)
15113 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015114 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015115 case 'e': mask = SP_END; break;
15116 case 'm': mask = SP_RETCOUNT; break;
15117 case 'n': mask = SP_NOMOVE; break;
15118 case 'p': mask = SP_SUBPAT; break;
15119 case 'r': mask = SP_REPEAT; break;
15120 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121 }
15122 if (mask == 0)
15123 {
15124 EMSG2(_(e_invarg2), flags);
15125 dir = 0;
15126 }
15127 else
15128 *flagsp |= mask;
15129 }
15130 if (dir == 0)
15131 break;
15132 ++flags;
15133 }
15134 }
15135 return dir;
15136}
15137
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015139 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015141 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015142search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015143 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015144 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015145 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015146{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015147 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015148 char_u *pat;
15149 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015150 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151 int save_p_ws = p_ws;
15152 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015153 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015154 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015155 proftime_T tm;
15156#ifdef FEAT_RELTIME
15157 long time_limit = 0;
15158#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015159 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015160 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015162 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015163 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015164 if (dir == 0)
15165 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015166 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015167 if (flags & SP_START)
15168 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015169 if (flags & SP_END)
15170 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015171
Bram Moolenaar76929292008-01-06 19:07:36 +000015172 /* Optional arguments: line number to stop searching and timeout. */
15173 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015174 {
15175 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15176 if (lnum_stop < 0)
15177 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015178#ifdef FEAT_RELTIME
15179 if (argvars[3].v_type != VAR_UNKNOWN)
15180 {
15181 time_limit = get_tv_number_chk(&argvars[3], NULL);
15182 if (time_limit < 0)
15183 goto theend;
15184 }
15185#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015186 }
15187
Bram Moolenaar76929292008-01-06 19:07:36 +000015188#ifdef FEAT_RELTIME
15189 /* Set the time limit, if there is one. */
15190 profile_setlimit(time_limit, &tm);
15191#endif
15192
Bram Moolenaar231334e2005-07-25 20:46:57 +000015193 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015194 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015195 * Check to make sure only those flags are set.
15196 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15197 * flags cannot be set. Check for that condition also.
15198 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015199 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015200 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015202 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015203 goto theend;
15204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015206 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015207 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015208 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015209 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015211 if (flags & SP_SUBPAT)
15212 retval = subpatnum;
15213 else
15214 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015215 if (flags & SP_SETPCMARK)
15216 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015217 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015218 if (match_pos != NULL)
15219 {
15220 /* Store the match cursor position */
15221 match_pos->lnum = pos.lnum;
15222 match_pos->col = pos.col + 1;
15223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015224 /* "/$" will put the cursor after the end of the line, may need to
15225 * correct that here */
15226 check_cursor();
15227 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015228
15229 /* If 'n' flag is used: restore cursor position. */
15230 if (flags & SP_NOMOVE)
15231 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015232 else
15233 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015234theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015236
15237 return retval;
15238}
15239
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015240#ifdef FEAT_FLOAT
15241/*
15242 * "round({float})" function
15243 */
15244 static void
15245f_round(argvars, rettv)
15246 typval_T *argvars;
15247 typval_T *rettv;
15248{
15249 float_T f;
15250
15251 rettv->v_type = VAR_FLOAT;
15252 if (get_float_arg(argvars, &f) == OK)
15253 /* round() is not in C90, use ceil() or floor() instead. */
15254 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15255 else
15256 rettv->vval.v_float = 0.0;
15257}
15258#endif
15259
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015260/*
15261 * "search()" function
15262 */
15263 static void
15264f_search(argvars, rettv)
15265 typval_T *argvars;
15266 typval_T *rettv;
15267{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015268 int flags = 0;
15269
15270 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015271}
15272
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015274 * "searchdecl()" function
15275 */
15276 static void
15277f_searchdecl(argvars, rettv)
15278 typval_T *argvars;
15279 typval_T *rettv;
15280{
15281 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015282 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015283 int error = FALSE;
15284 char_u *name;
15285
15286 rettv->vval.v_number = 1; /* default: FAIL */
15287
15288 name = get_tv_string_chk(&argvars[0]);
15289 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015290 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015291 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015292 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15293 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15294 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015295 if (!error && name != NULL)
15296 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015297 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015298}
15299
15300/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015301 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015302 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015303 static int
15304searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015305 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015306 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307{
15308 char_u *spat, *mpat, *epat;
15309 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015311 int dir;
15312 int flags = 0;
15313 char_u nbuf1[NUMBUFLEN];
15314 char_u nbuf2[NUMBUFLEN];
15315 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015316 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015317 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015318 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015319
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015321 spat = get_tv_string_chk(&argvars[0]);
15322 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15323 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15324 if (spat == NULL || mpat == NULL || epat == NULL)
15325 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 /* Handle the optional fourth argument: flags */
15328 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015329 if (dir == 0)
15330 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015331
15332 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015333 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15334 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015335 if ((flags & (SP_END | SP_SUBPAT)) != 0
15336 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015337 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015338 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015339 goto theend;
15340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015342 /* Using 'r' implies 'W', otherwise it doesn't work. */
15343 if (flags & SP_REPEAT)
15344 p_ws = FALSE;
15345
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015346 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015347 if (argvars[3].v_type == VAR_UNKNOWN
15348 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349 skip = (char_u *)"";
15350 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015352 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015353 if (argvars[5].v_type != VAR_UNKNOWN)
15354 {
15355 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15356 if (lnum_stop < 0)
15357 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015358#ifdef FEAT_RELTIME
15359 if (argvars[6].v_type != VAR_UNKNOWN)
15360 {
15361 time_limit = get_tv_number_chk(&argvars[6], NULL);
15362 if (time_limit < 0)
15363 goto theend;
15364 }
15365#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015366 }
15367 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015368 if (skip == NULL)
15369 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015370
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015371 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015372 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015373
15374theend:
15375 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015376
15377 return retval;
15378}
15379
15380/*
15381 * "searchpair()" function
15382 */
15383 static void
15384f_searchpair(argvars, rettv)
15385 typval_T *argvars;
15386 typval_T *rettv;
15387{
15388 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15389}
15390
15391/*
15392 * "searchpairpos()" function
15393 */
15394 static void
15395f_searchpairpos(argvars, rettv)
15396 typval_T *argvars;
15397 typval_T *rettv;
15398{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015399 pos_T match_pos;
15400 int lnum = 0;
15401 int col = 0;
15402
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015403 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015404 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015405
15406 if (searchpair_cmn(argvars, &match_pos) > 0)
15407 {
15408 lnum = match_pos.lnum;
15409 col = match_pos.col;
15410 }
15411
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015412 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15413 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015414}
15415
15416/*
15417 * Search for a start/middle/end thing.
15418 * Used by searchpair(), see its documentation for the details.
15419 * Returns 0 or -1 for no match,
15420 */
15421 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015422do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15423 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015424 char_u *spat; /* start pattern */
15425 char_u *mpat; /* middle pattern */
15426 char_u *epat; /* end pattern */
15427 int dir; /* BACKWARD or FORWARD */
15428 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015429 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015430 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015431 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015432 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015433{
15434 char_u *save_cpo;
15435 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15436 long retval = 0;
15437 pos_T pos;
15438 pos_T firstpos;
15439 pos_T foundpos;
15440 pos_T save_cursor;
15441 pos_T save_pos;
15442 int n;
15443 int r;
15444 int nest = 1;
15445 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015446 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015447 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015448
15449 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15450 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015451 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015452
Bram Moolenaar76929292008-01-06 19:07:36 +000015453#ifdef FEAT_RELTIME
15454 /* Set the time limit, if there is one. */
15455 profile_setlimit(time_limit, &tm);
15456#endif
15457
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015458 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15459 * start/middle/end (pat3, for the top pair). */
15460 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15461 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15462 if (pat2 == NULL || pat3 == NULL)
15463 goto theend;
15464 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15465 if (*mpat == NUL)
15466 STRCPY(pat3, pat2);
15467 else
15468 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15469 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015470 if (flags & SP_START)
15471 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015472
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 save_cursor = curwin->w_cursor;
15474 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015475 clearpos(&firstpos);
15476 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477 pat = pat3;
15478 for (;;)
15479 {
15480 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015481 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015482 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15483 /* didn't find it or found the first match again: FAIL */
15484 break;
15485
15486 if (firstpos.lnum == 0)
15487 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015488 if (equalpos(pos, foundpos))
15489 {
15490 /* Found the same position again. Can happen with a pattern that
15491 * has "\zs" at the end and searching backwards. Advance one
15492 * character and try again. */
15493 if (dir == BACKWARD)
15494 decl(&pos);
15495 else
15496 incl(&pos);
15497 }
15498 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015500 /* clear the start flag to avoid getting stuck here */
15501 options &= ~SEARCH_START;
15502
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503 /* If the skip pattern matches, ignore this match. */
15504 if (*skip != NUL)
15505 {
15506 save_pos = curwin->w_cursor;
15507 curwin->w_cursor = pos;
15508 r = eval_to_bool(skip, &err, NULL, FALSE);
15509 curwin->w_cursor = save_pos;
15510 if (err)
15511 {
15512 /* Evaluating {skip} caused an error, break here. */
15513 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015514 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 break;
15516 }
15517 if (r)
15518 continue;
15519 }
15520
15521 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15522 {
15523 /* Found end when searching backwards or start when searching
15524 * forward: nested pair. */
15525 ++nest;
15526 pat = pat2; /* nested, don't search for middle */
15527 }
15528 else
15529 {
15530 /* Found end when searching forward or start when searching
15531 * backward: end of (nested) pair; or found middle in outer pair. */
15532 if (--nest == 1)
15533 pat = pat3; /* outer level, search for middle */
15534 }
15535
15536 if (nest == 0)
15537 {
15538 /* Found the match: return matchcount or line number. */
15539 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015540 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015542 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015543 if (flags & SP_SETPCMARK)
15544 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 curwin->w_cursor = pos;
15546 if (!(flags & SP_REPEAT))
15547 break;
15548 nest = 1; /* search for next unmatched */
15549 }
15550 }
15551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015552 if (match_pos != NULL)
15553 {
15554 /* Store the match cursor position */
15555 match_pos->lnum = curwin->w_cursor.lnum;
15556 match_pos->col = curwin->w_cursor.col + 1;
15557 }
15558
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015560 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 curwin->w_cursor = save_cursor;
15562
15563theend:
15564 vim_free(pat2);
15565 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015566 if (p_cpo == empty_option)
15567 p_cpo = save_cpo;
15568 else
15569 /* Darn, evaluating the {skip} expression changed the value. */
15570 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015571
15572 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573}
15574
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015575/*
15576 * "searchpos()" function
15577 */
15578 static void
15579f_searchpos(argvars, rettv)
15580 typval_T *argvars;
15581 typval_T *rettv;
15582{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015583 pos_T match_pos;
15584 int lnum = 0;
15585 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015586 int n;
15587 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015588
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015589 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015590 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015591
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015592 n = search_cmn(argvars, &match_pos, &flags);
15593 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015594 {
15595 lnum = match_pos.lnum;
15596 col = match_pos.col;
15597 }
15598
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015599 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15600 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015601 if (flags & SP_SUBPAT)
15602 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015603}
15604
15605
Bram Moolenaar0d660222005-01-07 21:51:51 +000015606 static void
15607f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015608 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015609 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015611#ifdef FEAT_CLIENTSERVER
15612 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015613 char_u *server = get_tv_string_chk(&argvars[0]);
15614 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615
Bram Moolenaar0d660222005-01-07 21:51:51 +000015616 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015617 if (server == NULL || reply == NULL)
15618 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015619 if (check_restricted() || check_secure())
15620 return;
15621# ifdef FEAT_X11
15622 if (check_connection() == FAIL)
15623 return;
15624# endif
15625
15626 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015628 EMSG(_("E258: Unable to send to client"));
15629 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015631 rettv->vval.v_number = 0;
15632#else
15633 rettv->vval.v_number = -1;
15634#endif
15635}
15636
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637 static void
15638f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015639 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015640 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015641{
15642 char_u *r = NULL;
15643
15644#ifdef FEAT_CLIENTSERVER
15645# ifdef WIN32
15646 r = serverGetVimNames();
15647# else
15648 make_connection();
15649 if (X_DISPLAY != NULL)
15650 r = serverGetVimNames(X_DISPLAY);
15651# endif
15652#endif
15653 rettv->v_type = VAR_STRING;
15654 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655}
15656
15657/*
15658 * "setbufvar()" function
15659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015661f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015662 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015663 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664{
15665 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015668 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 char_u nbuf[NUMBUFLEN];
15670
15671 if (check_restricted() || check_secure())
15672 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015673 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15674 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015675 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 varp = &argvars[2];
15677
15678 if (buf != NULL && varname != NULL && varp != NULL)
15679 {
15680 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015682
15683 if (*varname == '&')
15684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015685 long numval;
15686 char_u *strval;
15687 int error = FALSE;
15688
Bram Moolenaar071d4272004-06-13 20:20:40 +000015689 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015690 numval = get_tv_number_chk(varp, &error);
15691 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015692 if (!error && strval != NULL)
15693 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694 }
15695 else
15696 {
15697 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15698 if (bufvarname != NULL)
15699 {
15700 STRCPY(bufvarname, "b:");
15701 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015702 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703 vim_free(bufvarname);
15704 }
15705 }
15706
15707 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710}
15711
15712/*
15713 * "setcmdpos()" function
15714 */
15715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015716f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015717 typval_T *argvars;
15718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015720 int pos = (int)get_tv_number(&argvars[0]) - 1;
15721
15722 if (pos >= 0)
15723 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724}
15725
15726/*
15727 * "setline()" function
15728 */
15729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015730f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015731 typval_T *argvars;
15732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733{
15734 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015735 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015736 list_T *l = NULL;
15737 listitem_T *li = NULL;
15738 long added = 0;
15739 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015741 lnum = get_tv_lnum(&argvars[0]);
15742 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015744 l = argvars[1].vval.v_list;
15745 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015747 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015748 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015749
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015750 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015751 for (;;)
15752 {
15753 if (l != NULL)
15754 {
15755 /* list argument, get next string */
15756 if (li == NULL)
15757 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015758 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015759 li = li->li_next;
15760 }
15761
15762 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015763 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015764 break;
15765 if (lnum <= curbuf->b_ml.ml_line_count)
15766 {
15767 /* existing line, replace it */
15768 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15769 {
15770 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015771 if (lnum == curwin->w_cursor.lnum)
15772 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015773 rettv->vval.v_number = 0; /* OK */
15774 }
15775 }
15776 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15777 {
15778 /* lnum is one past the last line, append the line */
15779 ++added;
15780 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15781 rettv->vval.v_number = 0; /* OK */
15782 }
15783
15784 if (l == NULL) /* only one string argument */
15785 break;
15786 ++lnum;
15787 }
15788
15789 if (added > 0)
15790 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791}
15792
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015793static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15794
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015796 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015797 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015798 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015799set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015800 win_T *wp UNUSED;
15801 typval_T *list_arg UNUSED;
15802 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015803 typval_T *rettv;
15804{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015805#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015806 char_u *act;
15807 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015808#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015809
Bram Moolenaar2641f772005-03-25 21:58:17 +000015810 rettv->vval.v_number = -1;
15811
15812#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015813 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015814 EMSG(_(e_listreq));
15815 else
15816 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015817 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015818
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015819 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015820 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015821 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015822 if (act == NULL)
15823 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015824 if (*act == 'a' || *act == 'r')
15825 action = *act;
15826 }
15827
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015828 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015829 rettv->vval.v_number = 0;
15830 }
15831#endif
15832}
15833
15834/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015835 * "setloclist()" function
15836 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015837 static void
15838f_setloclist(argvars, rettv)
15839 typval_T *argvars;
15840 typval_T *rettv;
15841{
15842 win_T *win;
15843
15844 rettv->vval.v_number = -1;
15845
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015846 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015847 if (win != NULL)
15848 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15849}
15850
15851/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015852 * "setmatches()" function
15853 */
15854 static void
15855f_setmatches(argvars, rettv)
15856 typval_T *argvars;
15857 typval_T *rettv;
15858{
15859#ifdef FEAT_SEARCH_EXTRA
15860 list_T *l;
15861 listitem_T *li;
15862 dict_T *d;
15863
15864 rettv->vval.v_number = -1;
15865 if (argvars[0].v_type != VAR_LIST)
15866 {
15867 EMSG(_(e_listreq));
15868 return;
15869 }
15870 if ((l = argvars[0].vval.v_list) != NULL)
15871 {
15872
15873 /* To some extent make sure that we are dealing with a list from
15874 * "getmatches()". */
15875 li = l->lv_first;
15876 while (li != NULL)
15877 {
15878 if (li->li_tv.v_type != VAR_DICT
15879 || (d = li->li_tv.vval.v_dict) == NULL)
15880 {
15881 EMSG(_(e_invarg));
15882 return;
15883 }
15884 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15885 && dict_find(d, (char_u *)"pattern", -1) != NULL
15886 && dict_find(d, (char_u *)"priority", -1) != NULL
15887 && dict_find(d, (char_u *)"id", -1) != NULL))
15888 {
15889 EMSG(_(e_invarg));
15890 return;
15891 }
15892 li = li->li_next;
15893 }
15894
15895 clear_matches(curwin);
15896 li = l->lv_first;
15897 while (li != NULL)
15898 {
15899 d = li->li_tv.vval.v_dict;
15900 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15901 get_dict_string(d, (char_u *)"pattern", FALSE),
15902 (int)get_dict_number(d, (char_u *)"priority"),
15903 (int)get_dict_number(d, (char_u *)"id"));
15904 li = li->li_next;
15905 }
15906 rettv->vval.v_number = 0;
15907 }
15908#endif
15909}
15910
15911/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015912 * "setpos()" function
15913 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015914 static void
15915f_setpos(argvars, rettv)
15916 typval_T *argvars;
15917 typval_T *rettv;
15918{
15919 pos_T pos;
15920 int fnum;
15921 char_u *name;
15922
Bram Moolenaar08250432008-02-13 11:42:46 +000015923 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015924 name = get_tv_string_chk(argvars);
15925 if (name != NULL)
15926 {
15927 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15928 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015929 if (--pos.col < 0)
15930 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015931 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015932 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015933 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015934 if (fnum == curbuf->b_fnum)
15935 {
15936 curwin->w_cursor = pos;
15937 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015938 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015939 }
15940 else
15941 EMSG(_(e_invarg));
15942 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015943 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15944 {
15945 /* set mark */
15946 if (setmark_pos(name[1], &pos, fnum) == OK)
15947 rettv->vval.v_number = 0;
15948 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015949 else
15950 EMSG(_(e_invarg));
15951 }
15952 }
15953}
15954
15955/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015956 * "setqflist()" function
15957 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015958 static void
15959f_setqflist(argvars, rettv)
15960 typval_T *argvars;
15961 typval_T *rettv;
15962{
15963 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
15964}
15965
15966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015967 * "setreg()" function
15968 */
15969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015970f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015971 typval_T *argvars;
15972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973{
15974 int regname;
15975 char_u *strregname;
15976 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015977 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978 int append;
15979 char_u yank_type;
15980 long block_len;
15981
15982 block_len = -1;
15983 yank_type = MAUTO;
15984 append = FALSE;
15985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015986 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015987 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015988
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015989 if (strregname == NULL)
15990 return; /* type error; errmsg already given */
15991 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 if (regname == 0 || regname == '@')
15993 regname = '"';
15994 else if (regname == '=')
15995 return;
15996
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015997 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015999 stropt = get_tv_string_chk(&argvars[2]);
16000 if (stropt == NULL)
16001 return; /* type error */
16002 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 switch (*stropt)
16004 {
16005 case 'a': case 'A': /* append */
16006 append = TRUE;
16007 break;
16008 case 'v': case 'c': /* character-wise selection */
16009 yank_type = MCHAR;
16010 break;
16011 case 'V': case 'l': /* line-wise selection */
16012 yank_type = MLINE;
16013 break;
16014#ifdef FEAT_VISUAL
16015 case 'b': case Ctrl_V: /* block-wise selection */
16016 yank_type = MBLOCK;
16017 if (VIM_ISDIGIT(stropt[1]))
16018 {
16019 ++stropt;
16020 block_len = getdigits(&stropt) - 1;
16021 --stropt;
16022 }
16023 break;
16024#endif
16025 }
16026 }
16027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 strval = get_tv_string_chk(&argvars[1]);
16029 if (strval != NULL)
16030 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016032 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033}
16034
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016035/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016036 * "settabvar()" function
16037 */
16038 static void
16039f_settabvar(argvars, rettv)
16040 typval_T *argvars;
16041 typval_T *rettv;
16042{
16043 tabpage_T *save_curtab;
16044 char_u *varname, *tabvarname;
16045 typval_T *varp;
16046 tabpage_T *tp;
16047
16048 rettv->vval.v_number = 0;
16049
16050 if (check_restricted() || check_secure())
16051 return;
16052
16053 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16054 varname = get_tv_string_chk(&argvars[1]);
16055 varp = &argvars[2];
16056
16057 if (tp != NULL && varname != NULL && varp != NULL)
16058 {
16059 save_curtab = curtab;
16060 goto_tabpage_tp(tp);
16061
16062 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16063 if (tabvarname != NULL)
16064 {
16065 STRCPY(tabvarname, "t:");
16066 STRCPY(tabvarname + 2, varname);
16067 set_var(tabvarname, varp, TRUE);
16068 vim_free(tabvarname);
16069 }
16070
16071 /* Restore current tabpage */
16072 if (valid_tabpage(save_curtab))
16073 goto_tabpage_tp(save_curtab);
16074 }
16075}
16076
16077/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016078 * "settabwinvar()" function
16079 */
16080 static void
16081f_settabwinvar(argvars, rettv)
16082 typval_T *argvars;
16083 typval_T *rettv;
16084{
16085 setwinvar(argvars, rettv, 1);
16086}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016087
16088/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016089 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016092f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016093 typval_T *argvars;
16094 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016096 setwinvar(argvars, rettv, 0);
16097}
16098
16099/*
16100 * "setwinvar()" and "settabwinvar()" functions
16101 */
16102 static void
16103setwinvar(argvars, rettv, off)
16104 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016105 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016106 int off;
16107{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 win_T *win;
16109#ifdef FEAT_WINDOWS
16110 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016111 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016112#endif
16113 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016114 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016116 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117
16118 if (check_restricted() || check_secure())
16119 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016120
16121#ifdef FEAT_WINDOWS
16122 if (off == 1)
16123 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16124 else
16125 tp = curtab;
16126#endif
16127 win = find_win_by_nr(&argvars[off], tp);
16128 varname = get_tv_string_chk(&argvars[off + 1]);
16129 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130
16131 if (win != NULL && varname != NULL && varp != NULL)
16132 {
16133#ifdef FEAT_WINDOWS
16134 /* set curwin to be our win, temporarily */
16135 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016136 save_curtab = curtab;
16137 goto_tabpage_tp(tp);
16138 if (!win_valid(win))
16139 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 curwin = win;
16141 curbuf = curwin->w_buffer;
16142#endif
16143
16144 if (*varname == '&')
16145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016146 long numval;
16147 char_u *strval;
16148 int error = FALSE;
16149
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016151 numval = get_tv_number_chk(varp, &error);
16152 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016153 if (!error && strval != NULL)
16154 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016155 }
16156 else
16157 {
16158 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16159 if (winvarname != NULL)
16160 {
16161 STRCPY(winvarname, "w:");
16162 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016163 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016164 vim_free(winvarname);
16165 }
16166 }
16167
16168#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016169 /* Restore current tabpage and window, if still valid (autocomands can
16170 * make them invalid). */
16171 if (valid_tabpage(save_curtab))
16172 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 if (win_valid(save_curwin))
16174 {
16175 curwin = save_curwin;
16176 curbuf = curwin->w_buffer;
16177 }
16178#endif
16179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180}
16181
16182/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016183 * "shellescape({string})" function
16184 */
16185 static void
16186f_shellescape(argvars, rettv)
16187 typval_T *argvars;
16188 typval_T *rettv;
16189{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016190 rettv->vval.v_string = vim_strsave_shellescape(
16191 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016192 rettv->v_type = VAR_STRING;
16193}
16194
16195/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016196 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 */
16198 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016199f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016200 typval_T *argvars;
16201 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016203 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204
Bram Moolenaar0d660222005-01-07 21:51:51 +000016205 p = get_tv_string(&argvars[0]);
16206 rettv->vval.v_string = vim_strsave(p);
16207 simplify_filename(rettv->vval.v_string); /* simplify in place */
16208 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016209}
16210
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016211#ifdef FEAT_FLOAT
16212/*
16213 * "sin()" function
16214 */
16215 static void
16216f_sin(argvars, rettv)
16217 typval_T *argvars;
16218 typval_T *rettv;
16219{
16220 float_T f;
16221
16222 rettv->v_type = VAR_FLOAT;
16223 if (get_float_arg(argvars, &f) == OK)
16224 rettv->vval.v_float = sin(f);
16225 else
16226 rettv->vval.v_float = 0.0;
16227}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016228
16229/*
16230 * "sinh()" function
16231 */
16232 static void
16233f_sinh(argvars, rettv)
16234 typval_T *argvars;
16235 typval_T *rettv;
16236{
16237 float_T f;
16238
16239 rettv->v_type = VAR_FLOAT;
16240 if (get_float_arg(argvars, &f) == OK)
16241 rettv->vval.v_float = sinh(f);
16242 else
16243 rettv->vval.v_float = 0.0;
16244}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016245#endif
16246
Bram Moolenaar0d660222005-01-07 21:51:51 +000016247static int
16248#ifdef __BORLANDC__
16249 _RTLENTRYF
16250#endif
16251 item_compare __ARGS((const void *s1, const void *s2));
16252static int
16253#ifdef __BORLANDC__
16254 _RTLENTRYF
16255#endif
16256 item_compare2 __ARGS((const void *s1, const void *s2));
16257
16258static int item_compare_ic;
16259static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016260static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261#define ITEM_COMPARE_FAIL 999
16262
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016265 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016266 static int
16267#ifdef __BORLANDC__
16268_RTLENTRYF
16269#endif
16270item_compare(s1, s2)
16271 const void *s1;
16272 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016273{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016274 char_u *p1, *p2;
16275 char_u *tofree1, *tofree2;
16276 int res;
16277 char_u numbuf1[NUMBUFLEN];
16278 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016280 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16281 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016282 if (p1 == NULL)
16283 p1 = (char_u *)"";
16284 if (p2 == NULL)
16285 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286 if (item_compare_ic)
16287 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016289 res = STRCMP(p1, p2);
16290 vim_free(tofree1);
16291 vim_free(tofree2);
16292 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293}
16294
16295 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016296#ifdef __BORLANDC__
16297_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016299item_compare2(s1, s2)
16300 const void *s1;
16301 const void *s2;
16302{
16303 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016304 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016305 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016306 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016308 /* shortcut after failure in previous call; compare all items equal */
16309 if (item_compare_func_err)
16310 return 0;
16311
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016312 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16313 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016314 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16315 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316
16317 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016318 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016319 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016320 clear_tv(&argv[0]);
16321 clear_tv(&argv[1]);
16322
16323 if (res == FAIL)
16324 res = ITEM_COMPARE_FAIL;
16325 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016326 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16327 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016328 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016329 clear_tv(&rettv);
16330 return res;
16331}
16332
16333/*
16334 * "sort({list})" function
16335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016337f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016338 typval_T *argvars;
16339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340{
Bram Moolenaar33570922005-01-25 22:26:29 +000016341 list_T *l;
16342 listitem_T *li;
16343 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344 long len;
16345 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346
Bram Moolenaar0d660222005-01-07 21:51:51 +000016347 if (argvars[0].v_type != VAR_LIST)
16348 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349 else
16350 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016352 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353 return;
16354 rettv->vval.v_list = l;
16355 rettv->v_type = VAR_LIST;
16356 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357
Bram Moolenaar0d660222005-01-07 21:51:51 +000016358 len = list_len(l);
16359 if (len <= 1)
16360 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361
Bram Moolenaar0d660222005-01-07 21:51:51 +000016362 item_compare_ic = FALSE;
16363 item_compare_func = NULL;
16364 if (argvars[1].v_type != VAR_UNKNOWN)
16365 {
16366 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016367 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016368 else
16369 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016370 int error = FALSE;
16371
16372 i = get_tv_number_chk(&argvars[1], &error);
16373 if (error)
16374 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016375 if (i == 1)
16376 item_compare_ic = TRUE;
16377 else
16378 item_compare_func = get_tv_string(&argvars[1]);
16379 }
16380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016383 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016384 if (ptrs == NULL)
16385 return;
16386 i = 0;
16387 for (li = l->lv_first; li != NULL; li = li->li_next)
16388 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016390 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016391 /* test the compare function */
16392 if (item_compare_func != NULL
16393 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16394 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016395 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397 {
16398 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016399 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400 item_compare_func == NULL ? item_compare : item_compare2);
16401
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016402 if (!item_compare_func_err)
16403 {
16404 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016405 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406 l->lv_len = 0;
16407 for (i = 0; i < len; ++i)
16408 list_append(l, ptrs[i]);
16409 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410 }
16411
16412 vim_free(ptrs);
16413 }
16414}
16415
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016416/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016417 * "soundfold({word})" function
16418 */
16419 static void
16420f_soundfold(argvars, rettv)
16421 typval_T *argvars;
16422 typval_T *rettv;
16423{
16424 char_u *s;
16425
16426 rettv->v_type = VAR_STRING;
16427 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016428#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016429 rettv->vval.v_string = eval_soundfold(s);
16430#else
16431 rettv->vval.v_string = vim_strsave(s);
16432#endif
16433}
16434
16435/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016436 * "spellbadword()" function
16437 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016438 static void
16439f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016440 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016441 typval_T *rettv;
16442{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016443 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016444 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016445 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016446
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016447 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016448 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016449
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016450#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016451 if (argvars[0].v_type == VAR_UNKNOWN)
16452 {
16453 /* Find the start and length of the badly spelled word. */
16454 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16455 if (len != 0)
16456 word = ml_get_cursor();
16457 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016458 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016459 {
16460 char_u *str = get_tv_string_chk(&argvars[0]);
16461 int capcol = -1;
16462
16463 if (str != NULL)
16464 {
16465 /* Check the argument for spelling. */
16466 while (*str != NUL)
16467 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016468 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016469 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016470 {
16471 word = str;
16472 break;
16473 }
16474 str += len;
16475 }
16476 }
16477 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016478#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016479
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016480 list_append_string(rettv->vval.v_list, word, len);
16481 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016482 attr == HLF_SPB ? "bad" :
16483 attr == HLF_SPR ? "rare" :
16484 attr == HLF_SPL ? "local" :
16485 attr == HLF_SPC ? "caps" :
16486 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016487}
16488
16489/*
16490 * "spellsuggest()" function
16491 */
16492 static void
16493f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016494 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016495 typval_T *rettv;
16496{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016497#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016498 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016499 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016500 int maxcount;
16501 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016502 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016503 listitem_T *li;
16504 int need_capital = FALSE;
16505#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016506
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016507 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016508 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016509
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016510#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016511 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016512 {
16513 str = get_tv_string(&argvars[0]);
16514 if (argvars[1].v_type != VAR_UNKNOWN)
16515 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016516 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016517 if (maxcount <= 0)
16518 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016519 if (argvars[2].v_type != VAR_UNKNOWN)
16520 {
16521 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16522 if (typeerr)
16523 return;
16524 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016525 }
16526 else
16527 maxcount = 25;
16528
Bram Moolenaar4770d092006-01-12 23:22:24 +000016529 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016530
16531 for (i = 0; i < ga.ga_len; ++i)
16532 {
16533 str = ((char_u **)ga.ga_data)[i];
16534
16535 li = listitem_alloc();
16536 if (li == NULL)
16537 vim_free(str);
16538 else
16539 {
16540 li->li_tv.v_type = VAR_STRING;
16541 li->li_tv.v_lock = 0;
16542 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016543 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016544 }
16545 }
16546 ga_clear(&ga);
16547 }
16548#endif
16549}
16550
Bram Moolenaar0d660222005-01-07 21:51:51 +000016551 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016552f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016553 typval_T *argvars;
16554 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016555{
16556 char_u *str;
16557 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016558 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016559 regmatch_T regmatch;
16560 char_u patbuf[NUMBUFLEN];
16561 char_u *save_cpo;
16562 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016563 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016564 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016565 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016566
16567 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16568 save_cpo = p_cpo;
16569 p_cpo = (char_u *)"";
16570
16571 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016572 if (argvars[1].v_type != VAR_UNKNOWN)
16573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016574 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16575 if (pat == NULL)
16576 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016577 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016578 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016579 }
16580 if (pat == NULL || *pat == NUL)
16581 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016582
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016583 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016584 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016585 if (typeerr)
16586 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587
Bram Moolenaar0d660222005-01-07 21:51:51 +000016588 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16589 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016590 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016591 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016592 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016593 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016594 if (*str == NUL)
16595 match = FALSE; /* empty item at the end */
16596 else
16597 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598 if (match)
16599 end = regmatch.startp[0];
16600 else
16601 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016602 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16603 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016604 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016605 if (list_append_string(rettv->vval.v_list, str,
16606 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016607 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016608 }
16609 if (!match)
16610 break;
16611 /* Advance to just after the match. */
16612 if (regmatch.endp[0] > str)
16613 col = 0;
16614 else
16615 {
16616 /* Don't get stuck at the same match. */
16617#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016618 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016619#else
16620 col = 1;
16621#endif
16622 }
16623 str = regmatch.endp[0];
16624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016625
Bram Moolenaar0d660222005-01-07 21:51:51 +000016626 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016628
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630}
16631
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016632#ifdef FEAT_FLOAT
16633/*
16634 * "sqrt()" function
16635 */
16636 static void
16637f_sqrt(argvars, rettv)
16638 typval_T *argvars;
16639 typval_T *rettv;
16640{
16641 float_T f;
16642
16643 rettv->v_type = VAR_FLOAT;
16644 if (get_float_arg(argvars, &f) == OK)
16645 rettv->vval.v_float = sqrt(f);
16646 else
16647 rettv->vval.v_float = 0.0;
16648}
16649
16650/*
16651 * "str2float()" function
16652 */
16653 static void
16654f_str2float(argvars, rettv)
16655 typval_T *argvars;
16656 typval_T *rettv;
16657{
16658 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16659
16660 if (*p == '+')
16661 p = skipwhite(p + 1);
16662 (void)string2float(p, &rettv->vval.v_float);
16663 rettv->v_type = VAR_FLOAT;
16664}
16665#endif
16666
Bram Moolenaar2c932302006-03-18 21:42:09 +000016667/*
16668 * "str2nr()" function
16669 */
16670 static void
16671f_str2nr(argvars, rettv)
16672 typval_T *argvars;
16673 typval_T *rettv;
16674{
16675 int base = 10;
16676 char_u *p;
16677 long n;
16678
16679 if (argvars[1].v_type != VAR_UNKNOWN)
16680 {
16681 base = get_tv_number(&argvars[1]);
16682 if (base != 8 && base != 10 && base != 16)
16683 {
16684 EMSG(_(e_invarg));
16685 return;
16686 }
16687 }
16688
16689 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016690 if (*p == '+')
16691 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016692 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16693 rettv->vval.v_number = n;
16694}
16695
Bram Moolenaar071d4272004-06-13 20:20:40 +000016696#ifdef HAVE_STRFTIME
16697/*
16698 * "strftime({format}[, {time}])" function
16699 */
16700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016701f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016702 typval_T *argvars;
16703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016704{
16705 char_u result_buf[256];
16706 struct tm *curtime;
16707 time_t seconds;
16708 char_u *p;
16709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016710 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016712 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016713 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 seconds = time(NULL);
16715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016716 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016717 curtime = localtime(&seconds);
16718 /* MSVC returns NULL for an invalid value of seconds. */
16719 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016720 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 else
16722 {
16723# ifdef FEAT_MBYTE
16724 vimconv_T conv;
16725 char_u *enc;
16726
16727 conv.vc_type = CONV_NONE;
16728 enc = enc_locale();
16729 convert_setup(&conv, p_enc, enc);
16730 if (conv.vc_type != CONV_NONE)
16731 p = string_convert(&conv, p, NULL);
16732# endif
16733 if (p != NULL)
16734 (void)strftime((char *)result_buf, sizeof(result_buf),
16735 (char *)p, curtime);
16736 else
16737 result_buf[0] = NUL;
16738
16739# ifdef FEAT_MBYTE
16740 if (conv.vc_type != CONV_NONE)
16741 vim_free(p);
16742 convert_setup(&conv, enc, p_enc);
16743 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016744 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 else
16746# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016747 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748
16749# ifdef FEAT_MBYTE
16750 /* Release conversion descriptors */
16751 convert_setup(&conv, NULL, NULL);
16752 vim_free(enc);
16753# endif
16754 }
16755}
16756#endif
16757
16758/*
16759 * "stridx()" function
16760 */
16761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016762f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016763 typval_T *argvars;
16764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016765{
16766 char_u buf[NUMBUFLEN];
16767 char_u *needle;
16768 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016769 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016771 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016773 needle = get_tv_string_chk(&argvars[1]);
16774 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016775 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016776 if (needle == NULL || haystack == NULL)
16777 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778
Bram Moolenaar33570922005-01-25 22:26:29 +000016779 if (argvars[2].v_type != VAR_UNKNOWN)
16780 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016781 int error = FALSE;
16782
16783 start_idx = get_tv_number_chk(&argvars[2], &error);
16784 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016785 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016786 if (start_idx >= 0)
16787 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016788 }
16789
16790 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16791 if (pos != NULL)
16792 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016793}
16794
16795/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016796 * "string()" function
16797 */
16798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016799f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016800 typval_T *argvars;
16801 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016802{
16803 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016804 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016805
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016806 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016807 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016808 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016809 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016810 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811}
16812
16813/*
16814 * "strlen()" function
16815 */
16816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016817f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016818 typval_T *argvars;
16819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016821 rettv->vval.v_number = (varnumber_T)(STRLEN(
16822 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823}
16824
16825/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016826 * "strchars()" function
16827 */
16828 static void
16829f_strchars(argvars, rettv)
16830 typval_T *argvars;
16831 typval_T *rettv;
16832{
16833 char_u *s = get_tv_string(&argvars[0]);
16834#ifdef FEAT_MBYTE
16835 varnumber_T len = 0;
16836
16837 while (*s != NUL)
16838 {
16839 mb_cptr2char_adv(&s);
16840 ++len;
16841 }
16842 rettv->vval.v_number = len;
16843#else
16844 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16845#endif
16846}
16847
16848/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016849 * "strdisplaywidth()" function
16850 */
16851 static void
16852f_strdisplaywidth(argvars, rettv)
16853 typval_T *argvars;
16854 typval_T *rettv;
16855{
16856 char_u *s = get_tv_string(&argvars[0]);
16857 int col = 0;
16858
16859 if (argvars[1].v_type != VAR_UNKNOWN)
16860 col = get_tv_number(&argvars[1]);
16861
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016862 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016863}
16864
16865/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016866 * "strwidth()" function
16867 */
16868 static void
16869f_strwidth(argvars, rettv)
16870 typval_T *argvars;
16871 typval_T *rettv;
16872{
16873 char_u *s = get_tv_string(&argvars[0]);
16874
16875 rettv->vval.v_number = (varnumber_T)(
16876#ifdef FEAT_MBYTE
16877 mb_string2cells(s, -1)
16878#else
16879 STRLEN(s)
16880#endif
16881 );
16882}
16883
16884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885 * "strpart()" function
16886 */
16887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016888f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016889 typval_T *argvars;
16890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016891{
16892 char_u *p;
16893 int n;
16894 int len;
16895 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016896 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016898 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899 slen = (int)STRLEN(p);
16900
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016901 n = get_tv_number_chk(&argvars[1], &error);
16902 if (error)
16903 len = 0;
16904 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016905 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 else
16907 len = slen - n; /* default len: all bytes that are available. */
16908
16909 /*
16910 * Only return the overlap between the specified part and the actual
16911 * string.
16912 */
16913 if (n < 0)
16914 {
16915 len += n;
16916 n = 0;
16917 }
16918 else if (n > slen)
16919 n = slen;
16920 if (len < 0)
16921 len = 0;
16922 else if (n + len > slen)
16923 len = slen - n;
16924
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016925 rettv->v_type = VAR_STRING;
16926 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927}
16928
16929/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930 * "strridx()" function
16931 */
16932 static void
16933f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016934 typval_T *argvars;
16935 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936{
16937 char_u buf[NUMBUFLEN];
16938 char_u *needle;
16939 char_u *haystack;
16940 char_u *rest;
16941 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016942 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016944 needle = get_tv_string_chk(&argvars[1]);
16945 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016946
16947 rettv->vval.v_number = -1;
16948 if (needle == NULL || haystack == NULL)
16949 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016950
16951 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016952 if (argvars[2].v_type != VAR_UNKNOWN)
16953 {
16954 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016955 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016956 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016957 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016958 }
16959 else
16960 end_idx = haystack_len;
16961
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000016963 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016965 lastmatch = haystack + end_idx;
16966 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000016968 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969 for (rest = haystack; *rest != '\0'; ++rest)
16970 {
16971 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000016972 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 break;
16974 lastmatch = rest;
16975 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000016976 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977
16978 if (lastmatch == NULL)
16979 rettv->vval.v_number = -1;
16980 else
16981 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
16982}
16983
16984/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016985 * "strtrans()" function
16986 */
16987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016988f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016989 typval_T *argvars;
16990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016992 rettv->v_type = VAR_STRING;
16993 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016994}
16995
16996/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 * "submatch()" function
16998 */
16999 static void
17000f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017001 typval_T *argvars;
17002 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017003{
17004 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017005 rettv->vval.v_string =
17006 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007}
17008
17009/*
17010 * "substitute()" function
17011 */
17012 static void
17013f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017014 typval_T *argvars;
17015 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017016{
17017 char_u patbuf[NUMBUFLEN];
17018 char_u subbuf[NUMBUFLEN];
17019 char_u flagsbuf[NUMBUFLEN];
17020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 char_u *str = get_tv_string_chk(&argvars[0]);
17022 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17023 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17024 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17025
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017027 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17028 rettv->vval.v_string = NULL;
17029 else
17030 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031}
17032
17033/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017034 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017037f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017038 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017039 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040{
17041 int id = 0;
17042#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017043 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 long col;
17045 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017046 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017048 lnum = get_tv_lnum(argvars); /* -1 on type error */
17049 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17050 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017052 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017053 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017054 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017055#endif
17056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017057 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058}
17059
17060/*
17061 * "synIDattr(id, what [, mode])" function
17062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017064f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017065 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017066 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067{
17068 char_u *p = NULL;
17069#ifdef FEAT_SYN_HL
17070 int id;
17071 char_u *what;
17072 char_u *mode;
17073 char_u modebuf[NUMBUFLEN];
17074 int modec;
17075
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017076 id = get_tv_number(&argvars[0]);
17077 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017078 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017080 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017082 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 modec = 0; /* replace invalid with current */
17084 }
17085 else
17086 {
17087#ifdef FEAT_GUI
17088 if (gui.in_use)
17089 modec = 'g';
17090 else
17091#endif
17092 if (t_colors > 1)
17093 modec = 'c';
17094 else
17095 modec = 't';
17096 }
17097
17098
17099 switch (TOLOWER_ASC(what[0]))
17100 {
17101 case 'b':
17102 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17103 p = highlight_color(id, what, modec);
17104 else /* bold */
17105 p = highlight_has_attr(id, HL_BOLD, modec);
17106 break;
17107
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017108 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109 p = highlight_color(id, what, modec);
17110 break;
17111
17112 case 'i':
17113 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17114 p = highlight_has_attr(id, HL_INVERSE, modec);
17115 else /* italic */
17116 p = highlight_has_attr(id, HL_ITALIC, modec);
17117 break;
17118
17119 case 'n': /* name */
17120 p = get_highlight_name(NULL, id - 1);
17121 break;
17122
17123 case 'r': /* reverse */
17124 p = highlight_has_attr(id, HL_INVERSE, modec);
17125 break;
17126
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017127 case 's':
17128 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17129 p = highlight_color(id, what, modec);
17130 else /* standout */
17131 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132 break;
17133
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017134 case 'u':
17135 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17136 /* underline */
17137 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17138 else
17139 /* undercurl */
17140 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 break;
17142 }
17143
17144 if (p != NULL)
17145 p = vim_strsave(p);
17146#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017147 rettv->v_type = VAR_STRING;
17148 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149}
17150
17151/*
17152 * "synIDtrans(id)" function
17153 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017155f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017156 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017158{
17159 int id;
17160
17161#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017162 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163
17164 if (id > 0)
17165 id = syn_get_final_id(id);
17166 else
17167#endif
17168 id = 0;
17169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017170 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171}
17172
17173/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017174 * "synconcealed(lnum, col)" function
17175 */
17176 static void
17177f_synconcealed(argvars, rettv)
17178 typval_T *argvars UNUSED;
17179 typval_T *rettv;
17180{
17181#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17182 long lnum;
17183 long col;
17184 int syntax_flags = 0;
17185 int cchar;
17186 int matchid = 0;
17187 char_u str[NUMBUFLEN];
17188#endif
17189
17190 rettv->v_type = VAR_LIST;
17191 rettv->vval.v_list = NULL;
17192
17193#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17194 lnum = get_tv_lnum(argvars); /* -1 on type error */
17195 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17196
17197 vim_memset(str, NUL, sizeof(str));
17198
17199 if (rettv_list_alloc(rettv) != FAIL)
17200 {
17201 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17202 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17203 && curwin->w_p_cole > 0)
17204 {
17205 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17206 syntax_flags = get_syntax_info(&matchid);
17207
17208 /* get the conceal character */
17209 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17210 {
17211 cchar = syn_get_sub_char();
17212 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17213 cchar = lcs_conceal;
17214 if (cchar != NUL)
17215 {
17216# ifdef FEAT_MBYTE
17217 if (has_mbyte)
17218 (*mb_char2bytes)(cchar, str);
17219 else
17220# endif
17221 str[0] = cchar;
17222 }
17223 }
17224 }
17225
17226 list_append_number(rettv->vval.v_list,
17227 (syntax_flags & HL_CONCEAL) != 0);
17228 /* -1 to auto-determine strlen */
17229 list_append_string(rettv->vval.v_list, str, -1);
17230 list_append_number(rettv->vval.v_list, matchid);
17231 }
17232#endif
17233}
17234
17235/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017236 * "synstack(lnum, col)" function
17237 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017238 static void
17239f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017240 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017241 typval_T *rettv;
17242{
17243#ifdef FEAT_SYN_HL
17244 long lnum;
17245 long col;
17246 int i;
17247 int id;
17248#endif
17249
17250 rettv->v_type = VAR_LIST;
17251 rettv->vval.v_list = NULL;
17252
17253#ifdef FEAT_SYN_HL
17254 lnum = get_tv_lnum(argvars); /* -1 on type error */
17255 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17256
17257 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017258 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017259 && rettv_list_alloc(rettv) != FAIL)
17260 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017261 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017262 for (i = 0; ; ++i)
17263 {
17264 id = syn_get_stack_item(i);
17265 if (id < 0)
17266 break;
17267 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17268 break;
17269 }
17270 }
17271#endif
17272}
17273
17274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275 * "system()" function
17276 */
17277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017279 typval_T *argvars;
17280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017282 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017284 char_u *infile = NULL;
17285 char_u buf[NUMBUFLEN];
17286 int err = FALSE;
17287 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017288
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017289 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017290 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017291
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017292 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017293 {
17294 /*
17295 * Write the string to a temp file, to be used for input of the shell
17296 * command.
17297 */
17298 if ((infile = vim_tempname('i')) == NULL)
17299 {
17300 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017301 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017302 }
17303
17304 fd = mch_fopen((char *)infile, WRITEBIN);
17305 if (fd == NULL)
17306 {
17307 EMSG2(_(e_notopen), infile);
17308 goto done;
17309 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017310 p = get_tv_string_buf_chk(&argvars[1], buf);
17311 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017312 {
17313 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017314 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017315 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017316 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17317 err = TRUE;
17318 if (fclose(fd) != 0)
17319 err = TRUE;
17320 if (err)
17321 {
17322 EMSG(_("E677: Error writing temp file"));
17323 goto done;
17324 }
17325 }
17326
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017327 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17328 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017329
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330#ifdef USE_CR
17331 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017332 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 {
17334 char_u *s;
17335
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017336 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 {
17338 if (*s == CAR)
17339 *s = NL;
17340 }
17341 }
17342#else
17343# ifdef USE_CRNL
17344 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017345 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 {
17347 char_u *s, *d;
17348
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017349 d = res;
17350 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 {
17352 if (s[0] == CAR && s[1] == NL)
17353 ++s;
17354 *d++ = *s;
17355 }
17356 *d = NUL;
17357 }
17358# endif
17359#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017360
17361done:
17362 if (infile != NULL)
17363 {
17364 mch_remove(infile);
17365 vim_free(infile);
17366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017367 rettv->v_type = VAR_STRING;
17368 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369}
17370
17371/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017372 * "tabpagebuflist()" function
17373 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017374 static void
17375f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017376 typval_T *argvars UNUSED;
17377 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017378{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017379#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017380 tabpage_T *tp;
17381 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017382
17383 if (argvars[0].v_type == VAR_UNKNOWN)
17384 wp = firstwin;
17385 else
17386 {
17387 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17388 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017389 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017390 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017391 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017392 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017393 for (; wp != NULL; wp = wp->w_next)
17394 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017395 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017396 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017397 }
17398#endif
17399}
17400
17401
17402/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017403 * "tabpagenr()" function
17404 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017405 static void
17406f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017407 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017408 typval_T *rettv;
17409{
17410 int nr = 1;
17411#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017412 char_u *arg;
17413
17414 if (argvars[0].v_type != VAR_UNKNOWN)
17415 {
17416 arg = get_tv_string_chk(&argvars[0]);
17417 nr = 0;
17418 if (arg != NULL)
17419 {
17420 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017421 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017422 else
17423 EMSG2(_(e_invexpr2), arg);
17424 }
17425 }
17426 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017427 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017428#endif
17429 rettv->vval.v_number = nr;
17430}
17431
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017432
17433#ifdef FEAT_WINDOWS
17434static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17435
17436/*
17437 * Common code for tabpagewinnr() and winnr().
17438 */
17439 static int
17440get_winnr(tp, argvar)
17441 tabpage_T *tp;
17442 typval_T *argvar;
17443{
17444 win_T *twin;
17445 int nr = 1;
17446 win_T *wp;
17447 char_u *arg;
17448
17449 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17450 if (argvar->v_type != VAR_UNKNOWN)
17451 {
17452 arg = get_tv_string_chk(argvar);
17453 if (arg == NULL)
17454 nr = 0; /* type error; errmsg already given */
17455 else if (STRCMP(arg, "$") == 0)
17456 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17457 else if (STRCMP(arg, "#") == 0)
17458 {
17459 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17460 if (twin == NULL)
17461 nr = 0;
17462 }
17463 else
17464 {
17465 EMSG2(_(e_invexpr2), arg);
17466 nr = 0;
17467 }
17468 }
17469
17470 if (nr > 0)
17471 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17472 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017473 {
17474 if (wp == NULL)
17475 {
17476 /* didn't find it in this tabpage */
17477 nr = 0;
17478 break;
17479 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017480 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017481 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017482 return nr;
17483}
17484#endif
17485
17486/*
17487 * "tabpagewinnr()" function
17488 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017489 static void
17490f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017491 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017492 typval_T *rettv;
17493{
17494 int nr = 1;
17495#ifdef FEAT_WINDOWS
17496 tabpage_T *tp;
17497
17498 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17499 if (tp == NULL)
17500 nr = 0;
17501 else
17502 nr = get_winnr(tp, &argvars[1]);
17503#endif
17504 rettv->vval.v_number = nr;
17505}
17506
17507
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017508/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017509 * "tagfiles()" function
17510 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017511 static void
17512f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017513 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017514 typval_T *rettv;
17515{
17516 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017517 tagname_T tn;
17518 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017519
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017520 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017521 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017522
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017523 for (first = TRUE; ; first = FALSE)
17524 if (get_tagfname(&tn, first, fname) == FAIL
17525 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017526 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017527 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017528}
17529
17530/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017531 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017532 */
17533 static void
17534f_taglist(argvars, rettv)
17535 typval_T *argvars;
17536 typval_T *rettv;
17537{
17538 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017539
17540 tag_pattern = get_tv_string(&argvars[0]);
17541
17542 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017543 if (*tag_pattern == NUL)
17544 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017545
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017546 if (rettv_list_alloc(rettv) == OK)
17547 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017548}
17549
17550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551 * "tempname()" function
17552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017554f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017555 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557{
17558 static int x = 'A';
17559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017560 rettv->v_type = VAR_STRING;
17561 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562
17563 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17564 * names. Skip 'I' and 'O', they are used for shell redirection. */
17565 do
17566 {
17567 if (x == 'Z')
17568 x = '0';
17569 else if (x == '9')
17570 x = 'A';
17571 else
17572 {
17573#ifdef EBCDIC
17574 if (x == 'I')
17575 x = 'J';
17576 else if (x == 'R')
17577 x = 'S';
17578 else
17579#endif
17580 ++x;
17581 }
17582 } while (x == 'I' || x == 'O');
17583}
17584
17585/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017586 * "test(list)" function: Just checking the walls...
17587 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017588 static void
17589f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017590 typval_T *argvars UNUSED;
17591 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017592{
17593 /* Used for unit testing. Change the code below to your liking. */
17594#if 0
17595 listitem_T *li;
17596 list_T *l;
17597 char_u *bad, *good;
17598
17599 if (argvars[0].v_type != VAR_LIST)
17600 return;
17601 l = argvars[0].vval.v_list;
17602 if (l == NULL)
17603 return;
17604 li = l->lv_first;
17605 if (li == NULL)
17606 return;
17607 bad = get_tv_string(&li->li_tv);
17608 li = li->li_next;
17609 if (li == NULL)
17610 return;
17611 good = get_tv_string(&li->li_tv);
17612 rettv->vval.v_number = test_edit_score(bad, good);
17613#endif
17614}
17615
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017616#ifdef FEAT_FLOAT
17617/*
17618 * "tan()" function
17619 */
17620 static void
17621f_tan(argvars, rettv)
17622 typval_T *argvars;
17623 typval_T *rettv;
17624{
17625 float_T f;
17626
17627 rettv->v_type = VAR_FLOAT;
17628 if (get_float_arg(argvars, &f) == OK)
17629 rettv->vval.v_float = tan(f);
17630 else
17631 rettv->vval.v_float = 0.0;
17632}
17633
17634/*
17635 * "tanh()" function
17636 */
17637 static void
17638f_tanh(argvars, rettv)
17639 typval_T *argvars;
17640 typval_T *rettv;
17641{
17642 float_T f;
17643
17644 rettv->v_type = VAR_FLOAT;
17645 if (get_float_arg(argvars, &f) == OK)
17646 rettv->vval.v_float = tanh(f);
17647 else
17648 rettv->vval.v_float = 0.0;
17649}
17650#endif
17651
Bram Moolenaard52d9742005-08-21 22:20:28 +000017652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 * "tolower(string)" function
17654 */
17655 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017656f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017657 typval_T *argvars;
17658 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659{
17660 char_u *p;
17661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017662 p = vim_strsave(get_tv_string(&argvars[0]));
17663 rettv->v_type = VAR_STRING;
17664 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665
17666 if (p != NULL)
17667 while (*p != NUL)
17668 {
17669#ifdef FEAT_MBYTE
17670 int l;
17671
17672 if (enc_utf8)
17673 {
17674 int c, lc;
17675
17676 c = utf_ptr2char(p);
17677 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017678 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 /* TODO: reallocate string when byte count changes. */
17680 if (utf_char2len(lc) == l)
17681 utf_char2bytes(lc, p);
17682 p += l;
17683 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017684 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 p += l; /* skip multi-byte character */
17686 else
17687#endif
17688 {
17689 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17690 ++p;
17691 }
17692 }
17693}
17694
17695/*
17696 * "toupper(string)" function
17697 */
17698 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017699f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017700 typval_T *argvars;
17701 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017703 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017704 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705}
17706
17707/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017708 * "tr(string, fromstr, tostr)" function
17709 */
17710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017711f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017712 typval_T *argvars;
17713 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017714{
17715 char_u *instr;
17716 char_u *fromstr;
17717 char_u *tostr;
17718 char_u *p;
17719#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017720 int inlen;
17721 int fromlen;
17722 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017723 int idx;
17724 char_u *cpstr;
17725 int cplen;
17726 int first = TRUE;
17727#endif
17728 char_u buf[NUMBUFLEN];
17729 char_u buf2[NUMBUFLEN];
17730 garray_T ga;
17731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017732 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017733 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17734 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017735
17736 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017737 rettv->v_type = VAR_STRING;
17738 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017739 if (fromstr == NULL || tostr == NULL)
17740 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017741 ga_init2(&ga, (int)sizeof(char), 80);
17742
17743#ifdef FEAT_MBYTE
17744 if (!has_mbyte)
17745#endif
17746 /* not multi-byte: fromstr and tostr must be the same length */
17747 if (STRLEN(fromstr) != STRLEN(tostr))
17748 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017749#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017750error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017751#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017752 EMSG2(_(e_invarg2), fromstr);
17753 ga_clear(&ga);
17754 return;
17755 }
17756
17757 /* fromstr and tostr have to contain the same number of chars */
17758 while (*instr != NUL)
17759 {
17760#ifdef FEAT_MBYTE
17761 if (has_mbyte)
17762 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017763 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017764 cpstr = instr;
17765 cplen = inlen;
17766 idx = 0;
17767 for (p = fromstr; *p != NUL; p += fromlen)
17768 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017769 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017770 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17771 {
17772 for (p = tostr; *p != NUL; p += tolen)
17773 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017774 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017775 if (idx-- == 0)
17776 {
17777 cplen = tolen;
17778 cpstr = p;
17779 break;
17780 }
17781 }
17782 if (*p == NUL) /* tostr is shorter than fromstr */
17783 goto error;
17784 break;
17785 }
17786 ++idx;
17787 }
17788
17789 if (first && cpstr == instr)
17790 {
17791 /* Check that fromstr and tostr have the same number of
17792 * (multi-byte) characters. Done only once when a character
17793 * of instr doesn't appear in fromstr. */
17794 first = FALSE;
17795 for (p = tostr; *p != NUL; p += tolen)
17796 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017797 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017798 --idx;
17799 }
17800 if (idx != 0)
17801 goto error;
17802 }
17803
17804 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017805 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017806 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017807
17808 instr += inlen;
17809 }
17810 else
17811#endif
17812 {
17813 /* When not using multi-byte chars we can do it faster. */
17814 p = vim_strchr(fromstr, *instr);
17815 if (p != NULL)
17816 ga_append(&ga, tostr[p - fromstr]);
17817 else
17818 ga_append(&ga, *instr);
17819 ++instr;
17820 }
17821 }
17822
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017823 /* add a terminating NUL */
17824 ga_grow(&ga, 1);
17825 ga_append(&ga, NUL);
17826
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017827 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017828}
17829
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017830#ifdef FEAT_FLOAT
17831/*
17832 * "trunc({float})" function
17833 */
17834 static void
17835f_trunc(argvars, rettv)
17836 typval_T *argvars;
17837 typval_T *rettv;
17838{
17839 float_T f;
17840
17841 rettv->v_type = VAR_FLOAT;
17842 if (get_float_arg(argvars, &f) == OK)
17843 /* trunc() is not in C90, use floor() or ceil() instead. */
17844 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17845 else
17846 rettv->vval.v_float = 0.0;
17847}
17848#endif
17849
Bram Moolenaar8299df92004-07-10 09:47:34 +000017850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017851 * "type(expr)" function
17852 */
17853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017854f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017855 typval_T *argvars;
17856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017858 int n;
17859
17860 switch (argvars[0].v_type)
17861 {
17862 case VAR_NUMBER: n = 0; break;
17863 case VAR_STRING: n = 1; break;
17864 case VAR_FUNC: n = 2; break;
17865 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017866 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017867#ifdef FEAT_FLOAT
17868 case VAR_FLOAT: n = 5; break;
17869#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017870 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17871 }
17872 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873}
17874
17875/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017876 * "undofile(name)" function
17877 */
17878 static void
17879f_undofile(argvars, rettv)
17880 typval_T *argvars;
17881 typval_T *rettv;
17882{
17883 rettv->v_type = VAR_STRING;
17884#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017885 {
17886 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17887
17888 if (ffname != NULL)
17889 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17890 vim_free(ffname);
17891 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017892#else
17893 rettv->vval.v_string = NULL;
17894#endif
17895}
17896
17897/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017898 * "undotree()" function
17899 */
17900 static void
17901f_undotree(argvars, rettv)
17902 typval_T *argvars UNUSED;
17903 typval_T *rettv;
17904{
17905 if (rettv_dict_alloc(rettv) == OK)
17906 {
17907 dict_T *dict = rettv->vval.v_dict;
17908 list_T *list;
17909
Bram Moolenaar730cde92010-06-27 05:18:54 +020017910 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017911 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017912 dict_add_nr_str(dict, "save_last",
17913 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017914 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17915 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017916 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017917
17918 list = list_alloc();
17919 if (list != NULL)
17920 {
17921 u_eval_tree(curbuf->b_u_oldhead, list);
17922 dict_add_list(dict, "entries", list);
17923 }
17924 }
17925}
17926
17927/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017928 * "values(dict)" function
17929 */
17930 static void
17931f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017932 typval_T *argvars;
17933 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017934{
17935 dict_list(argvars, rettv, 1);
17936}
17937
17938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017939 * "virtcol(string)" function
17940 */
17941 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017942f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017943 typval_T *argvars;
17944 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017945{
17946 colnr_T vcol = 0;
17947 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017948 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017950 fp = var2fpos(&argvars[0], FALSE, &fnum);
17951 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17952 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 {
17954 getvvcol(curwin, fp, NULL, NULL, &vcol);
17955 ++vcol;
17956 }
17957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017958 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959}
17960
17961/*
17962 * "visualmode()" function
17963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017965f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017966 typval_T *argvars UNUSED;
17967 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968{
17969#ifdef FEAT_VISUAL
17970 char_u str[2];
17971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017972 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 str[0] = curbuf->b_visual_mode_eval;
17974 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017975 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976
17977 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000017978 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980#endif
17981}
17982
17983/*
17984 * "winbufnr(nr)" function
17985 */
17986 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017987f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017988 typval_T *argvars;
17989 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990{
17991 win_T *wp;
17992
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017993 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017997 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998}
17999
18000/*
18001 * "wincol()" function
18002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018004f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018005 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007{
18008 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018009 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010}
18011
18012/*
18013 * "winheight(nr)" function
18014 */
18015 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018016f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018017 typval_T *argvars;
18018 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019{
18020 win_T *wp;
18021
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018022 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018024 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018026 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027}
18028
18029/*
18030 * "winline()" function
18031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018033f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018034 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018035 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036{
18037 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018038 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039}
18040
18041/*
18042 * "winnr()" function
18043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018045f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018046 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018047 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048{
18049 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018050
Bram Moolenaar071d4272004-06-13 20:20:40 +000018051#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018052 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018054 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055}
18056
18057/*
18058 * "winrestcmd()" function
18059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018061f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018062 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018063 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064{
18065#ifdef FEAT_WINDOWS
18066 win_T *wp;
18067 int winnr = 1;
18068 garray_T ga;
18069 char_u buf[50];
18070
18071 ga_init2(&ga, (int)sizeof(char), 70);
18072 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18073 {
18074 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18075 ga_concat(&ga, buf);
18076# ifdef FEAT_VERTSPLIT
18077 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18078 ga_concat(&ga, buf);
18079# endif
18080 ++winnr;
18081 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018082 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018084 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018086 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018088 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018089}
18090
18091/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018092 * "winrestview()" function
18093 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018094 static void
18095f_winrestview(argvars, rettv)
18096 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018097 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018098{
18099 dict_T *dict;
18100
18101 if (argvars[0].v_type != VAR_DICT
18102 || (dict = argvars[0].vval.v_dict) == NULL)
18103 EMSG(_(e_invarg));
18104 else
18105 {
18106 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18107 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18108#ifdef FEAT_VIRTUALEDIT
18109 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18110#endif
18111 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018112 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018113
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018114 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018115#ifdef FEAT_DIFF
18116 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18117#endif
18118 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18119 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18120
18121 check_cursor();
18122 changed_cline_bef_curs();
18123 invalidate_botline();
18124 redraw_later(VALID);
18125
18126 if (curwin->w_topline == 0)
18127 curwin->w_topline = 1;
18128 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18129 curwin->w_topline = curbuf->b_ml.ml_line_count;
18130#ifdef FEAT_DIFF
18131 check_topfill(curwin, TRUE);
18132#endif
18133 }
18134}
18135
18136/*
18137 * "winsaveview()" function
18138 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018139 static void
18140f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018141 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018142 typval_T *rettv;
18143{
18144 dict_T *dict;
18145
Bram Moolenaara800b422010-06-27 01:15:55 +020018146 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018147 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018148 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018149
18150 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18151 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18152#ifdef FEAT_VIRTUALEDIT
18153 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18154#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018155 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018156 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18157
18158 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18159#ifdef FEAT_DIFF
18160 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18161#endif
18162 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18163 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18164}
18165
18166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167 * "winwidth(nr)" function
18168 */
18169 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018170f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018171 typval_T *argvars;
18172 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018173{
18174 win_T *wp;
18175
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018176 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018178 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 else
18180#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018181 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018182#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018183 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184#endif
18185}
18186
Bram Moolenaar071d4272004-06-13 20:20:40 +000018187/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018188 * "writefile()" function
18189 */
18190 static void
18191f_writefile(argvars, rettv)
18192 typval_T *argvars;
18193 typval_T *rettv;
18194{
18195 int binary = FALSE;
18196 char_u *fname;
18197 FILE *fd;
18198 listitem_T *li;
18199 char_u *s;
18200 int ret = 0;
18201 int c;
18202
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018203 if (check_restricted() || check_secure())
18204 return;
18205
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018206 if (argvars[0].v_type != VAR_LIST)
18207 {
18208 EMSG2(_(e_listarg), "writefile()");
18209 return;
18210 }
18211 if (argvars[0].vval.v_list == NULL)
18212 return;
18213
18214 if (argvars[2].v_type != VAR_UNKNOWN
18215 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18216 binary = TRUE;
18217
18218 /* Always open the file in binary mode, library functions have a mind of
18219 * their own about CR-LF conversion. */
18220 fname = get_tv_string(&argvars[1]);
18221 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18222 {
18223 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18224 ret = -1;
18225 }
18226 else
18227 {
18228 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18229 li = li->li_next)
18230 {
18231 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18232 {
18233 if (*s == '\n')
18234 c = putc(NUL, fd);
18235 else
18236 c = putc(*s, fd);
18237 if (c == EOF)
18238 {
18239 ret = -1;
18240 break;
18241 }
18242 }
18243 if (!binary || li->li_next != NULL)
18244 if (putc('\n', fd) == EOF)
18245 {
18246 ret = -1;
18247 break;
18248 }
18249 if (ret < 0)
18250 {
18251 EMSG(_(e_write));
18252 break;
18253 }
18254 }
18255 fclose(fd);
18256 }
18257
18258 rettv->vval.v_number = ret;
18259}
18260
18261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018262 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018263 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 */
18265 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018266var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018267 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018268 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018269 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018271 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018273 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274
Bram Moolenaara5525202006-03-02 22:52:09 +000018275 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018276 if (varp->v_type == VAR_LIST)
18277 {
18278 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018279 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018280 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018281 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018282
18283 l = varp->vval.v_list;
18284 if (l == NULL)
18285 return NULL;
18286
18287 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018288 pos.lnum = list_find_nr(l, 0L, &error);
18289 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018290 return NULL; /* invalid line number */
18291
18292 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018293 pos.col = list_find_nr(l, 1L, &error);
18294 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018295 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018296 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018297
18298 /* We accept "$" for the column number: last column. */
18299 li = list_find(l, 1L);
18300 if (li != NULL && li->li_tv.v_type == VAR_STRING
18301 && li->li_tv.vval.v_string != NULL
18302 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18303 pos.col = len + 1;
18304
Bram Moolenaara5525202006-03-02 22:52:09 +000018305 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018306 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018307 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018308 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018309
Bram Moolenaara5525202006-03-02 22:52:09 +000018310#ifdef FEAT_VIRTUALEDIT
18311 /* Get the virtual offset. Defaults to zero. */
18312 pos.coladd = list_find_nr(l, 2L, &error);
18313 if (error)
18314 pos.coladd = 0;
18315#endif
18316
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018317 return &pos;
18318 }
18319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018320 name = get_tv_string_chk(varp);
18321 if (name == NULL)
18322 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018323 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018325#ifdef FEAT_VISUAL
18326 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18327 {
18328 if (VIsual_active)
18329 return &VIsual;
18330 return &curwin->w_cursor;
18331 }
18332#endif
18333 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018335 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18337 return NULL;
18338 return pp;
18339 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018340
18341#ifdef FEAT_VIRTUALEDIT
18342 pos.coladd = 0;
18343#endif
18344
Bram Moolenaar477933c2007-07-17 14:32:23 +000018345 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018346 {
18347 pos.col = 0;
18348 if (name[1] == '0') /* "w0": first visible line */
18349 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018350 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018351 pos.lnum = curwin->w_topline;
18352 return &pos;
18353 }
18354 else if (name[1] == '$') /* "w$": last visible line */
18355 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018356 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018357 pos.lnum = curwin->w_botline - 1;
18358 return &pos;
18359 }
18360 }
18361 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018362 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018363 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364 {
18365 pos.lnum = curbuf->b_ml.ml_line_count;
18366 pos.col = 0;
18367 }
18368 else
18369 {
18370 pos.lnum = curwin->w_cursor.lnum;
18371 pos.col = (colnr_T)STRLEN(ml_get_curline());
18372 }
18373 return &pos;
18374 }
18375 return NULL;
18376}
18377
18378/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018379 * Convert list in "arg" into a position and optional file number.
18380 * When "fnump" is NULL there is no file number, only 3 items.
18381 * Note that the column is passed on as-is, the caller may want to decrement
18382 * it to use 1 for the first column.
18383 * Return FAIL when conversion is not possible, doesn't check the position for
18384 * validity.
18385 */
18386 static int
18387list2fpos(arg, posp, fnump)
18388 typval_T *arg;
18389 pos_T *posp;
18390 int *fnump;
18391{
18392 list_T *l = arg->vval.v_list;
18393 long i = 0;
18394 long n;
18395
Bram Moolenaarbde35262006-07-23 20:12:24 +000018396 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18397 * when "fnump" isn't NULL and "coladd" is optional. */
18398 if (arg->v_type != VAR_LIST
18399 || l == NULL
18400 || l->lv_len < (fnump == NULL ? 2 : 3)
18401 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018402 return FAIL;
18403
18404 if (fnump != NULL)
18405 {
18406 n = list_find_nr(l, i++, NULL); /* fnum */
18407 if (n < 0)
18408 return FAIL;
18409 if (n == 0)
18410 n = curbuf->b_fnum; /* current buffer */
18411 *fnump = n;
18412 }
18413
18414 n = list_find_nr(l, i++, NULL); /* lnum */
18415 if (n < 0)
18416 return FAIL;
18417 posp->lnum = n;
18418
18419 n = list_find_nr(l, i++, NULL); /* col */
18420 if (n < 0)
18421 return FAIL;
18422 posp->col = n;
18423
18424#ifdef FEAT_VIRTUALEDIT
18425 n = list_find_nr(l, i, NULL);
18426 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018427 posp->coladd = 0;
18428 else
18429 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018430#endif
18431
18432 return OK;
18433}
18434
18435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018436 * Get the length of an environment variable name.
18437 * Advance "arg" to the first character after the name.
18438 * Return 0 for error.
18439 */
18440 static int
18441get_env_len(arg)
18442 char_u **arg;
18443{
18444 char_u *p;
18445 int len;
18446
18447 for (p = *arg; vim_isIDc(*p); ++p)
18448 ;
18449 if (p == *arg) /* no name found */
18450 return 0;
18451
18452 len = (int)(p - *arg);
18453 *arg = p;
18454 return len;
18455}
18456
18457/*
18458 * Get the length of the name of a function or internal variable.
18459 * "arg" is advanced to the first non-white character after the name.
18460 * Return 0 if something is wrong.
18461 */
18462 static int
18463get_id_len(arg)
18464 char_u **arg;
18465{
18466 char_u *p;
18467 int len;
18468
18469 /* Find the end of the name. */
18470 for (p = *arg; eval_isnamec(*p); ++p)
18471 ;
18472 if (p == *arg) /* no name found */
18473 return 0;
18474
18475 len = (int)(p - *arg);
18476 *arg = skipwhite(p);
18477
18478 return len;
18479}
18480
18481/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018482 * Get the length of the name of a variable or function.
18483 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018484 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018485 * Return -1 if curly braces expansion failed.
18486 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 * If the name contains 'magic' {}'s, expand them and return the
18488 * expanded name in an allocated string via 'alias' - caller must free.
18489 */
18490 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018491get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492 char_u **arg;
18493 char_u **alias;
18494 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018495 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496{
18497 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 char_u *p;
18499 char_u *expr_start;
18500 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501
18502 *alias = NULL; /* default to no alias */
18503
18504 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18505 && (*arg)[2] == (int)KE_SNR)
18506 {
18507 /* hard coded <SNR>, already translated */
18508 *arg += 3;
18509 return get_id_len(arg) + 3;
18510 }
18511 len = eval_fname_script(*arg);
18512 if (len > 0)
18513 {
18514 /* literal "<SID>", "s:" or "<SNR>" */
18515 *arg += len;
18516 }
18517
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018519 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018520 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018521 p = find_name_end(*arg, &expr_start, &expr_end,
18522 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 if (expr_start != NULL)
18524 {
18525 char_u *temp_string;
18526
18527 if (!evaluate)
18528 {
18529 len += (int)(p - *arg);
18530 *arg = skipwhite(p);
18531 return len;
18532 }
18533
18534 /*
18535 * Include any <SID> etc in the expanded string:
18536 * Thus the -len here.
18537 */
18538 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18539 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018540 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 *alias = temp_string;
18542 *arg = skipwhite(p);
18543 return (int)STRLEN(temp_string);
18544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545
18546 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018547 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548 EMSG2(_(e_invexpr2), *arg);
18549
18550 return len;
18551}
18552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018553/*
18554 * Find the end of a variable or function name, taking care of magic braces.
18555 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18556 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018557 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558 * Return a pointer to just after the name. Equal to "arg" if there is no
18559 * valid name.
18560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018562find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 char_u *arg;
18564 char_u **expr_start;
18565 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018566 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018568 int mb_nest = 0;
18569 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 char_u *p;
18571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018572 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018574 *expr_start = NULL;
18575 *expr_end = NULL;
18576 }
18577
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018578 /* Quick check for valid starting character. */
18579 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18580 return arg;
18581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018582 for (p = arg; *p != NUL
18583 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018584 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018585 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018586 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018587 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018588 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018589 if (*p == '\'')
18590 {
18591 /* skip over 'string' to avoid counting [ and ] inside it. */
18592 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18593 ;
18594 if (*p == NUL)
18595 break;
18596 }
18597 else if (*p == '"')
18598 {
18599 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18600 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18601 if (*p == '\\' && p[1] != NUL)
18602 ++p;
18603 if (*p == NUL)
18604 break;
18605 }
18606
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018607 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018609 if (*p == '[')
18610 ++br_nest;
18611 else if (*p == ']')
18612 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018615 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018617 if (*p == '{')
18618 {
18619 mb_nest++;
18620 if (expr_start != NULL && *expr_start == NULL)
18621 *expr_start = p;
18622 }
18623 else if (*p == '}')
18624 {
18625 mb_nest--;
18626 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18627 *expr_end = p;
18628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 }
18631
18632 return p;
18633}
18634
18635/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018636 * Expands out the 'magic' {}'s in a variable/function name.
18637 * Note that this can call itself recursively, to deal with
18638 * constructs like foo{bar}{baz}{bam}
18639 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18640 * "in_start" ^
18641 * "expr_start" ^
18642 * "expr_end" ^
18643 * "in_end" ^
18644 *
18645 * Returns a new allocated string, which the caller must free.
18646 * Returns NULL for failure.
18647 */
18648 static char_u *
18649make_expanded_name(in_start, expr_start, expr_end, in_end)
18650 char_u *in_start;
18651 char_u *expr_start;
18652 char_u *expr_end;
18653 char_u *in_end;
18654{
18655 char_u c1;
18656 char_u *retval = NULL;
18657 char_u *temp_result;
18658 char_u *nextcmd = NULL;
18659
18660 if (expr_end == NULL || in_end == NULL)
18661 return NULL;
18662 *expr_start = NUL;
18663 *expr_end = NUL;
18664 c1 = *in_end;
18665 *in_end = NUL;
18666
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018667 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018668 if (temp_result != NULL && nextcmd == NULL)
18669 {
18670 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18671 + (in_end - expr_end) + 1));
18672 if (retval != NULL)
18673 {
18674 STRCPY(retval, in_start);
18675 STRCAT(retval, temp_result);
18676 STRCAT(retval, expr_end + 1);
18677 }
18678 }
18679 vim_free(temp_result);
18680
18681 *in_end = c1; /* put char back for error messages */
18682 *expr_start = '{';
18683 *expr_end = '}';
18684
18685 if (retval != NULL)
18686 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018687 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018688 if (expr_start != NULL)
18689 {
18690 /* Further expansion! */
18691 temp_result = make_expanded_name(retval, expr_start,
18692 expr_end, temp_result);
18693 vim_free(retval);
18694 retval = temp_result;
18695 }
18696 }
18697
18698 return retval;
18699}
18700
18701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018703 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704 */
18705 static int
18706eval_isnamec(c)
18707 int c;
18708{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018709 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18710}
18711
18712/*
18713 * Return TRUE if character "c" can be used as the first character in a
18714 * variable or function name (excluding '{' and '}').
18715 */
18716 static int
18717eval_isnamec1(c)
18718 int c;
18719{
18720 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721}
18722
18723/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 * Set number v: variable to "val".
18725 */
18726 void
18727set_vim_var_nr(idx, val)
18728 int idx;
18729 long val;
18730{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018731 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732}
18733
18734/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018735 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736 */
18737 long
18738get_vim_var_nr(idx)
18739 int idx;
18740{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018741 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018742}
18743
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018744/*
18745 * Get string v: variable value. Uses a static buffer, can only be used once.
18746 */
18747 char_u *
18748get_vim_var_str(idx)
18749 int idx;
18750{
18751 return get_tv_string(&vimvars[idx].vv_tv);
18752}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018753
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018755 * Get List v: variable value. Caller must take care of reference count when
18756 * needed.
18757 */
18758 list_T *
18759get_vim_var_list(idx)
18760 int idx;
18761{
18762 return vimvars[idx].vv_list;
18763}
18764
18765/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018766 * Set v:char to character "c".
18767 */
18768 void
18769set_vim_var_char(c)
18770 int c;
18771{
18772#ifdef FEAT_MBYTE
18773 char_u buf[MB_MAXBYTES];
18774#else
18775 char_u buf[2];
18776#endif
18777
18778#ifdef FEAT_MBYTE
18779 if (has_mbyte)
18780 buf[(*mb_char2bytes)(c, buf)] = NUL;
18781 else
18782#endif
18783 {
18784 buf[0] = c;
18785 buf[1] = NUL;
18786 }
18787 set_vim_var_string(VV_CHAR, buf, -1);
18788}
18789
18790/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018791 * Set v:count to "count" and v:count1 to "count1".
18792 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793 */
18794 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018795set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 long count;
18797 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018798 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018800 if (set_prevcount)
18801 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018802 vimvars[VV_COUNT].vv_nr = count;
18803 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804}
18805
18806/*
18807 * Set string v: variable to a copy of "val".
18808 */
18809 void
18810set_vim_var_string(idx, val, len)
18811 int idx;
18812 char_u *val;
18813 int len; /* length of "val" to use or -1 (whole string) */
18814{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018815 /* Need to do this (at least) once, since we can't initialize a union.
18816 * Will always be invoked when "v:progname" is set. */
18817 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18818
Bram Moolenaare9a41262005-01-15 22:18:47 +000018819 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018821 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018823 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018825 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826}
18827
18828/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018829 * Set List v: variable to "val".
18830 */
18831 void
18832set_vim_var_list(idx, val)
18833 int idx;
18834 list_T *val;
18835{
18836 list_unref(vimvars[idx].vv_list);
18837 vimvars[idx].vv_list = val;
18838 if (val != NULL)
18839 ++val->lv_refcount;
18840}
18841
18842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843 * Set v:register if needed.
18844 */
18845 void
18846set_reg_var(c)
18847 int c;
18848{
18849 char_u regname;
18850
18851 if (c == 0 || c == ' ')
18852 regname = '"';
18853 else
18854 regname = c;
18855 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018856 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 set_vim_var_string(VV_REG, &regname, 1);
18858}
18859
18860/*
18861 * Get or set v:exception. If "oldval" == NULL, return the current value.
18862 * Otherwise, restore the value to "oldval" and return NULL.
18863 * Must always be called in pairs to save and restore v:exception! Does not
18864 * take care of memory allocations.
18865 */
18866 char_u *
18867v_exception(oldval)
18868 char_u *oldval;
18869{
18870 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018871 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872
Bram Moolenaare9a41262005-01-15 22:18:47 +000018873 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 return NULL;
18875}
18876
18877/*
18878 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18879 * Otherwise, restore the value to "oldval" and return NULL.
18880 * Must always be called in pairs to save and restore v:throwpoint! Does not
18881 * take care of memory allocations.
18882 */
18883 char_u *
18884v_throwpoint(oldval)
18885 char_u *oldval;
18886{
18887 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018888 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018889
Bram Moolenaare9a41262005-01-15 22:18:47 +000018890 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891 return NULL;
18892}
18893
18894#if defined(FEAT_AUTOCMD) || defined(PROTO)
18895/*
18896 * Set v:cmdarg.
18897 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18898 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18899 * Must always be called in pairs!
18900 */
18901 char_u *
18902set_cmdarg(eap, oldarg)
18903 exarg_T *eap;
18904 char_u *oldarg;
18905{
18906 char_u *oldval;
18907 char_u *newval;
18908 unsigned len;
18909
Bram Moolenaare9a41262005-01-15 22:18:47 +000018910 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018911 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018913 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018914 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018915 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 }
18917
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018918 if (eap->force_bin == FORCE_BIN)
18919 len = 6;
18920 else if (eap->force_bin == FORCE_NOBIN)
18921 len = 8;
18922 else
18923 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018924
18925 if (eap->read_edit)
18926 len += 7;
18927
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018928 if (eap->force_ff != 0)
18929 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18930# ifdef FEAT_MBYTE
18931 if (eap->force_enc != 0)
18932 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018933 if (eap->bad_char != 0)
18934 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018935# endif
18936
18937 newval = alloc(len + 1);
18938 if (newval == NULL)
18939 return NULL;
18940
18941 if (eap->force_bin == FORCE_BIN)
18942 sprintf((char *)newval, " ++bin");
18943 else if (eap->force_bin == FORCE_NOBIN)
18944 sprintf((char *)newval, " ++nobin");
18945 else
18946 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018947
18948 if (eap->read_edit)
18949 STRCAT(newval, " ++edit");
18950
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018951 if (eap->force_ff != 0)
18952 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18953 eap->cmd + eap->force_ff);
18954# ifdef FEAT_MBYTE
18955 if (eap->force_enc != 0)
18956 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18957 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018958 if (eap->bad_char == BAD_KEEP)
18959 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18960 else if (eap->bad_char == BAD_DROP)
18961 STRCPY(newval + STRLEN(newval), " ++bad=drop");
18962 else if (eap->bad_char != 0)
18963 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018964# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000018965 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018966 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967}
18968#endif
18969
18970/*
18971 * Get the value of internal variable "name".
18972 * Return OK or FAIL.
18973 */
18974 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018975get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976 char_u *name;
18977 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000018978 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018979 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980{
18981 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000018982 typval_T *tv = NULL;
18983 typval_T atv;
18984 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986
18987 /* truncate the name, so that we can use strcmp() */
18988 cc = name[len];
18989 name[len] = NUL;
18990
18991 /*
18992 * Check for "b:changedtick".
18993 */
18994 if (STRCMP(name, "b:changedtick") == 0)
18995 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000018996 atv.v_type = VAR_NUMBER;
18997 atv.vval.v_number = curbuf->b_changedtick;
18998 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999 }
19000
19001 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019002 * Check for user-defined variables.
19003 */
19004 else
19005 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019006 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019008 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009 }
19010
Bram Moolenaare9a41262005-01-15 22:18:47 +000019011 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019013 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019014 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 ret = FAIL;
19016 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019017 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019018 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019
19020 name[len] = cc;
19021
19022 return ret;
19023}
19024
19025/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019026 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19027 * Also handle function call with Funcref variable: func(expr)
19028 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19029 */
19030 static int
19031handle_subscript(arg, rettv, evaluate, verbose)
19032 char_u **arg;
19033 typval_T *rettv;
19034 int evaluate; /* do more than finding the end */
19035 int verbose; /* give error messages */
19036{
19037 int ret = OK;
19038 dict_T *selfdict = NULL;
19039 char_u *s;
19040 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019041 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019042
19043 while (ret == OK
19044 && (**arg == '['
19045 || (**arg == '.' && rettv->v_type == VAR_DICT)
19046 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19047 && !vim_iswhite(*(*arg - 1)))
19048 {
19049 if (**arg == '(')
19050 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019051 /* need to copy the funcref so that we can clear rettv */
19052 functv = *rettv;
19053 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019054
19055 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019056 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019057 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019058 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19059 &len, evaluate, selfdict);
19060
19061 /* Clear the funcref afterwards, so that deleting it while
19062 * evaluating the arguments is possible (see test55). */
19063 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019064
19065 /* Stop the expression evaluation when immediately aborting on
19066 * error, or when an interrupt occurred or an exception was thrown
19067 * but not caught. */
19068 if (aborting())
19069 {
19070 if (ret == OK)
19071 clear_tv(rettv);
19072 ret = FAIL;
19073 }
19074 dict_unref(selfdict);
19075 selfdict = NULL;
19076 }
19077 else /* **arg == '[' || **arg == '.' */
19078 {
19079 dict_unref(selfdict);
19080 if (rettv->v_type == VAR_DICT)
19081 {
19082 selfdict = rettv->vval.v_dict;
19083 if (selfdict != NULL)
19084 ++selfdict->dv_refcount;
19085 }
19086 else
19087 selfdict = NULL;
19088 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19089 {
19090 clear_tv(rettv);
19091 ret = FAIL;
19092 }
19093 }
19094 }
19095 dict_unref(selfdict);
19096 return ret;
19097}
19098
19099/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019100 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019101 * value).
19102 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019103 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019104alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019105{
Bram Moolenaar33570922005-01-25 22:26:29 +000019106 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019107}
19108
19109/*
19110 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 * The string "s" must have been allocated, it is consumed.
19112 * Return NULL for out of memory, the variable otherwise.
19113 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019114 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019115alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019116 char_u *s;
19117{
Bram Moolenaar33570922005-01-25 22:26:29 +000019118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019120 rettv = alloc_tv();
19121 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019123 rettv->v_type = VAR_STRING;
19124 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125 }
19126 else
19127 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019128 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019129}
19130
19131/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019132 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019134 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019135free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019136 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137{
19138 if (varp != NULL)
19139 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019140 switch (varp->v_type)
19141 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019142 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019143 func_unref(varp->vval.v_string);
19144 /*FALLTHROUGH*/
19145 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019146 vim_free(varp->vval.v_string);
19147 break;
19148 case VAR_LIST:
19149 list_unref(varp->vval.v_list);
19150 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019151 case VAR_DICT:
19152 dict_unref(varp->vval.v_dict);
19153 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019154 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019155#ifdef FEAT_FLOAT
19156 case VAR_FLOAT:
19157#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019158 case VAR_UNKNOWN:
19159 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019160 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019161 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019162 break;
19163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 vim_free(varp);
19165 }
19166}
19167
19168/*
19169 * Free the memory for a variable value and set the value to NULL or 0.
19170 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019171 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019172clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019173 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174{
19175 if (varp != NULL)
19176 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019177 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019179 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019180 func_unref(varp->vval.v_string);
19181 /*FALLTHROUGH*/
19182 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019183 vim_free(varp->vval.v_string);
19184 varp->vval.v_string = NULL;
19185 break;
19186 case VAR_LIST:
19187 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019188 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019189 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019190 case VAR_DICT:
19191 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019192 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019193 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019194 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019195 varp->vval.v_number = 0;
19196 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019197#ifdef FEAT_FLOAT
19198 case VAR_FLOAT:
19199 varp->vval.v_float = 0.0;
19200 break;
19201#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019202 case VAR_UNKNOWN:
19203 break;
19204 default:
19205 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019207 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208 }
19209}
19210
19211/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019212 * Set the value of a variable to NULL without freeing items.
19213 */
19214 static void
19215init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019216 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217{
19218 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019219 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019220}
19221
19222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019223 * Get the number value of a variable.
19224 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019225 * For incompatible types, return 0.
19226 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19227 * caller of incompatible types: it sets *denote to TRUE if "denote"
19228 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229 */
19230 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019231get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019232 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019233{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019234 int error = FALSE;
19235
19236 return get_tv_number_chk(varp, &error); /* return 0L on error */
19237}
19238
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019239 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019240get_tv_number_chk(varp, denote)
19241 typval_T *varp;
19242 int *denote;
19243{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019244 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019246 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019248 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019249 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019250#ifdef FEAT_FLOAT
19251 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019252 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019253 break;
19254#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019255 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019256 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019257 break;
19258 case VAR_STRING:
19259 if (varp->vval.v_string != NULL)
19260 vim_str2nr(varp->vval.v_string, NULL, NULL,
19261 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019262 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019263 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019264 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019265 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019266 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019267 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019268 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019269 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019270 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019271 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019273 if (denote == NULL) /* useful for values that must be unsigned */
19274 n = -1;
19275 else
19276 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019277 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278}
19279
19280/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019281 * Get the lnum from the first argument.
19282 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019283 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284 */
19285 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019286get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019287 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288{
Bram Moolenaar33570922005-01-25 22:26:29 +000019289 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290 linenr_T lnum;
19291
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019292 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019293 if (lnum == 0) /* no valid number, try using line() */
19294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019295 rettv.v_type = VAR_NUMBER;
19296 f_line(argvars, &rettv);
19297 lnum = rettv.vval.v_number;
19298 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 }
19300 return lnum;
19301}
19302
19303/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019304 * Get the lnum from the first argument.
19305 * Also accepts "$", then "buf" is used.
19306 * Returns 0 on error.
19307 */
19308 static linenr_T
19309get_tv_lnum_buf(argvars, buf)
19310 typval_T *argvars;
19311 buf_T *buf;
19312{
19313 if (argvars[0].v_type == VAR_STRING
19314 && argvars[0].vval.v_string != NULL
19315 && argvars[0].vval.v_string[0] == '$'
19316 && buf != NULL)
19317 return buf->b_ml.ml_line_count;
19318 return get_tv_number_chk(&argvars[0], NULL);
19319}
19320
19321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322 * Get the string value of a variable.
19323 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019324 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19325 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 * If the String variable has never been set, return an empty string.
19327 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019328 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19329 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 */
19331 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019332get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019333 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019334{
19335 static char_u mybuf[NUMBUFLEN];
19336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019337 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338}
19339
19340 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019341get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019342 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019343 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019344{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019345 char_u *res = get_tv_string_buf_chk(varp, buf);
19346
19347 return res != NULL ? res : (char_u *)"";
19348}
19349
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019350 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019351get_tv_string_chk(varp)
19352 typval_T *varp;
19353{
19354 static char_u mybuf[NUMBUFLEN];
19355
19356 return get_tv_string_buf_chk(varp, mybuf);
19357}
19358
19359 static char_u *
19360get_tv_string_buf_chk(varp, buf)
19361 typval_T *varp;
19362 char_u *buf;
19363{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019364 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019366 case VAR_NUMBER:
19367 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19368 return buf;
19369 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019370 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019371 break;
19372 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019373 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019374 break;
19375 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019376 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019377 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019378#ifdef FEAT_FLOAT
19379 case VAR_FLOAT:
19380 EMSG(_("E806: using Float as a String"));
19381 break;
19382#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019383 case VAR_STRING:
19384 if (varp->vval.v_string != NULL)
19385 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019386 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019387 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019388 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019389 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019390 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019391 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019392}
19393
19394/*
19395 * Find variable "name" in the list of variables.
19396 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019397 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019398 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019399 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019401 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019402find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019404 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019407 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019408
Bram Moolenaara7043832005-01-21 11:56:39 +000019409 ht = find_var_ht(name, &varname);
19410 if (htp != NULL)
19411 *htp = ht;
19412 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019413 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019414 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019415}
19416
19417/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019418 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019419 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019421 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019422find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019423 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019424 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019425 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019426{
Bram Moolenaar33570922005-01-25 22:26:29 +000019427 hashitem_T *hi;
19428
19429 if (*varname == NUL)
19430 {
19431 /* Must be something like "s:", otherwise "ht" would be NULL. */
19432 switch (varname[-2])
19433 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019434 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019435 case 'g': return &globvars_var;
19436 case 'v': return &vimvars_var;
19437 case 'b': return &curbuf->b_bufvar;
19438 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019439#ifdef FEAT_WINDOWS
19440 case 't': return &curtab->tp_winvar;
19441#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019442 case 'l': return current_funccal == NULL
19443 ? NULL : &current_funccal->l_vars_var;
19444 case 'a': return current_funccal == NULL
19445 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019446 }
19447 return NULL;
19448 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019449
19450 hi = hash_find(ht, varname);
19451 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019452 {
19453 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019454 * worked find the variable again. Don't auto-load a script if it was
19455 * loaded already, otherwise it would be loaded every time when
19456 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019457 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019458 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019459 hi = hash_find(ht, varname);
19460 if (HASHITEM_EMPTY(hi))
19461 return NULL;
19462 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019463 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019464}
19465
19466/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019467 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019468 * Set "varname" to the start of name without ':'.
19469 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019470 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019471find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 char_u *name;
19473 char_u **varname;
19474{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019475 hashitem_T *hi;
19476
Bram Moolenaar071d4272004-06-13 20:20:40 +000019477 if (name[1] != ':')
19478 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019479 /* The name must not start with a colon or #. */
19480 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019481 return NULL;
19482 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019483
19484 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019485 hi = hash_find(&compat_hashtab, name);
19486 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019487 return &compat_hashtab;
19488
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019490 return &globvarht; /* global variable */
19491 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 }
19493 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019494 if (*name == 'g') /* global variable */
19495 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019496 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19497 */
19498 if (vim_strchr(name + 2, ':') != NULL
19499 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019500 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019502 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019504 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019505#ifdef FEAT_WINDOWS
19506 if (*name == 't') /* tab page variable */
19507 return &curtab->tp_vars.dv_hashtab;
19508#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019509 if (*name == 'v') /* v: variable */
19510 return &vimvarht;
19511 if (*name == 'a' && current_funccal != NULL) /* function argument */
19512 return &current_funccal->l_avars.dv_hashtab;
19513 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19514 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515 if (*name == 's' /* script variable */
19516 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19517 return &SCRIPT_VARS(current_SID);
19518 return NULL;
19519}
19520
19521/*
19522 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019523 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524 * Returns NULL when it doesn't exist.
19525 */
19526 char_u *
19527get_var_value(name)
19528 char_u *name;
19529{
Bram Moolenaar33570922005-01-25 22:26:29 +000019530 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531
Bram Moolenaara7043832005-01-21 11:56:39 +000019532 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 if (v == NULL)
19534 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019535 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536}
19537
19538/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019539 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 * sourcing this script and when executing functions defined in the script.
19541 */
19542 void
19543new_script_vars(id)
19544 scid_T id;
19545{
Bram Moolenaara7043832005-01-21 11:56:39 +000019546 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019547 hashtab_T *ht;
19548 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019549
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19551 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019552 /* Re-allocating ga_data means that an ht_array pointing to
19553 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019554 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019555 for (i = 1; i <= ga_scripts.ga_len; ++i)
19556 {
19557 ht = &SCRIPT_VARS(i);
19558 if (ht->ht_mask == HT_INIT_SIZE - 1)
19559 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019560 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019561 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019562 }
19563
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564 while (ga_scripts.ga_len < id)
19565 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019566 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019567 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019568 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570 }
19571 }
19572}
19573
19574/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019575 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19576 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577 */
19578 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019579init_var_dict(dict, dict_var)
19580 dict_T *dict;
19581 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582{
Bram Moolenaar33570922005-01-25 22:26:29 +000019583 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019584 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019585 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019586 dict_var->di_tv.vval.v_dict = dict;
19587 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019588 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019589 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19590 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591}
19592
19593/*
19594 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019595 * Frees all allocated variables and the value they contain.
19596 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 */
19598 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019599vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019600 hashtab_T *ht;
19601{
19602 vars_clear_ext(ht, TRUE);
19603}
19604
19605/*
19606 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19607 */
19608 static void
19609vars_clear_ext(ht, free_val)
19610 hashtab_T *ht;
19611 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612{
Bram Moolenaara7043832005-01-21 11:56:39 +000019613 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019614 hashitem_T *hi;
19615 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616
Bram Moolenaar33570922005-01-25 22:26:29 +000019617 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019618 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019619 for (hi = ht->ht_array; todo > 0; ++hi)
19620 {
19621 if (!HASHITEM_EMPTY(hi))
19622 {
19623 --todo;
19624
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019626 * ht_array might change then. hash_clear() takes care of it
19627 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019628 v = HI2DI(hi);
19629 if (free_val)
19630 clear_tv(&v->di_tv);
19631 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19632 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019633 }
19634 }
19635 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019636 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637}
19638
Bram Moolenaara7043832005-01-21 11:56:39 +000019639/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019640 * Delete a variable from hashtab "ht" at item "hi".
19641 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019644delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019645 hashtab_T *ht;
19646 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647{
Bram Moolenaar33570922005-01-25 22:26:29 +000019648 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019649
19650 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019651 clear_tv(&di->di_tv);
19652 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653}
19654
19655/*
19656 * List the value of one internal variable.
19657 */
19658 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019659list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019660 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019662 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019664 char_u *tofree;
19665 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019666 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019667
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019668 current_copyID += COPYID_INC;
19669 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019671 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019672 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673}
19674
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019676list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 char_u *prefix;
19678 char_u *name;
19679 int type;
19680 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019681 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682{
Bram Moolenaar31859182007-08-14 20:41:13 +000019683 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19684 msg_start();
19685 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 if (name != NULL) /* "a:" vars don't have a name stored */
19687 msg_puts(name);
19688 msg_putchar(' ');
19689 msg_advance(22);
19690 if (type == VAR_NUMBER)
19691 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019692 else if (type == VAR_FUNC)
19693 msg_putchar('*');
19694 else if (type == VAR_LIST)
19695 {
19696 msg_putchar('[');
19697 if (*string == '[')
19698 ++string;
19699 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019700 else if (type == VAR_DICT)
19701 {
19702 msg_putchar('{');
19703 if (*string == '{')
19704 ++string;
19705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706 else
19707 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019708
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019710
19711 if (type == VAR_FUNC)
19712 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019713 if (*first)
19714 {
19715 msg_clr_eos();
19716 *first = FALSE;
19717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718}
19719
19720/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019721 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 * If the variable already exists, the value is updated.
19723 * Otherwise the variable is created.
19724 */
19725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019726set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019728 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019729 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730{
Bram Moolenaar33570922005-01-25 22:26:29 +000019731 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019733 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019734 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019736 ht = find_var_ht(name, &varname);
19737 if (ht == NULL || *varname == NUL)
19738 {
19739 EMSG2(_(e_illvar), name);
19740 return;
19741 }
19742 v = find_var_in_ht(ht, varname, TRUE);
19743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019744 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019745 {
19746 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19747 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19748 ? name[2] : name[0]))
19749 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019750 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019751 return;
19752 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019753 /* Don't allow hiding a function. When "v" is not NULL we migth be
19754 * assigning another function to the same var, the type is checked
19755 * below. */
19756 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019757 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019758 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019759 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019760 return;
19761 }
19762 }
19763
Bram Moolenaar33570922005-01-25 22:26:29 +000019764 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019765 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019766 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019767 if (var_check_ro(v->di_flags, name)
19768 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019769 return;
19770 if (v->di_tv.v_type != tv->v_type
19771 && !((v->di_tv.v_type == VAR_STRING
19772 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019773 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019774 || tv->v_type == VAR_NUMBER))
19775#ifdef FEAT_FLOAT
19776 && !((v->di_tv.v_type == VAR_NUMBER
19777 || v->di_tv.v_type == VAR_FLOAT)
19778 && (tv->v_type == VAR_NUMBER
19779 || tv->v_type == VAR_FLOAT))
19780#endif
19781 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019782 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019783 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019784 return;
19785 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019786
19787 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019788 * Handle setting internal v: variables separately: we don't change
19789 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019790 */
19791 if (ht == &vimvarht)
19792 {
19793 if (v->di_tv.v_type == VAR_STRING)
19794 {
19795 vim_free(v->di_tv.vval.v_string);
19796 if (copy || tv->v_type != VAR_STRING)
19797 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19798 else
19799 {
19800 /* Take over the string to avoid an extra alloc/free. */
19801 v->di_tv.vval.v_string = tv->vval.v_string;
19802 tv->vval.v_string = NULL;
19803 }
19804 }
19805 else if (v->di_tv.v_type != VAR_NUMBER)
19806 EMSG2(_(e_intern2), "set_var()");
19807 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019808 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019809 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019810 if (STRCMP(varname, "searchforward") == 0)
19811 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19812 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019813 return;
19814 }
19815
19816 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 }
19818 else /* add a new variable */
19819 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019820 /* Can't add "v:" variable. */
19821 if (ht == &vimvarht)
19822 {
19823 EMSG2(_(e_illvar), name);
19824 return;
19825 }
19826
Bram Moolenaar92124a32005-06-17 22:03:40 +000019827 /* Make sure the variable name is valid. */
19828 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019829 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19830 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019831 {
19832 EMSG2(_(e_illvar), varname);
19833 return;
19834 }
19835
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019836 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19837 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019838 if (v == NULL)
19839 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019840 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019841 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019843 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844 return;
19845 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019846 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019848
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019849 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019850 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019851 else
19852 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019854 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019855 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019857}
19858
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019859/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019860 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 * Also give an error message.
19862 */
19863 static int
19864var_check_ro(flags, name)
19865 int flags;
19866 char_u *name;
19867{
19868 if (flags & DI_FLAGS_RO)
19869 {
19870 EMSG2(_(e_readonlyvar), name);
19871 return TRUE;
19872 }
19873 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19874 {
19875 EMSG2(_(e_readonlysbx), name);
19876 return TRUE;
19877 }
19878 return FALSE;
19879}
19880
19881/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019882 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19883 * Also give an error message.
19884 */
19885 static int
19886var_check_fixed(flags, name)
19887 int flags;
19888 char_u *name;
19889{
19890 if (flags & DI_FLAGS_FIX)
19891 {
19892 EMSG2(_("E795: Cannot delete variable %s"), name);
19893 return TRUE;
19894 }
19895 return FALSE;
19896}
19897
19898/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019899 * Return TRUE if typeval "tv" is set to be locked (immutable).
19900 * Also give an error message, using "name".
19901 */
19902 static int
19903tv_check_lock(lock, name)
19904 int lock;
19905 char_u *name;
19906{
19907 if (lock & VAR_LOCKED)
19908 {
19909 EMSG2(_("E741: Value is locked: %s"),
19910 name == NULL ? (char_u *)_("Unknown") : name);
19911 return TRUE;
19912 }
19913 if (lock & VAR_FIXED)
19914 {
19915 EMSG2(_("E742: Cannot change value of %s"),
19916 name == NULL ? (char_u *)_("Unknown") : name);
19917 return TRUE;
19918 }
19919 return FALSE;
19920}
19921
19922/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019923 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019924 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019925 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019926 * It is OK for "from" and "to" to point to the same item. This is used to
19927 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019928 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019929 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019930copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019931 typval_T *from;
19932 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019934 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019935 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019936 switch (from->v_type)
19937 {
19938 case VAR_NUMBER:
19939 to->vval.v_number = from->vval.v_number;
19940 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019941#ifdef FEAT_FLOAT
19942 case VAR_FLOAT:
19943 to->vval.v_float = from->vval.v_float;
19944 break;
19945#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019946 case VAR_STRING:
19947 case VAR_FUNC:
19948 if (from->vval.v_string == NULL)
19949 to->vval.v_string = NULL;
19950 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019952 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019953 if (from->v_type == VAR_FUNC)
19954 func_ref(to->vval.v_string);
19955 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019956 break;
19957 case VAR_LIST:
19958 if (from->vval.v_list == NULL)
19959 to->vval.v_list = NULL;
19960 else
19961 {
19962 to->vval.v_list = from->vval.v_list;
19963 ++to->vval.v_list->lv_refcount;
19964 }
19965 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019966 case VAR_DICT:
19967 if (from->vval.v_dict == NULL)
19968 to->vval.v_dict = NULL;
19969 else
19970 {
19971 to->vval.v_dict = from->vval.v_dict;
19972 ++to->vval.v_dict->dv_refcount;
19973 }
19974 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019975 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019976 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019977 break;
19978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019979}
19980
19981/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000019982 * Make a copy of an item.
19983 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019984 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19985 * reference to an already copied list/dict can be used.
19986 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019987 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019988 static int
19989item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000019990 typval_T *from;
19991 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019992 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019993 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019994{
19995 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019996 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019997
Bram Moolenaar33570922005-01-25 22:26:29 +000019998 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019999 {
20000 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020001 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020002 }
20003 ++recurse;
20004
20005 switch (from->v_type)
20006 {
20007 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020008#ifdef FEAT_FLOAT
20009 case VAR_FLOAT:
20010#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020011 case VAR_STRING:
20012 case VAR_FUNC:
20013 copy_tv(from, to);
20014 break;
20015 case VAR_LIST:
20016 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020017 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020018 if (from->vval.v_list == NULL)
20019 to->vval.v_list = NULL;
20020 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20021 {
20022 /* use the copy made earlier */
20023 to->vval.v_list = from->vval.v_list->lv_copylist;
20024 ++to->vval.v_list->lv_refcount;
20025 }
20026 else
20027 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20028 if (to->vval.v_list == NULL)
20029 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020030 break;
20031 case VAR_DICT:
20032 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020033 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020034 if (from->vval.v_dict == NULL)
20035 to->vval.v_dict = NULL;
20036 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20037 {
20038 /* use the copy made earlier */
20039 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20040 ++to->vval.v_dict->dv_refcount;
20041 }
20042 else
20043 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20044 if (to->vval.v_dict == NULL)
20045 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020046 break;
20047 default:
20048 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020049 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020050 }
20051 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020052 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020053}
20054
20055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 * ":echo expr1 ..." print each argument separated with a space, add a
20057 * newline at the end.
20058 * ":echon expr1 ..." print each argument plain.
20059 */
20060 void
20061ex_echo(eap)
20062 exarg_T *eap;
20063{
20064 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020065 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020066 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 char_u *p;
20068 int needclr = TRUE;
20069 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020070 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020071
20072 if (eap->skip)
20073 ++emsg_skip;
20074 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20075 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020076 /* If eval1() causes an error message the text from the command may
20077 * still need to be cleared. E.g., "echo 22,44". */
20078 need_clr_eos = needclr;
20079
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020081 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 {
20083 /*
20084 * Report the invalid expression unless the expression evaluation
20085 * has been cancelled due to an aborting error, an interrupt, or an
20086 * exception.
20087 */
20088 if (!aborting())
20089 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020090 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 break;
20092 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020093 need_clr_eos = FALSE;
20094
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 if (!eap->skip)
20096 {
20097 if (atstart)
20098 {
20099 atstart = FALSE;
20100 /* Call msg_start() after eval1(), evaluating the expression
20101 * may cause a message to appear. */
20102 if (eap->cmdidx == CMD_echo)
20103 msg_start();
20104 }
20105 else if (eap->cmdidx == CMD_echo)
20106 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020107 current_copyID += COPYID_INC;
20108 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020109 if (p != NULL)
20110 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020112 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020114 if (*p != TAB && needclr)
20115 {
20116 /* remove any text still there from the command */
20117 msg_clr_eos();
20118 needclr = FALSE;
20119 }
20120 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 }
20122 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020123 {
20124#ifdef FEAT_MBYTE
20125 if (has_mbyte)
20126 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020127 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020128
20129 (void)msg_outtrans_len_attr(p, i, echo_attr);
20130 p += i - 1;
20131 }
20132 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020134 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020137 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020139 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140 arg = skipwhite(arg);
20141 }
20142 eap->nextcmd = check_nextcmd(arg);
20143
20144 if (eap->skip)
20145 --emsg_skip;
20146 else
20147 {
20148 /* remove text that may still be there from the command */
20149 if (needclr)
20150 msg_clr_eos();
20151 if (eap->cmdidx == CMD_echo)
20152 msg_end();
20153 }
20154}
20155
20156/*
20157 * ":echohl {name}".
20158 */
20159 void
20160ex_echohl(eap)
20161 exarg_T *eap;
20162{
20163 int id;
20164
20165 id = syn_name2id(eap->arg);
20166 if (id == 0)
20167 echo_attr = 0;
20168 else
20169 echo_attr = syn_id2attr(id);
20170}
20171
20172/*
20173 * ":execute expr1 ..." execute the result of an expression.
20174 * ":echomsg expr1 ..." Print a message
20175 * ":echoerr expr1 ..." Print an error
20176 * Each gets spaces around each argument and a newline at the end for
20177 * echo commands
20178 */
20179 void
20180ex_execute(eap)
20181 exarg_T *eap;
20182{
20183 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020184 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 int ret = OK;
20186 char_u *p;
20187 garray_T ga;
20188 int len;
20189 int save_did_emsg;
20190
20191 ga_init2(&ga, 1, 80);
20192
20193 if (eap->skip)
20194 ++emsg_skip;
20195 while (*arg != NUL && *arg != '|' && *arg != '\n')
20196 {
20197 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020198 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 {
20200 /*
20201 * Report the invalid expression unless the expression evaluation
20202 * has been cancelled due to an aborting error, an interrupt, or an
20203 * exception.
20204 */
20205 if (!aborting())
20206 EMSG2(_(e_invexpr2), p);
20207 ret = FAIL;
20208 break;
20209 }
20210
20211 if (!eap->skip)
20212 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020213 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214 len = (int)STRLEN(p);
20215 if (ga_grow(&ga, len + 2) == FAIL)
20216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020217 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020218 ret = FAIL;
20219 break;
20220 }
20221 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020222 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 ga.ga_len += len;
20225 }
20226
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020227 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 arg = skipwhite(arg);
20229 }
20230
20231 if (ret != FAIL && ga.ga_data != NULL)
20232 {
20233 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020234 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020236 out_flush();
20237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 else if (eap->cmdidx == CMD_echoerr)
20239 {
20240 /* We don't want to abort following commands, restore did_emsg. */
20241 save_did_emsg = did_emsg;
20242 EMSG((char_u *)ga.ga_data);
20243 if (!force_abort)
20244 did_emsg = save_did_emsg;
20245 }
20246 else if (eap->cmdidx == CMD_execute)
20247 do_cmdline((char_u *)ga.ga_data,
20248 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20249 }
20250
20251 ga_clear(&ga);
20252
20253 if (eap->skip)
20254 --emsg_skip;
20255
20256 eap->nextcmd = check_nextcmd(arg);
20257}
20258
20259/*
20260 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20261 * "arg" points to the "&" or '+' when called, to "option" when returning.
20262 * Returns NULL when no option name found. Otherwise pointer to the char
20263 * after the option name.
20264 */
20265 static char_u *
20266find_option_end(arg, opt_flags)
20267 char_u **arg;
20268 int *opt_flags;
20269{
20270 char_u *p = *arg;
20271
20272 ++p;
20273 if (*p == 'g' && p[1] == ':')
20274 {
20275 *opt_flags = OPT_GLOBAL;
20276 p += 2;
20277 }
20278 else if (*p == 'l' && p[1] == ':')
20279 {
20280 *opt_flags = OPT_LOCAL;
20281 p += 2;
20282 }
20283 else
20284 *opt_flags = 0;
20285
20286 if (!ASCII_ISALPHA(*p))
20287 return NULL;
20288 *arg = p;
20289
20290 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20291 p += 4; /* termcap option */
20292 else
20293 while (ASCII_ISALPHA(*p))
20294 ++p;
20295 return p;
20296}
20297
20298/*
20299 * ":function"
20300 */
20301 void
20302ex_function(eap)
20303 exarg_T *eap;
20304{
20305 char_u *theline;
20306 int j;
20307 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020308 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 char_u *name = NULL;
20310 char_u *p;
20311 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020312 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 garray_T newargs;
20314 garray_T newlines;
20315 int varargs = FALSE;
20316 int mustend = FALSE;
20317 int flags = 0;
20318 ufunc_T *fp;
20319 int indent;
20320 int nesting;
20321 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020322 dictitem_T *v;
20323 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020324 static int func_nr = 0; /* number for nameless function */
20325 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020326 hashtab_T *ht;
20327 int todo;
20328 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020329 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330
20331 /*
20332 * ":function" without argument: list functions.
20333 */
20334 if (ends_excmd(*eap->arg))
20335 {
20336 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020337 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020338 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020339 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020340 {
20341 if (!HASHITEM_EMPTY(hi))
20342 {
20343 --todo;
20344 fp = HI2UF(hi);
20345 if (!isdigit(*fp->uf_name))
20346 list_func_head(fp, FALSE);
20347 }
20348 }
20349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 eap->nextcmd = check_nextcmd(eap->arg);
20351 return;
20352 }
20353
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020354 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020355 * ":function /pat": list functions matching pattern.
20356 */
20357 if (*eap->arg == '/')
20358 {
20359 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20360 if (!eap->skip)
20361 {
20362 regmatch_T regmatch;
20363
20364 c = *p;
20365 *p = NUL;
20366 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20367 *p = c;
20368 if (regmatch.regprog != NULL)
20369 {
20370 regmatch.rm_ic = p_ic;
20371
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020372 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020373 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20374 {
20375 if (!HASHITEM_EMPTY(hi))
20376 {
20377 --todo;
20378 fp = HI2UF(hi);
20379 if (!isdigit(*fp->uf_name)
20380 && vim_regexec(&regmatch, fp->uf_name, 0))
20381 list_func_head(fp, FALSE);
20382 }
20383 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020384 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020385 }
20386 }
20387 if (*p == '/')
20388 ++p;
20389 eap->nextcmd = check_nextcmd(p);
20390 return;
20391 }
20392
20393 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020394 * Get the function name. There are these situations:
20395 * func normal function name
20396 * "name" == func, "fudi.fd_dict" == NULL
20397 * dict.func new dictionary entry
20398 * "name" == NULL, "fudi.fd_dict" set,
20399 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20400 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020401 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020402 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20403 * dict.func existing dict entry that's not a Funcref
20404 * "name" == NULL, "fudi.fd_dict" set,
20405 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020407 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020408 name = trans_function_name(&p, eap->skip, 0, &fudi);
20409 paren = (vim_strchr(p, '(') != NULL);
20410 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411 {
20412 /*
20413 * Return on an invalid expression in braces, unless the expression
20414 * evaluation has been cancelled due to an aborting error, an
20415 * interrupt, or an exception.
20416 */
20417 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020418 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020419 if (!eap->skip && fudi.fd_newkey != NULL)
20420 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020421 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424 else
20425 eap->skip = TRUE;
20426 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020427
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 /* An error in a function call during evaluation of an expression in magic
20429 * braces should not cause the function not to be defined. */
20430 saved_did_emsg = did_emsg;
20431 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432
20433 /*
20434 * ":function func" with only function name: list function.
20435 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020436 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 {
20438 if (!ends_excmd(*skipwhite(p)))
20439 {
20440 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020441 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442 }
20443 eap->nextcmd = check_nextcmd(p);
20444 if (eap->nextcmd != NULL)
20445 *p = NUL;
20446 if (!eap->skip && !got_int)
20447 {
20448 fp = find_func(name);
20449 if (fp != NULL)
20450 {
20451 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020452 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020454 if (FUNCLINE(fp, j) == NULL)
20455 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020456 msg_putchar('\n');
20457 msg_outnum((long)(j + 1));
20458 if (j < 9)
20459 msg_putchar(' ');
20460 if (j < 99)
20461 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020462 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 out_flush(); /* show a line at a time */
20464 ui_breakcheck();
20465 }
20466 if (!got_int)
20467 {
20468 msg_putchar('\n');
20469 msg_puts((char_u *)" endfunction");
20470 }
20471 }
20472 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020473 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020475 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 }
20477
20478 /*
20479 * ":function name(arg1, arg2)" Define function.
20480 */
20481 p = skipwhite(p);
20482 if (*p != '(')
20483 {
20484 if (!eap->skip)
20485 {
20486 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020487 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020488 }
20489 /* attempt to continue by skipping some text */
20490 if (vim_strchr(p, '(') != NULL)
20491 p = vim_strchr(p, '(');
20492 }
20493 p = skipwhite(p + 1);
20494
20495 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20496 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20497
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020498 if (!eap->skip)
20499 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020500 /* Check the name of the function. Unless it's a dictionary function
20501 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020502 if (name != NULL)
20503 arg = name;
20504 else
20505 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020506 if (arg != NULL && (fudi.fd_di == NULL
20507 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020508 {
20509 if (*arg == K_SPECIAL)
20510 j = 3;
20511 else
20512 j = 0;
20513 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20514 : eval_isnamec(arg[j])))
20515 ++j;
20516 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020517 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020518 }
20519 }
20520
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 /*
20522 * Isolate the arguments: "arg1, arg2, ...)"
20523 */
20524 while (*p != ')')
20525 {
20526 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20527 {
20528 varargs = TRUE;
20529 p += 3;
20530 mustend = TRUE;
20531 }
20532 else
20533 {
20534 arg = p;
20535 while (ASCII_ISALNUM(*p) || *p == '_')
20536 ++p;
20537 if (arg == p || isdigit(*arg)
20538 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20539 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20540 {
20541 if (!eap->skip)
20542 EMSG2(_("E125: Illegal argument: %s"), arg);
20543 break;
20544 }
20545 if (ga_grow(&newargs, 1) == FAIL)
20546 goto erret;
20547 c = *p;
20548 *p = NUL;
20549 arg = vim_strsave(arg);
20550 if (arg == NULL)
20551 goto erret;
20552 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20553 *p = c;
20554 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 if (*p == ',')
20556 ++p;
20557 else
20558 mustend = TRUE;
20559 }
20560 p = skipwhite(p);
20561 if (mustend && *p != ')')
20562 {
20563 if (!eap->skip)
20564 EMSG2(_(e_invarg2), eap->arg);
20565 break;
20566 }
20567 }
20568 ++p; /* skip the ')' */
20569
Bram Moolenaare9a41262005-01-15 22:18:47 +000020570 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 for (;;)
20572 {
20573 p = skipwhite(p);
20574 if (STRNCMP(p, "range", 5) == 0)
20575 {
20576 flags |= FC_RANGE;
20577 p += 5;
20578 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020579 else if (STRNCMP(p, "dict", 4) == 0)
20580 {
20581 flags |= FC_DICT;
20582 p += 4;
20583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 else if (STRNCMP(p, "abort", 5) == 0)
20585 {
20586 flags |= FC_ABORT;
20587 p += 5;
20588 }
20589 else
20590 break;
20591 }
20592
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020593 /* When there is a line break use what follows for the function body.
20594 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20595 if (*p == '\n')
20596 line_arg = p + 1;
20597 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 EMSG(_(e_trailing));
20599
20600 /*
20601 * Read the body of the function, until ":endfunction" is found.
20602 */
20603 if (KeyTyped)
20604 {
20605 /* Check if the function already exists, don't let the user type the
20606 * whole function before telling him it doesn't work! For a script we
20607 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020608 if (!eap->skip && !eap->forceit)
20609 {
20610 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20611 EMSG(_(e_funcdict));
20612 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020613 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020616 if (!eap->skip && did_emsg)
20617 goto erret;
20618
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619 msg_putchar('\n'); /* don't overwrite the function name */
20620 cmdline_row = msg_row;
20621 }
20622
20623 indent = 2;
20624 nesting = 0;
20625 for (;;)
20626 {
20627 msg_scroll = TRUE;
20628 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020629 sourcing_lnum_off = sourcing_lnum;
20630
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020631 if (line_arg != NULL)
20632 {
20633 /* Use eap->arg, split up in parts by line breaks. */
20634 theline = line_arg;
20635 p = vim_strchr(theline, '\n');
20636 if (p == NULL)
20637 line_arg += STRLEN(line_arg);
20638 else
20639 {
20640 *p = NUL;
20641 line_arg = p + 1;
20642 }
20643 }
20644 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 theline = getcmdline(':', 0L, indent);
20646 else
20647 theline = eap->getline(':', eap->cookie, indent);
20648 if (KeyTyped)
20649 lines_left = Rows - 1;
20650 if (theline == NULL)
20651 {
20652 EMSG(_("E126: Missing :endfunction"));
20653 goto erret;
20654 }
20655
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020656 /* Detect line continuation: sourcing_lnum increased more than one. */
20657 if (sourcing_lnum > sourcing_lnum_off + 1)
20658 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20659 else
20660 sourcing_lnum_off = 0;
20661
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 if (skip_until != NULL)
20663 {
20664 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20665 * don't check for ":endfunc". */
20666 if (STRCMP(theline, skip_until) == 0)
20667 {
20668 vim_free(skip_until);
20669 skip_until = NULL;
20670 }
20671 }
20672 else
20673 {
20674 /* skip ':' and blanks*/
20675 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20676 ;
20677
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020678 /* Check for "endfunction". */
20679 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020681 if (line_arg == NULL)
20682 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 break;
20684 }
20685
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020686 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 * at "end". */
20688 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20689 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020690 else if (STRNCMP(p, "if", 2) == 0
20691 || STRNCMP(p, "wh", 2) == 0
20692 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693 || STRNCMP(p, "try", 3) == 0)
20694 indent += 2;
20695
20696 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020697 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020699 if (*p == '!')
20700 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 p += eval_fname_script(p);
20702 if (ASCII_ISALPHA(*p))
20703 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020704 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020705 if (*skipwhite(p) == '(')
20706 {
20707 ++nesting;
20708 indent += 2;
20709 }
20710 }
20711 }
20712
20713 /* Check for ":append" or ":insert". */
20714 p = skip_range(p, NULL);
20715 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20716 || (p[0] == 'i'
20717 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20718 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20719 skip_until = vim_strsave((char_u *)".");
20720
20721 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20722 arg = skipwhite(skiptowhite(p));
20723 if (arg[0] == '<' && arg[1] =='<'
20724 && ((p[0] == 'p' && p[1] == 'y'
20725 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20726 || (p[0] == 'p' && p[1] == 'e'
20727 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20728 || (p[0] == 't' && p[1] == 'c'
20729 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20730 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20731 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020732 || (p[0] == 'm' && p[1] == 'z'
20733 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 ))
20735 {
20736 /* ":python <<" continues until a dot, like ":append" */
20737 p = skipwhite(arg + 2);
20738 if (*p == NUL)
20739 skip_until = vim_strsave((char_u *)".");
20740 else
20741 skip_until = vim_strsave(p);
20742 }
20743 }
20744
20745 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020746 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020747 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020748 if (line_arg == NULL)
20749 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020751 }
20752
20753 /* Copy the line to newly allocated memory. get_one_sourceline()
20754 * allocates 250 bytes per line, this saves 80% on average. The cost
20755 * is an extra alloc/free. */
20756 p = vim_strsave(theline);
20757 if (p != NULL)
20758 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020759 if (line_arg == NULL)
20760 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020761 theline = p;
20762 }
20763
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020764 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20765
20766 /* Add NULL lines for continuation lines, so that the line count is
20767 * equal to the index in the growarray. */
20768 while (sourcing_lnum_off-- > 0)
20769 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020770
20771 /* Check for end of eap->arg. */
20772 if (line_arg != NULL && *line_arg == NUL)
20773 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 }
20775
20776 /* Don't define the function when skipping commands or when an error was
20777 * detected. */
20778 if (eap->skip || did_emsg)
20779 goto erret;
20780
20781 /*
20782 * If there are no errors, add the function
20783 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020784 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020785 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020786 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020787 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020788 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020789 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020790 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020791 goto erret;
20792 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020793
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020794 fp = find_func(name);
20795 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020796 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020797 if (!eap->forceit)
20798 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020799 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020800 goto erret;
20801 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020802 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020803 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020804 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020805 name);
20806 goto erret;
20807 }
20808 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020809 ga_clear_strings(&(fp->uf_args));
20810 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020811 vim_free(name);
20812 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020814 }
20815 else
20816 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020817 char numbuf[20];
20818
20819 fp = NULL;
20820 if (fudi.fd_newkey == NULL && !eap->forceit)
20821 {
20822 EMSG(_(e_funcdict));
20823 goto erret;
20824 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020825 if (fudi.fd_di == NULL)
20826 {
20827 /* Can't add a function to a locked dictionary */
20828 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20829 goto erret;
20830 }
20831 /* Can't change an existing function if it is locked */
20832 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20833 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020834
20835 /* Give the function a sequential number. Can only be used with a
20836 * Funcref! */
20837 vim_free(name);
20838 sprintf(numbuf, "%d", ++func_nr);
20839 name = vim_strsave((char_u *)numbuf);
20840 if (name == NULL)
20841 goto erret;
20842 }
20843
20844 if (fp == NULL)
20845 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020846 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020847 {
20848 int slen, plen;
20849 char_u *scriptname;
20850
20851 /* Check that the autoload name matches the script name. */
20852 j = FAIL;
20853 if (sourcing_name != NULL)
20854 {
20855 scriptname = autoload_name(name);
20856 if (scriptname != NULL)
20857 {
20858 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020859 plen = (int)STRLEN(p);
20860 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020861 if (slen > plen && fnamecmp(p,
20862 sourcing_name + slen - plen) == 0)
20863 j = OK;
20864 vim_free(scriptname);
20865 }
20866 }
20867 if (j == FAIL)
20868 {
20869 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20870 goto erret;
20871 }
20872 }
20873
20874 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 if (fp == NULL)
20876 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020877
20878 if (fudi.fd_dict != NULL)
20879 {
20880 if (fudi.fd_di == NULL)
20881 {
20882 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020883 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020884 if (fudi.fd_di == NULL)
20885 {
20886 vim_free(fp);
20887 goto erret;
20888 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020889 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20890 {
20891 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020892 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020893 goto erret;
20894 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020895 }
20896 else
20897 /* overwrite existing dict entry */
20898 clear_tv(&fudi.fd_di->di_tv);
20899 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020900 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020901 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020902 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020903
20904 /* behave like "dict" was used */
20905 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020906 }
20907
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020909 STRCPY(fp->uf_name, name);
20910 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020912 fp->uf_args = newargs;
20913 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020914#ifdef FEAT_PROFILE
20915 fp->uf_tml_count = NULL;
20916 fp->uf_tml_total = NULL;
20917 fp->uf_tml_self = NULL;
20918 fp->uf_profiling = FALSE;
20919 if (prof_def_func())
20920 func_do_profile(fp);
20921#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020922 fp->uf_varargs = varargs;
20923 fp->uf_flags = flags;
20924 fp->uf_calls = 0;
20925 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020926 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020927
20928erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 ga_clear_strings(&newargs);
20930 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020931ret_free:
20932 vim_free(skip_until);
20933 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936}
20937
20938/*
20939 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020940 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020942 * flags:
20943 * TFN_INT: internal function name OK
20944 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945 * Advances "pp" to just after the function name (if no error).
20946 */
20947 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020948trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020949 char_u **pp;
20950 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020951 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020952 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020954 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 char_u *start;
20956 char_u *end;
20957 int lead;
20958 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020960 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020961
20962 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020963 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000020965
20966 /* Check for hard coded <SNR>: already translated function ID (from a user
20967 * command). */
20968 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
20969 && (*pp)[2] == (int)KE_SNR)
20970 {
20971 *pp += 3;
20972 len = get_id_len(pp) + 3;
20973 return vim_strnsave(start, len);
20974 }
20975
20976 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20977 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000020979 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020981
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020982 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
20983 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020984 if (end == start)
20985 {
20986 if (!skip)
20987 EMSG(_("E129: Function name required"));
20988 goto theend;
20989 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020990 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020991 {
20992 /*
20993 * Report an invalid expression in braces, unless the expression
20994 * evaluation has been cancelled due to an aborting error, an
20995 * interrupt, or an exception.
20996 */
20997 if (!aborting())
20998 {
20999 if (end != NULL)
21000 EMSG2(_(e_invarg2), start);
21001 }
21002 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021003 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021004 goto theend;
21005 }
21006
21007 if (lv.ll_tv != NULL)
21008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021009 if (fdp != NULL)
21010 {
21011 fdp->fd_dict = lv.ll_dict;
21012 fdp->fd_newkey = lv.ll_newkey;
21013 lv.ll_newkey = NULL;
21014 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021015 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021016 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21017 {
21018 name = vim_strsave(lv.ll_tv->vval.v_string);
21019 *pp = end;
21020 }
21021 else
21022 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021023 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21024 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021025 EMSG(_(e_funcref));
21026 else
21027 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021028 name = NULL;
21029 }
21030 goto theend;
21031 }
21032
21033 if (lv.ll_name == NULL)
21034 {
21035 /* Error found, but continue after the function name. */
21036 *pp = end;
21037 goto theend;
21038 }
21039
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021040 /* Check if the name is a Funcref. If so, use the value. */
21041 if (lv.ll_exp_name != NULL)
21042 {
21043 len = (int)STRLEN(lv.ll_exp_name);
21044 name = deref_func_name(lv.ll_exp_name, &len);
21045 if (name == lv.ll_exp_name)
21046 name = NULL;
21047 }
21048 else
21049 {
21050 len = (int)(end - *pp);
21051 name = deref_func_name(*pp, &len);
21052 if (name == *pp)
21053 name = NULL;
21054 }
21055 if (name != NULL)
21056 {
21057 name = vim_strsave(name);
21058 *pp = end;
21059 goto theend;
21060 }
21061
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021062 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021063 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021064 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021065 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21066 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21067 {
21068 /* When there was "s:" already or the name expanded to get a
21069 * leading "s:" then remove it. */
21070 lv.ll_name += 2;
21071 len -= 2;
21072 lead = 2;
21073 }
21074 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021075 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021076 {
21077 if (lead == 2) /* skip over "s:" */
21078 lv.ll_name += 2;
21079 len = (int)(end - lv.ll_name);
21080 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021081
21082 /*
21083 * Copy the function name to allocated memory.
21084 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21085 * Accept <SNR>123_name() outside a script.
21086 */
21087 if (skip)
21088 lead = 0; /* do nothing */
21089 else if (lead > 0)
21090 {
21091 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021092 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21093 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021094 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021095 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021096 if (current_SID <= 0)
21097 {
21098 EMSG(_(e_usingsid));
21099 goto theend;
21100 }
21101 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21102 lead += (int)STRLEN(sid_buf);
21103 }
21104 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021105 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021106 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021107 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021108 goto theend;
21109 }
21110 name = alloc((unsigned)(len + lead + 1));
21111 if (name != NULL)
21112 {
21113 if (lead > 0)
21114 {
21115 name[0] = K_SPECIAL;
21116 name[1] = KS_EXTRA;
21117 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021118 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021119 STRCPY(name + 3, sid_buf);
21120 }
21121 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21122 name[len + lead] = NUL;
21123 }
21124 *pp = end;
21125
21126theend:
21127 clear_lval(&lv);
21128 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129}
21130
21131/*
21132 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21133 * Return 2 if "p" starts with "s:".
21134 * Return 0 otherwise.
21135 */
21136 static int
21137eval_fname_script(p)
21138 char_u *p;
21139{
21140 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21141 || STRNICMP(p + 1, "SNR>", 4) == 0))
21142 return 5;
21143 if (p[0] == 's' && p[1] == ':')
21144 return 2;
21145 return 0;
21146}
21147
21148/*
21149 * Return TRUE if "p" starts with "<SID>" or "s:".
21150 * Only works if eval_fname_script() returned non-zero for "p"!
21151 */
21152 static int
21153eval_fname_sid(p)
21154 char_u *p;
21155{
21156 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21157}
21158
21159/*
21160 * List the head of the function: "name(arg1, arg2)".
21161 */
21162 static void
21163list_func_head(fp, indent)
21164 ufunc_T *fp;
21165 int indent;
21166{
21167 int j;
21168
21169 msg_start();
21170 if (indent)
21171 MSG_PUTS(" ");
21172 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021173 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 {
21175 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021176 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 }
21178 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021179 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021181 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 {
21183 if (j)
21184 MSG_PUTS(", ");
21185 msg_puts(FUNCARG(fp, j));
21186 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021187 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 {
21189 if (j)
21190 MSG_PUTS(", ");
21191 MSG_PUTS("...");
21192 }
21193 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021194 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021195 if (p_verbose > 0)
21196 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197}
21198
21199/*
21200 * Find a function by name, return pointer to it in ufuncs.
21201 * Return NULL for unknown function.
21202 */
21203 static ufunc_T *
21204find_func(name)
21205 char_u *name;
21206{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021207 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021209 hi = hash_find(&func_hashtab, name);
21210 if (!HASHITEM_EMPTY(hi))
21211 return HI2UF(hi);
21212 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213}
21214
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021215#if defined(EXITFREE) || defined(PROTO)
21216 void
21217free_all_functions()
21218{
21219 hashitem_T *hi;
21220
21221 /* Need to start all over every time, because func_free() may change the
21222 * hash table. */
21223 while (func_hashtab.ht_used > 0)
21224 for (hi = func_hashtab.ht_array; ; ++hi)
21225 if (!HASHITEM_EMPTY(hi))
21226 {
21227 func_free(HI2UF(hi));
21228 break;
21229 }
21230}
21231#endif
21232
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021233/*
21234 * Return TRUE if a function "name" exists.
21235 */
21236 static int
21237function_exists(name)
21238 char_u *name;
21239{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021240 char_u *nm = name;
21241 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021242 int n = FALSE;
21243
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021244 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021245 nm = skipwhite(nm);
21246
21247 /* Only accept "funcname", "funcname ", "funcname (..." and
21248 * "funcname(...", not "funcname!...". */
21249 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021250 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021251 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021252 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021253 else
21254 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021255 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021256 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021257 return n;
21258}
21259
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021260/*
21261 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021262 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021263 */
21264 static int
21265builtin_function(name)
21266 char_u *name;
21267{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021268 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21269 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021270}
21271
Bram Moolenaar05159a02005-02-26 23:04:13 +000021272#if defined(FEAT_PROFILE) || defined(PROTO)
21273/*
21274 * Start profiling function "fp".
21275 */
21276 static void
21277func_do_profile(fp)
21278 ufunc_T *fp;
21279{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021280 int len = fp->uf_lines.ga_len;
21281
21282 if (len == 0)
21283 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021284 fp->uf_tm_count = 0;
21285 profile_zero(&fp->uf_tm_self);
21286 profile_zero(&fp->uf_tm_total);
21287 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021288 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021289 if (fp->uf_tml_total == NULL)
21290 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021291 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021292 if (fp->uf_tml_self == NULL)
21293 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021294 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021295 fp->uf_tml_idx = -1;
21296 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21297 || fp->uf_tml_self == NULL)
21298 return; /* out of memory */
21299
21300 fp->uf_profiling = TRUE;
21301}
21302
21303/*
21304 * Dump the profiling results for all functions in file "fd".
21305 */
21306 void
21307func_dump_profile(fd)
21308 FILE *fd;
21309{
21310 hashitem_T *hi;
21311 int todo;
21312 ufunc_T *fp;
21313 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021314 ufunc_T **sorttab;
21315 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021316
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021317 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021318 if (todo == 0)
21319 return; /* nothing to dump */
21320
Bram Moolenaar73830342005-02-28 22:48:19 +000021321 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21322
Bram Moolenaar05159a02005-02-26 23:04:13 +000021323 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21324 {
21325 if (!HASHITEM_EMPTY(hi))
21326 {
21327 --todo;
21328 fp = HI2UF(hi);
21329 if (fp->uf_profiling)
21330 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021331 if (sorttab != NULL)
21332 sorttab[st_len++] = fp;
21333
Bram Moolenaar05159a02005-02-26 23:04:13 +000021334 if (fp->uf_name[0] == K_SPECIAL)
21335 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21336 else
21337 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21338 if (fp->uf_tm_count == 1)
21339 fprintf(fd, "Called 1 time\n");
21340 else
21341 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21342 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21343 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21344 fprintf(fd, "\n");
21345 fprintf(fd, "count total (s) self (s)\n");
21346
21347 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21348 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021349 if (FUNCLINE(fp, i) == NULL)
21350 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021351 prof_func_line(fd, fp->uf_tml_count[i],
21352 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021353 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21354 }
21355 fprintf(fd, "\n");
21356 }
21357 }
21358 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021359
21360 if (sorttab != NULL && st_len > 0)
21361 {
21362 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21363 prof_total_cmp);
21364 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21365 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21366 prof_self_cmp);
21367 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21368 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021369
21370 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021371}
Bram Moolenaar73830342005-02-28 22:48:19 +000021372
21373 static void
21374prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21375 FILE *fd;
21376 ufunc_T **sorttab;
21377 int st_len;
21378 char *title;
21379 int prefer_self; /* when equal print only self time */
21380{
21381 int i;
21382 ufunc_T *fp;
21383
21384 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21385 fprintf(fd, "count total (s) self (s) function\n");
21386 for (i = 0; i < 20 && i < st_len; ++i)
21387 {
21388 fp = sorttab[i];
21389 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21390 prefer_self);
21391 if (fp->uf_name[0] == K_SPECIAL)
21392 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21393 else
21394 fprintf(fd, " %s()\n", fp->uf_name);
21395 }
21396 fprintf(fd, "\n");
21397}
21398
21399/*
21400 * Print the count and times for one function or function line.
21401 */
21402 static void
21403prof_func_line(fd, count, total, self, prefer_self)
21404 FILE *fd;
21405 int count;
21406 proftime_T *total;
21407 proftime_T *self;
21408 int prefer_self; /* when equal print only self time */
21409{
21410 if (count > 0)
21411 {
21412 fprintf(fd, "%5d ", count);
21413 if (prefer_self && profile_equal(total, self))
21414 fprintf(fd, " ");
21415 else
21416 fprintf(fd, "%s ", profile_msg(total));
21417 if (!prefer_self && profile_equal(total, self))
21418 fprintf(fd, " ");
21419 else
21420 fprintf(fd, "%s ", profile_msg(self));
21421 }
21422 else
21423 fprintf(fd, " ");
21424}
21425
21426/*
21427 * Compare function for total time sorting.
21428 */
21429 static int
21430#ifdef __BORLANDC__
21431_RTLENTRYF
21432#endif
21433prof_total_cmp(s1, s2)
21434 const void *s1;
21435 const void *s2;
21436{
21437 ufunc_T *p1, *p2;
21438
21439 p1 = *(ufunc_T **)s1;
21440 p2 = *(ufunc_T **)s2;
21441 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21442}
21443
21444/*
21445 * Compare function for self time sorting.
21446 */
21447 static int
21448#ifdef __BORLANDC__
21449_RTLENTRYF
21450#endif
21451prof_self_cmp(s1, s2)
21452 const void *s1;
21453 const void *s2;
21454{
21455 ufunc_T *p1, *p2;
21456
21457 p1 = *(ufunc_T **)s1;
21458 p2 = *(ufunc_T **)s2;
21459 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21460}
21461
Bram Moolenaar05159a02005-02-26 23:04:13 +000021462#endif
21463
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021464/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021465 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021466 * Return TRUE if a package was loaded.
21467 */
21468 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021469script_autoload(name, reload)
21470 char_u *name;
21471 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021472{
21473 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021474 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021475 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021476 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021477
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021478 /* Return quickly when autoload disabled. */
21479 if (no_autoload)
21480 return FALSE;
21481
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021482 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021483 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021484 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021485 return FALSE;
21486
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021487 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021488
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021489 /* Find the name in the list of previously loaded package names. Skip
21490 * "autoload/", it's always the same. */
21491 for (i = 0; i < ga_loaded.ga_len; ++i)
21492 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21493 break;
21494 if (!reload && i < ga_loaded.ga_len)
21495 ret = FALSE; /* was loaded already */
21496 else
21497 {
21498 /* Remember the name if it wasn't loaded already. */
21499 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21500 {
21501 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21502 tofree = NULL;
21503 }
21504
21505 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021506 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021507 ret = TRUE;
21508 }
21509
21510 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021511 return ret;
21512}
21513
21514/*
21515 * Return the autoload script name for a function or variable name.
21516 * Returns NULL when out of memory.
21517 */
21518 static char_u *
21519autoload_name(name)
21520 char_u *name;
21521{
21522 char_u *p;
21523 char_u *scriptname;
21524
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021525 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021526 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21527 if (scriptname == NULL)
21528 return FALSE;
21529 STRCPY(scriptname, "autoload/");
21530 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021531 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021532 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021533 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021534 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021535 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021536}
21537
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21539
21540/*
21541 * Function given to ExpandGeneric() to obtain the list of user defined
21542 * function names.
21543 */
21544 char_u *
21545get_user_func_name(xp, idx)
21546 expand_T *xp;
21547 int idx;
21548{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021549 static long_u done;
21550 static hashitem_T *hi;
21551 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552
21553 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021554 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021555 done = 0;
21556 hi = func_hashtab.ht_array;
21557 }
21558 if (done < func_hashtab.ht_used)
21559 {
21560 if (done++ > 0)
21561 ++hi;
21562 while (HASHITEM_EMPTY(hi))
21563 ++hi;
21564 fp = HI2UF(hi);
21565
21566 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21567 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568
21569 cat_func_name(IObuff, fp);
21570 if (xp->xp_context != EXPAND_USER_FUNC)
21571 {
21572 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021573 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021574 STRCAT(IObuff, ")");
21575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 return IObuff;
21577 }
21578 return NULL;
21579}
21580
21581#endif /* FEAT_CMDL_COMPL */
21582
21583/*
21584 * Copy the function name of "fp" to buffer "buf".
21585 * "buf" must be able to hold the function name plus three bytes.
21586 * Takes care of script-local function names.
21587 */
21588 static void
21589cat_func_name(buf, fp)
21590 char_u *buf;
21591 ufunc_T *fp;
21592{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021593 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594 {
21595 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021596 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597 }
21598 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021599 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600}
21601
21602/*
21603 * ":delfunction {name}"
21604 */
21605 void
21606ex_delfunction(eap)
21607 exarg_T *eap;
21608{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021609 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610 char_u *p;
21611 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021612 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613
21614 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021615 name = trans_function_name(&p, eap->skip, 0, &fudi);
21616 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021617 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021618 {
21619 if (fudi.fd_dict != NULL && !eap->skip)
21620 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021621 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021623 if (!ends_excmd(*skipwhite(p)))
21624 {
21625 vim_free(name);
21626 EMSG(_(e_trailing));
21627 return;
21628 }
21629 eap->nextcmd = check_nextcmd(p);
21630 if (eap->nextcmd != NULL)
21631 *p = NUL;
21632
21633 if (!eap->skip)
21634 fp = find_func(name);
21635 vim_free(name);
21636
21637 if (!eap->skip)
21638 {
21639 if (fp == NULL)
21640 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021641 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 return;
21643 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021644 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021645 {
21646 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21647 return;
21648 }
21649
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021650 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021652 /* Delete the dict item that refers to the function, it will
21653 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021654 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021656 else
21657 func_free(fp);
21658 }
21659}
21660
21661/*
21662 * Free a function and remove it from the list of functions.
21663 */
21664 static void
21665func_free(fp)
21666 ufunc_T *fp;
21667{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021668 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021669
21670 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021671 ga_clear_strings(&(fp->uf_args));
21672 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021673#ifdef FEAT_PROFILE
21674 vim_free(fp->uf_tml_count);
21675 vim_free(fp->uf_tml_total);
21676 vim_free(fp->uf_tml_self);
21677#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021678
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021679 /* remove the function from the function hashtable */
21680 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21681 if (HASHITEM_EMPTY(hi))
21682 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021683 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021684 hash_remove(&func_hashtab, hi);
21685
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021686 vim_free(fp);
21687}
21688
21689/*
21690 * Unreference a Function: decrement the reference count and free it when it
21691 * becomes zero. Only for numbered functions.
21692 */
21693 static void
21694func_unref(name)
21695 char_u *name;
21696{
21697 ufunc_T *fp;
21698
21699 if (name != NULL && isdigit(*name))
21700 {
21701 fp = find_func(name);
21702 if (fp == NULL)
21703 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021704 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021705 {
21706 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021707 * when "uf_calls" becomes zero. */
21708 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021709 func_free(fp);
21710 }
21711 }
21712}
21713
21714/*
21715 * Count a reference to a Function.
21716 */
21717 static void
21718func_ref(name)
21719 char_u *name;
21720{
21721 ufunc_T *fp;
21722
21723 if (name != NULL && isdigit(*name))
21724 {
21725 fp = find_func(name);
21726 if (fp == NULL)
21727 EMSG2(_(e_intern2), "func_ref()");
21728 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021729 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730 }
21731}
21732
21733/*
21734 * Call a user function.
21735 */
21736 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021737call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021738 ufunc_T *fp; /* pointer to function */
21739 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021740 typval_T *argvars; /* arguments */
21741 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 linenr_T firstline; /* first line of range */
21743 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021744 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745{
Bram Moolenaar33570922005-01-25 22:26:29 +000021746 char_u *save_sourcing_name;
21747 linenr_T save_sourcing_lnum;
21748 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021749 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021750 int save_did_emsg;
21751 static int depth = 0;
21752 dictitem_T *v;
21753 int fixvar_idx = 0; /* index in fixvar[] */
21754 int i;
21755 int ai;
21756 char_u numbuf[NUMBUFLEN];
21757 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021758#ifdef FEAT_PROFILE
21759 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021760 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762
21763 /* If depth of calling is getting too high, don't execute the function */
21764 if (depth >= p_mfd)
21765 {
21766 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021767 rettv->v_type = VAR_NUMBER;
21768 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 return;
21770 }
21771 ++depth;
21772
21773 line_breakcheck(); /* check for CTRL-C hit */
21774
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021775 fc = (funccall_T *)alloc(sizeof(funccall_T));
21776 fc->caller = current_funccal;
21777 current_funccal = fc;
21778 fc->func = fp;
21779 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021780 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021781 fc->linenr = 0;
21782 fc->returned = FALSE;
21783 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021785 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21786 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021789 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021790 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21791 * each argument variable and saves a lot of time.
21792 */
21793 /*
21794 * Init l: variables.
21795 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021796 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021797 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021798 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021799 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21800 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021801 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021802 name = v->di_key;
21803 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021804 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021805 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021806 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021807 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021808 v->di_tv.vval.v_dict = selfdict;
21809 ++selfdict->dv_refcount;
21810 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021811
Bram Moolenaar33570922005-01-25 22:26:29 +000021812 /*
21813 * Init a: variables.
21814 * Set a:0 to "argcount".
21815 * Set a:000 to a list with room for the "..." arguments.
21816 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021817 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21818 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021819 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021820 /* Use "name" to avoid a warning from some compiler that checks the
21821 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021822 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021823 name = v->di_key;
21824 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021825 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021826 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021828 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021829 v->di_tv.vval.v_list = &fc->l_varlist;
21830 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21831 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21832 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021833
21834 /*
21835 * Set a:firstline to "firstline" and a:lastline to "lastline".
21836 * Set a:name to named arguments.
21837 * Set a:N to the "..." arguments.
21838 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021839 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021840 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021841 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021842 (varnumber_T)lastline);
21843 for (i = 0; i < argcount; ++i)
21844 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021845 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021846 if (ai < 0)
21847 /* named argument a:name */
21848 name = FUNCARG(fp, i);
21849 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021850 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021851 /* "..." argument a:1, a:2, etc. */
21852 sprintf((char *)numbuf, "%d", ai + 1);
21853 name = numbuf;
21854 }
21855 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21856 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021857 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021858 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21859 }
21860 else
21861 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021862 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21863 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 if (v == NULL)
21865 break;
21866 v->di_flags = DI_FLAGS_RO;
21867 }
21868 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021869 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021870
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021871 /* Note: the values are copied directly to avoid alloc/free.
21872 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021873 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021874 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021875
21876 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21877 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021878 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21879 fc->l_listitems[ai].li_tv = argvars[i];
21880 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021881 }
21882 }
21883
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 /* Don't redraw while executing the function. */
21885 ++RedrawingDisabled;
21886 save_sourcing_name = sourcing_name;
21887 save_sourcing_lnum = sourcing_lnum;
21888 sourcing_lnum = 1;
21889 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021890 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 if (sourcing_name != NULL)
21892 {
21893 if (save_sourcing_name != NULL
21894 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21895 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21896 else
21897 STRCPY(sourcing_name, "function ");
21898 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21899
21900 if (p_verbose >= 12)
21901 {
21902 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021903 verbose_enter_scroll();
21904
Bram Moolenaar555b2802005-05-19 21:08:39 +000021905 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021906 if (p_verbose >= 14)
21907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021908 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021909 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021910 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021911 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912
21913 msg_puts((char_u *)"(");
21914 for (i = 0; i < argcount; ++i)
21915 {
21916 if (i > 0)
21917 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021918 if (argvars[i].v_type == VAR_NUMBER)
21919 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920 else
21921 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021922 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21923 if (s != NULL)
21924 {
21925 trunc_string(s, buf, MSG_BUF_CLEN);
21926 msg_puts(buf);
21927 vim_free(tofree);
21928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021929 }
21930 }
21931 msg_puts((char_u *)")");
21932 }
21933 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021934
21935 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 --no_wait_return;
21937 }
21938 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021939#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021940 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021941 {
21942 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21943 func_do_profile(fp);
21944 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021945 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021946 {
21947 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021948 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021949 profile_zero(&fp->uf_tm_children);
21950 }
21951 script_prof_save(&wait_start);
21952 }
21953#endif
21954
Bram Moolenaar071d4272004-06-13 20:20:40 +000021955 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021956 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 save_did_emsg = did_emsg;
21958 did_emsg = FALSE;
21959
21960 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021961 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
21963
21964 --RedrawingDisabled;
21965
21966 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021967 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021969 clear_tv(rettv);
21970 rettv->v_type = VAR_NUMBER;
21971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972 }
21973
Bram Moolenaar05159a02005-02-26 23:04:13 +000021974#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021975 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021976 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021977 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021978 profile_end(&call_start);
21979 profile_sub_wait(&wait_start, &call_start);
21980 profile_add(&fp->uf_tm_total, &call_start);
21981 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021982 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021983 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021984 profile_add(&fc->caller->func->uf_tm_children, &call_start);
21985 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021986 }
21987 }
21988#endif
21989
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 /* when being verbose, mention the return value */
21991 if (p_verbose >= 12)
21992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021994 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021995
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000021997 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021998 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000021999 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022000 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022001 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022003 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022004 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022005 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022006 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022007
Bram Moolenaar555b2802005-05-19 21:08:39 +000022008 /* The value may be very long. Skip the middle part, so that we
22009 * have some idea how it starts and ends. smsg() would always
22010 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022011 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022012 if (s != NULL)
22013 {
22014 trunc_string(s, buf, MSG_BUF_CLEN);
22015 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22016 vim_free(tofree);
22017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018 }
22019 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022020
22021 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 --no_wait_return;
22023 }
22024
22025 vim_free(sourcing_name);
22026 sourcing_name = save_sourcing_name;
22027 sourcing_lnum = save_sourcing_lnum;
22028 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022029#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022030 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022031 script_prof_restore(&wait_start);
22032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033
22034 if (p_verbose >= 12 && sourcing_name != NULL)
22035 {
22036 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022037 verbose_enter_scroll();
22038
Bram Moolenaar555b2802005-05-19 21:08:39 +000022039 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022041
22042 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043 --no_wait_return;
22044 }
22045
22046 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022047 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022049
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022050 /* If the a:000 list and the l: and a: dicts are not referenced we can
22051 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022052 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22053 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22054 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22055 {
22056 free_funccal(fc, FALSE);
22057 }
22058 else
22059 {
22060 hashitem_T *hi;
22061 listitem_T *li;
22062 int todo;
22063
22064 /* "fc" is still in use. This can happen when returning "a:000" or
22065 * assigning "l:" to a global variable.
22066 * Link "fc" in the list for garbage collection later. */
22067 fc->caller = previous_funccal;
22068 previous_funccal = fc;
22069
22070 /* Make a copy of the a: variables, since we didn't do that above. */
22071 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22072 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22073 {
22074 if (!HASHITEM_EMPTY(hi))
22075 {
22076 --todo;
22077 v = HI2DI(hi);
22078 copy_tv(&v->di_tv, &v->di_tv);
22079 }
22080 }
22081
22082 /* Make a copy of the a:000 items, since we didn't do that above. */
22083 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22084 copy_tv(&li->li_tv, &li->li_tv);
22085 }
22086}
22087
22088/*
22089 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022090 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022091 */
22092 static int
22093can_free_funccal(fc, copyID)
22094 funccall_T *fc;
22095 int copyID;
22096{
22097 return (fc->l_varlist.lv_copyID != copyID
22098 && fc->l_vars.dv_copyID != copyID
22099 && fc->l_avars.dv_copyID != copyID);
22100}
22101
22102/*
22103 * Free "fc" and what it contains.
22104 */
22105 static void
22106free_funccal(fc, free_val)
22107 funccall_T *fc;
22108 int free_val; /* a: vars were allocated */
22109{
22110 listitem_T *li;
22111
22112 /* The a: variables typevals may not have been allocated, only free the
22113 * allocated variables. */
22114 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22115
22116 /* free all l: variables */
22117 vars_clear(&fc->l_vars.dv_hashtab);
22118
22119 /* Free the a:000 variables if they were allocated. */
22120 if (free_val)
22121 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22122 clear_tv(&li->li_tv);
22123
22124 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022125}
22126
22127/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022128 * Add a number variable "name" to dict "dp" with value "nr".
22129 */
22130 static void
22131add_nr_var(dp, v, name, nr)
22132 dict_T *dp;
22133 dictitem_T *v;
22134 char *name;
22135 varnumber_T nr;
22136{
22137 STRCPY(v->di_key, name);
22138 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22139 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22140 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022141 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022142 v->di_tv.vval.v_number = nr;
22143}
22144
22145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 * ":return [expr]"
22147 */
22148 void
22149ex_return(eap)
22150 exarg_T *eap;
22151{
22152 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022153 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 int returning = FALSE;
22155
22156 if (current_funccal == NULL)
22157 {
22158 EMSG(_("E133: :return not inside a function"));
22159 return;
22160 }
22161
22162 if (eap->skip)
22163 ++emsg_skip;
22164
22165 eap->nextcmd = NULL;
22166 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022167 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 {
22169 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022170 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022172 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 }
22174 /* It's safer to return also on error. */
22175 else if (!eap->skip)
22176 {
22177 /*
22178 * Return unless the expression evaluation has been cancelled due to an
22179 * aborting error, an interrupt, or an exception.
22180 */
22181 if (!aborting())
22182 returning = do_return(eap, FALSE, TRUE, NULL);
22183 }
22184
22185 /* When skipping or the return gets pending, advance to the next command
22186 * in this line (!returning). Otherwise, ignore the rest of the line.
22187 * Following lines will be ignored by get_func_line(). */
22188 if (returning)
22189 eap->nextcmd = NULL;
22190 else if (eap->nextcmd == NULL) /* no argument */
22191 eap->nextcmd = check_nextcmd(arg);
22192
22193 if (eap->skip)
22194 --emsg_skip;
22195}
22196
22197/*
22198 * Return from a function. Possibly makes the return pending. Also called
22199 * for a pending return at the ":endtry" or after returning from an extra
22200 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022201 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022202 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 * FALSE when the return gets pending.
22204 */
22205 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022206do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 exarg_T *eap;
22208 int reanimate;
22209 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022210 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211{
22212 int idx;
22213 struct condstack *cstack = eap->cstack;
22214
22215 if (reanimate)
22216 /* Undo the return. */
22217 current_funccal->returned = FALSE;
22218
22219 /*
22220 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22221 * not in its finally clause (which then is to be executed next) is found.
22222 * In this case, make the ":return" pending for execution at the ":endtry".
22223 * Otherwise, return normally.
22224 */
22225 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22226 if (idx >= 0)
22227 {
22228 cstack->cs_pending[idx] = CSTP_RETURN;
22229
22230 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022231 /* A pending return again gets pending. "rettv" points to an
22232 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022234 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235 else
22236 {
22237 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022238 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022240 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022242 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243 {
22244 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022245 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022246 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 else
22248 EMSG(_(e_outofmem));
22249 }
22250 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022251 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252
22253 if (reanimate)
22254 {
22255 /* The pending return value could be overwritten by a ":return"
22256 * without argument in a finally clause; reset the default
22257 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022258 current_funccal->rettv->v_type = VAR_NUMBER;
22259 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 }
22261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022262 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 }
22264 else
22265 {
22266 current_funccal->returned = TRUE;
22267
22268 /* If the return is carried out now, store the return value. For
22269 * a return immediately after reanimation, the value is already
22270 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022271 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022273 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022274 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022276 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 }
22278 }
22279
22280 return idx < 0;
22281}
22282
22283/*
22284 * Free the variable with a pending return value.
22285 */
22286 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022287discard_pending_return(rettv)
22288 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289{
Bram Moolenaar33570922005-01-25 22:26:29 +000022290 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291}
22292
22293/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022294 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022295 * is an allocated string. Used by report_pending() for verbose messages.
22296 */
22297 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022298get_return_cmd(rettv)
22299 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022301 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022302 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022303 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022304
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022305 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022306 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022307 if (s == NULL)
22308 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022309
22310 STRCPY(IObuff, ":return ");
22311 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22312 if (STRLEN(s) + 8 >= IOSIZE)
22313 STRCPY(IObuff + IOSIZE - 4, "...");
22314 vim_free(tofree);
22315 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316}
22317
22318/*
22319 * Get next function line.
22320 * Called by do_cmdline() to get the next line.
22321 * Returns allocated string, or NULL for end of function.
22322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022323 char_u *
22324get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022325 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022327 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328{
Bram Moolenaar33570922005-01-25 22:26:29 +000022329 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022330 ufunc_T *fp = fcp->func;
22331 char_u *retval;
22332 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333
22334 /* If breakpoints have been added/deleted need to check for it. */
22335 if (fcp->dbg_tick != debug_tick)
22336 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022337 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 sourcing_lnum);
22339 fcp->dbg_tick = debug_tick;
22340 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022341#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022342 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022343 func_line_end(cookie);
22344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345
Bram Moolenaar05159a02005-02-26 23:04:13 +000022346 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022347 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22348 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349 retval = NULL;
22350 else
22351 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022352 /* Skip NULL lines (continuation lines). */
22353 while (fcp->linenr < gap->ga_len
22354 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22355 ++fcp->linenr;
22356 if (fcp->linenr >= gap->ga_len)
22357 retval = NULL;
22358 else
22359 {
22360 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22361 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022362#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022363 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022364 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022365#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 }
22368
22369 /* Did we encounter a breakpoint? */
22370 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22371 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022372 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022374 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 sourcing_lnum);
22376 fcp->dbg_tick = debug_tick;
22377 }
22378
22379 return retval;
22380}
22381
Bram Moolenaar05159a02005-02-26 23:04:13 +000022382#if defined(FEAT_PROFILE) || defined(PROTO)
22383/*
22384 * Called when starting to read a function line.
22385 * "sourcing_lnum" must be correct!
22386 * When skipping lines it may not actually be executed, but we won't find out
22387 * until later and we need to store the time now.
22388 */
22389 void
22390func_line_start(cookie)
22391 void *cookie;
22392{
22393 funccall_T *fcp = (funccall_T *)cookie;
22394 ufunc_T *fp = fcp->func;
22395
22396 if (fp->uf_profiling && sourcing_lnum >= 1
22397 && sourcing_lnum <= fp->uf_lines.ga_len)
22398 {
22399 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022400 /* Skip continuation lines. */
22401 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22402 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022403 fp->uf_tml_execed = FALSE;
22404 profile_start(&fp->uf_tml_start);
22405 profile_zero(&fp->uf_tml_children);
22406 profile_get_wait(&fp->uf_tml_wait);
22407 }
22408}
22409
22410/*
22411 * Called when actually executing a function line.
22412 */
22413 void
22414func_line_exec(cookie)
22415 void *cookie;
22416{
22417 funccall_T *fcp = (funccall_T *)cookie;
22418 ufunc_T *fp = fcp->func;
22419
22420 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22421 fp->uf_tml_execed = TRUE;
22422}
22423
22424/*
22425 * Called when done with a function line.
22426 */
22427 void
22428func_line_end(cookie)
22429 void *cookie;
22430{
22431 funccall_T *fcp = (funccall_T *)cookie;
22432 ufunc_T *fp = fcp->func;
22433
22434 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22435 {
22436 if (fp->uf_tml_execed)
22437 {
22438 ++fp->uf_tml_count[fp->uf_tml_idx];
22439 profile_end(&fp->uf_tml_start);
22440 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022441 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022442 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22443 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022444 }
22445 fp->uf_tml_idx = -1;
22446 }
22447}
22448#endif
22449
Bram Moolenaar071d4272004-06-13 20:20:40 +000022450/*
22451 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022452 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 */
22454 int
22455func_has_ended(cookie)
22456 void *cookie;
22457{
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022459
22460 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22461 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022462 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 || fcp->returned);
22464}
22465
22466/*
22467 * return TRUE if cookie indicates a function which "abort"s on errors.
22468 */
22469 int
22470func_has_abort(cookie)
22471 void *cookie;
22472{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022473 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474}
22475
22476#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22477typedef enum
22478{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022479 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22480 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22481 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482} var_flavour_T;
22483
22484static var_flavour_T var_flavour __ARGS((char_u *varname));
22485
22486 static var_flavour_T
22487var_flavour(varname)
22488 char_u *varname;
22489{
22490 char_u *p = varname;
22491
22492 if (ASCII_ISUPPER(*p))
22493 {
22494 while (*(++p))
22495 if (ASCII_ISLOWER(*p))
22496 return VAR_FLAVOUR_SESSION;
22497 return VAR_FLAVOUR_VIMINFO;
22498 }
22499 else
22500 return VAR_FLAVOUR_DEFAULT;
22501}
22502#endif
22503
22504#if defined(FEAT_VIMINFO) || defined(PROTO)
22505/*
22506 * Restore global vars that start with a capital from the viminfo file
22507 */
22508 int
22509read_viminfo_varlist(virp, writing)
22510 vir_T *virp;
22511 int writing;
22512{
22513 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022514 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022515 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516
22517 if (!writing && (find_viminfo_parameter('!') != NULL))
22518 {
22519 tab = vim_strchr(virp->vir_line + 1, '\t');
22520 if (tab != NULL)
22521 {
22522 *tab++ = '\0'; /* isolate the variable name */
22523 if (*tab == 'S') /* string var */
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022524 type = VAR_STRING;
22525#ifdef FEAT_FLOAT
22526 else if (*tab == 'F')
22527 type = VAR_FLOAT;
22528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529
22530 tab = vim_strchr(tab, '\t');
22531 if (tab != NULL)
22532 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022533 tv.v_type = type;
22534 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022535 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022537#ifdef FEAT_FLOAT
22538 else if (type == VAR_FLOAT)
22539 (void)string2float(tab + 1, &tv.vval.v_float);
22540#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022542 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022543 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022544 if (type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022545 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546 }
22547 }
22548 }
22549
22550 return viminfo_readline(virp);
22551}
22552
22553/*
22554 * Write global vars that start with a capital to the viminfo file
22555 */
22556 void
22557write_viminfo_varlist(fp)
22558 FILE *fp;
22559{
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 hashitem_T *hi;
22561 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022562 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022563 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022564 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022565 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022566 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022567
22568 if (find_viminfo_parameter('!') == NULL)
22569 return;
22570
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022571 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022572
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022573 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022574 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022576 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022578 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022579 this_var = HI2DI(hi);
22580 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022581 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022582 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022583 {
22584 case VAR_STRING: s = "STR"; break;
22585 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022586#ifdef FEAT_FLOAT
22587 case VAR_FLOAT: s = "FLO"; break;
22588#endif
Bram Moolenaara7043832005-01-21 11:56:39 +000022589 default: continue;
22590 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022591 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022592 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022593 if (p != NULL)
22594 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022595 vim_free(tofree);
22596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597 }
22598 }
22599}
22600#endif
22601
22602#if defined(FEAT_SESSION) || defined(PROTO)
22603 int
22604store_session_globals(fd)
22605 FILE *fd;
22606{
Bram Moolenaar33570922005-01-25 22:26:29 +000022607 hashitem_T *hi;
22608 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022609 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 char_u *p, *t;
22611
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022612 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022613 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022615 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022617 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022618 this_var = HI2DI(hi);
22619 if ((this_var->di_tv.v_type == VAR_NUMBER
22620 || this_var->di_tv.v_type == VAR_STRING)
22621 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022622 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022623 /* Escape special characters with a backslash. Turn a LF and
22624 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022625 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022626 (char_u *)"\\\"\n\r");
22627 if (p == NULL) /* out of memory */
22628 break;
22629 for (t = p; *t != NUL; ++t)
22630 if (*t == '\n')
22631 *t = 'n';
22632 else if (*t == '\r')
22633 *t = 'r';
22634 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022635 this_var->di_key,
22636 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22637 : ' ',
22638 p,
22639 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22640 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022641 || put_eol(fd) == FAIL)
22642 {
22643 vim_free(p);
22644 return FAIL;
22645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 vim_free(p);
22647 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022648#ifdef FEAT_FLOAT
22649 else if (this_var->di_tv.v_type == VAR_FLOAT
22650 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22651 {
22652 float_T f = this_var->di_tv.vval.v_float;
22653 int sign = ' ';
22654
22655 if (f < 0)
22656 {
22657 f = -f;
22658 sign = '-';
22659 }
22660 if ((fprintf(fd, "let %s = %c&%f",
22661 this_var->di_key, sign, f) < 0)
22662 || put_eol(fd) == FAIL)
22663 return FAIL;
22664 }
22665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022666 }
22667 }
22668 return OK;
22669}
22670#endif
22671
Bram Moolenaar661b1822005-07-28 22:36:45 +000022672/*
22673 * Display script name where an item was last set.
22674 * Should only be invoked when 'verbose' is non-zero.
22675 */
22676 void
22677last_set_msg(scriptID)
22678 scid_T scriptID;
22679{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022680 char_u *p;
22681
Bram Moolenaar661b1822005-07-28 22:36:45 +000022682 if (scriptID != 0)
22683 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022684 p = home_replace_save(NULL, get_scriptname(scriptID));
22685 if (p != NULL)
22686 {
22687 verbose_enter();
22688 MSG_PUTS(_("\n\tLast set from "));
22689 MSG_PUTS(p);
22690 vim_free(p);
22691 verbose_leave();
22692 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022693 }
22694}
22695
Bram Moolenaard812df62008-11-09 12:46:09 +000022696/*
22697 * List v:oldfiles in a nice way.
22698 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022699 void
22700ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022701 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022702{
22703 list_T *l = vimvars[VV_OLDFILES].vv_list;
22704 listitem_T *li;
22705 int nr = 0;
22706
22707 if (l == NULL)
22708 msg((char_u *)_("No old files"));
22709 else
22710 {
22711 msg_start();
22712 msg_scroll = TRUE;
22713 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22714 {
22715 msg_outnum((long)++nr);
22716 MSG_PUTS(": ");
22717 msg_outtrans(get_tv_string(&li->li_tv));
22718 msg_putchar('\n');
22719 out_flush(); /* output one line at a time */
22720 ui_breakcheck();
22721 }
22722 /* Assume "got_int" was set to truncate the listing. */
22723 got_int = FALSE;
22724
22725#ifdef FEAT_BROWSE_CMD
22726 if (cmdmod.browse)
22727 {
22728 quit_more = FALSE;
22729 nr = prompt_for_number(FALSE);
22730 msg_starthere();
22731 if (nr > 0)
22732 {
22733 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22734 (long)nr);
22735
22736 if (p != NULL)
22737 {
22738 p = expand_env_save(p);
22739 eap->arg = p;
22740 eap->cmdidx = CMD_edit;
22741 cmdmod.browse = FALSE;
22742 do_exedit(eap, NULL);
22743 vim_free(p);
22744 }
22745 }
22746 }
22747#endif
22748 }
22749}
22750
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751#endif /* FEAT_EVAL */
22752
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022754#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755
22756#ifdef WIN3264
22757/*
22758 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22759 */
22760static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22761static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22762static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22763
22764/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022765 * Get the short path (8.3) for the filename in "fnamep".
22766 * Only works for a valid file name.
22767 * When the path gets longer "fnamep" is changed and the allocated buffer
22768 * is put in "bufp".
22769 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22770 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 */
22772 static int
22773get_short_pathname(fnamep, bufp, fnamelen)
22774 char_u **fnamep;
22775 char_u **bufp;
22776 int *fnamelen;
22777{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022778 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 char_u *newbuf;
22780
22781 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 l = GetShortPathName(*fnamep, *fnamep, len);
22783 if (l > len - 1)
22784 {
22785 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022786 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022787 newbuf = vim_strnsave(*fnamep, l);
22788 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022789 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790
22791 vim_free(*bufp);
22792 *fnamep = *bufp = newbuf;
22793
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022794 /* Really should always succeed, as the buffer is big enough. */
22795 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 }
22797
22798 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022799 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022800}
22801
22802/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022803 * Get the short path (8.3) for the filename in "fname". The converted
22804 * path is returned in "bufp".
22805 *
22806 * Some of the directories specified in "fname" may not exist. This function
22807 * will shorten the existing directories at the beginning of the path and then
22808 * append the remaining non-existing path.
22809 *
22810 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022811 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022812 * bufp - Pointer to an allocated buffer for the filename.
22813 * fnamelen - Length of the filename pointed to by fname
22814 *
22815 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816 */
22817 static int
22818shortpath_for_invalid_fname(fname, bufp, fnamelen)
22819 char_u **fname;
22820 char_u **bufp;
22821 int *fnamelen;
22822{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022823 char_u *short_fname, *save_fname, *pbuf_unused;
22824 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022826 int old_len, len;
22827 int new_len, sfx_len;
22828 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829
22830 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022831 old_len = *fnamelen;
22832 save_fname = vim_strnsave(*fname, old_len);
22833 pbuf_unused = NULL;
22834 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022836 endp = save_fname + old_len - 1; /* Find the end of the copy */
22837 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022839 /*
22840 * Try shortening the supplied path till it succeeds by removing one
22841 * directory at a time from the tail of the path.
22842 */
22843 len = 0;
22844 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022846 /* go back one path-separator */
22847 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22848 --endp;
22849 if (endp <= save_fname)
22850 break; /* processed the complete path */
22851
22852 /*
22853 * Replace the path separator with a NUL and try to shorten the
22854 * resulting path.
22855 */
22856 ch = *endp;
22857 *endp = 0;
22858 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022859 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022860 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22861 {
22862 retval = FAIL;
22863 goto theend;
22864 }
22865 *endp = ch; /* preserve the string */
22866
22867 if (len > 0)
22868 break; /* successfully shortened the path */
22869
22870 /* failed to shorten the path. Skip the path separator */
22871 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 }
22873
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022874 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022875 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022876 /*
22877 * Succeeded in shortening the path. Now concatenate the shortened
22878 * path with the remaining path at the tail.
22879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022881 /* Compute the length of the new path. */
22882 sfx_len = (int)(save_endp - endp) + 1;
22883 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022885 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022887 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022888 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022889 /* There is not enough space in the currently allocated string,
22890 * copy it to a buffer big enough. */
22891 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022893 {
22894 retval = FAIL;
22895 goto theend;
22896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 }
22898 else
22899 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022900 /* Transfer short_fname to the main buffer (it's big enough),
22901 * unless get_short_pathname() did its work in-place. */
22902 *fname = *bufp = save_fname;
22903 if (short_fname != save_fname)
22904 vim_strncpy(save_fname, short_fname, len);
22905 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022907
22908 /* concat the not-shortened part of the path */
22909 vim_strncpy(*fname + len, endp, sfx_len);
22910 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022912
22913theend:
22914 vim_free(pbuf_unused);
22915 vim_free(save_fname);
22916
22917 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918}
22919
22920/*
22921 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022922 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 */
22924 static int
22925shortpath_for_partial(fnamep, bufp, fnamelen)
22926 char_u **fnamep;
22927 char_u **bufp;
22928 int *fnamelen;
22929{
22930 int sepcount, len, tflen;
22931 char_u *p;
22932 char_u *pbuf, *tfname;
22933 int hasTilde;
22934
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022935 /* Count up the path separators from the RHS.. so we know which part
22936 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022938 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 if (vim_ispathsep(*p))
22940 ++sepcount;
22941
22942 /* Need full path first (use expand_env() to remove a "~/") */
22943 hasTilde = (**fnamep == '~');
22944 if (hasTilde)
22945 pbuf = tfname = expand_env_save(*fnamep);
22946 else
22947 pbuf = tfname = FullName_save(*fnamep, FALSE);
22948
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022949 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022951 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
22952 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953
22954 if (len == 0)
22955 {
22956 /* Don't have a valid filename, so shorten the rest of the
22957 * path if we can. This CAN give us invalid 8.3 filenames, but
22958 * there's not a lot of point in guessing what it might be.
22959 */
22960 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022961 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
22962 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963 }
22964
22965 /* Count the paths backward to find the beginning of the desired string. */
22966 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022967 {
22968#ifdef FEAT_MBYTE
22969 if (has_mbyte)
22970 p -= mb_head_off(tfname, p);
22971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 if (vim_ispathsep(*p))
22973 {
22974 if (sepcount == 0 || (hasTilde && sepcount == 1))
22975 break;
22976 else
22977 sepcount --;
22978 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000022979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 if (hasTilde)
22981 {
22982 --p;
22983 if (p >= tfname)
22984 *p = '~';
22985 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022986 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022987 }
22988 else
22989 ++p;
22990
22991 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22992 vim_free(*bufp);
22993 *fnamelen = (int)STRLEN(p);
22994 *bufp = pbuf;
22995 *fnamep = p;
22996
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022997 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022998}
22999#endif /* WIN3264 */
23000
23001/*
23002 * Adjust a filename, according to a string of modifiers.
23003 * *fnamep must be NUL terminated when called. When returning, the length is
23004 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023005 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 * When there is an error, *fnamep is set to NULL.
23007 */
23008 int
23009modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23010 char_u *src; /* string with modifiers */
23011 int *usedlen; /* characters after src that are used */
23012 char_u **fnamep; /* file name so far */
23013 char_u **bufp; /* buffer for allocated file name or NULL */
23014 int *fnamelen; /* length of fnamep */
23015{
23016 int valid = 0;
23017 char_u *tail;
23018 char_u *s, *p, *pbuf;
23019 char_u dirname[MAXPATHL];
23020 int c;
23021 int has_fullname = 0;
23022#ifdef WIN3264
23023 int has_shortname = 0;
23024#endif
23025
23026repeat:
23027 /* ":p" - full path/file_name */
23028 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23029 {
23030 has_fullname = 1;
23031
23032 valid |= VALID_PATH;
23033 *usedlen += 2;
23034
23035 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23036 if ((*fnamep)[0] == '~'
23037#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23038 && ((*fnamep)[1] == '/'
23039# ifdef BACKSLASH_IN_FILENAME
23040 || (*fnamep)[1] == '\\'
23041# endif
23042 || (*fnamep)[1] == NUL)
23043
23044#endif
23045 )
23046 {
23047 *fnamep = expand_env_save(*fnamep);
23048 vim_free(*bufp); /* free any allocated file name */
23049 *bufp = *fnamep;
23050 if (*fnamep == NULL)
23051 return -1;
23052 }
23053
23054 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023055 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023056 {
23057 if (vim_ispathsep(*p)
23058 && p[1] == '.'
23059 && (p[2] == NUL
23060 || vim_ispathsep(p[2])
23061 || (p[2] == '.'
23062 && (p[3] == NUL || vim_ispathsep(p[3])))))
23063 break;
23064 }
23065
23066 /* FullName_save() is slow, don't use it when not needed. */
23067 if (*p != NUL || !vim_isAbsName(*fnamep))
23068 {
23069 *fnamep = FullName_save(*fnamep, *p != NUL);
23070 vim_free(*bufp); /* free any allocated file name */
23071 *bufp = *fnamep;
23072 if (*fnamep == NULL)
23073 return -1;
23074 }
23075
23076 /* Append a path separator to a directory. */
23077 if (mch_isdir(*fnamep))
23078 {
23079 /* Make room for one or two extra characters. */
23080 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23081 vim_free(*bufp); /* free any allocated file name */
23082 *bufp = *fnamep;
23083 if (*fnamep == NULL)
23084 return -1;
23085 add_pathsep(*fnamep);
23086 }
23087 }
23088
23089 /* ":." - path relative to the current directory */
23090 /* ":~" - path relative to the home directory */
23091 /* ":8" - shortname path - postponed till after */
23092 while (src[*usedlen] == ':'
23093 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23094 {
23095 *usedlen += 2;
23096 if (c == '8')
23097 {
23098#ifdef WIN3264
23099 has_shortname = 1; /* Postpone this. */
23100#endif
23101 continue;
23102 }
23103 pbuf = NULL;
23104 /* Need full path first (use expand_env() to remove a "~/") */
23105 if (!has_fullname)
23106 {
23107 if (c == '.' && **fnamep == '~')
23108 p = pbuf = expand_env_save(*fnamep);
23109 else
23110 p = pbuf = FullName_save(*fnamep, FALSE);
23111 }
23112 else
23113 p = *fnamep;
23114
23115 has_fullname = 0;
23116
23117 if (p != NULL)
23118 {
23119 if (c == '.')
23120 {
23121 mch_dirname(dirname, MAXPATHL);
23122 s = shorten_fname(p, dirname);
23123 if (s != NULL)
23124 {
23125 *fnamep = s;
23126 if (pbuf != NULL)
23127 {
23128 vim_free(*bufp); /* free any allocated file name */
23129 *bufp = pbuf;
23130 pbuf = NULL;
23131 }
23132 }
23133 }
23134 else
23135 {
23136 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23137 /* Only replace it when it starts with '~' */
23138 if (*dirname == '~')
23139 {
23140 s = vim_strsave(dirname);
23141 if (s != NULL)
23142 {
23143 *fnamep = s;
23144 vim_free(*bufp);
23145 *bufp = s;
23146 }
23147 }
23148 }
23149 vim_free(pbuf);
23150 }
23151 }
23152
23153 tail = gettail(*fnamep);
23154 *fnamelen = (int)STRLEN(*fnamep);
23155
23156 /* ":h" - head, remove "/file_name", can be repeated */
23157 /* Don't remove the first "/" or "c:\" */
23158 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23159 {
23160 valid |= VALID_HEAD;
23161 *usedlen += 2;
23162 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023163 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023164 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023165 *fnamelen = (int)(tail - *fnamep);
23166#ifdef VMS
23167 if (*fnamelen > 0)
23168 *fnamelen += 1; /* the path separator is part of the path */
23169#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023170 if (*fnamelen == 0)
23171 {
23172 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23173 p = vim_strsave((char_u *)".");
23174 if (p == NULL)
23175 return -1;
23176 vim_free(*bufp);
23177 *bufp = *fnamep = tail = p;
23178 *fnamelen = 1;
23179 }
23180 else
23181 {
23182 while (tail > s && !after_pathsep(s, tail))
23183 mb_ptr_back(*fnamep, tail);
23184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185 }
23186
23187 /* ":8" - shortname */
23188 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23189 {
23190 *usedlen += 2;
23191#ifdef WIN3264
23192 has_shortname = 1;
23193#endif
23194 }
23195
23196#ifdef WIN3264
23197 /* Check shortname after we have done 'heads' and before we do 'tails'
23198 */
23199 if (has_shortname)
23200 {
23201 pbuf = NULL;
23202 /* Copy the string if it is shortened by :h */
23203 if (*fnamelen < (int)STRLEN(*fnamep))
23204 {
23205 p = vim_strnsave(*fnamep, *fnamelen);
23206 if (p == 0)
23207 return -1;
23208 vim_free(*bufp);
23209 *bufp = *fnamep = p;
23210 }
23211
23212 /* Split into two implementations - makes it easier. First is where
23213 * there isn't a full name already, second is where there is.
23214 */
23215 if (!has_fullname && !vim_isAbsName(*fnamep))
23216 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023217 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218 return -1;
23219 }
23220 else
23221 {
23222 int l;
23223
23224 /* Simple case, already have the full-name
23225 * Nearly always shorter, so try first time. */
23226 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023227 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 return -1;
23229
23230 if (l == 0)
23231 {
23232 /* Couldn't find the filename.. search the paths.
23233 */
23234 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023235 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 return -1;
23237 }
23238 *fnamelen = l;
23239 }
23240 }
23241#endif /* WIN3264 */
23242
23243 /* ":t" - tail, just the basename */
23244 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23245 {
23246 *usedlen += 2;
23247 *fnamelen -= (int)(tail - *fnamep);
23248 *fnamep = tail;
23249 }
23250
23251 /* ":e" - extension, can be repeated */
23252 /* ":r" - root, without extension, can be repeated */
23253 while (src[*usedlen] == ':'
23254 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23255 {
23256 /* find a '.' in the tail:
23257 * - for second :e: before the current fname
23258 * - otherwise: The last '.'
23259 */
23260 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23261 s = *fnamep - 2;
23262 else
23263 s = *fnamep + *fnamelen - 1;
23264 for ( ; s > tail; --s)
23265 if (s[0] == '.')
23266 break;
23267 if (src[*usedlen + 1] == 'e') /* :e */
23268 {
23269 if (s > tail)
23270 {
23271 *fnamelen += (int)(*fnamep - (s + 1));
23272 *fnamep = s + 1;
23273#ifdef VMS
23274 /* cut version from the extension */
23275 s = *fnamep + *fnamelen - 1;
23276 for ( ; s > *fnamep; --s)
23277 if (s[0] == ';')
23278 break;
23279 if (s > *fnamep)
23280 *fnamelen = s - *fnamep;
23281#endif
23282 }
23283 else if (*fnamep <= tail)
23284 *fnamelen = 0;
23285 }
23286 else /* :r */
23287 {
23288 if (s > tail) /* remove one extension */
23289 *fnamelen = (int)(s - *fnamep);
23290 }
23291 *usedlen += 2;
23292 }
23293
23294 /* ":s?pat?foo?" - substitute */
23295 /* ":gs?pat?foo?" - global substitute */
23296 if (src[*usedlen] == ':'
23297 && (src[*usedlen + 1] == 's'
23298 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23299 {
23300 char_u *str;
23301 char_u *pat;
23302 char_u *sub;
23303 int sep;
23304 char_u *flags;
23305 int didit = FALSE;
23306
23307 flags = (char_u *)"";
23308 s = src + *usedlen + 2;
23309 if (src[*usedlen + 1] == 'g')
23310 {
23311 flags = (char_u *)"g";
23312 ++s;
23313 }
23314
23315 sep = *s++;
23316 if (sep)
23317 {
23318 /* find end of pattern */
23319 p = vim_strchr(s, sep);
23320 if (p != NULL)
23321 {
23322 pat = vim_strnsave(s, (int)(p - s));
23323 if (pat != NULL)
23324 {
23325 s = p + 1;
23326 /* find end of substitution */
23327 p = vim_strchr(s, sep);
23328 if (p != NULL)
23329 {
23330 sub = vim_strnsave(s, (int)(p - s));
23331 str = vim_strnsave(*fnamep, *fnamelen);
23332 if (sub != NULL && str != NULL)
23333 {
23334 *usedlen = (int)(p + 1 - src);
23335 s = do_string_sub(str, pat, sub, flags);
23336 if (s != NULL)
23337 {
23338 *fnamep = s;
23339 *fnamelen = (int)STRLEN(s);
23340 vim_free(*bufp);
23341 *bufp = s;
23342 didit = TRUE;
23343 }
23344 }
23345 vim_free(sub);
23346 vim_free(str);
23347 }
23348 vim_free(pat);
23349 }
23350 }
23351 /* after using ":s", repeat all the modifiers */
23352 if (didit)
23353 goto repeat;
23354 }
23355 }
23356
23357 return valid;
23358}
23359
23360/*
23361 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23362 * "flags" can be "g" to do a global substitute.
23363 * Returns an allocated string, NULL for error.
23364 */
23365 char_u *
23366do_string_sub(str, pat, sub, flags)
23367 char_u *str;
23368 char_u *pat;
23369 char_u *sub;
23370 char_u *flags;
23371{
23372 int sublen;
23373 regmatch_T regmatch;
23374 int i;
23375 int do_all;
23376 char_u *tail;
23377 garray_T ga;
23378 char_u *ret;
23379 char_u *save_cpo;
23380
23381 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23382 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023383 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384
23385 ga_init2(&ga, 1, 200);
23386
23387 do_all = (flags[0] == 'g');
23388
23389 regmatch.rm_ic = p_ic;
23390 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23391 if (regmatch.regprog != NULL)
23392 {
23393 tail = str;
23394 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23395 {
23396 /*
23397 * Get some space for a temporary buffer to do the substitution
23398 * into. It will contain:
23399 * - The text up to where the match is.
23400 * - The substituted text.
23401 * - The text after the match.
23402 */
23403 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23404 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23405 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23406 {
23407 ga_clear(&ga);
23408 break;
23409 }
23410
23411 /* copy the text up to where the match is */
23412 i = (int)(regmatch.startp[0] - tail);
23413 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23414 /* add the substituted text */
23415 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23416 + ga.ga_len + i, TRUE, TRUE, FALSE);
23417 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 /* avoid getting stuck on a match with an empty string */
23419 if (tail == regmatch.endp[0])
23420 {
23421 if (*tail == NUL)
23422 break;
23423 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23424 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 }
23426 else
23427 {
23428 tail = regmatch.endp[0];
23429 if (*tail == NUL)
23430 break;
23431 }
23432 if (!do_all)
23433 break;
23434 }
23435
23436 if (ga.ga_data != NULL)
23437 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23438
23439 vim_free(regmatch.regprog);
23440 }
23441
23442 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23443 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023444 if (p_cpo == empty_option)
23445 p_cpo = save_cpo;
23446 else
23447 /* Darn, evaluating {sub} expression changed the value. */
23448 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449
23450 return ret;
23451}
23452
23453#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */