blob: a17d743e812e73d70f5563eba317465fdf126055 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarc236c162008-07-13 17:41:49 +000013#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
Bram Moolenaar362e1a32006-03-06 23:29:24 +000014# include "vimio.h" /* for mch_open(), must be before vim.h */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015#endif
16
17#include "vim.h"
18
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019#if defined(FEAT_EVAL) || defined(PROTO)
20
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#ifdef AMIGA
22# include <time.h> /* for strftime() */
23#endif
24
Bram Moolenaar314f11d2010-08-09 22:07:08 +020025#ifdef VMS
26# include <float.h>
27#endif
28
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef MACOS
30# include <time.h> /* for time_t */
31#endif
32
Bram Moolenaar8c8de832008-06-24 22:58:06 +000033#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
34# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#endif
36
Bram Moolenaar33570922005-01-25 22:26:29 +000037#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000039#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
40 be freed. */
41
Bram Moolenaar071d4272004-06-13 20:20:40 +000042/*
Bram Moolenaar33570922005-01-25 22:26:29 +000043 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
44 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000045 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
46 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
47 * HI2DI() converts a hashitem pointer to a dictitem pointer.
48 */
Bram Moolenaar33570922005-01-25 22:26:29 +000049static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000050#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000051#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000052#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000053
54/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000055 * Structure returned by get_lval() and used by set_var_lval().
56 * For a plain name:
57 * "name" points to the variable name.
58 * "exp_name" is NULL.
59 * "tv" is NULL
60 * For a magic braces name:
61 * "name" points to the expanded variable name.
62 * "exp_name" is non-NULL, to be freed later.
63 * "tv" is NULL
64 * For an index in a list:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the (first) list item value
68 * "li" points to the (first) list item
69 * "range", "n1", "n2" and "empty2" indicate what items are used.
70 * For an existing Dict item:
71 * "name" points to the (expanded) variable name.
72 * "exp_name" NULL or non-NULL, to be freed later.
73 * "tv" points to the dict item value
74 * "newkey" is NULL
75 * For a non-existing Dict item:
76 * "name" points to the (expanded) variable name.
77 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000078 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 * "newkey" is the key for the new item.
80 */
81typedef struct lval_S
82{
83 char_u *ll_name; /* start of variable name (can be NULL) */
84 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000086 isn't NULL it's the Dict to which to add
87 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 listitem_T *ll_li; /* The list item or NULL. */
89 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000090 int ll_range; /* TRUE when a [i:j] range was used */
91 long ll_n1; /* First index for list */
92 long ll_n2; /* Second index for list range */
93 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000094 dict_T *ll_dict; /* The Dictionary or NULL */
95 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000096 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000097} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000098
Bram Moolenaar8c711452005-01-14 21:53:12 +000099
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000100static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000101static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000102static char *e_undefvar = N_("E121: Undefined variable: %s");
103static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000105static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000106static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_listreq = N_("E714: List required");
108static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000109static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000110static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
111static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
112static char *e_funcdict = N_("E717: Dictionary entry already exists");
113static char *e_funcref = N_("E718: Funcref required");
114static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
115static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000116static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000117static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000119/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000120 * All user-defined global variables are stored in dictionary "globvardict".
121 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000123static dict_T globvardict;
124static dictitem_T globvars_var;
125#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000128 * Old Vim variables such as "v:version" are also available without the "v:".
129 * Also in functions. We need a special hashtable for them.
130 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000131static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000132
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200133/* When using exists() don't auto-load a script. */
134static int no_autoload = FALSE;
135
Bram Moolenaar532c7802005-01-27 14:44:31 +0000136/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137 * When recursively copying lists and dicts we need to remember which ones we
138 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000139 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140 */
141static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000142#define COPYID_INC 2
143#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000144
145/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000146 * Array to hold the hashtab with variables local to each sourced script.
147 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000149typedef struct
150{
151 dictitem_T sv_var;
152 dict_T sv_dict;
153} scriptvar_T;
154
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200155static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
156#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
157#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158
159static int echo_attr = 0; /* attributes used for ":echo" */
160
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161/* Values for trans_function_name() argument: */
162#define TFN_INT 1 /* internal function name OK */
163#define TFN_QUIET 2 /* no error messages */
164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215/* list heads for garbage collection */
216static dict_T *first_dict = NULL; /* list of all dicts */
217static list_T *first_list = NULL; /* list of all lists */
218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219/* From user function to hashitem and back. */
220static ufunc_T dumuf;
221#define UF2HIKEY(fp) ((fp)->uf_name)
222#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
223#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
224
225#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
226#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227
Bram Moolenaar33570922005-01-25 22:26:29 +0000228#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
229#define VAR_SHORT_LEN 20 /* short variable name length */
230#define FIXVAR_CNT 12 /* number of fixed variables */
231
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000233typedef struct funccall_S funccall_T;
234
235struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236{
237 ufunc_T *func; /* function being called */
238 int linenr; /* next line to be executed */
239 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000240 struct /* fixed variables for arguments */
241 {
242 dictitem_T var; /* variable (without room for name) */
243 char_u room[VAR_SHORT_LEN]; /* room for the name */
244 } fixvar[FIXVAR_CNT];
245 dict_T l_vars; /* l: local function variables */
246 dictitem_T l_vars_var; /* variable for l: scope */
247 dict_T l_avars; /* a: argument variables */
248 dictitem_T l_avars_var; /* variable for a: scope */
249 list_T l_varlist; /* list for a:000 */
250 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
251 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 linenr_T breakpoint; /* next line with breakpoint or zero */
253 int dbg_tick; /* debug_tick when breakpoint was set */
254 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000255#ifdef FEAT_PROFILE
256 proftime_T prof_child; /* time spent in a child */
257#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000258 funccall_T *caller; /* calling function or NULL */
259};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262 * Info used by a ":for" loop.
263 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000264typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000265{
266 int fi_semicolon; /* TRUE if ending in '; var]' */
267 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000268 listwatch_T fi_lw; /* keep an eye on the item used. */
269 list_T *fi_list; /* list being used */
270} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000272/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000273 * Struct used by trans_function_name()
274 */
275typedef struct
276{
Bram Moolenaar33570922005-01-25 22:26:29 +0000277 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000278 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dictitem_T *fd_di; /* Dictionary item used */
280} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000281
Bram Moolenaara7043832005-01-21 11:56:39 +0000282
283/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000284 * Array to hold the value of v: variables.
285 * The value is in a dictitem, so that it can also be used in the v: scope.
286 * The reason to use this table anyway is for very quick access to the
287 * variables with the VV_ defines.
288 */
289#include "version.h"
290
291/* values for vv_flags: */
292#define VV_COMPAT 1 /* compatible, also used without "v:" */
293#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000294#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000295
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000296#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
298static struct vimvar
299{
300 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 dictitem_T vv_di; /* value and name for key */
302 char vv_filler[16]; /* space for LONGEST name below!!! */
303 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
304} vimvars[VV_LEN] =
305{
306 /*
307 * The order here must match the VV_ defines in vim.h!
308 * Initializing a union does not work, leave tv.vval empty to get zero's.
309 */
310 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
311 {VV_NAME("count1", VAR_NUMBER), VV_RO},
312 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
313 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
314 {VV_NAME("warningmsg", VAR_STRING), 0},
315 {VV_NAME("statusmsg", VAR_STRING), 0},
316 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
318 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
319 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("termresponse", VAR_STRING), VV_RO},
321 {VV_NAME("fname", VAR_STRING), VV_RO},
322 {VV_NAME("lang", VAR_STRING), VV_RO},
323 {VV_NAME("lc_time", VAR_STRING), VV_RO},
324 {VV_NAME("ctype", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
327 {VV_NAME("fname_in", VAR_STRING), VV_RO},
328 {VV_NAME("fname_out", VAR_STRING), VV_RO},
329 {VV_NAME("fname_new", VAR_STRING), VV_RO},
330 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
331 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
332 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
335 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
336 {VV_NAME("progname", VAR_STRING), VV_RO},
337 {VV_NAME("servername", VAR_STRING), VV_RO},
338 {VV_NAME("dying", VAR_NUMBER), VV_RO},
339 {VV_NAME("exception", VAR_STRING), VV_RO},
340 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
341 {VV_NAME("register", VAR_STRING), VV_RO},
342 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
343 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000344 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
345 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000346 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000347 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
348 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000349 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000354 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000355 {VV_NAME("swapname", VAR_STRING), VV_RO},
356 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000357 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000358 {VV_NAME("char", VAR_STRING), VV_RO},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000359 {VV_NAME("mouse_win", VAR_NUMBER), 0},
360 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
361 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000362 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000363 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000366};
367
368/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_type vv_di.di_tv.v_type
370#define vv_nr vv_di.di_tv.vval.v_number
371#define vv_float vv_di.di_tv.vval.v_float
372#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000373#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000374#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000375
376/*
377 * The v: variables are stored in dictionary "vimvardict".
378 * "vimvars_var" is the variable that is used for the "l:" scope.
379 */
380static dict_T vimvardict;
381static dictitem_T vimvars_var;
382#define vimvarht vimvardict.dv_hashtab
383
Bram Moolenaara40058a2005-07-11 22:42:07 +0000384static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
385static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
386#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
387static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
388#endif
389static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
390static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
391static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000392static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
393static void list_glob_vars __ARGS((int *first));
394static void list_buf_vars __ARGS((int *first));
395static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000396#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000397static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000398#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000399static void list_vim_vars __ARGS((int *first));
400static void list_script_vars __ARGS((int *first));
401static void list_func_vars __ARGS((int *first));
402static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000403static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
404static int check_changedtick __ARGS((char_u *arg));
405static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
406static void clear_lval __ARGS((lval_T *lp));
407static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
408static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
409static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
410static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
411static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
412static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
413static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
414static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
415static void item_lock __ARGS((typval_T *tv, int deep, int lock));
416static int tv_islocked __ARGS((typval_T *tv));
417
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
419static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
420static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
421static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
422static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
423static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000424static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
425static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000426
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000427static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000428static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
429static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
430static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
431static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000432static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static listitem_T *listitem_alloc __ARGS((void));
434static void listitem_free __ARGS((listitem_T *item));
435static void listitem_remove __ARGS((list_T *l, listitem_T *item));
436static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100437static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
438static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
439static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaar33570922005-01-25 22:26:29 +0000440static listitem_T *list_find __ARGS((list_T *l, long n));
Bram Moolenaara5525202006-03-02 22:52:09 +0000441static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000442static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443static void list_append __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000444static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
446static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
447static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000448static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000449static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000450static char_u *list2string __ARGS((typval_T *tv, int copyID));
451static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000452static int free_unref_items __ARGS((int copyID));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000453static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
454static void set_ref_in_list __ARGS((list_T *l, int copyID));
455static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200456static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static void dict_unref __ARGS((dict_T *d));
Bram Moolenaar685295c2006-10-15 20:37:38 +0000458static void dict_free __ARGS((dict_T *d, int recurse));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
460static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000461static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000462static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000463static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000464static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000465static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
466static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000467static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000468#ifdef FEAT_FLOAT
469static int string2float __ARGS((char_u *text, float_T *value));
470#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000471static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
472static int find_internal_func __ARGS((char_u *name));
473static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
474static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200475static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000476static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000477static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000478
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
480static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200481static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000482#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000488#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200489static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200491static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000492#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000493static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000504#ifdef FEAT_FLOAT
505static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
506#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000507static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000508static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000510static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000512#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000513static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000514static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
516#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000519#ifdef FEAT_FLOAT
520static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200521static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000522#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000523static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
526static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200537#ifdef FEAT_FLOAT
538static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
539#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000542static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000548#ifdef FEAT_FLOAT
549static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000552#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000553static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000562static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000564static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000570static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000578static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000579static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000580static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000581static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200584static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000585static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000593static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000594static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000607static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000613static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000625#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200626static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000627static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
628#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000633static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000634static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000635static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000637static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
639static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
640static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000641#ifdef vim_mkdir
642static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
643#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100645#ifdef FEAT_MZSCHEME
646static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
647#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000650static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000651#ifdef FEAT_FLOAT
652static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
653#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000654static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000655static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000656static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000657static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000658static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000659static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
660static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
662static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
664static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
666static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
667static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
669static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#ifdef FEAT_FLOAT
672static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
673#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000674static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000675static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000677static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
678static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000679static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
680static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
681static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
682static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
683static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000684static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000685static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000686static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000687static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200689static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000690static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000692static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000693static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#ifdef FEAT_FLOAT
695static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200696static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000699static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000700static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000702static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
704static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
705static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
706#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000707static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200708static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000709#ifdef HAVE_STRFTIME
710static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
711#endif
712static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
714static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200718static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200719static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000720static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
721static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
722static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
723static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
724static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000725static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200726static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000728static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000729static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000730static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000731static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000732static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000734static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200735#ifdef FEAT_FLOAT
736static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
740static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
741static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000742#ifdef FEAT_FLOAT
743static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
744#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000745static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200746static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200747static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
754static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
756static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000757static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
758static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000760static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000761
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000762static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000763static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000764static int get_env_len __ARGS((char_u **arg));
765static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000766static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000767static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
768#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
769#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
770 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000771static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000773static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000774static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
775static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static typval_T *alloc_tv __ARGS((void));
777static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static void init_tv __ARGS((typval_T *varp));
779static long get_tv_number __ARGS((typval_T *varp));
780static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000781static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000782static char_u *get_tv_string __ARGS((typval_T *varp));
783static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000784static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000786static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000787static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
788static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
789static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000790static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
791static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000792static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
793static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000794static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000795static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000796static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
798static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
799static int eval_fname_script __ARGS((char_u *p));
800static int eval_fname_sid __ARGS((char_u *p));
801static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static ufunc_T *find_func __ARGS((char_u *name));
803static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000804static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#ifdef FEAT_PROFILE
806static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000807static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
808static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_total_cmp __ARGS((const void *s1, const void *s2));
814static int
815# ifdef __BORLANDC__
816 _RTLENTRYF
817# endif
818 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000820static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000821static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000822static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void func_free __ARGS((ufunc_T *fp));
824static void func_unref __ARGS((char_u *name));
825static void func_ref __ARGS((char_u *name));
826static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000827static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
828static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000829static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000830static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
831static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000832static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000833static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000834static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000835
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200836
837#ifdef EBCDIC
838static int compare_func_name __ARGS((const void *s1, const void *s2));
839static void sortFunctions __ARGS(());
840#endif
841
842
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000843/* Character used as separated in autoload function/variable names. */
844#define AUTOLOAD_CHAR '#'
845
Bram Moolenaar33570922005-01-25 22:26:29 +0000846/*
847 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000848 */
849 void
850eval_init()
851{
Bram Moolenaar33570922005-01-25 22:26:29 +0000852 int i;
853 struct vimvar *p;
854
855 init_var_dict(&globvardict, &globvars_var);
856 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000857 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000858 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
860 for (i = 0; i < VV_LEN; ++i)
861 {
862 p = &vimvars[i];
863 STRCPY(p->vv_di.di_key, p->vv_name);
864 if (p->vv_flags & VV_RO)
865 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
866 else if (p->vv_flags & VV_RO_SBX)
867 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
868 else
869 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000870
871 /* add to v: scope dict, unless the value is not always available */
872 if (p->vv_type != VAR_UNKNOWN)
873 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000874 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000875 /* add to compat scope dict */
876 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000878 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879
880#ifdef EBCDIC
881 /*
882 * Sort the function table, to enable binary sort.
883 */
884 sortFunctions();
885#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000886}
887
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000888#if defined(EXITFREE) || defined(PROTO)
889 void
890eval_clear()
891{
892 int i;
893 struct vimvar *p;
894
895 for (i = 0; i < VV_LEN; ++i)
896 {
897 p = &vimvars[i];
898 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000899 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000900 vim_free(p->vv_str);
901 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000902 }
903 else if (p->vv_di.di_tv.v_type == VAR_LIST)
904 {
905 list_unref(p->vv_list);
906 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000908 }
909 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000910 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911 hash_clear(&compat_hashtab);
912
Bram Moolenaard9fba312005-06-26 22:34:35 +0000913 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000914
915 /* global variables */
916 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000917
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000918 /* autoloaded script names */
919 ga_clear_strings(&ga_loaded);
920
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200921 /* script-local variables */
922 for (i = 1; i <= ga_scripts.ga_len; ++i)
923 {
924 vars_clear(&SCRIPT_VARS(i));
925 vim_free(SCRIPT_SV(i));
926 }
927 ga_clear(&ga_scripts);
928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000929 /* unreferenced lists and dicts */
930 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000931
932 /* functions */
933 free_all_functions();
934 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000935}
936#endif
937
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * Return the name of the executed function.
940 */
941 char_u *
942func_name(cookie)
943 void *cookie;
944{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946}
947
948/*
949 * Return the address holding the next breakpoint line for a funccall cookie.
950 */
951 linenr_T *
952func_breakpoint(cookie)
953 void *cookie;
954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the debug tick for a funccall cookie.
960 */
961 int *
962func_dbg_tick(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the nesting level for a funccall cookie.
970 */
971 int
972func_level(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000979funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000981/* pointer to list of previously used funccal, still around because some
982 * item in it is still being used. */
983funccall_T *previous_funccal = NULL;
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
986 * Return TRUE when a function was ended by a ":return" command.
987 */
988 int
989current_func_returned()
990{
991 return current_funccal->returned;
992}
993
994
995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * Set an internal variable to a string value. Creates the variable if it does
997 * not already exist.
998 */
999 void
1000set_internal_string_var(name, value)
1001 char_u *name;
1002 char_u *value;
1003{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 val = vim_strsave(value);
1008 if (val != NULL)
1009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 }
1017}
1018
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001020static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static char_u *redir_endp = NULL;
1022static char_u *redir_varname = NULL;
1023
1024/*
1025 * Start recording command output to a variable
1026 * Returns OK if successfully completed the setup. FAIL otherwise.
1027 */
1028 int
1029var_redir_start(name, append)
1030 char_u *name;
1031 int append; /* append to an existing variable */
1032{
1033 int save_emsg;
1034 int err;
1035 typval_T tv;
1036
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001037 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001038 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 {
1040 EMSG(_(e_invarg));
1041 return FAIL;
1042 }
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 redir_varname = vim_strsave(name);
1046 if (redir_varname == NULL)
1047 return FAIL;
1048
1049 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1050 if (redir_lval == NULL)
1051 {
1052 var_redir_stop();
1053 return FAIL;
1054 }
1055
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001056 /* The output is stored in growarray "redir_ga" until redirection ends. */
1057 ga_init2(&redir_ga, (int)sizeof(char), 500);
1058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001060 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1061 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1063 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001064 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 if (redir_endp != NULL && *redir_endp != NUL)
1066 /* Trailing characters are present after the variable name */
1067 EMSG(_(e_trailing));
1068 else
1069 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001070 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 var_redir_stop();
1072 return FAIL;
1073 }
1074
1075 /* check if we can write to the variable: set it to or append an empty
1076 * string */
1077 save_emsg = did_emsg;
1078 did_emsg = FALSE;
1079 tv.v_type = VAR_STRING;
1080 tv.vval.v_string = (char_u *)"";
1081 if (append)
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1083 else
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001085 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001087 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (err)
1089 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001090 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 var_redir_stop();
1092 return FAIL;
1093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 * Append "value[value_len]" to the variable set by var_redir_start().
1100 * The actual appending is postponed until redirection ends, because the value
1101 * appended may in fact be the string we write to, changing it may cause freed
1102 * memory to be used:
1103 * :redir => foo
1104 * :let foo
1105 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 */
1107 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
1114 if (redir_lval == NULL)
1115 return;
1116
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 {
1124 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 }
1127 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129}
1130
1131/*
1132 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 */
1135 void
1136var_redir_stop()
1137{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138 typval_T tv;
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_lval != NULL)
1141 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* If there was no error: assign the text to the variable. */
1143 if (redir_endp != NULL)
1144 {
1145 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1146 tv.v_type = VAR_STRING;
1147 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001148 /* Call get_lval() again, if it's inside a Dict or List it may
1149 * have changed. */
1150 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1151 FALSE, FALSE, FALSE, FNE_CHECK_START);
1152 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1153 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1154 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* free the collected output */
1158 vim_free(redir_ga.ga_data);
1159 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 vim_free(redir_lval);
1162 redir_lval = NULL;
1163 }
1164 vim_free(redir_varname);
1165 redir_varname = NULL;
1166}
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168# if defined(FEAT_MBYTE) || defined(PROTO)
1169 int
1170eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1171 char_u *enc_from;
1172 char_u *enc_to;
1173 char_u *fname_from;
1174 char_u *fname_to;
1175{
1176 int err = FALSE;
1177
1178 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1179 set_vim_var_string(VV_CC_TO, enc_to, -1);
1180 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1181 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1182 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1183 err = TRUE;
1184 set_vim_var_string(VV_CC_FROM, NULL, -1);
1185 set_vim_var_string(VV_CC_TO, NULL, -1);
1186 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1187 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1188
1189 if (err)
1190 return FAIL;
1191 return OK;
1192}
1193# endif
1194
1195# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1196 int
1197eval_printexpr(fname, args)
1198 char_u *fname;
1199 char_u *args;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_FNAME_IN, fname, -1);
1204 set_vim_var_string(VV_CMDARG, args, -1);
1205 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1206 err = TRUE;
1207 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1208 set_vim_var_string(VV_CMDARG, NULL, -1);
1209
1210 if (err)
1211 {
1212 mch_remove(fname);
1213 return FAIL;
1214 }
1215 return OK;
1216}
1217# endif
1218
1219# if defined(FEAT_DIFF) || defined(PROTO)
1220 void
1221eval_diff(origfile, newfile, outfile)
1222 char_u *origfile;
1223 char_u *newfile;
1224 char_u *outfile;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1229 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1230 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1231 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1234 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1235}
1236
1237 void
1238eval_patch(origfile, difffile, outfile)
1239 char_u *origfile;
1240 char_u *difffile;
1241 char_u *outfile;
1242{
1243 int err;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253# endif
1254
1255/*
1256 * Top level evaluation function, returning a boolean.
1257 * Sets "error" to TRUE if there was an error.
1258 * Return TRUE or FALSE.
1259 */
1260 int
1261eval_to_bool(arg, error, nextcmd, skip)
1262 char_u *arg;
1263 int *error;
1264 char_u **nextcmd;
1265 int skip; /* only parse, don't execute */
1266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 int retval = FALSE;
1269
1270 if (skip)
1271 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
1275 {
1276 *error = FALSE;
1277 if (!skip)
1278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001279 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 }
1283 if (skip)
1284 --emsg_skip;
1285
1286 return retval;
1287}
1288
1289/*
1290 * Top level evaluation function, returning a string. If "skip" is TRUE,
1291 * only parsing to "nextcmd" is done, without reporting errors. Return
1292 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1293 */
1294 char_u *
1295eval_to_string_skip(arg, nextcmd, skip)
1296 char_u *arg;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 char_u *retval;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 retval = NULL;
1307 else
1308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 retval = vim_strsave(get_tv_string(&tv));
1310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 if (skip)
1313 --emsg_skip;
1314
1315 return retval;
1316}
1317
1318/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319 * Skip over an expression at "*pp".
1320 * Return FAIL for an error, OK otherwise.
1321 */
1322 int
1323skip_expr(pp)
1324 char_u **pp;
1325{
Bram Moolenaar33570922005-01-25 22:26:29 +00001326 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327
1328 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001330}
1331
1332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 * When "convert" is TRUE convert a List into a sequence of lines and convert
1335 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 * Return pointer to allocated memory, or NULL for failure.
1337 */
1338 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *arg;
1341 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 retval = NULL;
1353 else
1354 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 {
1357 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 if (tv.vval.v_list != NULL)
1359 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001360 ga_append(&ga, NUL);
1361 retval = (char_u *)ga.ga_data;
1362 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001363#ifdef FEAT_FLOAT
1364 else if (convert && tv.v_type == VAR_FLOAT)
1365 {
1366 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1367 retval = vim_strsave(numbuf);
1368 }
1369#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001370 else
1371 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001372 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 }
1374
1375 return retval;
1376}
1377
1378/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001379 * Call eval_to_string() without using current local variables and using
1380 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 */
1382 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 char_u *arg;
1385 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387{
1388 char_u *retval;
1389 void *save_funccalp;
1390
1391 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001392 if (use_sandbox)
1393 ++sandbox;
1394 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001395 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 if (use_sandbox)
1397 --sandbox;
1398 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 restore_funccal(save_funccalp);
1400 return retval;
1401}
1402
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403/*
1404 * Top level evaluation function, returning a number.
1405 * Evaluates "expr" silently.
1406 * Returns -1 for an error.
1407 */
1408 int
1409eval_to_number(expr)
1410 char_u *expr;
1411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001414 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
1416 ++emsg_off;
1417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001418 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 retval = -1;
1420 else
1421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001422 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001423 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 }
1425 --emsg_off;
1426
1427 return retval;
1428}
1429
Bram Moolenaara40058a2005-07-11 22:42:07 +00001430/*
1431 * Prepare v: variable "idx" to be used.
1432 * Save the current typeval in "save_tv".
1433 * When not used yet add the variable to the v: hashtable.
1434 */
1435 static void
1436prepare_vimvar(idx, save_tv)
1437 int idx;
1438 typval_T *save_tv;
1439{
1440 *save_tv = vimvars[idx].vv_tv;
1441 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1442 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1443}
1444
1445/*
1446 * Restore v: variable "idx" to typeval "save_tv".
1447 * When no longer defined, remove the variable from the v: hashtable.
1448 */
1449 static void
1450restore_vimvar(idx, save_tv)
1451 int idx;
1452 typval_T *save_tv;
1453{
1454 hashitem_T *hi;
1455
Bram Moolenaara40058a2005-07-11 22:42:07 +00001456 vimvars[idx].vv_tv = *save_tv;
1457 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1458 {
1459 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1460 if (HASHITEM_EMPTY(hi))
1461 EMSG2(_(e_intern2), "restore_vimvar()");
1462 else
1463 hash_remove(&vimvarht, hi);
1464 }
1465}
1466
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001467#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001468/*
1469 * Evaluate an expression to a list with suggestions.
1470 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001471 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001472 */
1473 list_T *
1474eval_spell_expr(badword, expr)
1475 char_u *badword;
1476 char_u *expr;
1477{
1478 typval_T save_val;
1479 typval_T rettv;
1480 list_T *list = NULL;
1481 char_u *p = skipwhite(expr);
1482
1483 /* Set "v:val" to the bad word. */
1484 prepare_vimvar(VV_VAL, &save_val);
1485 vimvars[VV_VAL].vv_type = VAR_STRING;
1486 vimvars[VV_VAL].vv_str = badword;
1487 if (p_verbose == 0)
1488 ++emsg_off;
1489
1490 if (eval1(&p, &rettv, TRUE) == OK)
1491 {
1492 if (rettv.v_type != VAR_LIST)
1493 clear_tv(&rettv);
1494 else
1495 list = rettv.vval.v_list;
1496 }
1497
1498 if (p_verbose == 0)
1499 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001500 restore_vimvar(VV_VAL, &save_val);
1501
1502 return list;
1503}
1504
1505/*
1506 * "list" is supposed to contain two items: a word and a number. Return the
1507 * word in "pp" and the number as the return value.
1508 * Return -1 if anything isn't right.
1509 * Used to get the good word and score from the eval_spell_expr() result.
1510 */
1511 int
1512get_spellword(list, pp)
1513 list_T *list;
1514 char_u **pp;
1515{
1516 listitem_T *li;
1517
1518 li = list->lv_first;
1519 if (li == NULL)
1520 return -1;
1521 *pp = get_tv_string(&li->li_tv);
1522
1523 li = li->li_next;
1524 if (li == NULL)
1525 return -1;
1526 return get_tv_number(&li->li_tv);
1527}
1528#endif
1529
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001530/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001531 * Top level evaluation function.
1532 * Returns an allocated typval_T with the result.
1533 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534 */
1535 typval_T *
1536eval_expr(arg, nextcmd)
1537 char_u *arg;
1538 char_u **nextcmd;
1539{
1540 typval_T *tv;
1541
1542 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001543 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001544 {
1545 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001546 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001547 }
1548
1549 return tv;
1550}
1551
1552
Bram Moolenaar4f688582007-07-24 12:34:30 +00001553#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1554 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 * Uses argv[argc] for the function arguments. Only Number and String
1558 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 static int
1562call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 char_u *func;
1564 int argc;
1565 char_u **argv;
1566 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 long n;
1571 int len;
1572 int i;
1573 int doesrange;
1574 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 for (i = 0; i < argc; i++)
1582 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 /* Pass a NULL or empty argument as an empty string */
1584 if (argv[i] == NULL || *argv[i] == NUL)
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 continue;
1589 }
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* Recognize a number argument, the others must be strings. */
1592 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1593 if (len != 0 && len == (int)STRLEN(argv[i]))
1594 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001595 argvars[i].v_type = VAR_NUMBER;
1596 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 }
1598 else
1599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001600 argvars[i].v_type = VAR_STRING;
1601 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 }
1603 }
1604
1605 if (safe)
1606 {
1607 save_funccalp = save_funccal();
1608 ++sandbox;
1609 }
1610
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001611 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1612 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 if (safe)
1616 {
1617 --sandbox;
1618 restore_funccal(save_funccalp);
1619 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 vim_free(argvars);
1621
1622 if (ret == FAIL)
1623 clear_tv(rettv);
1624
1625 return ret;
1626}
1627
Bram Moolenaar4f688582007-07-24 12:34:30 +00001628# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001629/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001630 * Call vimL function "func" and return the result as a string.
1631 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001632 * Uses argv[argc] for the function arguments.
1633 */
1634 void *
1635call_func_retstr(func, argc, argv, safe)
1636 char_u *func;
1637 int argc;
1638 char_u **argv;
1639 int safe; /* use the sandbox */
1640{
1641 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001642 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643
1644 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1645 return NULL;
1646
1647 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 return retval;
1650}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001651# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652
Bram Moolenaar4f688582007-07-24 12:34:30 +00001653# if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001655 * Call vimL function "func" and return the result as a number.
1656 * Returns -1 when calling the function fails.
1657 * Uses argv[argc] for the function arguments.
1658 */
1659 long
1660call_func_retnr(func, argc, argv, safe)
1661 char_u *func;
1662 int argc;
1663 char_u **argv;
1664 int safe; /* use the sandbox */
1665{
1666 typval_T rettv;
1667 long retval;
1668
1669 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1670 return -1;
1671
1672 retval = get_tv_number_chk(&rettv, NULL);
1673 clear_tv(&rettv);
1674 return retval;
1675}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001676# endif
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001677
1678/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001679 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001681 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 */
1683 void *
1684call_func_retlist(func, argc, argv, safe)
1685 char_u *func;
1686 int argc;
1687 char_u **argv;
1688 int safe; /* use the sandbox */
1689{
1690 typval_T rettv;
1691
1692 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1693 return NULL;
1694
1695 if (rettv.v_type != VAR_LIST)
1696 {
1697 clear_tv(&rettv);
1698 return NULL;
1699 }
1700
1701 return rettv.vval.v_list;
1702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
1704
Bram Moolenaar4f688582007-07-24 12:34:30 +00001705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706/*
1707 * Save the current function call pointer, and set it to NULL.
1708 * Used when executing autocommands and for ":source".
1709 */
1710 void *
1711save_funccal()
1712{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001713 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 current_funccal = NULL;
1716 return (void *)fc;
1717}
1718
1719 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720restore_funccal(vfc)
1721 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001723 funccall_T *fc = (funccall_T *)vfc;
1724
1725 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726}
1727
Bram Moolenaar05159a02005-02-26 23:04:13 +00001728#if defined(FEAT_PROFILE) || defined(PROTO)
1729/*
1730 * Prepare profiling for entering a child or something else that is not
1731 * counted for the script/function itself.
1732 * Should always be called in pair with prof_child_exit().
1733 */
1734 void
1735prof_child_enter(tm)
1736 proftime_T *tm; /* place to store waittime */
1737{
1738 funccall_T *fc = current_funccal;
1739
1740 if (fc != NULL && fc->func->uf_profiling)
1741 profile_start(&fc->prof_child);
1742 script_prof_save(tm);
1743}
1744
1745/*
1746 * Take care of time spent in a child.
1747 * Should always be called after prof_child_enter().
1748 */
1749 void
1750prof_child_exit(tm)
1751 proftime_T *tm; /* where waittime was stored */
1752{
1753 funccall_T *fc = current_funccal;
1754
1755 if (fc != NULL && fc->func->uf_profiling)
1756 {
1757 profile_end(&fc->prof_child);
1758 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1759 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1760 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1761 }
1762 script_prof_restore(tm);
1763}
1764#endif
1765
1766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767#ifdef FEAT_FOLDING
1768/*
1769 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1770 * it in "*cp". Doesn't give error messages.
1771 */
1772 int
1773eval_foldexpr(arg, cp)
1774 char_u *arg;
1775 int *cp;
1776{
Bram Moolenaar33570922005-01-25 22:26:29 +00001777 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 int retval;
1779 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001780 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1781 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001784 if (use_sandbox)
1785 ++sandbox;
1786 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001788 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 retval = 0;
1790 else
1791 {
1792 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001793 if (tv.v_type == VAR_NUMBER)
1794 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001795 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 retval = 0;
1797 else
1798 {
1799 /* If the result is a string, check if there is a non-digit before
1800 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001801 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (!VIM_ISDIGIT(*s) && *s != '-')
1803 *cp = *s++;
1804 retval = atol((char *)s);
1805 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001806 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 }
1808 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001809 if (use_sandbox)
1810 --sandbox;
1811 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
1813 return retval;
1814}
1815#endif
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 * ":let" list all variable values
1819 * ":let var1 var2" list variable values
1820 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001821 * ":let var += expr" assignment command.
1822 * ":let var -= expr" assignment command.
1823 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 */
1826 void
1827ex_let(eap)
1828 exarg_T *eap;
1829{
1830 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001832 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001834 int var_count = 0;
1835 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001838 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839
Bram Moolenaardb552d602006-03-23 22:59:57 +00001840 argend = skip_var_list(arg, &var_count, &semicolon);
1841 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001842 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001843 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1844 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001845 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001848 /*
1849 * ":let" without "=": list variables
1850 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 if (*arg == '[')
1852 EMSG(_(e_invarg));
1853 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001855 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001859 list_glob_vars(&first);
1860 list_buf_vars(&first);
1861 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001862#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001863 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001864#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_script_vars(&first);
1866 list_func_vars(&first);
1867 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 eap->nextcmd = check_nextcmd(arg);
1870 }
1871 else
1872 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001873 op[0] = '=';
1874 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001875 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001876 {
1877 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1878 op[0] = expr[-1]; /* +=, -= or .= */
1879 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001880 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (eap->skip)
1883 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001884 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (eap->skip)
1886 {
1887 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 --emsg_skip;
1890 }
1891 else if (i != FAIL)
1892 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 }
1897 }
1898}
1899
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900/*
1901 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1902 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 * When "nextchars" is not NULL it points to a string with characters that
1904 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1905 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 * Returns OK or FAIL;
1907 */
1908 static int
1909ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1910 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 int copy; /* copy values from "tv", don't move */
1913 int semicolon; /* from skip_var_list() */
1914 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001915 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916{
1917 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 listitem_T *item;
1921 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922
1923 if (*arg != '[')
1924 {
1925 /*
1926 * ":let var = expr" or ":for var in list"
1927 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001928 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 return FAIL;
1930 return OK;
1931 }
1932
1933 /*
1934 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1935 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001936 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001937 {
1938 EMSG(_(e_listreq));
1939 return FAIL;
1940 }
1941
1942 i = list_len(l);
1943 if (semicolon == 0 && var_count < i)
1944 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001945 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 return FAIL;
1947 }
1948 if (var_count - semicolon > i)
1949 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001950 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 return FAIL;
1952 }
1953
1954 item = l->lv_first;
1955 while (*arg != ']')
1956 {
1957 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 item = item->li_next;
1960 if (arg == NULL)
1961 return FAIL;
1962
1963 arg = skipwhite(arg);
1964 if (*arg == ';')
1965 {
1966 /* Put the rest of the list (may be empty) in the var after ';'.
1967 * Create a new list for this. */
1968 l = list_alloc();
1969 if (l == NULL)
1970 return FAIL;
1971 while (item != NULL)
1972 {
1973 list_append_tv(l, &item->li_tv);
1974 item = item->li_next;
1975 }
1976
1977 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001978 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 ltv.vval.v_list = l;
1980 l->lv_refcount = 1;
1981
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001982 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1983 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001984 clear_tv(&ltv);
1985 if (arg == NULL)
1986 return FAIL;
1987 break;
1988 }
1989 else if (*arg != ',' && *arg != ']')
1990 {
1991 EMSG2(_(e_intern2), "ex_let_vars()");
1992 return FAIL;
1993 }
1994 }
1995
1996 return OK;
1997}
1998
1999/*
2000 * Skip over assignable variable "var" or list of variables "[var, var]".
2001 * Used for ":let varvar = expr" and ":for varvar in expr".
2002 * For "[var, var]" increment "*var_count" for each variable.
2003 * for "[var, var; var]" set "semicolon".
2004 * Return NULL for an error.
2005 */
2006 static char_u *
2007skip_var_list(arg, var_count, semicolon)
2008 char_u *arg;
2009 int *var_count;
2010 int *semicolon;
2011{
2012 char_u *p, *s;
2013
2014 if (*arg == '[')
2015 {
2016 /* "[var, var]": find the matching ']'. */
2017 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002018 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 {
2020 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2021 s = skip_var_one(p);
2022 if (s == p)
2023 {
2024 EMSG2(_(e_invarg2), p);
2025 return NULL;
2026 }
2027 ++*var_count;
2028
2029 p = skipwhite(s);
2030 if (*p == ']')
2031 break;
2032 else if (*p == ';')
2033 {
2034 if (*semicolon == 1)
2035 {
2036 EMSG(_("Double ; in list of variables"));
2037 return NULL;
2038 }
2039 *semicolon = 1;
2040 }
2041 else if (*p != ',')
2042 {
2043 EMSG2(_(e_invarg2), p);
2044 return NULL;
2045 }
2046 }
2047 return p + 1;
2048 }
2049 else
2050 return skip_var_one(arg);
2051}
2052
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002053/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002054 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002055 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002056 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 static char_u *
2058skip_var_one(arg)
2059 char_u *arg;
2060{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 if (*arg == '@' && arg[1] != NUL)
2062 return arg + 2;
2063 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2064 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002065}
2066
Bram Moolenaara7043832005-01-21 11:56:39 +00002067/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002068 * List variables for hashtab "ht" with prefix "prefix".
2069 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002072list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002073 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002074 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002076 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002077{
Bram Moolenaar33570922005-01-25 22:26:29 +00002078 hashitem_T *hi;
2079 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 int todo;
2081
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002082 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2084 {
2085 if (!HASHITEM_EMPTY(hi))
2086 {
2087 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002088 di = HI2DI(hi);
2089 if (empty || di->di_tv.v_type != VAR_STRING
2090 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002091 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 }
2093 }
2094}
2095
2096/*
2097 * List global variables.
2098 */
2099 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100list_glob_vars(first)
2101 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002102{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002103 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002104}
2105
2106/*
2107 * List buffer variables.
2108 */
2109 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110list_buf_vars(first)
2111 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002112{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002113 char_u numbuf[NUMBUFLEN];
2114
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002115 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:",
2116 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002117
2118 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2120 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002121}
2122
2123/*
2124 * List window variables.
2125 */
2126 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002127list_win_vars(first)
2128 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002129{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002130 list_hashtable_vars(&curwin->w_vars.dv_hashtab,
2131 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002132}
2133
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002134#ifdef FEAT_WINDOWS
2135/*
2136 * List tab page variables.
2137 */
2138 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002139list_tab_vars(first)
2140 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002142 list_hashtable_vars(&curtab->tp_vars.dv_hashtab,
2143 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002144}
2145#endif
2146
Bram Moolenaara7043832005-01-21 11:56:39 +00002147/*
2148 * List Vim variables.
2149 */
2150 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002151list_vim_vars(first)
2152 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002153{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002154 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155}
2156
2157/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002158 * List script-local variables, if there is a script.
2159 */
2160 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161list_script_vars(first)
2162 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002163{
2164 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2166 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167}
2168
2169/*
2170 * List function variables, if there is a function.
2171 */
2172 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002173list_func_vars(first)
2174 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002175{
2176 if (current_funccal != NULL)
2177 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002178 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002179}
2180
2181/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 * List variables in "arg".
2183 */
2184 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002186 exarg_T *eap;
2187 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189{
2190 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002191 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002193 char_u *name_start;
2194 char_u *arg_subsc;
2195 char_u *tofree;
2196 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002197
2198 while (!ends_excmd(*arg) && !got_int)
2199 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002202 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2204 {
2205 emsg_severe = TRUE;
2206 EMSG(_(e_trailing));
2207 break;
2208 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 /* get_name_len() takes care of expanding curly braces */
2213 name_start = name = arg;
2214 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2215 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 /* This is mainly to keep test 49 working: when expanding
2218 * curly braces fails overrule the exception error message. */
2219 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 emsg_severe = TRUE;
2222 EMSG2(_(e_invarg2), arg);
2223 break;
2224 }
2225 error = TRUE;
2226 }
2227 else
2228 {
2229 if (tofree != NULL)
2230 name = tofree;
2231 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 else
2234 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002235 /* handle d.key, l[idx], f(expr) */
2236 arg_subsc = arg;
2237 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002242 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002243 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 case 'g': list_glob_vars(first); break;
2246 case 'b': list_buf_vars(first); break;
2247 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002248#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002249 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002250#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'v': list_vim_vars(first); break;
2252 case 's': list_script_vars(first); break;
2253 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 default:
2255 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002256 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002257 }
2258 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 {
2260 char_u numbuf[NUMBUFLEN];
2261 char_u *tf;
2262 int c;
2263 char_u *s;
2264
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002265 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 c = *arg;
2267 *arg = NUL;
2268 list_one_var_a((char_u *)"",
2269 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002270 tv.v_type,
2271 s == NULL ? (char_u *)"" : s,
2272 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 *arg = c;
2274 vim_free(tf);
2275 }
2276 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 }
2279 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280
2281 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283
2284 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286
2287 return arg;
2288}
2289
2290/*
2291 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2292 * Returns a pointer to the char just after the var name.
2293 * Returns NULL if there is an error.
2294 */
2295 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002296ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002298 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002299 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002300 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302{
2303 int c1;
2304 char_u *name;
2305 char_u *p;
2306 char_u *arg_end = NULL;
2307 int len;
2308 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002309 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002310
2311 /*
2312 * ":let $VAR = expr": Set environment variable.
2313 */
2314 if (*arg == '$')
2315 {
2316 /* Find the end of the name. */
2317 ++arg;
2318 name = arg;
2319 len = get_env_len(&arg);
2320 if (len == 0)
2321 EMSG2(_(e_invarg2), name - 1);
2322 else
2323 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002324 if (op != NULL && (*op == '+' || *op == '-'))
2325 EMSG2(_(e_letwrong), op);
2326 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002327 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002328 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002329 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 {
2331 c1 = name[len];
2332 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 p = get_tv_string_chk(tv);
2334 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 {
2336 int mustfree = FALSE;
2337 char_u *s = vim_getenv(name, &mustfree);
2338
2339 if (s != NULL)
2340 {
2341 p = tofree = concat_str(s, p);
2342 if (mustfree)
2343 vim_free(s);
2344 }
2345 }
2346 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002347 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002349 if (STRICMP(name, "HOME") == 0)
2350 init_homedir();
2351 else if (didset_vim && STRICMP(name, "VIM") == 0)
2352 didset_vim = FALSE;
2353 else if (didset_vimruntime
2354 && STRICMP(name, "VIMRUNTIME") == 0)
2355 didset_vimruntime = FALSE;
2356 arg_end = arg;
2357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
2361 }
2362 }
2363
2364 /*
2365 * ":let &option = expr": Set option value.
2366 * ":let &l:option = expr": Set local option value.
2367 * ":let &g:option = expr": Set global option value.
2368 */
2369 else if (*arg == '&')
2370 {
2371 /* Find the end of the name. */
2372 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 if (p == NULL || (endchars != NULL
2374 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 EMSG(_(e_letunexp));
2376 else
2377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 long n;
2379 int opt_type;
2380 long numval;
2381 char_u *stringval = NULL;
2382 char_u *s;
2383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 c1 = *p;
2385 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386
2387 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 s = get_tv_string_chk(tv); /* != NULL if number or string */
2389 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 {
2391 opt_type = get_option_value(arg, &numval,
2392 &stringval, opt_flags);
2393 if ((opt_type == 1 && *op == '.')
2394 || (opt_type == 0 && *op != '.'))
2395 EMSG2(_(e_letwrong), op);
2396 else
2397 {
2398 if (opt_type == 1) /* number */
2399 {
2400 if (*op == '+')
2401 n = numval + n;
2402 else
2403 n = numval - n;
2404 }
2405 else if (opt_type == 0 && stringval != NULL) /* string */
2406 {
2407 s = concat_str(stringval, s);
2408 vim_free(stringval);
2409 stringval = s;
2410 }
2411 }
2412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 if (s != NULL)
2414 {
2415 set_option_value(arg, n, s, opt_flags);
2416 arg_end = p;
2417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 }
2421 }
2422
2423 /*
2424 * ":let @r = expr": Set register contents.
2425 */
2426 else if (*arg == '@')
2427 {
2428 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 if (op != NULL && (*op == '+' || *op == '-'))
2430 EMSG2(_(e_letwrong), op);
2431 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 EMSG(_(e_letunexp));
2434 else
2435 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002436 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 char_u *s;
2438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 p = get_tv_string_chk(tv);
2440 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002442 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 if (s != NULL)
2444 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 vim_free(s);
2447 }
2448 }
2449 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 arg_end = arg + 1;
2453 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 }
2456 }
2457
2458 /*
2459 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002462 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002464 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002466 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 arg_end = p;
2475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 }
2479
2480 else
2481 EMSG2(_(e_invarg2), arg);
2482
2483 return arg_end;
2484}
2485
2486/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2488 */
2489 static int
2490check_changedtick(arg)
2491 char_u *arg;
2492{
2493 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2494 {
2495 EMSG2(_(e_readonlyvar), arg);
2496 return TRUE;
2497 }
2498 return FALSE;
2499}
2500
2501/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * Get an lval: variable, Dict item or List item that can be assigned a value
2503 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2504 * "name.key", "name.key[expr]" etc.
2505 * Indexing only works if "name" is an existing List or Dictionary.
2506 * "name" points to the start of the name.
2507 * If "rettv" is not NULL it points to the value to be assigned.
2508 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2509 * wrong; must end in space or cmd separator.
2510 *
2511 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002512 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
2515 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 typval_T *rettv;
2519 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 int unlet;
2521 int skip;
2522 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 char_u *expr_start, *expr_end;
2527 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 dictitem_T *v;
2529 typval_T var1;
2530 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539
2540 if (skip)
2541 {
2542 /* When skipping just find the end of the name. */
2543 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002544 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 }
2546
2547 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002548 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (expr_start != NULL)
2550 {
2551 /* Don't expand the name when we already know there is an error. */
2552 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2553 && *p != '[' && *p != '.')
2554 {
2555 EMSG(_(e_trailing));
2556 return NULL;
2557 }
2558
2559 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2560 if (lp->ll_exp_name == NULL)
2561 {
2562 /* Report an invalid expression in braces, unless the
2563 * expression evaluation has been cancelled due to an
2564 * aborting error, an interrupt, or an exception. */
2565 if (!aborting() && !quiet)
2566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002567 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 EMSG2(_(e_invarg2), name);
2569 return NULL;
2570 }
2571 }
2572 lp->ll_name = lp->ll_exp_name;
2573 }
2574 else
2575 lp->ll_name = name;
2576
2577 /* Without [idx] or .key we are done. */
2578 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2579 return p;
2580
2581 cc = *p;
2582 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002583 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (v == NULL && !quiet)
2585 EMSG2(_(e_undefvar), lp->ll_name);
2586 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 if (v == NULL)
2588 return NULL;
2589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /*
2591 * Loop until no more [idx] or .key is following.
2592 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2597 && !(lp->ll_tv->v_type == VAR_DICT
2598 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!quiet)
2601 EMSG(_("E689: Can only index a List or Dictionary"));
2602 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E708: [:] must come last"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 len = -1;
2612 if (*p == '.')
2613 {
2614 key = p + 1;
2615 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2616 ;
2617 if (len == 0)
2618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_(e_emptykey));
2621 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623 p = key + len;
2624 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 else
2626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (*p == ':')
2630 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 empty1 = FALSE;
2634 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 if (get_tv_string_chk(&var1) == NULL)
2637 {
2638 /* not a number or string */
2639 clear_tv(&var1);
2640 return NULL;
2641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 }
2643
2644 /* Optionally get the second index [ :expr]. */
2645 if (*p == ':')
2646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 if (!empty1)
2652 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (rettv != NULL && (rettv->v_type != VAR_LIST
2656 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 if (!empty1)
2661 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 }
2664 p = skipwhite(p + 1);
2665 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 else
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (!empty1)
2673 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 if (get_tv_string_chk(&var2) == NULL)
2677 {
2678 /* not a number or string */
2679 if (!empty1)
2680 clear_tv(&var1);
2681 clear_tv(&var2);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700
2701 /* Skip to past ']'. */
2702 ++p;
2703 }
2704
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
2707 if (len == -1)
2708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (*key == NUL)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
2714 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 lp->ll_list = NULL;
2720 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002721 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002724 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (len == -1)
2730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (len == -1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 p = NULL;
2741 break;
2742 }
2743 if (len == -1)
2744 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747 else
2748 {
2749 /*
2750 * Get the number and item for the only or first index of the List.
2751 */
2752 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 else
2755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var1);
2758 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 lp->ll_list = lp->ll_tv->vval.v_list;
2761 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2762 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002764 if (lp->ll_n1 < 0)
2765 {
2766 lp->ll_n1 = 0;
2767 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2768 }
2769 }
2770 if (lp->ll_li == NULL)
2771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776
2777 /*
2778 * May need to find the item or absolute index for the second
2779 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 * When no index given: "lp->ll_empty2" is TRUE.
2781 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 }
2794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2796 if (lp->ll_n1 < 0)
2797 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2798 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002800 }
2801
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002804 }
2805
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return p;
2807}
2808
2809/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002810 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 */
2812 static void
2813clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002814 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815{
2816 vim_free(lp->ll_exp_name);
2817 vim_free(lp->ll_newkey);
2818}
2819
2820/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002821 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 */
2825 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002826set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002831 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832{
2833 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002834 listitem_T *ri;
2835 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836
2837 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002838 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002840 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 cc = *endp;
2842 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 if (op != NULL && *op != '=')
2844 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002847 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002848 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002849 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002850 {
2851 if (tv_op(&tv, rettv, op) == OK)
2852 set_var(lp->ll_name, &tv, FALSE);
2853 clear_tv(&tv);
2854 }
2855 }
2856 else
2857 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002859 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002861 else if (tv_check_lock(lp->ll_newkey == NULL
2862 ? lp->ll_tv->v_lock
2863 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2864 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 else if (lp->ll_range)
2866 {
2867 /*
2868 * Assign the List values to the list items.
2869 */
2870 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002871 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002872 if (op != NULL && *op != '=')
2873 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2874 else
2875 {
2876 clear_tv(&lp->ll_li->li_tv);
2877 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2878 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 ri = ri->li_next;
2880 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2881 break;
2882 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002885 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002886 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 ri = NULL;
2888 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002889 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002890 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_li = lp->ll_li->li_next;
2892 ++lp->ll_n1;
2893 }
2894 if (ri != NULL)
2895 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 else if (lp->ll_empty2
2897 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 : lp->ll_n1 != lp->ll_n2)
2899 EMSG(_("E711: List value has not enough items"));
2900 }
2901 else
2902 {
2903 /*
2904 * Assign to a List or Dictionary item.
2905 */
2906 if (lp->ll_newkey != NULL)
2907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 if (op != NULL && *op != '=')
2909 {
2910 EMSG2(_(e_letwrong), op);
2911 return;
2912 }
2913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002915 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (di == NULL)
2917 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002918 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2919 {
2920 vim_free(di);
2921 return;
2922 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 else if (op != NULL && *op != '=')
2926 {
2927 tv_op(lp->ll_tv, rettv, op);
2928 return;
2929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 /*
2934 * Assign the value to the variable or list item.
2935 */
2936 if (copy)
2937 copy_tv(rettv, lp->ll_tv);
2938 else
2939 {
2940 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002941 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 }
2944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945}
2946
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002947/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2949 * Returns OK or FAIL.
2950 */
2951 static int
2952tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 typval_T *tv1;
2954 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 char_u *op;
2956{
2957 long n;
2958 char_u numbuf[NUMBUFLEN];
2959 char_u *s;
2960
2961 /* Can't do anything with a Funcref or a Dict on the right. */
2962 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2963 {
2964 switch (tv1->v_type)
2965 {
2966 case VAR_DICT:
2967 case VAR_FUNC:
2968 break;
2969
2970 case VAR_LIST:
2971 if (*op != '+' || tv2->v_type != VAR_LIST)
2972 break;
2973 /* List += List */
2974 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2975 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2976 return OK;
2977
2978 case VAR_NUMBER:
2979 case VAR_STRING:
2980 if (tv2->v_type == VAR_LIST)
2981 break;
2982 if (*op == '+' || *op == '-')
2983 {
2984 /* nr += nr or nr -= nr*/
2985 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986#ifdef FEAT_FLOAT
2987 if (tv2->v_type == VAR_FLOAT)
2988 {
2989 float_T f = n;
2990
2991 if (*op == '+')
2992 f += tv2->vval.v_float;
2993 else
2994 f -= tv2->vval.v_float;
2995 clear_tv(tv1);
2996 tv1->v_type = VAR_FLOAT;
2997 tv1->vval.v_float = f;
2998 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000#endif
3001 {
3002 if (*op == '+')
3003 n += get_tv_number(tv2);
3004 else
3005 n -= get_tv_number(tv2);
3006 clear_tv(tv1);
3007 tv1->v_type = VAR_NUMBER;
3008 tv1->vval.v_number = n;
3009 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003010 }
3011 else
3012 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003013 if (tv2->v_type == VAR_FLOAT)
3014 break;
3015
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 /* str .= str */
3017 s = get_tv_string(tv1);
3018 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3019 clear_tv(tv1);
3020 tv1->v_type = VAR_STRING;
3021 tv1->vval.v_string = s;
3022 }
3023 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024
3025#ifdef FEAT_FLOAT
3026 case VAR_FLOAT:
3027 {
3028 float_T f;
3029
3030 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3031 && tv2->v_type != VAR_NUMBER
3032 && tv2->v_type != VAR_STRING))
3033 break;
3034 if (tv2->v_type == VAR_FLOAT)
3035 f = tv2->vval.v_float;
3036 else
3037 f = get_tv_number(tv2);
3038 if (*op == '+')
3039 tv1->vval.v_float += f;
3040 else
3041 tv1->vval.v_float -= f;
3042 }
3043 return OK;
3044#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003045 }
3046 }
3047
3048 EMSG2(_(e_letwrong), op);
3049 return FAIL;
3050}
3051
3052/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053 * Add a watcher to a list.
3054 */
3055 static void
3056list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 list_T *l;
3058 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059{
3060 lw->lw_next = l->lv_watch;
3061 l->lv_watch = lw;
3062}
3063
3064/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003065 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066 * No warning when it isn't found...
3067 */
3068 static void
3069list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 list_T *l;
3071 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003072{
Bram Moolenaar33570922005-01-25 22:26:29 +00003073 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003074
3075 lwp = &l->lv_watch;
3076 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3077 {
3078 if (lw == lwrem)
3079 {
3080 *lwp = lw->lw_next;
3081 break;
3082 }
3083 lwp = &lw->lw_next;
3084 }
3085}
3086
3087/*
3088 * Just before removing an item from a list: advance watchers to the next
3089 * item.
3090 */
3091 static void
3092list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 list_T *l;
3094 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003095{
Bram Moolenaar33570922005-01-25 22:26:29 +00003096 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097
3098 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3099 if (lw->lw_item == item)
3100 lw->lw_item = item->li_next;
3101}
3102
3103/*
3104 * Evaluate the expression used in a ":for var in expr" command.
3105 * "arg" points to "var".
3106 * Set "*errp" to TRUE for an error, FALSE otherwise;
3107 * Return a pointer that holds the info. Null when there is an error.
3108 */
3109 void *
3110eval_for_line(arg, errp, nextcmdp, skip)
3111 char_u *arg;
3112 int *errp;
3113 char_u **nextcmdp;
3114 int skip;
3115{
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 typval_T tv;
3119 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120
3121 *errp = TRUE; /* default: there is an error */
3122
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 if (fi == NULL)
3125 return NULL;
3126
3127 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3128 if (expr == NULL)
3129 return fi;
3130
3131 expr = skipwhite(expr);
3132 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3133 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003134 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135 return fi;
3136 }
3137
3138 if (skip)
3139 ++emsg_skip;
3140 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3141 {
3142 *errp = FALSE;
3143 if (!skip)
3144 {
3145 l = tv.vval.v_list;
3146 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003147 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003149 clear_tv(&tv);
3150 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 else
3152 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003153 /* No need to increment the refcount, it's already set for the
3154 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155 fi->fi_list = l;
3156 list_add_watch(l, &fi->fi_lw);
3157 fi->fi_lw.lw_item = l->lv_first;
3158 }
3159 }
3160 }
3161 if (skip)
3162 --emsg_skip;
3163
3164 return fi;
3165}
3166
3167/*
3168 * Use the first item in a ":for" list. Advance to the next.
3169 * Assign the values to the variable (list). "arg" points to the first one.
3170 * Return TRUE when a valid item was found, FALSE when at end of list or
3171 * something wrong.
3172 */
3173 int
3174next_for_item(fi_void, arg)
3175 void *fi_void;
3176 char_u *arg;
3177{
Bram Moolenaar33570922005-01-25 22:26:29 +00003178 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003179 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003180 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181
3182 item = fi->fi_lw.lw_item;
3183 if (item == NULL)
3184 result = FALSE;
3185 else
3186 {
3187 fi->fi_lw.lw_item = item->li_next;
3188 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3189 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3190 }
3191 return result;
3192}
3193
3194/*
3195 * Free the structure used to store info used by ":for".
3196 */
3197 void
3198free_for_info(fi_void)
3199 void *fi_void;
3200{
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003203 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003204 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003206 list_unref(fi->fi_list);
3207 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 vim_free(fi);
3209}
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3212
3213 void
3214set_context_for_expression(xp, arg, cmdidx)
3215 expand_T *xp;
3216 char_u *arg;
3217 cmdidx_T cmdidx;
3218{
3219 int got_eq = FALSE;
3220 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 if (cmdidx == CMD_let)
3224 {
3225 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003226 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 {
3228 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003229 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 {
3231 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 if (vim_iswhite(*p))
3234 break;
3235 }
3236 return;
3237 }
3238 }
3239 else
3240 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3241 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 while ((xp->xp_pattern = vim_strpbrk(arg,
3243 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3244 {
3245 c = *xp->xp_pattern;
3246 if (c == '&')
3247 {
3248 c = xp->xp_pattern[1];
3249 if (c == '&')
3250 {
3251 ++xp->xp_pattern;
3252 xp->xp_context = cmdidx != CMD_let || got_eq
3253 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3254 }
3255 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003258 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3259 xp->xp_pattern += 2;
3260
3261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 else if (c == '$')
3264 {
3265 /* environment variable */
3266 xp->xp_context = EXPAND_ENV_VARS;
3267 }
3268 else if (c == '=')
3269 {
3270 got_eq = TRUE;
3271 xp->xp_context = EXPAND_EXPRESSION;
3272 }
3273 else if (c == '<'
3274 && xp->xp_context == EXPAND_FUNCTIONS
3275 && vim_strchr(xp->xp_pattern, '(') == NULL)
3276 {
3277 /* Function name can start with "<SNR>" */
3278 break;
3279 }
3280 else if (cmdidx != CMD_let || got_eq)
3281 {
3282 if (c == '"') /* string */
3283 {
3284 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3285 if (c == '\\' && xp->xp_pattern[1] != NUL)
3286 ++xp->xp_pattern;
3287 xp->xp_context = EXPAND_NOTHING;
3288 }
3289 else if (c == '\'') /* literal string */
3290 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003291 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3293 /* skip */ ;
3294 xp->xp_context = EXPAND_NOTHING;
3295 }
3296 else if (c == '|')
3297 {
3298 if (xp->xp_pattern[1] == '|')
3299 {
3300 ++xp->xp_pattern;
3301 xp->xp_context = EXPAND_EXPRESSION;
3302 }
3303 else
3304 xp->xp_context = EXPAND_COMMANDS;
3305 }
3306 else
3307 xp->xp_context = EXPAND_EXPRESSION;
3308 }
3309 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310 /* Doesn't look like something valid, expand as an expression
3311 * anyway. */
3312 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 arg = xp->xp_pattern;
3314 if (*arg != NUL)
3315 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3316 /* skip */ ;
3317 }
3318 xp->xp_pattern = arg;
3319}
3320
3321#endif /* FEAT_CMDL_COMPL */
3322
3323/*
3324 * ":1,25call func(arg1, arg2)" function call.
3325 */
3326 void
3327ex_call(eap)
3328 exarg_T *eap;
3329{
3330 char_u *arg = eap->arg;
3331 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003333 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003335 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 linenr_T lnum;
3337 int doesrange;
3338 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003339 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003341 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003342 if (fudi.fd_newkey != NULL)
3343 {
3344 /* Still need to give an error message for missing key. */
3345 EMSG2(_(e_dictkey), fudi.fd_newkey);
3346 vim_free(fudi.fd_newkey);
3347 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003348 if (tofree == NULL)
3349 return;
3350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003351 /* Increase refcount on dictionary, it could get deleted when evaluating
3352 * the arguments. */
3353 if (fudi.fd_dict != NULL)
3354 ++fudi.fd_dict->dv_refcount;
3355
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003356 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003357 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003358 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
Bram Moolenaar532c7802005-01-27 14:44:31 +00003360 /* Skip white space to allow ":call func ()". Not good, but required for
3361 * backward compatibility. */
3362 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365 if (*startarg != '(')
3366 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003367 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 goto end;
3369 }
3370
3371 /*
3372 * When skipping, evaluate the function once, to find the end of the
3373 * arguments.
3374 * When the function takes a range, this is discovered after the first
3375 * call, and the loop is broken.
3376 */
3377 if (eap->skip)
3378 {
3379 ++emsg_skip;
3380 lnum = eap->line2; /* do it once, also with an invalid range */
3381 }
3382 else
3383 lnum = eap->line1;
3384 for ( ; lnum <= eap->line2; ++lnum)
3385 {
3386 if (!eap->skip && eap->addr_count > 0)
3387 {
3388 curwin->w_cursor.lnum = lnum;
3389 curwin->w_cursor.col = 0;
3390 }
3391 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003392 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003393 eap->line1, eap->line2, &doesrange,
3394 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 {
3396 failed = TRUE;
3397 break;
3398 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003399
3400 /* Handle a function returning a Funcref, Dictionary or List. */
3401 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3402 {
3403 failed = TRUE;
3404 break;
3405 }
3406
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (doesrange || eap->skip)
3409 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003412 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003414 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (aborting())
3416 break;
3417 }
3418 if (eap->skip)
3419 --emsg_skip;
3420
3421 if (!failed)
3422 {
3423 /* Check for trailing illegal characters and a following command. */
3424 if (!ends_excmd(*arg))
3425 {
3426 emsg_severe = TRUE;
3427 EMSG(_(e_trailing));
3428 }
3429 else
3430 eap->nextcmd = check_nextcmd(arg);
3431 }
3432
3433end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003434 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003435 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436}
3437
3438/*
3439 * ":unlet[!] var1 ... " command.
3440 */
3441 void
3442ex_unlet(eap)
3443 exarg_T *eap;
3444{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003445 ex_unletlock(eap, eap->arg, 0);
3446}
3447
3448/*
3449 * ":lockvar" and ":unlockvar" commands
3450 */
3451 void
3452ex_lockvar(eap)
3453 exarg_T *eap;
3454{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003456 int deep = 2;
3457
3458 if (eap->forceit)
3459 deep = -1;
3460 else if (vim_isdigit(*arg))
3461 {
3462 deep = getdigits(&arg);
3463 arg = skipwhite(arg);
3464 }
3465
3466 ex_unletlock(eap, arg, deep);
3467}
3468
3469/*
3470 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3471 */
3472 static void
3473ex_unletlock(eap, argstart, deep)
3474 exarg_T *eap;
3475 char_u *argstart;
3476 int deep;
3477{
3478 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003481 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483 do
3484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003485 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003486 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3487 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003488 if (lv.ll_name == NULL)
3489 error = TRUE; /* error but continue parsing */
3490 if (name_end == NULL || (!vim_iswhite(*name_end)
3491 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003493 if (name_end != NULL)
3494 {
3495 emsg_severe = TRUE;
3496 EMSG(_(e_trailing));
3497 }
3498 if (!(eap->skip || error))
3499 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 break;
3501 }
3502
3503 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003504 {
3505 if (eap->cmdidx == CMD_unlet)
3506 {
3507 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3508 error = TRUE;
3509 }
3510 else
3511 {
3512 if (do_lock_var(&lv, name_end, deep,
3513 eap->cmdidx == CMD_lockvar) == FAIL)
3514 error = TRUE;
3515 }
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003518 if (!eap->skip)
3519 clear_lval(&lv);
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 arg = skipwhite(name_end);
3522 } while (!ends_excmd(*arg));
3523
3524 eap->nextcmd = check_nextcmd(arg);
3525}
3526
Bram Moolenaar8c711452005-01-14 21:53:12 +00003527 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003528do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003529 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003530 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003531 int forceit;
3532{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003533 int ret = OK;
3534 int cc;
3535
3536 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538 cc = *name_end;
3539 *name_end = NUL;
3540
3541 /* Normal name or expanded name. */
3542 if (check_changedtick(lp->ll_name))
3543 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003544 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003545 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003546 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003547 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003548 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3549 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003550 else if (lp->ll_range)
3551 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553
3554 /* Delete a range of List items. */
3555 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3556 {
3557 li = lp->ll_li->li_next;
3558 listitem_remove(lp->ll_list, lp->ll_li);
3559 lp->ll_li = li;
3560 ++lp->ll_n1;
3561 }
3562 }
3563 else
3564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003566 /* unlet a List item. */
3567 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003569 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003570 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 }
3572
3573 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003574}
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576/*
3577 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003578 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 */
3580 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003581do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 hashtab_T *ht;
3586 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003587 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003588 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 ht = find_var_ht(name, &varname);
3591 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 hi = hash_find(ht, varname);
3594 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003595 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003596 di = HI2DI(hi);
3597 if (var_check_fixed(di->di_flags, name)
3598 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 return FAIL;
3600 delete_var(ht, hi);
3601 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003604 if (forceit)
3605 return OK;
3606 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 return FAIL;
3608}
3609
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610/*
3611 * Lock or unlock variable indicated by "lp".
3612 * "deep" is the levels to go (-1 for unlimited);
3613 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3614 */
3615 static int
3616do_lock_var(lp, name_end, deep, lock)
3617 lval_T *lp;
3618 char_u *name_end;
3619 int deep;
3620 int lock;
3621{
3622 int ret = OK;
3623 int cc;
3624 dictitem_T *di;
3625
3626 if (deep == 0) /* nothing to do */
3627 return OK;
3628
3629 if (lp->ll_tv == NULL)
3630 {
3631 cc = *name_end;
3632 *name_end = NUL;
3633
3634 /* Normal name or expanded name. */
3635 if (check_changedtick(lp->ll_name))
3636 ret = FAIL;
3637 else
3638 {
3639 di = find_var(lp->ll_name, NULL);
3640 if (di == NULL)
3641 ret = FAIL;
3642 else
3643 {
3644 if (lock)
3645 di->di_flags |= DI_FLAGS_LOCK;
3646 else
3647 di->di_flags &= ~DI_FLAGS_LOCK;
3648 item_lock(&di->di_tv, deep, lock);
3649 }
3650 }
3651 *name_end = cc;
3652 }
3653 else if (lp->ll_range)
3654 {
3655 listitem_T *li = lp->ll_li;
3656
3657 /* (un)lock a range of List items. */
3658 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3659 {
3660 item_lock(&li->li_tv, deep, lock);
3661 li = li->li_next;
3662 ++lp->ll_n1;
3663 }
3664 }
3665 else if (lp->ll_list != NULL)
3666 /* (un)lock a List item. */
3667 item_lock(&lp->ll_li->li_tv, deep, lock);
3668 else
3669 /* un(lock) a Dictionary item. */
3670 item_lock(&lp->ll_di->di_tv, deep, lock);
3671
3672 return ret;
3673}
3674
3675/*
3676 * Lock or unlock an item. "deep" is nr of levels to go.
3677 */
3678 static void
3679item_lock(tv, deep, lock)
3680 typval_T *tv;
3681 int deep;
3682 int lock;
3683{
3684 static int recurse = 0;
3685 list_T *l;
3686 listitem_T *li;
3687 dict_T *d;
3688 hashitem_T *hi;
3689 int todo;
3690
3691 if (recurse >= DICT_MAXNEST)
3692 {
3693 EMSG(_("E743: variable nested too deep for (un)lock"));
3694 return;
3695 }
3696 if (deep == 0)
3697 return;
3698 ++recurse;
3699
3700 /* lock/unlock the item itself */
3701 if (lock)
3702 tv->v_lock |= VAR_LOCKED;
3703 else
3704 tv->v_lock &= ~VAR_LOCKED;
3705
3706 switch (tv->v_type)
3707 {
3708 case VAR_LIST:
3709 if ((l = tv->vval.v_list) != NULL)
3710 {
3711 if (lock)
3712 l->lv_lock |= VAR_LOCKED;
3713 else
3714 l->lv_lock &= ~VAR_LOCKED;
3715 if (deep < 0 || deep > 1)
3716 /* recursive: lock/unlock the items the List contains */
3717 for (li = l->lv_first; li != NULL; li = li->li_next)
3718 item_lock(&li->li_tv, deep - 1, lock);
3719 }
3720 break;
3721 case VAR_DICT:
3722 if ((d = tv->vval.v_dict) != NULL)
3723 {
3724 if (lock)
3725 d->dv_lock |= VAR_LOCKED;
3726 else
3727 d->dv_lock &= ~VAR_LOCKED;
3728 if (deep < 0 || deep > 1)
3729 {
3730 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003731 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003732 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3733 {
3734 if (!HASHITEM_EMPTY(hi))
3735 {
3736 --todo;
3737 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3738 }
3739 }
3740 }
3741 }
3742 }
3743 --recurse;
3744}
3745
Bram Moolenaara40058a2005-07-11 22:42:07 +00003746/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003747 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3748 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003749 */
3750 static int
3751tv_islocked(tv)
3752 typval_T *tv;
3753{
3754 return (tv->v_lock & VAR_LOCKED)
3755 || (tv->v_type == VAR_LIST
3756 && tv->vval.v_list != NULL
3757 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3758 || (tv->v_type == VAR_DICT
3759 && tv->vval.v_dict != NULL
3760 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3761}
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3764/*
3765 * Delete all "menutrans_" variables.
3766 */
3767 void
3768del_menutrans_vars()
3769{
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003771 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Bram Moolenaar33570922005-01-25 22:26:29 +00003773 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003774 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003775 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003776 {
3777 if (!HASHITEM_EMPTY(hi))
3778 {
3779 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3781 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 }
3783 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785}
3786#endif
3787
3788#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3789
3790/*
3791 * Local string buffer for the next two functions to store a variable name
3792 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3793 * get_user_var_name().
3794 */
3795
3796static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3797
3798static char_u *varnamebuf = NULL;
3799static int varnamebuflen = 0;
3800
3801/*
3802 * Function to concatenate a prefix and a variable name.
3803 */
3804 static char_u *
3805cat_prefix_varname(prefix, name)
3806 int prefix;
3807 char_u *name;
3808{
3809 int len;
3810
3811 len = (int)STRLEN(name) + 3;
3812 if (len > varnamebuflen)
3813 {
3814 vim_free(varnamebuf);
3815 len += 10; /* some additional space */
3816 varnamebuf = alloc(len);
3817 if (varnamebuf == NULL)
3818 {
3819 varnamebuflen = 0;
3820 return NULL;
3821 }
3822 varnamebuflen = len;
3823 }
3824 *varnamebuf = prefix;
3825 varnamebuf[1] = ':';
3826 STRCPY(varnamebuf + 2, name);
3827 return varnamebuf;
3828}
3829
3830/*
3831 * Function given to ExpandGeneric() to obtain the list of user defined
3832 * (global/buffer/window/built-in) variable names.
3833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 char_u *
3835get_user_var_name(xp, idx)
3836 expand_T *xp;
3837 int idx;
3838{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003839 static long_u gdone;
3840 static long_u bdone;
3841 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003842#ifdef FEAT_WINDOWS
3843 static long_u tdone;
3844#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003845 static int vidx;
3846 static hashitem_T *hi;
3847 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003850 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003852#ifdef FEAT_WINDOWS
3853 tdone = 0;
3854#endif
3855 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003856
3857 /* Global variables */
3858 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003861 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003862 else
3863 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003864 while (HASHITEM_EMPTY(hi))
3865 ++hi;
3866 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3867 return cat_prefix_varname('g', hi->hi_key);
3868 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003870
3871 /* b: variables */
3872 ht = &curbuf->b_vars.dv_hashtab;
3873 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003875 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003876 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003877 else
3878 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003879 while (HASHITEM_EMPTY(hi))
3880 ++hi;
3881 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003883 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003885 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 return (char_u *)"b:changedtick";
3887 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003888
3889 /* w: variables */
3890 ht = &curwin->w_vars.dv_hashtab;
3891 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003893 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003894 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003895 else
3896 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003897 while (HASHITEM_EMPTY(hi))
3898 ++hi;
3899 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003902#ifdef FEAT_WINDOWS
3903 /* t: variables */
3904 ht = &curtab->tp_vars.dv_hashtab;
3905 if (tdone < ht->ht_used)
3906 {
3907 if (tdone++ == 0)
3908 hi = ht->ht_array;
3909 else
3910 ++hi;
3911 while (HASHITEM_EMPTY(hi))
3912 ++hi;
3913 return cat_prefix_varname('t', hi->hi_key);
3914 }
3915#endif
3916
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 /* v: variables */
3918 if (vidx < VV_LEN)
3919 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 vim_free(varnamebuf);
3922 varnamebuf = NULL;
3923 varnamebuflen = 0;
3924 return NULL;
3925}
3926
3927#endif /* FEAT_CMDL_COMPL */
3928
3929/*
3930 * types for expressions.
3931 */
3932typedef enum
3933{
3934 TYPE_UNKNOWN = 0
3935 , TYPE_EQUAL /* == */
3936 , TYPE_NEQUAL /* != */
3937 , TYPE_GREATER /* > */
3938 , TYPE_GEQUAL /* >= */
3939 , TYPE_SMALLER /* < */
3940 , TYPE_SEQUAL /* <= */
3941 , TYPE_MATCH /* =~ */
3942 , TYPE_NOMATCH /* !~ */
3943} exptype_T;
3944
3945/*
3946 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3949 */
3950
3951/*
3952 * Handle zero level expression.
3953 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003955 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 * Return OK or FAIL.
3957 */
3958 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 char_u **nextcmd;
3963 int evaluate;
3964{
3965 int ret;
3966 char_u *p;
3967
3968 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (ret == FAIL || !ends_excmd(*p))
3971 {
3972 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003973 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 /*
3975 * Report the invalid expression unless the expression evaluation has
3976 * been cancelled due to an aborting error, an interrupt, or an
3977 * exception.
3978 */
3979 if (!aborting())
3980 EMSG2(_(e_invexpr2), arg);
3981 ret = FAIL;
3982 }
3983 if (nextcmd != NULL)
3984 *nextcmd = check_nextcmd(p);
3985
3986 return ret;
3987}
3988
3989/*
3990 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003991 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 *
3993 * "arg" must point to the first non-white of the expression.
3994 * "arg" is advanced to the next non-white after the recognized expression.
3995 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003996 * Note: "rettv.v_lock" is not set.
3997 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * Return OK or FAIL.
3999 */
4000 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 int evaluate;
4005{
4006 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 /*
4010 * Get the first variable.
4011 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return FAIL;
4014
4015 if ((*arg)[0] == '?')
4016 {
4017 result = FALSE;
4018 if (evaluate)
4019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004020 int error = FALSE;
4021
4022 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004024 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004025 if (error)
4026 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028
4029 /*
4030 * Get the second variable.
4031 */
4032 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return FAIL;
4035
4036 /*
4037 * Check for the ":".
4038 */
4039 if ((*arg)[0] != ':')
4040 {
4041 EMSG(_("E109: Missing ':' after '?'"));
4042 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return FAIL;
4045 }
4046
4047 /*
4048 * Get the third variable.
4049 */
4050 *arg = skipwhite(*arg + 1);
4051 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4052 {
4053 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return FAIL;
4056 }
4057 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060
4061 return OK;
4062}
4063
4064/*
4065 * Handle first level expression:
4066 * expr2 || expr2 || expr2 logical OR
4067 *
4068 * "arg" must point to the first non-white of the expression.
4069 * "arg" is advanced to the next non-white after the recognized expression.
4070 *
4071 * Return OK or FAIL.
4072 */
4073 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 int evaluate;
4078{
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 long result;
4081 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004082 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /*
4085 * Get the first variable.
4086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return FAIL;
4089
4090 /*
4091 * Repeat until there is no following "||".
4092 */
4093 first = TRUE;
4094 result = FALSE;
4095 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4096 {
4097 if (evaluate && first)
4098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004102 if (error)
4103 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 first = FALSE;
4105 }
4106
4107 /*
4108 * Get the second variable.
4109 */
4110 *arg = skipwhite(*arg + 2);
4111 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4112 return FAIL;
4113
4114 /*
4115 * Compute the result.
4116 */
4117 if (evaluate && !result)
4118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004119 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004122 if (error)
4123 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125 if (evaluate)
4126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 rettv->v_type = VAR_NUMBER;
4128 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 }
4131
4132 return OK;
4133}
4134
4135/*
4136 * Handle second level expression:
4137 * expr3 && expr3 && expr3 logical AND
4138 *
4139 * "arg" must point to the first non-white of the expression.
4140 * "arg" is advanced to the next non-white after the recognized expression.
4141 *
4142 * Return OK or FAIL.
4143 */
4144 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int evaluate;
4149{
Bram Moolenaar33570922005-01-25 22:26:29 +00004150 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 long result;
4152 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
4155 /*
4156 * Get the first variable.
4157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 return FAIL;
4160
4161 /*
4162 * Repeat until there is no following "&&".
4163 */
4164 first = TRUE;
4165 result = TRUE;
4166 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4167 {
4168 if (evaluate && first)
4169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 if (error)
4174 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 first = FALSE;
4176 }
4177
4178 /*
4179 * Get the second variable.
4180 */
4181 *arg = skipwhite(*arg + 2);
4182 if (eval4(arg, &var2, evaluate && result) == FAIL)
4183 return FAIL;
4184
4185 /*
4186 * Compute the result.
4187 */
4188 if (evaluate && result)
4189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 if (error)
4194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 if (evaluate)
4197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 rettv->v_type = VAR_NUMBER;
4199 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202
4203 return OK;
4204}
4205
4206/*
4207 * Handle third level expression:
4208 * var1 == var2
4209 * var1 =~ var2
4210 * var1 != var2
4211 * var1 !~ var2
4212 * var1 > var2
4213 * var1 >= var2
4214 * var1 < var2
4215 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 * var1 is var2
4217 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 *
4219 * "arg" must point to the first non-white of the expression.
4220 * "arg" is advanced to the next non-white after the recognized expression.
4221 *
4222 * Return OK or FAIL.
4223 */
4224 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int evaluate;
4229{
Bram Moolenaar33570922005-01-25 22:26:29 +00004230 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 char_u *p;
4232 int i;
4233 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004234 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 int len = 2;
4236 long n1, n2;
4237 char_u *s1, *s2;
4238 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4239 regmatch_T regmatch;
4240 int ic;
4241 char_u *save_cpo;
4242
4243 /*
4244 * Get the first variable.
4245 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 return FAIL;
4248
4249 p = *arg;
4250 switch (p[0])
4251 {
4252 case '=': if (p[1] == '=')
4253 type = TYPE_EQUAL;
4254 else if (p[1] == '~')
4255 type = TYPE_MATCH;
4256 break;
4257 case '!': if (p[1] == '=')
4258 type = TYPE_NEQUAL;
4259 else if (p[1] == '~')
4260 type = TYPE_NOMATCH;
4261 break;
4262 case '>': if (p[1] != '=')
4263 {
4264 type = TYPE_GREATER;
4265 len = 1;
4266 }
4267 else
4268 type = TYPE_GEQUAL;
4269 break;
4270 case '<': if (p[1] != '=')
4271 {
4272 type = TYPE_SMALLER;
4273 len = 1;
4274 }
4275 else
4276 type = TYPE_SEQUAL;
4277 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004278 case 'i': if (p[1] == 's')
4279 {
4280 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4281 len = 5;
4282 if (!vim_isIDc(p[len]))
4283 {
4284 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4285 type_is = TRUE;
4286 }
4287 }
4288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290
4291 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004292 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 */
4294 if (type != TYPE_UNKNOWN)
4295 {
4296 /* extra question mark appended: ignore case */
4297 if (p[len] == '?')
4298 {
4299 ic = TRUE;
4300 ++len;
4301 }
4302 /* extra '#' appended: match case */
4303 else if (p[len] == '#')
4304 {
4305 ic = FALSE;
4306 ++len;
4307 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004308 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 else
4310 ic = p_ic;
4311
4312 /*
4313 * Get the second variable.
4314 */
4315 *arg = skipwhite(p + len);
4316 if (eval5(arg, &var2, evaluate) == FAIL)
4317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return FAIL;
4320 }
4321
4322 if (evaluate)
4323 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004324 if (type_is && rettv->v_type != var2.v_type)
4325 {
4326 /* For "is" a different type always means FALSE, for "notis"
4327 * it means TRUE. */
4328 n1 = (type == TYPE_NEQUAL);
4329 }
4330 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4331 {
4332 if (type_is)
4333 {
4334 n1 = (rettv->v_type == var2.v_type
4335 && rettv->vval.v_list == var2.vval.v_list);
4336 if (type == TYPE_NEQUAL)
4337 n1 = !n1;
4338 }
4339 else if (rettv->v_type != var2.v_type
4340 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4341 {
4342 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004343 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004344 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004345 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004346 clear_tv(rettv);
4347 clear_tv(&var2);
4348 return FAIL;
4349 }
4350 else
4351 {
4352 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004353 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4354 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004355 if (type == TYPE_NEQUAL)
4356 n1 = !n1;
4357 }
4358 }
4359
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004360 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4361 {
4362 if (type_is)
4363 {
4364 n1 = (rettv->v_type == var2.v_type
4365 && rettv->vval.v_dict == var2.vval.v_dict);
4366 if (type == TYPE_NEQUAL)
4367 n1 = !n1;
4368 }
4369 else if (rettv->v_type != var2.v_type
4370 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4371 {
4372 if (rettv->v_type != var2.v_type)
4373 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4374 else
4375 EMSG(_("E736: Invalid operation for Dictionary"));
4376 clear_tv(rettv);
4377 clear_tv(&var2);
4378 return FAIL;
4379 }
4380 else
4381 {
4382 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004383 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4384 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004385 if (type == TYPE_NEQUAL)
4386 n1 = !n1;
4387 }
4388 }
4389
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004390 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4391 {
4392 if (rettv->v_type != var2.v_type
4393 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4394 {
4395 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004396 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004398 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004399 clear_tv(rettv);
4400 clear_tv(&var2);
4401 return FAIL;
4402 }
4403 else
4404 {
4405 /* Compare two Funcrefs for being equal or unequal. */
4406 if (rettv->vval.v_string == NULL
4407 || var2.vval.v_string == NULL)
4408 n1 = FALSE;
4409 else
4410 n1 = STRCMP(rettv->vval.v_string,
4411 var2.vval.v_string) == 0;
4412 if (type == TYPE_NEQUAL)
4413 n1 = !n1;
4414 }
4415 }
4416
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004417#ifdef FEAT_FLOAT
4418 /*
4419 * If one of the two variables is a float, compare as a float.
4420 * When using "=~" or "!~", always compare as string.
4421 */
4422 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4423 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4424 {
4425 float_T f1, f2;
4426
4427 if (rettv->v_type == VAR_FLOAT)
4428 f1 = rettv->vval.v_float;
4429 else
4430 f1 = get_tv_number(rettv);
4431 if (var2.v_type == VAR_FLOAT)
4432 f2 = var2.vval.v_float;
4433 else
4434 f2 = get_tv_number(&var2);
4435 n1 = FALSE;
4436 switch (type)
4437 {
4438 case TYPE_EQUAL: n1 = (f1 == f2); break;
4439 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4440 case TYPE_GREATER: n1 = (f1 > f2); break;
4441 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4442 case TYPE_SMALLER: n1 = (f1 < f2); break;
4443 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4444 case TYPE_UNKNOWN:
4445 case TYPE_MATCH:
4446 case TYPE_NOMATCH: break; /* avoid gcc warning */
4447 }
4448 }
4449#endif
4450
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 /*
4452 * If one of the two variables is a number, compare as a number.
4453 * When using "=~" or "!~", always compare as string.
4454 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004455 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4457 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004458 n1 = get_tv_number(rettv);
4459 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 switch (type)
4461 {
4462 case TYPE_EQUAL: n1 = (n1 == n2); break;
4463 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4464 case TYPE_GREATER: n1 = (n1 > n2); break;
4465 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4466 case TYPE_SMALLER: n1 = (n1 < n2); break;
4467 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4468 case TYPE_UNKNOWN:
4469 case TYPE_MATCH:
4470 case TYPE_NOMATCH: break; /* avoid gcc warning */
4471 }
4472 }
4473 else
4474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 s1 = get_tv_string_buf(rettv, buf1);
4476 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4478 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4479 else
4480 i = 0;
4481 n1 = FALSE;
4482 switch (type)
4483 {
4484 case TYPE_EQUAL: n1 = (i == 0); break;
4485 case TYPE_NEQUAL: n1 = (i != 0); break;
4486 case TYPE_GREATER: n1 = (i > 0); break;
4487 case TYPE_GEQUAL: n1 = (i >= 0); break;
4488 case TYPE_SMALLER: n1 = (i < 0); break;
4489 case TYPE_SEQUAL: n1 = (i <= 0); break;
4490
4491 case TYPE_MATCH:
4492 case TYPE_NOMATCH:
4493 /* avoid 'l' flag in 'cpoptions' */
4494 save_cpo = p_cpo;
4495 p_cpo = (char_u *)"";
4496 regmatch.regprog = vim_regcomp(s2,
4497 RE_MAGIC + RE_STRING);
4498 regmatch.rm_ic = ic;
4499 if (regmatch.regprog != NULL)
4500 {
4501 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4502 vim_free(regmatch.regprog);
4503 if (type == TYPE_NOMATCH)
4504 n1 = !n1;
4505 }
4506 p_cpo = save_cpo;
4507 break;
4508
4509 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4510 }
4511 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004512 clear_tv(rettv);
4513 clear_tv(&var2);
4514 rettv->v_type = VAR_NUMBER;
4515 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517 }
4518
4519 return OK;
4520}
4521
4522/*
4523 * Handle fourth level expression:
4524 * + number addition
4525 * - number subtraction
4526 * . string concatenation
4527 *
4528 * "arg" must point to the first non-white of the expression.
4529 * "arg" is advanced to the next non-white after the recognized expression.
4530 *
4531 * Return OK or FAIL.
4532 */
4533 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004534eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 int evaluate;
4538{
Bram Moolenaar33570922005-01-25 22:26:29 +00004539 typval_T var2;
4540 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 int op;
4542 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004543#ifdef FEAT_FLOAT
4544 float_T f1 = 0, f2 = 0;
4545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 char_u *s1, *s2;
4547 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4548 char_u *p;
4549
4550 /*
4551 * Get the first variable.
4552 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004553 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 return FAIL;
4555
4556 /*
4557 * Repeat computing, until no '+', '-' or '.' is following.
4558 */
4559 for (;;)
4560 {
4561 op = **arg;
4562 if (op != '+' && op != '-' && op != '.')
4563 break;
4564
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004565 if ((op != '+' || rettv->v_type != VAR_LIST)
4566#ifdef FEAT_FLOAT
4567 && (op == '.' || rettv->v_type != VAR_FLOAT)
4568#endif
4569 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004570 {
4571 /* For "list + ...", an illegal use of the first operand as
4572 * a number cannot be determined before evaluating the 2nd
4573 * operand: if this is also a list, all is ok.
4574 * For "something . ...", "something - ..." or "non-list + ...",
4575 * we know that the first operand needs to be a string or number
4576 * without evaluating the 2nd operand. So check before to avoid
4577 * side effects after an error. */
4578 if (evaluate && get_tv_string_chk(rettv) == NULL)
4579 {
4580 clear_tv(rettv);
4581 return FAIL;
4582 }
4583 }
4584
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 /*
4586 * Get the second variable.
4587 */
4588 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004589 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004591 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 return FAIL;
4593 }
4594
4595 if (evaluate)
4596 {
4597 /*
4598 * Compute the result.
4599 */
4600 if (op == '.')
4601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004602 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4603 s2 = get_tv_string_buf_chk(&var2, buf2);
4604 if (s2 == NULL) /* type error ? */
4605 {
4606 clear_tv(rettv);
4607 clear_tv(&var2);
4608 return FAIL;
4609 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004610 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004611 clear_tv(rettv);
4612 rettv->v_type = VAR_STRING;
4613 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004615 else if (op == '+' && rettv->v_type == VAR_LIST
4616 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004617 {
4618 /* concatenate Lists */
4619 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4620 &var3) == FAIL)
4621 {
4622 clear_tv(rettv);
4623 clear_tv(&var2);
4624 return FAIL;
4625 }
4626 clear_tv(rettv);
4627 *rettv = var3;
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 else
4630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 int error = FALSE;
4632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633#ifdef FEAT_FLOAT
4634 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004635 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004636 f1 = rettv->vval.v_float;
4637 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004639 else
4640#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004641 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004642 n1 = get_tv_number_chk(rettv, &error);
4643 if (error)
4644 {
4645 /* This can only happen for "list + non-list". For
4646 * "non-list + ..." or "something - ...", we returned
4647 * before evaluating the 2nd operand. */
4648 clear_tv(rettv);
4649 return FAIL;
4650 }
4651#ifdef FEAT_FLOAT
4652 if (var2.v_type == VAR_FLOAT)
4653 f1 = n1;
4654#endif
4655 }
4656#ifdef FEAT_FLOAT
4657 if (var2.v_type == VAR_FLOAT)
4658 {
4659 f2 = var2.vval.v_float;
4660 n2 = 0;
4661 }
4662 else
4663#endif
4664 {
4665 n2 = get_tv_number_chk(&var2, &error);
4666 if (error)
4667 {
4668 clear_tv(rettv);
4669 clear_tv(&var2);
4670 return FAIL;
4671 }
4672#ifdef FEAT_FLOAT
4673 if (rettv->v_type == VAR_FLOAT)
4674 f2 = n2;
4675#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004678
4679#ifdef FEAT_FLOAT
4680 /* If there is a float on either side the result is a float. */
4681 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4682 {
4683 if (op == '+')
4684 f1 = f1 + f2;
4685 else
4686 f1 = f1 - f2;
4687 rettv->v_type = VAR_FLOAT;
4688 rettv->vval.v_float = f1;
4689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004691#endif
4692 {
4693 if (op == '+')
4694 n1 = n1 + n2;
4695 else
4696 n1 = n1 - n2;
4697 rettv->v_type = VAR_NUMBER;
4698 rettv->vval.v_number = n1;
4699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004701 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 }
4704 return OK;
4705}
4706
4707/*
4708 * Handle fifth level expression:
4709 * * number multiplication
4710 * / number division
4711 * % number modulo
4712 *
4713 * "arg" must point to the first non-white of the expression.
4714 * "arg" is advanced to the next non-white after the recognized expression.
4715 *
4716 * Return OK or FAIL.
4717 */
4718 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004719eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004723 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
Bram Moolenaar33570922005-01-25 22:26:29 +00004725 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 int op;
4727 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728#ifdef FEAT_FLOAT
4729 int use_float = FALSE;
4730 float_T f1 = 0, f2;
4731#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004732 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733
4734 /*
4735 * Get the first variable.
4736 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004737 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 return FAIL;
4739
4740 /*
4741 * Repeat computing, until no '*', '/' or '%' is following.
4742 */
4743 for (;;)
4744 {
4745 op = **arg;
4746 if (op != '*' && op != '/' && op != '%')
4747 break;
4748
4749 if (evaluate)
4750 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004751#ifdef FEAT_FLOAT
4752 if (rettv->v_type == VAR_FLOAT)
4753 {
4754 f1 = rettv->vval.v_float;
4755 use_float = TRUE;
4756 n1 = 0;
4757 }
4758 else
4759#endif
4760 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004762 if (error)
4763 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765 else
4766 n1 = 0;
4767
4768 /*
4769 * Get the second variable.
4770 */
4771 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004772 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 return FAIL;
4774
4775 if (evaluate)
4776 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004777#ifdef FEAT_FLOAT
4778 if (var2.v_type == VAR_FLOAT)
4779 {
4780 if (!use_float)
4781 {
4782 f1 = n1;
4783 use_float = TRUE;
4784 }
4785 f2 = var2.vval.v_float;
4786 n2 = 0;
4787 }
4788 else
4789#endif
4790 {
4791 n2 = get_tv_number_chk(&var2, &error);
4792 clear_tv(&var2);
4793 if (error)
4794 return FAIL;
4795#ifdef FEAT_FLOAT
4796 if (use_float)
4797 f2 = n2;
4798#endif
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
4801 /*
4802 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805#ifdef FEAT_FLOAT
4806 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808 if (op == '*')
4809 f1 = f1 * f2;
4810 else if (op == '/')
4811 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004812# ifdef VMS
4813 /* VMS crashes on divide by zero, work around it */
4814 if (f2 == 0.0)
4815 {
4816 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004817 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004818 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004819 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004820 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004821 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004822 }
4823 else
4824 f1 = f1 / f2;
4825# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004826 /* We rely on the floating point library to handle divide
4827 * by zero to result in "inf" and not a crash. */
4828 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004829# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004832 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004833 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004834 return FAIL;
4835 }
4836 rettv->v_type = VAR_FLOAT;
4837 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 }
4839 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842 if (op == '*')
4843 n1 = n1 * n2;
4844 else if (op == '/')
4845 {
4846 if (n2 == 0) /* give an error message? */
4847 {
4848 if (n1 == 0)
4849 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4850 else if (n1 < 0)
4851 n1 = -0x7fffffffL;
4852 else
4853 n1 = 0x7fffffffL;
4854 }
4855 else
4856 n1 = n1 / n2;
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004859 {
4860 if (n2 == 0) /* give an error message? */
4861 n1 = 0;
4862 else
4863 n1 = n1 % n2;
4864 }
4865 rettv->v_type = VAR_NUMBER;
4866 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 }
4869 }
4870
4871 return OK;
4872}
4873
4874/*
4875 * Handle sixth level expression:
4876 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004877 * "string" string constant
4878 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 * &option-name option value
4880 * @r register contents
4881 * identifier variable value
4882 * function() function call
4883 * $VAR environment variable
4884 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004885 * [expr, expr] List
4886 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 *
4888 * Also handle:
4889 * ! in front logical NOT
4890 * - in front unary minus
4891 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004892 * trailing [] subscript in String or List
4893 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 *
4895 * "arg" must point to the first non-white of the expression.
4896 * "arg" is advanced to the next non-white after the recognized expression.
4897 *
4898 * Return OK or FAIL.
4899 */
4900 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004901eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004905 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 long n;
4908 int len;
4909 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 char_u *start_leader, *end_leader;
4911 int ret = OK;
4912 char_u *alias;
4913
4914 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004916 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004918 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919
4920 /*
4921 * Skip '!' and '-' characters. They are handled later.
4922 */
4923 start_leader = *arg;
4924 while (**arg == '!' || **arg == '-' || **arg == '+')
4925 *arg = skipwhite(*arg + 1);
4926 end_leader = *arg;
4927
4928 switch (**arg)
4929 {
4930 /*
4931 * Number constant.
4932 */
4933 case '0':
4934 case '1':
4935 case '2':
4936 case '3':
4937 case '4':
4938 case '5':
4939 case '6':
4940 case '7':
4941 case '8':
4942 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004943 {
4944#ifdef FEAT_FLOAT
4945 char_u *p = skipdigits(*arg + 1);
4946 int get_float = FALSE;
4947
4948 /* We accept a float when the format matches
4949 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004950 * strict to avoid backwards compatibility problems.
4951 * Don't look for a float after the "." operator, so that
4952 * ":let vers = 1.2.3" doesn't fail. */
4953 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955 get_float = TRUE;
4956 p = skipdigits(p + 2);
4957 if (*p == 'e' || *p == 'E')
4958 {
4959 ++p;
4960 if (*p == '-' || *p == '+')
4961 ++p;
4962 if (!vim_isdigit(*p))
4963 get_float = FALSE;
4964 else
4965 p = skipdigits(p + 1);
4966 }
4967 if (ASCII_ISALPHA(*p) || *p == '.')
4968 get_float = FALSE;
4969 }
4970 if (get_float)
4971 {
4972 float_T f;
4973
4974 *arg += string2float(*arg, &f);
4975 if (evaluate)
4976 {
4977 rettv->v_type = VAR_FLOAT;
4978 rettv->vval.v_float = f;
4979 }
4980 }
4981 else
4982#endif
4983 {
4984 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4985 *arg += len;
4986 if (evaluate)
4987 {
4988 rettv->v_type = VAR_NUMBER;
4989 rettv->vval.v_number = n;
4990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995 /*
4996 * String constant: "string".
4997 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004998 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 break;
5000
5001 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005004 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005005 break;
5006
5007 /*
5008 * List: [expr, expr]
5009 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005010 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 break;
5012
5013 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 * Dictionary: {key: val, key: val}
5015 */
5016 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5017 break;
5018
5019 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005020 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005022 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 break;
5024
5025 /*
5026 * Environment variable: $VAR.
5027 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005028 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030
5031 /*
5032 * Register contents: @r.
5033 */
5034 case '@': ++*arg;
5035 if (evaluate)
5036 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005037 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005038 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040 if (**arg != NUL)
5041 ++*arg;
5042 break;
5043
5044 /*
5045 * nested expression: (expression).
5046 */
5047 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005048 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 if (**arg == ')')
5050 ++*arg;
5051 else if (ret == OK)
5052 {
5053 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005054 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 ret = FAIL;
5056 }
5057 break;
5058
Bram Moolenaar8c711452005-01-14 21:53:12 +00005059 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 break;
5061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062
5063 if (ret == NOTDONE)
5064 {
5065 /*
5066 * Must be a variable or function name.
5067 * Can also be a curly-braces kind of name: {expr}.
5068 */
5069 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005070 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 if (alias != NULL)
5072 s = alias;
5073
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005074 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 ret = FAIL;
5076 else
5077 {
5078 if (**arg == '(') /* recursive! */
5079 {
5080 /* If "s" is the name of a variable of type VAR_FUNC
5081 * use its contents. */
5082 s = deref_func_name(s, &len);
5083
5084 /* Invoke the function. */
5085 ret = get_func_tv(s, len, rettv, arg,
5086 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005087 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 /* Stop the expression evaluation when immediately
5089 * aborting on error, or when an interrupt occurred or
5090 * an exception was thrown but not caught. */
5091 if (aborting())
5092 {
5093 if (ret == OK)
5094 clear_tv(rettv);
5095 ret = FAIL;
5096 }
5097 }
5098 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005099 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005100 else
5101 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005102 }
5103
5104 if (alias != NULL)
5105 vim_free(alias);
5106 }
5107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 *arg = skipwhite(*arg);
5109
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005110 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5111 * expr(expr). */
5112 if (ret == OK)
5113 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115 /*
5116 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5117 */
5118 if (ret == OK && evaluate && end_leader > start_leader)
5119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005120 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005121 int val = 0;
5122#ifdef FEAT_FLOAT
5123 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005124
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005125 if (rettv->v_type == VAR_FLOAT)
5126 f = rettv->vval.v_float;
5127 else
5128#endif
5129 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005130 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005132 clear_tv(rettv);
5133 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005135 else
5136 {
5137 while (end_leader > start_leader)
5138 {
5139 --end_leader;
5140 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005141 {
5142#ifdef FEAT_FLOAT
5143 if (rettv->v_type == VAR_FLOAT)
5144 f = !f;
5145 else
5146#endif
5147 val = !val;
5148 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005149 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005150 {
5151#ifdef FEAT_FLOAT
5152 if (rettv->v_type == VAR_FLOAT)
5153 f = -f;
5154 else
5155#endif
5156 val = -val;
5157 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005158 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005159#ifdef FEAT_FLOAT
5160 if (rettv->v_type == VAR_FLOAT)
5161 {
5162 clear_tv(rettv);
5163 rettv->vval.v_float = f;
5164 }
5165 else
5166#endif
5167 {
5168 clear_tv(rettv);
5169 rettv->v_type = VAR_NUMBER;
5170 rettv->vval.v_number = val;
5171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
5174
5175 return ret;
5176}
5177
5178/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005179 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5180 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005181 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5182 */
5183 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005184eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005186 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005188 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005189{
5190 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005191 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005192 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005193 long len = -1;
5194 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005195 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 if (rettv->v_type == VAR_FUNC
5199#ifdef FEAT_FLOAT
5200 || rettv->v_type == VAR_FLOAT
5201#endif
5202 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005204 if (verbose)
5205 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005206 return FAIL;
5207 }
5208
Bram Moolenaar8c711452005-01-14 21:53:12 +00005209 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005211 /*
5212 * dict.name
5213 */
5214 key = *arg + 1;
5215 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5216 ;
5217 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005218 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005220 }
5221 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005223 /*
5224 * something[idx]
5225 *
5226 * Get the (first) variable from inside the [].
5227 */
5228 *arg = skipwhite(*arg + 1);
5229 if (**arg == ':')
5230 empty1 = TRUE;
5231 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5232 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005233 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5234 {
5235 /* not a number or string */
5236 clear_tv(&var1);
5237 return FAIL;
5238 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239
5240 /*
5241 * Get the second variable from inside the [:].
5242 */
5243 if (**arg == ':')
5244 {
5245 range = TRUE;
5246 *arg = skipwhite(*arg + 1);
5247 if (**arg == ']')
5248 empty2 = TRUE;
5249 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005251 if (!empty1)
5252 clear_tv(&var1);
5253 return FAIL;
5254 }
5255 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5256 {
5257 /* not a number or string */
5258 if (!empty1)
5259 clear_tv(&var1);
5260 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 return FAIL;
5262 }
5263 }
5264
5265 /* Check for the ']'. */
5266 if (**arg != ']')
5267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005268 if (verbose)
5269 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 clear_tv(&var1);
5271 if (range)
5272 clear_tv(&var2);
5273 return FAIL;
5274 }
5275 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 }
5277
5278 if (evaluate)
5279 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 n1 = 0;
5281 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005283 n1 = get_tv_number(&var1);
5284 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 }
5286 if (range)
5287 {
5288 if (empty2)
5289 n2 = -1;
5290 else
5291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005292 n2 = get_tv_number(&var2);
5293 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 }
5295 }
5296
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005297 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 {
5299 case VAR_NUMBER:
5300 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005301 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 len = (long)STRLEN(s);
5303 if (range)
5304 {
5305 /* The resulting variable is a substring. If the indexes
5306 * are out of range the result is empty. */
5307 if (n1 < 0)
5308 {
5309 n1 = len + n1;
5310 if (n1 < 0)
5311 n1 = 0;
5312 }
5313 if (n2 < 0)
5314 n2 = len + n2;
5315 else if (n2 >= len)
5316 n2 = len;
5317 if (n1 >= len || n2 < 0 || n1 > n2)
5318 s = NULL;
5319 else
5320 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5321 }
5322 else
5323 {
5324 /* The resulting variable is a string of a single
5325 * character. If the index is too big or negative the
5326 * result is empty. */
5327 if (n1 >= len || n1 < 0)
5328 s = NULL;
5329 else
5330 s = vim_strnsave(s + n1, 1);
5331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005332 clear_tv(rettv);
5333 rettv->v_type = VAR_STRING;
5334 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335 break;
5336
5337 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005338 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005339 if (n1 < 0)
5340 n1 = len + n1;
5341 if (!empty1 && (n1 < 0 || n1 >= len))
5342 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005343 /* For a range we allow invalid values and return an empty
5344 * list. A list index out of range is an error. */
5345 if (!range)
5346 {
5347 if (verbose)
5348 EMSGN(_(e_listidx), n1);
5349 return FAIL;
5350 }
5351 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005352 }
5353 if (range)
5354 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005355 list_T *l;
5356 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357
5358 if (n2 < 0)
5359 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005360 else if (n2 >= len)
5361 n2 = len - 1;
5362 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005363 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 l = list_alloc();
5365 if (l == NULL)
5366 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005367 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 n1 <= n2; ++n1)
5369 {
5370 if (list_append_tv(l, &item->li_tv) == FAIL)
5371 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005372 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373 return FAIL;
5374 }
5375 item = item->li_next;
5376 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 clear_tv(rettv);
5378 rettv->v_type = VAR_LIST;
5379 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005380 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 }
5382 else
5383 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005384 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 clear_tv(rettv);
5386 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387 }
5388 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005389
5390 case VAR_DICT:
5391 if (range)
5392 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005393 if (verbose)
5394 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 if (len == -1)
5396 clear_tv(&var1);
5397 return FAIL;
5398 }
5399 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005400 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401
5402 if (len == -1)
5403 {
5404 key = get_tv_string(&var1);
5405 if (*key == NUL)
5406 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005407 if (verbose)
5408 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409 clear_tv(&var1);
5410 return FAIL;
5411 }
5412 }
5413
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005414 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005415
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005416 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005417 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005418 if (len == -1)
5419 clear_tv(&var1);
5420 if (item == NULL)
5421 return FAIL;
5422
5423 copy_tv(&item->di_tv, &var1);
5424 clear_tv(rettv);
5425 *rettv = var1;
5426 }
5427 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 }
5429 }
5430
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 return OK;
5432}
5433
5434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 * Get an option value.
5436 * "arg" points to the '&' or '+' before the option name.
5437 * "arg" is advanced to character after the option name.
5438 * Return OK or FAIL.
5439 */
5440 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005441get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005443 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 int evaluate;
5445{
5446 char_u *option_end;
5447 long numval;
5448 char_u *stringval;
5449 int opt_type;
5450 int c;
5451 int working = (**arg == '+'); /* has("+option") */
5452 int ret = OK;
5453 int opt_flags;
5454
5455 /*
5456 * Isolate the option name and find its value.
5457 */
5458 option_end = find_option_end(arg, &opt_flags);
5459 if (option_end == NULL)
5460 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005461 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 EMSG2(_("E112: Option name missing: %s"), *arg);
5463 return FAIL;
5464 }
5465
5466 if (!evaluate)
5467 {
5468 *arg = option_end;
5469 return OK;
5470 }
5471
5472 c = *option_end;
5473 *option_end = NUL;
5474 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477 if (opt_type == -3) /* invalid name */
5478 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 EMSG2(_("E113: Unknown option: %s"), *arg);
5481 ret = FAIL;
5482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
5485 if (opt_type == -2) /* hidden string option */
5486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005487 rettv->v_type = VAR_STRING;
5488 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 }
5490 else if (opt_type == -1) /* hidden number option */
5491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 rettv->v_type = VAR_NUMBER;
5493 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495 else if (opt_type == 1) /* number option */
5496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497 rettv->v_type = VAR_NUMBER;
5498 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
5500 else /* string option */
5501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 rettv->v_type = VAR_STRING;
5503 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 }
5505 }
5506 else if (working && (opt_type == -2 || opt_type == -1))
5507 ret = FAIL;
5508
5509 *option_end = c; /* put back for error messages */
5510 *arg = option_end;
5511
5512 return ret;
5513}
5514
5515/*
5516 * Allocate a variable for a string constant.
5517 * Return OK or FAIL.
5518 */
5519 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 int evaluate;
5524{
5525 char_u *p;
5526 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 int extra = 0;
5528
5529 /*
5530 * Find the end of the string, skipping backslashed characters.
5531 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005532 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
5534 if (*p == '\\' && p[1] != NUL)
5535 {
5536 ++p;
5537 /* A "\<x>" form occupies at least 4 characters, and produces up
5538 * to 6 characters: reserve space for 2 extra */
5539 if (*p == '<')
5540 extra += 2;
5541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 }
5543
5544 if (*p != '"')
5545 {
5546 EMSG2(_("E114: Missing quote: %s"), *arg);
5547 return FAIL;
5548 }
5549
5550 /* If only parsing, set *arg and return here */
5551 if (!evaluate)
5552 {
5553 *arg = p + 1;
5554 return OK;
5555 }
5556
5557 /*
5558 * Copy the string into allocated memory, handling backslashed
5559 * characters.
5560 */
5561 name = alloc((unsigned)(p - *arg + extra));
5562 if (name == NULL)
5563 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005564 rettv->v_type = VAR_STRING;
5565 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
Bram Moolenaar8c711452005-01-14 21:53:12 +00005567 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
5569 if (*p == '\\')
5570 {
5571 switch (*++p)
5572 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005573 case 'b': *name++ = BS; ++p; break;
5574 case 'e': *name++ = ESC; ++p; break;
5575 case 'f': *name++ = FF; ++p; break;
5576 case 'n': *name++ = NL; ++p; break;
5577 case 'r': *name++ = CAR; ++p; break;
5578 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579
5580 case 'X': /* hex: "\x1", "\x12" */
5581 case 'x':
5582 case 'u': /* Unicode: "\u0023" */
5583 case 'U':
5584 if (vim_isxdigit(p[1]))
5585 {
5586 int n, nr;
5587 int c = toupper(*p);
5588
5589 if (c == 'X')
5590 n = 2;
5591 else
5592 n = 4;
5593 nr = 0;
5594 while (--n >= 0 && vim_isxdigit(p[1]))
5595 {
5596 ++p;
5597 nr = (nr << 4) + hex2nr(*p);
5598 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005599 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600#ifdef FEAT_MBYTE
5601 /* For "\u" store the number according to
5602 * 'encoding'. */
5603 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 else
5606#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005607 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 break;
5610
5611 /* octal: "\1", "\12", "\123" */
5612 case '0':
5613 case '1':
5614 case '2':
5615 case '3':
5616 case '4':
5617 case '5':
5618 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005619 case '7': *name = *p++ - '0';
5620 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005622 *name = (*name << 3) + *p++ - '0';
5623 if (*p >= '0' && *p <= '7')
5624 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 break;
5628
5629 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005630 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (extra != 0)
5632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005633 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 break;
5635 }
5636 /* FALLTHROUGH */
5637
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 break;
5640 }
5641 }
5642 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 *arg = p + 1;
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 return OK;
5650}
5651
5652/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 * Return OK or FAIL.
5655 */
5656 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005657get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 int evaluate;
5661{
5662 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005663 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005664 int reduce = 0;
5665
5666 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005667 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005668 */
5669 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5670 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005672 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005673 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005674 break;
5675 ++reduce;
5676 ++p;
5677 }
5678 }
5679
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005681 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005683 return FAIL;
5684 }
5685
Bram Moolenaar8c711452005-01-14 21:53:12 +00005686 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005687 if (!evaluate)
5688 {
5689 *arg = p + 1;
5690 return OK;
5691 }
5692
5693 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005694 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005695 */
5696 str = alloc((unsigned)((p - *arg) - reduce));
5697 if (str == NULL)
5698 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 rettv->v_type = VAR_STRING;
5700 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005701
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005703 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005704 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005707 break;
5708 ++p;
5709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005711 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005712 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005713 *arg = p + 1;
5714
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005715 return OK;
5716}
5717
5718/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005719 * Allocate a variable for a List and fill it from "*arg".
5720 * Return OK or FAIL.
5721 */
5722 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005723get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005724 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005725 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005726 int evaluate;
5727{
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 list_T *l = NULL;
5729 typval_T tv;
5730 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731
5732 if (evaluate)
5733 {
5734 l = list_alloc();
5735 if (l == NULL)
5736 return FAIL;
5737 }
5738
5739 *arg = skipwhite(*arg + 1);
5740 while (**arg != ']' && **arg != NUL)
5741 {
5742 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5743 goto failret;
5744 if (evaluate)
5745 {
5746 item = listitem_alloc();
5747 if (item != NULL)
5748 {
5749 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005750 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751 list_append(l, item);
5752 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005753 else
5754 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005755 }
5756
5757 if (**arg == ']')
5758 break;
5759 if (**arg != ',')
5760 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005762 goto failret;
5763 }
5764 *arg = skipwhite(*arg + 1);
5765 }
5766
5767 if (**arg != ']')
5768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005770failret:
5771 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005772 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773 return FAIL;
5774 }
5775
5776 *arg = skipwhite(*arg + 1);
5777 if (evaluate)
5778 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005779 rettv->v_type = VAR_LIST;
5780 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005781 ++l->lv_refcount;
5782 }
5783
5784 return OK;
5785}
5786
5787/*
5788 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005789 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005790 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005791 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005792list_alloc()
5793{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005794 list_T *l;
5795
5796 l = (list_T *)alloc_clear(sizeof(list_T));
5797 if (l != NULL)
5798 {
5799 /* Prepend the list to the list of lists for garbage collection. */
5800 if (first_list != NULL)
5801 first_list->lv_used_prev = l;
5802 l->lv_used_prev = NULL;
5803 l->lv_used_next = first_list;
5804 first_list = l;
5805 }
5806 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807}
5808
5809/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005810 * Allocate an empty list for a return value.
5811 * Returns OK or FAIL.
5812 */
5813 static int
5814rettv_list_alloc(rettv)
5815 typval_T *rettv;
5816{
5817 list_T *l = list_alloc();
5818
5819 if (l == NULL)
5820 return FAIL;
5821
5822 rettv->vval.v_list = l;
5823 rettv->v_type = VAR_LIST;
5824 ++l->lv_refcount;
5825 return OK;
5826}
5827
5828/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 * Unreference a list: decrement the reference count and free it when it
5830 * becomes zero.
5831 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005832 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005836 if (l != NULL && --l->lv_refcount <= 0)
5837 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838}
5839
5840/*
5841 * Free a list, including all items it points to.
5842 * Ignores the reference count.
5843 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005844 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005845list_free(l, recurse)
5846 list_T *l;
5847 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005848{
Bram Moolenaar33570922005-01-25 22:26:29 +00005849 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005851 /* Remove the list from the list of lists for garbage collection. */
5852 if (l->lv_used_prev == NULL)
5853 first_list = l->lv_used_next;
5854 else
5855 l->lv_used_prev->lv_used_next = l->lv_used_next;
5856 if (l->lv_used_next != NULL)
5857 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5858
Bram Moolenaard9fba312005-06-26 22:34:35 +00005859 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005860 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005861 /* Remove the item before deleting it. */
5862 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005863 if (recurse || (item->li_tv.v_type != VAR_LIST
5864 && item->li_tv.v_type != VAR_DICT))
5865 clear_tv(&item->li_tv);
5866 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867 }
5868 vim_free(l);
5869}
5870
5871/*
5872 * Allocate a list item.
5873 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005874 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875listitem_alloc()
5876{
Bram Moolenaar33570922005-01-25 22:26:29 +00005877 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005878}
5879
5880/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005881 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005882 */
5883 static void
5884listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005885 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005886{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005887 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 vim_free(item);
5889}
5890
5891/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005892 * Remove a list item from a List and free it. Also clears the value.
5893 */
5894 static void
5895listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005896 list_T *l;
5897 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005898{
5899 list_remove(l, item, item);
5900 listitem_free(item);
5901}
5902
5903/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 * Get the number of items in a list.
5905 */
5906 static long
5907list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005908 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 if (l == NULL)
5911 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005912 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913}
5914
5915/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005916 * Return TRUE when two lists have exactly the same values.
5917 */
5918 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005919list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 list_T *l1;
5921 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005922 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005923 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005924{
Bram Moolenaar33570922005-01-25 22:26:29 +00005925 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005926
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005927 if (l1 == NULL || l2 == NULL)
5928 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005929 if (l1 == l2)
5930 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005931 if (list_len(l1) != list_len(l2))
5932 return FALSE;
5933
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005934 for (item1 = l1->lv_first, item2 = l2->lv_first;
5935 item1 != NULL && item2 != NULL;
5936 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005937 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005938 return FALSE;
5939 return item1 == NULL && item2 == NULL;
5940}
5941
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02005942#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5943 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005944/*
5945 * Return the dictitem that an entry in a hashtable points to.
5946 */
5947 dictitem_T *
5948dict_lookup(hi)
5949 hashitem_T *hi;
5950{
5951 return HI2DI(hi);
5952}
5953#endif
5954
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005955/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005956 * Return TRUE when two dictionaries have exactly the same key/values.
5957 */
5958 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005959dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 dict_T *d1;
5961 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005962 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005963 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005964{
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 hashitem_T *hi;
5966 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005967 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005968
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00005969 if (d1 == NULL || d2 == NULL)
5970 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005971 if (d1 == d2)
5972 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005973 if (dict_len(d1) != dict_len(d2))
5974 return FALSE;
5975
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005976 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005978 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005979 if (!HASHITEM_EMPTY(hi))
5980 {
5981 item2 = dict_find(d2, hi->hi_key, -1);
5982 if (item2 == NULL)
5983 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005984 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005985 return FALSE;
5986 --todo;
5987 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005988 }
5989 return TRUE;
5990}
5991
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005992static int tv_equal_recurse_limit;
5993
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005994/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005995 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005996 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005997 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005998 */
5999 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006000tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 typval_T *tv1;
6002 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006003 int ic; /* ignore case */
6004 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005{
6006 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006007 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006008 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006009 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006010
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006011 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006012 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006013
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006014 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006015 * recursiveness to a limit. We guess they are equal then.
6016 * A fixed limit has the problem of still taking an awful long time.
6017 * Reduce the limit every time running into it. That should work fine for
6018 * deeply linked structures that are not recursively linked and catch
6019 * recursiveness quickly. */
6020 if (!recursive)
6021 tv_equal_recurse_limit = 1000;
6022 if (recursive_cnt >= tv_equal_recurse_limit)
6023 {
6024 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006025 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006026 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006027
6028 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006029 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006030 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006031 ++recursive_cnt;
6032 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6033 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006034 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006035
6036 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006037 ++recursive_cnt;
6038 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6039 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006040 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006041
6042 case VAR_FUNC:
6043 return (tv1->vval.v_string != NULL
6044 && tv2->vval.v_string != NULL
6045 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6046
6047 case VAR_NUMBER:
6048 return tv1->vval.v_number == tv2->vval.v_number;
6049
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006050#ifdef FEAT_FLOAT
6051 case VAR_FLOAT:
6052 return tv1->vval.v_float == tv2->vval.v_float;
6053#endif
6054
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006055 case VAR_STRING:
6056 s1 = get_tv_string_buf(tv1, buf1);
6057 s2 = get_tv_string_buf(tv2, buf2);
6058 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006059 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006060
6061 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006062 return TRUE;
6063}
6064
6065/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 * Locate item with index "n" in list "l" and return it.
6067 * A negative index is counted from the end; -1 is the last item.
6068 * Returns NULL when "n" is out of range.
6069 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006070 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 long n;
6074{
Bram Moolenaar33570922005-01-25 22:26:29 +00006075 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 long idx;
6077
6078 if (l == NULL)
6079 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006080
6081 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006083 n = l->lv_len + n;
6084
6085 /* Check for index out of range. */
6086 if (n < 0 || n >= l->lv_len)
6087 return NULL;
6088
6089 /* When there is a cached index may start search from there. */
6090 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006092 if (n < l->lv_idx / 2)
6093 {
6094 /* closest to the start of the list */
6095 item = l->lv_first;
6096 idx = 0;
6097 }
6098 else if (n > (l->lv_idx + l->lv_len) / 2)
6099 {
6100 /* closest to the end of the list */
6101 item = l->lv_last;
6102 idx = l->lv_len - 1;
6103 }
6104 else
6105 {
6106 /* closest to the cached index */
6107 item = l->lv_idx_item;
6108 idx = l->lv_idx;
6109 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006110 }
6111 else
6112 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006113 if (n < l->lv_len / 2)
6114 {
6115 /* closest to the start of the list */
6116 item = l->lv_first;
6117 idx = 0;
6118 }
6119 else
6120 {
6121 /* closest to the end of the list */
6122 item = l->lv_last;
6123 idx = l->lv_len - 1;
6124 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006125 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006126
6127 while (n > idx)
6128 {
6129 /* search forward */
6130 item = item->li_next;
6131 ++idx;
6132 }
6133 while (n < idx)
6134 {
6135 /* search backward */
6136 item = item->li_prev;
6137 --idx;
6138 }
6139
6140 /* cache the used index */
6141 l->lv_idx = idx;
6142 l->lv_idx_item = item;
6143
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006144 return item;
6145}
6146
6147/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006148 * Get list item "l[idx]" as a number.
6149 */
6150 static long
6151list_find_nr(l, idx, errorp)
6152 list_T *l;
6153 long idx;
6154 int *errorp; /* set to TRUE when something wrong */
6155{
6156 listitem_T *li;
6157
6158 li = list_find(l, idx);
6159 if (li == NULL)
6160 {
6161 if (errorp != NULL)
6162 *errorp = TRUE;
6163 return -1L;
6164 }
6165 return get_tv_number_chk(&li->li_tv, errorp);
6166}
6167
6168/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006169 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6170 */
6171 char_u *
6172list_find_str(l, idx)
6173 list_T *l;
6174 long idx;
6175{
6176 listitem_T *li;
6177
6178 li = list_find(l, idx - 1);
6179 if (li == NULL)
6180 {
6181 EMSGN(_(e_listidx), idx);
6182 return NULL;
6183 }
6184 return get_tv_string(&li->li_tv);
6185}
6186
6187/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006188 * Locate "item" list "l" and return its index.
6189 * Returns -1 when "item" is not in the list.
6190 */
6191 static long
6192list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 list_T *l;
6194 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006195{
6196 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006197 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006198
6199 if (l == NULL)
6200 return -1;
6201 idx = 0;
6202 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6203 ++idx;
6204 if (li == NULL)
6205 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006206 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006207}
6208
6209/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006210 * Append item "item" to the end of list "l".
6211 */
6212 static void
6213list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006214 list_T *l;
6215 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006216{
6217 if (l->lv_last == NULL)
6218 {
6219 /* empty list */
6220 l->lv_first = item;
6221 l->lv_last = item;
6222 item->li_prev = NULL;
6223 }
6224 else
6225 {
6226 l->lv_last->li_next = item;
6227 item->li_prev = l->lv_last;
6228 l->lv_last = item;
6229 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006230 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006231 item->li_next = NULL;
6232}
6233
6234/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006235 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006236 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006237 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006238 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006239list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006240 list_T *l;
6241 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006242{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006243 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006244
Bram Moolenaar05159a02005-02-26 23:04:13 +00006245 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006246 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006247 copy_tv(tv, &li->li_tv);
6248 list_append(l, li);
6249 return OK;
6250}
6251
6252/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006253 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006254 * Return FAIL when out of memory.
6255 */
6256 int
6257list_append_dict(list, dict)
6258 list_T *list;
6259 dict_T *dict;
6260{
6261 listitem_T *li = listitem_alloc();
6262
6263 if (li == NULL)
6264 return FAIL;
6265 li->li_tv.v_type = VAR_DICT;
6266 li->li_tv.v_lock = 0;
6267 li->li_tv.vval.v_dict = dict;
6268 list_append(list, li);
6269 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006270 return OK;
6271}
6272
6273/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006274 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006275 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006276 * Returns FAIL when out of memory.
6277 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006278 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006279list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006280 list_T *l;
6281 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006282 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006283{
6284 listitem_T *li = listitem_alloc();
6285
6286 if (li == NULL)
6287 return FAIL;
6288 list_append(l, li);
6289 li->li_tv.v_type = VAR_STRING;
6290 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006291 if (str == NULL)
6292 li->li_tv.vval.v_string = NULL;
6293 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006294 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006295 return FAIL;
6296 return OK;
6297}
6298
6299/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006300 * Append "n" to list "l".
6301 * Returns FAIL when out of memory.
6302 */
6303 static int
6304list_append_number(l, n)
6305 list_T *l;
6306 varnumber_T n;
6307{
6308 listitem_T *li;
6309
6310 li = listitem_alloc();
6311 if (li == NULL)
6312 return FAIL;
6313 li->li_tv.v_type = VAR_NUMBER;
6314 li->li_tv.v_lock = 0;
6315 li->li_tv.vval.v_number = n;
6316 list_append(l, li);
6317 return OK;
6318}
6319
6320/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006322 * If "item" is NULL append at the end.
6323 * Return FAIL when out of memory.
6324 */
6325 static int
6326list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006327 list_T *l;
6328 typval_T *tv;
6329 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006330{
Bram Moolenaar33570922005-01-25 22:26:29 +00006331 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332
6333 if (ni == NULL)
6334 return FAIL;
6335 copy_tv(tv, &ni->li_tv);
6336 if (item == NULL)
6337 /* Append new item at end of list. */
6338 list_append(l, ni);
6339 else
6340 {
6341 /* Insert new item before existing item. */
6342 ni->li_prev = item->li_prev;
6343 ni->li_next = item;
6344 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006345 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006346 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006347 ++l->lv_idx;
6348 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006349 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006350 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006352 l->lv_idx_item = NULL;
6353 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006354 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006355 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006356 }
6357 return OK;
6358}
6359
6360/*
6361 * Extend "l1" with "l2".
6362 * If "bef" is NULL append at the end, otherwise insert before this item.
6363 * Returns FAIL when out of memory.
6364 */
6365 static int
6366list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006367 list_T *l1;
6368 list_T *l2;
6369 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006370{
Bram Moolenaar33570922005-01-25 22:26:29 +00006371 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006372 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006373
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006374 /* We also quit the loop when we have inserted the original item count of
6375 * the list, avoid a hang when we extend a list with itself. */
6376 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006377 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6378 return FAIL;
6379 return OK;
6380}
6381
6382/*
6383 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6384 * Return FAIL when out of memory.
6385 */
6386 static int
6387list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 list_T *l1;
6389 list_T *l2;
6390 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006391{
Bram Moolenaar33570922005-01-25 22:26:29 +00006392 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006393
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006394 if (l1 == NULL || l2 == NULL)
6395 return FAIL;
6396
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006397 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006398 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006399 if (l == NULL)
6400 return FAIL;
6401 tv->v_type = VAR_LIST;
6402 tv->vval.v_list = l;
6403
6404 /* append all items from the second list */
6405 return list_extend(l, l2, NULL);
6406}
6407
6408/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006409 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006410 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006411 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 * Returns NULL when out of memory.
6413 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006415list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006417 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006418 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006419{
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 list_T *copy;
6421 listitem_T *item;
6422 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423
6424 if (orig == NULL)
6425 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426
6427 copy = list_alloc();
6428 if (copy != NULL)
6429 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006430 if (copyID != 0)
6431 {
6432 /* Do this before adding the items, because one of the items may
6433 * refer back to this list. */
6434 orig->lv_copyID = copyID;
6435 orig->lv_copylist = copy;
6436 }
6437 for (item = orig->lv_first; item != NULL && !got_int;
6438 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439 {
6440 ni = listitem_alloc();
6441 if (ni == NULL)
6442 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006443 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006444 {
6445 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6446 {
6447 vim_free(ni);
6448 break;
6449 }
6450 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006452 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453 list_append(copy, ni);
6454 }
6455 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006456 if (item != NULL)
6457 {
6458 list_unref(copy);
6459 copy = NULL;
6460 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461 }
6462
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463 return copy;
6464}
6465
6466/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006467 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006468 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006470 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006471list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 list_T *l;
6473 listitem_T *item;
6474 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006475{
Bram Moolenaar33570922005-01-25 22:26:29 +00006476 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006477
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478 /* notify watchers */
6479 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006481 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482 list_fix_watch(l, ip);
6483 if (ip == item2)
6484 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006485 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006486
6487 if (item2->li_next == NULL)
6488 l->lv_last = item->li_prev;
6489 else
6490 item2->li_next->li_prev = item->li_prev;
6491 if (item->li_prev == NULL)
6492 l->lv_first = item2->li_next;
6493 else
6494 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006495 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496}
6497
6498/*
6499 * Return an allocated string with the string representation of a list.
6500 * May return NULL.
6501 */
6502 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006503list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006504 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006505 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506{
6507 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508
6509 if (tv->vval.v_list == NULL)
6510 return NULL;
6511 ga_init2(&ga, (int)sizeof(char), 80);
6512 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006513 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006514 {
6515 vim_free(ga.ga_data);
6516 return NULL;
6517 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 ga_append(&ga, ']');
6519 ga_append(&ga, NUL);
6520 return (char_u *)ga.ga_data;
6521}
6522
6523/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006524 * Join list "l" into a string in "*gap", using separator "sep".
6525 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006526 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006527 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006528 static int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006529list_join(gap, l, sep, echo, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006530 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006532 char_u *sep;
6533 int echo;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006534 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006535{
6536 int first = TRUE;
6537 char_u *tofree;
6538 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006540 char_u *s;
6541
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006542 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006543 {
6544 if (first)
6545 first = FALSE;
6546 else
6547 ga_concat(gap, sep);
6548
6549 if (echo)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006550 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006551 else
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006552 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006553 if (s != NULL)
6554 ga_concat(gap, s);
6555 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006556 if (s == NULL)
6557 return FAIL;
Bram Moolenaarf68f6562010-01-19 12:48:05 +01006558 line_breakcheck();
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006559 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006561}
6562
6563/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006564 * Garbage collection for lists and dictionaries.
6565 *
6566 * We use reference counts to be able to free most items right away when they
6567 * are no longer used. But for composite items it's possible that it becomes
6568 * unused while the reference count is > 0: When there is a recursive
6569 * reference. Example:
6570 * :let l = [1, 2, 3]
6571 * :let d = {9: l}
6572 * :let l[1] = d
6573 *
6574 * Since this is quite unusual we handle this with garbage collection: every
6575 * once in a while find out which lists and dicts are not referenced from any
6576 * variable.
6577 *
6578 * Here is a good reference text about garbage collection (refers to Python
6579 * but it applies to all reference-counting mechanisms):
6580 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006581 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006582
6583/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006584 * Do garbage collection for lists and dicts.
6585 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006586 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006587 int
6588garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006589{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006590 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006591 buf_T *buf;
6592 win_T *wp;
6593 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006594 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006595 int did_free;
6596 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006597#ifdef FEAT_WINDOWS
6598 tabpage_T *tp;
6599#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006600
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006601 /* Only do this once. */
6602 want_garbage_collect = FALSE;
6603 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006604 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006605
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006606 /* We advance by two because we add one for items referenced through
6607 * previous_funccal. */
6608 current_copyID += COPYID_INC;
6609 copyID = current_copyID;
6610
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006611 /*
6612 * 1. Go through all accessible variables and mark all lists and dicts
6613 * with copyID.
6614 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006615
6616 /* Don't free variables in the previous_funccal list unless they are only
6617 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006618 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006619 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6620 {
6621 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6622 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6623 }
6624
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006625 /* script-local variables */
6626 for (i = 1; i <= ga_scripts.ga_len; ++i)
6627 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6628
6629 /* buffer-local variables */
6630 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6631 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
6632
6633 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006634 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006635 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
6636
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006637#ifdef FEAT_WINDOWS
6638 /* tabpage-local variables */
6639 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6640 set_ref_in_ht(&tp->tp_vars.dv_hashtab, copyID);
6641#endif
6642
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006643 /* global variables */
6644 set_ref_in_ht(&globvarht, copyID);
6645
6646 /* function-local variables */
6647 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6648 {
6649 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6650 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6651 }
6652
Bram Moolenaard812df62008-11-09 12:46:09 +00006653 /* v: vars */
6654 set_ref_in_ht(&vimvarht, copyID);
6655
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006656 /*
6657 * 2. Free lists and dictionaries that are not referenced.
6658 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006659 did_free = free_unref_items(copyID);
6660
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006661 /*
6662 * 3. Check if any funccal can be freed now.
6663 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006664 for (pfc = &previous_funccal; *pfc != NULL; )
6665 {
6666 if (can_free_funccal(*pfc, copyID))
6667 {
6668 fc = *pfc;
6669 *pfc = fc->caller;
6670 free_funccal(fc, TRUE);
6671 did_free = TRUE;
6672 did_free_funccal = TRUE;
6673 }
6674 else
6675 pfc = &(*pfc)->caller;
6676 }
6677 if (did_free_funccal)
6678 /* When a funccal was freed some more items might be garbage
6679 * collected, so run again. */
6680 (void)garbage_collect();
6681
6682 return did_free;
6683}
6684
6685/*
6686 * Free lists and dictionaries that are no longer referenced.
6687 */
6688 static int
6689free_unref_items(copyID)
6690 int copyID;
6691{
6692 dict_T *dd;
6693 list_T *ll;
6694 int did_free = FALSE;
6695
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006696 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006697 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006698 */
6699 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006700 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006701 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006702 /* Free the Dictionary and ordinary items it contains, but don't
6703 * recurse into Lists and Dictionaries, they will be in the list
6704 * of dicts or list of lists. */
6705 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006706 did_free = TRUE;
6707
6708 /* restart, next dict may also have been freed */
6709 dd = first_dict;
6710 }
6711 else
6712 dd = dd->dv_used_next;
6713
6714 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006715 * Go through the list of lists and free items without the copyID.
6716 * But don't free a list that has a watcher (used in a for loop), these
6717 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006718 */
6719 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006720 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6721 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006722 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006723 /* Free the List and ordinary items it contains, but don't recurse
6724 * into Lists and Dictionaries, they will be in the list of dicts
6725 * or list of lists. */
6726 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006727 did_free = TRUE;
6728
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006729 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006730 ll = first_list;
6731 }
6732 else
6733 ll = ll->lv_used_next;
6734
6735 return did_free;
6736}
6737
6738/*
6739 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6740 */
6741 static void
6742set_ref_in_ht(ht, copyID)
6743 hashtab_T *ht;
6744 int copyID;
6745{
6746 int todo;
6747 hashitem_T *hi;
6748
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006749 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006750 for (hi = ht->ht_array; todo > 0; ++hi)
6751 if (!HASHITEM_EMPTY(hi))
6752 {
6753 --todo;
6754 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6755 }
6756}
6757
6758/*
6759 * Mark all lists and dicts referenced through list "l" with "copyID".
6760 */
6761 static void
6762set_ref_in_list(l, copyID)
6763 list_T *l;
6764 int copyID;
6765{
6766 listitem_T *li;
6767
6768 for (li = l->lv_first; li != NULL; li = li->li_next)
6769 set_ref_in_item(&li->li_tv, copyID);
6770}
6771
6772/*
6773 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6774 */
6775 static void
6776set_ref_in_item(tv, copyID)
6777 typval_T *tv;
6778 int copyID;
6779{
6780 dict_T *dd;
6781 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006782
6783 switch (tv->v_type)
6784 {
6785 case VAR_DICT:
6786 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006787 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006788 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006789 /* Didn't see this dict yet. */
6790 dd->dv_copyID = copyID;
6791 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006792 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006793 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006794
6795 case VAR_LIST:
6796 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006797 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006798 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006799 /* Didn't see this list yet. */
6800 ll->lv_copyID = copyID;
6801 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006802 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006803 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006804 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006805 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006806}
6807
6808/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006809 * Allocate an empty header for a dictionary.
6810 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006811 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006812dict_alloc()
6813{
Bram Moolenaar33570922005-01-25 22:26:29 +00006814 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006815
Bram Moolenaar33570922005-01-25 22:26:29 +00006816 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006817 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006818 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006819 /* Add the list to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 if (first_dict != NULL)
6821 first_dict->dv_used_prev = d;
6822 d->dv_used_next = first_dict;
6823 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006824 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006825
Bram Moolenaar33570922005-01-25 22:26:29 +00006826 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006827 d->dv_lock = 0;
6828 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006829 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006830 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006831 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006832}
6833
6834/*
Bram Moolenaara800b422010-06-27 01:15:55 +02006835 * Allocate an empty dict for a return value.
6836 * Returns OK or FAIL.
6837 */
6838 static int
6839rettv_dict_alloc(rettv)
6840 typval_T *rettv;
6841{
6842 dict_T *d = dict_alloc();
6843
6844 if (d == NULL)
6845 return FAIL;
6846
6847 rettv->vval.v_dict = d;
6848 rettv->v_type = VAR_DICT;
6849 ++d->dv_refcount;
6850 return OK;
6851}
6852
6853
6854/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006855 * Unreference a Dictionary: decrement the reference count and free it when it
6856 * becomes zero.
6857 */
6858 static void
6859dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006860 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006861{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006862 if (d != NULL && --d->dv_refcount <= 0)
6863 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006864}
6865
6866/*
6867 * Free a Dictionary, including all items it contains.
6868 * Ignores the reference count.
6869 */
6870 static void
Bram Moolenaar685295c2006-10-15 20:37:38 +00006871dict_free(d, recurse)
6872 dict_T *d;
6873 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006874{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006875 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006876 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006877 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006878
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006879 /* Remove the dict from the list of dicts for garbage collection. */
6880 if (d->dv_used_prev == NULL)
6881 first_dict = d->dv_used_next;
6882 else
6883 d->dv_used_prev->dv_used_next = d->dv_used_next;
6884 if (d->dv_used_next != NULL)
6885 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6886
6887 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006888 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006889 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006890 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006892 if (!HASHITEM_EMPTY(hi))
6893 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006894 /* Remove the item before deleting it, just in case there is
6895 * something recursive causing trouble. */
6896 di = HI2DI(hi);
6897 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00006898 if (recurse || (di->di_tv.v_type != VAR_LIST
6899 && di->di_tv.v_type != VAR_DICT))
6900 clear_tv(&di->di_tv);
6901 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006902 --todo;
6903 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006904 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006905 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006906 vim_free(d);
6907}
6908
6909/*
6910 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006911 * The "key" is copied to the new item.
6912 * Note that the value of the item "di_tv" still needs to be initialized!
6913 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006914 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006915 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006916dictitem_alloc(key)
6917 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006918{
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006920
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006921 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006922 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006923 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006924 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006925 di->di_flags = 0;
6926 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006927 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006928}
6929
6930/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006931 * Make a copy of a Dictionary item.
6932 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006933 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006934dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006935 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006936{
Bram Moolenaar33570922005-01-25 22:26:29 +00006937 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006938
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006939 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
6940 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006941 if (di != NULL)
6942 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006943 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006944 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006945 copy_tv(&org->di_tv, &di->di_tv);
6946 }
6947 return di;
6948}
6949
6950/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006951 * Remove item "item" from Dictionary "dict" and free it.
6952 */
6953 static void
6954dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006955 dict_T *dict;
6956 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006957{
Bram Moolenaar33570922005-01-25 22:26:29 +00006958 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006959
Bram Moolenaar33570922005-01-25 22:26:29 +00006960 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006961 if (HASHITEM_EMPTY(hi))
6962 EMSG2(_(e_intern2), "dictitem_remove()");
6963 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006964 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006965 dictitem_free(item);
6966}
6967
6968/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006969 * Free a dict item. Also clears the value.
6970 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006971 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00006972dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006973 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006974{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006975 clear_tv(&item->di_tv);
6976 vim_free(item);
6977}
6978
6979/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006980 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6981 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006982 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006983 * Returns NULL when out of memory.
6984 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006986dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006988 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006989 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006990{
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 dict_T *copy;
6992 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006993 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006994 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006995
6996 if (orig == NULL)
6997 return NULL;
6998
6999 copy = dict_alloc();
7000 if (copy != NULL)
7001 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007002 if (copyID != 0)
7003 {
7004 orig->dv_copyID = copyID;
7005 orig->dv_copydict = copy;
7006 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007007 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007008 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007009 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007010 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007011 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007012 --todo;
7013
7014 di = dictitem_alloc(hi->hi_key);
7015 if (di == NULL)
7016 break;
7017 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007018 {
7019 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7020 copyID) == FAIL)
7021 {
7022 vim_free(di);
7023 break;
7024 }
7025 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007026 else
7027 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7028 if (dict_add(copy, di) == FAIL)
7029 {
7030 dictitem_free(di);
7031 break;
7032 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007034 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007035
Bram Moolenaare9a41262005-01-15 22:18:47 +00007036 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007037 if (todo > 0)
7038 {
7039 dict_unref(copy);
7040 copy = NULL;
7041 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007042 }
7043
7044 return copy;
7045}
7046
7047/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007048 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007049 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007051 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007052dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007053 dict_T *d;
7054 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007055{
Bram Moolenaar33570922005-01-25 22:26:29 +00007056 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007057}
7058
Bram Moolenaar8c711452005-01-14 21:53:12 +00007059/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007060 * Add a number or string entry to dictionary "d".
7061 * When "str" is NULL use number "nr", otherwise use "str".
7062 * Returns FAIL when out of memory and when key already exists.
7063 */
7064 int
7065dict_add_nr_str(d, key, nr, str)
7066 dict_T *d;
7067 char *key;
7068 long nr;
7069 char_u *str;
7070{
7071 dictitem_T *item;
7072
7073 item = dictitem_alloc((char_u *)key);
7074 if (item == NULL)
7075 return FAIL;
7076 item->di_tv.v_lock = 0;
7077 if (str == NULL)
7078 {
7079 item->di_tv.v_type = VAR_NUMBER;
7080 item->di_tv.vval.v_number = nr;
7081 }
7082 else
7083 {
7084 item->di_tv.v_type = VAR_STRING;
7085 item->di_tv.vval.v_string = vim_strsave(str);
7086 }
7087 if (dict_add(d, item) == FAIL)
7088 {
7089 dictitem_free(item);
7090 return FAIL;
7091 }
7092 return OK;
7093}
7094
7095/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007096 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007097 * Returns FAIL when out of memory and when key already exists.
7098 */
7099 int
7100dict_add_list(d, key, list)
7101 dict_T *d;
7102 char *key;
7103 list_T *list;
7104{
7105 dictitem_T *item;
7106
7107 item = dictitem_alloc((char_u *)key);
7108 if (item == NULL)
7109 return FAIL;
7110 item->di_tv.v_lock = 0;
7111 item->di_tv.v_type = VAR_LIST;
7112 item->di_tv.vval.v_list = list;
7113 if (dict_add(d, item) == FAIL)
7114 {
7115 dictitem_free(item);
7116 return FAIL;
7117 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007118 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007119 return OK;
7120}
7121
7122/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007123 * Get the number of items in a Dictionary.
7124 */
7125 static long
7126dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007128{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007129 if (d == NULL)
7130 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007131 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007132}
7133
7134/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007135 * Find item "key[len]" in Dictionary "d".
7136 * If "len" is negative use strlen(key).
7137 * Returns NULL when not found.
7138 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007139 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007140dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007141 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007142 char_u *key;
7143 int len;
7144{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007145#define AKEYLEN 200
7146 char_u buf[AKEYLEN];
7147 char_u *akey;
7148 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007149 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007150
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007151 if (len < 0)
7152 akey = key;
7153 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007154 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007155 tofree = akey = vim_strnsave(key, len);
7156 if (akey == NULL)
7157 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007158 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007159 else
7160 {
7161 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007162 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 akey = buf;
7164 }
7165
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007167 vim_free(tofree);
7168 if (HASHITEM_EMPTY(hi))
7169 return NULL;
7170 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007171}
7172
7173/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007174 * Get a string item from a dictionary.
7175 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007176 * Returns NULL if the entry doesn't exist or out of memory.
7177 */
7178 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007179get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007180 dict_T *d;
7181 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007182 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007183{
7184 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007185 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007186
7187 di = dict_find(d, key, -1);
7188 if (di == NULL)
7189 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007190 s = get_tv_string(&di->di_tv);
7191 if (save && s != NULL)
7192 s = vim_strsave(s);
7193 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007194}
7195
7196/*
7197 * Get a number item from a dictionary.
7198 * Returns 0 if the entry doesn't exist or out of memory.
7199 */
7200 long
7201get_dict_number(d, key)
7202 dict_T *d;
7203 char_u *key;
7204{
7205 dictitem_T *di;
7206
7207 di = dict_find(d, key, -1);
7208 if (di == NULL)
7209 return 0;
7210 return get_tv_number(&di->di_tv);
7211}
7212
7213/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007214 * Return an allocated string with the string representation of a Dictionary.
7215 * May return NULL.
7216 */
7217 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007218dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007219 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007220 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221{
7222 garray_T ga;
7223 int first = TRUE;
7224 char_u *tofree;
7225 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007231 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007232 return NULL;
7233 ga_init2(&ga, (int)sizeof(char), 80);
7234 ga_append(&ga, '{');
7235
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007236 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007237 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 --todo;
7242
7243 if (first)
7244 first = FALSE;
7245 else
7246 ga_concat(&ga, (char_u *)", ");
7247
7248 tofree = string_quote(hi->hi_key, FALSE);
7249 if (tofree != NULL)
7250 {
7251 ga_concat(&ga, tofree);
7252 vim_free(tofree);
7253 }
7254 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007255 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007256 if (s != NULL)
7257 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007258 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007259 if (s == NULL)
7260 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007261 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007262 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007263 if (todo > 0)
7264 {
7265 vim_free(ga.ga_data);
7266 return NULL;
7267 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007268
7269 ga_append(&ga, '}');
7270 ga_append(&ga, NUL);
7271 return (char_u *)ga.ga_data;
7272}
7273
7274/*
7275 * Allocate a variable for a Dictionary and fill it from "*arg".
7276 * Return OK or FAIL. Returns NOTDONE for {expr}.
7277 */
7278 static int
7279get_dict_tv(arg, rettv, evaluate)
7280 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007281 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007282 int evaluate;
7283{
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 dict_T *d = NULL;
7285 typval_T tvkey;
7286 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007287 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007288 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007290 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291
7292 /*
7293 * First check if it's not a curly-braces thing: {expr}.
7294 * Must do this without evaluating, otherwise a function may be called
7295 * twice. Unfortunately this means we need to call eval1() twice for the
7296 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 if (*start != '}')
7300 {
7301 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7302 return FAIL;
7303 if (*start == '}')
7304 return NOTDONE;
7305 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306
7307 if (evaluate)
7308 {
7309 d = dict_alloc();
7310 if (d == NULL)
7311 return FAIL;
7312 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 tvkey.v_type = VAR_UNKNOWN;
7314 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007315
7316 *arg = skipwhite(*arg + 1);
7317 while (**arg != '}' && **arg != NUL)
7318 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320 goto failret;
7321 if (**arg != ':')
7322 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007323 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007325 goto failret;
7326 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007327 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007329 key = get_tv_string_buf_chk(&tvkey, buf);
7330 if (key == NULL || *key == NUL)
7331 {
7332 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7333 if (key != NULL)
7334 EMSG(_(e_emptykey));
7335 clear_tv(&tvkey);
7336 goto failret;
7337 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007338 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339
7340 *arg = skipwhite(*arg + 1);
7341 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7342 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007343 if (evaluate)
7344 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345 goto failret;
7346 }
7347 if (evaluate)
7348 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007350 if (item != NULL)
7351 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007352 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007354 clear_tv(&tv);
7355 goto failret;
7356 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 item = dictitem_alloc(key);
7358 clear_tv(&tvkey);
7359 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007362 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 if (dict_add(d, item) == FAIL)
7364 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365 }
7366 }
7367
7368 if (**arg == '}')
7369 break;
7370 if (**arg != ',')
7371 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007372 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007373 goto failret;
7374 }
7375 *arg = skipwhite(*arg + 1);
7376 }
7377
7378 if (**arg != '}')
7379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007380 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007381failret:
7382 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007383 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007384 return FAIL;
7385 }
7386
7387 *arg = skipwhite(*arg + 1);
7388 if (evaluate)
7389 {
7390 rettv->v_type = VAR_DICT;
7391 rettv->vval.v_dict = d;
7392 ++d->dv_refcount;
7393 }
7394
7395 return OK;
7396}
7397
Bram Moolenaar8c711452005-01-14 21:53:12 +00007398/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007399 * Return a string with the string representation of a variable.
7400 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007401 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007402 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007403 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007404 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007405 */
7406 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007407echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007408 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007409 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007410 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007411 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007412{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007413 static int recurse = 0;
7414 char_u *r = NULL;
7415
Bram Moolenaar33570922005-01-25 22:26:29 +00007416 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007418 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 *tofree = NULL;
7420 return NULL;
7421 }
7422 ++recurse;
7423
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007424 switch (tv->v_type)
7425 {
7426 case VAR_FUNC:
7427 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 r = tv->vval.v_string;
7429 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007430
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007431 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007432 if (tv->vval.v_list == NULL)
7433 {
7434 *tofree = NULL;
7435 r = NULL;
7436 }
7437 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7438 {
7439 *tofree = NULL;
7440 r = (char_u *)"[...]";
7441 }
7442 else
7443 {
7444 tv->vval.v_list->lv_copyID = copyID;
7445 *tofree = list2string(tv, copyID);
7446 r = *tofree;
7447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007449
Bram Moolenaar8c711452005-01-14 21:53:12 +00007450 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007451 if (tv->vval.v_dict == NULL)
7452 {
7453 *tofree = NULL;
7454 r = NULL;
7455 }
7456 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7457 {
7458 *tofree = NULL;
7459 r = (char_u *)"{...}";
7460 }
7461 else
7462 {
7463 tv->vval.v_dict->dv_copyID = copyID;
7464 *tofree = dict2string(tv, copyID);
7465 r = *tofree;
7466 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007467 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007468
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469 case VAR_STRING:
7470 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007471 *tofree = NULL;
7472 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007473 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007474
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007475#ifdef FEAT_FLOAT
7476 case VAR_FLOAT:
7477 *tofree = NULL;
7478 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7479 r = numbuf;
7480 break;
7481#endif
7482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007483 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007484 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007485 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007486 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007487
7488 --recurse;
7489 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007490}
7491
7492/*
7493 * Return a string with the string representation of a variable.
7494 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7495 * "numbuf" is used for a number.
7496 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007497 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007498 */
7499 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007500tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007502 char_u **tofree;
7503 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007504 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007505{
7506 switch (tv->v_type)
7507 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007508 case VAR_FUNC:
7509 *tofree = string_quote(tv->vval.v_string, TRUE);
7510 return *tofree;
7511 case VAR_STRING:
7512 *tofree = string_quote(tv->vval.v_string, FALSE);
7513 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007514#ifdef FEAT_FLOAT
7515 case VAR_FLOAT:
7516 *tofree = NULL;
7517 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7518 return numbuf;
7519#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007520 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007521 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007523 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007524 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007525 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007526 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007527 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007528}
7529
7530/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007531 * Return string "str" in ' quotes, doubling ' characters.
7532 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007534 */
7535 static char_u *
7536string_quote(str, function)
7537 char_u *str;
7538 int function;
7539{
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007541 char_u *p, *r, *s;
7542
Bram Moolenaar33570922005-01-25 22:26:29 +00007543 len = (function ? 13 : 3);
7544 if (str != NULL)
7545 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007546 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007547 for (p = str; *p != NUL; mb_ptr_adv(p))
7548 if (*p == '\'')
7549 ++len;
7550 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007551 s = r = alloc(len);
7552 if (r != NULL)
7553 {
7554 if (function)
7555 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007557 r += 10;
7558 }
7559 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007560 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 if (str != NULL)
7562 for (p = str; *p != NUL; )
7563 {
7564 if (*p == '\'')
7565 *r++ = '\'';
7566 MB_COPY_CHAR(p, r);
7567 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007569 if (function)
7570 *r++ = ')';
7571 *r++ = NUL;
7572 }
7573 return s;
7574}
7575
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007576#ifdef FEAT_FLOAT
7577/*
7578 * Convert the string "text" to a floating point number.
7579 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7580 * this always uses a decimal point.
7581 * Returns the length of the text that was consumed.
7582 */
7583 static int
7584string2float(text, value)
7585 char_u *text;
7586 float_T *value; /* result stored here */
7587{
7588 char *s = (char *)text;
7589 float_T f;
7590
7591 f = strtod(s, &s);
7592 *value = f;
7593 return (int)((char_u *)s - text);
7594}
7595#endif
7596
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 * Get the value of an environment variable.
7599 * "arg" is pointing to the '$'. It is advanced to after the name.
7600 * If the environment variable was not set, silently assume it is empty.
7601 * Always return OK.
7602 */
7603 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007604get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 int evaluate;
7608{
7609 char_u *string = NULL;
7610 int len;
7611 int cc;
7612 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007613 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614
7615 ++*arg;
7616 name = *arg;
7617 len = get_env_len(arg);
7618 if (evaluate)
7619 {
7620 if (len != 0)
7621 {
7622 cc = name[len];
7623 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007624 /* first try vim_getenv(), fast for normal environment vars */
7625 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007627 {
7628 if (!mustfree)
7629 string = vim_strsave(string);
7630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 else
7632 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007633 if (mustfree)
7634 vim_free(string);
7635
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 /* next try expanding things like $VIM and ${HOME} */
7637 string = expand_env_save(name - 1);
7638 if (string != NULL && *string == '$')
7639 {
7640 vim_free(string);
7641 string = NULL;
7642 }
7643 }
7644 name[len] = cc;
7645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007646 rettv->v_type = VAR_STRING;
7647 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649
7650 return OK;
7651}
7652
7653/*
7654 * Array with names and number of arguments of all internal functions
7655 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7656 */
7657static struct fst
7658{
7659 char *f_name; /* function name */
7660 char f_min_argc; /* minimal number of arguments */
7661 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007662 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007663 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664} functions[] =
7665{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007666#ifdef FEAT_FLOAT
7667 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007668 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007669#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007670 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 {"append", 2, 2, f_append},
7672 {"argc", 0, 0, f_argc},
7673 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007674 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007675#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007676 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007677 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007678 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007681 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 {"bufexists", 1, 1, f_bufexists},
7683 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7684 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7685 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7686 {"buflisted", 1, 1, f_buflisted},
7687 {"bufloaded", 1, 1, f_bufloaded},
7688 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007689 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 {"bufwinnr", 1, 1, f_bufwinnr},
7691 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007692 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007693 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007694#ifdef FEAT_FLOAT
7695 {"ceil", 1, 1, f_ceil},
7696#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007697 {"changenr", 0, 0, f_changenr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 {"char2nr", 1, 1, f_char2nr},
7699 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007700 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007702#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007703 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007704 {"complete_add", 1, 1, f_complete_add},
7705 {"complete_check", 0, 0, f_complete_check},
7706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007708 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007709#ifdef FEAT_FLOAT
7710 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007711 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007712#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007715 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007716 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 {"delete", 1, 1, f_delete},
7718 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007719 {"diff_filler", 1, 1, f_diff_filler},
7720 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007721 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 {"eventhandler", 0, 0, f_eventhandler},
7725 {"executable", 1, 1, f_executable},
7726 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007727#ifdef FEAT_FLOAT
7728 {"exp", 1, 1, f_exp},
7729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007731 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007732 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7734 {"filereadable", 1, 1, f_filereadable},
7735 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007736 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007737 {"finddir", 1, 3, f_finddir},
7738 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007739#ifdef FEAT_FLOAT
7740 {"float2nr", 1, 1, f_float2nr},
7741 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007742 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007743#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007744 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 {"fnamemodify", 2, 2, f_fnamemodify},
7746 {"foldclosed", 1, 1, f_foldclosed},
7747 {"foldclosedend", 1, 1, f_foldclosedend},
7748 {"foldlevel", 1, 1, f_foldlevel},
7749 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007750 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007752 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007753 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007754 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007755 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {"getbufvar", 2, 2, f_getbufvar},
7757 {"getchar", 0, 1, f_getchar},
7758 {"getcharmod", 0, 0, f_getcharmod},
7759 {"getcmdline", 0, 0, f_getcmdline},
7760 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007761 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007763 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007764 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {"getfsize", 1, 1, f_getfsize},
7766 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007767 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007768 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007769 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007770 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007771 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007772 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007773 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007774 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007776 {"gettabvar", 2, 2, f_gettabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007777 {"gettabwinvar", 3, 3, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 {"getwinposx", 0, 0, f_getwinposx},
7779 {"getwinposy", 0, 0, f_getwinposy},
7780 {"getwinvar", 2, 2, f_getwinvar},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007781 {"glob", 1, 2, f_glob},
7782 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007784 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007785 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007786 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7788 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7789 {"histadd", 2, 2, f_histadd},
7790 {"histdel", 1, 2, f_histdel},
7791 {"histget", 1, 2, f_histget},
7792 {"histnr", 1, 1, f_histnr},
7793 {"hlID", 1, 1, f_hlID},
7794 {"hlexists", 1, 1, f_hlexists},
7795 {"hostname", 0, 0, f_hostname},
7796 {"iconv", 3, 3, f_iconv},
7797 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007798 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007799 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007801 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 {"inputrestore", 0, 0, f_inputrestore},
7803 {"inputsave", 0, 0, f_inputsave},
7804 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007805 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007807 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007808 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007809 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007810 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007812 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 {"libcall", 3, 3, f_libcall},
7814 {"libcallnr", 3, 3, f_libcallnr},
7815 {"line", 1, 1, f_line},
7816 {"line2byte", 1, 1, f_line2byte},
7817 {"lispindent", 1, 1, f_lispindent},
7818 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007819#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007820 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007821 {"log10", 1, 1, f_log10},
7822#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007823 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007824 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007825 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007826 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007827 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007828 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007829 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007830 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007831 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007832 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007833 {"max", 1, 1, f_max},
7834 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007835#ifdef vim_mkdir
7836 {"mkdir", 1, 3, f_mkdir},
7837#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007839#ifdef FEAT_MZSCHEME
7840 {"mzeval", 1, 1, f_mzeval},
7841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"nextnonblank", 1, 1, f_nextnonblank},
7843 {"nr2char", 1, 1, f_nr2char},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007844 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#ifdef FEAT_FLOAT
7846 {"pow", 2, 2, f_pow},
7847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007849 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007850 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007851 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007852 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00007853 {"reltime", 0, 2, f_reltime},
7854 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"remote_expr", 2, 3, f_remote_expr},
7856 {"remote_foreground", 1, 1, f_remote_foreground},
7857 {"remote_peek", 1, 2, f_remote_peek},
7858 {"remote_read", 1, 1, f_remote_read},
7859 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007860 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007862 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007864 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007865#ifdef FEAT_FLOAT
7866 {"round", 1, 1, f_round},
7867#endif
Bram Moolenaar76929292008-01-06 19:07:36 +00007868 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00007869 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00007870 {"searchpair", 3, 7, f_searchpair},
7871 {"searchpairpos", 3, 7, f_searchpairpos},
7872 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"server2client", 2, 2, f_server2client},
7874 {"serverlist", 0, 0, f_serverlist},
7875 {"setbufvar", 3, 3, f_setbufvar},
7876 {"setcmdpos", 1, 1, f_setcmdpos},
7877 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00007878 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007879 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007880 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00007881 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02007883 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00007884 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007886 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007888#ifdef FEAT_FLOAT
7889 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007890 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007891#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007892 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00007893 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00007894 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00007895 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007896 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007897#ifdef FEAT_FLOAT
7898 {"sqrt", 1, 1, f_sqrt},
7899 {"str2float", 1, 1, f_str2float},
7900#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00007901 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007902 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02007903 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904#ifdef HAVE_STRFTIME
7905 {"strftime", 1, 2, f_strftime},
7906#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00007907 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007908 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 {"strlen", 1, 1, f_strlen},
7910 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00007911 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02007913 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 {"submatch", 1, 1, f_submatch},
7915 {"substitute", 4, 4, f_substitute},
7916 {"synID", 3, 3, f_synID},
7917 {"synIDattr", 2, 3, f_synIDattr},
7918 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02007919 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00007920 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007921 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007922 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007923 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00007924 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00007925 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00007926 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007927#ifdef FEAT_FLOAT
7928 {"tan", 1, 1, f_tan},
7929 {"tanh", 1, 1, f_tanh},
7930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00007932 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"tolower", 1, 1, f_tolower},
7934 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00007935 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007936#ifdef FEAT_FLOAT
7937 {"trunc", 1, 1, f_trunc},
7938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02007940 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02007941 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007942 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 {"virtcol", 1, 1, f_virtcol},
7944 {"visualmode", 0, 1, f_visualmode},
7945 {"winbufnr", 1, 1, f_winbufnr},
7946 {"wincol", 0, 0, f_wincol},
7947 {"winheight", 1, 1, f_winheight},
7948 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007949 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00007951 {"winrestview", 1, 1, f_winrestview},
7952 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007954 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955};
7956
7957#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7958
7959/*
7960 * Function given to ExpandGeneric() to obtain the list of internal
7961 * or user defined function names.
7962 */
7963 char_u *
7964get_function_name(xp, idx)
7965 expand_T *xp;
7966 int idx;
7967{
7968 static int intidx = -1;
7969 char_u *name;
7970
7971 if (idx == 0)
7972 intidx = -1;
7973 if (intidx < 0)
7974 {
7975 name = get_user_func_name(xp, idx);
7976 if (name != NULL)
7977 return name;
7978 }
7979 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
7980 {
7981 STRCPY(IObuff, functions[intidx].f_name);
7982 STRCAT(IObuff, "(");
7983 if (functions[intidx].f_max_argc == 0)
7984 STRCAT(IObuff, ")");
7985 return IObuff;
7986 }
7987
7988 return NULL;
7989}
7990
7991/*
7992 * Function given to ExpandGeneric() to obtain the list of internal or
7993 * user defined variable or function names.
7994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 char_u *
7996get_expr_name(xp, idx)
7997 expand_T *xp;
7998 int idx;
7999{
8000 static int intidx = -1;
8001 char_u *name;
8002
8003 if (idx == 0)
8004 intidx = -1;
8005 if (intidx < 0)
8006 {
8007 name = get_function_name(xp, idx);
8008 if (name != NULL)
8009 return name;
8010 }
8011 return get_user_var_name(xp, ++intidx);
8012}
8013
8014#endif /* FEAT_CMDL_COMPL */
8015
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008016#if defined(EBCDIC) || defined(PROTO)
8017/*
8018 * Compare struct fst by function name.
8019 */
8020 static int
8021compare_func_name(s1, s2)
8022 const void *s1;
8023 const void *s2;
8024{
8025 struct fst *p1 = (struct fst *)s1;
8026 struct fst *p2 = (struct fst *)s2;
8027
8028 return STRCMP(p1->f_name, p2->f_name);
8029}
8030
8031/*
8032 * Sort the function table by function name.
8033 * The sorting of the table above is ASCII dependant.
8034 * On machines using EBCDIC we have to sort it.
8035 */
8036 static void
8037sortFunctions()
8038{
8039 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8040
8041 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8042}
8043#endif
8044
8045
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046/*
8047 * Find internal function in table above.
8048 * Return index, or -1 if not found
8049 */
8050 static int
8051find_internal_func(name)
8052 char_u *name; /* name of the function */
8053{
8054 int first = 0;
8055 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8056 int cmp;
8057 int x;
8058
8059 /*
8060 * Find the function name in the table. Binary search.
8061 */
8062 while (first <= last)
8063 {
8064 x = first + ((unsigned)(last - first) >> 1);
8065 cmp = STRCMP(name, functions[x].f_name);
8066 if (cmp < 0)
8067 last = x - 1;
8068 else if (cmp > 0)
8069 first = x + 1;
8070 else
8071 return x;
8072 }
8073 return -1;
8074}
8075
8076/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008077 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8078 * name it contains, otherwise return "name".
8079 */
8080 static char_u *
8081deref_func_name(name, lenp)
8082 char_u *name;
8083 int *lenp;
8084{
Bram Moolenaar33570922005-01-25 22:26:29 +00008085 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008086 int cc;
8087
8088 cc = name[*lenp];
8089 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008090 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008091 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008092 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008093 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008094 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008095 {
8096 *lenp = 0;
8097 return (char_u *)""; /* just in case */
8098 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008099 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008101 }
8102
8103 return name;
8104}
8105
8106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 * Allocate a variable for the result of a function.
8108 * Return OK or FAIL.
8109 */
8110 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008111get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8112 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 char_u *name; /* name of the function */
8114 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008115 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 char_u **arg; /* argument, pointing to the '(' */
8117 linenr_T firstline; /* first line of range */
8118 linenr_T lastline; /* last line of range */
8119 int *doesrange; /* return: function handled range */
8120 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008121 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122{
8123 char_u *argp;
8124 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008125 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 int argcount = 0; /* number of arguments found */
8127
8128 /*
8129 * Get the arguments.
8130 */
8131 argp = *arg;
8132 while (argcount < MAX_FUNC_ARGS)
8133 {
8134 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8135 if (*argp == ')' || *argp == ',' || *argp == NUL)
8136 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8138 {
8139 ret = FAIL;
8140 break;
8141 }
8142 ++argcount;
8143 if (*argp != ',')
8144 break;
8145 }
8146 if (*argp == ')')
8147 ++argp;
8148 else
8149 ret = FAIL;
8150
8151 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008152 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008153 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008155 {
8156 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008157 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008158 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008159 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161
8162 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008163 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164
8165 *arg = skipwhite(argp);
8166 return ret;
8167}
8168
8169
8170/*
8171 * Call a function with its resolved parameters
Bram Moolenaar280f1262006-01-30 00:14:18 +00008172 * Return OK when the function can't be called, FAIL otherwise.
8173 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 */
8175 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008176call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008177 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008178 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008180 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008182 typval_T *argvars; /* vars for arguments, must have "argcount"
8183 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 linenr_T firstline; /* first line of range */
8185 linenr_T lastline; /* last line of range */
8186 int *doesrange; /* return: function handled range */
8187 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008188 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189{
8190 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191#define ERROR_UNKNOWN 0
8192#define ERROR_TOOMANY 1
8193#define ERROR_TOOFEW 2
8194#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008195#define ERROR_DICT 4
8196#define ERROR_NONE 5
8197#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 int error = ERROR_NONE;
8199 int i;
8200 int llen;
8201 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202#define FLEN_FIXED 40
8203 char_u fname_buf[FLEN_FIXED + 1];
8204 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008205 char_u *name;
8206
8207 /* Make a copy of the name, if it comes from a funcref variable it could
8208 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008209 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008210 if (name == NULL)
8211 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212
8213 /*
8214 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8215 * Change <SNR>123_name() to K_SNR 123_name().
8216 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8217 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 llen = eval_fname_script(name);
8219 if (llen > 0)
8220 {
8221 fname_buf[0] = K_SPECIAL;
8222 fname_buf[1] = KS_EXTRA;
8223 fname_buf[2] = (int)KE_SNR;
8224 i = 3;
8225 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8226 {
8227 if (current_SID <= 0)
8228 error = ERROR_SCRIPT;
8229 else
8230 {
8231 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8232 i = (int)STRLEN(fname_buf);
8233 }
8234 }
8235 if (i + STRLEN(name + llen) < FLEN_FIXED)
8236 {
8237 STRCPY(fname_buf + i, name + llen);
8238 fname = fname_buf;
8239 }
8240 else
8241 {
8242 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8243 if (fname == NULL)
8244 error = ERROR_OTHER;
8245 else
8246 {
8247 mch_memmove(fname, fname_buf, (size_t)i);
8248 STRCPY(fname + i, name + llen);
8249 }
8250 }
8251 }
8252 else
8253 fname = name;
8254
8255 *doesrange = FALSE;
8256
8257
8258 /* execute the function if no errors detected and executing */
8259 if (evaluate && error == ERROR_NONE)
8260 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008261 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8262 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 error = ERROR_UNKNOWN;
8264
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008265 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 {
8267 /*
8268 * User defined function.
8269 */
8270 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008271
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008273 /* Trigger FuncUndefined event, may load the function. */
8274 if (fp == NULL
8275 && apply_autocmds(EVENT_FUNCUNDEFINED,
8276 fname, fname, TRUE, NULL)
8277 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008279 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 fp = find_func(fname);
8281 }
8282#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008283 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008284 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008285 {
8286 /* loaded a package, search for the function again */
8287 fp = find_func(fname);
8288 }
8289
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 if (fp != NULL)
8291 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008292 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008294 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008296 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008298 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008299 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 else
8301 {
8302 /*
8303 * Call the user function.
8304 * Save and restore search patterns, script variables and
8305 * redo buffer.
8306 */
8307 save_search_patterns();
8308 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008309 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008310 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008311 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008312 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8313 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8314 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008315 /* Function was unreferenced while being used, free it
8316 * now. */
8317 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 restoreRedobuff();
8319 restore_search_patterns();
8320 error = ERROR_NONE;
8321 }
8322 }
8323 }
8324 else
8325 {
8326 /*
8327 * Find the function name in the table, call its implementation.
8328 */
8329 i = find_internal_func(fname);
8330 if (i >= 0)
8331 {
8332 if (argcount < functions[i].f_min_argc)
8333 error = ERROR_TOOFEW;
8334 else if (argcount > functions[i].f_max_argc)
8335 error = ERROR_TOOMANY;
8336 else
8337 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008338 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 error = ERROR_NONE;
8341 }
8342 }
8343 }
8344 /*
8345 * The function call (or "FuncUndefined" autocommand sequence) might
8346 * have been aborted by an error, an interrupt, or an explicitly thrown
8347 * exception that has not been caught so far. This situation can be
8348 * tested for by calling aborting(). For an error in an internal
8349 * function or for the "E132" error in call_user_func(), however, the
8350 * throw point at which the "force_abort" flag (temporarily reset by
8351 * emsg()) is normally updated has not been reached yet. We need to
8352 * update that flag first to make aborting() reliable.
8353 */
8354 update_force_abort();
8355 }
8356 if (error == ERROR_NONE)
8357 ret = OK;
8358
8359 /*
8360 * Report an error unless the argument evaluation or function call has been
8361 * cancelled due to an aborting error, an interrupt, or an exception.
8362 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008363 if (!aborting())
8364 {
8365 switch (error)
8366 {
8367 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008368 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008369 break;
8370 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008371 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008372 break;
8373 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008374 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008375 name);
8376 break;
8377 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008378 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008379 name);
8380 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008381 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008382 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008383 name);
8384 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008385 }
8386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 if (fname != name && fname != fname_buf)
8389 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008390 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392 return ret;
8393}
8394
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008395/*
8396 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008397 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008398 */
8399 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008400emsg_funcname(ermsg, name)
8401 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008402 char_u *name;
8403{
8404 char_u *p;
8405
8406 if (*name == K_SPECIAL)
8407 p = concat_str((char_u *)"<SNR>", name + 3);
8408 else
8409 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008410 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008411 if (p != name)
8412 vim_free(p);
8413}
8414
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008415/*
8416 * Return TRUE for a non-zero Number and a non-empty String.
8417 */
8418 static int
8419non_zero_arg(argvars)
8420 typval_T *argvars;
8421{
8422 return ((argvars[0].v_type == VAR_NUMBER
8423 && argvars[0].vval.v_number != 0)
8424 || (argvars[0].v_type == VAR_STRING
8425 && argvars[0].vval.v_string != NULL
8426 && *argvars[0].vval.v_string != NUL));
8427}
8428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429/*********************************************
8430 * Implementation of the built-in functions
8431 */
8432
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008433#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008434static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8435
8436/*
8437 * Get the float value of "argvars[0]" into "f".
8438 * Returns FAIL when the argument is not a Number or Float.
8439 */
8440 static int
8441get_float_arg(argvars, f)
8442 typval_T *argvars;
8443 float_T *f;
8444{
8445 if (argvars[0].v_type == VAR_FLOAT)
8446 {
8447 *f = argvars[0].vval.v_float;
8448 return OK;
8449 }
8450 if (argvars[0].v_type == VAR_NUMBER)
8451 {
8452 *f = (float_T)argvars[0].vval.v_number;
8453 return OK;
8454 }
8455 EMSG(_("E808: Number or Float required"));
8456 return FAIL;
8457}
8458
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008459/*
8460 * "abs(expr)" function
8461 */
8462 static void
8463f_abs(argvars, rettv)
8464 typval_T *argvars;
8465 typval_T *rettv;
8466{
8467 if (argvars[0].v_type == VAR_FLOAT)
8468 {
8469 rettv->v_type = VAR_FLOAT;
8470 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8471 }
8472 else
8473 {
8474 varnumber_T n;
8475 int error = FALSE;
8476
8477 n = get_tv_number_chk(&argvars[0], &error);
8478 if (error)
8479 rettv->vval.v_number = -1;
8480 else if (n > 0)
8481 rettv->vval.v_number = n;
8482 else
8483 rettv->vval.v_number = -n;
8484 }
8485}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008486
8487/*
8488 * "acos()" function
8489 */
8490 static void
8491f_acos(argvars, rettv)
8492 typval_T *argvars;
8493 typval_T *rettv;
8494{
8495 float_T f;
8496
8497 rettv->v_type = VAR_FLOAT;
8498 if (get_float_arg(argvars, &f) == OK)
8499 rettv->vval.v_float = acos(f);
8500 else
8501 rettv->vval.v_float = 0.0;
8502}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008503#endif
8504
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008506 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 */
8508 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008509f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008510 typval_T *argvars;
8511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
Bram Moolenaar33570922005-01-25 22:26:29 +00008513 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008515 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008516 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008518 if ((l = argvars[0].vval.v_list) != NULL
8519 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
8520 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008522 }
8523 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008524 EMSG(_(e_listreq));
8525}
8526
8527/*
8528 * "append(lnum, string/list)" function
8529 */
8530 static void
8531f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008532 typval_T *argvars;
8533 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008534{
8535 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008536 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008537 list_T *l = NULL;
8538 listitem_T *li = NULL;
8539 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008540 long added = 0;
8541
Bram Moolenaar0d660222005-01-07 21:51:51 +00008542 lnum = get_tv_lnum(argvars);
8543 if (lnum >= 0
8544 && lnum <= curbuf->b_ml.ml_line_count
8545 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008546 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008547 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008548 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008549 l = argvars[1].vval.v_list;
8550 if (l == NULL)
8551 return;
8552 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008553 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008554 for (;;)
8555 {
8556 if (l == NULL)
8557 tv = &argvars[1]; /* append a string */
8558 else if (li == NULL)
8559 break; /* end of list */
8560 else
8561 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008562 line = get_tv_string_chk(tv);
8563 if (line == NULL) /* type error */
8564 {
8565 rettv->vval.v_number = 1; /* Failed */
8566 break;
8567 }
8568 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008569 ++added;
8570 if (l == NULL)
8571 break;
8572 li = li->li_next;
8573 }
8574
8575 appended_lines_mark(lnum, added);
8576 if (curwin->w_cursor.lnum > lnum)
8577 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008579 else
8580 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581}
8582
8583/*
8584 * "argc()" function
8585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008587f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008588 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008591 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592}
8593
8594/*
8595 * "argidx()" function
8596 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008599 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603}
8604
8605/*
8606 * "argv(nr)" function
8607 */
8608 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008609f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008610 typval_T *argvars;
8611 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612{
8613 int idx;
8614
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008615 if (argvars[0].v_type != VAR_UNKNOWN)
8616 {
8617 idx = get_tv_number_chk(&argvars[0], NULL);
8618 if (idx >= 0 && idx < ARGCOUNT)
8619 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8620 else
8621 rettv->vval.v_string = NULL;
8622 rettv->v_type = VAR_STRING;
8623 }
8624 else if (rettv_list_alloc(rettv) == OK)
8625 for (idx = 0; idx < ARGCOUNT; ++idx)
8626 list_append_string(rettv->vval.v_list,
8627 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628}
8629
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008630#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008631/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008632 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008633 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008634 static void
8635f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008636 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008637 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008638{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008639 float_T f;
8640
8641 rettv->v_type = VAR_FLOAT;
8642 if (get_float_arg(argvars, &f) == OK)
8643 rettv->vval.v_float = asin(f);
8644 else
8645 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008646}
8647
8648/*
8649 * "atan()" function
8650 */
8651 static void
8652f_atan(argvars, rettv)
8653 typval_T *argvars;
8654 typval_T *rettv;
8655{
8656 float_T f;
8657
8658 rettv->v_type = VAR_FLOAT;
8659 if (get_float_arg(argvars, &f) == OK)
8660 rettv->vval.v_float = atan(f);
8661 else
8662 rettv->vval.v_float = 0.0;
8663}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008664
8665/*
8666 * "atan2()" function
8667 */
8668 static void
8669f_atan2(argvars, rettv)
8670 typval_T *argvars;
8671 typval_T *rettv;
8672{
8673 float_T fx, fy;
8674
8675 rettv->v_type = VAR_FLOAT;
8676 if (get_float_arg(argvars, &fx) == OK
8677 && get_float_arg(&argvars[1], &fy) == OK)
8678 rettv->vval.v_float = atan2(fx, fy);
8679 else
8680 rettv->vval.v_float = 0.0;
8681}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008682#endif
8683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684/*
8685 * "browse(save, title, initdir, default)" function
8686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008688f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008689 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691{
8692#ifdef FEAT_BROWSE
8693 int save;
8694 char_u *title;
8695 char_u *initdir;
8696 char_u *defname;
8697 char_u buf[NUMBUFLEN];
8698 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008699 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008701 save = get_tv_number_chk(&argvars[0], &error);
8702 title = get_tv_string_chk(&argvars[1]);
8703 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8704 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008706 if (error || title == NULL || initdir == NULL || defname == NULL)
8707 rettv->vval.v_string = NULL;
8708 else
8709 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008710 do_browse(save ? BROWSE_SAVE : 0,
8711 title, defname, NULL, initdir, NULL, curbuf);
8712#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008714#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008715 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008716}
8717
8718/*
8719 * "browsedir(title, initdir)" function
8720 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008723 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008724 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008725{
8726#ifdef FEAT_BROWSE
8727 char_u *title;
8728 char_u *initdir;
8729 char_u buf[NUMBUFLEN];
8730
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008731 title = get_tv_string_chk(&argvars[0]);
8732 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008733
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008734 if (title == NULL || initdir == NULL)
8735 rettv->vval.v_string = NULL;
8736 else
8737 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008738 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008742 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743}
8744
Bram Moolenaar33570922005-01-25 22:26:29 +00008745static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008746
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747/*
8748 * Find a buffer by number or exact name.
8749 */
8750 static buf_T *
8751find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008752 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753{
8754 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008756 if (avar->v_type == VAR_NUMBER)
8757 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008758 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008760 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008761 if (buf == NULL)
8762 {
8763 /* No full path name match, try a match with a URL or a "nofile"
8764 * buffer, these don't use the full path. */
8765 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8766 if (buf->b_fname != NULL
8767 && (path_with_url(buf->b_fname)
8768#ifdef FEAT_QUICKFIX
8769 || bt_nofile(buf)
8770#endif
8771 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008772 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008773 break;
8774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 }
8776 return buf;
8777}
8778
8779/*
8780 * "bufexists(expr)" function
8781 */
8782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008783f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008784 typval_T *argvars;
8785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
8789
8790/*
8791 * "buflisted(expr)" function
8792 */
8793 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008795 typval_T *argvars;
8796 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797{
8798 buf_T *buf;
8799
8800 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802}
8803
8804/*
8805 * "bufloaded(expr)" function
8806 */
8807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008808f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008809 typval_T *argvars;
8810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
8812 buf_T *buf;
8813
8814 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008815 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816}
8817
Bram Moolenaar33570922005-01-25 22:26:29 +00008818static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008819
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820/*
8821 * Get buffer by number or pattern.
8822 */
8823 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008825 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828 int save_magic;
8829 char_u *save_cpo;
8830 buf_T *buf;
8831
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832 if (tv->v_type == VAR_NUMBER)
8833 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008834 if (tv->v_type != VAR_STRING)
8835 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (name == NULL || *name == NUL)
8837 return curbuf;
8838 if (name[0] == '$' && name[1] == NUL)
8839 return lastbuf;
8840
8841 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8842 save_magic = p_magic;
8843 p_magic = TRUE;
8844 save_cpo = p_cpo;
8845 p_cpo = (char_u *)"";
8846
8847 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
8848 TRUE, FALSE));
8849
8850 p_magic = save_magic;
8851 p_cpo = save_cpo;
8852
8853 /* If not found, try expanding the name, like done for bufexists(). */
8854 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008855 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856
8857 return buf;
8858}
8859
8860/*
8861 * "bufname(expr)" function
8862 */
8863 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008864f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008865 typval_T *argvars;
8866 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867{
8868 buf_T *buf;
8869
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008870 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 buf = get_buf_tv(&argvars[0]);
8873 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008875 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008877 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 --emsg_off;
8879}
8880
8881/*
8882 * "bufnr(expr)" function
8883 */
8884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008885f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008886 typval_T *argvars;
8887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008890 int error = FALSE;
8891 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008893 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008895 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008896 --emsg_off;
8897
8898 /* If the buffer isn't found and the second argument is not zero create a
8899 * new buffer. */
8900 if (buf == NULL
8901 && argvars[1].v_type != VAR_UNKNOWN
8902 && get_tv_number_chk(&argvars[1], &error) != 0
8903 && !error
8904 && (name = get_tv_string_chk(&argvars[0])) != NULL
8905 && !error)
8906 buf = buflist_new(name, NULL, (linenr_T)1, 0);
8907
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008909 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008911 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912}
8913
8914/*
8915 * "bufwinnr(nr)" function
8916 */
8917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008919 typval_T *argvars;
8920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922#ifdef FEAT_WINDOWS
8923 win_T *wp;
8924 int winnr = 0;
8925#endif
8926 buf_T *buf;
8927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008928 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931#ifdef FEAT_WINDOWS
8932 for (wp = firstwin; wp; wp = wp->w_next)
8933 {
8934 ++winnr;
8935 if (wp->w_buffer == buf)
8936 break;
8937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008938 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008940 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941#endif
8942 --emsg_off;
8943}
8944
8945/*
8946 * "byte2line(byte)" function
8947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00008950 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008951 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
8953#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955#else
8956 long boff = 0;
8957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008958 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 (linenr_T)0, &boff);
8964#endif
8965}
8966
8967/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008968 * "byteidx()" function
8969 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008972 typval_T *argvars;
8973 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008974{
8975#ifdef FEAT_MBYTE
8976 char_u *t;
8977#endif
8978 char_u *str;
8979 long idx;
8980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008981 str = get_tv_string_chk(&argvars[0]);
8982 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008983 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008984 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008985 return;
8986
8987#ifdef FEAT_MBYTE
8988 t = str;
8989 for ( ; idx > 0; idx--)
8990 {
8991 if (*t == NUL) /* EOL reached */
8992 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008993 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008994 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008995 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008996#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008997 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008999#endif
9000}
9001
9002/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009003 * "call(func, arglist)" function
9004 */
9005 static void
9006f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009009{
9010 char_u *func;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009011 typval_T argv[MAX_FUNC_ARGS + 1];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009012 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009013 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009014 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00009015 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009016
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009017 if (argvars[1].v_type != VAR_LIST)
9018 {
9019 EMSG(_(e_listreq));
9020 return;
9021 }
9022 if (argvars[1].vval.v_list == NULL)
9023 return;
9024
9025 if (argvars[0].v_type == VAR_FUNC)
9026 func = argvars[0].vval.v_string;
9027 else
9028 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009029 if (*func == NUL)
9030 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009031
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 if (argvars[2].v_type != VAR_UNKNOWN)
9033 {
9034 if (argvars[2].v_type != VAR_DICT)
9035 {
9036 EMSG(_(e_dictreq));
9037 return;
9038 }
9039 selfdict = argvars[2].vval.v_dict;
9040 }
9041
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009042 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
9043 item = item->li_next)
9044 {
9045 if (argc == MAX_FUNC_ARGS)
9046 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009047 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009048 break;
9049 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009050 /* Make a copy of each argument. This is needed to be able to set
9051 * v_lock to VAR_FIXED in the copy without changing the original list.
9052 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009053 copy_tv(&item->li_tv, &argv[argc++]);
9054 }
9055
9056 if (item == NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009057 (void)call_func(func, (int)STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009058 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9059 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009060
9061 /* Free the arguments. */
9062 while (argc > 0)
9063 clear_tv(&argv[--argc]);
9064}
9065
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009066#ifdef FEAT_FLOAT
9067/*
9068 * "ceil({float})" function
9069 */
9070 static void
9071f_ceil(argvars, rettv)
9072 typval_T *argvars;
9073 typval_T *rettv;
9074{
9075 float_T f;
9076
9077 rettv->v_type = VAR_FLOAT;
9078 if (get_float_arg(argvars, &f) == OK)
9079 rettv->vval.v_float = ceil(f);
9080 else
9081 rettv->vval.v_float = 0.0;
9082}
9083#endif
9084
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009085/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009086 * "changenr()" function
9087 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009088 static void
9089f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009090 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009091 typval_T *rettv;
9092{
9093 rettv->vval.v_number = curbuf->b_u_seq_cur;
9094}
9095
9096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 * "char2nr(string)" function
9098 */
9099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009100f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009101 typval_T *argvars;
9102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103{
9104#ifdef FEAT_MBYTE
9105 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 else
9108#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009109 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110}
9111
9112/*
9113 * "cindent(lnum)" function
9114 */
9115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009116f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009117 typval_T *argvars;
9118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119{
9120#ifdef FEAT_CINDENT
9121 pos_T pos;
9122 linenr_T lnum;
9123
9124 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9127 {
9128 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 curwin->w_cursor = pos;
9131 }
9132 else
9133#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135}
9136
9137/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009138 * "clearmatches()" function
9139 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009140 static void
9141f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009142 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009143 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009144{
9145#ifdef FEAT_SEARCH_EXTRA
9146 clear_matches(curwin);
9147#endif
9148}
9149
9150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 * "col(string)" function
9152 */
9153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *argvars;
9156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
9158 colnr_T col = 0;
9159 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009160 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009162 fp = var2fpos(&argvars[0], FALSE, &fnum);
9163 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 {
9165 if (fp->col == MAXCOL)
9166 {
9167 /* '> can be MAXCOL, get the length of the line then */
9168 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009169 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 else
9171 col = MAXCOL;
9172 }
9173 else
9174 {
9175 col = fp->col + 1;
9176#ifdef FEAT_VIRTUALEDIT
9177 /* col(".") when the cursor is on the NUL at the end of the line
9178 * because of "coladd" can be seen as an extra column. */
9179 if (virtual_active() && fp == &curwin->w_cursor)
9180 {
9181 char_u *p = ml_get_cursor();
9182
9183 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9184 curwin->w_virtcol - curwin->w_cursor.coladd))
9185 {
9186# ifdef FEAT_MBYTE
9187 int l;
9188
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009189 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 col += l;
9191# else
9192 if (*p != NUL && p[1] == NUL)
9193 ++col;
9194# endif
9195 }
9196 }
9197#endif
9198 }
9199 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
Bram Moolenaar572cb562005-08-05 21:35:02 +00009203#if defined(FEAT_INS_EXPAND)
9204/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009205 * "complete()" function
9206 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009207 static void
9208f_complete(argvars, rettv)
9209 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009210 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009211{
9212 int startcol;
9213
9214 if ((State & INSERT) == 0)
9215 {
9216 EMSG(_("E785: complete() can only be used in Insert mode"));
9217 return;
9218 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009219
9220 /* Check for undo allowed here, because if something was already inserted
9221 * the line was already saved for undo and this check isn't done. */
9222 if (!undo_allowed())
9223 return;
9224
Bram Moolenaarade00832006-03-10 21:46:58 +00009225 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9226 {
9227 EMSG(_(e_invarg));
9228 return;
9229 }
9230
9231 startcol = get_tv_number_chk(&argvars[0], NULL);
9232 if (startcol <= 0)
9233 return;
9234
9235 set_completion(startcol - 1, argvars[1].vval.v_list);
9236}
9237
9238/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009239 * "complete_add()" function
9240 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009241 static void
9242f_complete_add(argvars, rettv)
9243 typval_T *argvars;
9244 typval_T *rettv;
9245{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009246 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009247}
9248
9249/*
9250 * "complete_check()" function
9251 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009252 static void
9253f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009254 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009255 typval_T *rettv;
9256{
9257 int saved = RedrawingDisabled;
9258
9259 RedrawingDisabled = 0;
9260 ins_compl_check_keys(0);
9261 rettv->vval.v_number = compl_interrupted;
9262 RedrawingDisabled = saved;
9263}
9264#endif
9265
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266/*
9267 * "confirm(message, buttons[, default [, type]])" function
9268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009270f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009271 typval_T *argvars UNUSED;
9272 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273{
9274#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9275 char_u *message;
9276 char_u *buttons = NULL;
9277 char_u buf[NUMBUFLEN];
9278 char_u buf2[NUMBUFLEN];
9279 int def = 1;
9280 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009281 char_u *typestr;
9282 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009284 message = get_tv_string_chk(&argvars[0]);
9285 if (message == NULL)
9286 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009287 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009289 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9290 if (buttons == NULL)
9291 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009292 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009294 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009295 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009297 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9298 if (typestr == NULL)
9299 error = TRUE;
9300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009302 switch (TOUPPER_ASC(*typestr))
9303 {
9304 case 'E': type = VIM_ERROR; break;
9305 case 'Q': type = VIM_QUESTION; break;
9306 case 'I': type = VIM_INFO; break;
9307 case 'W': type = VIM_WARNING; break;
9308 case 'G': type = VIM_GENERIC; break;
9309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 }
9311 }
9312 }
9313 }
9314
9315 if (buttons == NULL || *buttons == NUL)
9316 buttons = (char_u *)_("&Ok");
9317
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009318 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009319 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 def, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321#endif
9322}
9323
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009324/*
9325 * "copy()" function
9326 */
9327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009329 typval_T *argvars;
9330 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009331{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009332 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009333}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009335#ifdef FEAT_FLOAT
9336/*
9337 * "cos()" function
9338 */
9339 static void
9340f_cos(argvars, rettv)
9341 typval_T *argvars;
9342 typval_T *rettv;
9343{
9344 float_T f;
9345
9346 rettv->v_type = VAR_FLOAT;
9347 if (get_float_arg(argvars, &f) == OK)
9348 rettv->vval.v_float = cos(f);
9349 else
9350 rettv->vval.v_float = 0.0;
9351}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009352
9353/*
9354 * "cosh()" function
9355 */
9356 static void
9357f_cosh(argvars, rettv)
9358 typval_T *argvars;
9359 typval_T *rettv;
9360{
9361 float_T f;
9362
9363 rettv->v_type = VAR_FLOAT;
9364 if (get_float_arg(argvars, &f) == OK)
9365 rettv->vval.v_float = cosh(f);
9366 else
9367 rettv->vval.v_float = 0.0;
9368}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009369#endif
9370
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009372 * "count()" function
9373 */
9374 static void
9375f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009376 typval_T *argvars;
9377 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009378{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009379 long n = 0;
9380 int ic = FALSE;
9381
Bram Moolenaare9a41262005-01-15 22:18:47 +00009382 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009383 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009384 listitem_T *li;
9385 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009386 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009387
Bram Moolenaare9a41262005-01-15 22:18:47 +00009388 if ((l = argvars[0].vval.v_list) != NULL)
9389 {
9390 li = l->lv_first;
9391 if (argvars[2].v_type != VAR_UNKNOWN)
9392 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009393 int error = FALSE;
9394
9395 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009396 if (argvars[3].v_type != VAR_UNKNOWN)
9397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 idx = get_tv_number_chk(&argvars[3], &error);
9399 if (!error)
9400 {
9401 li = list_find(l, idx);
9402 if (li == NULL)
9403 EMSGN(_(e_listidx), idx);
9404 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009406 if (error)
9407 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009408 }
9409
9410 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009411 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009412 ++n;
9413 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009414 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009415 else if (argvars[0].v_type == VAR_DICT)
9416 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009417 int todo;
9418 dict_T *d;
9419 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009420
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009421 if ((d = argvars[0].vval.v_dict) != NULL)
9422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 int error = FALSE;
9424
Bram Moolenaare9a41262005-01-15 22:18:47 +00009425 if (argvars[2].v_type != VAR_UNKNOWN)
9426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009427 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009428 if (argvars[3].v_type != VAR_UNKNOWN)
9429 EMSG(_(e_invarg));
9430 }
9431
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009432 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009433 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009434 {
9435 if (!HASHITEM_EMPTY(hi))
9436 {
9437 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009438 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009439 ++n;
9440 }
9441 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009442 }
9443 }
9444 else
9445 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009446 rettv->vval.v_number = n;
9447}
9448
9449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9451 *
9452 * Checks the existence of a cscope connection.
9453 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009455f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009456 typval_T *argvars UNUSED;
9457 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
9459#ifdef FEAT_CSCOPE
9460 int num = 0;
9461 char_u *dbpath = NULL;
9462 char_u *prepend = NULL;
9463 char_u buf[NUMBUFLEN];
9464
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009465 if (argvars[0].v_type != VAR_UNKNOWN
9466 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468 num = (int)get_tv_number(&argvars[0]);
9469 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009470 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472 }
9473
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009474 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475#endif
9476}
9477
9478/*
9479 * "cursor(lnum, col)" function
9480 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009481 * Moves the cursor to the specified line and column.
9482 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009485f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009486 typval_T *argvars;
9487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488{
9489 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009490#ifdef FEAT_VIRTUALEDIT
9491 long coladd = 0;
9492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009494 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009495 if (argvars[1].v_type == VAR_UNKNOWN)
9496 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009497 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009498
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009499 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009500 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009501 line = pos.lnum;
9502 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009503#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009504 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009505#endif
9506 }
9507 else
9508 {
9509 line = get_tv_lnum(argvars);
9510 col = get_tv_number_chk(&argvars[1], NULL);
9511#ifdef FEAT_VIRTUALEDIT
9512 if (argvars[2].v_type != VAR_UNKNOWN)
9513 coladd = get_tv_number_chk(&argvars[2], NULL);
9514#endif
9515 }
9516 if (line < 0 || col < 0
9517#ifdef FEAT_VIRTUALEDIT
9518 || coladd < 0
9519#endif
9520 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009521 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 if (line > 0)
9523 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 if (col > 0)
9525 curwin->w_cursor.col = col - 1;
9526#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009527 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528#endif
9529
9530 /* Make sure the cursor is in a valid position. */
9531 check_cursor();
9532#ifdef FEAT_MBYTE
9533 /* Correct cursor for multi-byte character. */
9534 if (has_mbyte)
9535 mb_adjust_cursor();
9536#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009537
9538 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009539 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540}
9541
9542/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009543 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009544 */
9545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009546f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009547 typval_T *argvars;
9548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009550 int noref = 0;
9551
9552 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009554 if (noref < 0 || noref > 1)
9555 EMSG(_(e_invarg));
9556 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009557 {
9558 current_copyID += COPYID_INC;
9559 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561}
9562
9563/*
9564 * "delete()" function
9565 */
9566 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009568 typval_T *argvars;
9569 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570{
9571 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009572 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575}
9576
9577/*
9578 * "did_filetype()" function
9579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009582 typval_T *argvars UNUSED;
9583 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584{
9585#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009586 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587#endif
9588}
9589
9590/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009591 * "diff_filler()" function
9592 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009595 typval_T *argvars UNUSED;
9596 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009597{
9598#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009599 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009600#endif
9601}
9602
9603/*
9604 * "diff_hlID()" function
9605 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009606 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009607f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009608 typval_T *argvars UNUSED;
9609 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009610{
9611#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009612 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009613 static linenr_T prev_lnum = 0;
9614 static int changedtick = 0;
9615 static int fnum = 0;
9616 static int change_start = 0;
9617 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009618 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009619 int filler_lines;
9620 int col;
9621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009622 if (lnum < 0) /* ignore type error in {lnum} arg */
9623 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009624 if (lnum != prev_lnum
9625 || changedtick != curbuf->b_changedtick
9626 || fnum != curbuf->b_fnum)
9627 {
9628 /* New line, buffer, change: need to get the values. */
9629 filler_lines = diff_check(curwin, lnum);
9630 if (filler_lines < 0)
9631 {
9632 if (filler_lines == -1)
9633 {
9634 change_start = MAXCOL;
9635 change_end = -1;
9636 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9637 hlID = HLF_ADD; /* added line */
9638 else
9639 hlID = HLF_CHD; /* changed line */
9640 }
9641 else
9642 hlID = HLF_ADD; /* added line */
9643 }
9644 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009645 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009646 prev_lnum = lnum;
9647 changedtick = curbuf->b_changedtick;
9648 fnum = curbuf->b_fnum;
9649 }
9650
9651 if (hlID == HLF_CHD || hlID == HLF_TXD)
9652 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009653 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009654 if (col >= change_start && col <= change_end)
9655 hlID = HLF_TXD; /* changed text */
9656 else
9657 hlID = HLF_CHD; /* changed line */
9658 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009659 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009660#endif
9661}
9662
9663/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009664 * "empty({expr})" function
9665 */
9666 static void
9667f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009668 typval_T *argvars;
9669 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009670{
9671 int n;
9672
9673 switch (argvars[0].v_type)
9674 {
9675 case VAR_STRING:
9676 case VAR_FUNC:
9677 n = argvars[0].vval.v_string == NULL
9678 || *argvars[0].vval.v_string == NUL;
9679 break;
9680 case VAR_NUMBER:
9681 n = argvars[0].vval.v_number == 0;
9682 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009683#ifdef FEAT_FLOAT
9684 case VAR_FLOAT:
9685 n = argvars[0].vval.v_float == 0.0;
9686 break;
9687#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009688 case VAR_LIST:
9689 n = argvars[0].vval.v_list == NULL
9690 || argvars[0].vval.v_list->lv_first == NULL;
9691 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009692 case VAR_DICT:
9693 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009694 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009695 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009696 default:
9697 EMSG2(_(e_intern2), "f_empty()");
9698 n = 0;
9699 }
9700
9701 rettv->vval.v_number = n;
9702}
9703
9704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 * "escape({string}, {chars})" function
9706 */
9707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009709 typval_T *argvars;
9710 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711{
9712 char_u buf[NUMBUFLEN];
9713
Bram Moolenaar758711c2005-02-02 23:11:38 +00009714 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9715 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717}
9718
9719/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009720 * "eval()" function
9721 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009722 static void
9723f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009724 typval_T *argvars;
9725 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009726{
9727 char_u *s;
9728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009729 s = get_tv_string_chk(&argvars[0]);
9730 if (s != NULL)
9731 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009733 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9734 {
9735 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009736 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009737 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009738 else if (*s != NUL)
9739 EMSG(_(e_trailing));
9740}
9741
9742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 * "eventhandler()" function
9744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009747 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751}
9752
9753/*
9754 * "executable()" function
9755 */
9756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009757f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009758 typval_T *argvars;
9759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009761 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762}
9763
9764/*
9765 * "exists()" function
9766 */
9767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009769 typval_T *argvars;
9770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771{
9772 char_u *p;
9773 char_u *name;
9774 int n = FALSE;
9775 int len = 0;
9776
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009777 no_autoload = TRUE;
9778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009779 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 if (*p == '$') /* environment variable */
9781 {
9782 /* first try "normal" environment variables (fast) */
9783 if (mch_getenv(p + 1) != NULL)
9784 n = TRUE;
9785 else
9786 {
9787 /* try expanding things like $VIM and ${HOME} */
9788 p = expand_env_save(p);
9789 if (p != NULL && *p != '$')
9790 n = TRUE;
9791 vim_free(p);
9792 }
9793 }
9794 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +00009795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +00009797 if (*skipwhite(p) != NUL)
9798 n = FALSE; /* trailing garbage */
9799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800 else if (*p == '*') /* internal or user defined function */
9801 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009802 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803 }
9804 else if (*p == ':')
9805 {
9806 n = cmd_exists(p + 1);
9807 }
9808 else if (*p == '#')
9809 {
9810#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009811 if (p[1] == '#')
9812 n = autocmd_supported(p + 2);
9813 else
9814 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815#endif
9816 }
9817 else /* internal variable */
9818 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009819 char_u *tofree;
9820 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009822 /* get_name_len() takes care of expanding curly braces */
9823 name = p;
9824 len = get_name_len(&p, &tofree, TRUE, FALSE);
9825 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009827 if (tofree != NULL)
9828 name = tofree;
9829 n = (get_var_tv(name, len, &tv, FALSE) == OK);
9830 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009832 /* handle d.key, l[idx], f(expr) */
9833 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
9834 if (n)
9835 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 }
9837 }
Bram Moolenaar79783442006-05-05 21:18:03 +00009838 if (*p != NUL)
9839 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00009841 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +02009845
9846 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847}
9848
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009849#ifdef FEAT_FLOAT
9850/*
9851 * "exp()" function
9852 */
9853 static void
9854f_exp(argvars, rettv)
9855 typval_T *argvars;
9856 typval_T *rettv;
9857{
9858 float_T f;
9859
9860 rettv->v_type = VAR_FLOAT;
9861 if (get_float_arg(argvars, &f) == OK)
9862 rettv->vval.v_float = exp(f);
9863 else
9864 rettv->vval.v_float = 0.0;
9865}
9866#endif
9867
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868/*
9869 * "expand()" function
9870 */
9871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009873 typval_T *argvars;
9874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875{
9876 char_u *s;
9877 int len;
9878 char_u *errormsg;
9879 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
9880 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009883 rettv->v_type = VAR_STRING;
9884 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 if (*s == '%' || *s == '#' || *s == '<')
9886 {
9887 ++emsg_off;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009888 rettv->vval.v_string = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 --emsg_off;
9890 }
9891 else
9892 {
9893 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00009894 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009895 if (argvars[1].v_type != VAR_UNKNOWN
9896 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009898 if (!error)
9899 {
9900 ExpandInit(&xpc);
9901 xpc.xp_context = EXPAND_FILES;
9902 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009903 }
9904 else
9905 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 }
9907}
9908
9909/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009910 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00009911 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009912 */
9913 static void
9914f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009915 typval_T *argvars;
9916 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009917{
Bram Moolenaare9a41262005-01-15 22:18:47 +00009918 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009919 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 list_T *l1, *l2;
9921 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009922 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009924
Bram Moolenaare9a41262005-01-15 22:18:47 +00009925 l1 = argvars[0].vval.v_list;
9926 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009927 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
9928 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 {
9930 if (argvars[2].v_type != VAR_UNKNOWN)
9931 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009932 before = get_tv_number_chk(&argvars[2], &error);
9933 if (error)
9934 return; /* type error; errmsg already given */
9935
Bram Moolenaar758711c2005-02-02 23:11:38 +00009936 if (before == l1->lv_len)
9937 item = NULL;
9938 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009939 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009940 item = list_find(l1, before);
9941 if (item == NULL)
9942 {
9943 EMSGN(_(e_listidx), before);
9944 return;
9945 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009946 }
9947 }
9948 else
9949 item = NULL;
9950 list_extend(l1, l2, item);
9951
Bram Moolenaare9a41262005-01-15 22:18:47 +00009952 copy_tv(&argvars[0], rettv);
9953 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009954 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009955 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
9956 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 dict_T *d1, *d2;
9958 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009959 char_u *action;
9960 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00009961 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009962 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009963
9964 d1 = argvars[0].vval.v_dict;
9965 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009966 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
9967 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009968 {
9969 /* Check the third argument. */
9970 if (argvars[2].v_type != VAR_UNKNOWN)
9971 {
9972 static char *(av[]) = {"keep", "force", "error"};
9973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009974 action = get_tv_string_chk(&argvars[2]);
9975 if (action == NULL)
9976 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009977 for (i = 0; i < 3; ++i)
9978 if (STRCMP(action, av[i]) == 0)
9979 break;
9980 if (i == 3)
9981 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009982 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009983 return;
9984 }
9985 }
9986 else
9987 action = (char_u *)"force";
9988
9989 /* Go over all entries in the second dict and add them to the
9990 * first dict. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009991 todo = (int)d2->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009992 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009993 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009994 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009995 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009996 --todo;
9997 di1 = dict_find(d1, hi2->hi_key, -1);
9998 if (di1 == NULL)
9999 {
10000 di1 = dictitem_copy(HI2DI(hi2));
10001 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10002 dictitem_free(di1);
10003 }
10004 else if (*action == 'e')
10005 {
10006 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10007 break;
10008 }
10009 else if (*action == 'f')
10010 {
10011 clear_tv(&di1->di_tv);
10012 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10013 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010014 }
10015 }
10016
Bram Moolenaare9a41262005-01-15 22:18:47 +000010017 copy_tv(&argvars[0], rettv);
10018 }
10019 }
10020 else
10021 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010022}
10023
10024/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010025 * "feedkeys()" function
10026 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010027 static void
10028f_feedkeys(argvars, rettv)
10029 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010030 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010031{
10032 int remap = TRUE;
10033 char_u *keys, *flags;
10034 char_u nbuf[NUMBUFLEN];
10035 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010036 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010037
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010038 /* This is not allowed in the sandbox. If the commands would still be
10039 * executed in the sandbox it would be OK, but it probably happens later,
10040 * when "sandbox" is no longer set. */
10041 if (check_secure())
10042 return;
10043
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010044 keys = get_tv_string(&argvars[0]);
10045 if (*keys != NUL)
10046 {
10047 if (argvars[1].v_type != VAR_UNKNOWN)
10048 {
10049 flags = get_tv_string_buf(&argvars[1], nbuf);
10050 for ( ; *flags != NUL; ++flags)
10051 {
10052 switch (*flags)
10053 {
10054 case 'n': remap = FALSE; break;
10055 case 'm': remap = TRUE; break;
10056 case 't': typed = TRUE; break;
10057 }
10058 }
10059 }
10060
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010061 /* Need to escape K_SPECIAL and CSI before putting the string in the
10062 * typeahead buffer. */
10063 keys_esc = vim_strsave_escape_csi(keys);
10064 if (keys_esc != NULL)
10065 {
10066 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010067 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010068 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010069 if (vgetc_busy)
10070 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010071 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010072 }
10073}
10074
10075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 * "filereadable()" function
10077 */
10078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010079f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010080 typval_T *argvars;
10081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010083 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084 char_u *p;
10085 int n;
10086
Bram Moolenaarc236c162008-07-13 17:41:49 +000010087#ifndef O_NONBLOCK
10088# define O_NONBLOCK 0
10089#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010091 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10092 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093 {
10094 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010095 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096 }
10097 else
10098 n = FALSE;
10099
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101}
10102
10103/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010104 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 * rights to write into.
10106 */
10107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010109 typval_T *argvars;
10110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010112 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010113}
10114
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010115static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010116
10117 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010118findfilendir(argvars, rettv, find_what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010119 typval_T *argvars;
10120 typval_T *rettv;
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010121 int find_what;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010122{
10123#ifdef FEAT_SEARCHPATH
10124 char_u *fname;
10125 char_u *fresult = NULL;
10126 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10127 char_u *p;
10128 char_u pathbuf[NUMBUFLEN];
10129 int count = 1;
10130 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010131 int error = FALSE;
10132#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010133
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010134 rettv->vval.v_string = NULL;
10135 rettv->v_type = VAR_STRING;
10136
10137#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010138 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010139
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010140 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10143 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010144 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 else
10146 {
10147 if (*p != NUL)
10148 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010151 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010152 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010153 }
10154
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010155 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10156 error = TRUE;
10157
10158 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010159 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010160 do
10161 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010162 if (rettv->v_type == VAR_STRING)
10163 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010164 fresult = find_file_in_path_option(first ? fname : NULL,
10165 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010166 0, first, path,
10167 find_what,
10168 curbuf->b_ffname,
10169 find_what == FINDFILE_DIR
10170 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010171 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010172
10173 if (fresult != NULL && rettv->v_type == VAR_LIST)
10174 list_append_string(rettv->vval.v_list, fresult, -1);
10175
10176 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010178
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010179 if (rettv->v_type == VAR_STRING)
10180 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010181#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010182}
10183
Bram Moolenaar33570922005-01-25 22:26:29 +000010184static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10185static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010186
10187/*
10188 * Implementation of map() and filter().
10189 */
10190 static void
10191filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010192 typval_T *argvars;
10193 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010194 int map;
10195{
10196 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010197 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010198 listitem_T *li, *nli;
10199 list_T *l = NULL;
10200 dictitem_T *di;
10201 hashtab_T *ht;
10202 hashitem_T *hi;
10203 dict_T *d = NULL;
10204 typval_T save_val;
10205 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010206 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010207 int todo;
Bram Moolenaar89d40322006-08-29 15:30:07 +000010208 char_u *ermsg = map ? (char_u *)"map()" : (char_u *)"filter()";
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010209 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010210 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010211
Bram Moolenaare9a41262005-01-15 22:18:47 +000010212 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010213 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010214 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010215 || (map && tv_check_lock(l->lv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010216 return;
10217 }
10218 else if (argvars[0].v_type == VAR_DICT)
10219 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010220 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar89d40322006-08-29 15:30:07 +000010221 || (map && tv_check_lock(d->dv_lock, ermsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010222 return;
10223 }
10224 else
10225 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010226 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 return;
10228 }
10229
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010230 expr = get_tv_string_buf_chk(&argvars[1], buf);
10231 /* On type errors, the preceding call has already displayed an error
10232 * message. Avoid a misleading error message for an empty string that
10233 * was not passed as argument. */
10234 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010236 prepare_vimvar(VV_VAL, &save_val);
10237 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010238
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010239 /* We reset "did_emsg" to be able to detect whether an error
10240 * occurred during evaluation of the expression. */
10241 save_did_emsg = did_emsg;
10242 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010243
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010244 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010245 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010246 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010247 vimvars[VV_KEY].vv_type = VAR_STRING;
10248
10249 ht = &d->dv_hashtab;
10250 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010251 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010252 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010254 if (!HASHITEM_EMPTY(hi))
10255 {
10256 --todo;
10257 di = HI2DI(hi);
Bram Moolenaar89d40322006-08-29 15:30:07 +000010258 if (tv_check_lock(di->di_tv.v_lock, ermsg))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 break;
10260 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010261 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010262 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010263 break;
10264 if (!map && rem)
10265 dictitem_remove(d, di);
10266 clear_tv(&vimvars[VV_KEY].vv_tv);
10267 }
10268 }
10269 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010270 }
10271 else
10272 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010273 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10274
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010275 for (li = l->lv_first; li != NULL; li = nli)
10276 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010277 if (tv_check_lock(li->li_tv.v_lock, ermsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010278 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010279 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010280 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010281 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010282 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010283 break;
10284 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010285 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010286 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010287 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010289
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010290 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010291 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010292
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010293 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010294 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010295
10296 copy_tv(&argvars[0], rettv);
10297}
10298
10299 static int
10300filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010301 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010302 char_u *expr;
10303 int map;
10304 int *remp;
10305{
Bram Moolenaar33570922005-01-25 22:26:29 +000010306 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010308 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010309
Bram Moolenaar33570922005-01-25 22:26:29 +000010310 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010311 s = expr;
10312 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010313 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 if (*s != NUL) /* check for trailing chars after expr */
10315 {
10316 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010317 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318 }
10319 if (map)
10320 {
10321 /* map(): replace the list item value */
10322 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010323 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010324 *tv = rettv;
10325 }
10326 else
10327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010328 int error = FALSE;
10329
Bram Moolenaare9a41262005-01-15 22:18:47 +000010330 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010331 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010332 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010333 /* On type error, nothing has been removed; return FAIL to stop the
10334 * loop. The error message was given by get_tv_number_chk(). */
10335 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010336 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010337 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010338 retval = OK;
10339theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010340 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010341 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010342}
10343
10344/*
10345 * "filter()" function
10346 */
10347 static void
10348f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010349 typval_T *argvars;
10350 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010351{
10352 filter_map(argvars, rettv, FALSE);
10353}
10354
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010355/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010356 * "finddir({fname}[, {path}[, {count}]])" function
10357 */
10358 static void
10359f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010360 typval_T *argvars;
10361 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010362{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010363 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010364}
10365
10366/*
10367 * "findfile({fname}[, {path}[, {count}]])" function
10368 */
10369 static void
10370f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010371 typval_T *argvars;
10372 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010373{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010374 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010375}
10376
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010377#ifdef FEAT_FLOAT
10378/*
10379 * "float2nr({float})" function
10380 */
10381 static void
10382f_float2nr(argvars, rettv)
10383 typval_T *argvars;
10384 typval_T *rettv;
10385{
10386 float_T f;
10387
10388 if (get_float_arg(argvars, &f) == OK)
10389 {
10390 if (f < -0x7fffffff)
10391 rettv->vval.v_number = -0x7fffffff;
10392 else if (f > 0x7fffffff)
10393 rettv->vval.v_number = 0x7fffffff;
10394 else
10395 rettv->vval.v_number = (varnumber_T)f;
10396 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010397}
10398
10399/*
10400 * "floor({float})" function
10401 */
10402 static void
10403f_floor(argvars, rettv)
10404 typval_T *argvars;
10405 typval_T *rettv;
10406{
10407 float_T f;
10408
10409 rettv->v_type = VAR_FLOAT;
10410 if (get_float_arg(argvars, &f) == OK)
10411 rettv->vval.v_float = floor(f);
10412 else
10413 rettv->vval.v_float = 0.0;
10414}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010415
10416/*
10417 * "fmod()" function
10418 */
10419 static void
10420f_fmod(argvars, rettv)
10421 typval_T *argvars;
10422 typval_T *rettv;
10423{
10424 float_T fx, fy;
10425
10426 rettv->v_type = VAR_FLOAT;
10427 if (get_float_arg(argvars, &fx) == OK
10428 && get_float_arg(&argvars[1], &fy) == OK)
10429 rettv->vval.v_float = fmod(fx, fy);
10430 else
10431 rettv->vval.v_float = 0.0;
10432}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010433#endif
10434
Bram Moolenaar0d660222005-01-07 21:51:51 +000010435/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010436 * "fnameescape({string})" function
10437 */
10438 static void
10439f_fnameescape(argvars, rettv)
10440 typval_T *argvars;
10441 typval_T *rettv;
10442{
10443 rettv->vval.v_string = vim_strsave_fnameescape(
10444 get_tv_string(&argvars[0]), FALSE);
10445 rettv->v_type = VAR_STRING;
10446}
10447
10448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 * "fnamemodify({fname}, {mods})" function
10450 */
10451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010453 typval_T *argvars;
10454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455{
10456 char_u *fname;
10457 char_u *mods;
10458 int usedlen = 0;
10459 int len;
10460 char_u *fbuf = NULL;
10461 char_u buf[NUMBUFLEN];
10462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010463 fname = get_tv_string_chk(&argvars[0]);
10464 mods = get_tv_string_buf_chk(&argvars[1], buf);
10465 if (fname == NULL || mods == NULL)
10466 fname = NULL;
10467 else
10468 {
10469 len = (int)STRLEN(fname);
10470 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010473 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010477 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 vim_free(fbuf);
10479}
10480
Bram Moolenaar33570922005-01-25 22:26:29 +000010481static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010482
10483/*
10484 * "foldclosed()" function
10485 */
10486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010487foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +000010488 typval_T *argvars;
10489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 int end;
10491{
10492#ifdef FEAT_FOLDING
10493 linenr_T lnum;
10494 linenr_T first, last;
10495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010496 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10498 {
10499 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10500 {
10501 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010502 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010504 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 return;
10506 }
10507 }
10508#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510}
10511
10512/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010513 * "foldclosed()" function
10514 */
10515 static void
10516f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010517 typval_T *argvars;
10518 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010519{
10520 foldclosed_both(argvars, rettv, FALSE);
10521}
10522
10523/*
10524 * "foldclosedend()" function
10525 */
10526 static void
10527f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010528 typval_T *argvars;
10529 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010530{
10531 foldclosed_both(argvars, rettv, TRUE);
10532}
10533
10534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535 * "foldlevel()" function
10536 */
10537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010539 typval_T *argvars;
10540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541{
10542#ifdef FEAT_FOLDING
10543 linenr_T lnum;
10544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010547 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549}
10550
10551/*
10552 * "foldtext()" function
10553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010555f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010556 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010557 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558{
10559#ifdef FEAT_FOLDING
10560 linenr_T lnum;
10561 char_u *s;
10562 char_u *r;
10563 int len;
10564 char *txt;
10565#endif
10566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010567 rettv->v_type = VAR_STRING;
10568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010570 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10571 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10572 <= curbuf->b_ml.ml_line_count
10573 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 {
10575 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10577 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578 {
10579 if (!linewhite(lnum))
10580 break;
10581 ++lnum;
10582 }
10583
10584 /* Find interesting text in this line. */
10585 s = skipwhite(ml_get(lnum));
10586 /* skip C comment-start */
10587 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010590 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010592 {
10593 s = skipwhite(ml_get(lnum + 1));
10594 if (*s == '*')
10595 s = skipwhite(s + 1);
10596 }
10597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 txt = _("+-%s%3ld lines: ");
10599 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010600 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 + 20 /* for %3ld */
10602 + STRLEN(s))); /* concatenated */
10603 if (r != NULL)
10604 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10606 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10607 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010608 len = (int)STRLEN(r);
10609 STRCAT(r, s);
10610 /* remove 'foldmarker' and 'commentstring' */
10611 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010612 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 }
10614 }
10615#endif
10616}
10617
10618/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010619 * "foldtextresult(lnum)" function
10620 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010623 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010624 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010625{
10626#ifdef FEAT_FOLDING
10627 linenr_T lnum;
10628 char_u *text;
10629 char_u buf[51];
10630 foldinfo_T foldinfo;
10631 int fold_count;
10632#endif
10633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634 rettv->v_type = VAR_STRING;
10635 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010636#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010638 /* treat illegal types and illegal string values for {lnum} the same */
10639 if (lnum < 0)
10640 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010641 fold_count = foldedCount(curwin, lnum, &foldinfo);
10642 if (fold_count > 0)
10643 {
10644 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10645 &foldinfo, buf);
10646 if (text == buf)
10647 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010648 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010649 }
10650#endif
10651}
10652
10653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 * "foreground()" function
10655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010657f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010658 typval_T *argvars UNUSED;
10659 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661#ifdef FEAT_GUI
10662 if (gui.in_use)
10663 gui_mch_set_foreground();
10664#else
10665# ifdef WIN32
10666 win32_set_foreground();
10667# endif
10668#endif
10669}
10670
10671/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010672 * "function()" function
10673 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T *argvars;
10677 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010678{
10679 char_u *s;
10680
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010682 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010683 EMSG2(_(e_invarg2), s);
Bram Moolenaar3d0089f2008-12-03 08:52:26 +000010684 /* Don't check an autoload name for existence here. */
10685 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010686 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010687 else
10688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 rettv->vval.v_string = vim_strsave(s);
10690 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010691 }
10692}
10693
10694/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010695 * "garbagecollect()" function
10696 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010697 static void
10698f_garbagecollect(argvars, rettv)
10699 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010700 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010701{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000010702 /* This is postponed until we are back at the toplevel, because we may be
10703 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10704 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000010705
10706 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
10707 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000010708}
10709
10710/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010711 * "get()" function
10712 */
10713 static void
10714f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010715 typval_T *argvars;
10716 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010717{
Bram Moolenaar33570922005-01-25 22:26:29 +000010718 listitem_T *li;
10719 list_T *l;
10720 dictitem_T *di;
10721 dict_T *d;
10722 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010723
Bram Moolenaare9a41262005-01-15 22:18:47 +000010724 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010725 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010726 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010727 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010728 int error = FALSE;
10729
10730 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
10731 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010732 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010733 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010734 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010735 else if (argvars[0].v_type == VAR_DICT)
10736 {
10737 if ((d = argvars[0].vval.v_dict) != NULL)
10738 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010739 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010740 if (di != NULL)
10741 tv = &di->di_tv;
10742 }
10743 }
10744 else
10745 EMSG2(_(e_listdictarg), "get()");
10746
10747 if (tv == NULL)
10748 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010749 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010750 copy_tv(&argvars[2], rettv);
10751 }
10752 else
10753 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010754}
10755
Bram Moolenaar342337a2005-07-21 21:11:17 +000010756static 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 +000010757
10758/*
10759 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000010760 * Return a range (from start to end) of lines in rettv from the specified
10761 * buffer.
10762 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010763 */
10764 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000010765get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010766 buf_T *buf;
10767 linenr_T start;
10768 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010769 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010770 typval_T *rettv;
10771{
10772 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010773
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010774 if (retlist && rettv_list_alloc(rettv) == FAIL)
10775 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000010776
10777 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
10778 return;
10779
10780 if (!retlist)
10781 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010782 if (start >= 1 && start <= buf->b_ml.ml_line_count)
10783 p = ml_get_buf(buf, start, FALSE);
10784 else
10785 p = (char_u *)"";
10786
10787 rettv->v_type = VAR_STRING;
10788 rettv->vval.v_string = vim_strsave(p);
10789 }
10790 else
10791 {
10792 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010793 return;
10794
10795 if (start < 1)
10796 start = 1;
10797 if (end > buf->b_ml.ml_line_count)
10798 end = buf->b_ml.ml_line_count;
10799 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000010800 if (list_append_string(rettv->vval.v_list,
10801 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000010802 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010803 }
10804}
10805
10806/*
10807 * "getbufline()" function
10808 */
10809 static void
10810f_getbufline(argvars, rettv)
10811 typval_T *argvars;
10812 typval_T *rettv;
10813{
10814 linenr_T lnum;
10815 linenr_T end;
10816 buf_T *buf;
10817
10818 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10819 ++emsg_off;
10820 buf = get_buf_tv(&argvars[0]);
10821 --emsg_off;
10822
Bram Moolenaar661b1822005-07-28 22:36:45 +000010823 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010824 if (argvars[2].v_type == VAR_UNKNOWN)
10825 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010826 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000010827 end = get_tv_lnum_buf(&argvars[2], buf);
10828
Bram Moolenaar342337a2005-07-21 21:11:17 +000010829 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000010830}
10831
Bram Moolenaar0d660222005-01-07 21:51:51 +000010832/*
10833 * "getbufvar()" function
10834 */
10835 static void
10836f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010837 typval_T *argvars;
10838 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010839{
10840 buf_T *buf;
10841 buf_T *save_curbuf;
10842 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000010843 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
10846 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010847 ++emsg_off;
10848 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010849
10850 rettv->v_type = VAR_STRING;
10851 rettv->vval.v_string = NULL;
10852
10853 if (buf != NULL && varname != NULL)
10854 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000010855 /* set curbuf to be our buf, temporarily */
10856 save_curbuf = curbuf;
10857 curbuf = buf;
10858
Bram Moolenaar0d660222005-01-07 21:51:51 +000010859 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010860 get_option_tv(&varname, rettv, TRUE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010861 else
10862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 if (*varname == NUL)
10864 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10865 * scope prefix before the NUL byte is required by
10866 * find_var_in_ht(). */
10867 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010868 /* look up the variable */
Bram Moolenaar632deed2008-06-27 18:26:11 +000010869 v = find_var_in_ht(&curbuf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010870 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000010871 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010872 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000010873
10874 /* restore previous notion of curbuf */
10875 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010876 }
10877
10878 --emsg_off;
10879}
10880
10881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 * "getchar()" function
10883 */
10884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010886 typval_T *argvars;
10887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888{
10889 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010890 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010891
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000010892 /* Position the cursor. Needed after a message that ends in a space. */
10893 windgoto(msg_row, msg_col);
10894
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 ++no_mapping;
10896 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000010897 for (;;)
10898 {
10899 if (argvars[0].v_type == VAR_UNKNOWN)
10900 /* getchar(): blocking wait. */
10901 n = safe_vgetc();
10902 else if (get_tv_number_chk(&argvars[0], &error) == 1)
10903 /* getchar(1): only check if char avail */
10904 n = vpeekc();
10905 else if (error || vpeekc() == NUL)
10906 /* illegal argument or getchar(0) and no char avail: return zero */
10907 n = 0;
10908 else
10909 /* getchar(0) and char avail: return char */
10910 n = safe_vgetc();
10911 if (n == K_IGNORE)
10912 continue;
10913 break;
10914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 --no_mapping;
10916 --allow_keys;
10917
Bram Moolenaar219b8702006-11-01 14:32:36 +000010918 vimvars[VV_MOUSE_WIN].vv_nr = 0;
10919 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
10920 vimvars[VV_MOUSE_COL].vv_nr = 0;
10921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923 if (IS_SPECIAL(n) || mod_mask != 0)
10924 {
10925 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10926 int i = 0;
10927
10928 /* Turn a special key into three bytes, plus modifier. */
10929 if (mod_mask != 0)
10930 {
10931 temp[i++] = K_SPECIAL;
10932 temp[i++] = KS_MODIFIER;
10933 temp[i++] = mod_mask;
10934 }
10935 if (IS_SPECIAL(n))
10936 {
10937 temp[i++] = K_SPECIAL;
10938 temp[i++] = K_SECOND(n);
10939 temp[i++] = K_THIRD(n);
10940 }
10941#ifdef FEAT_MBYTE
10942 else if (has_mbyte)
10943 i += (*mb_char2bytes)(n, temp + i);
10944#endif
10945 else
10946 temp[i++] = n;
10947 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948 rettv->v_type = VAR_STRING;
10949 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000010950
10951#ifdef FEAT_MOUSE
10952 if (n == K_LEFTMOUSE
10953 || n == K_LEFTMOUSE_NM
10954 || n == K_LEFTDRAG
10955 || n == K_LEFTRELEASE
10956 || n == K_LEFTRELEASE_NM
10957 || n == K_MIDDLEMOUSE
10958 || n == K_MIDDLEDRAG
10959 || n == K_MIDDLERELEASE
10960 || n == K_RIGHTMOUSE
10961 || n == K_RIGHTDRAG
10962 || n == K_RIGHTRELEASE
10963 || n == K_X1MOUSE
10964 || n == K_X1DRAG
10965 || n == K_X1RELEASE
10966 || n == K_X2MOUSE
10967 || n == K_X2DRAG
10968 || n == K_X2RELEASE
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +020010969 || n == K_MOUSELEFT
10970 || n == K_MOUSERIGHT
Bram Moolenaar219b8702006-11-01 14:32:36 +000010971 || n == K_MOUSEDOWN
10972 || n == K_MOUSEUP)
10973 {
10974 int row = mouse_row;
10975 int col = mouse_col;
10976 win_T *win;
10977 linenr_T lnum;
10978# ifdef FEAT_WINDOWS
10979 win_T *wp;
10980# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010981 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010982
10983 if (row >= 0 && col >= 0)
10984 {
10985 /* Find the window at the mouse coordinates and compute the
10986 * text position. */
10987 win = mouse_find_win(&row, &col);
10988 (void)mouse_comp_pos(win, &row, &col, &lnum);
10989# ifdef FEAT_WINDOWS
10990 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010991 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010992# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000010993 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000010994 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
10995 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
10996 }
10997 }
10998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 }
11000}
11001
11002/*
11003 * "getcharmod()" function
11004 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011006f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011007 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011}
11012
11013/*
11014 * "getcmdline()" function
11015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011018 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011019 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011021 rettv->v_type = VAR_STRING;
11022 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011023}
11024
11025/*
11026 * "getcmdpos()" function
11027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011030 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011034}
11035
11036/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011037 * "getcmdtype()" function
11038 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011039 static void
11040f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011041 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011042 typval_T *rettv;
11043{
11044 rettv->v_type = VAR_STRING;
11045 rettv->vval.v_string = alloc(2);
11046 if (rettv->vval.v_string != NULL)
11047 {
11048 rettv->vval.v_string[0] = get_cmdline_type();
11049 rettv->vval.v_string[1] = NUL;
11050 }
11051}
11052
11053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 * "getcwd()" function
11055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011058 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060{
11061 char_u cwd[MAXPATHL];
11062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011063 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066 else
11067 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011069#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +000011070 if (rettv->vval.v_string != NULL)
11071 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072#endif
11073 }
11074}
11075
11076/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011077 * "getfontname()" function
11078 */
11079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011080f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011081 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011082 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011083{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011084 rettv->v_type = VAR_STRING;
11085 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011086#ifdef FEAT_GUI
11087 if (gui.in_use)
11088 {
11089 GuiFont font;
11090 char_u *name = NULL;
11091
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011092 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011093 {
11094 /* Get the "Normal" font. Either the name saved by
11095 * hl_set_font_name() or from the font ID. */
11096 font = gui.norm_font;
11097 name = hl_get_font_name();
11098 }
11099 else
11100 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011101 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011102 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11103 return;
11104 font = gui_mch_get_font(name, FALSE);
11105 if (font == NOFONT)
11106 return; /* Invalid font name, return empty string. */
11107 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011109 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011110 gui_mch_free_font(font);
11111 }
11112#endif
11113}
11114
11115/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011116 * "getfperm({fname})" function
11117 */
11118 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011119f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011120 typval_T *argvars;
11121 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011122{
11123 char_u *fname;
11124 struct stat st;
11125 char_u *perm = NULL;
11126 char_u flags[] = "rwx";
11127 int i;
11128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011132 if (mch_stat((char *)fname, &st) >= 0)
11133 {
11134 perm = vim_strsave((char_u *)"---------");
11135 if (perm != NULL)
11136 {
11137 for (i = 0; i < 9; i++)
11138 {
11139 if (st.st_mode & (1 << (8 - i)))
11140 perm[i] = flags[i % 3];
11141 }
11142 }
11143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011145}
11146
11147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 * "getfsize({fname})" function
11149 */
11150 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011151f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011152 typval_T *argvars;
11153 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011154{
11155 char_u *fname;
11156 struct stat st;
11157
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011160 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011161
11162 if (mch_stat((char *)fname, &st) >= 0)
11163 {
11164 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011166 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011169
11170 /* non-perfect check for overflow */
11171 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11172 rettv->vval.v_number = -2;
11173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 }
11175 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011176 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011177}
11178
11179/*
11180 * "getftime({fname})" function
11181 */
11182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011183f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011184 typval_T *argvars;
11185 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186{
11187 char_u *fname;
11188 struct stat st;
11189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011190 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191
11192 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011193 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011194 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196}
11197
11198/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011199 * "getftype({fname})" function
11200 */
11201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011203 typval_T *argvars;
11204 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011205{
11206 char_u *fname;
11207 struct stat st;
11208 char_u *type = NULL;
11209 char *t;
11210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011212
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011214 if (mch_lstat((char *)fname, &st) >= 0)
11215 {
11216#ifdef S_ISREG
11217 if (S_ISREG(st.st_mode))
11218 t = "file";
11219 else if (S_ISDIR(st.st_mode))
11220 t = "dir";
11221# ifdef S_ISLNK
11222 else if (S_ISLNK(st.st_mode))
11223 t = "link";
11224# endif
11225# ifdef S_ISBLK
11226 else if (S_ISBLK(st.st_mode))
11227 t = "bdev";
11228# endif
11229# ifdef S_ISCHR
11230 else if (S_ISCHR(st.st_mode))
11231 t = "cdev";
11232# endif
11233# ifdef S_ISFIFO
11234 else if (S_ISFIFO(st.st_mode))
11235 t = "fifo";
11236# endif
11237# ifdef S_ISSOCK
11238 else if (S_ISSOCK(st.st_mode))
11239 t = "fifo";
11240# endif
11241 else
11242 t = "other";
11243#else
11244# ifdef S_IFMT
11245 switch (st.st_mode & S_IFMT)
11246 {
11247 case S_IFREG: t = "file"; break;
11248 case S_IFDIR: t = "dir"; break;
11249# ifdef S_IFLNK
11250 case S_IFLNK: t = "link"; break;
11251# endif
11252# ifdef S_IFBLK
11253 case S_IFBLK: t = "bdev"; break;
11254# endif
11255# ifdef S_IFCHR
11256 case S_IFCHR: t = "cdev"; break;
11257# endif
11258# ifdef S_IFIFO
11259 case S_IFIFO: t = "fifo"; break;
11260# endif
11261# ifdef S_IFSOCK
11262 case S_IFSOCK: t = "socket"; break;
11263# endif
11264 default: t = "other";
11265 }
11266# else
11267 if (mch_isdir(fname))
11268 t = "dir";
11269 else
11270 t = "file";
11271# endif
11272#endif
11273 type = vim_strsave((char_u *)t);
11274 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011276}
11277
11278/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011279 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011280 */
11281 static void
11282f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 typval_T *argvars;
11284 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011285{
11286 linenr_T lnum;
11287 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011288 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011289
11290 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011291 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011292 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011293 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011294 retlist = FALSE;
11295 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011296 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011297 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011298 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011299 retlist = TRUE;
11300 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011301
Bram Moolenaar342337a2005-07-21 21:11:17 +000011302 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011303}
11304
11305/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011306 * "getmatches()" function
11307 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011308 static void
11309f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011310 typval_T *argvars UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011311 typval_T *rettv;
11312{
11313#ifdef FEAT_SEARCH_EXTRA
11314 dict_T *dict;
11315 matchitem_T *cur = curwin->w_match_head;
11316
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011317 if (rettv_list_alloc(rettv) == OK)
11318 {
11319 while (cur != NULL)
11320 {
11321 dict = dict_alloc();
11322 if (dict == NULL)
11323 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011324 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11325 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11326 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11327 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11328 list_append_dict(rettv->vval.v_list, dict);
11329 cur = cur->next;
11330 }
11331 }
11332#endif
11333}
11334
11335/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011336 * "getpid()" function
11337 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011338 static void
11339f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011340 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011341 typval_T *rettv;
11342{
11343 rettv->vval.v_number = mch_get_pid();
11344}
11345
11346/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011347 * "getpos(string)" function
11348 */
11349 static void
11350f_getpos(argvars, rettv)
11351 typval_T *argvars;
11352 typval_T *rettv;
11353{
11354 pos_T *fp;
11355 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011356 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011357
11358 if (rettv_list_alloc(rettv) == OK)
11359 {
11360 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011361 fp = var2fpos(&argvars[0], TRUE, &fnum);
11362 if (fnum != -1)
11363 list_append_number(l, (varnumber_T)fnum);
11364 else
11365 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011366 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11367 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011368 list_append_number(l, (fp != NULL)
11369 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011370 : (varnumber_T)0);
11371 list_append_number(l,
11372#ifdef FEAT_VIRTUALEDIT
11373 (fp != NULL) ? (varnumber_T)fp->coladd :
11374#endif
11375 (varnumber_T)0);
11376 }
11377 else
11378 rettv->vval.v_number = FALSE;
11379}
11380
11381/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011382 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011383 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011384 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011385f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011386 typval_T *argvars UNUSED;
11387 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011388{
11389#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011390 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011391#endif
11392
Bram Moolenaar2641f772005-03-25 21:58:17 +000011393#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011394 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011395 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011396 wp = NULL;
11397 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11398 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011399 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011400 if (wp == NULL)
11401 return;
11402 }
11403
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011404 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011405 }
11406#endif
11407}
11408
11409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 * "getreg()" function
11411 */
11412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011413f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011414 typval_T *argvars;
11415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011416{
11417 char_u *strregname;
11418 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011419 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011420 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011422 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011424 strregname = get_tv_string_chk(&argvars[0]);
11425 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011426 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011427 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011430 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 regname = (strregname == NULL ? '"' : *strregname);
11432 if (regname == 0)
11433 regname = '"';
11434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 rettv->vval.v_string = error ? NULL :
11437 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438}
11439
11440/*
11441 * "getregtype()" function
11442 */
11443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011445 typval_T *argvars;
11446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447{
11448 char_u *strregname;
11449 int regname;
11450 char_u buf[NUMBUFLEN + 2];
11451 long reglen = 0;
11452
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011453 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011454 {
11455 strregname = get_tv_string_chk(&argvars[0]);
11456 if (strregname == NULL) /* type error; errmsg already given */
11457 {
11458 rettv->v_type = VAR_STRING;
11459 rettv->vval.v_string = NULL;
11460 return;
11461 }
11462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 else
11464 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011465 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466
11467 regname = (strregname == NULL ? '"' : *strregname);
11468 if (regname == 0)
11469 regname = '"';
11470
11471 buf[0] = NUL;
11472 buf[1] = NUL;
11473 switch (get_reg_type(regname, &reglen))
11474 {
11475 case MLINE: buf[0] = 'V'; break;
11476 case MCHAR: buf[0] = 'v'; break;
11477#ifdef FEAT_VISUAL
11478 case MBLOCK:
11479 buf[0] = Ctrl_V;
11480 sprintf((char *)buf + 1, "%ld", reglen + 1);
11481 break;
11482#endif
11483 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011484 rettv->v_type = VAR_STRING;
11485 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486}
11487
11488/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011489 * "gettabvar()" function
11490 */
11491 static void
11492f_gettabvar(argvars, rettv)
11493 typval_T *argvars;
11494 typval_T *rettv;
11495{
11496 tabpage_T *tp;
11497 dictitem_T *v;
11498 char_u *varname;
11499
11500 rettv->v_type = VAR_STRING;
11501 rettv->vval.v_string = NULL;
11502
11503 varname = get_tv_string_chk(&argvars[1]);
11504 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11505 if (tp != NULL && varname != NULL)
11506 {
11507 /* look up the variable */
11508 v = find_var_in_ht(&tp->tp_vars.dv_hashtab, varname, FALSE);
11509 if (v != NULL)
11510 copy_tv(&v->di_tv, rettv);
11511 }
11512}
11513
11514/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011515 * "gettabwinvar()" function
11516 */
11517 static void
11518f_gettabwinvar(argvars, rettv)
11519 typval_T *argvars;
11520 typval_T *rettv;
11521{
11522 getwinvar(argvars, rettv, 1);
11523}
11524
11525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526 * "getwinposx()" function
11527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011530 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011533 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011534#ifdef FEAT_GUI
11535 if (gui.in_use)
11536 {
11537 int x, y;
11538
11539 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011540 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011541 }
11542#endif
11543}
11544
11545/*
11546 * "getwinposy()" function
11547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011549f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011550 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011554#ifdef FEAT_GUI
11555 if (gui.in_use)
11556 {
11557 int x, y;
11558
11559 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 }
11562#endif
11563}
11564
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011565/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011566 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011567 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011568 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011569find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011570 typval_T *vp;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011571 tabpage_T *tp; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011572{
11573#ifdef FEAT_WINDOWS
11574 win_T *wp;
11575#endif
11576 int nr;
11577
11578 nr = get_tv_number_chk(vp, NULL);
11579
11580#ifdef FEAT_WINDOWS
11581 if (nr < 0)
11582 return NULL;
11583 if (nr == 0)
11584 return curwin;
11585
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011586 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11587 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011588 if (--nr <= 0)
11589 break;
11590 return wp;
11591#else
11592 if (nr == 0 || nr == 1)
11593 return curwin;
11594 return NULL;
11595#endif
11596}
11597
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598/*
11599 * "getwinvar()" function
11600 */
11601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011602f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011603 typval_T *argvars;
11604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011605{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011606 getwinvar(argvars, rettv, 0);
11607}
11608
11609/*
11610 * getwinvar() and gettabwinvar()
11611 */
11612 static void
11613getwinvar(argvars, rettv, off)
11614 typval_T *argvars;
11615 typval_T *rettv;
11616 int off; /* 1 for gettabwinvar() */
11617{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 win_T *win, *oldcurwin;
11619 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011620 dictitem_T *v;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011621 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011623#ifdef FEAT_WINDOWS
11624 if (off == 1)
11625 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11626 else
11627 tp = curtab;
11628#endif
11629 win = find_win_by_nr(&argvars[off], tp);
11630 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011631 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633 rettv->v_type = VAR_STRING;
11634 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635
11636 if (win != NULL && varname != NULL)
11637 {
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011638 /* Set curwin to be our win, temporarily. Also set curbuf, so
11639 * that we can get buffer-local options. */
11640 oldcurwin = curwin;
11641 curwin = win;
11642 curbuf = win->w_buffer;
11643
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644 if (*varname == '&') /* window-local-option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 else
11647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011648 if (*varname == NUL)
11649 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11650 * scope prefix before the NUL byte is required by
11651 * find_var_in_ht(). */
11652 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011653 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011654 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011655 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000011656 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011658
11659 /* restore previous notion of curwin */
11660 curwin = oldcurwin;
11661 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 }
11663
11664 --emsg_off;
11665}
11666
11667/*
11668 * "glob()" function
11669 */
11670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011672 typval_T *argvars;
11673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011675 int flags = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011677 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011678
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011679 /* When the optional second argument is non-zero, don't remove matches
11680 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11681 if (argvars[1].v_type != VAR_UNKNOWN
11682 && get_tv_number_chk(&argvars[1], &error))
11683 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011685 if (!error)
11686 {
11687 ExpandInit(&xpc);
11688 xpc.xp_context = EXPAND_FILES;
11689 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
11690 NULL, flags, WILD_ALL);
11691 }
11692 else
11693 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694}
11695
11696/*
11697 * "globpath()" function
11698 */
11699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011700f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011701 typval_T *argvars;
11702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011703{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011704 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011706 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011707 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011708
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011709 /* When the optional second argument is non-zero, don't remove matches
11710 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11711 if (argvars[2].v_type != VAR_UNKNOWN
11712 && get_tv_number_chk(&argvars[2], &error))
11713 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011714 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011715 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011716 rettv->vval.v_string = NULL;
11717 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011718 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
11719 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011720}
11721
11722/*
11723 * "has()" function
11724 */
11725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011727 typval_T *argvars;
11728 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011729{
11730 int i;
11731 char_u *name;
11732 int n = FALSE;
11733 static char *(has_list[]) =
11734 {
11735#ifdef AMIGA
11736 "amiga",
11737# ifdef FEAT_ARP
11738 "arp",
11739# endif
11740#endif
11741#ifdef __BEOS__
11742 "beos",
11743#endif
11744#ifdef MSDOS
11745# ifdef DJGPP
11746 "dos32",
11747# else
11748 "dos16",
11749# endif
11750#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000011751#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752 "mac",
11753#endif
11754#if defined(MACOS_X_UNIX)
11755 "macunix",
11756#endif
11757#ifdef OS2
11758 "os2",
11759#endif
11760#ifdef __QNX__
11761 "qnx",
11762#endif
11763#ifdef RISCOS
11764 "riscos",
11765#endif
11766#ifdef UNIX
11767 "unix",
11768#endif
11769#ifdef VMS
11770 "vms",
11771#endif
11772#ifdef WIN16
11773 "win16",
11774#endif
11775#ifdef WIN32
11776 "win32",
11777#endif
11778#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11779 "win32unix",
11780#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010011781#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782 "win64",
11783#endif
11784#ifdef EBCDIC
11785 "ebcdic",
11786#endif
11787#ifndef CASE_INSENSITIVE_FILENAME
11788 "fname_case",
11789#endif
11790#ifdef FEAT_ARABIC
11791 "arabic",
11792#endif
11793#ifdef FEAT_AUTOCMD
11794 "autocmd",
11795#endif
11796#ifdef FEAT_BEVAL
11797 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000011798# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11799 "balloon_multiline",
11800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801#endif
11802#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11803 "builtin_terms",
11804# ifdef ALL_BUILTIN_TCAPS
11805 "all_builtin_terms",
11806# endif
11807#endif
11808#ifdef FEAT_BYTEOFF
11809 "byte_offset",
11810#endif
11811#ifdef FEAT_CINDENT
11812 "cindent",
11813#endif
11814#ifdef FEAT_CLIENTSERVER
11815 "clientserver",
11816#endif
11817#ifdef FEAT_CLIPBOARD
11818 "clipboard",
11819#endif
11820#ifdef FEAT_CMDL_COMPL
11821 "cmdline_compl",
11822#endif
11823#ifdef FEAT_CMDHIST
11824 "cmdline_hist",
11825#endif
11826#ifdef FEAT_COMMENTS
11827 "comments",
11828#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011829#ifdef FEAT_CONCEAL
11830 "conceal",
11831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832#ifdef FEAT_CRYPT
11833 "cryptv",
11834#endif
11835#ifdef FEAT_CSCOPE
11836 "cscope",
11837#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020011838#ifdef FEAT_CURSORBIND
11839 "cursorbind",
11840#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000011841#ifdef CURSOR_SHAPE
11842 "cursorshape",
11843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844#ifdef DEBUG
11845 "debug",
11846#endif
11847#ifdef FEAT_CON_DIALOG
11848 "dialog_con",
11849#endif
11850#ifdef FEAT_GUI_DIALOG
11851 "dialog_gui",
11852#endif
11853#ifdef FEAT_DIFF
11854 "diff",
11855#endif
11856#ifdef FEAT_DIGRAPHS
11857 "digraphs",
11858#endif
11859#ifdef FEAT_DND
11860 "dnd",
11861#endif
11862#ifdef FEAT_EMACS_TAGS
11863 "emacs_tags",
11864#endif
11865 "eval", /* always present, of course! */
11866#ifdef FEAT_EX_EXTRA
11867 "ex_extra",
11868#endif
11869#ifdef FEAT_SEARCH_EXTRA
11870 "extra_search",
11871#endif
11872#ifdef FEAT_FKMAP
11873 "farsi",
11874#endif
11875#ifdef FEAT_SEARCHPATH
11876 "file_in_path",
11877#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011878#if defined(UNIX) && !defined(USE_SYSTEM)
11879 "filterpipe",
11880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881#ifdef FEAT_FIND_ID
11882 "find_in_path",
11883#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011884#ifdef FEAT_FLOAT
11885 "float",
11886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011887#ifdef FEAT_FOLDING
11888 "folding",
11889#endif
11890#ifdef FEAT_FOOTER
11891 "footer",
11892#endif
11893#if !defined(USE_SYSTEM) && defined(UNIX)
11894 "fork",
11895#endif
11896#ifdef FEAT_GETTEXT
11897 "gettext",
11898#endif
11899#ifdef FEAT_GUI
11900 "gui",
11901#endif
11902#ifdef FEAT_GUI_ATHENA
11903# ifdef FEAT_GUI_NEXTAW
11904 "gui_neXtaw",
11905# else
11906 "gui_athena",
11907# endif
11908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011909#ifdef FEAT_GUI_GTK
11910 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011911 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000011913#ifdef FEAT_GUI_GNOME
11914 "gui_gnome",
11915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011916#ifdef FEAT_GUI_MAC
11917 "gui_mac",
11918#endif
11919#ifdef FEAT_GUI_MOTIF
11920 "gui_motif",
11921#endif
11922#ifdef FEAT_GUI_PHOTON
11923 "gui_photon",
11924#endif
11925#ifdef FEAT_GUI_W16
11926 "gui_win16",
11927#endif
11928#ifdef FEAT_GUI_W32
11929 "gui_win32",
11930#endif
11931#ifdef FEAT_HANGULIN
11932 "hangul_input",
11933#endif
11934#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11935 "iconv",
11936#endif
11937#ifdef FEAT_INS_EXPAND
11938 "insert_expand",
11939#endif
11940#ifdef FEAT_JUMPLIST
11941 "jumplist",
11942#endif
11943#ifdef FEAT_KEYMAP
11944 "keymap",
11945#endif
11946#ifdef FEAT_LANGMAP
11947 "langmap",
11948#endif
11949#ifdef FEAT_LIBCALL
11950 "libcall",
11951#endif
11952#ifdef FEAT_LINEBREAK
11953 "linebreak",
11954#endif
11955#ifdef FEAT_LISP
11956 "lispindent",
11957#endif
11958#ifdef FEAT_LISTCMDS
11959 "listcmds",
11960#endif
11961#ifdef FEAT_LOCALMAP
11962 "localmap",
11963#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020011964#ifdef FEAT_LUA
11965# ifndef DYNAMIC_LUA
11966 "lua",
11967# endif
11968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969#ifdef FEAT_MENU
11970 "menu",
11971#endif
11972#ifdef FEAT_SESSION
11973 "mksession",
11974#endif
11975#ifdef FEAT_MODIFY_FNAME
11976 "modify_fname",
11977#endif
11978#ifdef FEAT_MOUSE
11979 "mouse",
11980#endif
11981#ifdef FEAT_MOUSESHAPE
11982 "mouseshape",
11983#endif
11984#if defined(UNIX) || defined(VMS)
11985# ifdef FEAT_MOUSE_DEC
11986 "mouse_dec",
11987# endif
11988# ifdef FEAT_MOUSE_GPM
11989 "mouse_gpm",
11990# endif
11991# ifdef FEAT_MOUSE_JSB
11992 "mouse_jsbterm",
11993# endif
11994# ifdef FEAT_MOUSE_NET
11995 "mouse_netterm",
11996# endif
11997# ifdef FEAT_MOUSE_PTERM
11998 "mouse_pterm",
11999# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012000# ifdef FEAT_SYSMOUSE
12001 "mouse_sysmouse",
12002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003# ifdef FEAT_MOUSE_XTERM
12004 "mouse_xterm",
12005# endif
12006#endif
12007#ifdef FEAT_MBYTE
12008 "multi_byte",
12009#endif
12010#ifdef FEAT_MBYTE_IME
12011 "multi_byte_ime",
12012#endif
12013#ifdef FEAT_MULTI_LANG
12014 "multi_lang",
12015#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012016#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012017#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012018 "mzscheme",
12019#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021#ifdef FEAT_OLE
12022 "ole",
12023#endif
12024#ifdef FEAT_OSFILETYPE
12025 "osfiletype",
12026#endif
12027#ifdef FEAT_PATH_EXTRA
12028 "path_extra",
12029#endif
12030#ifdef FEAT_PERL
12031#ifndef DYNAMIC_PERL
12032 "perl",
12033#endif
12034#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012035#ifdef FEAT_PERSISTENT_UNDO
12036 "persistent_undo",
12037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012038#ifdef FEAT_PYTHON
12039#ifndef DYNAMIC_PYTHON
12040 "python",
12041#endif
12042#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012043#ifdef FEAT_PYTHON3
12044#ifndef DYNAMIC_PYTHON3
12045 "python3",
12046#endif
12047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048#ifdef FEAT_POSTSCRIPT
12049 "postscript",
12050#endif
12051#ifdef FEAT_PRINTER
12052 "printer",
12053#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012054#ifdef FEAT_PROFILE
12055 "profile",
12056#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012057#ifdef FEAT_RELTIME
12058 "reltime",
12059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060#ifdef FEAT_QUICKFIX
12061 "quickfix",
12062#endif
12063#ifdef FEAT_RIGHTLEFT
12064 "rightleft",
12065#endif
12066#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12067 "ruby",
12068#endif
12069#ifdef FEAT_SCROLLBIND
12070 "scrollbind",
12071#endif
12072#ifdef FEAT_CMDL_INFO
12073 "showcmd",
12074 "cmdline_info",
12075#endif
12076#ifdef FEAT_SIGNS
12077 "signs",
12078#endif
12079#ifdef FEAT_SMARTINDENT
12080 "smartindent",
12081#endif
12082#ifdef FEAT_SNIFF
12083 "sniff",
12084#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012085#ifdef STARTUPTIME
12086 "startuptime",
12087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088#ifdef FEAT_STL_OPT
12089 "statusline",
12090#endif
12091#ifdef FEAT_SUN_WORKSHOP
12092 "sun_workshop",
12093#endif
12094#ifdef FEAT_NETBEANS_INTG
12095 "netbeans_intg",
12096#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012097#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012098 "spell",
12099#endif
12100#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012101 "syntax",
12102#endif
12103#if defined(USE_SYSTEM) || !defined(UNIX)
12104 "system",
12105#endif
12106#ifdef FEAT_TAG_BINS
12107 "tag_binary",
12108#endif
12109#ifdef FEAT_TAG_OLDSTATIC
12110 "tag_old_static",
12111#endif
12112#ifdef FEAT_TAG_ANYWHITE
12113 "tag_any_white",
12114#endif
12115#ifdef FEAT_TCL
12116# ifndef DYNAMIC_TCL
12117 "tcl",
12118# endif
12119#endif
12120#ifdef TERMINFO
12121 "terminfo",
12122#endif
12123#ifdef FEAT_TERMRESPONSE
12124 "termresponse",
12125#endif
12126#ifdef FEAT_TEXTOBJ
12127 "textobjects",
12128#endif
12129#ifdef HAVE_TGETENT
12130 "tgetent",
12131#endif
12132#ifdef FEAT_TITLE
12133 "title",
12134#endif
12135#ifdef FEAT_TOOLBAR
12136 "toolbar",
12137#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012138#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12139 "unnamedplus",
12140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141#ifdef FEAT_USR_CMDS
12142 "user-commands", /* was accidentally included in 5.4 */
12143 "user_commands",
12144#endif
12145#ifdef FEAT_VIMINFO
12146 "viminfo",
12147#endif
12148#ifdef FEAT_VERTSPLIT
12149 "vertsplit",
12150#endif
12151#ifdef FEAT_VIRTUALEDIT
12152 "virtualedit",
12153#endif
12154#ifdef FEAT_VISUAL
12155 "visual",
12156#endif
12157#ifdef FEAT_VISUALEXTRA
12158 "visualextra",
12159#endif
12160#ifdef FEAT_VREPLACE
12161 "vreplace",
12162#endif
12163#ifdef FEAT_WILDIGN
12164 "wildignore",
12165#endif
12166#ifdef FEAT_WILDMENU
12167 "wildmenu",
12168#endif
12169#ifdef FEAT_WINDOWS
12170 "windows",
12171#endif
12172#ifdef FEAT_WAK
12173 "winaltkeys",
12174#endif
12175#ifdef FEAT_WRITEBACKUP
12176 "writebackup",
12177#endif
12178#ifdef FEAT_XIM
12179 "xim",
12180#endif
12181#ifdef FEAT_XFONTSET
12182 "xfontset",
12183#endif
12184#ifdef USE_XSMP
12185 "xsmp",
12186#endif
12187#ifdef USE_XSMP_INTERACT
12188 "xsmp_interact",
12189#endif
12190#ifdef FEAT_XCLIPBOARD
12191 "xterm_clipboard",
12192#endif
12193#ifdef FEAT_XTERM_SAVE
12194 "xterm_save",
12195#endif
12196#if defined(UNIX) && defined(FEAT_X11)
12197 "X11",
12198#endif
12199 NULL
12200 };
12201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012202 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012203 for (i = 0; has_list[i] != NULL; ++i)
12204 if (STRICMP(name, has_list[i]) == 0)
12205 {
12206 n = TRUE;
12207 break;
12208 }
12209
12210 if (n == FALSE)
12211 {
12212 if (STRNICMP(name, "patch", 5) == 0)
12213 n = has_patch(atoi((char *)name + 5));
12214 else if (STRICMP(name, "vim_starting") == 0)
12215 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012216#ifdef FEAT_MBYTE
12217 else if (STRICMP(name, "multi_byte_encoding") == 0)
12218 n = has_mbyte;
12219#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012220#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12221 else if (STRICMP(name, "balloon_multiline") == 0)
12222 n = multiline_balloon_available();
12223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224#ifdef DYNAMIC_TCL
12225 else if (STRICMP(name, "tcl") == 0)
12226 n = tcl_enabled(FALSE);
12227#endif
12228#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12229 else if (STRICMP(name, "iconv") == 0)
12230 n = iconv_enabled(FALSE);
12231#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012232#ifdef DYNAMIC_LUA
12233 else if (STRICMP(name, "lua") == 0)
12234 n = lua_enabled(FALSE);
12235#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012236#ifdef DYNAMIC_MZSCHEME
12237 else if (STRICMP(name, "mzscheme") == 0)
12238 n = mzscheme_enabled(FALSE);
12239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240#ifdef DYNAMIC_RUBY
12241 else if (STRICMP(name, "ruby") == 0)
12242 n = ruby_enabled(FALSE);
12243#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012244#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245#ifdef DYNAMIC_PYTHON
12246 else if (STRICMP(name, "python") == 0)
12247 n = python_enabled(FALSE);
12248#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012249#endif
12250#ifdef FEAT_PYTHON3
12251#ifdef DYNAMIC_PYTHON3
12252 else if (STRICMP(name, "python3") == 0)
12253 n = python3_enabled(FALSE);
12254#endif
12255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256#ifdef DYNAMIC_PERL
12257 else if (STRICMP(name, "perl") == 0)
12258 n = perl_enabled(FALSE);
12259#endif
12260#ifdef FEAT_GUI
12261 else if (STRICMP(name, "gui_running") == 0)
12262 n = (gui.in_use || gui.starting);
12263# ifdef FEAT_GUI_W32
12264 else if (STRICMP(name, "gui_win32s") == 0)
12265 n = gui_is_win32s();
12266# endif
12267# ifdef FEAT_BROWSE
12268 else if (STRICMP(name, "browse") == 0)
12269 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12270# endif
12271#endif
12272#ifdef FEAT_SYN_HL
12273 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012274 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275#endif
12276#if defined(WIN3264)
12277 else if (STRICMP(name, "win95") == 0)
12278 n = mch_windows95();
12279#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012280#ifdef FEAT_NETBEANS_INTG
12281 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012282 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 }
12285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012286 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287}
12288
12289/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012290 * "has_key()" function
12291 */
12292 static void
12293f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012294 typval_T *argvars;
12295 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012296{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012297 if (argvars[0].v_type != VAR_DICT)
12298 {
12299 EMSG(_(e_dictreq));
12300 return;
12301 }
12302 if (argvars[0].vval.v_dict == NULL)
12303 return;
12304
12305 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012306 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012307}
12308
12309/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012310 * "haslocaldir()" function
12311 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012312 static void
12313f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012314 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012315 typval_T *rettv;
12316{
12317 rettv->vval.v_number = (curwin->w_localdir != NULL);
12318}
12319
12320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 * "hasmapto()" function
12322 */
12323 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012325 typval_T *argvars;
12326 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327{
12328 char_u *name;
12329 char_u *mode;
12330 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012331 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012333 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012334 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012335 mode = (char_u *)"nvo";
12336 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012339 if (argvars[2].v_type != VAR_UNKNOWN)
12340 abbr = get_tv_number(&argvars[2]);
12341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342
Bram Moolenaar2c932302006-03-18 21:42:09 +000012343 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012344 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012345 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012346 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347}
12348
12349/*
12350 * "histadd()" function
12351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012354 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356{
12357#ifdef FEAT_CMDHIST
12358 int histype;
12359 char_u *str;
12360 char_u buf[NUMBUFLEN];
12361#endif
12362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364 if (check_restricted() || check_secure())
12365 return;
12366#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012367 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12368 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 if (histype >= 0)
12370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012371 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 if (*str != NUL)
12373 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012374 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012376 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 return;
12378 }
12379 }
12380#endif
12381}
12382
12383/*
12384 * "histdel()" function
12385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012387f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012388 typval_T *argvars UNUSED;
12389 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390{
12391#ifdef FEAT_CMDHIST
12392 int n;
12393 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012394 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012396 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12397 if (str == NULL)
12398 n = 0;
12399 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012402 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012404 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012405 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 else
12407 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012408 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 get_tv_string_buf(&argvars[1], buf));
12410 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411#endif
12412}
12413
12414/*
12415 * "histget()" function
12416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012418f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012419 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012421{
12422#ifdef FEAT_CMDHIST
12423 int type;
12424 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012425 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012427 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12428 if (str == NULL)
12429 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012431 {
12432 type = get_histtype(str);
12433 if (argvars[1].v_type == VAR_UNKNOWN)
12434 idx = get_history_idx(type);
12435 else
12436 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12437 /* -1 on type error */
12438 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444}
12445
12446/*
12447 * "histnr()" function
12448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012451 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012452 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453{
12454 int i;
12455
12456#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012457 char_u *history = get_tv_string_chk(&argvars[0]);
12458
12459 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460 if (i >= HIST_CMD && i < HIST_COUNT)
12461 i = get_history_idx(i);
12462 else
12463#endif
12464 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466}
12467
12468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012469 * "highlightID(name)" function
12470 */
12471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012472f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012473 typval_T *argvars;
12474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012476 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012477}
12478
12479/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012480 * "highlight_exists()" function
12481 */
12482 static void
12483f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012484 typval_T *argvars;
12485 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012486{
12487 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12488}
12489
12490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 * "hostname()" function
12492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012495 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012496 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497{
12498 char_u hostname[256];
12499
12500 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->v_type = VAR_STRING;
12502 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012503}
12504
12505/*
12506 * iconv() function
12507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012509f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012510 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012511 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012512{
12513#ifdef FEAT_MBYTE
12514 char_u buf1[NUMBUFLEN];
12515 char_u buf2[NUMBUFLEN];
12516 char_u *from, *to, *str;
12517 vimconv_T vimconv;
12518#endif
12519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012520 rettv->v_type = VAR_STRING;
12521 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012522
12523#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012524 str = get_tv_string(&argvars[0]);
12525 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12526 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 vimconv.vc_type = CONV_NONE;
12528 convert_setup(&vimconv, from, to);
12529
12530 /* If the encodings are equal, no conversion needed. */
12531 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535
12536 convert_setup(&vimconv, NULL, NULL);
12537 vim_free(from);
12538 vim_free(to);
12539#endif
12540}
12541
12542/*
12543 * "indent()" function
12544 */
12545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012546f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012547 typval_T *argvars;
12548 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012549{
12550 linenr_T lnum;
12551
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012552 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012553 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012554 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012556 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557}
12558
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012559/*
12560 * "index()" function
12561 */
12562 static void
12563f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012564 typval_T *argvars;
12565 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012566{
Bram Moolenaar33570922005-01-25 22:26:29 +000012567 list_T *l;
12568 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012569 long idx = 0;
12570 int ic = FALSE;
12571
12572 rettv->vval.v_number = -1;
12573 if (argvars[0].v_type != VAR_LIST)
12574 {
12575 EMSG(_(e_listreq));
12576 return;
12577 }
12578 l = argvars[0].vval.v_list;
12579 if (l != NULL)
12580 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012581 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012584 int error = FALSE;
12585
Bram Moolenaar758711c2005-02-02 23:11:38 +000012586 /* Start at specified item. Use the cached index that list_find()
12587 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012588 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012589 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012590 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012591 ic = get_tv_number_chk(&argvars[3], &error);
12592 if (error)
12593 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012594 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012595
Bram Moolenaar758711c2005-02-02 23:11:38 +000012596 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012597 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012598 {
12599 rettv->vval.v_number = idx;
12600 break;
12601 }
12602 }
12603}
12604
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605static int inputsecret_flag = 0;
12606
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012607static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12608
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012610 * This function is used by f_input() and f_inputdialog() functions. The third
12611 * argument to f_input() specifies the type of completion to use at the
12612 * prompt. The third argument to f_inputdialog() specifies the value to return
12613 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 */
12615 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012616get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012617 typval_T *argvars;
12618 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012619 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012621 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622 char_u *p = NULL;
12623 int c;
12624 char_u buf[NUMBUFLEN];
12625 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012626 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012627 int xp_type = EXPAND_NOTHING;
12628 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012631 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632
12633#ifdef NO_CONSOLE_INPUT
12634 /* While starting up, there is no place to enter text. */
12635 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012636 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012637#endif
12638
12639 cmd_silent = FALSE; /* Want to see the prompt. */
12640 if (prompt != NULL)
12641 {
12642 /* Only the part of the message after the last NL is considered as
12643 * prompt for the command line */
12644 p = vim_strrchr(prompt, '\n');
12645 if (p == NULL)
12646 p = prompt;
12647 else
12648 {
12649 ++p;
12650 c = *p;
12651 *p = NUL;
12652 msg_start();
12653 msg_clr_eos();
12654 msg_puts_attr(prompt, echo_attr);
12655 msg_didout = FALSE;
12656 msg_starthere();
12657 *p = c;
12658 }
12659 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012661 if (argvars[1].v_type != VAR_UNKNOWN)
12662 {
12663 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12664 if (defstr != NULL)
12665 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012667 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012668 {
12669 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012670 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012671 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012672
Bram Moolenaar4463f292005-09-25 22:20:24 +000012673 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012674
Bram Moolenaar4463f292005-09-25 22:20:24 +000012675 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12676 if (xp_name == NULL)
12677 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012678
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012679 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012680
Bram Moolenaar4463f292005-09-25 22:20:24 +000012681 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12682 &xp_arg) == FAIL)
12683 return;
12684 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012685 }
12686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012687 if (defstr != NULL)
12688 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012689 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12690 xp_type, xp_arg);
12691
12692 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012694 /* since the user typed this, no need to wait for return */
12695 need_wait_return = FALSE;
12696 msg_didout = FALSE;
12697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 cmd_silent = cmd_silent_save;
12699}
12700
12701/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012702 * "input()" function
12703 * Also handles inputsecret() when inputsecret is set.
12704 */
12705 static void
12706f_input(argvars, rettv)
12707 typval_T *argvars;
12708 typval_T *rettv;
12709{
12710 get_user_input(argvars, rettv, FALSE);
12711}
12712
12713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714 * "inputdialog()" function
12715 */
12716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012718 typval_T *argvars;
12719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720{
12721#if defined(FEAT_GUI_TEXTDIALOG)
12722 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12723 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12724 {
12725 char_u *message;
12726 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012727 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012729 message = get_tv_string_chk(&argvars[0]);
12730 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012731 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012732 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 else
12734 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 if (message != NULL && defstr != NULL
12736 && do_dialog(VIM_QUESTION, NULL, message,
12737 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 else
12740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012741 if (message != NULL && defstr != NULL
12742 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012743 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744 rettv->vval.v_string = vim_strsave(
12745 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 }
12751 else
12752#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012753 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754}
12755
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012756/*
12757 * "inputlist()" function
12758 */
12759 static void
12760f_inputlist(argvars, rettv)
12761 typval_T *argvars;
12762 typval_T *rettv;
12763{
12764 listitem_T *li;
12765 int selected;
12766 int mouse_used;
12767
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012768#ifdef NO_CONSOLE_INPUT
12769 /* While starting up, there is no place to enter text. */
12770 if (no_console_input())
12771 return;
12772#endif
12773 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12774 {
12775 EMSG2(_(e_listarg), "inputlist()");
12776 return;
12777 }
12778
12779 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012780 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012781 lines_left = Rows; /* avoid more prompt */
12782 msg_scroll = TRUE;
12783 msg_clr_eos();
12784
12785 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12786 {
12787 msg_puts(get_tv_string(&li->li_tv));
12788 msg_putchar('\n');
12789 }
12790
12791 /* Ask for choice. */
12792 selected = prompt_for_number(&mouse_used);
12793 if (mouse_used)
12794 selected -= lines_left;
12795
12796 rettv->vval.v_number = selected;
12797}
12798
12799
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12801
12802/*
12803 * "inputrestore()" function
12804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012806f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012807 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012808 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809{
12810 if (ga_userinput.ga_len > 0)
12811 {
12812 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12814 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012815 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 }
12817 else if (p_verbose > 1)
12818 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012819 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
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/*
12825 * "inputsave()" function
12826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012829 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012832 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833 if (ga_grow(&ga_userinput, 1) == OK)
12834 {
12835 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12836 + ga_userinput.ga_len);
12837 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012838 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 }
12840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842}
12843
12844/*
12845 * "inputsecret()" function
12846 */
12847 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012849 typval_T *argvars;
12850 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851{
12852 ++cmdline_star;
12853 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012854 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855 --cmdline_star;
12856 --inputsecret_flag;
12857}
12858
12859/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012860 * "insert()" function
12861 */
12862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 typval_T *argvars;
12865 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012866{
12867 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012868 listitem_T *item;
12869 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012870 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012871
12872 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012873 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012874 else if ((l = argvars[0].vval.v_list) != NULL
12875 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012876 {
12877 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012878 before = get_tv_number_chk(&argvars[2], &error);
12879 if (error)
12880 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012881
Bram Moolenaar758711c2005-02-02 23:11:38 +000012882 if (before == l->lv_len)
12883 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012884 else
12885 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012886 item = list_find(l, before);
12887 if (item == NULL)
12888 {
12889 EMSGN(_(e_listidx), before);
12890 l = NULL;
12891 }
12892 }
12893 if (l != NULL)
12894 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012895 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012896 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012897 }
12898 }
12899}
12900
12901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 * "isdirectory()" function
12903 */
12904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012906 typval_T *argvars;
12907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012909 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012910}
12911
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012912/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012913 * "islocked()" function
12914 */
12915 static void
12916f_islocked(argvars, rettv)
12917 typval_T *argvars;
12918 typval_T *rettv;
12919{
12920 lval_T lv;
12921 char_u *end;
12922 dictitem_T *di;
12923
12924 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012925 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12926 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012927 if (end != NULL && lv.ll_name != NULL)
12928 {
12929 if (*end != NUL)
12930 EMSG(_(e_trailing));
12931 else
12932 {
12933 if (lv.ll_tv == NULL)
12934 {
12935 if (check_changedtick(lv.ll_name))
12936 rettv->vval.v_number = 1; /* always locked */
12937 else
12938 {
12939 di = find_var(lv.ll_name, NULL);
12940 if (di != NULL)
12941 {
12942 /* Consider a variable locked when:
12943 * 1. the variable itself is locked
12944 * 2. the value of the variable is locked.
12945 * 3. the List or Dict value is locked.
12946 */
12947 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12948 || tv_islocked(&di->di_tv));
12949 }
12950 }
12951 }
12952 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012953 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012954 else if (lv.ll_newkey != NULL)
12955 EMSG2(_(e_dictkey), lv.ll_newkey);
12956 else if (lv.ll_list != NULL)
12957 /* List item. */
12958 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12959 else
12960 /* Dictionary item. */
12961 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12962 }
12963 }
12964
12965 clear_lval(&lv);
12966}
12967
Bram Moolenaar33570922005-01-25 22:26:29 +000012968static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012969
12970/*
12971 * Turn a dict into a list:
12972 * "what" == 0: list of keys
12973 * "what" == 1: list of values
12974 * "what" == 2: list of items
12975 */
12976 static void
12977dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012978 typval_T *argvars;
12979 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012980 int what;
12981{
Bram Moolenaar33570922005-01-25 22:26:29 +000012982 list_T *l2;
12983 dictitem_T *di;
12984 hashitem_T *hi;
12985 listitem_T *li;
12986 listitem_T *li2;
12987 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012988 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012989
Bram Moolenaar8c711452005-01-14 21:53:12 +000012990 if (argvars[0].v_type != VAR_DICT)
12991 {
12992 EMSG(_(e_dictreq));
12993 return;
12994 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012995 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012996 return;
12997
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012998 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012999 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013000
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013001 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013002 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013003 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013004 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013005 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013006 --todo;
13007 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013008
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013009 li = listitem_alloc();
13010 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013011 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013012 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013013
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013014 if (what == 0)
13015 {
13016 /* keys() */
13017 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013018 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013019 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13020 }
13021 else if (what == 1)
13022 {
13023 /* values() */
13024 copy_tv(&di->di_tv, &li->li_tv);
13025 }
13026 else
13027 {
13028 /* items() */
13029 l2 = list_alloc();
13030 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013031 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013032 li->li_tv.vval.v_list = l2;
13033 if (l2 == NULL)
13034 break;
13035 ++l2->lv_refcount;
13036
13037 li2 = listitem_alloc();
13038 if (li2 == NULL)
13039 break;
13040 list_append(l2, li2);
13041 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013042 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013043 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13044
13045 li2 = listitem_alloc();
13046 if (li2 == NULL)
13047 break;
13048 list_append(l2, li2);
13049 copy_tv(&di->di_tv, &li2->li_tv);
13050 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013051 }
13052 }
13053}
13054
13055/*
13056 * "items(dict)" function
13057 */
13058 static void
13059f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013060 typval_T *argvars;
13061 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013062{
13063 dict_list(argvars, rettv, 2);
13064}
13065
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013067 * "join()" function
13068 */
13069 static void
13070f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013071 typval_T *argvars;
13072 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013073{
13074 garray_T ga;
13075 char_u *sep;
13076
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013077 if (argvars[0].v_type != VAR_LIST)
13078 {
13079 EMSG(_(e_listreq));
13080 return;
13081 }
13082 if (argvars[0].vval.v_list == NULL)
13083 return;
13084 if (argvars[1].v_type == VAR_UNKNOWN)
13085 sep = (char_u *)" ";
13086 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013087 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013088
13089 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013090
13091 if (sep != NULL)
13092 {
13093 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013094 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013095 ga_append(&ga, NUL);
13096 rettv->vval.v_string = (char_u *)ga.ga_data;
13097 }
13098 else
13099 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013100}
13101
13102/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013103 * "keys()" function
13104 */
13105 static void
13106f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013107 typval_T *argvars;
13108 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013109{
13110 dict_list(argvars, rettv, 0);
13111}
13112
13113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013114 * "last_buffer_nr()" function.
13115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013118 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013119 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120{
13121 int n = 0;
13122 buf_T *buf;
13123
13124 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13125 if (n < buf->b_fnum)
13126 n = buf->b_fnum;
13127
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013129}
13130
13131/*
13132 * "len()" function
13133 */
13134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013135f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013136 typval_T *argvars;
13137 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013138{
13139 switch (argvars[0].v_type)
13140 {
13141 case VAR_STRING:
13142 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013143 rettv->vval.v_number = (varnumber_T)STRLEN(
13144 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013145 break;
13146 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013148 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013149 case VAR_DICT:
13150 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13151 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013152 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013153 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013154 break;
13155 }
13156}
13157
Bram Moolenaar33570922005-01-25 22:26:29 +000013158static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013159
13160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013161libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013162 typval_T *argvars;
13163 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013164 int type;
13165{
13166#ifdef FEAT_LIBCALL
13167 char_u *string_in;
13168 char_u **string_result;
13169 int nr_result;
13170#endif
13171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013172 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013173 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013174 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013175
13176 if (check_restricted() || check_secure())
13177 return;
13178
13179#ifdef FEAT_LIBCALL
13180 /* The first two args must be strings, otherwise its meaningless */
13181 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13182 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013183 string_in = NULL;
13184 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013185 string_in = argvars[2].vval.v_string;
13186 if (type == VAR_NUMBER)
13187 string_result = NULL;
13188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013189 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013190 if (mch_libcall(argvars[0].vval.v_string,
13191 argvars[1].vval.v_string,
13192 string_in,
13193 argvars[2].vval.v_number,
13194 string_result,
13195 &nr_result) == OK
13196 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013197 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013198 }
13199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200}
13201
13202/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013203 * "libcall()" function
13204 */
13205 static void
13206f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 typval_T *argvars;
13208 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013209{
13210 libcall_common(argvars, rettv, VAR_STRING);
13211}
13212
13213/*
13214 * "libcallnr()" function
13215 */
13216 static void
13217f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013218 typval_T *argvars;
13219 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013220{
13221 libcall_common(argvars, rettv, VAR_NUMBER);
13222}
13223
13224/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 * "line(string)" function
13226 */
13227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013229 typval_T *argvars;
13230 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231{
13232 linenr_T lnum = 0;
13233 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013234 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013236 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 if (fp != NULL)
13238 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013239 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240}
13241
13242/*
13243 * "line2byte(lnum)" function
13244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013246f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013247 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013248 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249{
13250#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252#else
13253 linenr_T lnum;
13254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013255 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013259 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13260 if (rettv->vval.v_number >= 0)
13261 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262#endif
13263}
13264
13265/*
13266 * "lispindent(lnum)" function
13267 */
13268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013269f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013270 typval_T *argvars;
13271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272{
13273#ifdef FEAT_LISP
13274 pos_T pos;
13275 linenr_T lnum;
13276
13277 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013278 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13280 {
13281 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 curwin->w_cursor = pos;
13284 }
13285 else
13286#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013287 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288}
13289
13290/*
13291 * "localtime()" function
13292 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013294f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013295 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013298 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299}
13300
Bram Moolenaar33570922005-01-25 22:26:29 +000013301static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302
13303 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013304get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013305 typval_T *argvars;
13306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307 int exact;
13308{
13309 char_u *keys;
13310 char_u *which;
13311 char_u buf[NUMBUFLEN];
13312 char_u *keys_buf = NULL;
13313 char_u *rhs;
13314 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013315 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013316 int get_dict = FALSE;
13317 mapblock_T *mp;
13318 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319
13320 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321 rettv->v_type = VAR_STRING;
13322 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013324 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 if (*keys == NUL)
13326 return;
13327
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013328 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013330 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013331 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013332 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013333 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013334 if (argvars[3].v_type != VAR_UNKNOWN)
13335 get_dict = get_tv_number(&argvars[3]);
13336 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338 else
13339 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013340 if (which == NULL)
13341 return;
13342
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 mode = get_map_mode(&which, 0);
13344
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013345 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013346 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013348
13349 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013351 /* Return a string. */
13352 if (rhs != NULL)
13353 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013354
Bram Moolenaarbd743252010-10-20 21:23:33 +020013355 }
13356 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13357 {
13358 /* Return a dictionary. */
13359 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13360 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13361 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362
Bram Moolenaarbd743252010-10-20 21:23:33 +020013363 dict_add_nr_str(dict, "lhs", 0L, lhs);
13364 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13365 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13366 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13367 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13368 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13369 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13370 dict_add_nr_str(dict, "mode", 0L, mapmode);
13371
13372 vim_free(lhs);
13373 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013374 }
13375}
13376
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013377#ifdef FEAT_FLOAT
13378/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013379 * "log()" function
13380 */
13381 static void
13382f_log(argvars, rettv)
13383 typval_T *argvars;
13384 typval_T *rettv;
13385{
13386 float_T f;
13387
13388 rettv->v_type = VAR_FLOAT;
13389 if (get_float_arg(argvars, &f) == OK)
13390 rettv->vval.v_float = log(f);
13391 else
13392 rettv->vval.v_float = 0.0;
13393}
13394
13395/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013396 * "log10()" function
13397 */
13398 static void
13399f_log10(argvars, rettv)
13400 typval_T *argvars;
13401 typval_T *rettv;
13402{
13403 float_T f;
13404
13405 rettv->v_type = VAR_FLOAT;
13406 if (get_float_arg(argvars, &f) == OK)
13407 rettv->vval.v_float = log10(f);
13408 else
13409 rettv->vval.v_float = 0.0;
13410}
13411#endif
13412
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013414 * "map()" function
13415 */
13416 static void
13417f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013418 typval_T *argvars;
13419 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013420{
13421 filter_map(argvars, rettv, TRUE);
13422}
13423
13424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013425 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426 */
13427 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013428f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013429 typval_T *argvars;
13430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013432 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433}
13434
13435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 */
13438 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013439f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013440 typval_T *argvars;
13441 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013443 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444}
13445
Bram Moolenaar33570922005-01-25 22:26:29 +000013446static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447
13448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013450 typval_T *argvars;
13451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 int type;
13453{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013454 char_u *str = NULL;
13455 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 char_u *pat;
13457 regmatch_T regmatch;
13458 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013459 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 char_u *save_cpo;
13461 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013462 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013463 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013464 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013465 list_T *l = NULL;
13466 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013467 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013468 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013469
13470 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13471 save_cpo = p_cpo;
13472 p_cpo = (char_u *)"";
13473
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013474 rettv->vval.v_number = -1;
13475 if (type == 3)
13476 {
13477 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013478 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013479 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013480 }
13481 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013483 rettv->v_type = VAR_STRING;
13484 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013487 if (argvars[0].v_type == VAR_LIST)
13488 {
13489 if ((l = argvars[0].vval.v_list) == NULL)
13490 goto theend;
13491 li = l->lv_first;
13492 }
13493 else
13494 expr = str = get_tv_string(&argvars[0]);
13495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013496 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13497 if (pat == NULL)
13498 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013499
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013500 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013502 int error = FALSE;
13503
13504 start = get_tv_number_chk(&argvars[2], &error);
13505 if (error)
13506 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013507 if (l != NULL)
13508 {
13509 li = list_find(l, start);
13510 if (li == NULL)
13511 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013512 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013513 }
13514 else
13515 {
13516 if (start < 0)
13517 start = 0;
13518 if (start > (long)STRLEN(str))
13519 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013520 /* When "count" argument is there ignore matches before "start",
13521 * otherwise skip part of the string. Differs when pattern is "^"
13522 * or "\<". */
13523 if (argvars[3].v_type != VAR_UNKNOWN)
13524 startcol = start;
13525 else
13526 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013527 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013528
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013529 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013530 nth = get_tv_number_chk(&argvars[3], &error);
13531 if (error)
13532 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 }
13534
13535 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13536 if (regmatch.regprog != NULL)
13537 {
13538 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013539
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013540 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013541 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013542 if (l != NULL)
13543 {
13544 if (li == NULL)
13545 {
13546 match = FALSE;
13547 break;
13548 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013549 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013550 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013551 if (str == NULL)
13552 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013553 }
13554
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013555 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013556
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013557 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013558 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013559 if (l == NULL && !match)
13560 break;
13561
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013562 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013563 if (l != NULL)
13564 {
13565 li = li->li_next;
13566 ++idx;
13567 }
13568 else
13569 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013570#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013571 startcol = (colnr_T)(regmatch.startp[0]
13572 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013573#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013574 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013575#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013576 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013577 }
13578
13579 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013581 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013582 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013583 int i;
13584
13585 /* return list with matched string and submatches */
13586 for (i = 0; i < NSUBEXP; ++i)
13587 {
13588 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013589 {
13590 if (list_append_string(rettv->vval.v_list,
13591 (char_u *)"", 0) == FAIL)
13592 break;
13593 }
13594 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013595 regmatch.startp[i],
13596 (int)(regmatch.endp[i] - regmatch.startp[i]))
13597 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013598 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013599 }
13600 }
13601 else if (type == 2)
13602 {
13603 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013604 if (l != NULL)
13605 copy_tv(&li->li_tv, rettv);
13606 else
13607 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013609 }
13610 else if (l != NULL)
13611 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612 else
13613 {
13614 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 (varnumber_T)(regmatch.startp[0] - str);
13617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013618 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013620 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 }
13622 }
13623 vim_free(regmatch.regprog);
13624 }
13625
13626theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013627 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628 p_cpo = save_cpo;
13629}
13630
13631/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013632 * "match()" function
13633 */
13634 static void
13635f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013636 typval_T *argvars;
13637 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638{
13639 find_some_match(argvars, rettv, 1);
13640}
13641
13642/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013643 * "matchadd()" function
13644 */
13645 static void
13646f_matchadd(argvars, rettv)
13647 typval_T *argvars;
13648 typval_T *rettv;
13649{
13650#ifdef FEAT_SEARCH_EXTRA
13651 char_u buf[NUMBUFLEN];
13652 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13653 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13654 int prio = 10; /* default priority */
13655 int id = -1;
13656 int error = FALSE;
13657
13658 rettv->vval.v_number = -1;
13659
13660 if (grp == NULL || pat == NULL)
13661 return;
13662 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013663 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013664 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013665 if (argvars[3].v_type != VAR_UNKNOWN)
13666 id = get_tv_number_chk(&argvars[3], &error);
13667 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013668 if (error == TRUE)
13669 return;
13670 if (id >= 1 && id <= 3)
13671 {
13672 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13673 return;
13674 }
13675
13676 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13677#endif
13678}
13679
13680/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013681 * "matcharg()" function
13682 */
13683 static void
13684f_matcharg(argvars, rettv)
13685 typval_T *argvars;
13686 typval_T *rettv;
13687{
13688 if (rettv_list_alloc(rettv) == OK)
13689 {
13690#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013691 int id = get_tv_number(&argvars[0]);
13692 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013693
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013694 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013695 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013696 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13697 {
13698 list_append_string(rettv->vval.v_list,
13699 syn_id2name(m->hlg_id), -1);
13700 list_append_string(rettv->vval.v_list, m->pattern, -1);
13701 }
13702 else
13703 {
13704 list_append_string(rettv->vval.v_list, NUL, -1);
13705 list_append_string(rettv->vval.v_list, NUL, -1);
13706 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013707 }
13708#endif
13709 }
13710}
13711
13712/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013713 * "matchdelete()" function
13714 */
13715 static void
13716f_matchdelete(argvars, rettv)
13717 typval_T *argvars;
13718 typval_T *rettv;
13719{
13720#ifdef FEAT_SEARCH_EXTRA
13721 rettv->vval.v_number = match_delete(curwin,
13722 (int)get_tv_number(&argvars[0]), TRUE);
13723#endif
13724}
13725
13726/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013727 * "matchend()" function
13728 */
13729 static void
13730f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013731 typval_T *argvars;
13732 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013733{
13734 find_some_match(argvars, rettv, 0);
13735}
13736
13737/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013738 * "matchlist()" function
13739 */
13740 static void
13741f_matchlist(argvars, rettv)
13742 typval_T *argvars;
13743 typval_T *rettv;
13744{
13745 find_some_match(argvars, rettv, 3);
13746}
13747
13748/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013749 * "matchstr()" function
13750 */
13751 static void
13752f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013753 typval_T *argvars;
13754 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013755{
13756 find_some_match(argvars, rettv, 2);
13757}
13758
Bram Moolenaar33570922005-01-25 22:26:29 +000013759static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013760
13761 static void
13762max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013763 typval_T *argvars;
13764 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013765 int domax;
13766{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013767 long n = 0;
13768 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013770
13771 if (argvars[0].v_type == VAR_LIST)
13772 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013773 list_T *l;
13774 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013775
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013776 l = argvars[0].vval.v_list;
13777 if (l != NULL)
13778 {
13779 li = l->lv_first;
13780 if (li != NULL)
13781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013782 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013783 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013784 {
13785 li = li->li_next;
13786 if (li == NULL)
13787 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013788 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013789 if (domax ? i > n : i < n)
13790 n = i;
13791 }
13792 }
13793 }
13794 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013795 else if (argvars[0].v_type == VAR_DICT)
13796 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013798 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013799 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013800 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013801
13802 d = argvars[0].vval.v_dict;
13803 if (d != NULL)
13804 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013805 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013806 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013807 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013808 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013809 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013810 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013811 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013812 if (first)
13813 {
13814 n = i;
13815 first = FALSE;
13816 }
13817 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013818 n = i;
13819 }
13820 }
13821 }
13822 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013823 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013824 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013825 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013826}
13827
13828/*
13829 * "max()" function
13830 */
13831 static void
13832f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013833 typval_T *argvars;
13834 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013835{
13836 max_min(argvars, rettv, TRUE);
13837}
13838
13839/*
13840 * "min()" function
13841 */
13842 static void
13843f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013844 typval_T *argvars;
13845 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013846{
13847 max_min(argvars, rettv, FALSE);
13848}
13849
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013850static int mkdir_recurse __ARGS((char_u *dir, int prot));
13851
13852/*
13853 * Create the directory in which "dir" is located, and higher levels when
13854 * needed.
13855 */
13856 static int
13857mkdir_recurse(dir, prot)
13858 char_u *dir;
13859 int prot;
13860{
13861 char_u *p;
13862 char_u *updir;
13863 int r = FAIL;
13864
13865 /* Get end of directory name in "dir".
13866 * We're done when it's "/" or "c:/". */
13867 p = gettail_sep(dir);
13868 if (p <= get_past_head(dir))
13869 return OK;
13870
13871 /* If the directory exists we're done. Otherwise: create it.*/
13872 updir = vim_strnsave(dir, (int)(p - dir));
13873 if (updir == NULL)
13874 return FAIL;
13875 if (mch_isdir(updir))
13876 r = OK;
13877 else if (mkdir_recurse(updir, prot) == OK)
13878 r = vim_mkdir_emsg(updir, prot);
13879 vim_free(updir);
13880 return r;
13881}
13882
13883#ifdef vim_mkdir
13884/*
13885 * "mkdir()" function
13886 */
13887 static void
13888f_mkdir(argvars, rettv)
13889 typval_T *argvars;
13890 typval_T *rettv;
13891{
13892 char_u *dir;
13893 char_u buf[NUMBUFLEN];
13894 int prot = 0755;
13895
13896 rettv->vval.v_number = FAIL;
13897 if (check_restricted() || check_secure())
13898 return;
13899
13900 dir = get_tv_string_buf(&argvars[0], buf);
13901 if (argvars[1].v_type != VAR_UNKNOWN)
13902 {
13903 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013904 prot = get_tv_number_chk(&argvars[2], NULL);
13905 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013906 mkdir_recurse(dir, prot);
13907 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013908 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013909}
13910#endif
13911
Bram Moolenaar0d660222005-01-07 21:51:51 +000013912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 * "mode()" function
13914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013916f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013917 typval_T *argvars;
13918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013920 char_u buf[3];
13921
13922 buf[1] = NUL;
13923 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924
13925#ifdef FEAT_VISUAL
13926 if (VIsual_active)
13927 {
13928 if (VIsual_select)
13929 buf[0] = VIsual_mode + 's' - 'v';
13930 else
13931 buf[0] = VIsual_mode;
13932 }
13933 else
13934#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013935 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13936 || State == CONFIRM)
13937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013939 if (State == ASKMORE)
13940 buf[1] = 'm';
13941 else if (State == CONFIRM)
13942 buf[1] = '?';
13943 }
13944 else if (State == EXTERNCMD)
13945 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946 else if (State & INSERT)
13947 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013948#ifdef FEAT_VREPLACE
13949 if (State & VREPLACE_FLAG)
13950 {
13951 buf[0] = 'R';
13952 buf[1] = 'v';
13953 }
13954 else
13955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956 if (State & REPLACE_FLAG)
13957 buf[0] = 'R';
13958 else
13959 buf[0] = 'i';
13960 }
13961 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013964 if (exmode_active)
13965 buf[1] = 'v';
13966 }
13967 else if (exmode_active)
13968 {
13969 buf[0] = 'c';
13970 buf[1] = 'e';
13971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013975 if (finish_op)
13976 buf[1] = 'o';
13977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013978
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013979 /* Clear out the minor mode when the argument is not a non-zero number or
13980 * non-empty string. */
13981 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013982 buf[1] = NUL;
13983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013984 rettv->vval.v_string = vim_strsave(buf);
13985 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986}
13987
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013988#ifdef FEAT_MZSCHEME
13989/*
13990 * "mzeval()" function
13991 */
13992 static void
13993f_mzeval(argvars, rettv)
13994 typval_T *argvars;
13995 typval_T *rettv;
13996{
13997 char_u *str;
13998 char_u buf[NUMBUFLEN];
13999
14000 str = get_tv_string_buf(&argvars[0], buf);
14001 do_mzeval(str, rettv);
14002}
14003#endif
14004
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014006 * "nextnonblank()" function
14007 */
14008 static void
14009f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014010 typval_T *argvars;
14011 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014012{
14013 linenr_T lnum;
14014
14015 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14016 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014017 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014018 {
14019 lnum = 0;
14020 break;
14021 }
14022 if (*skipwhite(ml_get(lnum)) != NUL)
14023 break;
14024 }
14025 rettv->vval.v_number = lnum;
14026}
14027
14028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 * "nr2char()" function
14030 */
14031 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014033 typval_T *argvars;
14034 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035{
14036 char_u buf[NUMBUFLEN];
14037
14038#ifdef FEAT_MBYTE
14039 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014040 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 else
14042#endif
14043 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045 buf[1] = NUL;
14046 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->v_type = VAR_STRING;
14048 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014049}
14050
14051/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014052 * "pathshorten()" function
14053 */
14054 static void
14055f_pathshorten(argvars, rettv)
14056 typval_T *argvars;
14057 typval_T *rettv;
14058{
14059 char_u *p;
14060
14061 rettv->v_type = VAR_STRING;
14062 p = get_tv_string_chk(&argvars[0]);
14063 if (p == NULL)
14064 rettv->vval.v_string = NULL;
14065 else
14066 {
14067 p = vim_strsave(p);
14068 rettv->vval.v_string = p;
14069 if (p != NULL)
14070 shorten_dir(p);
14071 }
14072}
14073
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014074#ifdef FEAT_FLOAT
14075/*
14076 * "pow()" function
14077 */
14078 static void
14079f_pow(argvars, rettv)
14080 typval_T *argvars;
14081 typval_T *rettv;
14082{
14083 float_T fx, fy;
14084
14085 rettv->v_type = VAR_FLOAT;
14086 if (get_float_arg(argvars, &fx) == OK
14087 && get_float_arg(&argvars[1], &fy) == OK)
14088 rettv->vval.v_float = pow(fx, fy);
14089 else
14090 rettv->vval.v_float = 0.0;
14091}
14092#endif
14093
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014094/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014095 * "prevnonblank()" function
14096 */
14097 static void
14098f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014099 typval_T *argvars;
14100 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014101{
14102 linenr_T lnum;
14103
14104 lnum = get_tv_lnum(argvars);
14105 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14106 lnum = 0;
14107 else
14108 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14109 --lnum;
14110 rettv->vval.v_number = lnum;
14111}
14112
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014113#ifdef HAVE_STDARG_H
14114/* This dummy va_list is here because:
14115 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14116 * - locally in the function results in a "used before set" warning
14117 * - using va_start() to initialize it gives "function with fixed args" error */
14118static va_list ap;
14119#endif
14120
Bram Moolenaar8c711452005-01-14 21:53:12 +000014121/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014122 * "printf()" function
14123 */
14124 static void
14125f_printf(argvars, rettv)
14126 typval_T *argvars;
14127 typval_T *rettv;
14128{
14129 rettv->v_type = VAR_STRING;
14130 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014131#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014132 {
14133 char_u buf[NUMBUFLEN];
14134 int len;
14135 char_u *s;
14136 int saved_did_emsg = did_emsg;
14137 char *fmt;
14138
14139 /* Get the required length, allocate the buffer and do it for real. */
14140 did_emsg = FALSE;
14141 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014142 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014143 if (!did_emsg)
14144 {
14145 s = alloc(len + 1);
14146 if (s != NULL)
14147 {
14148 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014149 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014150 }
14151 }
14152 did_emsg |= saved_did_emsg;
14153 }
14154#endif
14155}
14156
14157/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014158 * "pumvisible()" function
14159 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014160 static void
14161f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014162 typval_T *argvars UNUSED;
14163 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014164{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014165#ifdef FEAT_INS_EXPAND
14166 if (pum_visible())
14167 rettv->vval.v_number = 1;
14168#endif
14169}
14170
14171/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014172 * "range()" function
14173 */
14174 static void
14175f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 typval_T *argvars;
14177 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014178{
14179 long start;
14180 long end;
14181 long stride = 1;
14182 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014183 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014185 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014186 if (argvars[1].v_type == VAR_UNKNOWN)
14187 {
14188 end = start - 1;
14189 start = 0;
14190 }
14191 else
14192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014193 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014194 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014196 }
14197
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198 if (error)
14199 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014200 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014201 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014202 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014203 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014204 else
14205 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014206 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014207 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014208 if (list_append_number(rettv->vval.v_list,
14209 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014210 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014211 }
14212}
14213
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014214/*
14215 * "readfile()" function
14216 */
14217 static void
14218f_readfile(argvars, rettv)
14219 typval_T *argvars;
14220 typval_T *rettv;
14221{
14222 int binary = FALSE;
14223 char_u *fname;
14224 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014225 listitem_T *li;
14226#define FREAD_SIZE 200 /* optimized for text lines */
14227 char_u buf[FREAD_SIZE];
14228 int readlen; /* size of last fread() */
14229 int buflen; /* nr of valid chars in buf[] */
14230 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14231 int tolist; /* first byte in buf[] still to be put in list */
14232 int chop; /* how many CR to chop off */
14233 char_u *prev = NULL; /* previously read bytes, if any */
14234 int prevlen = 0; /* length of "prev" if not NULL */
14235 char_u *s;
14236 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014237 long maxline = MAXLNUM;
14238 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014239
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014240 if (argvars[1].v_type != VAR_UNKNOWN)
14241 {
14242 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14243 binary = TRUE;
14244 if (argvars[2].v_type != VAR_UNKNOWN)
14245 maxline = get_tv_number(&argvars[2]);
14246 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014247
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014248 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014249 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014250
14251 /* Always open the file in binary mode, library functions have a mind of
14252 * their own about CR-LF conversion. */
14253 fname = get_tv_string(&argvars[0]);
14254 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14255 {
14256 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14257 return;
14258 }
14259
14260 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014261 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014262 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014263 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014264 buflen = filtd + readlen;
14265 tolist = 0;
14266 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14267 {
14268 if (buf[filtd] == '\n' || readlen <= 0)
14269 {
14270 /* Only when in binary mode add an empty list item when the
14271 * last line ends in a '\n'. */
14272 if (!binary && readlen == 0 && filtd == 0)
14273 break;
14274
14275 /* Found end-of-line or end-of-file: add a text line to the
14276 * list. */
14277 chop = 0;
14278 if (!binary)
14279 while (filtd - chop - 1 >= tolist
14280 && buf[filtd - chop - 1] == '\r')
14281 ++chop;
14282 len = filtd - tolist - chop;
14283 if (prev == NULL)
14284 s = vim_strnsave(buf + tolist, len);
14285 else
14286 {
14287 s = alloc((unsigned)(prevlen + len + 1));
14288 if (s != NULL)
14289 {
14290 mch_memmove(s, prev, prevlen);
14291 vim_free(prev);
14292 prev = NULL;
14293 mch_memmove(s + prevlen, buf + tolist, len);
14294 s[prevlen + len] = NUL;
14295 }
14296 }
14297 tolist = filtd + 1;
14298
14299 li = listitem_alloc();
14300 if (li == NULL)
14301 {
14302 vim_free(s);
14303 break;
14304 }
14305 li->li_tv.v_type = VAR_STRING;
14306 li->li_tv.v_lock = 0;
14307 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014308 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014309
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014310 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014311 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014312 if (readlen <= 0)
14313 break;
14314 }
14315 else if (buf[filtd] == NUL)
14316 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014317#ifdef FEAT_MBYTE
14318 else if (buf[filtd] == 0xef
14319 && enc_utf8
14320 && filtd + 2 < buflen
14321 && !binary
14322 && buf[filtd + 1] == 0xbb
14323 && buf[filtd + 2] == 0xbf)
14324 {
14325 /* remove utf-8 byte order mark */
14326 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14327 --filtd;
14328 buflen -= 3;
14329 }
14330#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014331 }
14332 if (readlen <= 0)
14333 break;
14334
14335 if (tolist == 0)
14336 {
14337 /* "buf" is full, need to move text to an allocated buffer */
14338 if (prev == NULL)
14339 {
14340 prev = vim_strnsave(buf, buflen);
14341 prevlen = buflen;
14342 }
14343 else
14344 {
14345 s = alloc((unsigned)(prevlen + buflen));
14346 if (s != NULL)
14347 {
14348 mch_memmove(s, prev, prevlen);
14349 mch_memmove(s + prevlen, buf, buflen);
14350 vim_free(prev);
14351 prev = s;
14352 prevlen += buflen;
14353 }
14354 }
14355 filtd = 0;
14356 }
14357 else
14358 {
14359 mch_memmove(buf, buf + tolist, buflen - tolist);
14360 filtd -= tolist;
14361 }
14362 }
14363
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014364 /*
14365 * For a negative line count use only the lines at the end of the file,
14366 * free the rest.
14367 */
14368 if (maxline < 0)
14369 while (cnt > -maxline)
14370 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014371 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014372 --cnt;
14373 }
14374
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014375 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014376 fclose(fd);
14377}
14378
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014379#if defined(FEAT_RELTIME)
14380static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14381
14382/*
14383 * Convert a List to proftime_T.
14384 * Return FAIL when there is something wrong.
14385 */
14386 static int
14387list2proftime(arg, tm)
14388 typval_T *arg;
14389 proftime_T *tm;
14390{
14391 long n1, n2;
14392 int error = FALSE;
14393
14394 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14395 || arg->vval.v_list->lv_len != 2)
14396 return FAIL;
14397 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14398 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14399# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014400 tm->HighPart = n1;
14401 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014402# else
14403 tm->tv_sec = n1;
14404 tm->tv_usec = n2;
14405# endif
14406 return error ? FAIL : OK;
14407}
14408#endif /* FEAT_RELTIME */
14409
14410/*
14411 * "reltime()" function
14412 */
14413 static void
14414f_reltime(argvars, rettv)
14415 typval_T *argvars;
14416 typval_T *rettv;
14417{
14418#ifdef FEAT_RELTIME
14419 proftime_T res;
14420 proftime_T start;
14421
14422 if (argvars[0].v_type == VAR_UNKNOWN)
14423 {
14424 /* No arguments: get current time. */
14425 profile_start(&res);
14426 }
14427 else if (argvars[1].v_type == VAR_UNKNOWN)
14428 {
14429 if (list2proftime(&argvars[0], &res) == FAIL)
14430 return;
14431 profile_end(&res);
14432 }
14433 else
14434 {
14435 /* Two arguments: compute the difference. */
14436 if (list2proftime(&argvars[0], &start) == FAIL
14437 || list2proftime(&argvars[1], &res) == FAIL)
14438 return;
14439 profile_sub(&res, &start);
14440 }
14441
14442 if (rettv_list_alloc(rettv) == OK)
14443 {
14444 long n1, n2;
14445
14446# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014447 n1 = res.HighPart;
14448 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014449# else
14450 n1 = res.tv_sec;
14451 n2 = res.tv_usec;
14452# endif
14453 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14454 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14455 }
14456#endif
14457}
14458
14459/*
14460 * "reltimestr()" function
14461 */
14462 static void
14463f_reltimestr(argvars, rettv)
14464 typval_T *argvars;
14465 typval_T *rettv;
14466{
14467#ifdef FEAT_RELTIME
14468 proftime_T tm;
14469#endif
14470
14471 rettv->v_type = VAR_STRING;
14472 rettv->vval.v_string = NULL;
14473#ifdef FEAT_RELTIME
14474 if (list2proftime(&argvars[0], &tm) == OK)
14475 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14476#endif
14477}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014478
Bram Moolenaar0d660222005-01-07 21:51:51 +000014479#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14480static void make_connection __ARGS((void));
14481static int check_connection __ARGS((void));
14482
14483 static void
14484make_connection()
14485{
14486 if (X_DISPLAY == NULL
14487# ifdef FEAT_GUI
14488 && !gui.in_use
14489# endif
14490 )
14491 {
14492 x_force_connect = TRUE;
14493 setup_term_clip();
14494 x_force_connect = FALSE;
14495 }
14496}
14497
14498 static int
14499check_connection()
14500{
14501 make_connection();
14502 if (X_DISPLAY == NULL)
14503 {
14504 EMSG(_("E240: No connection to Vim server"));
14505 return FAIL;
14506 }
14507 return OK;
14508}
14509#endif
14510
14511#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014512static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014513
14514 static void
14515remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014516 typval_T *argvars;
14517 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014518 int expr;
14519{
14520 char_u *server_name;
14521 char_u *keys;
14522 char_u *r = NULL;
14523 char_u buf[NUMBUFLEN];
14524# ifdef WIN32
14525 HWND w;
14526# else
14527 Window w;
14528# endif
14529
14530 if (check_restricted() || check_secure())
14531 return;
14532
14533# ifdef FEAT_X11
14534 if (check_connection() == FAIL)
14535 return;
14536# endif
14537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014538 server_name = get_tv_string_chk(&argvars[0]);
14539 if (server_name == NULL)
14540 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014541 keys = get_tv_string_buf(&argvars[1], buf);
14542# ifdef WIN32
14543 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14544# else
14545 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14546 < 0)
14547# endif
14548 {
14549 if (r != NULL)
14550 EMSG(r); /* sending worked but evaluation failed */
14551 else
14552 EMSG2(_("E241: Unable to send to %s"), server_name);
14553 return;
14554 }
14555
14556 rettv->vval.v_string = r;
14557
14558 if (argvars[2].v_type != VAR_UNKNOWN)
14559 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014560 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014561 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014562 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014563
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014564 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014565 v.di_tv.v_type = VAR_STRING;
14566 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 idvar = get_tv_string_chk(&argvars[2]);
14568 if (idvar != NULL)
14569 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014570 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014571 }
14572}
14573#endif
14574
14575/*
14576 * "remote_expr()" function
14577 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014578 static void
14579f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014580 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014581 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014582{
14583 rettv->v_type = VAR_STRING;
14584 rettv->vval.v_string = NULL;
14585#ifdef FEAT_CLIENTSERVER
14586 remote_common(argvars, rettv, TRUE);
14587#endif
14588}
14589
14590/*
14591 * "remote_foreground()" function
14592 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014593 static void
14594f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014595 typval_T *argvars UNUSED;
14596 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014597{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014598#ifdef FEAT_CLIENTSERVER
14599# ifdef WIN32
14600 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 {
14602 char_u *server_name = get_tv_string_chk(&argvars[0]);
14603
14604 if (server_name != NULL)
14605 serverForeground(server_name);
14606 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014607# else
14608 /* Send a foreground() expression to the server. */
14609 argvars[1].v_type = VAR_STRING;
14610 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14611 argvars[2].v_type = VAR_UNKNOWN;
14612 remote_common(argvars, rettv, TRUE);
14613 vim_free(argvars[1].vval.v_string);
14614# endif
14615#endif
14616}
14617
Bram Moolenaar0d660222005-01-07 21:51:51 +000014618 static void
14619f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014620 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622{
14623#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014624 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625 char_u *s = NULL;
14626# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014627 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014628# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014629 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014630
14631 if (check_restricted() || check_secure())
14632 {
14633 rettv->vval.v_number = -1;
14634 return;
14635 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 serverid = get_tv_string_chk(&argvars[0]);
14637 if (serverid == NULL)
14638 {
14639 rettv->vval.v_number = -1;
14640 return; /* type error; errmsg already given */
14641 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014642# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014643 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014644 if (n == 0)
14645 rettv->vval.v_number = -1;
14646 else
14647 {
14648 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14649 rettv->vval.v_number = (s != NULL);
14650 }
14651# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014652 if (check_connection() == FAIL)
14653 return;
14654
14655 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014657# endif
14658
14659 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 char_u *retvar;
14662
Bram Moolenaar33570922005-01-25 22:26:29 +000014663 v.di_tv.v_type = VAR_STRING;
14664 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014665 retvar = get_tv_string_chk(&argvars[1]);
14666 if (retvar != NULL)
14667 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014668 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014669 }
14670#else
14671 rettv->vval.v_number = -1;
14672#endif
14673}
14674
Bram Moolenaar0d660222005-01-07 21:51:51 +000014675 static void
14676f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014677 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014678 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014679{
14680 char_u *r = NULL;
14681
14682#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014683 char_u *serverid = get_tv_string_chk(&argvars[0]);
14684
14685 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014686 {
14687# ifdef WIN32
14688 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014689 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014690
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014691 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014692 if (n != 0)
14693 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14694 if (r == NULL)
14695# else
14696 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014697 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014698# endif
14699 EMSG(_("E277: Unable to read a server reply"));
14700 }
14701#endif
14702 rettv->v_type = VAR_STRING;
14703 rettv->vval.v_string = r;
14704}
14705
14706/*
14707 * "remote_send()" function
14708 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014709 static void
14710f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014711 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014712 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014713{
14714 rettv->v_type = VAR_STRING;
14715 rettv->vval.v_string = NULL;
14716#ifdef FEAT_CLIENTSERVER
14717 remote_common(argvars, rettv, FALSE);
14718#endif
14719}
14720
14721/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014722 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014723 */
14724 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014725f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014726 typval_T *argvars;
14727 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014728{
Bram Moolenaar33570922005-01-25 22:26:29 +000014729 list_T *l;
14730 listitem_T *item, *item2;
14731 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014732 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014733 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014734 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014735 dict_T *d;
14736 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014737
Bram Moolenaar8c711452005-01-14 21:53:12 +000014738 if (argvars[0].v_type == VAR_DICT)
14739 {
14740 if (argvars[2].v_type != VAR_UNKNOWN)
14741 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014742 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014743 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014745 key = get_tv_string_chk(&argvars[1]);
14746 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014748 di = dict_find(d, key, -1);
14749 if (di == NULL)
14750 EMSG2(_(e_dictkey), key);
14751 else
14752 {
14753 *rettv = di->di_tv;
14754 init_tv(&di->di_tv);
14755 dictitem_remove(d, di);
14756 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014758 }
14759 }
14760 else if (argvars[0].v_type != VAR_LIST)
14761 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014762 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014763 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014765 int error = FALSE;
14766
14767 idx = get_tv_number_chk(&argvars[1], &error);
14768 if (error)
14769 ; /* type error: do nothing, errmsg already given */
14770 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014771 EMSGN(_(e_listidx), idx);
14772 else
14773 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014774 if (argvars[2].v_type == VAR_UNKNOWN)
14775 {
14776 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014777 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014778 *rettv = item->li_tv;
14779 vim_free(item);
14780 }
14781 else
14782 {
14783 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014784 end = get_tv_number_chk(&argvars[2], &error);
14785 if (error)
14786 ; /* type error: do nothing */
14787 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014788 EMSGN(_(e_listidx), end);
14789 else
14790 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014791 int cnt = 0;
14792
14793 for (li = item; li != NULL; li = li->li_next)
14794 {
14795 ++cnt;
14796 if (li == item2)
14797 break;
14798 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014799 if (li == NULL) /* didn't find "item2" after "item" */
14800 EMSG(_(e_invrange));
14801 else
14802 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014803 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014804 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014805 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014806 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014807 l->lv_first = item;
14808 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014809 item->li_prev = NULL;
14810 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014811 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014812 }
14813 }
14814 }
14815 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014816 }
14817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818}
14819
14820/*
14821 * "rename({from}, {to})" function
14822 */
14823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014824f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014825 typval_T *argvars;
14826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827{
14828 char_u buf[NUMBUFLEN];
14829
14830 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014831 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014833 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14834 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835}
14836
14837/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014838 * "repeat()" function
14839 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014840 static void
14841f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014842 typval_T *argvars;
14843 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014844{
14845 char_u *p;
14846 int n;
14847 int slen;
14848 int len;
14849 char_u *r;
14850 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014851
14852 n = get_tv_number(&argvars[1]);
14853 if (argvars[0].v_type == VAR_LIST)
14854 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014855 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014856 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014857 if (list_extend(rettv->vval.v_list,
14858 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014859 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014860 }
14861 else
14862 {
14863 p = get_tv_string(&argvars[0]);
14864 rettv->v_type = VAR_STRING;
14865 rettv->vval.v_string = NULL;
14866
14867 slen = (int)STRLEN(p);
14868 len = slen * n;
14869 if (len <= 0)
14870 return;
14871
14872 r = alloc(len + 1);
14873 if (r != NULL)
14874 {
14875 for (i = 0; i < n; i++)
14876 mch_memmove(r + i * slen, p, (size_t)slen);
14877 r[len] = NUL;
14878 }
14879
14880 rettv->vval.v_string = r;
14881 }
14882}
14883
14884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885 * "resolve()" function
14886 */
14887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014889 typval_T *argvars;
14890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014891{
14892 char_u *p;
14893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895#ifdef FEAT_SHORTCUT
14896 {
14897 char_u *v = NULL;
14898
14899 v = mch_resolve_shortcut(p);
14900 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014901 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014903 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 }
14905#else
14906# ifdef HAVE_READLINK
14907 {
14908 char_u buf[MAXPATHL + 1];
14909 char_u *cpy;
14910 int len;
14911 char_u *remain = NULL;
14912 char_u *q;
14913 int is_relative_to_current = FALSE;
14914 int has_trailing_pathsep = FALSE;
14915 int limit = 100;
14916
14917 p = vim_strsave(p);
14918
14919 if (p[0] == '.' && (vim_ispathsep(p[1])
14920 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14921 is_relative_to_current = TRUE;
14922
14923 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014924 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 has_trailing_pathsep = TRUE;
14926
14927 q = getnextcomp(p);
14928 if (*q != NUL)
14929 {
14930 /* Separate the first path component in "p", and keep the
14931 * remainder (beginning with the path separator). */
14932 remain = vim_strsave(q - 1);
14933 q[-1] = NUL;
14934 }
14935
14936 for (;;)
14937 {
14938 for (;;)
14939 {
14940 len = readlink((char *)p, (char *)buf, MAXPATHL);
14941 if (len <= 0)
14942 break;
14943 buf[len] = NUL;
14944
14945 if (limit-- == 0)
14946 {
14947 vim_free(p);
14948 vim_free(remain);
14949 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014950 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 goto fail;
14952 }
14953
14954 /* Ensure that the result will have a trailing path separator
14955 * if the argument has one. */
14956 if (remain == NULL && has_trailing_pathsep)
14957 add_pathsep(buf);
14958
14959 /* Separate the first path component in the link value and
14960 * concatenate the remainders. */
14961 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14962 if (*q != NUL)
14963 {
14964 if (remain == NULL)
14965 remain = vim_strsave(q - 1);
14966 else
14967 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014968 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969 if (cpy != NULL)
14970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 vim_free(remain);
14972 remain = cpy;
14973 }
14974 }
14975 q[-1] = NUL;
14976 }
14977
14978 q = gettail(p);
14979 if (q > p && *q == NUL)
14980 {
14981 /* Ignore trailing path separator. */
14982 q[-1] = NUL;
14983 q = gettail(p);
14984 }
14985 if (q > p && !mch_isFullName(buf))
14986 {
14987 /* symlink is relative to directory of argument */
14988 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14989 if (cpy != NULL)
14990 {
14991 STRCPY(cpy, p);
14992 STRCPY(gettail(cpy), buf);
14993 vim_free(p);
14994 p = cpy;
14995 }
14996 }
14997 else
14998 {
14999 vim_free(p);
15000 p = vim_strsave(buf);
15001 }
15002 }
15003
15004 if (remain == NULL)
15005 break;
15006
15007 /* Append the first path component of "remain" to "p". */
15008 q = getnextcomp(remain + 1);
15009 len = q - remain - (*q != NUL);
15010 cpy = vim_strnsave(p, STRLEN(p) + len);
15011 if (cpy != NULL)
15012 {
15013 STRNCAT(cpy, remain, len);
15014 vim_free(p);
15015 p = cpy;
15016 }
15017 /* Shorten "remain". */
15018 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015019 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 else
15021 {
15022 vim_free(remain);
15023 remain = NULL;
15024 }
15025 }
15026
15027 /* If the result is a relative path name, make it explicitly relative to
15028 * the current directory if and only if the argument had this form. */
15029 if (!vim_ispathsep(*p))
15030 {
15031 if (is_relative_to_current
15032 && *p != NUL
15033 && !(p[0] == '.'
15034 && (p[1] == NUL
15035 || vim_ispathsep(p[1])
15036 || (p[1] == '.'
15037 && (p[2] == NUL
15038 || vim_ispathsep(p[2]))))))
15039 {
15040 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015041 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015042 if (cpy != NULL)
15043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015044 vim_free(p);
15045 p = cpy;
15046 }
15047 }
15048 else if (!is_relative_to_current)
15049 {
15050 /* Strip leading "./". */
15051 q = p;
15052 while (q[0] == '.' && vim_ispathsep(q[1]))
15053 q += 2;
15054 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015055 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 }
15057 }
15058
15059 /* Ensure that the result will have no trailing path separator
15060 * if the argument had none. But keep "/" or "//". */
15061 if (!has_trailing_pathsep)
15062 {
15063 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015064 if (after_pathsep(p, q))
15065 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 }
15067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069 }
15070# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015071 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072# endif
15073#endif
15074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076
15077#ifdef HAVE_READLINK
15078fail:
15079#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015080 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081}
15082
15083/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085 */
15086 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015088 typval_T *argvars;
15089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090{
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 list_T *l;
15092 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094 if (argvars[0].v_type != VAR_LIST)
15095 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015096 else if ((l = argvars[0].vval.v_list) != NULL
15097 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015098 {
15099 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015100 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015101 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 while (li != NULL)
15103 {
15104 ni = li->li_prev;
15105 list_append(l, li);
15106 li = ni;
15107 }
15108 rettv->vval.v_list = l;
15109 rettv->v_type = VAR_LIST;
15110 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015111 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113}
15114
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015115#define SP_NOMOVE 0x01 /* don't move cursor */
15116#define SP_REPEAT 0x02 /* repeat to find outer pair */
15117#define SP_RETCOUNT 0x04 /* return matchcount */
15118#define SP_SETPCMARK 0x08 /* set previous context mark */
15119#define SP_START 0x10 /* accept match at start position */
15120#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15121#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015122
Bram Moolenaar33570922005-01-25 22:26:29 +000015123static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015124
15125/*
15126 * Get flags for a search function.
15127 * Possibly sets "p_ws".
15128 * Returns BACKWARD, FORWARD or zero (for an error).
15129 */
15130 static int
15131get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015133 int *flagsp;
15134{
15135 int dir = FORWARD;
15136 char_u *flags;
15137 char_u nbuf[NUMBUFLEN];
15138 int mask;
15139
15140 if (varp->v_type != VAR_UNKNOWN)
15141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015142 flags = get_tv_string_buf_chk(varp, nbuf);
15143 if (flags == NULL)
15144 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015145 while (*flags != NUL)
15146 {
15147 switch (*flags)
15148 {
15149 case 'b': dir = BACKWARD; break;
15150 case 'w': p_ws = TRUE; break;
15151 case 'W': p_ws = FALSE; break;
15152 default: mask = 0;
15153 if (flagsp != NULL)
15154 switch (*flags)
15155 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015156 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015157 case 'e': mask = SP_END; break;
15158 case 'm': mask = SP_RETCOUNT; break;
15159 case 'n': mask = SP_NOMOVE; break;
15160 case 'p': mask = SP_SUBPAT; break;
15161 case 'r': mask = SP_REPEAT; break;
15162 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015163 }
15164 if (mask == 0)
15165 {
15166 EMSG2(_(e_invarg2), flags);
15167 dir = 0;
15168 }
15169 else
15170 *flagsp |= mask;
15171 }
15172 if (dir == 0)
15173 break;
15174 ++flags;
15175 }
15176 }
15177 return dir;
15178}
15179
Bram Moolenaar071d4272004-06-13 20:20:40 +000015180/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015181 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015183 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015184search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015185 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015186 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015187 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015188{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015189 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 char_u *pat;
15191 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015192 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015193 int save_p_ws = p_ws;
15194 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015195 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015196 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015197 proftime_T tm;
15198#ifdef FEAT_RELTIME
15199 long time_limit = 0;
15200#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015201 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015202 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015205 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015206 if (dir == 0)
15207 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015208 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015209 if (flags & SP_START)
15210 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015211 if (flags & SP_END)
15212 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015213
Bram Moolenaar76929292008-01-06 19:07:36 +000015214 /* Optional arguments: line number to stop searching and timeout. */
15215 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015216 {
15217 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15218 if (lnum_stop < 0)
15219 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015220#ifdef FEAT_RELTIME
15221 if (argvars[3].v_type != VAR_UNKNOWN)
15222 {
15223 time_limit = get_tv_number_chk(&argvars[3], NULL);
15224 if (time_limit < 0)
15225 goto theend;
15226 }
15227#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015228 }
15229
Bram Moolenaar76929292008-01-06 19:07:36 +000015230#ifdef FEAT_RELTIME
15231 /* Set the time limit, if there is one. */
15232 profile_setlimit(time_limit, &tm);
15233#endif
15234
Bram Moolenaar231334e2005-07-25 20:46:57 +000015235 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015236 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015237 * Check to make sure only those flags are set.
15238 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15239 * flags cannot be set. Check for that condition also.
15240 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015241 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015242 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015244 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015245 goto theend;
15246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015247
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015248 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015249 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015250 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015251 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015253 if (flags & SP_SUBPAT)
15254 retval = subpatnum;
15255 else
15256 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015257 if (flags & SP_SETPCMARK)
15258 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015260 if (match_pos != NULL)
15261 {
15262 /* Store the match cursor position */
15263 match_pos->lnum = pos.lnum;
15264 match_pos->col = pos.col + 1;
15265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015266 /* "/$" will put the cursor after the end of the line, may need to
15267 * correct that here */
15268 check_cursor();
15269 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015270
15271 /* If 'n' flag is used: restore cursor position. */
15272 if (flags & SP_NOMOVE)
15273 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015274 else
15275 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015276theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015278
15279 return retval;
15280}
15281
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015282#ifdef FEAT_FLOAT
15283/*
15284 * "round({float})" function
15285 */
15286 static void
15287f_round(argvars, rettv)
15288 typval_T *argvars;
15289 typval_T *rettv;
15290{
15291 float_T f;
15292
15293 rettv->v_type = VAR_FLOAT;
15294 if (get_float_arg(argvars, &f) == OK)
15295 /* round() is not in C90, use ceil() or floor() instead. */
15296 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15297 else
15298 rettv->vval.v_float = 0.0;
15299}
15300#endif
15301
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015302/*
15303 * "search()" function
15304 */
15305 static void
15306f_search(argvars, rettv)
15307 typval_T *argvars;
15308 typval_T *rettv;
15309{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015310 int flags = 0;
15311
15312 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313}
15314
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015316 * "searchdecl()" function
15317 */
15318 static void
15319f_searchdecl(argvars, rettv)
15320 typval_T *argvars;
15321 typval_T *rettv;
15322{
15323 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015324 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015325 int error = FALSE;
15326 char_u *name;
15327
15328 rettv->vval.v_number = 1; /* default: FAIL */
15329
15330 name = get_tv_string_chk(&argvars[0]);
15331 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015332 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015333 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015334 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15335 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15336 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015337 if (!error && name != NULL)
15338 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015339 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015340}
15341
15342/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015343 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015345 static int
15346searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015347 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015348 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349{
15350 char_u *spat, *mpat, *epat;
15351 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 int dir;
15354 int flags = 0;
15355 char_u nbuf1[NUMBUFLEN];
15356 char_u nbuf2[NUMBUFLEN];
15357 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015358 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015359 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015360 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015363 spat = get_tv_string_chk(&argvars[0]);
15364 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15365 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15366 if (spat == NULL || mpat == NULL || epat == NULL)
15367 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369 /* Handle the optional fourth argument: flags */
15370 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015371 if (dir == 0)
15372 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015373
15374 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015375 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15376 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015377 if ((flags & (SP_END | SP_SUBPAT)) != 0
15378 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015379 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015380 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015381 goto theend;
15382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015384 /* Using 'r' implies 'W', otherwise it doesn't work. */
15385 if (flags & SP_REPEAT)
15386 p_ws = FALSE;
15387
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015388 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015389 if (argvars[3].v_type == VAR_UNKNOWN
15390 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 skip = (char_u *)"";
15392 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015394 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015395 if (argvars[5].v_type != VAR_UNKNOWN)
15396 {
15397 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15398 if (lnum_stop < 0)
15399 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015400#ifdef FEAT_RELTIME
15401 if (argvars[6].v_type != VAR_UNKNOWN)
15402 {
15403 time_limit = get_tv_number_chk(&argvars[6], NULL);
15404 if (time_limit < 0)
15405 goto theend;
15406 }
15407#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015408 }
15409 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015410 if (skip == NULL)
15411 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015413 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015414 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015415
15416theend:
15417 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015418
15419 return retval;
15420}
15421
15422/*
15423 * "searchpair()" function
15424 */
15425 static void
15426f_searchpair(argvars, rettv)
15427 typval_T *argvars;
15428 typval_T *rettv;
15429{
15430 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15431}
15432
15433/*
15434 * "searchpairpos()" function
15435 */
15436 static void
15437f_searchpairpos(argvars, rettv)
15438 typval_T *argvars;
15439 typval_T *rettv;
15440{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015441 pos_T match_pos;
15442 int lnum = 0;
15443 int col = 0;
15444
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015445 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015446 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015447
15448 if (searchpair_cmn(argvars, &match_pos) > 0)
15449 {
15450 lnum = match_pos.lnum;
15451 col = match_pos.col;
15452 }
15453
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015454 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15455 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015456}
15457
15458/*
15459 * Search for a start/middle/end thing.
15460 * Used by searchpair(), see its documentation for the details.
15461 * Returns 0 or -1 for no match,
15462 */
15463 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015464do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15465 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015466 char_u *spat; /* start pattern */
15467 char_u *mpat; /* middle pattern */
15468 char_u *epat; /* end pattern */
15469 int dir; /* BACKWARD or FORWARD */
15470 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015471 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015472 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015473 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015474 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015475{
15476 char_u *save_cpo;
15477 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15478 long retval = 0;
15479 pos_T pos;
15480 pos_T firstpos;
15481 pos_T foundpos;
15482 pos_T save_cursor;
15483 pos_T save_pos;
15484 int n;
15485 int r;
15486 int nest = 1;
15487 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015488 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015489 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015490
15491 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15492 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015493 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015494
Bram Moolenaar76929292008-01-06 19:07:36 +000015495#ifdef FEAT_RELTIME
15496 /* Set the time limit, if there is one. */
15497 profile_setlimit(time_limit, &tm);
15498#endif
15499
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015500 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15501 * start/middle/end (pat3, for the top pair). */
15502 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15503 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15504 if (pat2 == NULL || pat3 == NULL)
15505 goto theend;
15506 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15507 if (*mpat == NUL)
15508 STRCPY(pat3, pat2);
15509 else
15510 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15511 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015512 if (flags & SP_START)
15513 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015514
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 save_cursor = curwin->w_cursor;
15516 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015517 clearpos(&firstpos);
15518 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 pat = pat3;
15520 for (;;)
15521 {
15522 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015523 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15525 /* didn't find it or found the first match again: FAIL */
15526 break;
15527
15528 if (firstpos.lnum == 0)
15529 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015530 if (equalpos(pos, foundpos))
15531 {
15532 /* Found the same position again. Can happen with a pattern that
15533 * has "\zs" at the end and searching backwards. Advance one
15534 * character and try again. */
15535 if (dir == BACKWARD)
15536 decl(&pos);
15537 else
15538 incl(&pos);
15539 }
15540 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015542 /* clear the start flag to avoid getting stuck here */
15543 options &= ~SEARCH_START;
15544
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 /* If the skip pattern matches, ignore this match. */
15546 if (*skip != NUL)
15547 {
15548 save_pos = curwin->w_cursor;
15549 curwin->w_cursor = pos;
15550 r = eval_to_bool(skip, &err, NULL, FALSE);
15551 curwin->w_cursor = save_pos;
15552 if (err)
15553 {
15554 /* Evaluating {skip} caused an error, break here. */
15555 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015556 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 break;
15558 }
15559 if (r)
15560 continue;
15561 }
15562
15563 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15564 {
15565 /* Found end when searching backwards or start when searching
15566 * forward: nested pair. */
15567 ++nest;
15568 pat = pat2; /* nested, don't search for middle */
15569 }
15570 else
15571 {
15572 /* Found end when searching forward or start when searching
15573 * backward: end of (nested) pair; or found middle in outer pair. */
15574 if (--nest == 1)
15575 pat = pat3; /* outer level, search for middle */
15576 }
15577
15578 if (nest == 0)
15579 {
15580 /* Found the match: return matchcount or line number. */
15581 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015582 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015584 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015585 if (flags & SP_SETPCMARK)
15586 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 curwin->w_cursor = pos;
15588 if (!(flags & SP_REPEAT))
15589 break;
15590 nest = 1; /* search for next unmatched */
15591 }
15592 }
15593
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015594 if (match_pos != NULL)
15595 {
15596 /* Store the match cursor position */
15597 match_pos->lnum = curwin->w_cursor.lnum;
15598 match_pos->col = curwin->w_cursor.col + 1;
15599 }
15600
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015602 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 curwin->w_cursor = save_cursor;
15604
15605theend:
15606 vim_free(pat2);
15607 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015608 if (p_cpo == empty_option)
15609 p_cpo = save_cpo;
15610 else
15611 /* Darn, evaluating the {skip} expression changed the value. */
15612 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015613
15614 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615}
15616
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015617/*
15618 * "searchpos()" function
15619 */
15620 static void
15621f_searchpos(argvars, rettv)
15622 typval_T *argvars;
15623 typval_T *rettv;
15624{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015625 pos_T match_pos;
15626 int lnum = 0;
15627 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015628 int n;
15629 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015630
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015631 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015632 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015633
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015634 n = search_cmn(argvars, &match_pos, &flags);
15635 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015636 {
15637 lnum = match_pos.lnum;
15638 col = match_pos.col;
15639 }
15640
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015641 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15642 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015643 if (flags & SP_SUBPAT)
15644 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015645}
15646
15647
Bram Moolenaar0d660222005-01-07 21:51:51 +000015648 static void
15649f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015650 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015653#ifdef FEAT_CLIENTSERVER
15654 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015655 char_u *server = get_tv_string_chk(&argvars[0]);
15656 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657
Bram Moolenaar0d660222005-01-07 21:51:51 +000015658 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015659 if (server == NULL || reply == NULL)
15660 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015661 if (check_restricted() || check_secure())
15662 return;
15663# ifdef FEAT_X11
15664 if (check_connection() == FAIL)
15665 return;
15666# endif
15667
15668 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015670 EMSG(_("E258: Unable to send to client"));
15671 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015673 rettv->vval.v_number = 0;
15674#else
15675 rettv->vval.v_number = -1;
15676#endif
15677}
15678
Bram Moolenaar0d660222005-01-07 21:51:51 +000015679 static void
15680f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015681 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015682 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015683{
15684 char_u *r = NULL;
15685
15686#ifdef FEAT_CLIENTSERVER
15687# ifdef WIN32
15688 r = serverGetVimNames();
15689# else
15690 make_connection();
15691 if (X_DISPLAY != NULL)
15692 r = serverGetVimNames(X_DISPLAY);
15693# endif
15694#endif
15695 rettv->v_type = VAR_STRING;
15696 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697}
15698
15699/*
15700 * "setbufvar()" function
15701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015703f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015704 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015705 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706{
15707 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015710 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711 char_u nbuf[NUMBUFLEN];
15712
15713 if (check_restricted() || check_secure())
15714 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015715 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15716 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015717 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 varp = &argvars[2];
15719
15720 if (buf != NULL && varname != NULL && varp != NULL)
15721 {
15722 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724
15725 if (*varname == '&')
15726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015727 long numval;
15728 char_u *strval;
15729 int error = FALSE;
15730
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015732 numval = get_tv_number_chk(varp, &error);
15733 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015734 if (!error && strval != NULL)
15735 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 }
15737 else
15738 {
15739 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15740 if (bufvarname != NULL)
15741 {
15742 STRCPY(bufvarname, "b:");
15743 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015744 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 vim_free(bufvarname);
15746 }
15747 }
15748
15749 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752}
15753
15754/*
15755 * "setcmdpos()" function
15756 */
15757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015758f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015759 typval_T *argvars;
15760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015762 int pos = (int)get_tv_number(&argvars[0]) - 1;
15763
15764 if (pos >= 0)
15765 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766}
15767
15768/*
15769 * "setline()" function
15770 */
15771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015772f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015773 typval_T *argvars;
15774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775{
15776 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015777 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015778 list_T *l = NULL;
15779 listitem_T *li = NULL;
15780 long added = 0;
15781 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015783 lnum = get_tv_lnum(&argvars[0]);
15784 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015786 l = argvars[1].vval.v_list;
15787 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015789 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015790 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015791
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015792 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015793 for (;;)
15794 {
15795 if (l != NULL)
15796 {
15797 /* list argument, get next string */
15798 if (li == NULL)
15799 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015800 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015801 li = li->li_next;
15802 }
15803
15804 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015805 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015806 break;
15807 if (lnum <= curbuf->b_ml.ml_line_count)
15808 {
15809 /* existing line, replace it */
15810 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15811 {
15812 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015813 if (lnum == curwin->w_cursor.lnum)
15814 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015815 rettv->vval.v_number = 0; /* OK */
15816 }
15817 }
15818 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15819 {
15820 /* lnum is one past the last line, append the line */
15821 ++added;
15822 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15823 rettv->vval.v_number = 0; /* OK */
15824 }
15825
15826 if (l == NULL) /* only one string argument */
15827 break;
15828 ++lnum;
15829 }
15830
15831 if (added > 0)
15832 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833}
15834
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015835static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15836
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015838 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015839 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015840 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015841set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015842 win_T *wp UNUSED;
15843 typval_T *list_arg UNUSED;
15844 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015845 typval_T *rettv;
15846{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015847#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015848 char_u *act;
15849 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015850#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015851
Bram Moolenaar2641f772005-03-25 21:58:17 +000015852 rettv->vval.v_number = -1;
15853
15854#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015855 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015856 EMSG(_(e_listreq));
15857 else
15858 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015859 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015860
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015861 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015862 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015863 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015864 if (act == NULL)
15865 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015866 if (*act == 'a' || *act == 'r')
15867 action = *act;
15868 }
15869
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015870 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015871 rettv->vval.v_number = 0;
15872 }
15873#endif
15874}
15875
15876/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015877 * "setloclist()" function
15878 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015879 static void
15880f_setloclist(argvars, rettv)
15881 typval_T *argvars;
15882 typval_T *rettv;
15883{
15884 win_T *win;
15885
15886 rettv->vval.v_number = -1;
15887
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015888 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015889 if (win != NULL)
15890 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15891}
15892
15893/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015894 * "setmatches()" function
15895 */
15896 static void
15897f_setmatches(argvars, rettv)
15898 typval_T *argvars;
15899 typval_T *rettv;
15900{
15901#ifdef FEAT_SEARCH_EXTRA
15902 list_T *l;
15903 listitem_T *li;
15904 dict_T *d;
15905
15906 rettv->vval.v_number = -1;
15907 if (argvars[0].v_type != VAR_LIST)
15908 {
15909 EMSG(_(e_listreq));
15910 return;
15911 }
15912 if ((l = argvars[0].vval.v_list) != NULL)
15913 {
15914
15915 /* To some extent make sure that we are dealing with a list from
15916 * "getmatches()". */
15917 li = l->lv_first;
15918 while (li != NULL)
15919 {
15920 if (li->li_tv.v_type != VAR_DICT
15921 || (d = li->li_tv.vval.v_dict) == NULL)
15922 {
15923 EMSG(_(e_invarg));
15924 return;
15925 }
15926 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15927 && dict_find(d, (char_u *)"pattern", -1) != NULL
15928 && dict_find(d, (char_u *)"priority", -1) != NULL
15929 && dict_find(d, (char_u *)"id", -1) != NULL))
15930 {
15931 EMSG(_(e_invarg));
15932 return;
15933 }
15934 li = li->li_next;
15935 }
15936
15937 clear_matches(curwin);
15938 li = l->lv_first;
15939 while (li != NULL)
15940 {
15941 d = li->li_tv.vval.v_dict;
15942 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15943 get_dict_string(d, (char_u *)"pattern", FALSE),
15944 (int)get_dict_number(d, (char_u *)"priority"),
15945 (int)get_dict_number(d, (char_u *)"id"));
15946 li = li->li_next;
15947 }
15948 rettv->vval.v_number = 0;
15949 }
15950#endif
15951}
15952
15953/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015954 * "setpos()" function
15955 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015956 static void
15957f_setpos(argvars, rettv)
15958 typval_T *argvars;
15959 typval_T *rettv;
15960{
15961 pos_T pos;
15962 int fnum;
15963 char_u *name;
15964
Bram Moolenaar08250432008-02-13 11:42:46 +000015965 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015966 name = get_tv_string_chk(argvars);
15967 if (name != NULL)
15968 {
15969 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15970 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015971 if (--pos.col < 0)
15972 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015973 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015974 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015975 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015976 if (fnum == curbuf->b_fnum)
15977 {
15978 curwin->w_cursor = pos;
15979 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015980 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015981 }
15982 else
15983 EMSG(_(e_invarg));
15984 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015985 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15986 {
15987 /* set mark */
15988 if (setmark_pos(name[1], &pos, fnum) == OK)
15989 rettv->vval.v_number = 0;
15990 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015991 else
15992 EMSG(_(e_invarg));
15993 }
15994 }
15995}
15996
15997/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015998 * "setqflist()" function
15999 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016000 static void
16001f_setqflist(argvars, rettv)
16002 typval_T *argvars;
16003 typval_T *rettv;
16004{
16005 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16006}
16007
16008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 * "setreg()" function
16010 */
16011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016013 typval_T *argvars;
16014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015{
16016 int regname;
16017 char_u *strregname;
16018 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016019 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 int append;
16021 char_u yank_type;
16022 long block_len;
16023
16024 block_len = -1;
16025 yank_type = MAUTO;
16026 append = FALSE;
16027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016029 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016030
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016031 if (strregname == NULL)
16032 return; /* type error; errmsg already given */
16033 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 if (regname == 0 || regname == '@')
16035 regname = '"';
16036 else if (regname == '=')
16037 return;
16038
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016039 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 stropt = get_tv_string_chk(&argvars[2]);
16042 if (stropt == NULL)
16043 return; /* type error */
16044 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 switch (*stropt)
16046 {
16047 case 'a': case 'A': /* append */
16048 append = TRUE;
16049 break;
16050 case 'v': case 'c': /* character-wise selection */
16051 yank_type = MCHAR;
16052 break;
16053 case 'V': case 'l': /* line-wise selection */
16054 yank_type = MLINE;
16055 break;
16056#ifdef FEAT_VISUAL
16057 case 'b': case Ctrl_V: /* block-wise selection */
16058 yank_type = MBLOCK;
16059 if (VIM_ISDIGIT(stropt[1]))
16060 {
16061 ++stropt;
16062 block_len = getdigits(&stropt) - 1;
16063 --stropt;
16064 }
16065 break;
16066#endif
16067 }
16068 }
16069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016070 strval = get_tv_string_chk(&argvars[1]);
16071 if (strval != NULL)
16072 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016075}
16076
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016077/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016078 * "settabvar()" function
16079 */
16080 static void
16081f_settabvar(argvars, rettv)
16082 typval_T *argvars;
16083 typval_T *rettv;
16084{
16085 tabpage_T *save_curtab;
16086 char_u *varname, *tabvarname;
16087 typval_T *varp;
16088 tabpage_T *tp;
16089
16090 rettv->vval.v_number = 0;
16091
16092 if (check_restricted() || check_secure())
16093 return;
16094
16095 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16096 varname = get_tv_string_chk(&argvars[1]);
16097 varp = &argvars[2];
16098
16099 if (tp != NULL && varname != NULL && varp != NULL)
16100 {
16101 save_curtab = curtab;
16102 goto_tabpage_tp(tp);
16103
16104 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16105 if (tabvarname != NULL)
16106 {
16107 STRCPY(tabvarname, "t:");
16108 STRCPY(tabvarname + 2, varname);
16109 set_var(tabvarname, varp, TRUE);
16110 vim_free(tabvarname);
16111 }
16112
16113 /* Restore current tabpage */
16114 if (valid_tabpage(save_curtab))
16115 goto_tabpage_tp(save_curtab);
16116 }
16117}
16118
16119/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016120 * "settabwinvar()" function
16121 */
16122 static void
16123f_settabwinvar(argvars, rettv)
16124 typval_T *argvars;
16125 typval_T *rettv;
16126{
16127 setwinvar(argvars, rettv, 1);
16128}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129
16130/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016131 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016134f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016135 typval_T *argvars;
16136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016138 setwinvar(argvars, rettv, 0);
16139}
16140
16141/*
16142 * "setwinvar()" and "settabwinvar()" functions
16143 */
16144 static void
16145setwinvar(argvars, rettv, off)
16146 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016147 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016148 int off;
16149{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016150 win_T *win;
16151#ifdef FEAT_WINDOWS
16152 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016153 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154#endif
16155 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016156 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016158 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159
16160 if (check_restricted() || check_secure())
16161 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016162
16163#ifdef FEAT_WINDOWS
16164 if (off == 1)
16165 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16166 else
16167 tp = curtab;
16168#endif
16169 win = find_win_by_nr(&argvars[off], tp);
16170 varname = get_tv_string_chk(&argvars[off + 1]);
16171 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016172
16173 if (win != NULL && varname != NULL && varp != NULL)
16174 {
16175#ifdef FEAT_WINDOWS
16176 /* set curwin to be our win, temporarily */
16177 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016178 save_curtab = curtab;
16179 goto_tabpage_tp(tp);
16180 if (!win_valid(win))
16181 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 curwin = win;
16183 curbuf = curwin->w_buffer;
16184#endif
16185
16186 if (*varname == '&')
16187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016188 long numval;
16189 char_u *strval;
16190 int error = FALSE;
16191
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016193 numval = get_tv_number_chk(varp, &error);
16194 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016195 if (!error && strval != NULL)
16196 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197 }
16198 else
16199 {
16200 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16201 if (winvarname != NULL)
16202 {
16203 STRCPY(winvarname, "w:");
16204 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016205 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 vim_free(winvarname);
16207 }
16208 }
16209
16210#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016211 /* Restore current tabpage and window, if still valid (autocomands can
16212 * make them invalid). */
16213 if (valid_tabpage(save_curtab))
16214 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215 if (win_valid(save_curwin))
16216 {
16217 curwin = save_curwin;
16218 curbuf = curwin->w_buffer;
16219 }
16220#endif
16221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222}
16223
16224/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016225 * "shellescape({string})" function
16226 */
16227 static void
16228f_shellescape(argvars, rettv)
16229 typval_T *argvars;
16230 typval_T *rettv;
16231{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016232 rettv->vval.v_string = vim_strsave_shellescape(
16233 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016234 rettv->v_type = VAR_STRING;
16235}
16236
16237/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016238 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 */
16240 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016241f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016242 typval_T *argvars;
16243 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016245 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246
Bram Moolenaar0d660222005-01-07 21:51:51 +000016247 p = get_tv_string(&argvars[0]);
16248 rettv->vval.v_string = vim_strsave(p);
16249 simplify_filename(rettv->vval.v_string); /* simplify in place */
16250 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251}
16252
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016253#ifdef FEAT_FLOAT
16254/*
16255 * "sin()" function
16256 */
16257 static void
16258f_sin(argvars, rettv)
16259 typval_T *argvars;
16260 typval_T *rettv;
16261{
16262 float_T f;
16263
16264 rettv->v_type = VAR_FLOAT;
16265 if (get_float_arg(argvars, &f) == OK)
16266 rettv->vval.v_float = sin(f);
16267 else
16268 rettv->vval.v_float = 0.0;
16269}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016270
16271/*
16272 * "sinh()" function
16273 */
16274 static void
16275f_sinh(argvars, rettv)
16276 typval_T *argvars;
16277 typval_T *rettv;
16278{
16279 float_T f;
16280
16281 rettv->v_type = VAR_FLOAT;
16282 if (get_float_arg(argvars, &f) == OK)
16283 rettv->vval.v_float = sinh(f);
16284 else
16285 rettv->vval.v_float = 0.0;
16286}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016287#endif
16288
Bram Moolenaar0d660222005-01-07 21:51:51 +000016289static int
16290#ifdef __BORLANDC__
16291 _RTLENTRYF
16292#endif
16293 item_compare __ARGS((const void *s1, const void *s2));
16294static int
16295#ifdef __BORLANDC__
16296 _RTLENTRYF
16297#endif
16298 item_compare2 __ARGS((const void *s1, const void *s2));
16299
16300static int item_compare_ic;
16301static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016302static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016303#define ITEM_COMPARE_FAIL 999
16304
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016306 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 static int
16309#ifdef __BORLANDC__
16310_RTLENTRYF
16311#endif
16312item_compare(s1, s2)
16313 const void *s1;
16314 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016316 char_u *p1, *p2;
16317 char_u *tofree1, *tofree2;
16318 int res;
16319 char_u numbuf1[NUMBUFLEN];
16320 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016322 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16323 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016324 if (p1 == NULL)
16325 p1 = (char_u *)"";
16326 if (p2 == NULL)
16327 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328 if (item_compare_ic)
16329 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016331 res = STRCMP(p1, p2);
16332 vim_free(tofree1);
16333 vim_free(tofree2);
16334 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335}
16336
16337 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016338#ifdef __BORLANDC__
16339_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016341item_compare2(s1, s2)
16342 const void *s1;
16343 const void *s2;
16344{
16345 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016346 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016347 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016350 /* shortcut after failure in previous call; compare all items equal */
16351 if (item_compare_func_err)
16352 return 0;
16353
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016354 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16355 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016356 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16357 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016358
16359 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016360 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016361 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016362 clear_tv(&argv[0]);
16363 clear_tv(&argv[1]);
16364
16365 if (res == FAIL)
16366 res = ITEM_COMPARE_FAIL;
16367 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016368 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16369 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016370 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016371 clear_tv(&rettv);
16372 return res;
16373}
16374
16375/*
16376 * "sort({list})" function
16377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016379f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016380 typval_T *argvars;
16381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382{
Bram Moolenaar33570922005-01-25 22:26:29 +000016383 list_T *l;
16384 listitem_T *li;
16385 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386 long len;
16387 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389 if (argvars[0].v_type != VAR_LIST)
16390 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 else
16392 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016393 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016394 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016395 return;
16396 rettv->vval.v_list = l;
16397 rettv->v_type = VAR_LIST;
16398 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016399
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400 len = list_len(l);
16401 if (len <= 1)
16402 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403
Bram Moolenaar0d660222005-01-07 21:51:51 +000016404 item_compare_ic = FALSE;
16405 item_compare_func = NULL;
16406 if (argvars[1].v_type != VAR_UNKNOWN)
16407 {
16408 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016409 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016410 else
16411 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016412 int error = FALSE;
16413
16414 i = get_tv_number_chk(&argvars[1], &error);
16415 if (error)
16416 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417 if (i == 1)
16418 item_compare_ic = TRUE;
16419 else
16420 item_compare_func = get_tv_string(&argvars[1]);
16421 }
16422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016423
Bram Moolenaar0d660222005-01-07 21:51:51 +000016424 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016425 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016426 if (ptrs == NULL)
16427 return;
16428 i = 0;
16429 for (li = l->lv_first; li != NULL; li = li->li_next)
16430 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016432 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016433 /* test the compare function */
16434 if (item_compare_func != NULL
16435 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16436 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016437 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016439 {
16440 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016441 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016442 item_compare_func == NULL ? item_compare : item_compare2);
16443
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016444 if (!item_compare_func_err)
16445 {
16446 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016447 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016448 l->lv_len = 0;
16449 for (i = 0; i < len; ++i)
16450 list_append(l, ptrs[i]);
16451 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016452 }
16453
16454 vim_free(ptrs);
16455 }
16456}
16457
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016458/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016459 * "soundfold({word})" function
16460 */
16461 static void
16462f_soundfold(argvars, rettv)
16463 typval_T *argvars;
16464 typval_T *rettv;
16465{
16466 char_u *s;
16467
16468 rettv->v_type = VAR_STRING;
16469 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016470#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016471 rettv->vval.v_string = eval_soundfold(s);
16472#else
16473 rettv->vval.v_string = vim_strsave(s);
16474#endif
16475}
16476
16477/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016478 * "spellbadword()" function
16479 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016480 static void
16481f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016482 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016483 typval_T *rettv;
16484{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016485 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016486 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016487 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016488
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016489 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016490 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016491
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016492#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016493 if (argvars[0].v_type == VAR_UNKNOWN)
16494 {
16495 /* Find the start and length of the badly spelled word. */
16496 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16497 if (len != 0)
16498 word = ml_get_cursor();
16499 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016500 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016501 {
16502 char_u *str = get_tv_string_chk(&argvars[0]);
16503 int capcol = -1;
16504
16505 if (str != NULL)
16506 {
16507 /* Check the argument for spelling. */
16508 while (*str != NUL)
16509 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016510 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016511 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016512 {
16513 word = str;
16514 break;
16515 }
16516 str += len;
16517 }
16518 }
16519 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016520#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016521
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016522 list_append_string(rettv->vval.v_list, word, len);
16523 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016524 attr == HLF_SPB ? "bad" :
16525 attr == HLF_SPR ? "rare" :
16526 attr == HLF_SPL ? "local" :
16527 attr == HLF_SPC ? "caps" :
16528 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016529}
16530
16531/*
16532 * "spellsuggest()" function
16533 */
16534 static void
16535f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016536 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016537 typval_T *rettv;
16538{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016539#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016540 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016541 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016542 int maxcount;
16543 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016544 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016545 listitem_T *li;
16546 int need_capital = FALSE;
16547#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016548
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016549 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016550 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016551
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016552#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016553 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016554 {
16555 str = get_tv_string(&argvars[0]);
16556 if (argvars[1].v_type != VAR_UNKNOWN)
16557 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016558 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016559 if (maxcount <= 0)
16560 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016561 if (argvars[2].v_type != VAR_UNKNOWN)
16562 {
16563 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16564 if (typeerr)
16565 return;
16566 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016567 }
16568 else
16569 maxcount = 25;
16570
Bram Moolenaar4770d092006-01-12 23:22:24 +000016571 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016572
16573 for (i = 0; i < ga.ga_len; ++i)
16574 {
16575 str = ((char_u **)ga.ga_data)[i];
16576
16577 li = listitem_alloc();
16578 if (li == NULL)
16579 vim_free(str);
16580 else
16581 {
16582 li->li_tv.v_type = VAR_STRING;
16583 li->li_tv.v_lock = 0;
16584 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016585 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016586 }
16587 }
16588 ga_clear(&ga);
16589 }
16590#endif
16591}
16592
Bram Moolenaar0d660222005-01-07 21:51:51 +000016593 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016594f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016595 typval_T *argvars;
16596 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016597{
16598 char_u *str;
16599 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016600 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016601 regmatch_T regmatch;
16602 char_u patbuf[NUMBUFLEN];
16603 char_u *save_cpo;
16604 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016605 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016606 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016607 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016608
16609 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16610 save_cpo = p_cpo;
16611 p_cpo = (char_u *)"";
16612
16613 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016614 if (argvars[1].v_type != VAR_UNKNOWN)
16615 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016616 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16617 if (pat == NULL)
16618 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016619 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016620 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016621 }
16622 if (pat == NULL || *pat == NUL)
16623 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016624
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016625 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016627 if (typeerr)
16628 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629
Bram Moolenaar0d660222005-01-07 21:51:51 +000016630 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16631 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016632 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016633 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016634 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016635 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016636 if (*str == NUL)
16637 match = FALSE; /* empty item at the end */
16638 else
16639 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016640 if (match)
16641 end = regmatch.startp[0];
16642 else
16643 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016644 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16645 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016647 if (list_append_string(rettv->vval.v_list, str,
16648 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016649 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016650 }
16651 if (!match)
16652 break;
16653 /* Advance to just after the match. */
16654 if (regmatch.endp[0] > str)
16655 col = 0;
16656 else
16657 {
16658 /* Don't get stuck at the same match. */
16659#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016660 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016661#else
16662 col = 1;
16663#endif
16664 }
16665 str = regmatch.endp[0];
16666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667
Bram Moolenaar0d660222005-01-07 21:51:51 +000016668 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670
Bram Moolenaar0d660222005-01-07 21:51:51 +000016671 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016672}
16673
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016674#ifdef FEAT_FLOAT
16675/*
16676 * "sqrt()" function
16677 */
16678 static void
16679f_sqrt(argvars, rettv)
16680 typval_T *argvars;
16681 typval_T *rettv;
16682{
16683 float_T f;
16684
16685 rettv->v_type = VAR_FLOAT;
16686 if (get_float_arg(argvars, &f) == OK)
16687 rettv->vval.v_float = sqrt(f);
16688 else
16689 rettv->vval.v_float = 0.0;
16690}
16691
16692/*
16693 * "str2float()" function
16694 */
16695 static void
16696f_str2float(argvars, rettv)
16697 typval_T *argvars;
16698 typval_T *rettv;
16699{
16700 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16701
16702 if (*p == '+')
16703 p = skipwhite(p + 1);
16704 (void)string2float(p, &rettv->vval.v_float);
16705 rettv->v_type = VAR_FLOAT;
16706}
16707#endif
16708
Bram Moolenaar2c932302006-03-18 21:42:09 +000016709/*
16710 * "str2nr()" function
16711 */
16712 static void
16713f_str2nr(argvars, rettv)
16714 typval_T *argvars;
16715 typval_T *rettv;
16716{
16717 int base = 10;
16718 char_u *p;
16719 long n;
16720
16721 if (argvars[1].v_type != VAR_UNKNOWN)
16722 {
16723 base = get_tv_number(&argvars[1]);
16724 if (base != 8 && base != 10 && base != 16)
16725 {
16726 EMSG(_(e_invarg));
16727 return;
16728 }
16729 }
16730
16731 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016732 if (*p == '+')
16733 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016734 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16735 rettv->vval.v_number = n;
16736}
16737
Bram Moolenaar071d4272004-06-13 20:20:40 +000016738#ifdef HAVE_STRFTIME
16739/*
16740 * "strftime({format}[, {time}])" function
16741 */
16742 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016743f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016744 typval_T *argvars;
16745 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746{
16747 char_u result_buf[256];
16748 struct tm *curtime;
16749 time_t seconds;
16750 char_u *p;
16751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016752 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016754 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016755 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 seconds = time(NULL);
16757 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016758 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 curtime = localtime(&seconds);
16760 /* MSVC returns NULL for an invalid value of seconds. */
16761 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016762 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763 else
16764 {
16765# ifdef FEAT_MBYTE
16766 vimconv_T conv;
16767 char_u *enc;
16768
16769 conv.vc_type = CONV_NONE;
16770 enc = enc_locale();
16771 convert_setup(&conv, p_enc, enc);
16772 if (conv.vc_type != CONV_NONE)
16773 p = string_convert(&conv, p, NULL);
16774# endif
16775 if (p != NULL)
16776 (void)strftime((char *)result_buf, sizeof(result_buf),
16777 (char *)p, curtime);
16778 else
16779 result_buf[0] = NUL;
16780
16781# ifdef FEAT_MBYTE
16782 if (conv.vc_type != CONV_NONE)
16783 vim_free(p);
16784 convert_setup(&conv, enc, p_enc);
16785 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016786 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 else
16788# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016789 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790
16791# ifdef FEAT_MBYTE
16792 /* Release conversion descriptors */
16793 convert_setup(&conv, NULL, NULL);
16794 vim_free(enc);
16795# endif
16796 }
16797}
16798#endif
16799
16800/*
16801 * "stridx()" function
16802 */
16803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016804f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016805 typval_T *argvars;
16806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807{
16808 char_u buf[NUMBUFLEN];
16809 char_u *needle;
16810 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016811 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016813 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016814
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016815 needle = get_tv_string_chk(&argvars[1]);
16816 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016817 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016818 if (needle == NULL || haystack == NULL)
16819 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820
Bram Moolenaar33570922005-01-25 22:26:29 +000016821 if (argvars[2].v_type != VAR_UNKNOWN)
16822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016823 int error = FALSE;
16824
16825 start_idx = get_tv_number_chk(&argvars[2], &error);
16826 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016827 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016828 if (start_idx >= 0)
16829 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016830 }
16831
16832 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16833 if (pos != NULL)
16834 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835}
16836
16837/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016838 * "string()" function
16839 */
16840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016841f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016842 typval_T *argvars;
16843 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016844{
16845 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016846 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016847
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016848 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016849 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016850 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016851 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016852 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853}
16854
16855/*
16856 * "strlen()" function
16857 */
16858 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016859f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 typval_T *argvars;
16861 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016863 rettv->vval.v_number = (varnumber_T)(STRLEN(
16864 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865}
16866
16867/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016868 * "strchars()" function
16869 */
16870 static void
16871f_strchars(argvars, rettv)
16872 typval_T *argvars;
16873 typval_T *rettv;
16874{
16875 char_u *s = get_tv_string(&argvars[0]);
16876#ifdef FEAT_MBYTE
16877 varnumber_T len = 0;
16878
16879 while (*s != NUL)
16880 {
16881 mb_cptr2char_adv(&s);
16882 ++len;
16883 }
16884 rettv->vval.v_number = len;
16885#else
16886 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16887#endif
16888}
16889
16890/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016891 * "strdisplaywidth()" function
16892 */
16893 static void
16894f_strdisplaywidth(argvars, rettv)
16895 typval_T *argvars;
16896 typval_T *rettv;
16897{
16898 char_u *s = get_tv_string(&argvars[0]);
16899 int col = 0;
16900
16901 if (argvars[1].v_type != VAR_UNKNOWN)
16902 col = get_tv_number(&argvars[1]);
16903
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016904 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016905}
16906
16907/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016908 * "strwidth()" function
16909 */
16910 static void
16911f_strwidth(argvars, rettv)
16912 typval_T *argvars;
16913 typval_T *rettv;
16914{
16915 char_u *s = get_tv_string(&argvars[0]);
16916
16917 rettv->vval.v_number = (varnumber_T)(
16918#ifdef FEAT_MBYTE
16919 mb_string2cells(s, -1)
16920#else
16921 STRLEN(s)
16922#endif
16923 );
16924}
16925
16926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016927 * "strpart()" function
16928 */
16929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016930f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016931 typval_T *argvars;
16932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016933{
16934 char_u *p;
16935 int n;
16936 int len;
16937 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016938 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016940 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 slen = (int)STRLEN(p);
16942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016943 n = get_tv_number_chk(&argvars[1], &error);
16944 if (error)
16945 len = 0;
16946 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016947 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 else
16949 len = slen - n; /* default len: all bytes that are available. */
16950
16951 /*
16952 * Only return the overlap between the specified part and the actual
16953 * string.
16954 */
16955 if (n < 0)
16956 {
16957 len += n;
16958 n = 0;
16959 }
16960 else if (n > slen)
16961 n = slen;
16962 if (len < 0)
16963 len = 0;
16964 else if (n + len > slen)
16965 len = slen - n;
16966
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016967 rettv->v_type = VAR_STRING;
16968 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969}
16970
16971/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972 * "strridx()" function
16973 */
16974 static void
16975f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 typval_T *argvars;
16977 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978{
16979 char_u buf[NUMBUFLEN];
16980 char_u *needle;
16981 char_u *haystack;
16982 char_u *rest;
16983 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016984 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016986 needle = get_tv_string_chk(&argvars[1]);
16987 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016988
16989 rettv->vval.v_number = -1;
16990 if (needle == NULL || haystack == NULL)
16991 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016992
16993 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016994 if (argvars[2].v_type != VAR_UNKNOWN)
16995 {
16996 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016997 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016998 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016999 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017000 }
17001 else
17002 end_idx = haystack_len;
17003
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017005 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017007 lastmatch = haystack + end_idx;
17008 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017010 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011 for (rest = haystack; *rest != '\0'; ++rest)
17012 {
17013 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017014 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017015 break;
17016 lastmatch = rest;
17017 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017018 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019
17020 if (lastmatch == NULL)
17021 rettv->vval.v_number = -1;
17022 else
17023 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17024}
17025
17026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027 * "strtrans()" function
17028 */
17029 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017030f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017031 typval_T *argvars;
17032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017034 rettv->v_type = VAR_STRING;
17035 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017036}
17037
17038/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039 * "submatch()" function
17040 */
17041 static void
17042f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 typval_T *argvars;
17044 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017045{
17046 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017047 rettv->vval.v_string =
17048 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049}
17050
17051/*
17052 * "substitute()" function
17053 */
17054 static void
17055f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017056 typval_T *argvars;
17057 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058{
17059 char_u patbuf[NUMBUFLEN];
17060 char_u subbuf[NUMBUFLEN];
17061 char_u flagsbuf[NUMBUFLEN];
17062
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017063 char_u *str = get_tv_string_chk(&argvars[0]);
17064 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17065 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17066 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17067
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017069 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17070 rettv->vval.v_string = NULL;
17071 else
17072 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073}
17074
17075/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017076 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017079f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017080 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017081 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082{
17083 int id = 0;
17084#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017085 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 long col;
17087 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017088 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017090 lnum = get_tv_lnum(argvars); /* -1 on type error */
17091 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17092 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017094 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017095 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017096 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097#endif
17098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017099 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017100}
17101
17102/*
17103 * "synIDattr(id, what [, mode])" function
17104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017106f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017107 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109{
17110 char_u *p = NULL;
17111#ifdef FEAT_SYN_HL
17112 int id;
17113 char_u *what;
17114 char_u *mode;
17115 char_u modebuf[NUMBUFLEN];
17116 int modec;
17117
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017118 id = get_tv_number(&argvars[0]);
17119 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017120 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017122 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017124 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017125 modec = 0; /* replace invalid with current */
17126 }
17127 else
17128 {
17129#ifdef FEAT_GUI
17130 if (gui.in_use)
17131 modec = 'g';
17132 else
17133#endif
17134 if (t_colors > 1)
17135 modec = 'c';
17136 else
17137 modec = 't';
17138 }
17139
17140
17141 switch (TOLOWER_ASC(what[0]))
17142 {
17143 case 'b':
17144 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17145 p = highlight_color(id, what, modec);
17146 else /* bold */
17147 p = highlight_has_attr(id, HL_BOLD, modec);
17148 break;
17149
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017150 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151 p = highlight_color(id, what, modec);
17152 break;
17153
17154 case 'i':
17155 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17156 p = highlight_has_attr(id, HL_INVERSE, modec);
17157 else /* italic */
17158 p = highlight_has_attr(id, HL_ITALIC, modec);
17159 break;
17160
17161 case 'n': /* name */
17162 p = get_highlight_name(NULL, id - 1);
17163 break;
17164
17165 case 'r': /* reverse */
17166 p = highlight_has_attr(id, HL_INVERSE, modec);
17167 break;
17168
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017169 case 's':
17170 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17171 p = highlight_color(id, what, modec);
17172 else /* standout */
17173 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174 break;
17175
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017176 case 'u':
17177 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17178 /* underline */
17179 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17180 else
17181 /* undercurl */
17182 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183 break;
17184 }
17185
17186 if (p != NULL)
17187 p = vim_strsave(p);
17188#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017189 rettv->v_type = VAR_STRING;
17190 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191}
17192
17193/*
17194 * "synIDtrans(id)" function
17195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017197f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017198 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017199 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200{
17201 int id;
17202
17203#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017204 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017205
17206 if (id > 0)
17207 id = syn_get_final_id(id);
17208 else
17209#endif
17210 id = 0;
17211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017212 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017213}
17214
17215/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017216 * "synconcealed(lnum, col)" function
17217 */
17218 static void
17219f_synconcealed(argvars, rettv)
17220 typval_T *argvars UNUSED;
17221 typval_T *rettv;
17222{
17223#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17224 long lnum;
17225 long col;
17226 int syntax_flags = 0;
17227 int cchar;
17228 int matchid = 0;
17229 char_u str[NUMBUFLEN];
17230#endif
17231
17232 rettv->v_type = VAR_LIST;
17233 rettv->vval.v_list = NULL;
17234
17235#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17236 lnum = get_tv_lnum(argvars); /* -1 on type error */
17237 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17238
17239 vim_memset(str, NUL, sizeof(str));
17240
17241 if (rettv_list_alloc(rettv) != FAIL)
17242 {
17243 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17244 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17245 && curwin->w_p_cole > 0)
17246 {
17247 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17248 syntax_flags = get_syntax_info(&matchid);
17249
17250 /* get the conceal character */
17251 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17252 {
17253 cchar = syn_get_sub_char();
17254 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17255 cchar = lcs_conceal;
17256 if (cchar != NUL)
17257 {
17258# ifdef FEAT_MBYTE
17259 if (has_mbyte)
17260 (*mb_char2bytes)(cchar, str);
17261 else
17262# endif
17263 str[0] = cchar;
17264 }
17265 }
17266 }
17267
17268 list_append_number(rettv->vval.v_list,
17269 (syntax_flags & HL_CONCEAL) != 0);
17270 /* -1 to auto-determine strlen */
17271 list_append_string(rettv->vval.v_list, str, -1);
17272 list_append_number(rettv->vval.v_list, matchid);
17273 }
17274#endif
17275}
17276
17277/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017278 * "synstack(lnum, col)" function
17279 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017280 static void
17281f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017282 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017283 typval_T *rettv;
17284{
17285#ifdef FEAT_SYN_HL
17286 long lnum;
17287 long col;
17288 int i;
17289 int id;
17290#endif
17291
17292 rettv->v_type = VAR_LIST;
17293 rettv->vval.v_list = NULL;
17294
17295#ifdef FEAT_SYN_HL
17296 lnum = get_tv_lnum(argvars); /* -1 on type error */
17297 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17298
17299 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017300 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017301 && rettv_list_alloc(rettv) != FAIL)
17302 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017303 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017304 for (i = 0; ; ++i)
17305 {
17306 id = syn_get_stack_item(i);
17307 if (id < 0)
17308 break;
17309 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17310 break;
17311 }
17312 }
17313#endif
17314}
17315
17316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 * "system()" function
17318 */
17319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017320f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017321 typval_T *argvars;
17322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017324 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017326 char_u *infile = NULL;
17327 char_u buf[NUMBUFLEN];
17328 int err = FALSE;
17329 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017331 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017332 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017333
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017334 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017335 {
17336 /*
17337 * Write the string to a temp file, to be used for input of the shell
17338 * command.
17339 */
17340 if ((infile = vim_tempname('i')) == NULL)
17341 {
17342 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017343 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017344 }
17345
17346 fd = mch_fopen((char *)infile, WRITEBIN);
17347 if (fd == NULL)
17348 {
17349 EMSG2(_(e_notopen), infile);
17350 goto done;
17351 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017352 p = get_tv_string_buf_chk(&argvars[1], buf);
17353 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017354 {
17355 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017356 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017357 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017358 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17359 err = TRUE;
17360 if (fclose(fd) != 0)
17361 err = TRUE;
17362 if (err)
17363 {
17364 EMSG(_("E677: Error writing temp file"));
17365 goto done;
17366 }
17367 }
17368
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017369 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17370 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017371
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372#ifdef USE_CR
17373 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017374 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375 {
17376 char_u *s;
17377
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017378 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 {
17380 if (*s == CAR)
17381 *s = NL;
17382 }
17383 }
17384#else
17385# ifdef USE_CRNL
17386 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017387 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 {
17389 char_u *s, *d;
17390
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017391 d = res;
17392 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393 {
17394 if (s[0] == CAR && s[1] == NL)
17395 ++s;
17396 *d++ = *s;
17397 }
17398 *d = NUL;
17399 }
17400# endif
17401#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017402
17403done:
17404 if (infile != NULL)
17405 {
17406 mch_remove(infile);
17407 vim_free(infile);
17408 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017409 rettv->v_type = VAR_STRING;
17410 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411}
17412
17413/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017414 * "tabpagebuflist()" function
17415 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017416 static void
17417f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017418 typval_T *argvars UNUSED;
17419 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017420{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017421#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017422 tabpage_T *tp;
17423 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017424
17425 if (argvars[0].v_type == VAR_UNKNOWN)
17426 wp = firstwin;
17427 else
17428 {
17429 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17430 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017431 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017432 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017433 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017434 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017435 for (; wp != NULL; wp = wp->w_next)
17436 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017437 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017438 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017439 }
17440#endif
17441}
17442
17443
17444/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017445 * "tabpagenr()" function
17446 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017447 static void
17448f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017449 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017450 typval_T *rettv;
17451{
17452 int nr = 1;
17453#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017454 char_u *arg;
17455
17456 if (argvars[0].v_type != VAR_UNKNOWN)
17457 {
17458 arg = get_tv_string_chk(&argvars[0]);
17459 nr = 0;
17460 if (arg != NULL)
17461 {
17462 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017463 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017464 else
17465 EMSG2(_(e_invexpr2), arg);
17466 }
17467 }
17468 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017469 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017470#endif
17471 rettv->vval.v_number = nr;
17472}
17473
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017474
17475#ifdef FEAT_WINDOWS
17476static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17477
17478/*
17479 * Common code for tabpagewinnr() and winnr().
17480 */
17481 static int
17482get_winnr(tp, argvar)
17483 tabpage_T *tp;
17484 typval_T *argvar;
17485{
17486 win_T *twin;
17487 int nr = 1;
17488 win_T *wp;
17489 char_u *arg;
17490
17491 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17492 if (argvar->v_type != VAR_UNKNOWN)
17493 {
17494 arg = get_tv_string_chk(argvar);
17495 if (arg == NULL)
17496 nr = 0; /* type error; errmsg already given */
17497 else if (STRCMP(arg, "$") == 0)
17498 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17499 else if (STRCMP(arg, "#") == 0)
17500 {
17501 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17502 if (twin == NULL)
17503 nr = 0;
17504 }
17505 else
17506 {
17507 EMSG2(_(e_invexpr2), arg);
17508 nr = 0;
17509 }
17510 }
17511
17512 if (nr > 0)
17513 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17514 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017515 {
17516 if (wp == NULL)
17517 {
17518 /* didn't find it in this tabpage */
17519 nr = 0;
17520 break;
17521 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017522 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017523 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017524 return nr;
17525}
17526#endif
17527
17528/*
17529 * "tabpagewinnr()" function
17530 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017531 static void
17532f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017533 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017534 typval_T *rettv;
17535{
17536 int nr = 1;
17537#ifdef FEAT_WINDOWS
17538 tabpage_T *tp;
17539
17540 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17541 if (tp == NULL)
17542 nr = 0;
17543 else
17544 nr = get_winnr(tp, &argvars[1]);
17545#endif
17546 rettv->vval.v_number = nr;
17547}
17548
17549
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017550/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017551 * "tagfiles()" function
17552 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017553 static void
17554f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017555 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017556 typval_T *rettv;
17557{
17558 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017559 tagname_T tn;
17560 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017561
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017562 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017563 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017565 for (first = TRUE; ; first = FALSE)
17566 if (get_tagfname(&tn, first, fname) == FAIL
17567 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017568 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017569 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017570}
17571
17572/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017573 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017574 */
17575 static void
17576f_taglist(argvars, rettv)
17577 typval_T *argvars;
17578 typval_T *rettv;
17579{
17580 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017581
17582 tag_pattern = get_tv_string(&argvars[0]);
17583
17584 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017585 if (*tag_pattern == NUL)
17586 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017587
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017588 if (rettv_list_alloc(rettv) == OK)
17589 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017590}
17591
17592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 * "tempname()" function
17594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017596f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017597 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599{
17600 static int x = 'A';
17601
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017602 rettv->v_type = VAR_STRING;
17603 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017604
17605 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17606 * names. Skip 'I' and 'O', they are used for shell redirection. */
17607 do
17608 {
17609 if (x == 'Z')
17610 x = '0';
17611 else if (x == '9')
17612 x = 'A';
17613 else
17614 {
17615#ifdef EBCDIC
17616 if (x == 'I')
17617 x = 'J';
17618 else if (x == 'R')
17619 x = 'S';
17620 else
17621#endif
17622 ++x;
17623 }
17624 } while (x == 'I' || x == 'O');
17625}
17626
17627/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017628 * "test(list)" function: Just checking the walls...
17629 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017630 static void
17631f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017632 typval_T *argvars UNUSED;
17633 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017634{
17635 /* Used for unit testing. Change the code below to your liking. */
17636#if 0
17637 listitem_T *li;
17638 list_T *l;
17639 char_u *bad, *good;
17640
17641 if (argvars[0].v_type != VAR_LIST)
17642 return;
17643 l = argvars[0].vval.v_list;
17644 if (l == NULL)
17645 return;
17646 li = l->lv_first;
17647 if (li == NULL)
17648 return;
17649 bad = get_tv_string(&li->li_tv);
17650 li = li->li_next;
17651 if (li == NULL)
17652 return;
17653 good = get_tv_string(&li->li_tv);
17654 rettv->vval.v_number = test_edit_score(bad, good);
17655#endif
17656}
17657
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017658#ifdef FEAT_FLOAT
17659/*
17660 * "tan()" function
17661 */
17662 static void
17663f_tan(argvars, rettv)
17664 typval_T *argvars;
17665 typval_T *rettv;
17666{
17667 float_T f;
17668
17669 rettv->v_type = VAR_FLOAT;
17670 if (get_float_arg(argvars, &f) == OK)
17671 rettv->vval.v_float = tan(f);
17672 else
17673 rettv->vval.v_float = 0.0;
17674}
17675
17676/*
17677 * "tanh()" function
17678 */
17679 static void
17680f_tanh(argvars, rettv)
17681 typval_T *argvars;
17682 typval_T *rettv;
17683{
17684 float_T f;
17685
17686 rettv->v_type = VAR_FLOAT;
17687 if (get_float_arg(argvars, &f) == OK)
17688 rettv->vval.v_float = tanh(f);
17689 else
17690 rettv->vval.v_float = 0.0;
17691}
17692#endif
17693
Bram Moolenaard52d9742005-08-21 22:20:28 +000017694/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017695 * "tolower(string)" function
17696 */
17697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017698f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017699 typval_T *argvars;
17700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701{
17702 char_u *p;
17703
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017704 p = vim_strsave(get_tv_string(&argvars[0]));
17705 rettv->v_type = VAR_STRING;
17706 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017707
17708 if (p != NULL)
17709 while (*p != NUL)
17710 {
17711#ifdef FEAT_MBYTE
17712 int l;
17713
17714 if (enc_utf8)
17715 {
17716 int c, lc;
17717
17718 c = utf_ptr2char(p);
17719 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017720 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 /* TODO: reallocate string when byte count changes. */
17722 if (utf_char2len(lc) == l)
17723 utf_char2bytes(lc, p);
17724 p += l;
17725 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017726 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017727 p += l; /* skip multi-byte character */
17728 else
17729#endif
17730 {
17731 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17732 ++p;
17733 }
17734 }
17735}
17736
17737/*
17738 * "toupper(string)" function
17739 */
17740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017741f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017742 typval_T *argvars;
17743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017745 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017746 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017747}
17748
17749/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017750 * "tr(string, fromstr, tostr)" function
17751 */
17752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017754 typval_T *argvars;
17755 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017756{
17757 char_u *instr;
17758 char_u *fromstr;
17759 char_u *tostr;
17760 char_u *p;
17761#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017762 int inlen;
17763 int fromlen;
17764 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017765 int idx;
17766 char_u *cpstr;
17767 int cplen;
17768 int first = TRUE;
17769#endif
17770 char_u buf[NUMBUFLEN];
17771 char_u buf2[NUMBUFLEN];
17772 garray_T ga;
17773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017774 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017775 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17776 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017777
17778 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017779 rettv->v_type = VAR_STRING;
17780 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017781 if (fromstr == NULL || tostr == NULL)
17782 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017783 ga_init2(&ga, (int)sizeof(char), 80);
17784
17785#ifdef FEAT_MBYTE
17786 if (!has_mbyte)
17787#endif
17788 /* not multi-byte: fromstr and tostr must be the same length */
17789 if (STRLEN(fromstr) != STRLEN(tostr))
17790 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017791#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017792error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017793#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017794 EMSG2(_(e_invarg2), fromstr);
17795 ga_clear(&ga);
17796 return;
17797 }
17798
17799 /* fromstr and tostr have to contain the same number of chars */
17800 while (*instr != NUL)
17801 {
17802#ifdef FEAT_MBYTE
17803 if (has_mbyte)
17804 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017805 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017806 cpstr = instr;
17807 cplen = inlen;
17808 idx = 0;
17809 for (p = fromstr; *p != NUL; p += fromlen)
17810 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017811 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017812 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17813 {
17814 for (p = tostr; *p != NUL; p += tolen)
17815 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017816 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017817 if (idx-- == 0)
17818 {
17819 cplen = tolen;
17820 cpstr = p;
17821 break;
17822 }
17823 }
17824 if (*p == NUL) /* tostr is shorter than fromstr */
17825 goto error;
17826 break;
17827 }
17828 ++idx;
17829 }
17830
17831 if (first && cpstr == instr)
17832 {
17833 /* Check that fromstr and tostr have the same number of
17834 * (multi-byte) characters. Done only once when a character
17835 * of instr doesn't appear in fromstr. */
17836 first = FALSE;
17837 for (p = tostr; *p != NUL; p += tolen)
17838 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017839 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017840 --idx;
17841 }
17842 if (idx != 0)
17843 goto error;
17844 }
17845
17846 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017847 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017848 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017849
17850 instr += inlen;
17851 }
17852 else
17853#endif
17854 {
17855 /* When not using multi-byte chars we can do it faster. */
17856 p = vim_strchr(fromstr, *instr);
17857 if (p != NULL)
17858 ga_append(&ga, tostr[p - fromstr]);
17859 else
17860 ga_append(&ga, *instr);
17861 ++instr;
17862 }
17863 }
17864
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017865 /* add a terminating NUL */
17866 ga_grow(&ga, 1);
17867 ga_append(&ga, NUL);
17868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017869 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017870}
17871
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017872#ifdef FEAT_FLOAT
17873/*
17874 * "trunc({float})" function
17875 */
17876 static void
17877f_trunc(argvars, rettv)
17878 typval_T *argvars;
17879 typval_T *rettv;
17880{
17881 float_T f;
17882
17883 rettv->v_type = VAR_FLOAT;
17884 if (get_float_arg(argvars, &f) == OK)
17885 /* trunc() is not in C90, use floor() or ceil() instead. */
17886 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17887 else
17888 rettv->vval.v_float = 0.0;
17889}
17890#endif
17891
Bram Moolenaar8299df92004-07-10 09:47:34 +000017892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 * "type(expr)" function
17894 */
17895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017896f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017897 typval_T *argvars;
17898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017900 int n;
17901
17902 switch (argvars[0].v_type)
17903 {
17904 case VAR_NUMBER: n = 0; break;
17905 case VAR_STRING: n = 1; break;
17906 case VAR_FUNC: n = 2; break;
17907 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017908 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017909#ifdef FEAT_FLOAT
17910 case VAR_FLOAT: n = 5; break;
17911#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017912 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17913 }
17914 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915}
17916
17917/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017918 * "undofile(name)" function
17919 */
17920 static void
17921f_undofile(argvars, rettv)
17922 typval_T *argvars;
17923 typval_T *rettv;
17924{
17925 rettv->v_type = VAR_STRING;
17926#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017927 {
17928 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17929
17930 if (ffname != NULL)
17931 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17932 vim_free(ffname);
17933 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017934#else
17935 rettv->vval.v_string = NULL;
17936#endif
17937}
17938
17939/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017940 * "undotree()" function
17941 */
17942 static void
17943f_undotree(argvars, rettv)
17944 typval_T *argvars UNUSED;
17945 typval_T *rettv;
17946{
17947 if (rettv_dict_alloc(rettv) == OK)
17948 {
17949 dict_T *dict = rettv->vval.v_dict;
17950 list_T *list;
17951
Bram Moolenaar730cde92010-06-27 05:18:54 +020017952 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017953 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017954 dict_add_nr_str(dict, "save_last",
17955 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017956 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17957 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017958 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017959
17960 list = list_alloc();
17961 if (list != NULL)
17962 {
17963 u_eval_tree(curbuf->b_u_oldhead, list);
17964 dict_add_list(dict, "entries", list);
17965 }
17966 }
17967}
17968
17969/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017970 * "values(dict)" function
17971 */
17972 static void
17973f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017974 typval_T *argvars;
17975 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017976{
17977 dict_list(argvars, rettv, 1);
17978}
17979
17980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981 * "virtcol(string)" function
17982 */
17983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017984f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017985 typval_T *argvars;
17986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987{
17988 colnr_T vcol = 0;
17989 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017990 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017992 fp = var2fpos(&argvars[0], FALSE, &fnum);
17993 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17994 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017995 {
17996 getvvcol(curwin, fp, NULL, NULL, &vcol);
17997 ++vcol;
17998 }
17999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018000 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001}
18002
18003/*
18004 * "visualmode()" function
18005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018007f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018008 typval_T *argvars UNUSED;
18009 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018010{
18011#ifdef FEAT_VISUAL
18012 char_u str[2];
18013
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018014 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 str[0] = curbuf->b_visual_mode_eval;
18016 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018017 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018
18019 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018020 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018021 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022#endif
18023}
18024
18025/*
18026 * "winbufnr(nr)" function
18027 */
18028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018029f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018030 typval_T *argvars;
18031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032{
18033 win_T *wp;
18034
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018035 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018037 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018039 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040}
18041
18042/*
18043 * "wincol()" function
18044 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018046f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018047 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049{
18050 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018051 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052}
18053
18054/*
18055 * "winheight(nr)" function
18056 */
18057 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018058f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018059 typval_T *argvars;
18060 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061{
18062 win_T *wp;
18063
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018064 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018066 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018068 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018069}
18070
18071/*
18072 * "winline()" function
18073 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018075f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018076 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018077 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078{
18079 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018080 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081}
18082
18083/*
18084 * "winnr()" function
18085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018087f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018088 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018089 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090{
18091 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018092
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018094 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018096 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097}
18098
18099/*
18100 * "winrestcmd()" function
18101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018103f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018104 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106{
18107#ifdef FEAT_WINDOWS
18108 win_T *wp;
18109 int winnr = 1;
18110 garray_T ga;
18111 char_u buf[50];
18112
18113 ga_init2(&ga, (int)sizeof(char), 70);
18114 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18115 {
18116 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18117 ga_concat(&ga, buf);
18118# ifdef FEAT_VERTSPLIT
18119 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18120 ga_concat(&ga, buf);
18121# endif
18122 ++winnr;
18123 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018124 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018126 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018128 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018130 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131}
18132
18133/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018134 * "winrestview()" function
18135 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018136 static void
18137f_winrestview(argvars, rettv)
18138 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018139 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018140{
18141 dict_T *dict;
18142
18143 if (argvars[0].v_type != VAR_DICT
18144 || (dict = argvars[0].vval.v_dict) == NULL)
18145 EMSG(_(e_invarg));
18146 else
18147 {
18148 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18149 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18150#ifdef FEAT_VIRTUALEDIT
18151 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18152#endif
18153 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018154 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018155
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018156 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018157#ifdef FEAT_DIFF
18158 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18159#endif
18160 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18161 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18162
18163 check_cursor();
18164 changed_cline_bef_curs();
18165 invalidate_botline();
18166 redraw_later(VALID);
18167
18168 if (curwin->w_topline == 0)
18169 curwin->w_topline = 1;
18170 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18171 curwin->w_topline = curbuf->b_ml.ml_line_count;
18172#ifdef FEAT_DIFF
18173 check_topfill(curwin, TRUE);
18174#endif
18175 }
18176}
18177
18178/*
18179 * "winsaveview()" function
18180 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018181 static void
18182f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018183 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018184 typval_T *rettv;
18185{
18186 dict_T *dict;
18187
Bram Moolenaara800b422010-06-27 01:15:55 +020018188 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018189 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018190 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018191
18192 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18193 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18194#ifdef FEAT_VIRTUALEDIT
18195 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18196#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018197 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018198 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18199
18200 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18201#ifdef FEAT_DIFF
18202 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18203#endif
18204 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18205 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18206}
18207
18208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018209 * "winwidth(nr)" function
18210 */
18211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018212f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018213 typval_T *argvars;
18214 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215{
18216 win_T *wp;
18217
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018218 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018220 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 else
18222#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018223 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018225 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226#endif
18227}
18228
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018230 * "writefile()" function
18231 */
18232 static void
18233f_writefile(argvars, rettv)
18234 typval_T *argvars;
18235 typval_T *rettv;
18236{
18237 int binary = FALSE;
18238 char_u *fname;
18239 FILE *fd;
18240 listitem_T *li;
18241 char_u *s;
18242 int ret = 0;
18243 int c;
18244
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018245 if (check_restricted() || check_secure())
18246 return;
18247
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018248 if (argvars[0].v_type != VAR_LIST)
18249 {
18250 EMSG2(_(e_listarg), "writefile()");
18251 return;
18252 }
18253 if (argvars[0].vval.v_list == NULL)
18254 return;
18255
18256 if (argvars[2].v_type != VAR_UNKNOWN
18257 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18258 binary = TRUE;
18259
18260 /* Always open the file in binary mode, library functions have a mind of
18261 * their own about CR-LF conversion. */
18262 fname = get_tv_string(&argvars[1]);
18263 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18264 {
18265 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18266 ret = -1;
18267 }
18268 else
18269 {
18270 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18271 li = li->li_next)
18272 {
18273 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18274 {
18275 if (*s == '\n')
18276 c = putc(NUL, fd);
18277 else
18278 c = putc(*s, fd);
18279 if (c == EOF)
18280 {
18281 ret = -1;
18282 break;
18283 }
18284 }
18285 if (!binary || li->li_next != NULL)
18286 if (putc('\n', fd) == EOF)
18287 {
18288 ret = -1;
18289 break;
18290 }
18291 if (ret < 0)
18292 {
18293 EMSG(_(e_write));
18294 break;
18295 }
18296 }
18297 fclose(fd);
18298 }
18299
18300 rettv->vval.v_number = ret;
18301}
18302
18303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018305 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 */
18307 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018308var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018309 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018310 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018311 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018312{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018313 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018315 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316
Bram Moolenaara5525202006-03-02 22:52:09 +000018317 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018318 if (varp->v_type == VAR_LIST)
18319 {
18320 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018321 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018322 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018323 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018324
18325 l = varp->vval.v_list;
18326 if (l == NULL)
18327 return NULL;
18328
18329 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018330 pos.lnum = list_find_nr(l, 0L, &error);
18331 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018332 return NULL; /* invalid line number */
18333
18334 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018335 pos.col = list_find_nr(l, 1L, &error);
18336 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018337 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018338 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018339
18340 /* We accept "$" for the column number: last column. */
18341 li = list_find(l, 1L);
18342 if (li != NULL && li->li_tv.v_type == VAR_STRING
18343 && li->li_tv.vval.v_string != NULL
18344 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18345 pos.col = len + 1;
18346
Bram Moolenaara5525202006-03-02 22:52:09 +000018347 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018348 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018349 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018350 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018351
Bram Moolenaara5525202006-03-02 22:52:09 +000018352#ifdef FEAT_VIRTUALEDIT
18353 /* Get the virtual offset. Defaults to zero. */
18354 pos.coladd = list_find_nr(l, 2L, &error);
18355 if (error)
18356 pos.coladd = 0;
18357#endif
18358
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018359 return &pos;
18360 }
18361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018362 name = get_tv_string_chk(varp);
18363 if (name == NULL)
18364 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018365 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018367#ifdef FEAT_VISUAL
18368 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18369 {
18370 if (VIsual_active)
18371 return &VIsual;
18372 return &curwin->w_cursor;
18373 }
18374#endif
18375 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018376 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018377 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018378 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18379 return NULL;
18380 return pp;
18381 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018382
18383#ifdef FEAT_VIRTUALEDIT
18384 pos.coladd = 0;
18385#endif
18386
Bram Moolenaar477933c2007-07-17 14:32:23 +000018387 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018388 {
18389 pos.col = 0;
18390 if (name[1] == '0') /* "w0": first visible line */
18391 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018392 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018393 pos.lnum = curwin->w_topline;
18394 return &pos;
18395 }
18396 else if (name[1] == '$') /* "w$": last visible line */
18397 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018398 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018399 pos.lnum = curwin->w_botline - 1;
18400 return &pos;
18401 }
18402 }
18403 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018405 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406 {
18407 pos.lnum = curbuf->b_ml.ml_line_count;
18408 pos.col = 0;
18409 }
18410 else
18411 {
18412 pos.lnum = curwin->w_cursor.lnum;
18413 pos.col = (colnr_T)STRLEN(ml_get_curline());
18414 }
18415 return &pos;
18416 }
18417 return NULL;
18418}
18419
18420/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018421 * Convert list in "arg" into a position and optional file number.
18422 * When "fnump" is NULL there is no file number, only 3 items.
18423 * Note that the column is passed on as-is, the caller may want to decrement
18424 * it to use 1 for the first column.
18425 * Return FAIL when conversion is not possible, doesn't check the position for
18426 * validity.
18427 */
18428 static int
18429list2fpos(arg, posp, fnump)
18430 typval_T *arg;
18431 pos_T *posp;
18432 int *fnump;
18433{
18434 list_T *l = arg->vval.v_list;
18435 long i = 0;
18436 long n;
18437
Bram Moolenaarbde35262006-07-23 20:12:24 +000018438 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18439 * when "fnump" isn't NULL and "coladd" is optional. */
18440 if (arg->v_type != VAR_LIST
18441 || l == NULL
18442 || l->lv_len < (fnump == NULL ? 2 : 3)
18443 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018444 return FAIL;
18445
18446 if (fnump != NULL)
18447 {
18448 n = list_find_nr(l, i++, NULL); /* fnum */
18449 if (n < 0)
18450 return FAIL;
18451 if (n == 0)
18452 n = curbuf->b_fnum; /* current buffer */
18453 *fnump = n;
18454 }
18455
18456 n = list_find_nr(l, i++, NULL); /* lnum */
18457 if (n < 0)
18458 return FAIL;
18459 posp->lnum = n;
18460
18461 n = list_find_nr(l, i++, NULL); /* col */
18462 if (n < 0)
18463 return FAIL;
18464 posp->col = n;
18465
18466#ifdef FEAT_VIRTUALEDIT
18467 n = list_find_nr(l, i, NULL);
18468 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018469 posp->coladd = 0;
18470 else
18471 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018472#endif
18473
18474 return OK;
18475}
18476
18477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478 * Get the length of an environment variable name.
18479 * Advance "arg" to the first character after the name.
18480 * Return 0 for error.
18481 */
18482 static int
18483get_env_len(arg)
18484 char_u **arg;
18485{
18486 char_u *p;
18487 int len;
18488
18489 for (p = *arg; vim_isIDc(*p); ++p)
18490 ;
18491 if (p == *arg) /* no name found */
18492 return 0;
18493
18494 len = (int)(p - *arg);
18495 *arg = p;
18496 return len;
18497}
18498
18499/*
18500 * Get the length of the name of a function or internal variable.
18501 * "arg" is advanced to the first non-white character after the name.
18502 * Return 0 if something is wrong.
18503 */
18504 static int
18505get_id_len(arg)
18506 char_u **arg;
18507{
18508 char_u *p;
18509 int len;
18510
18511 /* Find the end of the name. */
18512 for (p = *arg; eval_isnamec(*p); ++p)
18513 ;
18514 if (p == *arg) /* no name found */
18515 return 0;
18516
18517 len = (int)(p - *arg);
18518 *arg = skipwhite(p);
18519
18520 return len;
18521}
18522
18523/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018524 * Get the length of the name of a variable or function.
18525 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018527 * Return -1 if curly braces expansion failed.
18528 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018529 * If the name contains 'magic' {}'s, expand them and return the
18530 * expanded name in an allocated string via 'alias' - caller must free.
18531 */
18532 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018533get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 char_u **arg;
18535 char_u **alias;
18536 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018537 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538{
18539 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 char_u *p;
18541 char_u *expr_start;
18542 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543
18544 *alias = NULL; /* default to no alias */
18545
18546 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18547 && (*arg)[2] == (int)KE_SNR)
18548 {
18549 /* hard coded <SNR>, already translated */
18550 *arg += 3;
18551 return get_id_len(arg) + 3;
18552 }
18553 len = eval_fname_script(*arg);
18554 if (len > 0)
18555 {
18556 /* literal "<SID>", "s:" or "<SNR>" */
18557 *arg += len;
18558 }
18559
Bram Moolenaar071d4272004-06-13 20:20:40 +000018560 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018561 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018563 p = find_name_end(*arg, &expr_start, &expr_end,
18564 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565 if (expr_start != NULL)
18566 {
18567 char_u *temp_string;
18568
18569 if (!evaluate)
18570 {
18571 len += (int)(p - *arg);
18572 *arg = skipwhite(p);
18573 return len;
18574 }
18575
18576 /*
18577 * Include any <SID> etc in the expanded string:
18578 * Thus the -len here.
18579 */
18580 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18581 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018582 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 *alias = temp_string;
18584 *arg = skipwhite(p);
18585 return (int)STRLEN(temp_string);
18586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587
18588 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018589 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 EMSG2(_(e_invexpr2), *arg);
18591
18592 return len;
18593}
18594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018595/*
18596 * Find the end of a variable or function name, taking care of magic braces.
18597 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18598 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018599 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018600 * Return a pointer to just after the name. Equal to "arg" if there is no
18601 * valid name.
18602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018604find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018605 char_u *arg;
18606 char_u **expr_start;
18607 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018608 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 int mb_nest = 0;
18611 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 char_u *p;
18613
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018614 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018615 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018616 *expr_start = NULL;
18617 *expr_end = NULL;
18618 }
18619
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018620 /* Quick check for valid starting character. */
18621 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18622 return arg;
18623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018624 for (p = arg; *p != NUL
18625 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018626 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018627 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018628 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018629 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018630 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018631 if (*p == '\'')
18632 {
18633 /* skip over 'string' to avoid counting [ and ] inside it. */
18634 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18635 ;
18636 if (*p == NUL)
18637 break;
18638 }
18639 else if (*p == '"')
18640 {
18641 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18642 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18643 if (*p == '\\' && p[1] != NUL)
18644 ++p;
18645 if (*p == NUL)
18646 break;
18647 }
18648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018649 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018651 if (*p == '[')
18652 ++br_nest;
18653 else if (*p == ']')
18654 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018656
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018657 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659 if (*p == '{')
18660 {
18661 mb_nest++;
18662 if (expr_start != NULL && *expr_start == NULL)
18663 *expr_start = p;
18664 }
18665 else if (*p == '}')
18666 {
18667 mb_nest--;
18668 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18669 *expr_end = p;
18670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672 }
18673
18674 return p;
18675}
18676
18677/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018678 * Expands out the 'magic' {}'s in a variable/function name.
18679 * Note that this can call itself recursively, to deal with
18680 * constructs like foo{bar}{baz}{bam}
18681 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18682 * "in_start" ^
18683 * "expr_start" ^
18684 * "expr_end" ^
18685 * "in_end" ^
18686 *
18687 * Returns a new allocated string, which the caller must free.
18688 * Returns NULL for failure.
18689 */
18690 static char_u *
18691make_expanded_name(in_start, expr_start, expr_end, in_end)
18692 char_u *in_start;
18693 char_u *expr_start;
18694 char_u *expr_end;
18695 char_u *in_end;
18696{
18697 char_u c1;
18698 char_u *retval = NULL;
18699 char_u *temp_result;
18700 char_u *nextcmd = NULL;
18701
18702 if (expr_end == NULL || in_end == NULL)
18703 return NULL;
18704 *expr_start = NUL;
18705 *expr_end = NUL;
18706 c1 = *in_end;
18707 *in_end = NUL;
18708
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018709 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018710 if (temp_result != NULL && nextcmd == NULL)
18711 {
18712 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18713 + (in_end - expr_end) + 1));
18714 if (retval != NULL)
18715 {
18716 STRCPY(retval, in_start);
18717 STRCAT(retval, temp_result);
18718 STRCAT(retval, expr_end + 1);
18719 }
18720 }
18721 vim_free(temp_result);
18722
18723 *in_end = c1; /* put char back for error messages */
18724 *expr_start = '{';
18725 *expr_end = '}';
18726
18727 if (retval != NULL)
18728 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018729 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018730 if (expr_start != NULL)
18731 {
18732 /* Further expansion! */
18733 temp_result = make_expanded_name(retval, expr_start,
18734 expr_end, temp_result);
18735 vim_free(retval);
18736 retval = temp_result;
18737 }
18738 }
18739
18740 return retval;
18741}
18742
18743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018745 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 */
18747 static int
18748eval_isnamec(c)
18749 int c;
18750{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018751 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18752}
18753
18754/*
18755 * Return TRUE if character "c" can be used as the first character in a
18756 * variable or function name (excluding '{' and '}').
18757 */
18758 static int
18759eval_isnamec1(c)
18760 int c;
18761{
18762 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763}
18764
18765/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 * Set number v: variable to "val".
18767 */
18768 void
18769set_vim_var_nr(idx, val)
18770 int idx;
18771 long val;
18772{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018773 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774}
18775
18776/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018777 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 */
18779 long
18780get_vim_var_nr(idx)
18781 int idx;
18782{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018783 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784}
18785
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018786/*
18787 * Get string v: variable value. Uses a static buffer, can only be used once.
18788 */
18789 char_u *
18790get_vim_var_str(idx)
18791 int idx;
18792{
18793 return get_tv_string(&vimvars[idx].vv_tv);
18794}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018795
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018797 * Get List v: variable value. Caller must take care of reference count when
18798 * needed.
18799 */
18800 list_T *
18801get_vim_var_list(idx)
18802 int idx;
18803{
18804 return vimvars[idx].vv_list;
18805}
18806
18807/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018808 * Set v:char to character "c".
18809 */
18810 void
18811set_vim_var_char(c)
18812 int c;
18813{
18814#ifdef FEAT_MBYTE
18815 char_u buf[MB_MAXBYTES];
18816#else
18817 char_u buf[2];
18818#endif
18819
18820#ifdef FEAT_MBYTE
18821 if (has_mbyte)
18822 buf[(*mb_char2bytes)(c, buf)] = NUL;
18823 else
18824#endif
18825 {
18826 buf[0] = c;
18827 buf[1] = NUL;
18828 }
18829 set_vim_var_string(VV_CHAR, buf, -1);
18830}
18831
18832/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018833 * Set v:count to "count" and v:count1 to "count1".
18834 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 */
18836 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018837set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838 long count;
18839 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018840 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018842 if (set_prevcount)
18843 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018844 vimvars[VV_COUNT].vv_nr = count;
18845 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846}
18847
18848/*
18849 * Set string v: variable to a copy of "val".
18850 */
18851 void
18852set_vim_var_string(idx, val, len)
18853 int idx;
18854 char_u *val;
18855 int len; /* length of "val" to use or -1 (whole string) */
18856{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018857 /* Need to do this (at least) once, since we can't initialize a union.
18858 * Will always be invoked when "v:progname" is set. */
18859 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18860
Bram Moolenaare9a41262005-01-15 22:18:47 +000018861 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018862 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018863 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018865 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018867 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868}
18869
18870/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018871 * Set List v: variable to "val".
18872 */
18873 void
18874set_vim_var_list(idx, val)
18875 int idx;
18876 list_T *val;
18877{
18878 list_unref(vimvars[idx].vv_list);
18879 vimvars[idx].vv_list = val;
18880 if (val != NULL)
18881 ++val->lv_refcount;
18882}
18883
18884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885 * Set v:register if needed.
18886 */
18887 void
18888set_reg_var(c)
18889 int c;
18890{
18891 char_u regname;
18892
18893 if (c == 0 || c == ' ')
18894 regname = '"';
18895 else
18896 regname = c;
18897 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018898 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 set_vim_var_string(VV_REG, &regname, 1);
18900}
18901
18902/*
18903 * Get or set v:exception. If "oldval" == NULL, return the current value.
18904 * Otherwise, restore the value to "oldval" and return NULL.
18905 * Must always be called in pairs to save and restore v:exception! Does not
18906 * take care of memory allocations.
18907 */
18908 char_u *
18909v_exception(oldval)
18910 char_u *oldval;
18911{
18912 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018913 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018914
Bram Moolenaare9a41262005-01-15 22:18:47 +000018915 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916 return NULL;
18917}
18918
18919/*
18920 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18921 * Otherwise, restore the value to "oldval" and return NULL.
18922 * Must always be called in pairs to save and restore v:throwpoint! Does not
18923 * take care of memory allocations.
18924 */
18925 char_u *
18926v_throwpoint(oldval)
18927 char_u *oldval;
18928{
18929 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018930 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931
Bram Moolenaare9a41262005-01-15 22:18:47 +000018932 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 return NULL;
18934}
18935
18936#if defined(FEAT_AUTOCMD) || defined(PROTO)
18937/*
18938 * Set v:cmdarg.
18939 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18940 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18941 * Must always be called in pairs!
18942 */
18943 char_u *
18944set_cmdarg(eap, oldarg)
18945 exarg_T *eap;
18946 char_u *oldarg;
18947{
18948 char_u *oldval;
18949 char_u *newval;
18950 unsigned len;
18951
Bram Moolenaare9a41262005-01-15 22:18:47 +000018952 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018953 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018955 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018956 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018957 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 }
18959
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018960 if (eap->force_bin == FORCE_BIN)
18961 len = 6;
18962 else if (eap->force_bin == FORCE_NOBIN)
18963 len = 8;
18964 else
18965 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018966
18967 if (eap->read_edit)
18968 len += 7;
18969
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018970 if (eap->force_ff != 0)
18971 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18972# ifdef FEAT_MBYTE
18973 if (eap->force_enc != 0)
18974 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018975 if (eap->bad_char != 0)
18976 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018977# endif
18978
18979 newval = alloc(len + 1);
18980 if (newval == NULL)
18981 return NULL;
18982
18983 if (eap->force_bin == FORCE_BIN)
18984 sprintf((char *)newval, " ++bin");
18985 else if (eap->force_bin == FORCE_NOBIN)
18986 sprintf((char *)newval, " ++nobin");
18987 else
18988 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018989
18990 if (eap->read_edit)
18991 STRCAT(newval, " ++edit");
18992
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018993 if (eap->force_ff != 0)
18994 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18995 eap->cmd + eap->force_ff);
18996# ifdef FEAT_MBYTE
18997 if (eap->force_enc != 0)
18998 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18999 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019000 if (eap->bad_char == BAD_KEEP)
19001 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19002 else if (eap->bad_char == BAD_DROP)
19003 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19004 else if (eap->bad_char != 0)
19005 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019006# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019007 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019008 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009}
19010#endif
19011
19012/*
19013 * Get the value of internal variable "name".
19014 * Return OK or FAIL.
19015 */
19016 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019017get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 char_u *name;
19019 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019020 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019021 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019022{
19023 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019024 typval_T *tv = NULL;
19025 typval_T atv;
19026 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019027 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028
19029 /* truncate the name, so that we can use strcmp() */
19030 cc = name[len];
19031 name[len] = NUL;
19032
19033 /*
19034 * Check for "b:changedtick".
19035 */
19036 if (STRCMP(name, "b:changedtick") == 0)
19037 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019038 atv.v_type = VAR_NUMBER;
19039 atv.vval.v_number = curbuf->b_changedtick;
19040 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 }
19042
19043 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 * Check for user-defined variables.
19045 */
19046 else
19047 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019048 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019050 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 }
19052
Bram Moolenaare9a41262005-01-15 22:18:47 +000019053 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019055 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019056 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 ret = FAIL;
19058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019059 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019060 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019061
19062 name[len] = cc;
19063
19064 return ret;
19065}
19066
19067/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019068 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19069 * Also handle function call with Funcref variable: func(expr)
19070 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19071 */
19072 static int
19073handle_subscript(arg, rettv, evaluate, verbose)
19074 char_u **arg;
19075 typval_T *rettv;
19076 int evaluate; /* do more than finding the end */
19077 int verbose; /* give error messages */
19078{
19079 int ret = OK;
19080 dict_T *selfdict = NULL;
19081 char_u *s;
19082 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019083 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019084
19085 while (ret == OK
19086 && (**arg == '['
19087 || (**arg == '.' && rettv->v_type == VAR_DICT)
19088 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19089 && !vim_iswhite(*(*arg - 1)))
19090 {
19091 if (**arg == '(')
19092 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019093 /* need to copy the funcref so that we can clear rettv */
19094 functv = *rettv;
19095 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019096
19097 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019098 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019099 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019100 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19101 &len, evaluate, selfdict);
19102
19103 /* Clear the funcref afterwards, so that deleting it while
19104 * evaluating the arguments is possible (see test55). */
19105 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019106
19107 /* Stop the expression evaluation when immediately aborting on
19108 * error, or when an interrupt occurred or an exception was thrown
19109 * but not caught. */
19110 if (aborting())
19111 {
19112 if (ret == OK)
19113 clear_tv(rettv);
19114 ret = FAIL;
19115 }
19116 dict_unref(selfdict);
19117 selfdict = NULL;
19118 }
19119 else /* **arg == '[' || **arg == '.' */
19120 {
19121 dict_unref(selfdict);
19122 if (rettv->v_type == VAR_DICT)
19123 {
19124 selfdict = rettv->vval.v_dict;
19125 if (selfdict != NULL)
19126 ++selfdict->dv_refcount;
19127 }
19128 else
19129 selfdict = NULL;
19130 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19131 {
19132 clear_tv(rettv);
19133 ret = FAIL;
19134 }
19135 }
19136 }
19137 dict_unref(selfdict);
19138 return ret;
19139}
19140
19141/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019142 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019143 * value).
19144 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019145 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019146alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019147{
Bram Moolenaar33570922005-01-25 22:26:29 +000019148 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019149}
19150
19151/*
19152 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019153 * The string "s" must have been allocated, it is consumed.
19154 * Return NULL for out of memory, the variable otherwise.
19155 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019156 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019157alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158 char_u *s;
19159{
Bram Moolenaar33570922005-01-25 22:26:29 +000019160 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019162 rettv = alloc_tv();
19163 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019165 rettv->v_type = VAR_STRING;
19166 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019167 }
19168 else
19169 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171}
19172
19173/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019174 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019176 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019177free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179{
19180 if (varp != NULL)
19181 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019182 switch (varp->v_type)
19183 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019184 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019185 func_unref(varp->vval.v_string);
19186 /*FALLTHROUGH*/
19187 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019188 vim_free(varp->vval.v_string);
19189 break;
19190 case VAR_LIST:
19191 list_unref(varp->vval.v_list);
19192 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019193 case VAR_DICT:
19194 dict_unref(varp->vval.v_dict);
19195 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019196 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019197#ifdef FEAT_FLOAT
19198 case VAR_FLOAT:
19199#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019200 case VAR_UNKNOWN:
19201 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019202 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019203 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019204 break;
19205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 vim_free(varp);
19207 }
19208}
19209
19210/*
19211 * Free the memory for a variable value and set the value to NULL or 0.
19212 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019213 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019214clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019215 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216{
19217 if (varp != NULL)
19218 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019219 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019221 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019222 func_unref(varp->vval.v_string);
19223 /*FALLTHROUGH*/
19224 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019225 vim_free(varp->vval.v_string);
19226 varp->vval.v_string = NULL;
19227 break;
19228 case VAR_LIST:
19229 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019230 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019231 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019232 case VAR_DICT:
19233 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019234 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019235 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019236 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019237 varp->vval.v_number = 0;
19238 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019239#ifdef FEAT_FLOAT
19240 case VAR_FLOAT:
19241 varp->vval.v_float = 0.0;
19242 break;
19243#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019244 case VAR_UNKNOWN:
19245 break;
19246 default:
19247 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019248 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019249 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 }
19251}
19252
19253/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019254 * Set the value of a variable to NULL without freeing items.
19255 */
19256 static void
19257init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019258 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019259{
19260 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019261 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019262}
19263
19264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265 * Get the number value of a variable.
19266 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019267 * For incompatible types, return 0.
19268 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19269 * caller of incompatible types: it sets *denote to TRUE if "denote"
19270 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 */
19272 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019273get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019274 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019276 int error = FALSE;
19277
19278 return get_tv_number_chk(varp, &error); /* return 0L on error */
19279}
19280
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019281 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019282get_tv_number_chk(varp, denote)
19283 typval_T *varp;
19284 int *denote;
19285{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019286 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019288 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019290 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019291 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019292#ifdef FEAT_FLOAT
19293 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019294 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019295 break;
19296#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019297 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019298 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019299 break;
19300 case VAR_STRING:
19301 if (varp->vval.v_string != NULL)
19302 vim_str2nr(varp->vval.v_string, NULL, NULL,
19303 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019304 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019305 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019306 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019307 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019308 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019309 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019310 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019311 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019312 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019313 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019315 if (denote == NULL) /* useful for values that must be unsigned */
19316 n = -1;
19317 else
19318 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019319 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320}
19321
19322/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019323 * Get the lnum from the first argument.
19324 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019325 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326 */
19327 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019328get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019329 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330{
Bram Moolenaar33570922005-01-25 22:26:29 +000019331 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 linenr_T lnum;
19333
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019334 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335 if (lnum == 0) /* no valid number, try using line() */
19336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019337 rettv.v_type = VAR_NUMBER;
19338 f_line(argvars, &rettv);
19339 lnum = rettv.vval.v_number;
19340 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 }
19342 return lnum;
19343}
19344
19345/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019346 * Get the lnum from the first argument.
19347 * Also accepts "$", then "buf" is used.
19348 * Returns 0 on error.
19349 */
19350 static linenr_T
19351get_tv_lnum_buf(argvars, buf)
19352 typval_T *argvars;
19353 buf_T *buf;
19354{
19355 if (argvars[0].v_type == VAR_STRING
19356 && argvars[0].vval.v_string != NULL
19357 && argvars[0].vval.v_string[0] == '$'
19358 && buf != NULL)
19359 return buf->b_ml.ml_line_count;
19360 return get_tv_number_chk(&argvars[0], NULL);
19361}
19362
19363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 * Get the string value of a variable.
19365 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019366 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19367 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019368 * If the String variable has never been set, return an empty string.
19369 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019370 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19371 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019372 */
19373 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019374get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019375 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376{
19377 static char_u mybuf[NUMBUFLEN];
19378
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019379 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380}
19381
19382 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019383get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019384 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019385 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019387 char_u *res = get_tv_string_buf_chk(varp, buf);
19388
19389 return res != NULL ? res : (char_u *)"";
19390}
19391
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019392 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019393get_tv_string_chk(varp)
19394 typval_T *varp;
19395{
19396 static char_u mybuf[NUMBUFLEN];
19397
19398 return get_tv_string_buf_chk(varp, mybuf);
19399}
19400
19401 static char_u *
19402get_tv_string_buf_chk(varp, buf)
19403 typval_T *varp;
19404 char_u *buf;
19405{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019406 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019407 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019408 case VAR_NUMBER:
19409 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19410 return buf;
19411 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019412 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019413 break;
19414 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019415 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019416 break;
19417 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019418 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019419 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019420#ifdef FEAT_FLOAT
19421 case VAR_FLOAT:
19422 EMSG(_("E806: using Float as a String"));
19423 break;
19424#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019425 case VAR_STRING:
19426 if (varp->vval.v_string != NULL)
19427 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019428 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019429 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019430 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019432 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019433 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434}
19435
19436/*
19437 * Find variable "name" in the list of variables.
19438 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019439 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019440 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019441 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019443 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019444find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019446 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019448 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019449 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019450
Bram Moolenaara7043832005-01-21 11:56:39 +000019451 ht = find_var_ht(name, &varname);
19452 if (htp != NULL)
19453 *htp = ht;
19454 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019456 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457}
19458
19459/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019460 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019461 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019462 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019463 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019464find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019465 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019466 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019467 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019468{
Bram Moolenaar33570922005-01-25 22:26:29 +000019469 hashitem_T *hi;
19470
19471 if (*varname == NUL)
19472 {
19473 /* Must be something like "s:", otherwise "ht" would be NULL. */
19474 switch (varname[-2])
19475 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019476 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019477 case 'g': return &globvars_var;
19478 case 'v': return &vimvars_var;
19479 case 'b': return &curbuf->b_bufvar;
19480 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019481#ifdef FEAT_WINDOWS
19482 case 't': return &curtab->tp_winvar;
19483#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019484 case 'l': return current_funccal == NULL
19485 ? NULL : &current_funccal->l_vars_var;
19486 case 'a': return current_funccal == NULL
19487 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019488 }
19489 return NULL;
19490 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019491
19492 hi = hash_find(ht, varname);
19493 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019494 {
19495 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019496 * worked find the variable again. Don't auto-load a script if it was
19497 * loaded already, otherwise it would be loaded every time when
19498 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019499 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019500 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019501 hi = hash_find(ht, varname);
19502 if (HASHITEM_EMPTY(hi))
19503 return NULL;
19504 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019505 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019506}
19507
19508/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019509 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019510 * Set "varname" to the start of name without ':'.
19511 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019512 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019513find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514 char_u *name;
19515 char_u **varname;
19516{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019517 hashitem_T *hi;
19518
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519 if (name[1] != ':')
19520 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019521 /* The name must not start with a colon or #. */
19522 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523 return NULL;
19524 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019525
19526 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019527 hi = hash_find(&compat_hashtab, name);
19528 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019529 return &compat_hashtab;
19530
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019532 return &globvarht; /* global variable */
19533 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534 }
19535 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019536 if (*name == 'g') /* global variable */
19537 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019538 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19539 */
19540 if (vim_strchr(name + 2, ':') != NULL
19541 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019542 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019544 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019546 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019547#ifdef FEAT_WINDOWS
19548 if (*name == 't') /* tab page variable */
19549 return &curtab->tp_vars.dv_hashtab;
19550#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019551 if (*name == 'v') /* v: variable */
19552 return &vimvarht;
19553 if (*name == 'a' && current_funccal != NULL) /* function argument */
19554 return &current_funccal->l_avars.dv_hashtab;
19555 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19556 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 if (*name == 's' /* script variable */
19558 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19559 return &SCRIPT_VARS(current_SID);
19560 return NULL;
19561}
19562
19563/*
19564 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019565 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019566 * Returns NULL when it doesn't exist.
19567 */
19568 char_u *
19569get_var_value(name)
19570 char_u *name;
19571{
Bram Moolenaar33570922005-01-25 22:26:29 +000019572 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573
Bram Moolenaara7043832005-01-21 11:56:39 +000019574 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575 if (v == NULL)
19576 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019577 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578}
19579
19580/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019581 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019582 * sourcing this script and when executing functions defined in the script.
19583 */
19584 void
19585new_script_vars(id)
19586 scid_T id;
19587{
Bram Moolenaara7043832005-01-21 11:56:39 +000019588 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019589 hashtab_T *ht;
19590 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019591
Bram Moolenaar071d4272004-06-13 20:20:40 +000019592 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19593 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019594 /* Re-allocating ga_data means that an ht_array pointing to
19595 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019596 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019597 for (i = 1; i <= ga_scripts.ga_len; ++i)
19598 {
19599 ht = &SCRIPT_VARS(i);
19600 if (ht->ht_mask == HT_INIT_SIZE - 1)
19601 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019602 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019604 }
19605
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 while (ga_scripts.ga_len < id)
19607 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019608 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019609 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019610 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019611 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612 }
19613 }
19614}
19615
19616/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019617 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19618 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619 */
19620 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019621init_var_dict(dict, dict_var)
19622 dict_T *dict;
19623 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624{
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019626 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019627 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019628 dict_var->di_tv.vval.v_dict = dict;
19629 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019630 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019631 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19632 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633}
19634
19635/*
19636 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019637 * Frees all allocated variables and the value they contain.
19638 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 */
19640 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019641vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019642 hashtab_T *ht;
19643{
19644 vars_clear_ext(ht, TRUE);
19645}
19646
19647/*
19648 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19649 */
19650 static void
19651vars_clear_ext(ht, free_val)
19652 hashtab_T *ht;
19653 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654{
Bram Moolenaara7043832005-01-21 11:56:39 +000019655 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019656 hashitem_T *hi;
19657 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658
Bram Moolenaar33570922005-01-25 22:26:29 +000019659 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019660 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019661 for (hi = ht->ht_array; todo > 0; ++hi)
19662 {
19663 if (!HASHITEM_EMPTY(hi))
19664 {
19665 --todo;
19666
Bram Moolenaar33570922005-01-25 22:26:29 +000019667 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019668 * ht_array might change then. hash_clear() takes care of it
19669 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019670 v = HI2DI(hi);
19671 if (free_val)
19672 clear_tv(&v->di_tv);
19673 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19674 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019675 }
19676 }
19677 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019678 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679}
19680
Bram Moolenaara7043832005-01-21 11:56:39 +000019681/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019682 * Delete a variable from hashtab "ht" at item "hi".
19683 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019686delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 hashtab_T *ht;
19688 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689{
Bram Moolenaar33570922005-01-25 22:26:29 +000019690 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019691
19692 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019693 clear_tv(&di->di_tv);
19694 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695}
19696
19697/*
19698 * List the value of one internal variable.
19699 */
19700 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019701list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019702 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019704 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019706 char_u *tofree;
19707 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019708 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019709
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019710 current_copyID += COPYID_INC;
19711 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019712 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019713 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019714 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715}
19716
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019718list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019719 char_u *prefix;
19720 char_u *name;
19721 int type;
19722 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019723 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724{
Bram Moolenaar31859182007-08-14 20:41:13 +000019725 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19726 msg_start();
19727 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 if (name != NULL) /* "a:" vars don't have a name stored */
19729 msg_puts(name);
19730 msg_putchar(' ');
19731 msg_advance(22);
19732 if (type == VAR_NUMBER)
19733 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019734 else if (type == VAR_FUNC)
19735 msg_putchar('*');
19736 else if (type == VAR_LIST)
19737 {
19738 msg_putchar('[');
19739 if (*string == '[')
19740 ++string;
19741 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019742 else if (type == VAR_DICT)
19743 {
19744 msg_putchar('{');
19745 if (*string == '{')
19746 ++string;
19747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 else
19749 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019750
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019752
19753 if (type == VAR_FUNC)
19754 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019755 if (*first)
19756 {
19757 msg_clr_eos();
19758 *first = FALSE;
19759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019760}
19761
19762/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019763 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 * If the variable already exists, the value is updated.
19765 * Otherwise the variable is created.
19766 */
19767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019770 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019771 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772{
Bram Moolenaar33570922005-01-25 22:26:29 +000019773 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019775 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019776 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019778 ht = find_var_ht(name, &varname);
19779 if (ht == NULL || *varname == NUL)
19780 {
19781 EMSG2(_(e_illvar), name);
19782 return;
19783 }
19784 v = find_var_in_ht(ht, varname, TRUE);
19785
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019786 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019787 {
19788 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19789 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19790 ? name[2] : name[0]))
19791 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019792 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019793 return;
19794 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019795 /* Don't allow hiding a function. When "v" is not NULL we migth be
19796 * assigning another function to the same var, the type is checked
19797 * below. */
19798 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019799 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019800 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019801 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019802 return;
19803 }
19804 }
19805
Bram Moolenaar33570922005-01-25 22:26:29 +000019806 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019808 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019809 if (var_check_ro(v->di_flags, name)
19810 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019811 return;
19812 if (v->di_tv.v_type != tv->v_type
19813 && !((v->di_tv.v_type == VAR_STRING
19814 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019815 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019816 || tv->v_type == VAR_NUMBER))
19817#ifdef FEAT_FLOAT
19818 && !((v->di_tv.v_type == VAR_NUMBER
19819 || v->di_tv.v_type == VAR_FLOAT)
19820 && (tv->v_type == VAR_NUMBER
19821 || tv->v_type == VAR_FLOAT))
19822#endif
19823 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019824 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019825 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019826 return;
19827 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019828
19829 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019830 * Handle setting internal v: variables separately: we don't change
19831 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019832 */
19833 if (ht == &vimvarht)
19834 {
19835 if (v->di_tv.v_type == VAR_STRING)
19836 {
19837 vim_free(v->di_tv.vval.v_string);
19838 if (copy || tv->v_type != VAR_STRING)
19839 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19840 else
19841 {
19842 /* Take over the string to avoid an extra alloc/free. */
19843 v->di_tv.vval.v_string = tv->vval.v_string;
19844 tv->vval.v_string = NULL;
19845 }
19846 }
19847 else if (v->di_tv.v_type != VAR_NUMBER)
19848 EMSG2(_(e_intern2), "set_var()");
19849 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019850 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019851 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019852 if (STRCMP(varname, "searchforward") == 0)
19853 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19854 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019855 return;
19856 }
19857
19858 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859 }
19860 else /* add a new variable */
19861 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019862 /* Can't add "v:" variable. */
19863 if (ht == &vimvarht)
19864 {
19865 EMSG2(_(e_illvar), name);
19866 return;
19867 }
19868
Bram Moolenaar92124a32005-06-17 22:03:40 +000019869 /* Make sure the variable name is valid. */
19870 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019871 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19872 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019873 {
19874 EMSG2(_(e_illvar), varname);
19875 return;
19876 }
19877
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019878 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19879 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019880 if (v == NULL)
19881 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019882 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019883 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019885 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 return;
19887 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019888 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019890
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019891 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019892 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019893 else
19894 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019895 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019896 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019897 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899}
19900
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019901/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019902 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019903 * Also give an error message.
19904 */
19905 static int
19906var_check_ro(flags, name)
19907 int flags;
19908 char_u *name;
19909{
19910 if (flags & DI_FLAGS_RO)
19911 {
19912 EMSG2(_(e_readonlyvar), name);
19913 return TRUE;
19914 }
19915 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19916 {
19917 EMSG2(_(e_readonlysbx), name);
19918 return TRUE;
19919 }
19920 return FALSE;
19921}
19922
19923/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019924 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19925 * Also give an error message.
19926 */
19927 static int
19928var_check_fixed(flags, name)
19929 int flags;
19930 char_u *name;
19931{
19932 if (flags & DI_FLAGS_FIX)
19933 {
19934 EMSG2(_("E795: Cannot delete variable %s"), name);
19935 return TRUE;
19936 }
19937 return FALSE;
19938}
19939
19940/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019941 * Return TRUE if typeval "tv" is set to be locked (immutable).
19942 * Also give an error message, using "name".
19943 */
19944 static int
19945tv_check_lock(lock, name)
19946 int lock;
19947 char_u *name;
19948{
19949 if (lock & VAR_LOCKED)
19950 {
19951 EMSG2(_("E741: Value is locked: %s"),
19952 name == NULL ? (char_u *)_("Unknown") : name);
19953 return TRUE;
19954 }
19955 if (lock & VAR_FIXED)
19956 {
19957 EMSG2(_("E742: Cannot change value of %s"),
19958 name == NULL ? (char_u *)_("Unknown") : name);
19959 return TRUE;
19960 }
19961 return FALSE;
19962}
19963
19964/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019965 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019966 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019967 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019968 * It is OK for "from" and "to" to point to the same item. This is used to
19969 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019970 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019971 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019973 typval_T *from;
19974 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019976 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019977 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019978 switch (from->v_type)
19979 {
19980 case VAR_NUMBER:
19981 to->vval.v_number = from->vval.v_number;
19982 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019983#ifdef FEAT_FLOAT
19984 case VAR_FLOAT:
19985 to->vval.v_float = from->vval.v_float;
19986 break;
19987#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019988 case VAR_STRING:
19989 case VAR_FUNC:
19990 if (from->vval.v_string == NULL)
19991 to->vval.v_string = NULL;
19992 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019993 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019994 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019995 if (from->v_type == VAR_FUNC)
19996 func_ref(to->vval.v_string);
19997 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019998 break;
19999 case VAR_LIST:
20000 if (from->vval.v_list == NULL)
20001 to->vval.v_list = NULL;
20002 else
20003 {
20004 to->vval.v_list = from->vval.v_list;
20005 ++to->vval.v_list->lv_refcount;
20006 }
20007 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020008 case VAR_DICT:
20009 if (from->vval.v_dict == NULL)
20010 to->vval.v_dict = NULL;
20011 else
20012 {
20013 to->vval.v_dict = from->vval.v_dict;
20014 ++to->vval.v_dict->dv_refcount;
20015 }
20016 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020017 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020018 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020019 break;
20020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021}
20022
20023/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020024 * Make a copy of an item.
20025 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020026 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20027 * reference to an already copied list/dict can be used.
20028 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020029 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020030 static int
20031item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020032 typval_T *from;
20033 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020034 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020035 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020036{
20037 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020038 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020039
Bram Moolenaar33570922005-01-25 22:26:29 +000020040 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020041 {
20042 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020043 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020044 }
20045 ++recurse;
20046
20047 switch (from->v_type)
20048 {
20049 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020050#ifdef FEAT_FLOAT
20051 case VAR_FLOAT:
20052#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020053 case VAR_STRING:
20054 case VAR_FUNC:
20055 copy_tv(from, to);
20056 break;
20057 case VAR_LIST:
20058 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020059 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020060 if (from->vval.v_list == NULL)
20061 to->vval.v_list = NULL;
20062 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20063 {
20064 /* use the copy made earlier */
20065 to->vval.v_list = from->vval.v_list->lv_copylist;
20066 ++to->vval.v_list->lv_refcount;
20067 }
20068 else
20069 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20070 if (to->vval.v_list == NULL)
20071 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020072 break;
20073 case VAR_DICT:
20074 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020075 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020076 if (from->vval.v_dict == NULL)
20077 to->vval.v_dict = NULL;
20078 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20079 {
20080 /* use the copy made earlier */
20081 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20082 ++to->vval.v_dict->dv_refcount;
20083 }
20084 else
20085 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20086 if (to->vval.v_dict == NULL)
20087 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020088 break;
20089 default:
20090 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020091 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020092 }
20093 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020094 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020095}
20096
20097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 * ":echo expr1 ..." print each argument separated with a space, add a
20099 * newline at the end.
20100 * ":echon expr1 ..." print each argument plain.
20101 */
20102 void
20103ex_echo(eap)
20104 exarg_T *eap;
20105{
20106 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020108 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109 char_u *p;
20110 int needclr = TRUE;
20111 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020112 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113
20114 if (eap->skip)
20115 ++emsg_skip;
20116 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20117 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020118 /* If eval1() causes an error message the text from the command may
20119 * still need to be cleared. E.g., "echo 22,44". */
20120 need_clr_eos = needclr;
20121
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020123 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124 {
20125 /*
20126 * Report the invalid expression unless the expression evaluation
20127 * has been cancelled due to an aborting error, an interrupt, or an
20128 * exception.
20129 */
20130 if (!aborting())
20131 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020132 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 break;
20134 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020135 need_clr_eos = FALSE;
20136
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 if (!eap->skip)
20138 {
20139 if (atstart)
20140 {
20141 atstart = FALSE;
20142 /* Call msg_start() after eval1(), evaluating the expression
20143 * may cause a message to appear. */
20144 if (eap->cmdidx == CMD_echo)
20145 msg_start();
20146 }
20147 else if (eap->cmdidx == CMD_echo)
20148 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020149 current_copyID += COPYID_INC;
20150 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020151 if (p != NULL)
20152 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020154 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020156 if (*p != TAB && needclr)
20157 {
20158 /* remove any text still there from the command */
20159 msg_clr_eos();
20160 needclr = FALSE;
20161 }
20162 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 }
20164 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020165 {
20166#ifdef FEAT_MBYTE
20167 if (has_mbyte)
20168 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020169 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020170
20171 (void)msg_outtrans_len_attr(p, i, echo_attr);
20172 p += i - 1;
20173 }
20174 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020176 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020179 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020181 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 arg = skipwhite(arg);
20183 }
20184 eap->nextcmd = check_nextcmd(arg);
20185
20186 if (eap->skip)
20187 --emsg_skip;
20188 else
20189 {
20190 /* remove text that may still be there from the command */
20191 if (needclr)
20192 msg_clr_eos();
20193 if (eap->cmdidx == CMD_echo)
20194 msg_end();
20195 }
20196}
20197
20198/*
20199 * ":echohl {name}".
20200 */
20201 void
20202ex_echohl(eap)
20203 exarg_T *eap;
20204{
20205 int id;
20206
20207 id = syn_name2id(eap->arg);
20208 if (id == 0)
20209 echo_attr = 0;
20210 else
20211 echo_attr = syn_id2attr(id);
20212}
20213
20214/*
20215 * ":execute expr1 ..." execute the result of an expression.
20216 * ":echomsg expr1 ..." Print a message
20217 * ":echoerr expr1 ..." Print an error
20218 * Each gets spaces around each argument and a newline at the end for
20219 * echo commands
20220 */
20221 void
20222ex_execute(eap)
20223 exarg_T *eap;
20224{
20225 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020226 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 int ret = OK;
20228 char_u *p;
20229 garray_T ga;
20230 int len;
20231 int save_did_emsg;
20232
20233 ga_init2(&ga, 1, 80);
20234
20235 if (eap->skip)
20236 ++emsg_skip;
20237 while (*arg != NUL && *arg != '|' && *arg != '\n')
20238 {
20239 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020240 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 {
20242 /*
20243 * Report the invalid expression unless the expression evaluation
20244 * has been cancelled due to an aborting error, an interrupt, or an
20245 * exception.
20246 */
20247 if (!aborting())
20248 EMSG2(_(e_invexpr2), p);
20249 ret = FAIL;
20250 break;
20251 }
20252
20253 if (!eap->skip)
20254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 len = (int)STRLEN(p);
20257 if (ga_grow(&ga, len + 2) == FAIL)
20258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020259 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 ret = FAIL;
20261 break;
20262 }
20263 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266 ga.ga_len += len;
20267 }
20268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020269 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 arg = skipwhite(arg);
20271 }
20272
20273 if (ret != FAIL && ga.ga_data != NULL)
20274 {
20275 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020278 out_flush();
20279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020280 else if (eap->cmdidx == CMD_echoerr)
20281 {
20282 /* We don't want to abort following commands, restore did_emsg. */
20283 save_did_emsg = did_emsg;
20284 EMSG((char_u *)ga.ga_data);
20285 if (!force_abort)
20286 did_emsg = save_did_emsg;
20287 }
20288 else if (eap->cmdidx == CMD_execute)
20289 do_cmdline((char_u *)ga.ga_data,
20290 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20291 }
20292
20293 ga_clear(&ga);
20294
20295 if (eap->skip)
20296 --emsg_skip;
20297
20298 eap->nextcmd = check_nextcmd(arg);
20299}
20300
20301/*
20302 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20303 * "arg" points to the "&" or '+' when called, to "option" when returning.
20304 * Returns NULL when no option name found. Otherwise pointer to the char
20305 * after the option name.
20306 */
20307 static char_u *
20308find_option_end(arg, opt_flags)
20309 char_u **arg;
20310 int *opt_flags;
20311{
20312 char_u *p = *arg;
20313
20314 ++p;
20315 if (*p == 'g' && p[1] == ':')
20316 {
20317 *opt_flags = OPT_GLOBAL;
20318 p += 2;
20319 }
20320 else if (*p == 'l' && p[1] == ':')
20321 {
20322 *opt_flags = OPT_LOCAL;
20323 p += 2;
20324 }
20325 else
20326 *opt_flags = 0;
20327
20328 if (!ASCII_ISALPHA(*p))
20329 return NULL;
20330 *arg = p;
20331
20332 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20333 p += 4; /* termcap option */
20334 else
20335 while (ASCII_ISALPHA(*p))
20336 ++p;
20337 return p;
20338}
20339
20340/*
20341 * ":function"
20342 */
20343 void
20344ex_function(eap)
20345 exarg_T *eap;
20346{
20347 char_u *theline;
20348 int j;
20349 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020350 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 char_u *name = NULL;
20352 char_u *p;
20353 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020354 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 garray_T newargs;
20356 garray_T newlines;
20357 int varargs = FALSE;
20358 int mustend = FALSE;
20359 int flags = 0;
20360 ufunc_T *fp;
20361 int indent;
20362 int nesting;
20363 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020364 dictitem_T *v;
20365 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020366 static int func_nr = 0; /* number for nameless function */
20367 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020368 hashtab_T *ht;
20369 int todo;
20370 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020371 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372
20373 /*
20374 * ":function" without argument: list functions.
20375 */
20376 if (ends_excmd(*eap->arg))
20377 {
20378 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020379 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020380 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020381 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020382 {
20383 if (!HASHITEM_EMPTY(hi))
20384 {
20385 --todo;
20386 fp = HI2UF(hi);
20387 if (!isdigit(*fp->uf_name))
20388 list_func_head(fp, FALSE);
20389 }
20390 }
20391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020392 eap->nextcmd = check_nextcmd(eap->arg);
20393 return;
20394 }
20395
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020396 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020397 * ":function /pat": list functions matching pattern.
20398 */
20399 if (*eap->arg == '/')
20400 {
20401 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20402 if (!eap->skip)
20403 {
20404 regmatch_T regmatch;
20405
20406 c = *p;
20407 *p = NUL;
20408 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20409 *p = c;
20410 if (regmatch.regprog != NULL)
20411 {
20412 regmatch.rm_ic = p_ic;
20413
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020414 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020415 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20416 {
20417 if (!HASHITEM_EMPTY(hi))
20418 {
20419 --todo;
20420 fp = HI2UF(hi);
20421 if (!isdigit(*fp->uf_name)
20422 && vim_regexec(&regmatch, fp->uf_name, 0))
20423 list_func_head(fp, FALSE);
20424 }
20425 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020426 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020427 }
20428 }
20429 if (*p == '/')
20430 ++p;
20431 eap->nextcmd = check_nextcmd(p);
20432 return;
20433 }
20434
20435 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020436 * Get the function name. There are these situations:
20437 * func normal function name
20438 * "name" == func, "fudi.fd_dict" == NULL
20439 * dict.func new dictionary entry
20440 * "name" == NULL, "fudi.fd_dict" set,
20441 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20442 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020443 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020444 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20445 * dict.func existing dict entry that's not a Funcref
20446 * "name" == NULL, "fudi.fd_dict" set,
20447 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020450 name = trans_function_name(&p, eap->skip, 0, &fudi);
20451 paren = (vim_strchr(p, '(') != NULL);
20452 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020453 {
20454 /*
20455 * Return on an invalid expression in braces, unless the expression
20456 * evaluation has been cancelled due to an aborting error, an
20457 * interrupt, or an exception.
20458 */
20459 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020461 if (!eap->skip && fudi.fd_newkey != NULL)
20462 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020463 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 else
20467 eap->skip = TRUE;
20468 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020469
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470 /* An error in a function call during evaluation of an expression in magic
20471 * braces should not cause the function not to be defined. */
20472 saved_did_emsg = did_emsg;
20473 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474
20475 /*
20476 * ":function func" with only function name: list function.
20477 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020478 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020479 {
20480 if (!ends_excmd(*skipwhite(p)))
20481 {
20482 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020483 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020484 }
20485 eap->nextcmd = check_nextcmd(p);
20486 if (eap->nextcmd != NULL)
20487 *p = NUL;
20488 if (!eap->skip && !got_int)
20489 {
20490 fp = find_func(name);
20491 if (fp != NULL)
20492 {
20493 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020494 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020496 if (FUNCLINE(fp, j) == NULL)
20497 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498 msg_putchar('\n');
20499 msg_outnum((long)(j + 1));
20500 if (j < 9)
20501 msg_putchar(' ');
20502 if (j < 99)
20503 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020504 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 out_flush(); /* show a line at a time */
20506 ui_breakcheck();
20507 }
20508 if (!got_int)
20509 {
20510 msg_putchar('\n');
20511 msg_puts((char_u *)" endfunction");
20512 }
20513 }
20514 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020515 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020517 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 }
20519
20520 /*
20521 * ":function name(arg1, arg2)" Define function.
20522 */
20523 p = skipwhite(p);
20524 if (*p != '(')
20525 {
20526 if (!eap->skip)
20527 {
20528 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020529 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 }
20531 /* attempt to continue by skipping some text */
20532 if (vim_strchr(p, '(') != NULL)
20533 p = vim_strchr(p, '(');
20534 }
20535 p = skipwhite(p + 1);
20536
20537 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20538 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20539
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020540 if (!eap->skip)
20541 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020542 /* Check the name of the function. Unless it's a dictionary function
20543 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020544 if (name != NULL)
20545 arg = name;
20546 else
20547 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020548 if (arg != NULL && (fudi.fd_di == NULL
20549 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020550 {
20551 if (*arg == K_SPECIAL)
20552 j = 3;
20553 else
20554 j = 0;
20555 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20556 : eval_isnamec(arg[j])))
20557 ++j;
20558 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020559 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020560 }
20561 }
20562
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 /*
20564 * Isolate the arguments: "arg1, arg2, ...)"
20565 */
20566 while (*p != ')')
20567 {
20568 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20569 {
20570 varargs = TRUE;
20571 p += 3;
20572 mustend = TRUE;
20573 }
20574 else
20575 {
20576 arg = p;
20577 while (ASCII_ISALNUM(*p) || *p == '_')
20578 ++p;
20579 if (arg == p || isdigit(*arg)
20580 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20581 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20582 {
20583 if (!eap->skip)
20584 EMSG2(_("E125: Illegal argument: %s"), arg);
20585 break;
20586 }
20587 if (ga_grow(&newargs, 1) == FAIL)
20588 goto erret;
20589 c = *p;
20590 *p = NUL;
20591 arg = vim_strsave(arg);
20592 if (arg == NULL)
20593 goto erret;
20594 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20595 *p = c;
20596 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020597 if (*p == ',')
20598 ++p;
20599 else
20600 mustend = TRUE;
20601 }
20602 p = skipwhite(p);
20603 if (mustend && *p != ')')
20604 {
20605 if (!eap->skip)
20606 EMSG2(_(e_invarg2), eap->arg);
20607 break;
20608 }
20609 }
20610 ++p; /* skip the ')' */
20611
Bram Moolenaare9a41262005-01-15 22:18:47 +000020612 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 for (;;)
20614 {
20615 p = skipwhite(p);
20616 if (STRNCMP(p, "range", 5) == 0)
20617 {
20618 flags |= FC_RANGE;
20619 p += 5;
20620 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020621 else if (STRNCMP(p, "dict", 4) == 0)
20622 {
20623 flags |= FC_DICT;
20624 p += 4;
20625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 else if (STRNCMP(p, "abort", 5) == 0)
20627 {
20628 flags |= FC_ABORT;
20629 p += 5;
20630 }
20631 else
20632 break;
20633 }
20634
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020635 /* When there is a line break use what follows for the function body.
20636 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20637 if (*p == '\n')
20638 line_arg = p + 1;
20639 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 EMSG(_(e_trailing));
20641
20642 /*
20643 * Read the body of the function, until ":endfunction" is found.
20644 */
20645 if (KeyTyped)
20646 {
20647 /* Check if the function already exists, don't let the user type the
20648 * whole function before telling him it doesn't work! For a script we
20649 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020650 if (!eap->skip && !eap->forceit)
20651 {
20652 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20653 EMSG(_(e_funcdict));
20654 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020655 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020658 if (!eap->skip && did_emsg)
20659 goto erret;
20660
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661 msg_putchar('\n'); /* don't overwrite the function name */
20662 cmdline_row = msg_row;
20663 }
20664
20665 indent = 2;
20666 nesting = 0;
20667 for (;;)
20668 {
20669 msg_scroll = TRUE;
20670 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020671 sourcing_lnum_off = sourcing_lnum;
20672
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020673 if (line_arg != NULL)
20674 {
20675 /* Use eap->arg, split up in parts by line breaks. */
20676 theline = line_arg;
20677 p = vim_strchr(theline, '\n');
20678 if (p == NULL)
20679 line_arg += STRLEN(line_arg);
20680 else
20681 {
20682 *p = NUL;
20683 line_arg = p + 1;
20684 }
20685 }
20686 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 theline = getcmdline(':', 0L, indent);
20688 else
20689 theline = eap->getline(':', eap->cookie, indent);
20690 if (KeyTyped)
20691 lines_left = Rows - 1;
20692 if (theline == NULL)
20693 {
20694 EMSG(_("E126: Missing :endfunction"));
20695 goto erret;
20696 }
20697
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020698 /* Detect line continuation: sourcing_lnum increased more than one. */
20699 if (sourcing_lnum > sourcing_lnum_off + 1)
20700 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20701 else
20702 sourcing_lnum_off = 0;
20703
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 if (skip_until != NULL)
20705 {
20706 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20707 * don't check for ":endfunc". */
20708 if (STRCMP(theline, skip_until) == 0)
20709 {
20710 vim_free(skip_until);
20711 skip_until = NULL;
20712 }
20713 }
20714 else
20715 {
20716 /* skip ':' and blanks*/
20717 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20718 ;
20719
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020720 /* Check for "endfunction". */
20721 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020723 if (line_arg == NULL)
20724 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 break;
20726 }
20727
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020728 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729 * at "end". */
20730 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20731 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020732 else if (STRNCMP(p, "if", 2) == 0
20733 || STRNCMP(p, "wh", 2) == 0
20734 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 || STRNCMP(p, "try", 3) == 0)
20736 indent += 2;
20737
20738 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020739 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020741 if (*p == '!')
20742 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 p += eval_fname_script(p);
20744 if (ASCII_ISALPHA(*p))
20745 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020746 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020747 if (*skipwhite(p) == '(')
20748 {
20749 ++nesting;
20750 indent += 2;
20751 }
20752 }
20753 }
20754
20755 /* Check for ":append" or ":insert". */
20756 p = skip_range(p, NULL);
20757 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20758 || (p[0] == 'i'
20759 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20760 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20761 skip_until = vim_strsave((char_u *)".");
20762
20763 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20764 arg = skipwhite(skiptowhite(p));
20765 if (arg[0] == '<' && arg[1] =='<'
20766 && ((p[0] == 'p' && p[1] == 'y'
20767 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20768 || (p[0] == 'p' && p[1] == 'e'
20769 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20770 || (p[0] == 't' && p[1] == 'c'
20771 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20772 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20773 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020774 || (p[0] == 'm' && p[1] == 'z'
20775 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776 ))
20777 {
20778 /* ":python <<" continues until a dot, like ":append" */
20779 p = skipwhite(arg + 2);
20780 if (*p == NUL)
20781 skip_until = vim_strsave((char_u *)".");
20782 else
20783 skip_until = vim_strsave(p);
20784 }
20785 }
20786
20787 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020788 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020789 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020790 if (line_arg == NULL)
20791 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020793 }
20794
20795 /* Copy the line to newly allocated memory. get_one_sourceline()
20796 * allocates 250 bytes per line, this saves 80% on average. The cost
20797 * is an extra alloc/free. */
20798 p = vim_strsave(theline);
20799 if (p != NULL)
20800 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020801 if (line_arg == NULL)
20802 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020803 theline = p;
20804 }
20805
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020806 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20807
20808 /* Add NULL lines for continuation lines, so that the line count is
20809 * equal to the index in the growarray. */
20810 while (sourcing_lnum_off-- > 0)
20811 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020812
20813 /* Check for end of eap->arg. */
20814 if (line_arg != NULL && *line_arg == NUL)
20815 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020816 }
20817
20818 /* Don't define the function when skipping commands or when an error was
20819 * detected. */
20820 if (eap->skip || did_emsg)
20821 goto erret;
20822
20823 /*
20824 * If there are no errors, add the function
20825 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020826 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020827 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020828 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020829 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020830 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020831 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020832 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020833 goto erret;
20834 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020835
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020836 fp = find_func(name);
20837 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020838 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020839 if (!eap->forceit)
20840 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020841 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020842 goto erret;
20843 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020844 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020845 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020846 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020847 name);
20848 goto erret;
20849 }
20850 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020851 ga_clear_strings(&(fp->uf_args));
20852 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020853 vim_free(name);
20854 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 }
20857 else
20858 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020859 char numbuf[20];
20860
20861 fp = NULL;
20862 if (fudi.fd_newkey == NULL && !eap->forceit)
20863 {
20864 EMSG(_(e_funcdict));
20865 goto erret;
20866 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020867 if (fudi.fd_di == NULL)
20868 {
20869 /* Can't add a function to a locked dictionary */
20870 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20871 goto erret;
20872 }
20873 /* Can't change an existing function if it is locked */
20874 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20875 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020876
20877 /* Give the function a sequential number. Can only be used with a
20878 * Funcref! */
20879 vim_free(name);
20880 sprintf(numbuf, "%d", ++func_nr);
20881 name = vim_strsave((char_u *)numbuf);
20882 if (name == NULL)
20883 goto erret;
20884 }
20885
20886 if (fp == NULL)
20887 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020888 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020889 {
20890 int slen, plen;
20891 char_u *scriptname;
20892
20893 /* Check that the autoload name matches the script name. */
20894 j = FAIL;
20895 if (sourcing_name != NULL)
20896 {
20897 scriptname = autoload_name(name);
20898 if (scriptname != NULL)
20899 {
20900 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020901 plen = (int)STRLEN(p);
20902 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020903 if (slen > plen && fnamecmp(p,
20904 sourcing_name + slen - plen) == 0)
20905 j = OK;
20906 vim_free(scriptname);
20907 }
20908 }
20909 if (j == FAIL)
20910 {
20911 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20912 goto erret;
20913 }
20914 }
20915
20916 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020917 if (fp == NULL)
20918 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020919
20920 if (fudi.fd_dict != NULL)
20921 {
20922 if (fudi.fd_di == NULL)
20923 {
20924 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020925 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020926 if (fudi.fd_di == NULL)
20927 {
20928 vim_free(fp);
20929 goto erret;
20930 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020931 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20932 {
20933 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020934 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020935 goto erret;
20936 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020937 }
20938 else
20939 /* overwrite existing dict entry */
20940 clear_tv(&fudi.fd_di->di_tv);
20941 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020942 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020943 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020944 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020945
20946 /* behave like "dict" was used */
20947 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020948 }
20949
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020951 STRCPY(fp->uf_name, name);
20952 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020954 fp->uf_args = newargs;
20955 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020956#ifdef FEAT_PROFILE
20957 fp->uf_tml_count = NULL;
20958 fp->uf_tml_total = NULL;
20959 fp->uf_tml_self = NULL;
20960 fp->uf_profiling = FALSE;
20961 if (prof_def_func())
20962 func_do_profile(fp);
20963#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020964 fp->uf_varargs = varargs;
20965 fp->uf_flags = flags;
20966 fp->uf_calls = 0;
20967 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020968 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969
20970erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971 ga_clear_strings(&newargs);
20972 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020973ret_free:
20974 vim_free(skip_until);
20975 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020978}
20979
20980/*
20981 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020982 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020984 * flags:
20985 * TFN_INT: internal function name OK
20986 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987 * Advances "pp" to just after the function name (if no error).
20988 */
20989 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020990trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991 char_u **pp;
20992 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020993 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020994 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020996 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 char_u *start;
20998 char_u *end;
20999 int lead;
21000 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021003
21004 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021005 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021007
21008 /* Check for hard coded <SNR>: already translated function ID (from a user
21009 * command). */
21010 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21011 && (*pp)[2] == (int)KE_SNR)
21012 {
21013 *pp += 3;
21014 len = get_id_len(pp) + 3;
21015 return vim_strnsave(start, len);
21016 }
21017
21018 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21019 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021020 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021021 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021023
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021024 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21025 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021026 if (end == start)
21027 {
21028 if (!skip)
21029 EMSG(_("E129: Function name required"));
21030 goto theend;
21031 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021032 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021033 {
21034 /*
21035 * Report an invalid expression in braces, unless the expression
21036 * evaluation has been cancelled due to an aborting error, an
21037 * interrupt, or an exception.
21038 */
21039 if (!aborting())
21040 {
21041 if (end != NULL)
21042 EMSG2(_(e_invarg2), start);
21043 }
21044 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021045 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021046 goto theend;
21047 }
21048
21049 if (lv.ll_tv != NULL)
21050 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021051 if (fdp != NULL)
21052 {
21053 fdp->fd_dict = lv.ll_dict;
21054 fdp->fd_newkey = lv.ll_newkey;
21055 lv.ll_newkey = NULL;
21056 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021057 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021058 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21059 {
21060 name = vim_strsave(lv.ll_tv->vval.v_string);
21061 *pp = end;
21062 }
21063 else
21064 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021065 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21066 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021067 EMSG(_(e_funcref));
21068 else
21069 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021070 name = NULL;
21071 }
21072 goto theend;
21073 }
21074
21075 if (lv.ll_name == NULL)
21076 {
21077 /* Error found, but continue after the function name. */
21078 *pp = end;
21079 goto theend;
21080 }
21081
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021082 /* Check if the name is a Funcref. If so, use the value. */
21083 if (lv.ll_exp_name != NULL)
21084 {
21085 len = (int)STRLEN(lv.ll_exp_name);
21086 name = deref_func_name(lv.ll_exp_name, &len);
21087 if (name == lv.ll_exp_name)
21088 name = NULL;
21089 }
21090 else
21091 {
21092 len = (int)(end - *pp);
21093 name = deref_func_name(*pp, &len);
21094 if (name == *pp)
21095 name = NULL;
21096 }
21097 if (name != NULL)
21098 {
21099 name = vim_strsave(name);
21100 *pp = end;
21101 goto theend;
21102 }
21103
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021104 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021105 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021106 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021107 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21108 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21109 {
21110 /* When there was "s:" already or the name expanded to get a
21111 * leading "s:" then remove it. */
21112 lv.ll_name += 2;
21113 len -= 2;
21114 lead = 2;
21115 }
21116 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021117 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021118 {
21119 if (lead == 2) /* skip over "s:" */
21120 lv.ll_name += 2;
21121 len = (int)(end - lv.ll_name);
21122 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021123
21124 /*
21125 * Copy the function name to allocated memory.
21126 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21127 * Accept <SNR>123_name() outside a script.
21128 */
21129 if (skip)
21130 lead = 0; /* do nothing */
21131 else if (lead > 0)
21132 {
21133 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021134 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21135 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021136 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021137 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021138 if (current_SID <= 0)
21139 {
21140 EMSG(_(e_usingsid));
21141 goto theend;
21142 }
21143 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21144 lead += (int)STRLEN(sid_buf);
21145 }
21146 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021147 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021148 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021149 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021150 goto theend;
21151 }
21152 name = alloc((unsigned)(len + lead + 1));
21153 if (name != NULL)
21154 {
21155 if (lead > 0)
21156 {
21157 name[0] = K_SPECIAL;
21158 name[1] = KS_EXTRA;
21159 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021160 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021161 STRCPY(name + 3, sid_buf);
21162 }
21163 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21164 name[len + lead] = NUL;
21165 }
21166 *pp = end;
21167
21168theend:
21169 clear_lval(&lv);
21170 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171}
21172
21173/*
21174 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21175 * Return 2 if "p" starts with "s:".
21176 * Return 0 otherwise.
21177 */
21178 static int
21179eval_fname_script(p)
21180 char_u *p;
21181{
21182 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21183 || STRNICMP(p + 1, "SNR>", 4) == 0))
21184 return 5;
21185 if (p[0] == 's' && p[1] == ':')
21186 return 2;
21187 return 0;
21188}
21189
21190/*
21191 * Return TRUE if "p" starts with "<SID>" or "s:".
21192 * Only works if eval_fname_script() returned non-zero for "p"!
21193 */
21194 static int
21195eval_fname_sid(p)
21196 char_u *p;
21197{
21198 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21199}
21200
21201/*
21202 * List the head of the function: "name(arg1, arg2)".
21203 */
21204 static void
21205list_func_head(fp, indent)
21206 ufunc_T *fp;
21207 int indent;
21208{
21209 int j;
21210
21211 msg_start();
21212 if (indent)
21213 MSG_PUTS(" ");
21214 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021215 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 {
21217 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021218 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 }
21220 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021221 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021223 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 {
21225 if (j)
21226 MSG_PUTS(", ");
21227 msg_puts(FUNCARG(fp, j));
21228 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021229 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 {
21231 if (j)
21232 MSG_PUTS(", ");
21233 MSG_PUTS("...");
21234 }
21235 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021236 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021237 if (p_verbose > 0)
21238 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239}
21240
21241/*
21242 * Find a function by name, return pointer to it in ufuncs.
21243 * Return NULL for unknown function.
21244 */
21245 static ufunc_T *
21246find_func(name)
21247 char_u *name;
21248{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021249 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021250
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021251 hi = hash_find(&func_hashtab, name);
21252 if (!HASHITEM_EMPTY(hi))
21253 return HI2UF(hi);
21254 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255}
21256
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021257#if defined(EXITFREE) || defined(PROTO)
21258 void
21259free_all_functions()
21260{
21261 hashitem_T *hi;
21262
21263 /* Need to start all over every time, because func_free() may change the
21264 * hash table. */
21265 while (func_hashtab.ht_used > 0)
21266 for (hi = func_hashtab.ht_array; ; ++hi)
21267 if (!HASHITEM_EMPTY(hi))
21268 {
21269 func_free(HI2UF(hi));
21270 break;
21271 }
21272}
21273#endif
21274
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021275/*
21276 * Return TRUE if a function "name" exists.
21277 */
21278 static int
21279function_exists(name)
21280 char_u *name;
21281{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021282 char_u *nm = name;
21283 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021284 int n = FALSE;
21285
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021286 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021287 nm = skipwhite(nm);
21288
21289 /* Only accept "funcname", "funcname ", "funcname (..." and
21290 * "funcname(...", not "funcname!...". */
21291 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021292 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021293 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021294 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021295 else
21296 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021297 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021298 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021299 return n;
21300}
21301
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021302/*
21303 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021304 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021305 */
21306 static int
21307builtin_function(name)
21308 char_u *name;
21309{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021310 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21311 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021312}
21313
Bram Moolenaar05159a02005-02-26 23:04:13 +000021314#if defined(FEAT_PROFILE) || defined(PROTO)
21315/*
21316 * Start profiling function "fp".
21317 */
21318 static void
21319func_do_profile(fp)
21320 ufunc_T *fp;
21321{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021322 int len = fp->uf_lines.ga_len;
21323
21324 if (len == 0)
21325 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021326 fp->uf_tm_count = 0;
21327 profile_zero(&fp->uf_tm_self);
21328 profile_zero(&fp->uf_tm_total);
21329 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021330 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021331 if (fp->uf_tml_total == NULL)
21332 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021333 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021334 if (fp->uf_tml_self == NULL)
21335 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021336 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021337 fp->uf_tml_idx = -1;
21338 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21339 || fp->uf_tml_self == NULL)
21340 return; /* out of memory */
21341
21342 fp->uf_profiling = TRUE;
21343}
21344
21345/*
21346 * Dump the profiling results for all functions in file "fd".
21347 */
21348 void
21349func_dump_profile(fd)
21350 FILE *fd;
21351{
21352 hashitem_T *hi;
21353 int todo;
21354 ufunc_T *fp;
21355 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021356 ufunc_T **sorttab;
21357 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021358
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021359 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021360 if (todo == 0)
21361 return; /* nothing to dump */
21362
Bram Moolenaar73830342005-02-28 22:48:19 +000021363 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21364
Bram Moolenaar05159a02005-02-26 23:04:13 +000021365 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21366 {
21367 if (!HASHITEM_EMPTY(hi))
21368 {
21369 --todo;
21370 fp = HI2UF(hi);
21371 if (fp->uf_profiling)
21372 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021373 if (sorttab != NULL)
21374 sorttab[st_len++] = fp;
21375
Bram Moolenaar05159a02005-02-26 23:04:13 +000021376 if (fp->uf_name[0] == K_SPECIAL)
21377 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21378 else
21379 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21380 if (fp->uf_tm_count == 1)
21381 fprintf(fd, "Called 1 time\n");
21382 else
21383 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21384 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21385 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21386 fprintf(fd, "\n");
21387 fprintf(fd, "count total (s) self (s)\n");
21388
21389 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21390 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021391 if (FUNCLINE(fp, i) == NULL)
21392 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021393 prof_func_line(fd, fp->uf_tml_count[i],
21394 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021395 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21396 }
21397 fprintf(fd, "\n");
21398 }
21399 }
21400 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021401
21402 if (sorttab != NULL && st_len > 0)
21403 {
21404 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21405 prof_total_cmp);
21406 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21407 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21408 prof_self_cmp);
21409 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21410 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021411
21412 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021413}
Bram Moolenaar73830342005-02-28 22:48:19 +000021414
21415 static void
21416prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21417 FILE *fd;
21418 ufunc_T **sorttab;
21419 int st_len;
21420 char *title;
21421 int prefer_self; /* when equal print only self time */
21422{
21423 int i;
21424 ufunc_T *fp;
21425
21426 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21427 fprintf(fd, "count total (s) self (s) function\n");
21428 for (i = 0; i < 20 && i < st_len; ++i)
21429 {
21430 fp = sorttab[i];
21431 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21432 prefer_self);
21433 if (fp->uf_name[0] == K_SPECIAL)
21434 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21435 else
21436 fprintf(fd, " %s()\n", fp->uf_name);
21437 }
21438 fprintf(fd, "\n");
21439}
21440
21441/*
21442 * Print the count and times for one function or function line.
21443 */
21444 static void
21445prof_func_line(fd, count, total, self, prefer_self)
21446 FILE *fd;
21447 int count;
21448 proftime_T *total;
21449 proftime_T *self;
21450 int prefer_self; /* when equal print only self time */
21451{
21452 if (count > 0)
21453 {
21454 fprintf(fd, "%5d ", count);
21455 if (prefer_self && profile_equal(total, self))
21456 fprintf(fd, " ");
21457 else
21458 fprintf(fd, "%s ", profile_msg(total));
21459 if (!prefer_self && profile_equal(total, self))
21460 fprintf(fd, " ");
21461 else
21462 fprintf(fd, "%s ", profile_msg(self));
21463 }
21464 else
21465 fprintf(fd, " ");
21466}
21467
21468/*
21469 * Compare function for total time sorting.
21470 */
21471 static int
21472#ifdef __BORLANDC__
21473_RTLENTRYF
21474#endif
21475prof_total_cmp(s1, s2)
21476 const void *s1;
21477 const void *s2;
21478{
21479 ufunc_T *p1, *p2;
21480
21481 p1 = *(ufunc_T **)s1;
21482 p2 = *(ufunc_T **)s2;
21483 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21484}
21485
21486/*
21487 * Compare function for self time sorting.
21488 */
21489 static int
21490#ifdef __BORLANDC__
21491_RTLENTRYF
21492#endif
21493prof_self_cmp(s1, s2)
21494 const void *s1;
21495 const void *s2;
21496{
21497 ufunc_T *p1, *p2;
21498
21499 p1 = *(ufunc_T **)s1;
21500 p2 = *(ufunc_T **)s2;
21501 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21502}
21503
Bram Moolenaar05159a02005-02-26 23:04:13 +000021504#endif
21505
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021506/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021507 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021508 * Return TRUE if a package was loaded.
21509 */
21510 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021511script_autoload(name, reload)
21512 char_u *name;
21513 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021514{
21515 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021516 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021517 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021518 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021519
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021520 /* Return quickly when autoload disabled. */
21521 if (no_autoload)
21522 return FALSE;
21523
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021524 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021525 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021526 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021527 return FALSE;
21528
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021529 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021530
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021531 /* Find the name in the list of previously loaded package names. Skip
21532 * "autoload/", it's always the same. */
21533 for (i = 0; i < ga_loaded.ga_len; ++i)
21534 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21535 break;
21536 if (!reload && i < ga_loaded.ga_len)
21537 ret = FALSE; /* was loaded already */
21538 else
21539 {
21540 /* Remember the name if it wasn't loaded already. */
21541 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21542 {
21543 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21544 tofree = NULL;
21545 }
21546
21547 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021548 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021549 ret = TRUE;
21550 }
21551
21552 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021553 return ret;
21554}
21555
21556/*
21557 * Return the autoload script name for a function or variable name.
21558 * Returns NULL when out of memory.
21559 */
21560 static char_u *
21561autoload_name(name)
21562 char_u *name;
21563{
21564 char_u *p;
21565 char_u *scriptname;
21566
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021567 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021568 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21569 if (scriptname == NULL)
21570 return FALSE;
21571 STRCPY(scriptname, "autoload/");
21572 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021573 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021574 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021575 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021576 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021577 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021578}
21579
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21581
21582/*
21583 * Function given to ExpandGeneric() to obtain the list of user defined
21584 * function names.
21585 */
21586 char_u *
21587get_user_func_name(xp, idx)
21588 expand_T *xp;
21589 int idx;
21590{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021591 static long_u done;
21592 static hashitem_T *hi;
21593 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021594
21595 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021597 done = 0;
21598 hi = func_hashtab.ht_array;
21599 }
21600 if (done < func_hashtab.ht_used)
21601 {
21602 if (done++ > 0)
21603 ++hi;
21604 while (HASHITEM_EMPTY(hi))
21605 ++hi;
21606 fp = HI2UF(hi);
21607
21608 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21609 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021610
21611 cat_func_name(IObuff, fp);
21612 if (xp->xp_context != EXPAND_USER_FUNC)
21613 {
21614 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021615 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616 STRCAT(IObuff, ")");
21617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 return IObuff;
21619 }
21620 return NULL;
21621}
21622
21623#endif /* FEAT_CMDL_COMPL */
21624
21625/*
21626 * Copy the function name of "fp" to buffer "buf".
21627 * "buf" must be able to hold the function name plus three bytes.
21628 * Takes care of script-local function names.
21629 */
21630 static void
21631cat_func_name(buf, fp)
21632 char_u *buf;
21633 ufunc_T *fp;
21634{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021635 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 {
21637 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021638 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 }
21640 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021641 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642}
21643
21644/*
21645 * ":delfunction {name}"
21646 */
21647 void
21648ex_delfunction(eap)
21649 exarg_T *eap;
21650{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 char_u *p;
21653 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021654 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655
21656 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021657 name = trans_function_name(&p, eap->skip, 0, &fudi);
21658 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021659 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021660 {
21661 if (fudi.fd_dict != NULL && !eap->skip)
21662 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 if (!ends_excmd(*skipwhite(p)))
21666 {
21667 vim_free(name);
21668 EMSG(_(e_trailing));
21669 return;
21670 }
21671 eap->nextcmd = check_nextcmd(p);
21672 if (eap->nextcmd != NULL)
21673 *p = NUL;
21674
21675 if (!eap->skip)
21676 fp = find_func(name);
21677 vim_free(name);
21678
21679 if (!eap->skip)
21680 {
21681 if (fp == NULL)
21682 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021683 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 return;
21685 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021686 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 {
21688 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21689 return;
21690 }
21691
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021692 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021694 /* Delete the dict item that refers to the function, it will
21695 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021696 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021698 else
21699 func_free(fp);
21700 }
21701}
21702
21703/*
21704 * Free a function and remove it from the list of functions.
21705 */
21706 static void
21707func_free(fp)
21708 ufunc_T *fp;
21709{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021710 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021711
21712 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021713 ga_clear_strings(&(fp->uf_args));
21714 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021715#ifdef FEAT_PROFILE
21716 vim_free(fp->uf_tml_count);
21717 vim_free(fp->uf_tml_total);
21718 vim_free(fp->uf_tml_self);
21719#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021720
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021721 /* remove the function from the function hashtable */
21722 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21723 if (HASHITEM_EMPTY(hi))
21724 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021725 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021726 hash_remove(&func_hashtab, hi);
21727
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021728 vim_free(fp);
21729}
21730
21731/*
21732 * Unreference a Function: decrement the reference count and free it when it
21733 * becomes zero. Only for numbered functions.
21734 */
21735 static void
21736func_unref(name)
21737 char_u *name;
21738{
21739 ufunc_T *fp;
21740
21741 if (name != NULL && isdigit(*name))
21742 {
21743 fp = find_func(name);
21744 if (fp == NULL)
21745 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021746 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021747 {
21748 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021749 * when "uf_calls" becomes zero. */
21750 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021751 func_free(fp);
21752 }
21753 }
21754}
21755
21756/*
21757 * Count a reference to a Function.
21758 */
21759 static void
21760func_ref(name)
21761 char_u *name;
21762{
21763 ufunc_T *fp;
21764
21765 if (name != NULL && isdigit(*name))
21766 {
21767 fp = find_func(name);
21768 if (fp == NULL)
21769 EMSG2(_(e_intern2), "func_ref()");
21770 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021771 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 }
21773}
21774
21775/*
21776 * Call a user function.
21777 */
21778 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021779call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 ufunc_T *fp; /* pointer to function */
21781 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021782 typval_T *argvars; /* arguments */
21783 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784 linenr_T firstline; /* first line of range */
21785 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021786 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021787{
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 char_u *save_sourcing_name;
21789 linenr_T save_sourcing_lnum;
21790 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021791 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 int save_did_emsg;
21793 static int depth = 0;
21794 dictitem_T *v;
21795 int fixvar_idx = 0; /* index in fixvar[] */
21796 int i;
21797 int ai;
21798 char_u numbuf[NUMBUFLEN];
21799 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021800#ifdef FEAT_PROFILE
21801 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021802 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804
21805 /* If depth of calling is getting too high, don't execute the function */
21806 if (depth >= p_mfd)
21807 {
21808 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021809 rettv->v_type = VAR_NUMBER;
21810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 return;
21812 }
21813 ++depth;
21814
21815 line_breakcheck(); /* check for CTRL-C hit */
21816
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021817 fc = (funccall_T *)alloc(sizeof(funccall_T));
21818 fc->caller = current_funccal;
21819 current_funccal = fc;
21820 fc->func = fp;
21821 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021822 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021823 fc->linenr = 0;
21824 fc->returned = FALSE;
21825 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021827 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21828 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021829
Bram Moolenaar33570922005-01-25 22:26:29 +000021830 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021831 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021832 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21833 * each argument variable and saves a lot of time.
21834 */
21835 /*
21836 * Init l: variables.
21837 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021838 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021839 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021840 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021841 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21842 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021843 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021844 name = v->di_key;
21845 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021846 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021847 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021848 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021849 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021850 v->di_tv.vval.v_dict = selfdict;
21851 ++selfdict->dv_refcount;
21852 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021853
Bram Moolenaar33570922005-01-25 22:26:29 +000021854 /*
21855 * Init a: variables.
21856 * Set a:0 to "argcount".
21857 * Set a:000 to a list with room for the "..." arguments.
21858 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021859 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21860 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021861 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021862 /* Use "name" to avoid a warning from some compiler that checks the
21863 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021864 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021865 name = v->di_key;
21866 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021868 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021869 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021870 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021871 v->di_tv.vval.v_list = &fc->l_varlist;
21872 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21873 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21874 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021875
21876 /*
21877 * Set a:firstline to "firstline" and a:lastline to "lastline".
21878 * Set a:name to named arguments.
21879 * Set a:N to the "..." arguments.
21880 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021881 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021882 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021883 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021884 (varnumber_T)lastline);
21885 for (i = 0; i < argcount; ++i)
21886 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021887 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021888 if (ai < 0)
21889 /* named argument a:name */
21890 name = FUNCARG(fp, i);
21891 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021892 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021893 /* "..." argument a:1, a:2, etc. */
21894 sprintf((char *)numbuf, "%d", ai + 1);
21895 name = numbuf;
21896 }
21897 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21898 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021899 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021900 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21901 }
21902 else
21903 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021904 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21905 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021906 if (v == NULL)
21907 break;
21908 v->di_flags = DI_FLAGS_RO;
21909 }
21910 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021911 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021912
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021913 /* Note: the values are copied directly to avoid alloc/free.
21914 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021915 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021916 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021917
21918 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21919 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021920 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21921 fc->l_listitems[ai].li_tv = argvars[i];
21922 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021923 }
21924 }
21925
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926 /* Don't redraw while executing the function. */
21927 ++RedrawingDisabled;
21928 save_sourcing_name = sourcing_name;
21929 save_sourcing_lnum = sourcing_lnum;
21930 sourcing_lnum = 1;
21931 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021932 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 if (sourcing_name != NULL)
21934 {
21935 if (save_sourcing_name != NULL
21936 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21937 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21938 else
21939 STRCPY(sourcing_name, "function ");
21940 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21941
21942 if (p_verbose >= 12)
21943 {
21944 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021945 verbose_enter_scroll();
21946
Bram Moolenaar555b2802005-05-19 21:08:39 +000021947 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 if (p_verbose >= 14)
21949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021950 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021951 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021952 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021953 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954
21955 msg_puts((char_u *)"(");
21956 for (i = 0; i < argcount; ++i)
21957 {
21958 if (i > 0)
21959 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021960 if (argvars[i].v_type == VAR_NUMBER)
21961 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021962 else
21963 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021964 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21965 if (s != NULL)
21966 {
21967 trunc_string(s, buf, MSG_BUF_CLEN);
21968 msg_puts(buf);
21969 vim_free(tofree);
21970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021971 }
21972 }
21973 msg_puts((char_u *)")");
21974 }
21975 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021976
21977 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021978 --no_wait_return;
21979 }
21980 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021981#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021982 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021983 {
21984 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21985 func_do_profile(fp);
21986 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021987 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021988 {
21989 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021990 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021991 profile_zero(&fp->uf_tm_children);
21992 }
21993 script_prof_save(&wait_start);
21994 }
21995#endif
21996
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021998 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 save_did_emsg = did_emsg;
22000 did_emsg = FALSE;
22001
22002 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022003 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22005
22006 --RedrawingDisabled;
22007
22008 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022009 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022011 clear_tv(rettv);
22012 rettv->v_type = VAR_NUMBER;
22013 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 }
22015
Bram Moolenaar05159a02005-02-26 23:04:13 +000022016#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022017 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022018 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022019 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022020 profile_end(&call_start);
22021 profile_sub_wait(&wait_start, &call_start);
22022 profile_add(&fp->uf_tm_total, &call_start);
22023 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022024 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022025 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022026 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22027 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022028 }
22029 }
22030#endif
22031
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 /* when being verbose, mention the return value */
22033 if (p_verbose >= 12)
22034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022036 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022039 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022040 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022041 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022042 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022043 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022044 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022045 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022046 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022047 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022048 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022049
Bram Moolenaar555b2802005-05-19 21:08:39 +000022050 /* The value may be very long. Skip the middle part, so that we
22051 * have some idea how it starts and ends. smsg() would always
22052 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022053 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022054 if (s != NULL)
22055 {
22056 trunc_string(s, buf, MSG_BUF_CLEN);
22057 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22058 vim_free(tofree);
22059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 }
22061 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022062
22063 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064 --no_wait_return;
22065 }
22066
22067 vim_free(sourcing_name);
22068 sourcing_name = save_sourcing_name;
22069 sourcing_lnum = save_sourcing_lnum;
22070 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022071#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022072 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022073 script_prof_restore(&wait_start);
22074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075
22076 if (p_verbose >= 12 && sourcing_name != NULL)
22077 {
22078 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022079 verbose_enter_scroll();
22080
Bram Moolenaar555b2802005-05-19 21:08:39 +000022081 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022083
22084 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022085 --no_wait_return;
22086 }
22087
22088 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022089 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022091
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022092 /* If the a:000 list and the l: and a: dicts are not referenced we can
22093 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022094 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22095 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22096 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22097 {
22098 free_funccal(fc, FALSE);
22099 }
22100 else
22101 {
22102 hashitem_T *hi;
22103 listitem_T *li;
22104 int todo;
22105
22106 /* "fc" is still in use. This can happen when returning "a:000" or
22107 * assigning "l:" to a global variable.
22108 * Link "fc" in the list for garbage collection later. */
22109 fc->caller = previous_funccal;
22110 previous_funccal = fc;
22111
22112 /* Make a copy of the a: variables, since we didn't do that above. */
22113 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22114 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22115 {
22116 if (!HASHITEM_EMPTY(hi))
22117 {
22118 --todo;
22119 v = HI2DI(hi);
22120 copy_tv(&v->di_tv, &v->di_tv);
22121 }
22122 }
22123
22124 /* Make a copy of the a:000 items, since we didn't do that above. */
22125 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22126 copy_tv(&li->li_tv, &li->li_tv);
22127 }
22128}
22129
22130/*
22131 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022132 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022133 */
22134 static int
22135can_free_funccal(fc, copyID)
22136 funccall_T *fc;
22137 int copyID;
22138{
22139 return (fc->l_varlist.lv_copyID != copyID
22140 && fc->l_vars.dv_copyID != copyID
22141 && fc->l_avars.dv_copyID != copyID);
22142}
22143
22144/*
22145 * Free "fc" and what it contains.
22146 */
22147 static void
22148free_funccal(fc, free_val)
22149 funccall_T *fc;
22150 int free_val; /* a: vars were allocated */
22151{
22152 listitem_T *li;
22153
22154 /* The a: variables typevals may not have been allocated, only free the
22155 * allocated variables. */
22156 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22157
22158 /* free all l: variables */
22159 vars_clear(&fc->l_vars.dv_hashtab);
22160
22161 /* Free the a:000 variables if they were allocated. */
22162 if (free_val)
22163 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22164 clear_tv(&li->li_tv);
22165
22166 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167}
22168
22169/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022170 * Add a number variable "name" to dict "dp" with value "nr".
22171 */
22172 static void
22173add_nr_var(dp, v, name, nr)
22174 dict_T *dp;
22175 dictitem_T *v;
22176 char *name;
22177 varnumber_T nr;
22178{
22179 STRCPY(v->di_key, name);
22180 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22181 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22182 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022183 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022184 v->di_tv.vval.v_number = nr;
22185}
22186
22187/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 * ":return [expr]"
22189 */
22190 void
22191ex_return(eap)
22192 exarg_T *eap;
22193{
22194 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022195 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022196 int returning = FALSE;
22197
22198 if (current_funccal == NULL)
22199 {
22200 EMSG(_("E133: :return not inside a function"));
22201 return;
22202 }
22203
22204 if (eap->skip)
22205 ++emsg_skip;
22206
22207 eap->nextcmd = NULL;
22208 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022209 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 {
22211 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022212 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022214 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022215 }
22216 /* It's safer to return also on error. */
22217 else if (!eap->skip)
22218 {
22219 /*
22220 * Return unless the expression evaluation has been cancelled due to an
22221 * aborting error, an interrupt, or an exception.
22222 */
22223 if (!aborting())
22224 returning = do_return(eap, FALSE, TRUE, NULL);
22225 }
22226
22227 /* When skipping or the return gets pending, advance to the next command
22228 * in this line (!returning). Otherwise, ignore the rest of the line.
22229 * Following lines will be ignored by get_func_line(). */
22230 if (returning)
22231 eap->nextcmd = NULL;
22232 else if (eap->nextcmd == NULL) /* no argument */
22233 eap->nextcmd = check_nextcmd(arg);
22234
22235 if (eap->skip)
22236 --emsg_skip;
22237}
22238
22239/*
22240 * Return from a function. Possibly makes the return pending. Also called
22241 * for a pending return at the ":endtry" or after returning from an extra
22242 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022243 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022244 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 * FALSE when the return gets pending.
22246 */
22247 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022248do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022249 exarg_T *eap;
22250 int reanimate;
22251 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022252 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022253{
22254 int idx;
22255 struct condstack *cstack = eap->cstack;
22256
22257 if (reanimate)
22258 /* Undo the return. */
22259 current_funccal->returned = FALSE;
22260
22261 /*
22262 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22263 * not in its finally clause (which then is to be executed next) is found.
22264 * In this case, make the ":return" pending for execution at the ":endtry".
22265 * Otherwise, return normally.
22266 */
22267 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22268 if (idx >= 0)
22269 {
22270 cstack->cs_pending[idx] = CSTP_RETURN;
22271
22272 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022273 /* A pending return again gets pending. "rettv" points to an
22274 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022276 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022277 else
22278 {
22279 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022280 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022282 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022284 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 {
22286 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022287 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022288 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022289 else
22290 EMSG(_(e_outofmem));
22291 }
22292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022293 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294
22295 if (reanimate)
22296 {
22297 /* The pending return value could be overwritten by a ":return"
22298 * without argument in a finally clause; reset the default
22299 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022300 current_funccal->rettv->v_type = VAR_NUMBER;
22301 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 }
22303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022304 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022305 }
22306 else
22307 {
22308 current_funccal->returned = TRUE;
22309
22310 /* If the return is carried out now, store the return value. For
22311 * a return immediately after reanimation, the value is already
22312 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022313 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022315 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022316 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022318 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022319 }
22320 }
22321
22322 return idx < 0;
22323}
22324
22325/*
22326 * Free the variable with a pending return value.
22327 */
22328 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022329discard_pending_return(rettv)
22330 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022331{
Bram Moolenaar33570922005-01-25 22:26:29 +000022332 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333}
22334
22335/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022336 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 * is an allocated string. Used by report_pending() for verbose messages.
22338 */
22339 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022340get_return_cmd(rettv)
22341 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022343 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022344 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022346
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022347 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022348 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022349 if (s == NULL)
22350 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022351
22352 STRCPY(IObuff, ":return ");
22353 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22354 if (STRLEN(s) + 8 >= IOSIZE)
22355 STRCPY(IObuff + IOSIZE - 4, "...");
22356 vim_free(tofree);
22357 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358}
22359
22360/*
22361 * Get next function line.
22362 * Called by do_cmdline() to get the next line.
22363 * Returns allocated string, or NULL for end of function.
22364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 char_u *
22366get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022367 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022369 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370{
Bram Moolenaar33570922005-01-25 22:26:29 +000022371 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022372 ufunc_T *fp = fcp->func;
22373 char_u *retval;
22374 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375
22376 /* If breakpoints have been added/deleted need to check for it. */
22377 if (fcp->dbg_tick != debug_tick)
22378 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022379 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 sourcing_lnum);
22381 fcp->dbg_tick = debug_tick;
22382 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022383#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022384 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022385 func_line_end(cookie);
22386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387
Bram Moolenaar05159a02005-02-26 23:04:13 +000022388 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022389 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22390 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 retval = NULL;
22392 else
22393 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022394 /* Skip NULL lines (continuation lines). */
22395 while (fcp->linenr < gap->ga_len
22396 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22397 ++fcp->linenr;
22398 if (fcp->linenr >= gap->ga_len)
22399 retval = NULL;
22400 else
22401 {
22402 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22403 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022404#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022405 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022406 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022407#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409 }
22410
22411 /* Did we encounter a breakpoint? */
22412 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22413 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022414 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022416 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 sourcing_lnum);
22418 fcp->dbg_tick = debug_tick;
22419 }
22420
22421 return retval;
22422}
22423
Bram Moolenaar05159a02005-02-26 23:04:13 +000022424#if defined(FEAT_PROFILE) || defined(PROTO)
22425/*
22426 * Called when starting to read a function line.
22427 * "sourcing_lnum" must be correct!
22428 * When skipping lines it may not actually be executed, but we won't find out
22429 * until later and we need to store the time now.
22430 */
22431 void
22432func_line_start(cookie)
22433 void *cookie;
22434{
22435 funccall_T *fcp = (funccall_T *)cookie;
22436 ufunc_T *fp = fcp->func;
22437
22438 if (fp->uf_profiling && sourcing_lnum >= 1
22439 && sourcing_lnum <= fp->uf_lines.ga_len)
22440 {
22441 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022442 /* Skip continuation lines. */
22443 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22444 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022445 fp->uf_tml_execed = FALSE;
22446 profile_start(&fp->uf_tml_start);
22447 profile_zero(&fp->uf_tml_children);
22448 profile_get_wait(&fp->uf_tml_wait);
22449 }
22450}
22451
22452/*
22453 * Called when actually executing a function line.
22454 */
22455 void
22456func_line_exec(cookie)
22457 void *cookie;
22458{
22459 funccall_T *fcp = (funccall_T *)cookie;
22460 ufunc_T *fp = fcp->func;
22461
22462 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22463 fp->uf_tml_execed = TRUE;
22464}
22465
22466/*
22467 * Called when done with a function line.
22468 */
22469 void
22470func_line_end(cookie)
22471 void *cookie;
22472{
22473 funccall_T *fcp = (funccall_T *)cookie;
22474 ufunc_T *fp = fcp->func;
22475
22476 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22477 {
22478 if (fp->uf_tml_execed)
22479 {
22480 ++fp->uf_tml_count[fp->uf_tml_idx];
22481 profile_end(&fp->uf_tml_start);
22482 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022483 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022484 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22485 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022486 }
22487 fp->uf_tml_idx = -1;
22488 }
22489}
22490#endif
22491
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492/*
22493 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022494 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495 */
22496 int
22497func_has_ended(cookie)
22498 void *cookie;
22499{
Bram Moolenaar33570922005-01-25 22:26:29 +000022500 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501
22502 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22503 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022504 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505 || fcp->returned);
22506}
22507
22508/*
22509 * return TRUE if cookie indicates a function which "abort"s on errors.
22510 */
22511 int
22512func_has_abort(cookie)
22513 void *cookie;
22514{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022515 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516}
22517
22518#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22519typedef enum
22520{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022521 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22522 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22523 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524} var_flavour_T;
22525
22526static var_flavour_T var_flavour __ARGS((char_u *varname));
22527
22528 static var_flavour_T
22529var_flavour(varname)
22530 char_u *varname;
22531{
22532 char_u *p = varname;
22533
22534 if (ASCII_ISUPPER(*p))
22535 {
22536 while (*(++p))
22537 if (ASCII_ISLOWER(*p))
22538 return VAR_FLAVOUR_SESSION;
22539 return VAR_FLAVOUR_VIMINFO;
22540 }
22541 else
22542 return VAR_FLAVOUR_DEFAULT;
22543}
22544#endif
22545
22546#if defined(FEAT_VIMINFO) || defined(PROTO)
22547/*
22548 * Restore global vars that start with a capital from the viminfo file
22549 */
22550 int
22551read_viminfo_varlist(virp, writing)
22552 vir_T *virp;
22553 int writing;
22554{
22555 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022556 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022557 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558
22559 if (!writing && (find_viminfo_parameter('!') != NULL))
22560 {
22561 tab = vim_strchr(virp->vir_line + 1, '\t');
22562 if (tab != NULL)
22563 {
22564 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022565 switch (*tab)
22566 {
22567 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022568#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022569 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022570#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022571 case 'D': type = VAR_DICT; break;
22572 case 'L': type = VAR_LIST; break;
22573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574
22575 tab = vim_strchr(tab, '\t');
22576 if (tab != NULL)
22577 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022578 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022579 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022580 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022581 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022582#ifdef FEAT_FLOAT
22583 else if (type == VAR_FLOAT)
22584 (void)string2float(tab + 1, &tv.vval.v_float);
22585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022587 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022588 if (type == VAR_DICT || type == VAR_LIST)
22589 {
22590 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22591
22592 if (etv == NULL)
22593 /* Failed to parse back the dict or list, use it as a
22594 * string. */
22595 tv.v_type = VAR_STRING;
22596 else
22597 {
22598 vim_free(tv.vval.v_string);
22599 tv = *etv;
22600 }
22601 }
22602
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022603 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022604
22605 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022606 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022607 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22608 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 }
22610 }
22611 }
22612
22613 return viminfo_readline(virp);
22614}
22615
22616/*
22617 * Write global vars that start with a capital to the viminfo file
22618 */
22619 void
22620write_viminfo_varlist(fp)
22621 FILE *fp;
22622{
Bram Moolenaar33570922005-01-25 22:26:29 +000022623 hashitem_T *hi;
22624 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022625 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022626 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022627 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022628 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022629 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630
22631 if (find_viminfo_parameter('!') == NULL)
22632 return;
22633
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022634 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022635
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022636 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022637 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022639 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022641 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022642 this_var = HI2DI(hi);
22643 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022644 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022645 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022646 {
22647 case VAR_STRING: s = "STR"; break;
22648 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022649#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022650 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022651#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022652 case VAR_DICT: s = "DIC"; break;
22653 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022654 default: continue;
22655 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022657 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022658 if (p != NULL)
22659 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022660 vim_free(tofree);
22661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 }
22663 }
22664}
22665#endif
22666
22667#if defined(FEAT_SESSION) || defined(PROTO)
22668 int
22669store_session_globals(fd)
22670 FILE *fd;
22671{
Bram Moolenaar33570922005-01-25 22:26:29 +000022672 hashitem_T *hi;
22673 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022674 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 char_u *p, *t;
22676
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022677 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022678 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022680 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022682 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022683 this_var = HI2DI(hi);
22684 if ((this_var->di_tv.v_type == VAR_NUMBER
22685 || this_var->di_tv.v_type == VAR_STRING)
22686 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022687 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022688 /* Escape special characters with a backslash. Turn a LF and
22689 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022690 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022691 (char_u *)"\\\"\n\r");
22692 if (p == NULL) /* out of memory */
22693 break;
22694 for (t = p; *t != NUL; ++t)
22695 if (*t == '\n')
22696 *t = 'n';
22697 else if (*t == '\r')
22698 *t = 'r';
22699 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022700 this_var->di_key,
22701 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22702 : ' ',
22703 p,
22704 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22705 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022706 || put_eol(fd) == FAIL)
22707 {
22708 vim_free(p);
22709 return FAIL;
22710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711 vim_free(p);
22712 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022713#ifdef FEAT_FLOAT
22714 else if (this_var->di_tv.v_type == VAR_FLOAT
22715 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22716 {
22717 float_T f = this_var->di_tv.vval.v_float;
22718 int sign = ' ';
22719
22720 if (f < 0)
22721 {
22722 f = -f;
22723 sign = '-';
22724 }
22725 if ((fprintf(fd, "let %s = %c&%f",
22726 this_var->di_key, sign, f) < 0)
22727 || put_eol(fd) == FAIL)
22728 return FAIL;
22729 }
22730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022731 }
22732 }
22733 return OK;
22734}
22735#endif
22736
Bram Moolenaar661b1822005-07-28 22:36:45 +000022737/*
22738 * Display script name where an item was last set.
22739 * Should only be invoked when 'verbose' is non-zero.
22740 */
22741 void
22742last_set_msg(scriptID)
22743 scid_T scriptID;
22744{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022745 char_u *p;
22746
Bram Moolenaar661b1822005-07-28 22:36:45 +000022747 if (scriptID != 0)
22748 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022749 p = home_replace_save(NULL, get_scriptname(scriptID));
22750 if (p != NULL)
22751 {
22752 verbose_enter();
22753 MSG_PUTS(_("\n\tLast set from "));
22754 MSG_PUTS(p);
22755 vim_free(p);
22756 verbose_leave();
22757 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022758 }
22759}
22760
Bram Moolenaard812df62008-11-09 12:46:09 +000022761/*
22762 * List v:oldfiles in a nice way.
22763 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022764 void
22765ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022766 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022767{
22768 list_T *l = vimvars[VV_OLDFILES].vv_list;
22769 listitem_T *li;
22770 int nr = 0;
22771
22772 if (l == NULL)
22773 msg((char_u *)_("No old files"));
22774 else
22775 {
22776 msg_start();
22777 msg_scroll = TRUE;
22778 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22779 {
22780 msg_outnum((long)++nr);
22781 MSG_PUTS(": ");
22782 msg_outtrans(get_tv_string(&li->li_tv));
22783 msg_putchar('\n');
22784 out_flush(); /* output one line at a time */
22785 ui_breakcheck();
22786 }
22787 /* Assume "got_int" was set to truncate the listing. */
22788 got_int = FALSE;
22789
22790#ifdef FEAT_BROWSE_CMD
22791 if (cmdmod.browse)
22792 {
22793 quit_more = FALSE;
22794 nr = prompt_for_number(FALSE);
22795 msg_starthere();
22796 if (nr > 0)
22797 {
22798 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22799 (long)nr);
22800
22801 if (p != NULL)
22802 {
22803 p = expand_env_save(p);
22804 eap->arg = p;
22805 eap->cmdidx = CMD_edit;
22806 cmdmod.browse = FALSE;
22807 do_exedit(eap, NULL);
22808 vim_free(p);
22809 }
22810 }
22811 }
22812#endif
22813 }
22814}
22815
Bram Moolenaar071d4272004-06-13 20:20:40 +000022816#endif /* FEAT_EVAL */
22817
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022819#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820
22821#ifdef WIN3264
22822/*
22823 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22824 */
22825static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22826static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22827static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22828
22829/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022830 * Get the short path (8.3) for the filename in "fnamep".
22831 * Only works for a valid file name.
22832 * When the path gets longer "fnamep" is changed and the allocated buffer
22833 * is put in "bufp".
22834 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22835 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836 */
22837 static int
22838get_short_pathname(fnamep, bufp, fnamelen)
22839 char_u **fnamep;
22840 char_u **bufp;
22841 int *fnamelen;
22842{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022843 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 char_u *newbuf;
22845
22846 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 l = GetShortPathName(*fnamep, *fnamep, len);
22848 if (l > len - 1)
22849 {
22850 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022851 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 newbuf = vim_strnsave(*fnamep, l);
22853 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022854 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855
22856 vim_free(*bufp);
22857 *fnamep = *bufp = newbuf;
22858
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022859 /* Really should always succeed, as the buffer is big enough. */
22860 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022861 }
22862
22863 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022864 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865}
22866
22867/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022868 * Get the short path (8.3) for the filename in "fname". The converted
22869 * path is returned in "bufp".
22870 *
22871 * Some of the directories specified in "fname" may not exist. This function
22872 * will shorten the existing directories at the beginning of the path and then
22873 * append the remaining non-existing path.
22874 *
22875 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022876 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022877 * bufp - Pointer to an allocated buffer for the filename.
22878 * fnamelen - Length of the filename pointed to by fname
22879 *
22880 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 */
22882 static int
22883shortpath_for_invalid_fname(fname, bufp, fnamelen)
22884 char_u **fname;
22885 char_u **bufp;
22886 int *fnamelen;
22887{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022888 char_u *short_fname, *save_fname, *pbuf_unused;
22889 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022890 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022891 int old_len, len;
22892 int new_len, sfx_len;
22893 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894
22895 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022896 old_len = *fnamelen;
22897 save_fname = vim_strnsave(*fname, old_len);
22898 pbuf_unused = NULL;
22899 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022901 endp = save_fname + old_len - 1; /* Find the end of the copy */
22902 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022904 /*
22905 * Try shortening the supplied path till it succeeds by removing one
22906 * directory at a time from the tail of the path.
22907 */
22908 len = 0;
22909 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022911 /* go back one path-separator */
22912 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22913 --endp;
22914 if (endp <= save_fname)
22915 break; /* processed the complete path */
22916
22917 /*
22918 * Replace the path separator with a NUL and try to shorten the
22919 * resulting path.
22920 */
22921 ch = *endp;
22922 *endp = 0;
22923 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022924 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022925 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22926 {
22927 retval = FAIL;
22928 goto theend;
22929 }
22930 *endp = ch; /* preserve the string */
22931
22932 if (len > 0)
22933 break; /* successfully shortened the path */
22934
22935 /* failed to shorten the path. Skip the path separator */
22936 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 }
22938
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022939 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022940 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022941 /*
22942 * Succeeded in shortening the path. Now concatenate the shortened
22943 * path with the remaining path at the tail.
22944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022945
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022946 /* Compute the length of the new path. */
22947 sfx_len = (int)(save_endp - endp) + 1;
22948 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022950 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022952 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022954 /* There is not enough space in the currently allocated string,
22955 * copy it to a buffer big enough. */
22956 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022958 {
22959 retval = FAIL;
22960 goto theend;
22961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 }
22963 else
22964 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022965 /* Transfer short_fname to the main buffer (it's big enough),
22966 * unless get_short_pathname() did its work in-place. */
22967 *fname = *bufp = save_fname;
22968 if (short_fname != save_fname)
22969 vim_strncpy(save_fname, short_fname, len);
22970 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022971 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022972
22973 /* concat the not-shortened part of the path */
22974 vim_strncpy(*fname + len, endp, sfx_len);
22975 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022977
22978theend:
22979 vim_free(pbuf_unused);
22980 vim_free(save_fname);
22981
22982 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983}
22984
22985/*
22986 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022987 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 */
22989 static int
22990shortpath_for_partial(fnamep, bufp, fnamelen)
22991 char_u **fnamep;
22992 char_u **bufp;
22993 int *fnamelen;
22994{
22995 int sepcount, len, tflen;
22996 char_u *p;
22997 char_u *pbuf, *tfname;
22998 int hasTilde;
22999
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023000 /* Count up the path separators from the RHS.. so we know which part
23001 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023003 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004 if (vim_ispathsep(*p))
23005 ++sepcount;
23006
23007 /* Need full path first (use expand_env() to remove a "~/") */
23008 hasTilde = (**fnamep == '~');
23009 if (hasTilde)
23010 pbuf = tfname = expand_env_save(*fnamep);
23011 else
23012 pbuf = tfname = FullName_save(*fnamep, FALSE);
23013
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023014 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023016 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23017 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018
23019 if (len == 0)
23020 {
23021 /* Don't have a valid filename, so shorten the rest of the
23022 * path if we can. This CAN give us invalid 8.3 filenames, but
23023 * there's not a lot of point in guessing what it might be.
23024 */
23025 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023026 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23027 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028 }
23029
23030 /* Count the paths backward to find the beginning of the desired string. */
23031 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023032 {
23033#ifdef FEAT_MBYTE
23034 if (has_mbyte)
23035 p -= mb_head_off(tfname, p);
23036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 if (vim_ispathsep(*p))
23038 {
23039 if (sepcount == 0 || (hasTilde && sepcount == 1))
23040 break;
23041 else
23042 sepcount --;
23043 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 if (hasTilde)
23046 {
23047 --p;
23048 if (p >= tfname)
23049 *p = '~';
23050 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023051 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 }
23053 else
23054 ++p;
23055
23056 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23057 vim_free(*bufp);
23058 *fnamelen = (int)STRLEN(p);
23059 *bufp = pbuf;
23060 *fnamep = p;
23061
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023062 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063}
23064#endif /* WIN3264 */
23065
23066/*
23067 * Adjust a filename, according to a string of modifiers.
23068 * *fnamep must be NUL terminated when called. When returning, the length is
23069 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023070 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 * When there is an error, *fnamep is set to NULL.
23072 */
23073 int
23074modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23075 char_u *src; /* string with modifiers */
23076 int *usedlen; /* characters after src that are used */
23077 char_u **fnamep; /* file name so far */
23078 char_u **bufp; /* buffer for allocated file name or NULL */
23079 int *fnamelen; /* length of fnamep */
23080{
23081 int valid = 0;
23082 char_u *tail;
23083 char_u *s, *p, *pbuf;
23084 char_u dirname[MAXPATHL];
23085 int c;
23086 int has_fullname = 0;
23087#ifdef WIN3264
23088 int has_shortname = 0;
23089#endif
23090
23091repeat:
23092 /* ":p" - full path/file_name */
23093 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23094 {
23095 has_fullname = 1;
23096
23097 valid |= VALID_PATH;
23098 *usedlen += 2;
23099
23100 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23101 if ((*fnamep)[0] == '~'
23102#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23103 && ((*fnamep)[1] == '/'
23104# ifdef BACKSLASH_IN_FILENAME
23105 || (*fnamep)[1] == '\\'
23106# endif
23107 || (*fnamep)[1] == NUL)
23108
23109#endif
23110 )
23111 {
23112 *fnamep = expand_env_save(*fnamep);
23113 vim_free(*bufp); /* free any allocated file name */
23114 *bufp = *fnamep;
23115 if (*fnamep == NULL)
23116 return -1;
23117 }
23118
23119 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023120 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 {
23122 if (vim_ispathsep(*p)
23123 && p[1] == '.'
23124 && (p[2] == NUL
23125 || vim_ispathsep(p[2])
23126 || (p[2] == '.'
23127 && (p[3] == NUL || vim_ispathsep(p[3])))))
23128 break;
23129 }
23130
23131 /* FullName_save() is slow, don't use it when not needed. */
23132 if (*p != NUL || !vim_isAbsName(*fnamep))
23133 {
23134 *fnamep = FullName_save(*fnamep, *p != NUL);
23135 vim_free(*bufp); /* free any allocated file name */
23136 *bufp = *fnamep;
23137 if (*fnamep == NULL)
23138 return -1;
23139 }
23140
23141 /* Append a path separator to a directory. */
23142 if (mch_isdir(*fnamep))
23143 {
23144 /* Make room for one or two extra characters. */
23145 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23146 vim_free(*bufp); /* free any allocated file name */
23147 *bufp = *fnamep;
23148 if (*fnamep == NULL)
23149 return -1;
23150 add_pathsep(*fnamep);
23151 }
23152 }
23153
23154 /* ":." - path relative to the current directory */
23155 /* ":~" - path relative to the home directory */
23156 /* ":8" - shortname path - postponed till after */
23157 while (src[*usedlen] == ':'
23158 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23159 {
23160 *usedlen += 2;
23161 if (c == '8')
23162 {
23163#ifdef WIN3264
23164 has_shortname = 1; /* Postpone this. */
23165#endif
23166 continue;
23167 }
23168 pbuf = NULL;
23169 /* Need full path first (use expand_env() to remove a "~/") */
23170 if (!has_fullname)
23171 {
23172 if (c == '.' && **fnamep == '~')
23173 p = pbuf = expand_env_save(*fnamep);
23174 else
23175 p = pbuf = FullName_save(*fnamep, FALSE);
23176 }
23177 else
23178 p = *fnamep;
23179
23180 has_fullname = 0;
23181
23182 if (p != NULL)
23183 {
23184 if (c == '.')
23185 {
23186 mch_dirname(dirname, MAXPATHL);
23187 s = shorten_fname(p, dirname);
23188 if (s != NULL)
23189 {
23190 *fnamep = s;
23191 if (pbuf != NULL)
23192 {
23193 vim_free(*bufp); /* free any allocated file name */
23194 *bufp = pbuf;
23195 pbuf = NULL;
23196 }
23197 }
23198 }
23199 else
23200 {
23201 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23202 /* Only replace it when it starts with '~' */
23203 if (*dirname == '~')
23204 {
23205 s = vim_strsave(dirname);
23206 if (s != NULL)
23207 {
23208 *fnamep = s;
23209 vim_free(*bufp);
23210 *bufp = s;
23211 }
23212 }
23213 }
23214 vim_free(pbuf);
23215 }
23216 }
23217
23218 tail = gettail(*fnamep);
23219 *fnamelen = (int)STRLEN(*fnamep);
23220
23221 /* ":h" - head, remove "/file_name", can be repeated */
23222 /* Don't remove the first "/" or "c:\" */
23223 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23224 {
23225 valid |= VALID_HEAD;
23226 *usedlen += 2;
23227 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023228 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023229 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230 *fnamelen = (int)(tail - *fnamep);
23231#ifdef VMS
23232 if (*fnamelen > 0)
23233 *fnamelen += 1; /* the path separator is part of the path */
23234#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023235 if (*fnamelen == 0)
23236 {
23237 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23238 p = vim_strsave((char_u *)".");
23239 if (p == NULL)
23240 return -1;
23241 vim_free(*bufp);
23242 *bufp = *fnamep = tail = p;
23243 *fnamelen = 1;
23244 }
23245 else
23246 {
23247 while (tail > s && !after_pathsep(s, tail))
23248 mb_ptr_back(*fnamep, tail);
23249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 }
23251
23252 /* ":8" - shortname */
23253 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23254 {
23255 *usedlen += 2;
23256#ifdef WIN3264
23257 has_shortname = 1;
23258#endif
23259 }
23260
23261#ifdef WIN3264
23262 /* Check shortname after we have done 'heads' and before we do 'tails'
23263 */
23264 if (has_shortname)
23265 {
23266 pbuf = NULL;
23267 /* Copy the string if it is shortened by :h */
23268 if (*fnamelen < (int)STRLEN(*fnamep))
23269 {
23270 p = vim_strnsave(*fnamep, *fnamelen);
23271 if (p == 0)
23272 return -1;
23273 vim_free(*bufp);
23274 *bufp = *fnamep = p;
23275 }
23276
23277 /* Split into two implementations - makes it easier. First is where
23278 * there isn't a full name already, second is where there is.
23279 */
23280 if (!has_fullname && !vim_isAbsName(*fnamep))
23281 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023282 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 return -1;
23284 }
23285 else
23286 {
23287 int l;
23288
23289 /* Simple case, already have the full-name
23290 * Nearly always shorter, so try first time. */
23291 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023292 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 return -1;
23294
23295 if (l == 0)
23296 {
23297 /* Couldn't find the filename.. search the paths.
23298 */
23299 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023300 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 return -1;
23302 }
23303 *fnamelen = l;
23304 }
23305 }
23306#endif /* WIN3264 */
23307
23308 /* ":t" - tail, just the basename */
23309 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23310 {
23311 *usedlen += 2;
23312 *fnamelen -= (int)(tail - *fnamep);
23313 *fnamep = tail;
23314 }
23315
23316 /* ":e" - extension, can be repeated */
23317 /* ":r" - root, without extension, can be repeated */
23318 while (src[*usedlen] == ':'
23319 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23320 {
23321 /* find a '.' in the tail:
23322 * - for second :e: before the current fname
23323 * - otherwise: The last '.'
23324 */
23325 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23326 s = *fnamep - 2;
23327 else
23328 s = *fnamep + *fnamelen - 1;
23329 for ( ; s > tail; --s)
23330 if (s[0] == '.')
23331 break;
23332 if (src[*usedlen + 1] == 'e') /* :e */
23333 {
23334 if (s > tail)
23335 {
23336 *fnamelen += (int)(*fnamep - (s + 1));
23337 *fnamep = s + 1;
23338#ifdef VMS
23339 /* cut version from the extension */
23340 s = *fnamep + *fnamelen - 1;
23341 for ( ; s > *fnamep; --s)
23342 if (s[0] == ';')
23343 break;
23344 if (s > *fnamep)
23345 *fnamelen = s - *fnamep;
23346#endif
23347 }
23348 else if (*fnamep <= tail)
23349 *fnamelen = 0;
23350 }
23351 else /* :r */
23352 {
23353 if (s > tail) /* remove one extension */
23354 *fnamelen = (int)(s - *fnamep);
23355 }
23356 *usedlen += 2;
23357 }
23358
23359 /* ":s?pat?foo?" - substitute */
23360 /* ":gs?pat?foo?" - global substitute */
23361 if (src[*usedlen] == ':'
23362 && (src[*usedlen + 1] == 's'
23363 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23364 {
23365 char_u *str;
23366 char_u *pat;
23367 char_u *sub;
23368 int sep;
23369 char_u *flags;
23370 int didit = FALSE;
23371
23372 flags = (char_u *)"";
23373 s = src + *usedlen + 2;
23374 if (src[*usedlen + 1] == 'g')
23375 {
23376 flags = (char_u *)"g";
23377 ++s;
23378 }
23379
23380 sep = *s++;
23381 if (sep)
23382 {
23383 /* find end of pattern */
23384 p = vim_strchr(s, sep);
23385 if (p != NULL)
23386 {
23387 pat = vim_strnsave(s, (int)(p - s));
23388 if (pat != NULL)
23389 {
23390 s = p + 1;
23391 /* find end of substitution */
23392 p = vim_strchr(s, sep);
23393 if (p != NULL)
23394 {
23395 sub = vim_strnsave(s, (int)(p - s));
23396 str = vim_strnsave(*fnamep, *fnamelen);
23397 if (sub != NULL && str != NULL)
23398 {
23399 *usedlen = (int)(p + 1 - src);
23400 s = do_string_sub(str, pat, sub, flags);
23401 if (s != NULL)
23402 {
23403 *fnamep = s;
23404 *fnamelen = (int)STRLEN(s);
23405 vim_free(*bufp);
23406 *bufp = s;
23407 didit = TRUE;
23408 }
23409 }
23410 vim_free(sub);
23411 vim_free(str);
23412 }
23413 vim_free(pat);
23414 }
23415 }
23416 /* after using ":s", repeat all the modifiers */
23417 if (didit)
23418 goto repeat;
23419 }
23420 }
23421
23422 return valid;
23423}
23424
23425/*
23426 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23427 * "flags" can be "g" to do a global substitute.
23428 * Returns an allocated string, NULL for error.
23429 */
23430 char_u *
23431do_string_sub(str, pat, sub, flags)
23432 char_u *str;
23433 char_u *pat;
23434 char_u *sub;
23435 char_u *flags;
23436{
23437 int sublen;
23438 regmatch_T regmatch;
23439 int i;
23440 int do_all;
23441 char_u *tail;
23442 garray_T ga;
23443 char_u *ret;
23444 char_u *save_cpo;
23445
23446 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23447 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023448 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449
23450 ga_init2(&ga, 1, 200);
23451
23452 do_all = (flags[0] == 'g');
23453
23454 regmatch.rm_ic = p_ic;
23455 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23456 if (regmatch.regprog != NULL)
23457 {
23458 tail = str;
23459 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23460 {
23461 /*
23462 * Get some space for a temporary buffer to do the substitution
23463 * into. It will contain:
23464 * - The text up to where the match is.
23465 * - The substituted text.
23466 * - The text after the match.
23467 */
23468 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23469 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23470 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23471 {
23472 ga_clear(&ga);
23473 break;
23474 }
23475
23476 /* copy the text up to where the match is */
23477 i = (int)(regmatch.startp[0] - tail);
23478 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23479 /* add the substituted text */
23480 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23481 + ga.ga_len + i, TRUE, TRUE, FALSE);
23482 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 /* avoid getting stuck on a match with an empty string */
23484 if (tail == regmatch.endp[0])
23485 {
23486 if (*tail == NUL)
23487 break;
23488 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23489 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 }
23491 else
23492 {
23493 tail = regmatch.endp[0];
23494 if (*tail == NUL)
23495 break;
23496 }
23497 if (!do_all)
23498 break;
23499 }
23500
23501 if (ga.ga_data != NULL)
23502 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23503
23504 vim_free(regmatch.regprog);
23505 }
23506
23507 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23508 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023509 if (p_cpo == empty_option)
23510 p_cpo = save_cpo;
23511 else
23512 /* Darn, evaluating {sub} expression changed the value. */
23513 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514
23515 return ret;
23516}
23517
23518#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */