blob: 7c3abf1f57be355454c92847f2f4b3bdb84efd2e [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));
2329 else
2330 {
2331 c1 = name[len];
2332 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002333 p = get_tv_string_chk(tv);
2334 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002335 {
2336 int mustfree = FALSE;
2337 char_u *s = vim_getenv(name, &mustfree);
2338
2339 if (s != NULL)
2340 {
2341 p = tofree = concat_str(s, p);
2342 if (mustfree)
2343 vim_free(s);
2344 }
2345 }
2346 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002347 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002348 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002349 if (STRICMP(name, "HOME") == 0)
2350 init_homedir();
2351 else if (didset_vim && STRICMP(name, "VIM") == 0)
2352 didset_vim = FALSE;
2353 else if (didset_vimruntime
2354 && STRICMP(name, "VIMRUNTIME") == 0)
2355 didset_vimruntime = FALSE;
2356 arg_end = arg;
2357 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002359 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
2361 }
2362 }
2363
2364 /*
2365 * ":let &option = expr": Set option value.
2366 * ":let &l:option = expr": Set local option value.
2367 * ":let &g:option = expr": Set global option value.
2368 */
2369 else if (*arg == '&')
2370 {
2371 /* Find the end of the name. */
2372 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002373 if (p == NULL || (endchars != NULL
2374 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 EMSG(_(e_letunexp));
2376 else
2377 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 long n;
2379 int opt_type;
2380 long numval;
2381 char_u *stringval = NULL;
2382 char_u *s;
2383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 c1 = *p;
2385 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386
2387 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002388 s = get_tv_string_chk(tv); /* != NULL if number or string */
2389 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002390 {
2391 opt_type = get_option_value(arg, &numval,
2392 &stringval, opt_flags);
2393 if ((opt_type == 1 && *op == '.')
2394 || (opt_type == 0 && *op != '.'))
2395 EMSG2(_(e_letwrong), op);
2396 else
2397 {
2398 if (opt_type == 1) /* number */
2399 {
2400 if (*op == '+')
2401 n = numval + n;
2402 else
2403 n = numval - n;
2404 }
2405 else if (opt_type == 0 && stringval != NULL) /* string */
2406 {
2407 s = concat_str(stringval, s);
2408 vim_free(stringval);
2409 stringval = s;
2410 }
2411 }
2412 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 if (s != NULL)
2414 {
2415 set_option_value(arg, n, s, opt_flags);
2416 arg_end = p;
2417 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002418 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 }
2421 }
2422
2423 /*
2424 * ":let @r = expr": Set register contents.
2425 */
2426 else if (*arg == '@')
2427 {
2428 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002429 if (op != NULL && (*op == '+' || *op == '-'))
2430 EMSG2(_(e_letwrong), op);
2431 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 EMSG(_(e_letunexp));
2434 else
2435 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002436 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 char_u *s;
2438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 p = get_tv_string_chk(tv);
2440 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002442 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 if (s != NULL)
2444 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 vim_free(s);
2447 }
2448 }
2449 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 arg_end = arg + 1;
2453 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002455 }
2456 }
2457
2458 /*
2459 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002462 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002463 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002464 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002465
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002466 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2470 EMSG(_(e_letunexp));
2471 else
2472 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 arg_end = p;
2475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002478 }
2479
2480 else
2481 EMSG2(_(e_invarg2), arg);
2482
2483 return arg_end;
2484}
2485
2486/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002487 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2488 */
2489 static int
2490check_changedtick(arg)
2491 char_u *arg;
2492{
2493 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2494 {
2495 EMSG2(_(e_readonlyvar), arg);
2496 return TRUE;
2497 }
2498 return FALSE;
2499}
2500
2501/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 * Get an lval: variable, Dict item or List item that can be assigned a value
2503 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2504 * "name.key", "name.key[expr]" etc.
2505 * Indexing only works if "name" is an existing List or Dictionary.
2506 * "name" points to the start of the name.
2507 * If "rettv" is not NULL it points to the value to be assigned.
2508 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2509 * wrong; must end in space or cmd separator.
2510 *
2511 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002512 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002513 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 */
2515 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002516get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002518 typval_T *rettv;
2519 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 int unlet;
2521 int skip;
2522 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 char_u *expr_start, *expr_end;
2527 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 dictitem_T *v;
2529 typval_T var1;
2530 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002533 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002534 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002536
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539
2540 if (skip)
2541 {
2542 /* When skipping just find the end of the name. */
2543 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002544 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545 }
2546
2547 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002548 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (expr_start != NULL)
2550 {
2551 /* Don't expand the name when we already know there is an error. */
2552 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2553 && *p != '[' && *p != '.')
2554 {
2555 EMSG(_(e_trailing));
2556 return NULL;
2557 }
2558
2559 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2560 if (lp->ll_exp_name == NULL)
2561 {
2562 /* Report an invalid expression in braces, unless the
2563 * expression evaluation has been cancelled due to an
2564 * aborting error, an interrupt, or an exception. */
2565 if (!aborting() && !quiet)
2566 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002567 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 EMSG2(_(e_invarg2), name);
2569 return NULL;
2570 }
2571 }
2572 lp->ll_name = lp->ll_exp_name;
2573 }
2574 else
2575 lp->ll_name = name;
2576
2577 /* Without [idx] or .key we are done. */
2578 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2579 return p;
2580
2581 cc = *p;
2582 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002583 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (v == NULL && !quiet)
2585 EMSG2(_(e_undefvar), lp->ll_name);
2586 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 if (v == NULL)
2588 return NULL;
2589
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 /*
2591 * Loop until no more [idx] or .key is following.
2592 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002594 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002595 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2597 && !(lp->ll_tv->v_type == VAR_DICT
2598 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 if (!quiet)
2601 EMSG(_("E689: Can only index a List or Dictionary"));
2602 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E708: [:] must come last"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610
Bram Moolenaar8c711452005-01-14 21:53:12 +00002611 len = -1;
2612 if (*p == '.')
2613 {
2614 key = p + 1;
2615 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2616 ;
2617 if (len == 0)
2618 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002619 if (!quiet)
2620 EMSG(_(e_emptykey));
2621 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002622 }
2623 p = key + len;
2624 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002625 else
2626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002628 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 if (*p == ':')
2630 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 empty1 = FALSE;
2634 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002636 if (get_tv_string_chk(&var1) == NULL)
2637 {
2638 /* not a number or string */
2639 clear_tv(&var1);
2640 return NULL;
2641 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 }
2643
2644 /* Optionally get the second index [ :expr]. */
2645 if (*p == ':')
2646 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002649 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002651 if (!empty1)
2652 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002654 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (rettv != NULL && (rettv->v_type != VAR_LIST
2656 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
2659 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 if (!empty1)
2661 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 }
2664 p = skipwhite(p + 1);
2665 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 else
2668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (!empty1)
2673 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002676 if (get_tv_string_chk(&var2) == NULL)
2677 {
2678 /* not a number or string */
2679 if (!empty1)
2680 clear_tv(&var1);
2681 clear_tv(&var2);
2682 return NULL;
2683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002688 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002689
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 if (!empty1)
2695 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 }
2700
2701 /* Skip to past ']'. */
2702 ++p;
2703 }
2704
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 {
2707 if (len == -1)
2708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 if (*key == NUL)
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (!quiet)
2714 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002719 lp->ll_list = NULL;
2720 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002721 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002724 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002727 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 if (len == -1)
2730 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 }
2733 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (len == -1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 p = NULL;
2741 break;
2742 }
2743 if (len == -1)
2744 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 }
2747 else
2748 {
2749 /*
2750 * Get the number and item for the only or first index of the List.
2751 */
2752 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 else
2755 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002756 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var1);
2758 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002759 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 lp->ll_list = lp->ll_tv->vval.v_list;
2761 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2762 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002764 if (lp->ll_n1 < 0)
2765 {
2766 lp->ll_n1 = 0;
2767 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2768 }
2769 }
2770 if (lp->ll_li == NULL)
2771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 }
2776
2777 /*
2778 * May need to find the item or absolute index for the second
2779 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 * When no index given: "lp->ll_empty2" is TRUE.
2781 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002785 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (ni == NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 }
2794
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2796 if (lp->ll_n1 < 0)
2797 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2798 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002800 }
2801
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002802 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002804 }
2805
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 return p;
2807}
2808
2809/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002810 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 */
2812 static void
2813clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002814 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815{
2816 vim_free(lp->ll_exp_name);
2817 vim_free(lp->ll_newkey);
2818}
2819
2820/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002821 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002823 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 */
2825 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002826set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002827 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002829 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002831 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832{
2833 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002834 listitem_T *ri;
2835 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836
2837 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002838 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002840 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 cc = *endp;
2842 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002843 if (op != NULL && *op != '=')
2844 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002845 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002847 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002848 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002849 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002850 {
2851 if (tv_op(&tv, rettv, op) == OK)
2852 set_var(lp->ll_name, &tv, FALSE);
2853 clear_tv(&tv);
2854 }
2855 }
2856 else
2857 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002859 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002861 else if (tv_check_lock(lp->ll_newkey == NULL
2862 ? lp->ll_tv->v_lock
2863 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2864 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 else if (lp->ll_range)
2866 {
2867 /*
2868 * Assign the List values to the list items.
2869 */
2870 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002871 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002872 if (op != NULL && *op != '=')
2873 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2874 else
2875 {
2876 clear_tv(&lp->ll_li->li_tv);
2877 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2878 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 ri = ri->li_next;
2880 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2881 break;
2882 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002883 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002885 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002886 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 ri = NULL;
2888 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002889 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002890 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_li = lp->ll_li->li_next;
2892 ++lp->ll_n1;
2893 }
2894 if (ri != NULL)
2895 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 else if (lp->ll_empty2
2897 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 : lp->ll_n1 != lp->ll_n2)
2899 EMSG(_("E711: List value has not enough items"));
2900 }
2901 else
2902 {
2903 /*
2904 * Assign to a List or Dictionary item.
2905 */
2906 if (lp->ll_newkey != NULL)
2907 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002908 if (op != NULL && *op != '=')
2909 {
2910 EMSG2(_(e_letwrong), op);
2911 return;
2912 }
2913
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002915 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (di == NULL)
2917 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002918 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2919 {
2920 vim_free(di);
2921 return;
2922 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002923 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 else if (op != NULL && *op != '=')
2926 {
2927 tv_op(lp->ll_tv, rettv, op);
2928 return;
2929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 /*
2934 * Assign the value to the variable or list item.
2935 */
2936 if (copy)
2937 copy_tv(rettv, lp->ll_tv);
2938 else
2939 {
2940 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002941 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002943 }
2944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945}
2946
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002947/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002948 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2949 * Returns OK or FAIL.
2950 */
2951 static int
2952tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 typval_T *tv1;
2954 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 char_u *op;
2956{
2957 long n;
2958 char_u numbuf[NUMBUFLEN];
2959 char_u *s;
2960
2961 /* Can't do anything with a Funcref or a Dict on the right. */
2962 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2963 {
2964 switch (tv1->v_type)
2965 {
2966 case VAR_DICT:
2967 case VAR_FUNC:
2968 break;
2969
2970 case VAR_LIST:
2971 if (*op != '+' || tv2->v_type != VAR_LIST)
2972 break;
2973 /* List += List */
2974 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2975 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2976 return OK;
2977
2978 case VAR_NUMBER:
2979 case VAR_STRING:
2980 if (tv2->v_type == VAR_LIST)
2981 break;
2982 if (*op == '+' || *op == '-')
2983 {
2984 /* nr += nr or nr -= nr*/
2985 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002986#ifdef FEAT_FLOAT
2987 if (tv2->v_type == VAR_FLOAT)
2988 {
2989 float_T f = n;
2990
2991 if (*op == '+')
2992 f += tv2->vval.v_float;
2993 else
2994 f -= tv2->vval.v_float;
2995 clear_tv(tv1);
2996 tv1->v_type = VAR_FLOAT;
2997 tv1->vval.v_float = f;
2998 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002999 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003000#endif
3001 {
3002 if (*op == '+')
3003 n += get_tv_number(tv2);
3004 else
3005 n -= get_tv_number(tv2);
3006 clear_tv(tv1);
3007 tv1->v_type = VAR_NUMBER;
3008 tv1->vval.v_number = n;
3009 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003010 }
3011 else
3012 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003013 if (tv2->v_type == VAR_FLOAT)
3014 break;
3015
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 /* str .= str */
3017 s = get_tv_string(tv1);
3018 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3019 clear_tv(tv1);
3020 tv1->v_type = VAR_STRING;
3021 tv1->vval.v_string = s;
3022 }
3023 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003024
3025#ifdef FEAT_FLOAT
3026 case VAR_FLOAT:
3027 {
3028 float_T f;
3029
3030 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3031 && tv2->v_type != VAR_NUMBER
3032 && tv2->v_type != VAR_STRING))
3033 break;
3034 if (tv2->v_type == VAR_FLOAT)
3035 f = tv2->vval.v_float;
3036 else
3037 f = get_tv_number(tv2);
3038 if (*op == '+')
3039 tv1->vval.v_float += f;
3040 else
3041 tv1->vval.v_float -= f;
3042 }
3043 return OK;
3044#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003045 }
3046 }
3047
3048 EMSG2(_(e_letwrong), op);
3049 return FAIL;
3050}
3051
3052/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003053 * Add a watcher to a list.
3054 */
3055 static void
3056list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003057 list_T *l;
3058 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003059{
3060 lw->lw_next = l->lv_watch;
3061 l->lv_watch = lw;
3062}
3063
3064/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003065 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003066 * No warning when it isn't found...
3067 */
3068 static void
3069list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003070 list_T *l;
3071 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003072{
Bram Moolenaar33570922005-01-25 22:26:29 +00003073 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003074
3075 lwp = &l->lv_watch;
3076 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3077 {
3078 if (lw == lwrem)
3079 {
3080 *lwp = lw->lw_next;
3081 break;
3082 }
3083 lwp = &lw->lw_next;
3084 }
3085}
3086
3087/*
3088 * Just before removing an item from a list: advance watchers to the next
3089 * item.
3090 */
3091 static void
3092list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003093 list_T *l;
3094 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003095{
Bram Moolenaar33570922005-01-25 22:26:29 +00003096 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003097
3098 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3099 if (lw->lw_item == item)
3100 lw->lw_item = item->li_next;
3101}
3102
3103/*
3104 * Evaluate the expression used in a ":for var in expr" command.
3105 * "arg" points to "var".
3106 * Set "*errp" to TRUE for an error, FALSE otherwise;
3107 * Return a pointer that holds the info. Null when there is an error.
3108 */
3109 void *
3110eval_for_line(arg, errp, nextcmdp, skip)
3111 char_u *arg;
3112 int *errp;
3113 char_u **nextcmdp;
3114 int skip;
3115{
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003117 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003118 typval_T tv;
3119 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120
3121 *errp = TRUE; /* default: there is an error */
3122
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003124 if (fi == NULL)
3125 return NULL;
3126
3127 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3128 if (expr == NULL)
3129 return fi;
3130
3131 expr = skipwhite(expr);
3132 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3133 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003134 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003135 return fi;
3136 }
3137
3138 if (skip)
3139 ++emsg_skip;
3140 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3141 {
3142 *errp = FALSE;
3143 if (!skip)
3144 {
3145 l = tv.vval.v_list;
3146 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003147 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003149 clear_tv(&tv);
3150 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151 else
3152 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003153 /* No need to increment the refcount, it's already set for the
3154 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003155 fi->fi_list = l;
3156 list_add_watch(l, &fi->fi_lw);
3157 fi->fi_lw.lw_item = l->lv_first;
3158 }
3159 }
3160 }
3161 if (skip)
3162 --emsg_skip;
3163
3164 return fi;
3165}
3166
3167/*
3168 * Use the first item in a ":for" list. Advance to the next.
3169 * Assign the values to the variable (list). "arg" points to the first one.
3170 * Return TRUE when a valid item was found, FALSE when at end of list or
3171 * something wrong.
3172 */
3173 int
3174next_for_item(fi_void, arg)
3175 void *fi_void;
3176 char_u *arg;
3177{
Bram Moolenaar33570922005-01-25 22:26:29 +00003178 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003179 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003180 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003181
3182 item = fi->fi_lw.lw_item;
3183 if (item == NULL)
3184 result = FALSE;
3185 else
3186 {
3187 fi->fi_lw.lw_item = item->li_next;
3188 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3189 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3190 }
3191 return result;
3192}
3193
3194/*
3195 * Free the structure used to store info used by ":for".
3196 */
3197 void
3198free_for_info(fi_void)
3199 void *fi_void;
3200{
Bram Moolenaar33570922005-01-25 22:26:29 +00003201 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003203 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003204 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003206 list_unref(fi->fi_list);
3207 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 vim_free(fi);
3209}
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3212
3213 void
3214set_context_for_expression(xp, arg, cmdidx)
3215 expand_T *xp;
3216 char_u *arg;
3217 cmdidx_T cmdidx;
3218{
3219 int got_eq = FALSE;
3220 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223 if (cmdidx == CMD_let)
3224 {
3225 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003226 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227 {
3228 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003229 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003230 {
3231 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003232 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 if (vim_iswhite(*p))
3234 break;
3235 }
3236 return;
3237 }
3238 }
3239 else
3240 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3241 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 while ((xp->xp_pattern = vim_strpbrk(arg,
3243 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3244 {
3245 c = *xp->xp_pattern;
3246 if (c == '&')
3247 {
3248 c = xp->xp_pattern[1];
3249 if (c == '&')
3250 {
3251 ++xp->xp_pattern;
3252 xp->xp_context = cmdidx != CMD_let || got_eq
3253 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3254 }
3255 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003258 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3259 xp->xp_pattern += 2;
3260
3261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 else if (c == '$')
3264 {
3265 /* environment variable */
3266 xp->xp_context = EXPAND_ENV_VARS;
3267 }
3268 else if (c == '=')
3269 {
3270 got_eq = TRUE;
3271 xp->xp_context = EXPAND_EXPRESSION;
3272 }
3273 else if (c == '<'
3274 && xp->xp_context == EXPAND_FUNCTIONS
3275 && vim_strchr(xp->xp_pattern, '(') == NULL)
3276 {
3277 /* Function name can start with "<SNR>" */
3278 break;
3279 }
3280 else if (cmdidx != CMD_let || got_eq)
3281 {
3282 if (c == '"') /* string */
3283 {
3284 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3285 if (c == '\\' && xp->xp_pattern[1] != NUL)
3286 ++xp->xp_pattern;
3287 xp->xp_context = EXPAND_NOTHING;
3288 }
3289 else if (c == '\'') /* literal string */
3290 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003291 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3293 /* skip */ ;
3294 xp->xp_context = EXPAND_NOTHING;
3295 }
3296 else if (c == '|')
3297 {
3298 if (xp->xp_pattern[1] == '|')
3299 {
3300 ++xp->xp_pattern;
3301 xp->xp_context = EXPAND_EXPRESSION;
3302 }
3303 else
3304 xp->xp_context = EXPAND_COMMANDS;
3305 }
3306 else
3307 xp->xp_context = EXPAND_EXPRESSION;
3308 }
3309 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310 /* Doesn't look like something valid, expand as an expression
3311 * anyway. */
3312 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 arg = xp->xp_pattern;
3314 if (*arg != NUL)
3315 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3316 /* skip */ ;
3317 }
3318 xp->xp_pattern = arg;
3319}
3320
3321#endif /* FEAT_CMDL_COMPL */
3322
3323/*
3324 * ":1,25call func(arg1, arg2)" function call.
3325 */
3326 void
3327ex_call(eap)
3328 exarg_T *eap;
3329{
3330 char_u *arg = eap->arg;
3331 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003333 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003335 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 linenr_T lnum;
3337 int doesrange;
3338 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003339 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003341 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003342 if (fudi.fd_newkey != NULL)
3343 {
3344 /* Still need to give an error message for missing key. */
3345 EMSG2(_(e_dictkey), fudi.fd_newkey);
3346 vim_free(fudi.fd_newkey);
3347 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003348 if (tofree == NULL)
3349 return;
3350
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003351 /* Increase refcount on dictionary, it could get deleted when evaluating
3352 * the arguments. */
3353 if (fudi.fd_dict != NULL)
3354 ++fudi.fd_dict->dv_refcount;
3355
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003356 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003357 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003358 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
Bram Moolenaar532c7802005-01-27 14:44:31 +00003360 /* Skip white space to allow ":call func ()". Not good, but required for
3361 * backward compatibility. */
3362 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003363 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365 if (*startarg != '(')
3366 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003367 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 goto end;
3369 }
3370
3371 /*
3372 * When skipping, evaluate the function once, to find the end of the
3373 * arguments.
3374 * When the function takes a range, this is discovered after the first
3375 * call, and the loop is broken.
3376 */
3377 if (eap->skip)
3378 {
3379 ++emsg_skip;
3380 lnum = eap->line2; /* do it once, also with an invalid range */
3381 }
3382 else
3383 lnum = eap->line1;
3384 for ( ; lnum <= eap->line2; ++lnum)
3385 {
3386 if (!eap->skip && eap->addr_count > 0)
3387 {
3388 curwin->w_cursor.lnum = lnum;
3389 curwin->w_cursor.col = 0;
3390 }
3391 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003392 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003393 eap->line1, eap->line2, &doesrange,
3394 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 {
3396 failed = TRUE;
3397 break;
3398 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003399
3400 /* Handle a function returning a Funcref, Dictionary or List. */
3401 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3402 {
3403 failed = TRUE;
3404 break;
3405 }
3406
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003407 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 if (doesrange || eap->skip)
3409 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003412 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003413 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003414 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (aborting())
3416 break;
3417 }
3418 if (eap->skip)
3419 --emsg_skip;
3420
3421 if (!failed)
3422 {
3423 /* Check for trailing illegal characters and a following command. */
3424 if (!ends_excmd(*arg))
3425 {
3426 emsg_severe = TRUE;
3427 EMSG(_(e_trailing));
3428 }
3429 else
3430 eap->nextcmd = check_nextcmd(arg);
3431 }
3432
3433end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003434 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003435 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436}
3437
3438/*
3439 * ":unlet[!] var1 ... " command.
3440 */
3441 void
3442ex_unlet(eap)
3443 exarg_T *eap;
3444{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003445 ex_unletlock(eap, eap->arg, 0);
3446}
3447
3448/*
3449 * ":lockvar" and ":unlockvar" commands
3450 */
3451 void
3452ex_lockvar(eap)
3453 exarg_T *eap;
3454{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003456 int deep = 2;
3457
3458 if (eap->forceit)
3459 deep = -1;
3460 else if (vim_isdigit(*arg))
3461 {
3462 deep = getdigits(&arg);
3463 arg = skipwhite(arg);
3464 }
3465
3466 ex_unletlock(eap, arg, deep);
3467}
3468
3469/*
3470 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3471 */
3472 static void
3473ex_unletlock(eap, argstart, deep)
3474 exarg_T *eap;
3475 char_u *argstart;
3476 int deep;
3477{
3478 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003481 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482
3483 do
3484 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003485 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003486 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3487 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003488 if (lv.ll_name == NULL)
3489 error = TRUE; /* error but continue parsing */
3490 if (name_end == NULL || (!vim_iswhite(*name_end)
3491 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003493 if (name_end != NULL)
3494 {
3495 emsg_severe = TRUE;
3496 EMSG(_(e_trailing));
3497 }
3498 if (!(eap->skip || error))
3499 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 break;
3501 }
3502
3503 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003504 {
3505 if (eap->cmdidx == CMD_unlet)
3506 {
3507 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3508 error = TRUE;
3509 }
3510 else
3511 {
3512 if (do_lock_var(&lv, name_end, deep,
3513 eap->cmdidx == CMD_lockvar) == FAIL)
3514 error = TRUE;
3515 }
3516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003518 if (!eap->skip)
3519 clear_lval(&lv);
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 arg = skipwhite(name_end);
3522 } while (!ends_excmd(*arg));
3523
3524 eap->nextcmd = check_nextcmd(arg);
3525}
3526
Bram Moolenaar8c711452005-01-14 21:53:12 +00003527 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003528do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003529 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003530 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003531 int forceit;
3532{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003533 int ret = OK;
3534 int cc;
3535
3536 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003538 cc = *name_end;
3539 *name_end = NUL;
3540
3541 /* Normal name or expanded name. */
3542 if (check_changedtick(lp->ll_name))
3543 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003544 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003545 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003546 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003547 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003548 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3549 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003550 else if (lp->ll_range)
3551 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003552 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553
3554 /* Delete a range of List items. */
3555 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3556 {
3557 li = lp->ll_li->li_next;
3558 listitem_remove(lp->ll_list, lp->ll_li);
3559 lp->ll_li = li;
3560 ++lp->ll_n1;
3561 }
3562 }
3563 else
3564 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003565 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003566 /* unlet a List item. */
3567 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003568 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003569 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003570 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003571 }
3572
3573 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003574}
3575
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576/*
3577 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003578 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 */
3580 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003581do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003583 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaar33570922005-01-25 22:26:29 +00003585 hashtab_T *ht;
3586 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003587 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003588 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 ht = find_var_ht(name, &varname);
3591 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003593 hi = hash_find(ht, varname);
3594 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003595 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003596 di = HI2DI(hi);
3597 if (var_check_fixed(di->di_flags, name)
3598 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003599 return FAIL;
3600 delete_var(ht, hi);
3601 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003604 if (forceit)
3605 return OK;
3606 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 return FAIL;
3608}
3609
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610/*
3611 * Lock or unlock variable indicated by "lp".
3612 * "deep" is the levels to go (-1 for unlimited);
3613 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3614 */
3615 static int
3616do_lock_var(lp, name_end, deep, lock)
3617 lval_T *lp;
3618 char_u *name_end;
3619 int deep;
3620 int lock;
3621{
3622 int ret = OK;
3623 int cc;
3624 dictitem_T *di;
3625
3626 if (deep == 0) /* nothing to do */
3627 return OK;
3628
3629 if (lp->ll_tv == NULL)
3630 {
3631 cc = *name_end;
3632 *name_end = NUL;
3633
3634 /* Normal name or expanded name. */
3635 if (check_changedtick(lp->ll_name))
3636 ret = FAIL;
3637 else
3638 {
3639 di = find_var(lp->ll_name, NULL);
3640 if (di == NULL)
3641 ret = FAIL;
3642 else
3643 {
3644 if (lock)
3645 di->di_flags |= DI_FLAGS_LOCK;
3646 else
3647 di->di_flags &= ~DI_FLAGS_LOCK;
3648 item_lock(&di->di_tv, deep, lock);
3649 }
3650 }
3651 *name_end = cc;
3652 }
3653 else if (lp->ll_range)
3654 {
3655 listitem_T *li = lp->ll_li;
3656
3657 /* (un)lock a range of List items. */
3658 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3659 {
3660 item_lock(&li->li_tv, deep, lock);
3661 li = li->li_next;
3662 ++lp->ll_n1;
3663 }
3664 }
3665 else if (lp->ll_list != NULL)
3666 /* (un)lock a List item. */
3667 item_lock(&lp->ll_li->li_tv, deep, lock);
3668 else
3669 /* un(lock) a Dictionary item. */
3670 item_lock(&lp->ll_di->di_tv, deep, lock);
3671
3672 return ret;
3673}
3674
3675/*
3676 * Lock or unlock an item. "deep" is nr of levels to go.
3677 */
3678 static void
3679item_lock(tv, deep, lock)
3680 typval_T *tv;
3681 int deep;
3682 int lock;
3683{
3684 static int recurse = 0;
3685 list_T *l;
3686 listitem_T *li;
3687 dict_T *d;
3688 hashitem_T *hi;
3689 int todo;
3690
3691 if (recurse >= DICT_MAXNEST)
3692 {
3693 EMSG(_("E743: variable nested too deep for (un)lock"));
3694 return;
3695 }
3696 if (deep == 0)
3697 return;
3698 ++recurse;
3699
3700 /* lock/unlock the item itself */
3701 if (lock)
3702 tv->v_lock |= VAR_LOCKED;
3703 else
3704 tv->v_lock &= ~VAR_LOCKED;
3705
3706 switch (tv->v_type)
3707 {
3708 case VAR_LIST:
3709 if ((l = tv->vval.v_list) != NULL)
3710 {
3711 if (lock)
3712 l->lv_lock |= VAR_LOCKED;
3713 else
3714 l->lv_lock &= ~VAR_LOCKED;
3715 if (deep < 0 || deep > 1)
3716 /* recursive: lock/unlock the items the List contains */
3717 for (li = l->lv_first; li != NULL; li = li->li_next)
3718 item_lock(&li->li_tv, deep - 1, lock);
3719 }
3720 break;
3721 case VAR_DICT:
3722 if ((d = tv->vval.v_dict) != NULL)
3723 {
3724 if (lock)
3725 d->dv_lock |= VAR_LOCKED;
3726 else
3727 d->dv_lock &= ~VAR_LOCKED;
3728 if (deep < 0 || deep > 1)
3729 {
3730 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003731 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003732 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3733 {
3734 if (!HASHITEM_EMPTY(hi))
3735 {
3736 --todo;
3737 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3738 }
3739 }
3740 }
3741 }
3742 }
3743 --recurse;
3744}
3745
Bram Moolenaara40058a2005-07-11 22:42:07 +00003746/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003747 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3748 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003749 */
3750 static int
3751tv_islocked(tv)
3752 typval_T *tv;
3753{
3754 return (tv->v_lock & VAR_LOCKED)
3755 || (tv->v_type == VAR_LIST
3756 && tv->vval.v_list != NULL
3757 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3758 || (tv->v_type == VAR_DICT
3759 && tv->vval.v_dict != NULL
3760 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3761}
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3764/*
3765 * Delete all "menutrans_" variables.
3766 */
3767 void
3768del_menutrans_vars()
3769{
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003771 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Bram Moolenaar33570922005-01-25 22:26:29 +00003773 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003774 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003775 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003776 {
3777 if (!HASHITEM_EMPTY(hi))
3778 {
3779 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3781 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 }
3783 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003784 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785}
3786#endif
3787
3788#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3789
3790/*
3791 * Local string buffer for the next two functions to store a variable name
3792 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3793 * get_user_var_name().
3794 */
3795
3796static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3797
3798static char_u *varnamebuf = NULL;
3799static int varnamebuflen = 0;
3800
3801/*
3802 * Function to concatenate a prefix and a variable name.
3803 */
3804 static char_u *
3805cat_prefix_varname(prefix, name)
3806 int prefix;
3807 char_u *name;
3808{
3809 int len;
3810
3811 len = (int)STRLEN(name) + 3;
3812 if (len > varnamebuflen)
3813 {
3814 vim_free(varnamebuf);
3815 len += 10; /* some additional space */
3816 varnamebuf = alloc(len);
3817 if (varnamebuf == NULL)
3818 {
3819 varnamebuflen = 0;
3820 return NULL;
3821 }
3822 varnamebuflen = len;
3823 }
3824 *varnamebuf = prefix;
3825 varnamebuf[1] = ':';
3826 STRCPY(varnamebuf + 2, name);
3827 return varnamebuf;
3828}
3829
3830/*
3831 * Function given to ExpandGeneric() to obtain the list of user defined
3832 * (global/buffer/window/built-in) variable names.
3833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 char_u *
3835get_user_var_name(xp, idx)
3836 expand_T *xp;
3837 int idx;
3838{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003839 static long_u gdone;
3840 static long_u bdone;
3841 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003842#ifdef FEAT_WINDOWS
3843 static long_u tdone;
3844#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003845 static int vidx;
3846 static hashitem_T *hi;
3847 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003850 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003852#ifdef FEAT_WINDOWS
3853 tdone = 0;
3854#endif
3855 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003856
3857 /* Global variables */
3858 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003860 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003861 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003862 else
3863 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003864 while (HASHITEM_EMPTY(hi))
3865 ++hi;
3866 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3867 return cat_prefix_varname('g', hi->hi_key);
3868 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003870
3871 /* b: variables */
3872 ht = &curbuf->b_vars.dv_hashtab;
3873 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003875 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003876 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003877 else
3878 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003879 while (HASHITEM_EMPTY(hi))
3880 ++hi;
3881 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003883 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003885 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 return (char_u *)"b:changedtick";
3887 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003888
3889 /* w: variables */
3890 ht = &curwin->w_vars.dv_hashtab;
3891 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003893 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003894 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003895 else
3896 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003897 while (HASHITEM_EMPTY(hi))
3898 ++hi;
3899 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003902#ifdef FEAT_WINDOWS
3903 /* t: variables */
3904 ht = &curtab->tp_vars.dv_hashtab;
3905 if (tdone < ht->ht_used)
3906 {
3907 if (tdone++ == 0)
3908 hi = ht->ht_array;
3909 else
3910 ++hi;
3911 while (HASHITEM_EMPTY(hi))
3912 ++hi;
3913 return cat_prefix_varname('t', hi->hi_key);
3914 }
3915#endif
3916
Bram Moolenaar33570922005-01-25 22:26:29 +00003917 /* v: variables */
3918 if (vidx < VV_LEN)
3919 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 vim_free(varnamebuf);
3922 varnamebuf = NULL;
3923 varnamebuflen = 0;
3924 return NULL;
3925}
3926
3927#endif /* FEAT_CMDL_COMPL */
3928
3929/*
3930 * types for expressions.
3931 */
3932typedef enum
3933{
3934 TYPE_UNKNOWN = 0
3935 , TYPE_EQUAL /* == */
3936 , TYPE_NEQUAL /* != */
3937 , TYPE_GREATER /* > */
3938 , TYPE_GEQUAL /* >= */
3939 , TYPE_SMALLER /* < */
3940 , TYPE_SEQUAL /* <= */
3941 , TYPE_MATCH /* =~ */
3942 , TYPE_NOMATCH /* !~ */
3943} exptype_T;
3944
3945/*
3946 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003947 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3949 */
3950
3951/*
3952 * Handle zero level expression.
3953 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003954 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003955 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 * Return OK or FAIL.
3957 */
3958 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 char_u **nextcmd;
3963 int evaluate;
3964{
3965 int ret;
3966 char_u *p;
3967
3968 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003969 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (ret == FAIL || !ends_excmd(*p))
3971 {
3972 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003973 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 /*
3975 * Report the invalid expression unless the expression evaluation has
3976 * been cancelled due to an aborting error, an interrupt, or an
3977 * exception.
3978 */
3979 if (!aborting())
3980 EMSG2(_(e_invexpr2), arg);
3981 ret = FAIL;
3982 }
3983 if (nextcmd != NULL)
3984 *nextcmd = check_nextcmd(p);
3985
3986 return ret;
3987}
3988
3989/*
3990 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003991 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 *
3993 * "arg" must point to the first non-white of the expression.
3994 * "arg" is advanced to the next non-white after the recognized expression.
3995 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003996 * Note: "rettv.v_lock" is not set.
3997 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 * Return OK or FAIL.
3999 */
4000 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004001eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 int evaluate;
4005{
4006 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004007 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 /*
4010 * Get the first variable.
4011 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004012 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return FAIL;
4014
4015 if ((*arg)[0] == '?')
4016 {
4017 result = FALSE;
4018 if (evaluate)
4019 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004020 int error = FALSE;
4021
4022 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004024 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004025 if (error)
4026 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 }
4028
4029 /*
4030 * Get the second variable.
4031 */
4032 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004033 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 return FAIL;
4035
4036 /*
4037 * Check for the ":".
4038 */
4039 if ((*arg)[0] != ':')
4040 {
4041 EMSG(_("E109: Missing ':' after '?'"));
4042 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004043 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return FAIL;
4045 }
4046
4047 /*
4048 * Get the third variable.
4049 */
4050 *arg = skipwhite(*arg + 1);
4051 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4052 {
4053 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004054 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 return FAIL;
4056 }
4057 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004058 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060
4061 return OK;
4062}
4063
4064/*
4065 * Handle first level expression:
4066 * expr2 || expr2 || expr2 logical OR
4067 *
4068 * "arg" must point to the first non-white of the expression.
4069 * "arg" is advanced to the next non-white after the recognized expression.
4070 *
4071 * Return OK or FAIL.
4072 */
4073 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004074eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 int evaluate;
4078{
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 long result;
4081 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004082 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084 /*
4085 * Get the first variable.
4086 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004087 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 return FAIL;
4089
4090 /*
4091 * Repeat until there is no following "||".
4092 */
4093 first = TRUE;
4094 result = FALSE;
4095 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4096 {
4097 if (evaluate && first)
4098 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004099 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004102 if (error)
4103 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 first = FALSE;
4105 }
4106
4107 /*
4108 * Get the second variable.
4109 */
4110 *arg = skipwhite(*arg + 2);
4111 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4112 return FAIL;
4113
4114 /*
4115 * Compute the result.
4116 */
4117 if (evaluate && !result)
4118 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004119 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004122 if (error)
4123 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 }
4125 if (evaluate)
4126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 rettv->v_type = VAR_NUMBER;
4128 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 }
4130 }
4131
4132 return OK;
4133}
4134
4135/*
4136 * Handle second level expression:
4137 * expr3 && expr3 && expr3 logical AND
4138 *
4139 * "arg" must point to the first non-white of the expression.
4140 * "arg" is advanced to the next non-white after the recognized expression.
4141 *
4142 * Return OK or FAIL.
4143 */
4144 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int evaluate;
4149{
Bram Moolenaar33570922005-01-25 22:26:29 +00004150 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 long result;
4152 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
4155 /*
4156 * Get the first variable.
4157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 return FAIL;
4160
4161 /*
4162 * Repeat until there is no following "&&".
4163 */
4164 first = TRUE;
4165 result = TRUE;
4166 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4167 {
4168 if (evaluate && first)
4169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 if (error)
4174 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 first = FALSE;
4176 }
4177
4178 /*
4179 * Get the second variable.
4180 */
4181 *arg = skipwhite(*arg + 2);
4182 if (eval4(arg, &var2, evaluate && result) == FAIL)
4183 return FAIL;
4184
4185 /*
4186 * Compute the result.
4187 */
4188 if (evaluate && result)
4189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 if (error)
4194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 if (evaluate)
4197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 rettv->v_type = VAR_NUMBER;
4199 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202
4203 return OK;
4204}
4205
4206/*
4207 * Handle third level expression:
4208 * var1 == var2
4209 * var1 =~ var2
4210 * var1 != var2
4211 * var1 !~ var2
4212 * var1 > var2
4213 * var1 >= var2
4214 * var1 < var2
4215 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 * var1 is var2
4217 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 *
4219 * "arg" must point to the first non-white of the expression.
4220 * "arg" is advanced to the next non-white after the recognized expression.
4221 *
4222 * Return OK or FAIL.
4223 */
4224 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004225eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 int evaluate;
4229{
Bram Moolenaar33570922005-01-25 22:26:29 +00004230 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 char_u *p;
4232 int i;
4233 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004234 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 int len = 2;
4236 long n1, n2;
4237 char_u *s1, *s2;
4238 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4239 regmatch_T regmatch;
4240 int ic;
4241 char_u *save_cpo;
4242
4243 /*
4244 * Get the first variable.
4245 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004246 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 return FAIL;
4248
4249 p = *arg;
4250 switch (p[0])
4251 {
4252 case '=': if (p[1] == '=')
4253 type = TYPE_EQUAL;
4254 else if (p[1] == '~')
4255 type = TYPE_MATCH;
4256 break;
4257 case '!': if (p[1] == '=')
4258 type = TYPE_NEQUAL;
4259 else if (p[1] == '~')
4260 type = TYPE_NOMATCH;
4261 break;
4262 case '>': if (p[1] != '=')
4263 {
4264 type = TYPE_GREATER;
4265 len = 1;
4266 }
4267 else
4268 type = TYPE_GEQUAL;
4269 break;
4270 case '<': if (p[1] != '=')
4271 {
4272 type = TYPE_SMALLER;
4273 len = 1;
4274 }
4275 else
4276 type = TYPE_SEQUAL;
4277 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004278 case 'i': if (p[1] == 's')
4279 {
4280 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4281 len = 5;
4282 if (!vim_isIDc(p[len]))
4283 {
4284 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4285 type_is = TRUE;
4286 }
4287 }
4288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290
4291 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004292 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 */
4294 if (type != TYPE_UNKNOWN)
4295 {
4296 /* extra question mark appended: ignore case */
4297 if (p[len] == '?')
4298 {
4299 ic = TRUE;
4300 ++len;
4301 }
4302 /* extra '#' appended: match case */
4303 else if (p[len] == '#')
4304 {
4305 ic = FALSE;
4306 ++len;
4307 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004308 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 else
4310 ic = p_ic;
4311
4312 /*
4313 * Get the second variable.
4314 */
4315 *arg = skipwhite(p + len);
4316 if (eval5(arg, &var2, evaluate) == FAIL)
4317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return FAIL;
4320 }
4321
4322 if (evaluate)
4323 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004324 if (type_is && rettv->v_type != var2.v_type)
4325 {
4326 /* For "is" a different type always means FALSE, for "notis"
4327 * it means TRUE. */
4328 n1 = (type == TYPE_NEQUAL);
4329 }
4330 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4331 {
4332 if (type_is)
4333 {
4334 n1 = (rettv->v_type == var2.v_type
4335 && rettv->vval.v_list == var2.vval.v_list);
4336 if (type == TYPE_NEQUAL)
4337 n1 = !n1;
4338 }
4339 else if (rettv->v_type != var2.v_type
4340 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4341 {
4342 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004343 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004344 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004345 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004346 clear_tv(rettv);
4347 clear_tv(&var2);
4348 return FAIL;
4349 }
4350 else
4351 {
4352 /* Compare two Lists for being equal or unequal. */
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
12138#ifdef FEAT_USR_CMDS
12139 "user-commands", /* was accidentally included in 5.4 */
12140 "user_commands",
12141#endif
12142#ifdef FEAT_VIMINFO
12143 "viminfo",
12144#endif
12145#ifdef FEAT_VERTSPLIT
12146 "vertsplit",
12147#endif
12148#ifdef FEAT_VIRTUALEDIT
12149 "virtualedit",
12150#endif
12151#ifdef FEAT_VISUAL
12152 "visual",
12153#endif
12154#ifdef FEAT_VISUALEXTRA
12155 "visualextra",
12156#endif
12157#ifdef FEAT_VREPLACE
12158 "vreplace",
12159#endif
12160#ifdef FEAT_WILDIGN
12161 "wildignore",
12162#endif
12163#ifdef FEAT_WILDMENU
12164 "wildmenu",
12165#endif
12166#ifdef FEAT_WINDOWS
12167 "windows",
12168#endif
12169#ifdef FEAT_WAK
12170 "winaltkeys",
12171#endif
12172#ifdef FEAT_WRITEBACKUP
12173 "writebackup",
12174#endif
12175#ifdef FEAT_XIM
12176 "xim",
12177#endif
12178#ifdef FEAT_XFONTSET
12179 "xfontset",
12180#endif
12181#ifdef USE_XSMP
12182 "xsmp",
12183#endif
12184#ifdef USE_XSMP_INTERACT
12185 "xsmp_interact",
12186#endif
12187#ifdef FEAT_XCLIPBOARD
12188 "xterm_clipboard",
12189#endif
12190#ifdef FEAT_XTERM_SAVE
12191 "xterm_save",
12192#endif
12193#if defined(UNIX) && defined(FEAT_X11)
12194 "X11",
12195#endif
12196 NULL
12197 };
12198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012199 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 for (i = 0; has_list[i] != NULL; ++i)
12201 if (STRICMP(name, has_list[i]) == 0)
12202 {
12203 n = TRUE;
12204 break;
12205 }
12206
12207 if (n == FALSE)
12208 {
12209 if (STRNICMP(name, "patch", 5) == 0)
12210 n = has_patch(atoi((char *)name + 5));
12211 else if (STRICMP(name, "vim_starting") == 0)
12212 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012213#ifdef FEAT_MBYTE
12214 else if (STRICMP(name, "multi_byte_encoding") == 0)
12215 n = has_mbyte;
12216#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012217#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12218 else if (STRICMP(name, "balloon_multiline") == 0)
12219 n = multiline_balloon_available();
12220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221#ifdef DYNAMIC_TCL
12222 else if (STRICMP(name, "tcl") == 0)
12223 n = tcl_enabled(FALSE);
12224#endif
12225#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12226 else if (STRICMP(name, "iconv") == 0)
12227 n = iconv_enabled(FALSE);
12228#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012229#ifdef DYNAMIC_LUA
12230 else if (STRICMP(name, "lua") == 0)
12231 n = lua_enabled(FALSE);
12232#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012233#ifdef DYNAMIC_MZSCHEME
12234 else if (STRICMP(name, "mzscheme") == 0)
12235 n = mzscheme_enabled(FALSE);
12236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237#ifdef DYNAMIC_RUBY
12238 else if (STRICMP(name, "ruby") == 0)
12239 n = ruby_enabled(FALSE);
12240#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012241#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242#ifdef DYNAMIC_PYTHON
12243 else if (STRICMP(name, "python") == 0)
12244 n = python_enabled(FALSE);
12245#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012246#endif
12247#ifdef FEAT_PYTHON3
12248#ifdef DYNAMIC_PYTHON3
12249 else if (STRICMP(name, "python3") == 0)
12250 n = python3_enabled(FALSE);
12251#endif
12252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012253#ifdef DYNAMIC_PERL
12254 else if (STRICMP(name, "perl") == 0)
12255 n = perl_enabled(FALSE);
12256#endif
12257#ifdef FEAT_GUI
12258 else if (STRICMP(name, "gui_running") == 0)
12259 n = (gui.in_use || gui.starting);
12260# ifdef FEAT_GUI_W32
12261 else if (STRICMP(name, "gui_win32s") == 0)
12262 n = gui_is_win32s();
12263# endif
12264# ifdef FEAT_BROWSE
12265 else if (STRICMP(name, "browse") == 0)
12266 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12267# endif
12268#endif
12269#ifdef FEAT_SYN_HL
12270 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012271 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272#endif
12273#if defined(WIN3264)
12274 else if (STRICMP(name, "win95") == 0)
12275 n = mch_windows95();
12276#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012277#ifdef FEAT_NETBEANS_INTG
12278 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012279 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012281 }
12282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012283 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284}
12285
12286/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012287 * "has_key()" function
12288 */
12289 static void
12290f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012291 typval_T *argvars;
12292 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012293{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012294 if (argvars[0].v_type != VAR_DICT)
12295 {
12296 EMSG(_(e_dictreq));
12297 return;
12298 }
12299 if (argvars[0].vval.v_dict == NULL)
12300 return;
12301
12302 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012303 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012304}
12305
12306/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012307 * "haslocaldir()" function
12308 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012309 static void
12310f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012311 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012312 typval_T *rettv;
12313{
12314 rettv->vval.v_number = (curwin->w_localdir != NULL);
12315}
12316
12317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318 * "hasmapto()" function
12319 */
12320 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012322 typval_T *argvars;
12323 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324{
12325 char_u *name;
12326 char_u *mode;
12327 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012328 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012330 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012331 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 mode = (char_u *)"nvo";
12333 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012336 if (argvars[2].v_type != VAR_UNKNOWN)
12337 abbr = get_tv_number(&argvars[2]);
12338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339
Bram Moolenaar2c932302006-03-18 21:42:09 +000012340 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012341 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012344}
12345
12346/*
12347 * "histadd()" function
12348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012350f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012351 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012353{
12354#ifdef FEAT_CMDHIST
12355 int histype;
12356 char_u *str;
12357 char_u buf[NUMBUFLEN];
12358#endif
12359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012361 if (check_restricted() || check_secure())
12362 return;
12363#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012364 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12365 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366 if (histype >= 0)
12367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012368 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012369 if (*str != NUL)
12370 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012371 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012373 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 return;
12375 }
12376 }
12377#endif
12378}
12379
12380/*
12381 * "histdel()" function
12382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012385 typval_T *argvars UNUSED;
12386 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387{
12388#ifdef FEAT_CMDHIST
12389 int n;
12390 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012391 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012393 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12394 if (str == NULL)
12395 n = 0;
12396 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012398 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012399 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012402 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 else
12404 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012405 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 get_tv_string_buf(&argvars[1], buf));
12407 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408#endif
12409}
12410
12411/*
12412 * "histget()" function
12413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012416 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012417 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418{
12419#ifdef FEAT_CMDHIST
12420 int type;
12421 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012422 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012423
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012424 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12425 if (str == NULL)
12426 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012427 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012428 {
12429 type = get_histtype(str);
12430 if (argvars[1].v_type == VAR_UNKNOWN)
12431 idx = get_history_idx(type);
12432 else
12433 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12434 /* -1 on type error */
12435 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012438 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012440 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441}
12442
12443/*
12444 * "histnr()" function
12445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012447f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012448 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450{
12451 int i;
12452
12453#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012454 char_u *history = get_tv_string_chk(&argvars[0]);
12455
12456 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 if (i >= HIST_CMD && i < HIST_COUNT)
12458 i = get_history_idx(i);
12459 else
12460#endif
12461 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012462 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012463}
12464
12465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012466 * "highlightID(name)" function
12467 */
12468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012469f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012470 typval_T *argvars;
12471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012473 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474}
12475
12476/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012477 * "highlight_exists()" function
12478 */
12479 static void
12480f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012481 typval_T *argvars;
12482 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012483{
12484 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12485}
12486
12487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 * "hostname()" function
12489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012491f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012492 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494{
12495 char_u hostname[256];
12496
12497 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012498 rettv->v_type = VAR_STRING;
12499 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500}
12501
12502/*
12503 * iconv() function
12504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012506f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012507 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012508 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509{
12510#ifdef FEAT_MBYTE
12511 char_u buf1[NUMBUFLEN];
12512 char_u buf2[NUMBUFLEN];
12513 char_u *from, *to, *str;
12514 vimconv_T vimconv;
12515#endif
12516
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517 rettv->v_type = VAR_STRING;
12518 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519
12520#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521 str = get_tv_string(&argvars[0]);
12522 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12523 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 vimconv.vc_type = CONV_NONE;
12525 convert_setup(&vimconv, from, to);
12526
12527 /* If the encodings are equal, no conversion needed. */
12528 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012529 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532
12533 convert_setup(&vimconv, NULL, NULL);
12534 vim_free(from);
12535 vim_free(to);
12536#endif
12537}
12538
12539/*
12540 * "indent()" function
12541 */
12542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012543f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012544 typval_T *argvars;
12545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012546{
12547 linenr_T lnum;
12548
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012549 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012550 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012551 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554}
12555
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012556/*
12557 * "index()" function
12558 */
12559 static void
12560f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012561 typval_T *argvars;
12562 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012563{
Bram Moolenaar33570922005-01-25 22:26:29 +000012564 list_T *l;
12565 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012566 long idx = 0;
12567 int ic = FALSE;
12568
12569 rettv->vval.v_number = -1;
12570 if (argvars[0].v_type != VAR_LIST)
12571 {
12572 EMSG(_(e_listreq));
12573 return;
12574 }
12575 l = argvars[0].vval.v_list;
12576 if (l != NULL)
12577 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012578 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012579 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012580 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012581 int error = FALSE;
12582
Bram Moolenaar758711c2005-02-02 23:11:38 +000012583 /* Start at specified item. Use the cached index that list_find()
12584 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012585 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012586 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012587 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012588 ic = get_tv_number_chk(&argvars[3], &error);
12589 if (error)
12590 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012591 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012592
Bram Moolenaar758711c2005-02-02 23:11:38 +000012593 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012594 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012595 {
12596 rettv->vval.v_number = idx;
12597 break;
12598 }
12599 }
12600}
12601
Bram Moolenaar071d4272004-06-13 20:20:40 +000012602static int inputsecret_flag = 0;
12603
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012604static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12605
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012607 * This function is used by f_input() and f_inputdialog() functions. The third
12608 * argument to f_input() specifies the type of completion to use at the
12609 * prompt. The third argument to f_inputdialog() specifies the value to return
12610 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012611 */
12612 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012613get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012614 typval_T *argvars;
12615 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012616 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012618 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012619 char_u *p = NULL;
12620 int c;
12621 char_u buf[NUMBUFLEN];
12622 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012623 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012624 int xp_type = EXPAND_NOTHING;
12625 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012627 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012628 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629
12630#ifdef NO_CONSOLE_INPUT
12631 /* While starting up, there is no place to enter text. */
12632 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634#endif
12635
12636 cmd_silent = FALSE; /* Want to see the prompt. */
12637 if (prompt != NULL)
12638 {
12639 /* Only the part of the message after the last NL is considered as
12640 * prompt for the command line */
12641 p = vim_strrchr(prompt, '\n');
12642 if (p == NULL)
12643 p = prompt;
12644 else
12645 {
12646 ++p;
12647 c = *p;
12648 *p = NUL;
12649 msg_start();
12650 msg_clr_eos();
12651 msg_puts_attr(prompt, echo_attr);
12652 msg_didout = FALSE;
12653 msg_starthere();
12654 *p = c;
12655 }
12656 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012658 if (argvars[1].v_type != VAR_UNKNOWN)
12659 {
12660 defstr = get_tv_string_buf_chk(&argvars[1], buf);
12661 if (defstr != NULL)
12662 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012664 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000012665 {
12666 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000012667 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000012668 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012669
Bram Moolenaar4463f292005-09-25 22:20:24 +000012670 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012671
Bram Moolenaar4463f292005-09-25 22:20:24 +000012672 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
12673 if (xp_name == NULL)
12674 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012675
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012676 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012677
Bram Moolenaar4463f292005-09-25 22:20:24 +000012678 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
12679 &xp_arg) == FAIL)
12680 return;
12681 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012682 }
12683
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012684 if (defstr != NULL)
12685 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012686 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
12687 xp_type, xp_arg);
12688
12689 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012691 /* since the user typed this, no need to wait for return */
12692 need_wait_return = FALSE;
12693 msg_didout = FALSE;
12694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 cmd_silent = cmd_silent_save;
12696}
12697
12698/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012699 * "input()" function
12700 * Also handles inputsecret() when inputsecret is set.
12701 */
12702 static void
12703f_input(argvars, rettv)
12704 typval_T *argvars;
12705 typval_T *rettv;
12706{
12707 get_user_input(argvars, rettv, FALSE);
12708}
12709
12710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 * "inputdialog()" function
12712 */
12713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012715 typval_T *argvars;
12716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717{
12718#if defined(FEAT_GUI_TEXTDIALOG)
12719 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12720 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
12721 {
12722 char_u *message;
12723 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012724 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012726 message = get_tv_string_chk(&argvars[0]);
12727 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000012728 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000012729 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 else
12731 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012732 if (message != NULL && defstr != NULL
12733 && do_dialog(VIM_QUESTION, NULL, message,
12734 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 else
12737 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012738 if (message != NULL && defstr != NULL
12739 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012740 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012741 rettv->vval.v_string = vim_strsave(
12742 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 }
12748 else
12749#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012750 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751}
12752
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012753/*
12754 * "inputlist()" function
12755 */
12756 static void
12757f_inputlist(argvars, rettv)
12758 typval_T *argvars;
12759 typval_T *rettv;
12760{
12761 listitem_T *li;
12762 int selected;
12763 int mouse_used;
12764
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012765#ifdef NO_CONSOLE_INPUT
12766 /* While starting up, there is no place to enter text. */
12767 if (no_console_input())
12768 return;
12769#endif
12770 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
12771 {
12772 EMSG2(_(e_listarg), "inputlist()");
12773 return;
12774 }
12775
12776 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000012777 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000012778 lines_left = Rows; /* avoid more prompt */
12779 msg_scroll = TRUE;
12780 msg_clr_eos();
12781
12782 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
12783 {
12784 msg_puts(get_tv_string(&li->li_tv));
12785 msg_putchar('\n');
12786 }
12787
12788 /* Ask for choice. */
12789 selected = prompt_for_number(&mouse_used);
12790 if (mouse_used)
12791 selected -= lines_left;
12792
12793 rettv->vval.v_number = selected;
12794}
12795
12796
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
12798
12799/*
12800 * "inputrestore()" function
12801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012804 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012806{
12807 if (ga_userinput.ga_len > 0)
12808 {
12809 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
12811 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012812 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 }
12814 else if (p_verbose > 1)
12815 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000012816 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 }
12819}
12820
12821/*
12822 * "inputsave()" function
12823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012826 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012829 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 if (ga_grow(&ga_userinput, 1) == OK)
12831 {
12832 save_typeahead((tasave_T *)(ga_userinput.ga_data)
12833 + ga_userinput.ga_len);
12834 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012835 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 }
12837 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012838 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839}
12840
12841/*
12842 * "inputsecret()" function
12843 */
12844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *argvars;
12847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848{
12849 ++cmdline_star;
12850 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 --cmdline_star;
12853 --inputsecret_flag;
12854}
12855
12856/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012857 * "insert()" function
12858 */
12859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012861 typval_T *argvars;
12862 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012863{
12864 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000012865 listitem_T *item;
12866 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012867 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012868
12869 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012870 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012871 else if ((l = argvars[0].vval.v_list) != NULL
12872 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012873 {
12874 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012875 before = get_tv_number_chk(&argvars[2], &error);
12876 if (error)
12877 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012878
Bram Moolenaar758711c2005-02-02 23:11:38 +000012879 if (before == l->lv_len)
12880 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012881 else
12882 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012883 item = list_find(l, before);
12884 if (item == NULL)
12885 {
12886 EMSGN(_(e_listidx), before);
12887 l = NULL;
12888 }
12889 }
12890 if (l != NULL)
12891 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012892 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012893 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012894 }
12895 }
12896}
12897
12898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 * "isdirectory()" function
12900 */
12901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012903 typval_T *argvars;
12904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907}
12908
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012909/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012910 * "islocked()" function
12911 */
12912 static void
12913f_islocked(argvars, rettv)
12914 typval_T *argvars;
12915 typval_T *rettv;
12916{
12917 lval_T lv;
12918 char_u *end;
12919 dictitem_T *di;
12920
12921 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000012922 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
12923 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012924 if (end != NULL && lv.ll_name != NULL)
12925 {
12926 if (*end != NUL)
12927 EMSG(_(e_trailing));
12928 else
12929 {
12930 if (lv.ll_tv == NULL)
12931 {
12932 if (check_changedtick(lv.ll_name))
12933 rettv->vval.v_number = 1; /* always locked */
12934 else
12935 {
12936 di = find_var(lv.ll_name, NULL);
12937 if (di != NULL)
12938 {
12939 /* Consider a variable locked when:
12940 * 1. the variable itself is locked
12941 * 2. the value of the variable is locked.
12942 * 3. the List or Dict value is locked.
12943 */
12944 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
12945 || tv_islocked(&di->di_tv));
12946 }
12947 }
12948 }
12949 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000012950 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012951 else if (lv.ll_newkey != NULL)
12952 EMSG2(_(e_dictkey), lv.ll_newkey);
12953 else if (lv.ll_list != NULL)
12954 /* List item. */
12955 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
12956 else
12957 /* Dictionary item. */
12958 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
12959 }
12960 }
12961
12962 clear_lval(&lv);
12963}
12964
Bram Moolenaar33570922005-01-25 22:26:29 +000012965static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012966
12967/*
12968 * Turn a dict into a list:
12969 * "what" == 0: list of keys
12970 * "what" == 1: list of values
12971 * "what" == 2: list of items
12972 */
12973 static void
12974dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000012975 typval_T *argvars;
12976 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012977 int what;
12978{
Bram Moolenaar33570922005-01-25 22:26:29 +000012979 list_T *l2;
12980 dictitem_T *di;
12981 hashitem_T *hi;
12982 listitem_T *li;
12983 listitem_T *li2;
12984 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012985 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012986
Bram Moolenaar8c711452005-01-14 21:53:12 +000012987 if (argvars[0].v_type != VAR_DICT)
12988 {
12989 EMSG(_(e_dictreq));
12990 return;
12991 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012992 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012993 return;
12994
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012995 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000012996 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012997
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000012998 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000012999 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013000 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013001 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013002 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013003 --todo;
13004 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013005
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013006 li = listitem_alloc();
13007 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013008 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013009 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013010
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013011 if (what == 0)
13012 {
13013 /* keys() */
13014 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013015 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013016 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13017 }
13018 else if (what == 1)
13019 {
13020 /* values() */
13021 copy_tv(&di->di_tv, &li->li_tv);
13022 }
13023 else
13024 {
13025 /* items() */
13026 l2 = list_alloc();
13027 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013028 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013029 li->li_tv.vval.v_list = l2;
13030 if (l2 == NULL)
13031 break;
13032 ++l2->lv_refcount;
13033
13034 li2 = listitem_alloc();
13035 if (li2 == NULL)
13036 break;
13037 list_append(l2, li2);
13038 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013039 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013040 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13041
13042 li2 = listitem_alloc();
13043 if (li2 == NULL)
13044 break;
13045 list_append(l2, li2);
13046 copy_tv(&di->di_tv, &li2->li_tv);
13047 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013048 }
13049 }
13050}
13051
13052/*
13053 * "items(dict)" function
13054 */
13055 static void
13056f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013057 typval_T *argvars;
13058 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013059{
13060 dict_list(argvars, rettv, 2);
13061}
13062
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013064 * "join()" function
13065 */
13066 static void
13067f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013068 typval_T *argvars;
13069 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013070{
13071 garray_T ga;
13072 char_u *sep;
13073
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013074 if (argvars[0].v_type != VAR_LIST)
13075 {
13076 EMSG(_(e_listreq));
13077 return;
13078 }
13079 if (argvars[0].vval.v_list == NULL)
13080 return;
13081 if (argvars[1].v_type == VAR_UNKNOWN)
13082 sep = (char_u *)" ";
13083 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013085
13086 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013087
13088 if (sep != NULL)
13089 {
13090 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013091 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013092 ga_append(&ga, NUL);
13093 rettv->vval.v_string = (char_u *)ga.ga_data;
13094 }
13095 else
13096 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013097}
13098
13099/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013100 * "keys()" function
13101 */
13102 static void
13103f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013104 typval_T *argvars;
13105 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013106{
13107 dict_list(argvars, rettv, 0);
13108}
13109
13110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111 * "last_buffer_nr()" function.
13112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013114f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013115 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117{
13118 int n = 0;
13119 buf_T *buf;
13120
13121 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13122 if (n < buf->b_fnum)
13123 n = buf->b_fnum;
13124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013126}
13127
13128/*
13129 * "len()" function
13130 */
13131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013132f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013133 typval_T *argvars;
13134 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013135{
13136 switch (argvars[0].v_type)
13137 {
13138 case VAR_STRING:
13139 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140 rettv->vval.v_number = (varnumber_T)STRLEN(
13141 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013142 break;
13143 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013144 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013145 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013146 case VAR_DICT:
13147 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13148 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013149 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013150 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013151 break;
13152 }
13153}
13154
Bram Moolenaar33570922005-01-25 22:26:29 +000013155static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013156
13157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013159 typval_T *argvars;
13160 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013161 int type;
13162{
13163#ifdef FEAT_LIBCALL
13164 char_u *string_in;
13165 char_u **string_result;
13166 int nr_result;
13167#endif
13168
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013169 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013170 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013172
13173 if (check_restricted() || check_secure())
13174 return;
13175
13176#ifdef FEAT_LIBCALL
13177 /* The first two args must be strings, otherwise its meaningless */
13178 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13179 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013180 string_in = NULL;
13181 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013182 string_in = argvars[2].vval.v_string;
13183 if (type == VAR_NUMBER)
13184 string_result = NULL;
13185 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013186 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013187 if (mch_libcall(argvars[0].vval.v_string,
13188 argvars[1].vval.v_string,
13189 string_in,
13190 argvars[2].vval.v_number,
13191 string_result,
13192 &nr_result) == OK
13193 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013195 }
13196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013197}
13198
13199/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013200 * "libcall()" function
13201 */
13202 static void
13203f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013204 typval_T *argvars;
13205 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013206{
13207 libcall_common(argvars, rettv, VAR_STRING);
13208}
13209
13210/*
13211 * "libcallnr()" function
13212 */
13213 static void
13214f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013215 typval_T *argvars;
13216 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013217{
13218 libcall_common(argvars, rettv, VAR_NUMBER);
13219}
13220
13221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 * "line(string)" function
13223 */
13224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013225f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013226 typval_T *argvars;
13227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228{
13229 linenr_T lnum = 0;
13230 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013231 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013233 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013234 if (fp != NULL)
13235 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237}
13238
13239/*
13240 * "line2byte(lnum)" function
13241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013243f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013244 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246{
13247#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013248 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249#else
13250 linenr_T lnum;
13251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013252 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013254 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13257 if (rettv->vval.v_number >= 0)
13258 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259#endif
13260}
13261
13262/*
13263 * "lispindent(lnum)" function
13264 */
13265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013266f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013267 typval_T *argvars;
13268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269{
13270#ifdef FEAT_LISP
13271 pos_T pos;
13272 linenr_T lnum;
13273
13274 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013275 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13277 {
13278 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013279 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 curwin->w_cursor = pos;
13281 }
13282 else
13283#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013284 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285}
13286
13287/*
13288 * "localtime()" function
13289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013291f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013292 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296}
13297
Bram Moolenaar33570922005-01-25 22:26:29 +000013298static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299
13300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013301get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013302 typval_T *argvars;
13303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 int exact;
13305{
13306 char_u *keys;
13307 char_u *which;
13308 char_u buf[NUMBUFLEN];
13309 char_u *keys_buf = NULL;
13310 char_u *rhs;
13311 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013312 int abbr = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013313 int get_dict = FALSE;
13314 mapblock_T *mp;
13315 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316
13317 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 rettv->v_type = VAR_STRING;
13319 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013321 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 if (*keys == NUL)
13323 return;
13324
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013325 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013327 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013328 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013329 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013330 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013331 if (argvars[3].v_type != VAR_UNKNOWN)
13332 get_dict = get_tv_number(&argvars[3]);
13333 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335 else
13336 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013337 if (which == NULL)
13338 return;
13339
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340 mode = get_map_mode(&which, 0);
13341
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013342 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013343 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013344 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013345
13346 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013348 /* Return a string. */
13349 if (rhs != NULL)
13350 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351
Bram Moolenaarbd743252010-10-20 21:23:33 +020013352 }
13353 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13354 {
13355 /* Return a dictionary. */
13356 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13357 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13358 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359
Bram Moolenaarbd743252010-10-20 21:23:33 +020013360 dict_add_nr_str(dict, "lhs", 0L, lhs);
13361 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13362 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13363 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13364 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13365 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13366 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13367 dict_add_nr_str(dict, "mode", 0L, mapmode);
13368
13369 vim_free(lhs);
13370 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 }
13372}
13373
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013374#ifdef FEAT_FLOAT
13375/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013376 * "log()" function
13377 */
13378 static void
13379f_log(argvars, rettv)
13380 typval_T *argvars;
13381 typval_T *rettv;
13382{
13383 float_T f;
13384
13385 rettv->v_type = VAR_FLOAT;
13386 if (get_float_arg(argvars, &f) == OK)
13387 rettv->vval.v_float = log(f);
13388 else
13389 rettv->vval.v_float = 0.0;
13390}
13391
13392/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013393 * "log10()" function
13394 */
13395 static void
13396f_log10(argvars, rettv)
13397 typval_T *argvars;
13398 typval_T *rettv;
13399{
13400 float_T f;
13401
13402 rettv->v_type = VAR_FLOAT;
13403 if (get_float_arg(argvars, &f) == OK)
13404 rettv->vval.v_float = log10(f);
13405 else
13406 rettv->vval.v_float = 0.0;
13407}
13408#endif
13409
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013411 * "map()" function
13412 */
13413 static void
13414f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013415 typval_T *argvars;
13416 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013417{
13418 filter_map(argvars, rettv, TRUE);
13419}
13420
13421/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013422 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423 */
13424 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013425f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013426 typval_T *argvars;
13427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013429 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430}
13431
13432/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013433 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434 */
13435 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013436f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013437 typval_T *argvars;
13438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013440 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441}
13442
Bram Moolenaar33570922005-01-25 22:26:29 +000013443static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444
13445 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013446find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013447 typval_T *argvars;
13448 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449 int type;
13450{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013451 char_u *str = NULL;
13452 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013453 char_u *pat;
13454 regmatch_T regmatch;
13455 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013456 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457 char_u *save_cpo;
13458 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013459 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013460 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013461 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013462 list_T *l = NULL;
13463 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013465 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466
13467 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13468 save_cpo = p_cpo;
13469 p_cpo = (char_u *)"";
13470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013471 rettv->vval.v_number = -1;
13472 if (type == 3)
13473 {
13474 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013475 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013476 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013477 }
13478 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013480 rettv->v_type = VAR_STRING;
13481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013483
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013484 if (argvars[0].v_type == VAR_LIST)
13485 {
13486 if ((l = argvars[0].vval.v_list) == NULL)
13487 goto theend;
13488 li = l->lv_first;
13489 }
13490 else
13491 expr = str = get_tv_string(&argvars[0]);
13492
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013493 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13494 if (pat == NULL)
13495 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013496
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013497 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013499 int error = FALSE;
13500
13501 start = get_tv_number_chk(&argvars[2], &error);
13502 if (error)
13503 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013504 if (l != NULL)
13505 {
13506 li = list_find(l, start);
13507 if (li == NULL)
13508 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013509 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013510 }
13511 else
13512 {
13513 if (start < 0)
13514 start = 0;
13515 if (start > (long)STRLEN(str))
13516 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013517 /* When "count" argument is there ignore matches before "start",
13518 * otherwise skip part of the string. Differs when pattern is "^"
13519 * or "\<". */
13520 if (argvars[3].v_type != VAR_UNKNOWN)
13521 startcol = start;
13522 else
13523 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013524 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013525
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013526 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013527 nth = get_tv_number_chk(&argvars[3], &error);
13528 if (error)
13529 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 }
13531
13532 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13533 if (regmatch.regprog != NULL)
13534 {
13535 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013536
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013537 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013538 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013539 if (l != NULL)
13540 {
13541 if (li == NULL)
13542 {
13543 match = FALSE;
13544 break;
13545 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013546 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013547 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013548 if (str == NULL)
13549 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013550 }
13551
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013552 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013553
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013554 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013555 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013556 if (l == NULL && !match)
13557 break;
13558
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013559 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013560 if (l != NULL)
13561 {
13562 li = li->li_next;
13563 ++idx;
13564 }
13565 else
13566 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013567#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013568 startcol = (colnr_T)(regmatch.startp[0]
13569 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013570#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013571 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013572#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013573 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013574 }
13575
13576 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013577 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013578 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013579 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013580 int i;
13581
13582 /* return list with matched string and submatches */
13583 for (i = 0; i < NSUBEXP; ++i)
13584 {
13585 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013586 {
13587 if (list_append_string(rettv->vval.v_list,
13588 (char_u *)"", 0) == FAIL)
13589 break;
13590 }
13591 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013592 regmatch.startp[i],
13593 (int)(regmatch.endp[i] - regmatch.startp[i]))
13594 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013595 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013596 }
13597 }
13598 else if (type == 2)
13599 {
13600 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013601 if (l != NULL)
13602 copy_tv(&li->li_tv, rettv);
13603 else
13604 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013606 }
13607 else if (l != NULL)
13608 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 else
13610 {
13611 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 (varnumber_T)(regmatch.startp[0] - str);
13614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013617 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 }
13619 }
13620 vim_free(regmatch.regprog);
13621 }
13622
13623theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013624 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625 p_cpo = save_cpo;
13626}
13627
13628/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013629 * "match()" function
13630 */
13631 static void
13632f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013633 typval_T *argvars;
13634 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013635{
13636 find_some_match(argvars, rettv, 1);
13637}
13638
13639/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013640 * "matchadd()" function
13641 */
13642 static void
13643f_matchadd(argvars, rettv)
13644 typval_T *argvars;
13645 typval_T *rettv;
13646{
13647#ifdef FEAT_SEARCH_EXTRA
13648 char_u buf[NUMBUFLEN];
13649 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
13650 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
13651 int prio = 10; /* default priority */
13652 int id = -1;
13653 int error = FALSE;
13654
13655 rettv->vval.v_number = -1;
13656
13657 if (grp == NULL || pat == NULL)
13658 return;
13659 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013660 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013661 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000013662 if (argvars[3].v_type != VAR_UNKNOWN)
13663 id = get_tv_number_chk(&argvars[3], &error);
13664 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013665 if (error == TRUE)
13666 return;
13667 if (id >= 1 && id <= 3)
13668 {
13669 EMSGN("E798: ID is reserved for \":match\": %ld", id);
13670 return;
13671 }
13672
13673 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
13674#endif
13675}
13676
13677/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013678 * "matcharg()" function
13679 */
13680 static void
13681f_matcharg(argvars, rettv)
13682 typval_T *argvars;
13683 typval_T *rettv;
13684{
13685 if (rettv_list_alloc(rettv) == OK)
13686 {
13687#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013688 int id = get_tv_number(&argvars[0]);
13689 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013690
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013691 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013692 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013693 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
13694 {
13695 list_append_string(rettv->vval.v_list,
13696 syn_id2name(m->hlg_id), -1);
13697 list_append_string(rettv->vval.v_list, m->pattern, -1);
13698 }
13699 else
13700 {
13701 list_append_string(rettv->vval.v_list, NUL, -1);
13702 list_append_string(rettv->vval.v_list, NUL, -1);
13703 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013704 }
13705#endif
13706 }
13707}
13708
13709/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013710 * "matchdelete()" function
13711 */
13712 static void
13713f_matchdelete(argvars, rettv)
13714 typval_T *argvars;
13715 typval_T *rettv;
13716{
13717#ifdef FEAT_SEARCH_EXTRA
13718 rettv->vval.v_number = match_delete(curwin,
13719 (int)get_tv_number(&argvars[0]), TRUE);
13720#endif
13721}
13722
13723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013724 * "matchend()" function
13725 */
13726 static void
13727f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013728 typval_T *argvars;
13729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013730{
13731 find_some_match(argvars, rettv, 0);
13732}
13733
13734/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013735 * "matchlist()" function
13736 */
13737 static void
13738f_matchlist(argvars, rettv)
13739 typval_T *argvars;
13740 typval_T *rettv;
13741{
13742 find_some_match(argvars, rettv, 3);
13743}
13744
13745/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013746 * "matchstr()" function
13747 */
13748 static void
13749f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013750 typval_T *argvars;
13751 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013752{
13753 find_some_match(argvars, rettv, 2);
13754}
13755
Bram Moolenaar33570922005-01-25 22:26:29 +000013756static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013757
13758 static void
13759max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000013760 typval_T *argvars;
13761 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013762 int domax;
13763{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013764 long n = 0;
13765 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013766 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013767
13768 if (argvars[0].v_type == VAR_LIST)
13769 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013770 list_T *l;
13771 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013772
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013773 l = argvars[0].vval.v_list;
13774 if (l != NULL)
13775 {
13776 li = l->lv_first;
13777 if (li != NULL)
13778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013779 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013780 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013781 {
13782 li = li->li_next;
13783 if (li == NULL)
13784 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013785 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013786 if (domax ? i > n : i < n)
13787 n = i;
13788 }
13789 }
13790 }
13791 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000013792 else if (argvars[0].v_type == VAR_DICT)
13793 {
Bram Moolenaar33570922005-01-25 22:26:29 +000013794 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013795 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000013796 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013797 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013798
13799 d = argvars[0].vval.v_dict;
13800 if (d != NULL)
13801 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013802 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013803 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013804 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013805 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000013806 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013807 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013808 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013809 if (first)
13810 {
13811 n = i;
13812 first = FALSE;
13813 }
13814 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013815 n = i;
13816 }
13817 }
13818 }
13819 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013820 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000013821 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013822 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013823}
13824
13825/*
13826 * "max()" function
13827 */
13828 static void
13829f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013830 typval_T *argvars;
13831 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013832{
13833 max_min(argvars, rettv, TRUE);
13834}
13835
13836/*
13837 * "min()" function
13838 */
13839 static void
13840f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013841 typval_T *argvars;
13842 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000013843{
13844 max_min(argvars, rettv, FALSE);
13845}
13846
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013847static int mkdir_recurse __ARGS((char_u *dir, int prot));
13848
13849/*
13850 * Create the directory in which "dir" is located, and higher levels when
13851 * needed.
13852 */
13853 static int
13854mkdir_recurse(dir, prot)
13855 char_u *dir;
13856 int prot;
13857{
13858 char_u *p;
13859 char_u *updir;
13860 int r = FAIL;
13861
13862 /* Get end of directory name in "dir".
13863 * We're done when it's "/" or "c:/". */
13864 p = gettail_sep(dir);
13865 if (p <= get_past_head(dir))
13866 return OK;
13867
13868 /* If the directory exists we're done. Otherwise: create it.*/
13869 updir = vim_strnsave(dir, (int)(p - dir));
13870 if (updir == NULL)
13871 return FAIL;
13872 if (mch_isdir(updir))
13873 r = OK;
13874 else if (mkdir_recurse(updir, prot) == OK)
13875 r = vim_mkdir_emsg(updir, prot);
13876 vim_free(updir);
13877 return r;
13878}
13879
13880#ifdef vim_mkdir
13881/*
13882 * "mkdir()" function
13883 */
13884 static void
13885f_mkdir(argvars, rettv)
13886 typval_T *argvars;
13887 typval_T *rettv;
13888{
13889 char_u *dir;
13890 char_u buf[NUMBUFLEN];
13891 int prot = 0755;
13892
13893 rettv->vval.v_number = FAIL;
13894 if (check_restricted() || check_secure())
13895 return;
13896
13897 dir = get_tv_string_buf(&argvars[0], buf);
13898 if (argvars[1].v_type != VAR_UNKNOWN)
13899 {
13900 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013901 prot = get_tv_number_chk(&argvars[2], NULL);
13902 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013903 mkdir_recurse(dir, prot);
13904 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013905 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013906}
13907#endif
13908
Bram Moolenaar0d660222005-01-07 21:51:51 +000013909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 * "mode()" function
13911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013913f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013914 typval_T *argvars;
13915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013917 char_u buf[3];
13918
13919 buf[1] = NUL;
13920 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921
13922#ifdef FEAT_VISUAL
13923 if (VIsual_active)
13924 {
13925 if (VIsual_select)
13926 buf[0] = VIsual_mode + 's' - 'v';
13927 else
13928 buf[0] = VIsual_mode;
13929 }
13930 else
13931#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013932 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
13933 || State == CONFIRM)
13934 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013936 if (State == ASKMORE)
13937 buf[1] = 'm';
13938 else if (State == CONFIRM)
13939 buf[1] = '?';
13940 }
13941 else if (State == EXTERNCMD)
13942 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 else if (State & INSERT)
13944 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013945#ifdef FEAT_VREPLACE
13946 if (State & VREPLACE_FLAG)
13947 {
13948 buf[0] = 'R';
13949 buf[1] = 'v';
13950 }
13951 else
13952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 if (State & REPLACE_FLAG)
13954 buf[0] = 'R';
13955 else
13956 buf[0] = 'i';
13957 }
13958 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013959 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013961 if (exmode_active)
13962 buf[1] = 'v';
13963 }
13964 else if (exmode_active)
13965 {
13966 buf[0] = 'c';
13967 buf[1] = 'e';
13968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013972 if (finish_op)
13973 buf[1] = 'o';
13974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975
Bram Moolenaar05bb9532008-07-04 09:44:11 +000013976 /* Clear out the minor mode when the argument is not a non-zero number or
13977 * non-empty string. */
13978 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013979 buf[1] = NUL;
13980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981 rettv->vval.v_string = vim_strsave(buf);
13982 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983}
13984
Bram Moolenaar7e506b62010-01-19 15:55:06 +010013985#ifdef FEAT_MZSCHEME
13986/*
13987 * "mzeval()" function
13988 */
13989 static void
13990f_mzeval(argvars, rettv)
13991 typval_T *argvars;
13992 typval_T *rettv;
13993{
13994 char_u *str;
13995 char_u buf[NUMBUFLEN];
13996
13997 str = get_tv_string_buf(&argvars[0], buf);
13998 do_mzeval(str, rettv);
13999}
14000#endif
14001
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014003 * "nextnonblank()" function
14004 */
14005 static void
14006f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014007 typval_T *argvars;
14008 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014009{
14010 linenr_T lnum;
14011
14012 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014014 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014015 {
14016 lnum = 0;
14017 break;
14018 }
14019 if (*skipwhite(ml_get(lnum)) != NUL)
14020 break;
14021 }
14022 rettv->vval.v_number = lnum;
14023}
14024
14025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 * "nr2char()" function
14027 */
14028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014029f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014030 typval_T *argvars;
14031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032{
14033 char_u buf[NUMBUFLEN];
14034
14035#ifdef FEAT_MBYTE
14036 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014037 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 else
14039#endif
14040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014042 buf[1] = NUL;
14043 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014044 rettv->v_type = VAR_STRING;
14045 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014046}
14047
14048/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014049 * "pathshorten()" function
14050 */
14051 static void
14052f_pathshorten(argvars, rettv)
14053 typval_T *argvars;
14054 typval_T *rettv;
14055{
14056 char_u *p;
14057
14058 rettv->v_type = VAR_STRING;
14059 p = get_tv_string_chk(&argvars[0]);
14060 if (p == NULL)
14061 rettv->vval.v_string = NULL;
14062 else
14063 {
14064 p = vim_strsave(p);
14065 rettv->vval.v_string = p;
14066 if (p != NULL)
14067 shorten_dir(p);
14068 }
14069}
14070
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014071#ifdef FEAT_FLOAT
14072/*
14073 * "pow()" function
14074 */
14075 static void
14076f_pow(argvars, rettv)
14077 typval_T *argvars;
14078 typval_T *rettv;
14079{
14080 float_T fx, fy;
14081
14082 rettv->v_type = VAR_FLOAT;
14083 if (get_float_arg(argvars, &fx) == OK
14084 && get_float_arg(&argvars[1], &fy) == OK)
14085 rettv->vval.v_float = pow(fx, fy);
14086 else
14087 rettv->vval.v_float = 0.0;
14088}
14089#endif
14090
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014091/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014092 * "prevnonblank()" function
14093 */
14094 static void
14095f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 typval_T *argvars;
14097 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014098{
14099 linenr_T lnum;
14100
14101 lnum = get_tv_lnum(argvars);
14102 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14103 lnum = 0;
14104 else
14105 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14106 --lnum;
14107 rettv->vval.v_number = lnum;
14108}
14109
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014110#ifdef HAVE_STDARG_H
14111/* This dummy va_list is here because:
14112 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14113 * - locally in the function results in a "used before set" warning
14114 * - using va_start() to initialize it gives "function with fixed args" error */
14115static va_list ap;
14116#endif
14117
Bram Moolenaar8c711452005-01-14 21:53:12 +000014118/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014119 * "printf()" function
14120 */
14121 static void
14122f_printf(argvars, rettv)
14123 typval_T *argvars;
14124 typval_T *rettv;
14125{
14126 rettv->v_type = VAR_STRING;
14127 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014128#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014129 {
14130 char_u buf[NUMBUFLEN];
14131 int len;
14132 char_u *s;
14133 int saved_did_emsg = did_emsg;
14134 char *fmt;
14135
14136 /* Get the required length, allocate the buffer and do it for real. */
14137 did_emsg = FALSE;
14138 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014139 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014140 if (!did_emsg)
14141 {
14142 s = alloc(len + 1);
14143 if (s != NULL)
14144 {
14145 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014146 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014147 }
14148 }
14149 did_emsg |= saved_did_emsg;
14150 }
14151#endif
14152}
14153
14154/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014155 * "pumvisible()" function
14156 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014157 static void
14158f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014159 typval_T *argvars UNUSED;
14160 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014161{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014162#ifdef FEAT_INS_EXPAND
14163 if (pum_visible())
14164 rettv->vval.v_number = 1;
14165#endif
14166}
14167
14168/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014169 * "range()" function
14170 */
14171 static void
14172f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014173 typval_T *argvars;
14174 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014175{
14176 long start;
14177 long end;
14178 long stride = 1;
14179 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014180 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014183 if (argvars[1].v_type == VAR_UNKNOWN)
14184 {
14185 end = start - 1;
14186 start = 0;
14187 }
14188 else
14189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014191 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014192 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014193 }
14194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 if (error)
14196 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014197 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014198 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014199 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014200 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014201 else
14202 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014203 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014204 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014205 if (list_append_number(rettv->vval.v_list,
14206 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014207 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014208 }
14209}
14210
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014211/*
14212 * "readfile()" function
14213 */
14214 static void
14215f_readfile(argvars, rettv)
14216 typval_T *argvars;
14217 typval_T *rettv;
14218{
14219 int binary = FALSE;
14220 char_u *fname;
14221 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014222 listitem_T *li;
14223#define FREAD_SIZE 200 /* optimized for text lines */
14224 char_u buf[FREAD_SIZE];
14225 int readlen; /* size of last fread() */
14226 int buflen; /* nr of valid chars in buf[] */
14227 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
14228 int tolist; /* first byte in buf[] still to be put in list */
14229 int chop; /* how many CR to chop off */
14230 char_u *prev = NULL; /* previously read bytes, if any */
14231 int prevlen = 0; /* length of "prev" if not NULL */
14232 char_u *s;
14233 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014234 long maxline = MAXLNUM;
14235 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014236
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014237 if (argvars[1].v_type != VAR_UNKNOWN)
14238 {
14239 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14240 binary = TRUE;
14241 if (argvars[2].v_type != VAR_UNKNOWN)
14242 maxline = get_tv_number(&argvars[2]);
14243 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014244
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014245 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014246 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014247
14248 /* Always open the file in binary mode, library functions have a mind of
14249 * their own about CR-LF conversion. */
14250 fname = get_tv_string(&argvars[0]);
14251 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14252 {
14253 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14254 return;
14255 }
14256
14257 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014258 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014259 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014260 readlen = (int)fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014261 buflen = filtd + readlen;
14262 tolist = 0;
14263 for ( ; filtd < buflen || readlen <= 0; ++filtd)
14264 {
14265 if (buf[filtd] == '\n' || readlen <= 0)
14266 {
14267 /* Only when in binary mode add an empty list item when the
14268 * last line ends in a '\n'. */
14269 if (!binary && readlen == 0 && filtd == 0)
14270 break;
14271
14272 /* Found end-of-line or end-of-file: add a text line to the
14273 * list. */
14274 chop = 0;
14275 if (!binary)
14276 while (filtd - chop - 1 >= tolist
14277 && buf[filtd - chop - 1] == '\r')
14278 ++chop;
14279 len = filtd - tolist - chop;
14280 if (prev == NULL)
14281 s = vim_strnsave(buf + tolist, len);
14282 else
14283 {
14284 s = alloc((unsigned)(prevlen + len + 1));
14285 if (s != NULL)
14286 {
14287 mch_memmove(s, prev, prevlen);
14288 vim_free(prev);
14289 prev = NULL;
14290 mch_memmove(s + prevlen, buf + tolist, len);
14291 s[prevlen + len] = NUL;
14292 }
14293 }
14294 tolist = filtd + 1;
14295
14296 li = listitem_alloc();
14297 if (li == NULL)
14298 {
14299 vim_free(s);
14300 break;
14301 }
14302 li->li_tv.v_type = VAR_STRING;
14303 li->li_tv.v_lock = 0;
14304 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014305 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014306
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014307 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014308 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014309 if (readlen <= 0)
14310 break;
14311 }
14312 else if (buf[filtd] == NUL)
14313 buf[filtd] = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014314#ifdef FEAT_MBYTE
14315 else if (buf[filtd] == 0xef
14316 && enc_utf8
14317 && filtd + 2 < buflen
14318 && !binary
14319 && buf[filtd + 1] == 0xbb
14320 && buf[filtd + 2] == 0xbf)
14321 {
14322 /* remove utf-8 byte order mark */
14323 mch_memmove(buf + filtd, buf + filtd + 3, buflen - filtd - 3);
14324 --filtd;
14325 buflen -= 3;
14326 }
14327#endif
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014328 }
14329 if (readlen <= 0)
14330 break;
14331
14332 if (tolist == 0)
14333 {
14334 /* "buf" is full, need to move text to an allocated buffer */
14335 if (prev == NULL)
14336 {
14337 prev = vim_strnsave(buf, buflen);
14338 prevlen = buflen;
14339 }
14340 else
14341 {
14342 s = alloc((unsigned)(prevlen + buflen));
14343 if (s != NULL)
14344 {
14345 mch_memmove(s, prev, prevlen);
14346 mch_memmove(s + prevlen, buf, buflen);
14347 vim_free(prev);
14348 prev = s;
14349 prevlen += buflen;
14350 }
14351 }
14352 filtd = 0;
14353 }
14354 else
14355 {
14356 mch_memmove(buf, buf + tolist, buflen - tolist);
14357 filtd -= tolist;
14358 }
14359 }
14360
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014361 /*
14362 * For a negative line count use only the lines at the end of the file,
14363 * free the rest.
14364 */
14365 if (maxline < 0)
14366 while (cnt > -maxline)
14367 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014368 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014369 --cnt;
14370 }
14371
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014372 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014373 fclose(fd);
14374}
14375
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014376#if defined(FEAT_RELTIME)
14377static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14378
14379/*
14380 * Convert a List to proftime_T.
14381 * Return FAIL when there is something wrong.
14382 */
14383 static int
14384list2proftime(arg, tm)
14385 typval_T *arg;
14386 proftime_T *tm;
14387{
14388 long n1, n2;
14389 int error = FALSE;
14390
14391 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14392 || arg->vval.v_list->lv_len != 2)
14393 return FAIL;
14394 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14395 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14396# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014397 tm->HighPart = n1;
14398 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014399# else
14400 tm->tv_sec = n1;
14401 tm->tv_usec = n2;
14402# endif
14403 return error ? FAIL : OK;
14404}
14405#endif /* FEAT_RELTIME */
14406
14407/*
14408 * "reltime()" function
14409 */
14410 static void
14411f_reltime(argvars, rettv)
14412 typval_T *argvars;
14413 typval_T *rettv;
14414{
14415#ifdef FEAT_RELTIME
14416 proftime_T res;
14417 proftime_T start;
14418
14419 if (argvars[0].v_type == VAR_UNKNOWN)
14420 {
14421 /* No arguments: get current time. */
14422 profile_start(&res);
14423 }
14424 else if (argvars[1].v_type == VAR_UNKNOWN)
14425 {
14426 if (list2proftime(&argvars[0], &res) == FAIL)
14427 return;
14428 profile_end(&res);
14429 }
14430 else
14431 {
14432 /* Two arguments: compute the difference. */
14433 if (list2proftime(&argvars[0], &start) == FAIL
14434 || list2proftime(&argvars[1], &res) == FAIL)
14435 return;
14436 profile_sub(&res, &start);
14437 }
14438
14439 if (rettv_list_alloc(rettv) == OK)
14440 {
14441 long n1, n2;
14442
14443# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014444 n1 = res.HighPart;
14445 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014446# else
14447 n1 = res.tv_sec;
14448 n2 = res.tv_usec;
14449# endif
14450 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14451 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14452 }
14453#endif
14454}
14455
14456/*
14457 * "reltimestr()" function
14458 */
14459 static void
14460f_reltimestr(argvars, rettv)
14461 typval_T *argvars;
14462 typval_T *rettv;
14463{
14464#ifdef FEAT_RELTIME
14465 proftime_T tm;
14466#endif
14467
14468 rettv->v_type = VAR_STRING;
14469 rettv->vval.v_string = NULL;
14470#ifdef FEAT_RELTIME
14471 if (list2proftime(&argvars[0], &tm) == OK)
14472 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14473#endif
14474}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014475
Bram Moolenaar0d660222005-01-07 21:51:51 +000014476#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14477static void make_connection __ARGS((void));
14478static int check_connection __ARGS((void));
14479
14480 static void
14481make_connection()
14482{
14483 if (X_DISPLAY == NULL
14484# ifdef FEAT_GUI
14485 && !gui.in_use
14486# endif
14487 )
14488 {
14489 x_force_connect = TRUE;
14490 setup_term_clip();
14491 x_force_connect = FALSE;
14492 }
14493}
14494
14495 static int
14496check_connection()
14497{
14498 make_connection();
14499 if (X_DISPLAY == NULL)
14500 {
14501 EMSG(_("E240: No connection to Vim server"));
14502 return FAIL;
14503 }
14504 return OK;
14505}
14506#endif
14507
14508#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014509static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014510
14511 static void
14512remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000014513 typval_T *argvars;
14514 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014515 int expr;
14516{
14517 char_u *server_name;
14518 char_u *keys;
14519 char_u *r = NULL;
14520 char_u buf[NUMBUFLEN];
14521# ifdef WIN32
14522 HWND w;
14523# else
14524 Window w;
14525# endif
14526
14527 if (check_restricted() || check_secure())
14528 return;
14529
14530# ifdef FEAT_X11
14531 if (check_connection() == FAIL)
14532 return;
14533# endif
14534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014535 server_name = get_tv_string_chk(&argvars[0]);
14536 if (server_name == NULL)
14537 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014538 keys = get_tv_string_buf(&argvars[1], buf);
14539# ifdef WIN32
14540 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
14541# else
14542 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
14543 < 0)
14544# endif
14545 {
14546 if (r != NULL)
14547 EMSG(r); /* sending worked but evaluation failed */
14548 else
14549 EMSG2(_("E241: Unable to send to %s"), server_name);
14550 return;
14551 }
14552
14553 rettv->vval.v_string = r;
14554
14555 if (argvars[2].v_type != VAR_UNKNOWN)
14556 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014557 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000014558 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014559 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014560
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014561 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000014562 v.di_tv.v_type = VAR_STRING;
14563 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014564 idvar = get_tv_string_chk(&argvars[2]);
14565 if (idvar != NULL)
14566 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014567 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014568 }
14569}
14570#endif
14571
14572/*
14573 * "remote_expr()" function
14574 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014575 static void
14576f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014577 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014578 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014579{
14580 rettv->v_type = VAR_STRING;
14581 rettv->vval.v_string = NULL;
14582#ifdef FEAT_CLIENTSERVER
14583 remote_common(argvars, rettv, TRUE);
14584#endif
14585}
14586
14587/*
14588 * "remote_foreground()" function
14589 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014590 static void
14591f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014592 typval_T *argvars UNUSED;
14593 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014594{
Bram Moolenaar0d660222005-01-07 21:51:51 +000014595#ifdef FEAT_CLIENTSERVER
14596# ifdef WIN32
14597 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 {
14599 char_u *server_name = get_tv_string_chk(&argvars[0]);
14600
14601 if (server_name != NULL)
14602 serverForeground(server_name);
14603 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014604# else
14605 /* Send a foreground() expression to the server. */
14606 argvars[1].v_type = VAR_STRING;
14607 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
14608 argvars[2].v_type = VAR_UNKNOWN;
14609 remote_common(argvars, rettv, TRUE);
14610 vim_free(argvars[1].vval.v_string);
14611# endif
14612#endif
14613}
14614
Bram Moolenaar0d660222005-01-07 21:51:51 +000014615 static void
14616f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014617 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014618 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014619{
14620#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000014621 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014622 char_u *s = NULL;
14623# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014624 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014625# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014626 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014627
14628 if (check_restricted() || check_secure())
14629 {
14630 rettv->vval.v_number = -1;
14631 return;
14632 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014633 serverid = get_tv_string_chk(&argvars[0]);
14634 if (serverid == NULL)
14635 {
14636 rettv->vval.v_number = -1;
14637 return; /* type error; errmsg already given */
14638 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014639# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014640 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014641 if (n == 0)
14642 rettv->vval.v_number = -1;
14643 else
14644 {
14645 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
14646 rettv->vval.v_number = (s != NULL);
14647 }
14648# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000014649 if (check_connection() == FAIL)
14650 return;
14651
14652 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014653 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014654# endif
14655
14656 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
14657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014658 char_u *retvar;
14659
Bram Moolenaar33570922005-01-25 22:26:29 +000014660 v.di_tv.v_type = VAR_STRING;
14661 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014662 retvar = get_tv_string_chk(&argvars[1]);
14663 if (retvar != NULL)
14664 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000014665 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014666 }
14667#else
14668 rettv->vval.v_number = -1;
14669#endif
14670}
14671
Bram Moolenaar0d660222005-01-07 21:51:51 +000014672 static void
14673f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014674 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014675 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014676{
14677 char_u *r = NULL;
14678
14679#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014680 char_u *serverid = get_tv_string_chk(&argvars[0]);
14681
14682 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000014683 {
14684# ifdef WIN32
14685 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014686 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014687
Bram Moolenaareb3593b2006-04-22 22:33:57 +000014688 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014689 if (n != 0)
14690 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
14691 if (r == NULL)
14692# else
14693 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014694 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014695# endif
14696 EMSG(_("E277: Unable to read a server reply"));
14697 }
14698#endif
14699 rettv->v_type = VAR_STRING;
14700 rettv->vval.v_string = r;
14701}
14702
14703/*
14704 * "remote_send()" function
14705 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000014706 static void
14707f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014708 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014709 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014710{
14711 rettv->v_type = VAR_STRING;
14712 rettv->vval.v_string = NULL;
14713#ifdef FEAT_CLIENTSERVER
14714 remote_common(argvars, rettv, FALSE);
14715#endif
14716}
14717
14718/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014719 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014720 */
14721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014722f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014723 typval_T *argvars;
14724 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014725{
Bram Moolenaar33570922005-01-25 22:26:29 +000014726 list_T *l;
14727 listitem_T *item, *item2;
14728 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014729 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014730 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014731 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000014732 dict_T *d;
14733 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014734
Bram Moolenaar8c711452005-01-14 21:53:12 +000014735 if (argvars[0].v_type == VAR_DICT)
14736 {
14737 if (argvars[2].v_type != VAR_UNKNOWN)
14738 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014739 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014740 && !tv_check_lock(d->dv_lock, (char_u *)"remove() argument"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 key = get_tv_string_chk(&argvars[1]);
14743 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014744 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014745 di = dict_find(d, key, -1);
14746 if (di == NULL)
14747 EMSG2(_(e_dictkey), key);
14748 else
14749 {
14750 *rettv = di->di_tv;
14751 init_tv(&di->di_tv);
14752 dictitem_remove(d, di);
14753 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014755 }
14756 }
14757 else if (argvars[0].v_type != VAR_LIST)
14758 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014759 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar9dfb0f82006-06-22 16:03:05 +000014760 && !tv_check_lock(l->lv_lock, (char_u *)"remove() argument"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014762 int error = FALSE;
14763
14764 idx = get_tv_number_chk(&argvars[1], &error);
14765 if (error)
14766 ; /* type error: do nothing, errmsg already given */
14767 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014768 EMSGN(_(e_listidx), idx);
14769 else
14770 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014771 if (argvars[2].v_type == VAR_UNKNOWN)
14772 {
14773 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014774 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014775 *rettv = item->li_tv;
14776 vim_free(item);
14777 }
14778 else
14779 {
14780 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014781 end = get_tv_number_chk(&argvars[2], &error);
14782 if (error)
14783 ; /* type error: do nothing */
14784 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014785 EMSGN(_(e_listidx), end);
14786 else
14787 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014788 int cnt = 0;
14789
14790 for (li = item; li != NULL; li = li->li_next)
14791 {
14792 ++cnt;
14793 if (li == item2)
14794 break;
14795 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014796 if (li == NULL) /* didn't find "item2" after "item" */
14797 EMSG(_(e_invrange));
14798 else
14799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000014800 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014801 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014802 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014803 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014804 l->lv_first = item;
14805 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014806 item->li_prev = NULL;
14807 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014808 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014809 }
14810 }
14811 }
14812 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014813 }
14814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815}
14816
14817/*
14818 * "rename({from}, {to})" function
14819 */
14820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014821f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014822 typval_T *argvars;
14823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824{
14825 char_u buf[NUMBUFLEN];
14826
14827 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014828 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014829 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014830 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
14831 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832}
14833
14834/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014835 * "repeat()" function
14836 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014837 static void
14838f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014839 typval_T *argvars;
14840 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014841{
14842 char_u *p;
14843 int n;
14844 int slen;
14845 int len;
14846 char_u *r;
14847 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014848
14849 n = get_tv_number(&argvars[1]);
14850 if (argvars[0].v_type == VAR_LIST)
14851 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014852 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014853 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014854 if (list_extend(rettv->vval.v_list,
14855 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014856 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014857 }
14858 else
14859 {
14860 p = get_tv_string(&argvars[0]);
14861 rettv->v_type = VAR_STRING;
14862 rettv->vval.v_string = NULL;
14863
14864 slen = (int)STRLEN(p);
14865 len = slen * n;
14866 if (len <= 0)
14867 return;
14868
14869 r = alloc(len + 1);
14870 if (r != NULL)
14871 {
14872 for (i = 0; i < n; i++)
14873 mch_memmove(r + i * slen, p, (size_t)slen);
14874 r[len] = NUL;
14875 }
14876
14877 rettv->vval.v_string = r;
14878 }
14879}
14880
14881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882 * "resolve()" function
14883 */
14884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014885f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014886 typval_T *argvars;
14887 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888{
14889 char_u *p;
14890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014891 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892#ifdef FEAT_SHORTCUT
14893 {
14894 char_u *v = NULL;
14895
14896 v = mch_resolve_shortcut(p);
14897 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014898 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014900 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 }
14902#else
14903# ifdef HAVE_READLINK
14904 {
14905 char_u buf[MAXPATHL + 1];
14906 char_u *cpy;
14907 int len;
14908 char_u *remain = NULL;
14909 char_u *q;
14910 int is_relative_to_current = FALSE;
14911 int has_trailing_pathsep = FALSE;
14912 int limit = 100;
14913
14914 p = vim_strsave(p);
14915
14916 if (p[0] == '.' && (vim_ispathsep(p[1])
14917 || (p[1] == '.' && (vim_ispathsep(p[2])))))
14918 is_relative_to_current = TRUE;
14919
14920 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014921 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014922 has_trailing_pathsep = TRUE;
14923
14924 q = getnextcomp(p);
14925 if (*q != NUL)
14926 {
14927 /* Separate the first path component in "p", and keep the
14928 * remainder (beginning with the path separator). */
14929 remain = vim_strsave(q - 1);
14930 q[-1] = NUL;
14931 }
14932
14933 for (;;)
14934 {
14935 for (;;)
14936 {
14937 len = readlink((char *)p, (char *)buf, MAXPATHL);
14938 if (len <= 0)
14939 break;
14940 buf[len] = NUL;
14941
14942 if (limit-- == 0)
14943 {
14944 vim_free(p);
14945 vim_free(remain);
14946 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014947 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948 goto fail;
14949 }
14950
14951 /* Ensure that the result will have a trailing path separator
14952 * if the argument has one. */
14953 if (remain == NULL && has_trailing_pathsep)
14954 add_pathsep(buf);
14955
14956 /* Separate the first path component in the link value and
14957 * concatenate the remainders. */
14958 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
14959 if (*q != NUL)
14960 {
14961 if (remain == NULL)
14962 remain = vim_strsave(q - 1);
14963 else
14964 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000014965 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014966 if (cpy != NULL)
14967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014968 vim_free(remain);
14969 remain = cpy;
14970 }
14971 }
14972 q[-1] = NUL;
14973 }
14974
14975 q = gettail(p);
14976 if (q > p && *q == NUL)
14977 {
14978 /* Ignore trailing path separator. */
14979 q[-1] = NUL;
14980 q = gettail(p);
14981 }
14982 if (q > p && !mch_isFullName(buf))
14983 {
14984 /* symlink is relative to directory of argument */
14985 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
14986 if (cpy != NULL)
14987 {
14988 STRCPY(cpy, p);
14989 STRCPY(gettail(cpy), buf);
14990 vim_free(p);
14991 p = cpy;
14992 }
14993 }
14994 else
14995 {
14996 vim_free(p);
14997 p = vim_strsave(buf);
14998 }
14999 }
15000
15001 if (remain == NULL)
15002 break;
15003
15004 /* Append the first path component of "remain" to "p". */
15005 q = getnextcomp(remain + 1);
15006 len = q - remain - (*q != NUL);
15007 cpy = vim_strnsave(p, STRLEN(p) + len);
15008 if (cpy != NULL)
15009 {
15010 STRNCAT(cpy, remain, len);
15011 vim_free(p);
15012 p = cpy;
15013 }
15014 /* Shorten "remain". */
15015 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015016 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017 else
15018 {
15019 vim_free(remain);
15020 remain = NULL;
15021 }
15022 }
15023
15024 /* If the result is a relative path name, make it explicitly relative to
15025 * the current directory if and only if the argument had this form. */
15026 if (!vim_ispathsep(*p))
15027 {
15028 if (is_relative_to_current
15029 && *p != NUL
15030 && !(p[0] == '.'
15031 && (p[1] == NUL
15032 || vim_ispathsep(p[1])
15033 || (p[1] == '.'
15034 && (p[2] == NUL
15035 || vim_ispathsep(p[2]))))))
15036 {
15037 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015038 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039 if (cpy != NULL)
15040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 vim_free(p);
15042 p = cpy;
15043 }
15044 }
15045 else if (!is_relative_to_current)
15046 {
15047 /* Strip leading "./". */
15048 q = p;
15049 while (q[0] == '.' && vim_ispathsep(q[1]))
15050 q += 2;
15051 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015052 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053 }
15054 }
15055
15056 /* Ensure that the result will have no trailing path separator
15057 * if the argument had none. But keep "/" or "//". */
15058 if (!has_trailing_pathsep)
15059 {
15060 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015061 if (after_pathsep(p, q))
15062 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063 }
15064
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015065 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066 }
15067# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015068 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015069# endif
15070#endif
15071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015072 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073
15074#ifdef HAVE_READLINK
15075fail:
15076#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015077 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078}
15079
15080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015081 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 */
15083 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015084f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015085 typval_T *argvars;
15086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087{
Bram Moolenaar33570922005-01-25 22:26:29 +000015088 list_T *l;
15089 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015090
Bram Moolenaar0d660222005-01-07 21:51:51 +000015091 if (argvars[0].v_type != VAR_LIST)
15092 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015093 else if ((l = argvars[0].vval.v_list) != NULL
15094 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095 {
15096 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015097 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015098 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015099 while (li != NULL)
15100 {
15101 ni = li->li_prev;
15102 list_append(l, li);
15103 li = ni;
15104 }
15105 rettv->vval.v_list = l;
15106 rettv->v_type = VAR_LIST;
15107 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015108 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110}
15111
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015112#define SP_NOMOVE 0x01 /* don't move cursor */
15113#define SP_REPEAT 0x02 /* repeat to find outer pair */
15114#define SP_RETCOUNT 0x04 /* return matchcount */
15115#define SP_SETPCMARK 0x08 /* set previous context mark */
15116#define SP_START 0x10 /* accept match at start position */
15117#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15118#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015119
Bram Moolenaar33570922005-01-25 22:26:29 +000015120static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121
15122/*
15123 * Get flags for a search function.
15124 * Possibly sets "p_ws".
15125 * Returns BACKWARD, FORWARD or zero (for an error).
15126 */
15127 static int
15128get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015129 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015130 int *flagsp;
15131{
15132 int dir = FORWARD;
15133 char_u *flags;
15134 char_u nbuf[NUMBUFLEN];
15135 int mask;
15136
15137 if (varp->v_type != VAR_UNKNOWN)
15138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015139 flags = get_tv_string_buf_chk(varp, nbuf);
15140 if (flags == NULL)
15141 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142 while (*flags != NUL)
15143 {
15144 switch (*flags)
15145 {
15146 case 'b': dir = BACKWARD; break;
15147 case 'w': p_ws = TRUE; break;
15148 case 'W': p_ws = FALSE; break;
15149 default: mask = 0;
15150 if (flagsp != NULL)
15151 switch (*flags)
15152 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015153 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015154 case 'e': mask = SP_END; break;
15155 case 'm': mask = SP_RETCOUNT; break;
15156 case 'n': mask = SP_NOMOVE; break;
15157 case 'p': mask = SP_SUBPAT; break;
15158 case 'r': mask = SP_REPEAT; break;
15159 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160 }
15161 if (mask == 0)
15162 {
15163 EMSG2(_(e_invarg2), flags);
15164 dir = 0;
15165 }
15166 else
15167 *flagsp |= mask;
15168 }
15169 if (dir == 0)
15170 break;
15171 ++flags;
15172 }
15173 }
15174 return dir;
15175}
15176
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015178 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015180 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015181search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015182 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015183 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015184 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015185{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015186 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015187 char_u *pat;
15188 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015189 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190 int save_p_ws = p_ws;
15191 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015192 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015193 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015194 proftime_T tm;
15195#ifdef FEAT_RELTIME
15196 long time_limit = 0;
15197#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015198 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015199 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015201 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015202 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015203 if (dir == 0)
15204 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015205 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015206 if (flags & SP_START)
15207 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015208 if (flags & SP_END)
15209 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015210
Bram Moolenaar76929292008-01-06 19:07:36 +000015211 /* Optional arguments: line number to stop searching and timeout. */
15212 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015213 {
15214 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15215 if (lnum_stop < 0)
15216 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015217#ifdef FEAT_RELTIME
15218 if (argvars[3].v_type != VAR_UNKNOWN)
15219 {
15220 time_limit = get_tv_number_chk(&argvars[3], NULL);
15221 if (time_limit < 0)
15222 goto theend;
15223 }
15224#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015225 }
15226
Bram Moolenaar76929292008-01-06 19:07:36 +000015227#ifdef FEAT_RELTIME
15228 /* Set the time limit, if there is one. */
15229 profile_setlimit(time_limit, &tm);
15230#endif
15231
Bram Moolenaar231334e2005-07-25 20:46:57 +000015232 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015233 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015234 * Check to make sure only those flags are set.
15235 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15236 * flags cannot be set. Check for that condition also.
15237 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015238 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015239 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015241 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015242 goto theend;
15243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015245 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015246 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015247 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015248 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015249 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015250 if (flags & SP_SUBPAT)
15251 retval = subpatnum;
15252 else
15253 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015254 if (flags & SP_SETPCMARK)
15255 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015257 if (match_pos != NULL)
15258 {
15259 /* Store the match cursor position */
15260 match_pos->lnum = pos.lnum;
15261 match_pos->col = pos.col + 1;
15262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263 /* "/$" will put the cursor after the end of the line, may need to
15264 * correct that here */
15265 check_cursor();
15266 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015267
15268 /* If 'n' flag is used: restore cursor position. */
15269 if (flags & SP_NOMOVE)
15270 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015271 else
15272 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015273theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015274 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015275
15276 return retval;
15277}
15278
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015279#ifdef FEAT_FLOAT
15280/*
15281 * "round({float})" function
15282 */
15283 static void
15284f_round(argvars, rettv)
15285 typval_T *argvars;
15286 typval_T *rettv;
15287{
15288 float_T f;
15289
15290 rettv->v_type = VAR_FLOAT;
15291 if (get_float_arg(argvars, &f) == OK)
15292 /* round() is not in C90, use ceil() or floor() instead. */
15293 rettv->vval.v_float = f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15294 else
15295 rettv->vval.v_float = 0.0;
15296}
15297#endif
15298
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015299/*
15300 * "search()" function
15301 */
15302 static void
15303f_search(argvars, rettv)
15304 typval_T *argvars;
15305 typval_T *rettv;
15306{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015307 int flags = 0;
15308
15309 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015310}
15311
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015313 * "searchdecl()" function
15314 */
15315 static void
15316f_searchdecl(argvars, rettv)
15317 typval_T *argvars;
15318 typval_T *rettv;
15319{
15320 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015321 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015322 int error = FALSE;
15323 char_u *name;
15324
15325 rettv->vval.v_number = 1; /* default: FAIL */
15326
15327 name = get_tv_string_chk(&argvars[0]);
15328 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015329 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015330 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015331 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15332 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15333 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015334 if (!error && name != NULL)
15335 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015336 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015337}
15338
15339/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015340 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015342 static int
15343searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015344 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015345 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015346{
15347 char_u *spat, *mpat, *epat;
15348 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 int dir;
15351 int flags = 0;
15352 char_u nbuf1[NUMBUFLEN];
15353 char_u nbuf2[NUMBUFLEN];
15354 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015355 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015356 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015357 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015360 spat = get_tv_string_chk(&argvars[0]);
15361 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15362 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15363 if (spat == NULL || mpat == NULL || epat == NULL)
15364 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 /* Handle the optional fourth argument: flags */
15367 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015368 if (dir == 0)
15369 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015370
15371 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015372 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15373 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015374 if ((flags & (SP_END | SP_SUBPAT)) != 0
15375 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015376 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015377 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015378 goto theend;
15379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015381 /* Using 'r' implies 'W', otherwise it doesn't work. */
15382 if (flags & SP_REPEAT)
15383 p_ws = FALSE;
15384
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015385 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015386 if (argvars[3].v_type == VAR_UNKNOWN
15387 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 skip = (char_u *)"";
15389 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015391 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015392 if (argvars[5].v_type != VAR_UNKNOWN)
15393 {
15394 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
15395 if (lnum_stop < 0)
15396 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015397#ifdef FEAT_RELTIME
15398 if (argvars[6].v_type != VAR_UNKNOWN)
15399 {
15400 time_limit = get_tv_number_chk(&argvars[6], NULL);
15401 if (time_limit < 0)
15402 goto theend;
15403 }
15404#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015405 }
15406 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015407 if (skip == NULL)
15408 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015410 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000015411 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015412
15413theend:
15414 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015415
15416 return retval;
15417}
15418
15419/*
15420 * "searchpair()" function
15421 */
15422 static void
15423f_searchpair(argvars, rettv)
15424 typval_T *argvars;
15425 typval_T *rettv;
15426{
15427 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
15428}
15429
15430/*
15431 * "searchpairpos()" function
15432 */
15433 static void
15434f_searchpairpos(argvars, rettv)
15435 typval_T *argvars;
15436 typval_T *rettv;
15437{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015438 pos_T match_pos;
15439 int lnum = 0;
15440 int col = 0;
15441
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015442 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015443 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015444
15445 if (searchpair_cmn(argvars, &match_pos) > 0)
15446 {
15447 lnum = match_pos.lnum;
15448 col = match_pos.col;
15449 }
15450
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015451 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15452 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015453}
15454
15455/*
15456 * Search for a start/middle/end thing.
15457 * Used by searchpair(), see its documentation for the details.
15458 * Returns 0 or -1 for no match,
15459 */
15460 long
Bram Moolenaar76929292008-01-06 19:07:36 +000015461do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
15462 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015463 char_u *spat; /* start pattern */
15464 char_u *mpat; /* middle pattern */
15465 char_u *epat; /* end pattern */
15466 int dir; /* BACKWARD or FORWARD */
15467 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015468 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015469 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015470 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaar76929292008-01-06 19:07:36 +000015471 long time_limit; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015472{
15473 char_u *save_cpo;
15474 char_u *pat, *pat2 = NULL, *pat3 = NULL;
15475 long retval = 0;
15476 pos_T pos;
15477 pos_T firstpos;
15478 pos_T foundpos;
15479 pos_T save_cursor;
15480 pos_T save_pos;
15481 int n;
15482 int r;
15483 int nest = 1;
15484 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015485 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000015486 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015487
15488 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15489 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015490 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015491
Bram Moolenaar76929292008-01-06 19:07:36 +000015492#ifdef FEAT_RELTIME
15493 /* Set the time limit, if there is one. */
15494 profile_setlimit(time_limit, &tm);
15495#endif
15496
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015497 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15498 * start/middle/end (pat3, for the top pair). */
15499 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
15500 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
15501 if (pat2 == NULL || pat3 == NULL)
15502 goto theend;
15503 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
15504 if (*mpat == NUL)
15505 STRCPY(pat3, pat2);
15506 else
15507 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15508 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015509 if (flags & SP_START)
15510 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015511
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 save_cursor = curwin->w_cursor;
15513 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000015514 clearpos(&firstpos);
15515 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516 pat = pat3;
15517 for (;;)
15518 {
15519 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015520 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
15522 /* didn't find it or found the first match again: FAIL */
15523 break;
15524
15525 if (firstpos.lnum == 0)
15526 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000015527 if (equalpos(pos, foundpos))
15528 {
15529 /* Found the same position again. Can happen with a pattern that
15530 * has "\zs" at the end and searching backwards. Advance one
15531 * character and try again. */
15532 if (dir == BACKWARD)
15533 decl(&pos);
15534 else
15535 incl(&pos);
15536 }
15537 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015539 /* clear the start flag to avoid getting stuck here */
15540 options &= ~SEARCH_START;
15541
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 /* If the skip pattern matches, ignore this match. */
15543 if (*skip != NUL)
15544 {
15545 save_pos = curwin->w_cursor;
15546 curwin->w_cursor = pos;
15547 r = eval_to_bool(skip, &err, NULL, FALSE);
15548 curwin->w_cursor = save_pos;
15549 if (err)
15550 {
15551 /* Evaluating {skip} caused an error, break here. */
15552 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015553 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 break;
15555 }
15556 if (r)
15557 continue;
15558 }
15559
15560 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
15561 {
15562 /* Found end when searching backwards or start when searching
15563 * forward: nested pair. */
15564 ++nest;
15565 pat = pat2; /* nested, don't search for middle */
15566 }
15567 else
15568 {
15569 /* Found end when searching forward or start when searching
15570 * backward: end of (nested) pair; or found middle in outer pair. */
15571 if (--nest == 1)
15572 pat = pat3; /* outer level, search for middle */
15573 }
15574
15575 if (nest == 0)
15576 {
15577 /* Found the match: return matchcount or line number. */
15578 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015579 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015581 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015582 if (flags & SP_SETPCMARK)
15583 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 curwin->w_cursor = pos;
15585 if (!(flags & SP_REPEAT))
15586 break;
15587 nest = 1; /* search for next unmatched */
15588 }
15589 }
15590
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015591 if (match_pos != NULL)
15592 {
15593 /* Store the match cursor position */
15594 match_pos->lnum = curwin->w_cursor.lnum;
15595 match_pos->col = curwin->w_cursor.col + 1;
15596 }
15597
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015599 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600 curwin->w_cursor = save_cursor;
15601
15602theend:
15603 vim_free(pat2);
15604 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000015605 if (p_cpo == empty_option)
15606 p_cpo = save_cpo;
15607 else
15608 /* Darn, evaluating the {skip} expression changed the value. */
15609 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000015610
15611 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612}
15613
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015614/*
15615 * "searchpos()" function
15616 */
15617 static void
15618f_searchpos(argvars, rettv)
15619 typval_T *argvars;
15620 typval_T *rettv;
15621{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015622 pos_T match_pos;
15623 int lnum = 0;
15624 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015625 int n;
15626 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015627
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015628 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015629 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015630
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015631 n = search_cmn(argvars, &match_pos, &flags);
15632 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015633 {
15634 lnum = match_pos.lnum;
15635 col = match_pos.col;
15636 }
15637
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015638 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
15639 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015640 if (flags & SP_SUBPAT)
15641 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015642}
15643
15644
Bram Moolenaar0d660222005-01-07 21:51:51 +000015645 static void
15646f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015647 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015650#ifdef FEAT_CLIENTSERVER
15651 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015652 char_u *server = get_tv_string_chk(&argvars[0]);
15653 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654
Bram Moolenaar0d660222005-01-07 21:51:51 +000015655 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015656 if (server == NULL || reply == NULL)
15657 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015658 if (check_restricted() || check_secure())
15659 return;
15660# ifdef FEAT_X11
15661 if (check_connection() == FAIL)
15662 return;
15663# endif
15664
15665 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000015667 EMSG(_("E258: Unable to send to client"));
15668 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015669 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015670 rettv->vval.v_number = 0;
15671#else
15672 rettv->vval.v_number = -1;
15673#endif
15674}
15675
Bram Moolenaar0d660222005-01-07 21:51:51 +000015676 static void
15677f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015678 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015679 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680{
15681 char_u *r = NULL;
15682
15683#ifdef FEAT_CLIENTSERVER
15684# ifdef WIN32
15685 r = serverGetVimNames();
15686# else
15687 make_connection();
15688 if (X_DISPLAY != NULL)
15689 r = serverGetVimNames(X_DISPLAY);
15690# endif
15691#endif
15692 rettv->v_type = VAR_STRING;
15693 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694}
15695
15696/*
15697 * "setbufvar()" function
15698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015700f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015701 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015702 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015703{
15704 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015707 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708 char_u nbuf[NUMBUFLEN];
15709
15710 if (check_restricted() || check_secure())
15711 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015712 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
15713 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015714 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 varp = &argvars[2];
15716
15717 if (buf != NULL && varname != NULL && varp != NULL)
15718 {
15719 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721
15722 if (*varname == '&')
15723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015724 long numval;
15725 char_u *strval;
15726 int error = FALSE;
15727
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015729 numval = get_tv_number_chk(varp, &error);
15730 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015731 if (!error && strval != NULL)
15732 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 }
15734 else
15735 {
15736 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
15737 if (bufvarname != NULL)
15738 {
15739 STRCPY(bufvarname, "b:");
15740 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015741 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 vim_free(bufvarname);
15743 }
15744 }
15745
15746 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749}
15750
15751/*
15752 * "setcmdpos()" function
15753 */
15754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015755f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015756 typval_T *argvars;
15757 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015759 int pos = (int)get_tv_number(&argvars[0]) - 1;
15760
15761 if (pos >= 0)
15762 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763}
15764
15765/*
15766 * "setline()" function
15767 */
15768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015769f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015770 typval_T *argvars;
15771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772{
15773 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000015774 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015775 list_T *l = NULL;
15776 listitem_T *li = NULL;
15777 long added = 0;
15778 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015780 lnum = get_tv_lnum(&argvars[0]);
15781 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015783 l = argvars[1].vval.v_list;
15784 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015786 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015787 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015788
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015789 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015790 for (;;)
15791 {
15792 if (l != NULL)
15793 {
15794 /* list argument, get next string */
15795 if (li == NULL)
15796 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015797 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015798 li = li->li_next;
15799 }
15800
15801 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015802 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015803 break;
15804 if (lnum <= curbuf->b_ml.ml_line_count)
15805 {
15806 /* existing line, replace it */
15807 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
15808 {
15809 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000015810 if (lnum == curwin->w_cursor.lnum)
15811 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000015812 rettv->vval.v_number = 0; /* OK */
15813 }
15814 }
15815 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
15816 {
15817 /* lnum is one past the last line, append the line */
15818 ++added;
15819 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
15820 rettv->vval.v_number = 0; /* OK */
15821 }
15822
15823 if (l == NULL) /* only one string argument */
15824 break;
15825 ++lnum;
15826 }
15827
15828 if (added > 0)
15829 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830}
15831
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000015832static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
15833
Bram Moolenaar071d4272004-06-13 20:20:40 +000015834/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015835 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000015836 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000015837 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015838set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015839 win_T *wp UNUSED;
15840 typval_T *list_arg UNUSED;
15841 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015842 typval_T *rettv;
15843{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015844#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015845 char_u *act;
15846 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000015847#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015848
Bram Moolenaar2641f772005-03-25 21:58:17 +000015849 rettv->vval.v_number = -1;
15850
15851#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015852 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015853 EMSG(_(e_listreq));
15854 else
15855 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015856 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000015857
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015858 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015859 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015860 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015861 if (act == NULL)
15862 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000015863 if (*act == 'a' || *act == 'r')
15864 action = *act;
15865 }
15866
Bram Moolenaarbc226b62010-08-09 22:14:48 +020015867 if (l != NULL && set_errorlist(wp, l, action, NULL) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000015868 rettv->vval.v_number = 0;
15869 }
15870#endif
15871}
15872
15873/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015874 * "setloclist()" function
15875 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015876 static void
15877f_setloclist(argvars, rettv)
15878 typval_T *argvars;
15879 typval_T *rettv;
15880{
15881 win_T *win;
15882
15883 rettv->vval.v_number = -1;
15884
Bram Moolenaar99ebf042006-04-15 20:28:54 +000015885 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015886 if (win != NULL)
15887 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
15888}
15889
15890/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015891 * "setmatches()" function
15892 */
15893 static void
15894f_setmatches(argvars, rettv)
15895 typval_T *argvars;
15896 typval_T *rettv;
15897{
15898#ifdef FEAT_SEARCH_EXTRA
15899 list_T *l;
15900 listitem_T *li;
15901 dict_T *d;
15902
15903 rettv->vval.v_number = -1;
15904 if (argvars[0].v_type != VAR_LIST)
15905 {
15906 EMSG(_(e_listreq));
15907 return;
15908 }
15909 if ((l = argvars[0].vval.v_list) != NULL)
15910 {
15911
15912 /* To some extent make sure that we are dealing with a list from
15913 * "getmatches()". */
15914 li = l->lv_first;
15915 while (li != NULL)
15916 {
15917 if (li->li_tv.v_type != VAR_DICT
15918 || (d = li->li_tv.vval.v_dict) == NULL)
15919 {
15920 EMSG(_(e_invarg));
15921 return;
15922 }
15923 if (!(dict_find(d, (char_u *)"group", -1) != NULL
15924 && dict_find(d, (char_u *)"pattern", -1) != NULL
15925 && dict_find(d, (char_u *)"priority", -1) != NULL
15926 && dict_find(d, (char_u *)"id", -1) != NULL))
15927 {
15928 EMSG(_(e_invarg));
15929 return;
15930 }
15931 li = li->li_next;
15932 }
15933
15934 clear_matches(curwin);
15935 li = l->lv_first;
15936 while (li != NULL)
15937 {
15938 d = li->li_tv.vval.v_dict;
15939 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
15940 get_dict_string(d, (char_u *)"pattern", FALSE),
15941 (int)get_dict_number(d, (char_u *)"priority"),
15942 (int)get_dict_number(d, (char_u *)"id"));
15943 li = li->li_next;
15944 }
15945 rettv->vval.v_number = 0;
15946 }
15947#endif
15948}
15949
15950/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015951 * "setpos()" function
15952 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015953 static void
15954f_setpos(argvars, rettv)
15955 typval_T *argvars;
15956 typval_T *rettv;
15957{
15958 pos_T pos;
15959 int fnum;
15960 char_u *name;
15961
Bram Moolenaar08250432008-02-13 11:42:46 +000015962 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015963 name = get_tv_string_chk(argvars);
15964 if (name != NULL)
15965 {
15966 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
15967 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000015968 if (--pos.col < 0)
15969 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000015970 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015971 {
Bram Moolenaar08250432008-02-13 11:42:46 +000015972 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015973 if (fnum == curbuf->b_fnum)
15974 {
15975 curwin->w_cursor = pos;
15976 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000015977 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015978 }
15979 else
15980 EMSG(_(e_invarg));
15981 }
Bram Moolenaar08250432008-02-13 11:42:46 +000015982 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
15983 {
15984 /* set mark */
15985 if (setmark_pos(name[1], &pos, fnum) == OK)
15986 rettv->vval.v_number = 0;
15987 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015988 else
15989 EMSG(_(e_invarg));
15990 }
15991 }
15992}
15993
15994/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015995 * "setqflist()" function
15996 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000015997 static void
15998f_setqflist(argvars, rettv)
15999 typval_T *argvars;
16000 typval_T *rettv;
16001{
16002 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16003}
16004
16005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 * "setreg()" function
16007 */
16008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016010 typval_T *argvars;
16011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012{
16013 int regname;
16014 char_u *strregname;
16015 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016016 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 int append;
16018 char_u yank_type;
16019 long block_len;
16020
16021 block_len = -1;
16022 yank_type = MAUTO;
16023 append = FALSE;
16024
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016025 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016026 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 if (strregname == NULL)
16029 return; /* type error; errmsg already given */
16030 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016031 if (regname == 0 || regname == '@')
16032 regname = '"';
16033 else if (regname == '=')
16034 return;
16035
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016036 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016038 stropt = get_tv_string_chk(&argvars[2]);
16039 if (stropt == NULL)
16040 return; /* type error */
16041 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042 switch (*stropt)
16043 {
16044 case 'a': case 'A': /* append */
16045 append = TRUE;
16046 break;
16047 case 'v': case 'c': /* character-wise selection */
16048 yank_type = MCHAR;
16049 break;
16050 case 'V': case 'l': /* line-wise selection */
16051 yank_type = MLINE;
16052 break;
16053#ifdef FEAT_VISUAL
16054 case 'b': case Ctrl_V: /* block-wise selection */
16055 yank_type = MBLOCK;
16056 if (VIM_ISDIGIT(stropt[1]))
16057 {
16058 ++stropt;
16059 block_len = getdigits(&stropt) - 1;
16060 --stropt;
16061 }
16062 break;
16063#endif
16064 }
16065 }
16066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016067 strval = get_tv_string_chk(&argvars[1]);
16068 if (strval != NULL)
16069 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016070 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016071 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016072}
16073
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016074/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016075 * "settabvar()" function
16076 */
16077 static void
16078f_settabvar(argvars, rettv)
16079 typval_T *argvars;
16080 typval_T *rettv;
16081{
16082 tabpage_T *save_curtab;
16083 char_u *varname, *tabvarname;
16084 typval_T *varp;
16085 tabpage_T *tp;
16086
16087 rettv->vval.v_number = 0;
16088
16089 if (check_restricted() || check_secure())
16090 return;
16091
16092 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16093 varname = get_tv_string_chk(&argvars[1]);
16094 varp = &argvars[2];
16095
16096 if (tp != NULL && varname != NULL && varp != NULL)
16097 {
16098 save_curtab = curtab;
16099 goto_tabpage_tp(tp);
16100
16101 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16102 if (tabvarname != NULL)
16103 {
16104 STRCPY(tabvarname, "t:");
16105 STRCPY(tabvarname + 2, varname);
16106 set_var(tabvarname, varp, TRUE);
16107 vim_free(tabvarname);
16108 }
16109
16110 /* Restore current tabpage */
16111 if (valid_tabpage(save_curtab))
16112 goto_tabpage_tp(save_curtab);
16113 }
16114}
16115
16116/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016117 * "settabwinvar()" function
16118 */
16119 static void
16120f_settabwinvar(argvars, rettv)
16121 typval_T *argvars;
16122 typval_T *rettv;
16123{
16124 setwinvar(argvars, rettv, 1);
16125}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016126
16127/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016128 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016131f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016132 typval_T *argvars;
16133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016134{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016135 setwinvar(argvars, rettv, 0);
16136}
16137
16138/*
16139 * "setwinvar()" and "settabwinvar()" functions
16140 */
16141 static void
16142setwinvar(argvars, rettv, off)
16143 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016144 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016145 int off;
16146{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 win_T *win;
16148#ifdef FEAT_WINDOWS
16149 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016150 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151#endif
16152 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016153 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 char_u nbuf[NUMBUFLEN];
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016155 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156
16157 if (check_restricted() || check_secure())
16158 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016159
16160#ifdef FEAT_WINDOWS
16161 if (off == 1)
16162 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16163 else
16164 tp = curtab;
16165#endif
16166 win = find_win_by_nr(&argvars[off], tp);
16167 varname = get_tv_string_chk(&argvars[off + 1]);
16168 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169
16170 if (win != NULL && varname != NULL && varp != NULL)
16171 {
16172#ifdef FEAT_WINDOWS
16173 /* set curwin to be our win, temporarily */
16174 save_curwin = curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016175 save_curtab = curtab;
16176 goto_tabpage_tp(tp);
16177 if (!win_valid(win))
16178 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179 curwin = win;
16180 curbuf = curwin->w_buffer;
16181#endif
16182
16183 if (*varname == '&')
16184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016185 long numval;
16186 char_u *strval;
16187 int error = FALSE;
16188
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016190 numval = get_tv_number_chk(varp, &error);
16191 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016192 if (!error && strval != NULL)
16193 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016194 }
16195 else
16196 {
16197 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16198 if (winvarname != NULL)
16199 {
16200 STRCPY(winvarname, "w:");
16201 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016202 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 vim_free(winvarname);
16204 }
16205 }
16206
16207#ifdef FEAT_WINDOWS
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016208 /* Restore current tabpage and window, if still valid (autocomands can
16209 * make them invalid). */
16210 if (valid_tabpage(save_curtab))
16211 goto_tabpage_tp(save_curtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 if (win_valid(save_curwin))
16213 {
16214 curwin = save_curwin;
16215 curbuf = curwin->w_buffer;
16216 }
16217#endif
16218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219}
16220
16221/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016222 * "shellescape({string})" function
16223 */
16224 static void
16225f_shellescape(argvars, rettv)
16226 typval_T *argvars;
16227 typval_T *rettv;
16228{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016229 rettv->vval.v_string = vim_strsave_shellescape(
16230 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016231 rettv->v_type = VAR_STRING;
16232}
16233
16234/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016235 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 */
16237 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016238f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016239 typval_T *argvars;
16240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016241{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016242 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243
Bram Moolenaar0d660222005-01-07 21:51:51 +000016244 p = get_tv_string(&argvars[0]);
16245 rettv->vval.v_string = vim_strsave(p);
16246 simplify_filename(rettv->vval.v_string); /* simplify in place */
16247 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248}
16249
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016250#ifdef FEAT_FLOAT
16251/*
16252 * "sin()" function
16253 */
16254 static void
16255f_sin(argvars, rettv)
16256 typval_T *argvars;
16257 typval_T *rettv;
16258{
16259 float_T f;
16260
16261 rettv->v_type = VAR_FLOAT;
16262 if (get_float_arg(argvars, &f) == OK)
16263 rettv->vval.v_float = sin(f);
16264 else
16265 rettv->vval.v_float = 0.0;
16266}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016267
16268/*
16269 * "sinh()" function
16270 */
16271 static void
16272f_sinh(argvars, rettv)
16273 typval_T *argvars;
16274 typval_T *rettv;
16275{
16276 float_T f;
16277
16278 rettv->v_type = VAR_FLOAT;
16279 if (get_float_arg(argvars, &f) == OK)
16280 rettv->vval.v_float = sinh(f);
16281 else
16282 rettv->vval.v_float = 0.0;
16283}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016284#endif
16285
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286static int
16287#ifdef __BORLANDC__
16288 _RTLENTRYF
16289#endif
16290 item_compare __ARGS((const void *s1, const void *s2));
16291static int
16292#ifdef __BORLANDC__
16293 _RTLENTRYF
16294#endif
16295 item_compare2 __ARGS((const void *s1, const void *s2));
16296
16297static int item_compare_ic;
16298static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016299static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300#define ITEM_COMPARE_FAIL 999
16301
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016303 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016304 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016305 static int
16306#ifdef __BORLANDC__
16307_RTLENTRYF
16308#endif
16309item_compare(s1, s2)
16310 const void *s1;
16311 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313 char_u *p1, *p2;
16314 char_u *tofree1, *tofree2;
16315 int res;
16316 char_u numbuf1[NUMBUFLEN];
16317 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016319 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16320 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016321 if (p1 == NULL)
16322 p1 = (char_u *)"";
16323 if (p2 == NULL)
16324 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016325 if (item_compare_ic)
16326 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016328 res = STRCMP(p1, p2);
16329 vim_free(tofree1);
16330 vim_free(tofree2);
16331 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332}
16333
16334 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016335#ifdef __BORLANDC__
16336_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016338item_compare2(s1, s2)
16339 const void *s1;
16340 const void *s2;
16341{
16342 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016344 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016345 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016347 /* shortcut after failure in previous call; compare all items equal */
16348 if (item_compare_func_err)
16349 return 0;
16350
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016351 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16352 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016353 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16354 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016355
16356 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016357 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000016358 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016359 clear_tv(&argv[0]);
16360 clear_tv(&argv[1]);
16361
16362 if (res == FAIL)
16363 res = ITEM_COMPARE_FAIL;
16364 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016365 res = get_tv_number_chk(&rettv, &item_compare_func_err);
16366 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000016367 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016368 clear_tv(&rettv);
16369 return res;
16370}
16371
16372/*
16373 * "sort({list})" function
16374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016377 typval_T *argvars;
16378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379{
Bram Moolenaar33570922005-01-25 22:26:29 +000016380 list_T *l;
16381 listitem_T *li;
16382 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383 long len;
16384 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386 if (argvars[0].v_type != VAR_LIST)
16387 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 else
16389 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016390 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016391 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392 return;
16393 rettv->vval.v_list = l;
16394 rettv->v_type = VAR_LIST;
16395 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396
Bram Moolenaar0d660222005-01-07 21:51:51 +000016397 len = list_len(l);
16398 if (len <= 1)
16399 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400
Bram Moolenaar0d660222005-01-07 21:51:51 +000016401 item_compare_ic = FALSE;
16402 item_compare_func = NULL;
16403 if (argvars[1].v_type != VAR_UNKNOWN)
16404 {
16405 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016406 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016407 else
16408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016409 int error = FALSE;
16410
16411 i = get_tv_number_chk(&argvars[1], &error);
16412 if (error)
16413 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016414 if (i == 1)
16415 item_compare_ic = TRUE;
16416 else
16417 item_compare_func = get_tv_string(&argvars[1]);
16418 }
16419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016420
Bram Moolenaar0d660222005-01-07 21:51:51 +000016421 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016422 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000016423 if (ptrs == NULL)
16424 return;
16425 i = 0;
16426 for (li = l->lv_first; li != NULL; li = li->li_next)
16427 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016430 /* test the compare function */
16431 if (item_compare_func != NULL
16432 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
16433 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016434 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016435 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016436 {
16437 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016438 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000016439 item_compare_func == NULL ? item_compare : item_compare2);
16440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016441 if (!item_compare_func_err)
16442 {
16443 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000016444 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016445 l->lv_len = 0;
16446 for (i = 0; i < len; ++i)
16447 list_append(l, ptrs[i]);
16448 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016449 }
16450
16451 vim_free(ptrs);
16452 }
16453}
16454
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016455/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016456 * "soundfold({word})" function
16457 */
16458 static void
16459f_soundfold(argvars, rettv)
16460 typval_T *argvars;
16461 typval_T *rettv;
16462{
16463 char_u *s;
16464
16465 rettv->v_type = VAR_STRING;
16466 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016467#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000016468 rettv->vval.v_string = eval_soundfold(s);
16469#else
16470 rettv->vval.v_string = vim_strsave(s);
16471#endif
16472}
16473
16474/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016475 * "spellbadword()" function
16476 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016477 static void
16478f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016479 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016480 typval_T *rettv;
16481{
Bram Moolenaar4463f292005-09-25 22:20:24 +000016482 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016483 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016484 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016485
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016486 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016487 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016488
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016489#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000016490 if (argvars[0].v_type == VAR_UNKNOWN)
16491 {
16492 /* Find the start and length of the badly spelled word. */
16493 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
16494 if (len != 0)
16495 word = ml_get_cursor();
16496 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020016497 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016498 {
16499 char_u *str = get_tv_string_chk(&argvars[0]);
16500 int capcol = -1;
16501
16502 if (str != NULL)
16503 {
16504 /* Check the argument for spelling. */
16505 while (*str != NUL)
16506 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000016507 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016508 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000016509 {
16510 word = str;
16511 break;
16512 }
16513 str += len;
16514 }
16515 }
16516 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016517#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000016518
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016519 list_append_string(rettv->vval.v_list, word, len);
16520 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000016521 attr == HLF_SPB ? "bad" :
16522 attr == HLF_SPR ? "rare" :
16523 attr == HLF_SPL ? "local" :
16524 attr == HLF_SPC ? "caps" :
16525 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016526}
16527
16528/*
16529 * "spellsuggest()" function
16530 */
16531 static void
16532f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016533 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016534 typval_T *rettv;
16535{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016536#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016537 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016538 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016539 int maxcount;
16540 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016541 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016542 listitem_T *li;
16543 int need_capital = FALSE;
16544#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016545
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016546 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016547 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016548
Bram Moolenaar3c56a962006-03-12 22:19:04 +000016549#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020016550 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016551 {
16552 str = get_tv_string(&argvars[0]);
16553 if (argvars[1].v_type != VAR_UNKNOWN)
16554 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016555 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016556 if (maxcount <= 0)
16557 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000016558 if (argvars[2].v_type != VAR_UNKNOWN)
16559 {
16560 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
16561 if (typeerr)
16562 return;
16563 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016564 }
16565 else
16566 maxcount = 25;
16567
Bram Moolenaar4770d092006-01-12 23:22:24 +000016568 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016569
16570 for (i = 0; i < ga.ga_len; ++i)
16571 {
16572 str = ((char_u **)ga.ga_data)[i];
16573
16574 li = listitem_alloc();
16575 if (li == NULL)
16576 vim_free(str);
16577 else
16578 {
16579 li->li_tv.v_type = VAR_STRING;
16580 li->li_tv.v_lock = 0;
16581 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016582 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016583 }
16584 }
16585 ga_clear(&ga);
16586 }
16587#endif
16588}
16589
Bram Moolenaar0d660222005-01-07 21:51:51 +000016590 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016591f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016592 typval_T *argvars;
16593 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016594{
16595 char_u *str;
16596 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016597 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598 regmatch_T regmatch;
16599 char_u patbuf[NUMBUFLEN];
16600 char_u *save_cpo;
16601 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016602 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016603 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016604 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016605
16606 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16607 save_cpo = p_cpo;
16608 p_cpo = (char_u *)"";
16609
16610 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016611 if (argvars[1].v_type != VAR_UNKNOWN)
16612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016613 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
16614 if (pat == NULL)
16615 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016616 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016618 }
16619 if (pat == NULL || *pat == NUL)
16620 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016622 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016623 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016624 if (typeerr)
16625 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016626
Bram Moolenaar0d660222005-01-07 21:51:51 +000016627 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
16628 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016629 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016630 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016631 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016632 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016633 if (*str == NUL)
16634 match = FALSE; /* empty item at the end */
16635 else
16636 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016637 if (match)
16638 end = regmatch.startp[0];
16639 else
16640 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016641 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
16642 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016644 if (list_append_string(rettv->vval.v_list, str,
16645 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016646 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016647 }
16648 if (!match)
16649 break;
16650 /* Advance to just after the match. */
16651 if (regmatch.endp[0] > str)
16652 col = 0;
16653 else
16654 {
16655 /* Don't get stuck at the same match. */
16656#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016657 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016658#else
16659 col = 1;
16660#endif
16661 }
16662 str = regmatch.endp[0];
16663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664
Bram Moolenaar0d660222005-01-07 21:51:51 +000016665 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667
Bram Moolenaar0d660222005-01-07 21:51:51 +000016668 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669}
16670
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016671#ifdef FEAT_FLOAT
16672/*
16673 * "sqrt()" function
16674 */
16675 static void
16676f_sqrt(argvars, rettv)
16677 typval_T *argvars;
16678 typval_T *rettv;
16679{
16680 float_T f;
16681
16682 rettv->v_type = VAR_FLOAT;
16683 if (get_float_arg(argvars, &f) == OK)
16684 rettv->vval.v_float = sqrt(f);
16685 else
16686 rettv->vval.v_float = 0.0;
16687}
16688
16689/*
16690 * "str2float()" function
16691 */
16692 static void
16693f_str2float(argvars, rettv)
16694 typval_T *argvars;
16695 typval_T *rettv;
16696{
16697 char_u *p = skipwhite(get_tv_string(&argvars[0]));
16698
16699 if (*p == '+')
16700 p = skipwhite(p + 1);
16701 (void)string2float(p, &rettv->vval.v_float);
16702 rettv->v_type = VAR_FLOAT;
16703}
16704#endif
16705
Bram Moolenaar2c932302006-03-18 21:42:09 +000016706/*
16707 * "str2nr()" function
16708 */
16709 static void
16710f_str2nr(argvars, rettv)
16711 typval_T *argvars;
16712 typval_T *rettv;
16713{
16714 int base = 10;
16715 char_u *p;
16716 long n;
16717
16718 if (argvars[1].v_type != VAR_UNKNOWN)
16719 {
16720 base = get_tv_number(&argvars[1]);
16721 if (base != 8 && base != 10 && base != 16)
16722 {
16723 EMSG(_(e_invarg));
16724 return;
16725 }
16726 }
16727
16728 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016729 if (*p == '+')
16730 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000016731 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
16732 rettv->vval.v_number = n;
16733}
16734
Bram Moolenaar071d4272004-06-13 20:20:40 +000016735#ifdef HAVE_STRFTIME
16736/*
16737 * "strftime({format}[, {time}])" function
16738 */
16739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016740f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016741 typval_T *argvars;
16742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743{
16744 char_u result_buf[256];
16745 struct tm *curtime;
16746 time_t seconds;
16747 char_u *p;
16748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016749 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016751 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016752 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753 seconds = time(NULL);
16754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016755 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016756 curtime = localtime(&seconds);
16757 /* MSVC returns NULL for an invalid value of seconds. */
16758 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016759 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016760 else
16761 {
16762# ifdef FEAT_MBYTE
16763 vimconv_T conv;
16764 char_u *enc;
16765
16766 conv.vc_type = CONV_NONE;
16767 enc = enc_locale();
16768 convert_setup(&conv, p_enc, enc);
16769 if (conv.vc_type != CONV_NONE)
16770 p = string_convert(&conv, p, NULL);
16771# endif
16772 if (p != NULL)
16773 (void)strftime((char *)result_buf, sizeof(result_buf),
16774 (char *)p, curtime);
16775 else
16776 result_buf[0] = NUL;
16777
16778# ifdef FEAT_MBYTE
16779 if (conv.vc_type != CONV_NONE)
16780 vim_free(p);
16781 convert_setup(&conv, enc, p_enc);
16782 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016783 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 else
16785# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016786 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787
16788# ifdef FEAT_MBYTE
16789 /* Release conversion descriptors */
16790 convert_setup(&conv, NULL, NULL);
16791 vim_free(enc);
16792# endif
16793 }
16794}
16795#endif
16796
16797/*
16798 * "stridx()" function
16799 */
16800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016801f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016802 typval_T *argvars;
16803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016804{
16805 char_u buf[NUMBUFLEN];
16806 char_u *needle;
16807 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000016808 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000016810 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016812 needle = get_tv_string_chk(&argvars[1]);
16813 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000016814 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016815 if (needle == NULL || haystack == NULL)
16816 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817
Bram Moolenaar33570922005-01-25 22:26:29 +000016818 if (argvars[2].v_type != VAR_UNKNOWN)
16819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016820 int error = FALSE;
16821
16822 start_idx = get_tv_number_chk(&argvars[2], &error);
16823 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000016824 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016825 if (start_idx >= 0)
16826 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000016827 }
16828
16829 pos = (char_u *)strstr((char *)haystack, (char *)needle);
16830 if (pos != NULL)
16831 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832}
16833
16834/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016835 * "string()" function
16836 */
16837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016838f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016839 typval_T *argvars;
16840 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016841{
16842 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016843 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016845 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016846 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016847 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016848 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016849 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850}
16851
16852/*
16853 * "strlen()" function
16854 */
16855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016856f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016857 typval_T *argvars;
16858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016860 rettv->vval.v_number = (varnumber_T)(STRLEN(
16861 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016862}
16863
16864/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016865 * "strchars()" function
16866 */
16867 static void
16868f_strchars(argvars, rettv)
16869 typval_T *argvars;
16870 typval_T *rettv;
16871{
16872 char_u *s = get_tv_string(&argvars[0]);
16873#ifdef FEAT_MBYTE
16874 varnumber_T len = 0;
16875
16876 while (*s != NUL)
16877 {
16878 mb_cptr2char_adv(&s);
16879 ++len;
16880 }
16881 rettv->vval.v_number = len;
16882#else
16883 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
16884#endif
16885}
16886
16887/*
Bram Moolenaardc536092010-07-18 15:45:49 +020016888 * "strdisplaywidth()" function
16889 */
16890 static void
16891f_strdisplaywidth(argvars, rettv)
16892 typval_T *argvars;
16893 typval_T *rettv;
16894{
16895 char_u *s = get_tv_string(&argvars[0]);
16896 int col = 0;
16897
16898 if (argvars[1].v_type != VAR_UNKNOWN)
16899 col = get_tv_number(&argvars[1]);
16900
Bram Moolenaar8a09b982010-07-22 22:20:57 +020016901 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020016902}
16903
16904/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020016905 * "strwidth()" function
16906 */
16907 static void
16908f_strwidth(argvars, rettv)
16909 typval_T *argvars;
16910 typval_T *rettv;
16911{
16912 char_u *s = get_tv_string(&argvars[0]);
16913
16914 rettv->vval.v_number = (varnumber_T)(
16915#ifdef FEAT_MBYTE
16916 mb_string2cells(s, -1)
16917#else
16918 STRLEN(s)
16919#endif
16920 );
16921}
16922
16923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 * "strpart()" function
16925 */
16926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016927f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016928 typval_T *argvars;
16929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016930{
16931 char_u *p;
16932 int n;
16933 int len;
16934 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016935 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016937 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 slen = (int)STRLEN(p);
16939
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016940 n = get_tv_number_chk(&argvars[1], &error);
16941 if (error)
16942 len = 0;
16943 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016944 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016945 else
16946 len = slen - n; /* default len: all bytes that are available. */
16947
16948 /*
16949 * Only return the overlap between the specified part and the actual
16950 * string.
16951 */
16952 if (n < 0)
16953 {
16954 len += n;
16955 n = 0;
16956 }
16957 else if (n > slen)
16958 n = slen;
16959 if (len < 0)
16960 len = 0;
16961 else if (n + len > slen)
16962 len = slen - n;
16963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016964 rettv->v_type = VAR_STRING;
16965 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966}
16967
16968/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969 * "strridx()" function
16970 */
16971 static void
16972f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016973 typval_T *argvars;
16974 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975{
16976 char_u buf[NUMBUFLEN];
16977 char_u *needle;
16978 char_u *haystack;
16979 char_u *rest;
16980 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016981 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016983 needle = get_tv_string_chk(&argvars[1]);
16984 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016985
16986 rettv->vval.v_number = -1;
16987 if (needle == NULL || haystack == NULL)
16988 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016989
16990 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016991 if (argvars[2].v_type != VAR_UNKNOWN)
16992 {
16993 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016994 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000016995 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016996 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000016997 }
16998 else
16999 end_idx = haystack_len;
17000
Bram Moolenaar0d660222005-01-07 21:51:51 +000017001 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017002 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017003 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017004 lastmatch = haystack + end_idx;
17005 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017007 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017008 for (rest = haystack; *rest != '\0'; ++rest)
17009 {
17010 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017011 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012 break;
17013 lastmatch = rest;
17014 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017015 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017016
17017 if (lastmatch == NULL)
17018 rettv->vval.v_number = -1;
17019 else
17020 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17021}
17022
17023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 * "strtrans()" function
17025 */
17026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017027f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017028 typval_T *argvars;
17029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017031 rettv->v_type = VAR_STRING;
17032 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033}
17034
17035/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017036 * "submatch()" function
17037 */
17038 static void
17039f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017040 typval_T *argvars;
17041 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017042{
17043 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017044 rettv->vval.v_string =
17045 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046}
17047
17048/*
17049 * "substitute()" function
17050 */
17051 static void
17052f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017053 typval_T *argvars;
17054 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055{
17056 char_u patbuf[NUMBUFLEN];
17057 char_u subbuf[NUMBUFLEN];
17058 char_u flagsbuf[NUMBUFLEN];
17059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017060 char_u *str = get_tv_string_chk(&argvars[0]);
17061 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17062 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17063 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17064
Bram Moolenaar0d660222005-01-07 21:51:51 +000017065 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017066 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17067 rettv->vval.v_string = NULL;
17068 else
17069 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070}
17071
17072/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017073 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017076f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017077 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079{
17080 int id = 0;
17081#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017082 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 long col;
17084 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017085 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017087 lnum = get_tv_lnum(argvars); /* -1 on type error */
17088 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17089 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017091 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017092 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017093 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094#endif
17095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017096 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097}
17098
17099/*
17100 * "synIDattr(id, what [, mode])" function
17101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017103f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017104 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106{
17107 char_u *p = NULL;
17108#ifdef FEAT_SYN_HL
17109 int id;
17110 char_u *what;
17111 char_u *mode;
17112 char_u modebuf[NUMBUFLEN];
17113 int modec;
17114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017115 id = get_tv_number(&argvars[0]);
17116 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017117 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017119 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017121 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 modec = 0; /* replace invalid with current */
17123 }
17124 else
17125 {
17126#ifdef FEAT_GUI
17127 if (gui.in_use)
17128 modec = 'g';
17129 else
17130#endif
17131 if (t_colors > 1)
17132 modec = 'c';
17133 else
17134 modec = 't';
17135 }
17136
17137
17138 switch (TOLOWER_ASC(what[0]))
17139 {
17140 case 'b':
17141 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17142 p = highlight_color(id, what, modec);
17143 else /* bold */
17144 p = highlight_has_attr(id, HL_BOLD, modec);
17145 break;
17146
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017147 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017148 p = highlight_color(id, what, modec);
17149 break;
17150
17151 case 'i':
17152 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17153 p = highlight_has_attr(id, HL_INVERSE, modec);
17154 else /* italic */
17155 p = highlight_has_attr(id, HL_ITALIC, modec);
17156 break;
17157
17158 case 'n': /* name */
17159 p = get_highlight_name(NULL, id - 1);
17160 break;
17161
17162 case 'r': /* reverse */
17163 p = highlight_has_attr(id, HL_INVERSE, modec);
17164 break;
17165
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017166 case 's':
17167 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17168 p = highlight_color(id, what, modec);
17169 else /* standout */
17170 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 break;
17172
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017173 case 'u':
17174 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17175 /* underline */
17176 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17177 else
17178 /* undercurl */
17179 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 break;
17181 }
17182
17183 if (p != NULL)
17184 p = vim_strsave(p);
17185#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017186 rettv->v_type = VAR_STRING;
17187 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188}
17189
17190/*
17191 * "synIDtrans(id)" function
17192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017194f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017195 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197{
17198 int id;
17199
17200#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202
17203 if (id > 0)
17204 id = syn_get_final_id(id);
17205 else
17206#endif
17207 id = 0;
17208
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017209 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210}
17211
17212/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017213 * "synconcealed(lnum, col)" function
17214 */
17215 static void
17216f_synconcealed(argvars, rettv)
17217 typval_T *argvars UNUSED;
17218 typval_T *rettv;
17219{
17220#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17221 long lnum;
17222 long col;
17223 int syntax_flags = 0;
17224 int cchar;
17225 int matchid = 0;
17226 char_u str[NUMBUFLEN];
17227#endif
17228
17229 rettv->v_type = VAR_LIST;
17230 rettv->vval.v_list = NULL;
17231
17232#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17233 lnum = get_tv_lnum(argvars); /* -1 on type error */
17234 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17235
17236 vim_memset(str, NUL, sizeof(str));
17237
17238 if (rettv_list_alloc(rettv) != FAIL)
17239 {
17240 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17241 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17242 && curwin->w_p_cole > 0)
17243 {
17244 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17245 syntax_flags = get_syntax_info(&matchid);
17246
17247 /* get the conceal character */
17248 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17249 {
17250 cchar = syn_get_sub_char();
17251 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17252 cchar = lcs_conceal;
17253 if (cchar != NUL)
17254 {
17255# ifdef FEAT_MBYTE
17256 if (has_mbyte)
17257 (*mb_char2bytes)(cchar, str);
17258 else
17259# endif
17260 str[0] = cchar;
17261 }
17262 }
17263 }
17264
17265 list_append_number(rettv->vval.v_list,
17266 (syntax_flags & HL_CONCEAL) != 0);
17267 /* -1 to auto-determine strlen */
17268 list_append_string(rettv->vval.v_list, str, -1);
17269 list_append_number(rettv->vval.v_list, matchid);
17270 }
17271#endif
17272}
17273
17274/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017275 * "synstack(lnum, col)" function
17276 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017277 static void
17278f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017279 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017280 typval_T *rettv;
17281{
17282#ifdef FEAT_SYN_HL
17283 long lnum;
17284 long col;
17285 int i;
17286 int id;
17287#endif
17288
17289 rettv->v_type = VAR_LIST;
17290 rettv->vval.v_list = NULL;
17291
17292#ifdef FEAT_SYN_HL
17293 lnum = get_tv_lnum(argvars); /* -1 on type error */
17294 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17295
17296 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017297 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017298 && rettv_list_alloc(rettv) != FAIL)
17299 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017300 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017301 for (i = 0; ; ++i)
17302 {
17303 id = syn_get_stack_item(i);
17304 if (id < 0)
17305 break;
17306 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17307 break;
17308 }
17309 }
17310#endif
17311}
17312
17313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017314 * "system()" function
17315 */
17316 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017317f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017318 typval_T *argvars;
17319 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017321 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017323 char_u *infile = NULL;
17324 char_u buf[NUMBUFLEN];
17325 int err = FALSE;
17326 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017327
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017328 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017329 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017330
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017331 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017332 {
17333 /*
17334 * Write the string to a temp file, to be used for input of the shell
17335 * command.
17336 */
17337 if ((infile = vim_tempname('i')) == NULL)
17338 {
17339 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017340 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017341 }
17342
17343 fd = mch_fopen((char *)infile, WRITEBIN);
17344 if (fd == NULL)
17345 {
17346 EMSG2(_(e_notopen), infile);
17347 goto done;
17348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017349 p = get_tv_string_buf_chk(&argvars[1], buf);
17350 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017351 {
17352 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017353 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017354 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017355 if (fwrite(p, STRLEN(p), 1, fd) != 1)
17356 err = TRUE;
17357 if (fclose(fd) != 0)
17358 err = TRUE;
17359 if (err)
17360 {
17361 EMSG(_("E677: Error writing temp file"));
17362 goto done;
17363 }
17364 }
17365
Bram Moolenaare580b0c2006-03-21 21:33:03 +000017366 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
17367 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017368
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369#ifdef USE_CR
17370 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017371 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 {
17373 char_u *s;
17374
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017375 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017376 {
17377 if (*s == CAR)
17378 *s = NL;
17379 }
17380 }
17381#else
17382# ifdef USE_CRNL
17383 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017384 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385 {
17386 char_u *s, *d;
17387
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017388 d = res;
17389 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017390 {
17391 if (s[0] == CAR && s[1] == NL)
17392 ++s;
17393 *d++ = *s;
17394 }
17395 *d = NUL;
17396 }
17397# endif
17398#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017399
17400done:
17401 if (infile != NULL)
17402 {
17403 mch_remove(infile);
17404 vim_free(infile);
17405 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017406 rettv->v_type = VAR_STRING;
17407 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408}
17409
17410/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017411 * "tabpagebuflist()" function
17412 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017413 static void
17414f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017415 typval_T *argvars UNUSED;
17416 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017417{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017418#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017419 tabpage_T *tp;
17420 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017421
17422 if (argvars[0].v_type == VAR_UNKNOWN)
17423 wp = firstwin;
17424 else
17425 {
17426 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17427 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000017428 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017429 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017430 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017431 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017432 for (; wp != NULL; wp = wp->w_next)
17433 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017434 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017435 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017436 }
17437#endif
17438}
17439
17440
17441/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017442 * "tabpagenr()" function
17443 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017444 static void
17445f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017446 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017447 typval_T *rettv;
17448{
17449 int nr = 1;
17450#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017451 char_u *arg;
17452
17453 if (argvars[0].v_type != VAR_UNKNOWN)
17454 {
17455 arg = get_tv_string_chk(&argvars[0]);
17456 nr = 0;
17457 if (arg != NULL)
17458 {
17459 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000017460 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017461 else
17462 EMSG2(_(e_invexpr2), arg);
17463 }
17464 }
17465 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000017466 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017467#endif
17468 rettv->vval.v_number = nr;
17469}
17470
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017471
17472#ifdef FEAT_WINDOWS
17473static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
17474
17475/*
17476 * Common code for tabpagewinnr() and winnr().
17477 */
17478 static int
17479get_winnr(tp, argvar)
17480 tabpage_T *tp;
17481 typval_T *argvar;
17482{
17483 win_T *twin;
17484 int nr = 1;
17485 win_T *wp;
17486 char_u *arg;
17487
17488 twin = (tp == curtab) ? curwin : tp->tp_curwin;
17489 if (argvar->v_type != VAR_UNKNOWN)
17490 {
17491 arg = get_tv_string_chk(argvar);
17492 if (arg == NULL)
17493 nr = 0; /* type error; errmsg already given */
17494 else if (STRCMP(arg, "$") == 0)
17495 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
17496 else if (STRCMP(arg, "#") == 0)
17497 {
17498 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
17499 if (twin == NULL)
17500 nr = 0;
17501 }
17502 else
17503 {
17504 EMSG2(_(e_invexpr2), arg);
17505 nr = 0;
17506 }
17507 }
17508
17509 if (nr > 0)
17510 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
17511 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017512 {
17513 if (wp == NULL)
17514 {
17515 /* didn't find it in this tabpage */
17516 nr = 0;
17517 break;
17518 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017519 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000017520 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017521 return nr;
17522}
17523#endif
17524
17525/*
17526 * "tabpagewinnr()" function
17527 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017528 static void
17529f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017530 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000017531 typval_T *rettv;
17532{
17533 int nr = 1;
17534#ifdef FEAT_WINDOWS
17535 tabpage_T *tp;
17536
17537 tp = find_tabpage((int)get_tv_number(&argvars[0]));
17538 if (tp == NULL)
17539 nr = 0;
17540 else
17541 nr = get_winnr(tp, &argvars[1]);
17542#endif
17543 rettv->vval.v_number = nr;
17544}
17545
17546
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000017547/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017548 * "tagfiles()" function
17549 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017550 static void
17551f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017552 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017553 typval_T *rettv;
17554{
17555 char_u fname[MAXPATHL + 1];
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017556 tagname_T tn;
17557 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017558
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017559 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017560 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017561
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017562 for (first = TRUE; ; first = FALSE)
17563 if (get_tagfname(&tn, first, fname) == FAIL
17564 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017565 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017566 tagname_free(&tn);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000017567}
17568
17569/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000017570 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017571 */
17572 static void
17573f_taglist(argvars, rettv)
17574 typval_T *argvars;
17575 typval_T *rettv;
17576{
17577 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017578
17579 tag_pattern = get_tv_string(&argvars[0]);
17580
17581 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017582 if (*tag_pattern == NUL)
17583 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017584
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017585 if (rettv_list_alloc(rettv) == OK)
17586 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000017587}
17588
17589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017590 * "tempname()" function
17591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017593f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017594 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596{
17597 static int x = 'A';
17598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017599 rettv->v_type = VAR_STRING;
17600 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017601
17602 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17603 * names. Skip 'I' and 'O', they are used for shell redirection. */
17604 do
17605 {
17606 if (x == 'Z')
17607 x = '0';
17608 else if (x == '9')
17609 x = 'A';
17610 else
17611 {
17612#ifdef EBCDIC
17613 if (x == 'I')
17614 x = 'J';
17615 else if (x == 'R')
17616 x = 'S';
17617 else
17618#endif
17619 ++x;
17620 }
17621 } while (x == 'I' || x == 'O');
17622}
17623
17624/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000017625 * "test(list)" function: Just checking the walls...
17626 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000017627 static void
17628f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017629 typval_T *argvars UNUSED;
17630 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000017631{
17632 /* Used for unit testing. Change the code below to your liking. */
17633#if 0
17634 listitem_T *li;
17635 list_T *l;
17636 char_u *bad, *good;
17637
17638 if (argvars[0].v_type != VAR_LIST)
17639 return;
17640 l = argvars[0].vval.v_list;
17641 if (l == NULL)
17642 return;
17643 li = l->lv_first;
17644 if (li == NULL)
17645 return;
17646 bad = get_tv_string(&li->li_tv);
17647 li = li->li_next;
17648 if (li == NULL)
17649 return;
17650 good = get_tv_string(&li->li_tv);
17651 rettv->vval.v_number = test_edit_score(bad, good);
17652#endif
17653}
17654
Bram Moolenaardb7c6862010-05-21 16:33:48 +020017655#ifdef FEAT_FLOAT
17656/*
17657 * "tan()" function
17658 */
17659 static void
17660f_tan(argvars, rettv)
17661 typval_T *argvars;
17662 typval_T *rettv;
17663{
17664 float_T f;
17665
17666 rettv->v_type = VAR_FLOAT;
17667 if (get_float_arg(argvars, &f) == OK)
17668 rettv->vval.v_float = tan(f);
17669 else
17670 rettv->vval.v_float = 0.0;
17671}
17672
17673/*
17674 * "tanh()" function
17675 */
17676 static void
17677f_tanh(argvars, rettv)
17678 typval_T *argvars;
17679 typval_T *rettv;
17680{
17681 float_T f;
17682
17683 rettv->v_type = VAR_FLOAT;
17684 if (get_float_arg(argvars, &f) == OK)
17685 rettv->vval.v_float = tanh(f);
17686 else
17687 rettv->vval.v_float = 0.0;
17688}
17689#endif
17690
Bram Moolenaard52d9742005-08-21 22:20:28 +000017691/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692 * "tolower(string)" function
17693 */
17694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017695f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017696 typval_T *argvars;
17697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698{
17699 char_u *p;
17700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017701 p = vim_strsave(get_tv_string(&argvars[0]));
17702 rettv->v_type = VAR_STRING;
17703 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017704
17705 if (p != NULL)
17706 while (*p != NUL)
17707 {
17708#ifdef FEAT_MBYTE
17709 int l;
17710
17711 if (enc_utf8)
17712 {
17713 int c, lc;
17714
17715 c = utf_ptr2char(p);
17716 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017717 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017718 /* TODO: reallocate string when byte count changes. */
17719 if (utf_char2len(lc) == l)
17720 utf_char2bytes(lc, p);
17721 p += l;
17722 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017723 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 p += l; /* skip multi-byte character */
17725 else
17726#endif
17727 {
17728 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
17729 ++p;
17730 }
17731 }
17732}
17733
17734/*
17735 * "toupper(string)" function
17736 */
17737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017738f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017739 typval_T *argvars;
17740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017742 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017743 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744}
17745
17746/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000017747 * "tr(string, fromstr, tostr)" function
17748 */
17749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017750f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017751 typval_T *argvars;
17752 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017753{
17754 char_u *instr;
17755 char_u *fromstr;
17756 char_u *tostr;
17757 char_u *p;
17758#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000017759 int inlen;
17760 int fromlen;
17761 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017762 int idx;
17763 char_u *cpstr;
17764 int cplen;
17765 int first = TRUE;
17766#endif
17767 char_u buf[NUMBUFLEN];
17768 char_u buf2[NUMBUFLEN];
17769 garray_T ga;
17770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017771 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017772 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
17773 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017774
17775 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017776 rettv->v_type = VAR_STRING;
17777 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017778 if (fromstr == NULL || tostr == NULL)
17779 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000017780 ga_init2(&ga, (int)sizeof(char), 80);
17781
17782#ifdef FEAT_MBYTE
17783 if (!has_mbyte)
17784#endif
17785 /* not multi-byte: fromstr and tostr must be the same length */
17786 if (STRLEN(fromstr) != STRLEN(tostr))
17787 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017788#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000017789error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017790#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000017791 EMSG2(_(e_invarg2), fromstr);
17792 ga_clear(&ga);
17793 return;
17794 }
17795
17796 /* fromstr and tostr have to contain the same number of chars */
17797 while (*instr != NUL)
17798 {
17799#ifdef FEAT_MBYTE
17800 if (has_mbyte)
17801 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017802 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017803 cpstr = instr;
17804 cplen = inlen;
17805 idx = 0;
17806 for (p = fromstr; *p != NUL; p += fromlen)
17807 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017808 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017809 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
17810 {
17811 for (p = tostr; *p != NUL; p += tolen)
17812 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017813 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017814 if (idx-- == 0)
17815 {
17816 cplen = tolen;
17817 cpstr = p;
17818 break;
17819 }
17820 }
17821 if (*p == NUL) /* tostr is shorter than fromstr */
17822 goto error;
17823 break;
17824 }
17825 ++idx;
17826 }
17827
17828 if (first && cpstr == instr)
17829 {
17830 /* Check that fromstr and tostr have the same number of
17831 * (multi-byte) characters. Done only once when a character
17832 * of instr doesn't appear in fromstr. */
17833 first = FALSE;
17834 for (p = tostr; *p != NUL; p += tolen)
17835 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017836 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017837 --idx;
17838 }
17839 if (idx != 0)
17840 goto error;
17841 }
17842
17843 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000017844 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000017845 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017846
17847 instr += inlen;
17848 }
17849 else
17850#endif
17851 {
17852 /* When not using multi-byte chars we can do it faster. */
17853 p = vim_strchr(fromstr, *instr);
17854 if (p != NULL)
17855 ga_append(&ga, tostr[p - fromstr]);
17856 else
17857 ga_append(&ga, *instr);
17858 ++instr;
17859 }
17860 }
17861
Bram Moolenaar61b974b2006-12-05 09:32:29 +000017862 /* add a terminating NUL */
17863 ga_grow(&ga, 1);
17864 ga_append(&ga, NUL);
17865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017866 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000017867}
17868
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017869#ifdef FEAT_FLOAT
17870/*
17871 * "trunc({float})" function
17872 */
17873 static void
17874f_trunc(argvars, rettv)
17875 typval_T *argvars;
17876 typval_T *rettv;
17877{
17878 float_T f;
17879
17880 rettv->v_type = VAR_FLOAT;
17881 if (get_float_arg(argvars, &f) == OK)
17882 /* trunc() is not in C90, use floor() or ceil() instead. */
17883 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
17884 else
17885 rettv->vval.v_float = 0.0;
17886}
17887#endif
17888
Bram Moolenaar8299df92004-07-10 09:47:34 +000017889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 * "type(expr)" function
17891 */
17892 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017893f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017894 typval_T *argvars;
17895 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017897 int n;
17898
17899 switch (argvars[0].v_type)
17900 {
17901 case VAR_NUMBER: n = 0; break;
17902 case VAR_STRING: n = 1; break;
17903 case VAR_FUNC: n = 2; break;
17904 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017905 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017906#ifdef FEAT_FLOAT
17907 case VAR_FLOAT: n = 5; break;
17908#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000017909 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
17910 }
17911 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912}
17913
17914/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017915 * "undofile(name)" function
17916 */
17917 static void
17918f_undofile(argvars, rettv)
17919 typval_T *argvars;
17920 typval_T *rettv;
17921{
17922 rettv->v_type = VAR_STRING;
17923#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020017924 {
17925 char_u *ffname = FullName_save(get_tv_string(&argvars[0]), FALSE);
17926
17927 if (ffname != NULL)
17928 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
17929 vim_free(ffname);
17930 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020017931#else
17932 rettv->vval.v_string = NULL;
17933#endif
17934}
17935
17936/*
Bram Moolenaara800b422010-06-27 01:15:55 +020017937 * "undotree()" function
17938 */
17939 static void
17940f_undotree(argvars, rettv)
17941 typval_T *argvars UNUSED;
17942 typval_T *rettv;
17943{
17944 if (rettv_dict_alloc(rettv) == OK)
17945 {
17946 dict_T *dict = rettv->vval.v_dict;
17947 list_T *list;
17948
Bram Moolenaar730cde92010-06-27 05:18:54 +020017949 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017950 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017951 dict_add_nr_str(dict, "save_last",
17952 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017953 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
17954 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020017955 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020017956
17957 list = list_alloc();
17958 if (list != NULL)
17959 {
17960 u_eval_tree(curbuf->b_u_oldhead, list);
17961 dict_add_list(dict, "entries", list);
17962 }
17963 }
17964}
17965
17966/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017967 * "values(dict)" function
17968 */
17969 static void
17970f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017971 typval_T *argvars;
17972 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017973{
17974 dict_list(argvars, rettv, 1);
17975}
17976
17977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 * "virtcol(string)" function
17979 */
17980 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017981f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017982 typval_T *argvars;
17983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017984{
17985 colnr_T vcol = 0;
17986 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017987 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017988
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017989 fp = var2fpos(&argvars[0], FALSE, &fnum);
17990 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
17991 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 {
17993 getvvcol(curwin, fp, NULL, NULL, &vcol);
17994 ++vcol;
17995 }
17996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017997 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998}
17999
18000/*
18001 * "visualmode()" function
18002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018004f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018005 typval_T *argvars UNUSED;
18006 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007{
18008#ifdef FEAT_VISUAL
18009 char_u str[2];
18010
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018011 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012 str[0] = curbuf->b_visual_mode_eval;
18013 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018014 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015
18016 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018017 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019#endif
18020}
18021
18022/*
18023 * "winbufnr(nr)" function
18024 */
18025 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018026f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018027 typval_T *argvars;
18028 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029{
18030 win_T *wp;
18031
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018032 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018034 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018036 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018037}
18038
18039/*
18040 * "wincol()" function
18041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018043f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018044 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046{
18047 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018048 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049}
18050
18051/*
18052 * "winheight(nr)" function
18053 */
18054 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018055f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018056 typval_T *argvars;
18057 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058{
18059 win_T *wp;
18060
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018061 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018063 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018065 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066}
18067
18068/*
18069 * "winline()" function
18070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018072f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018073 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075{
18076 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018077 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078}
18079
18080/*
18081 * "winnr()" function
18082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018084f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018085 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087{
18088 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018089
Bram Moolenaar071d4272004-06-13 20:20:40 +000018090#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018091 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018093 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094}
18095
18096/*
18097 * "winrestcmd()" function
18098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018100f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018101 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103{
18104#ifdef FEAT_WINDOWS
18105 win_T *wp;
18106 int winnr = 1;
18107 garray_T ga;
18108 char_u buf[50];
18109
18110 ga_init2(&ga, (int)sizeof(char), 70);
18111 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18112 {
18113 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18114 ga_concat(&ga, buf);
18115# ifdef FEAT_VERTSPLIT
18116 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18117 ga_concat(&ga, buf);
18118# endif
18119 ++winnr;
18120 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018121 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018123 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018125 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018126#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018127 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128}
18129
18130/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018131 * "winrestview()" function
18132 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018133 static void
18134f_winrestview(argvars, rettv)
18135 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018136 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018137{
18138 dict_T *dict;
18139
18140 if (argvars[0].v_type != VAR_DICT
18141 || (dict = argvars[0].vval.v_dict) == NULL)
18142 EMSG(_(e_invarg));
18143 else
18144 {
18145 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18146 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18147#ifdef FEAT_VIRTUALEDIT
18148 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18149#endif
18150 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018151 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018152
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018153 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018154#ifdef FEAT_DIFF
18155 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18156#endif
18157 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18158 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18159
18160 check_cursor();
18161 changed_cline_bef_curs();
18162 invalidate_botline();
18163 redraw_later(VALID);
18164
18165 if (curwin->w_topline == 0)
18166 curwin->w_topline = 1;
18167 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18168 curwin->w_topline = curbuf->b_ml.ml_line_count;
18169#ifdef FEAT_DIFF
18170 check_topfill(curwin, TRUE);
18171#endif
18172 }
18173}
18174
18175/*
18176 * "winsaveview()" function
18177 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018178 static void
18179f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018180 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018181 typval_T *rettv;
18182{
18183 dict_T *dict;
18184
Bram Moolenaara800b422010-06-27 01:15:55 +020018185 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018186 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018187 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018188
18189 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18190 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18191#ifdef FEAT_VIRTUALEDIT
18192 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18193#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018194 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018195 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18196
18197 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18198#ifdef FEAT_DIFF
18199 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18200#endif
18201 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18202 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18203}
18204
18205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206 * "winwidth(nr)" function
18207 */
18208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018209f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018210 typval_T *argvars;
18211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212{
18213 win_T *wp;
18214
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018215 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018217 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 else
18219#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018220 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018222 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223#endif
18224}
18225
Bram Moolenaar071d4272004-06-13 20:20:40 +000018226/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018227 * "writefile()" function
18228 */
18229 static void
18230f_writefile(argvars, rettv)
18231 typval_T *argvars;
18232 typval_T *rettv;
18233{
18234 int binary = FALSE;
18235 char_u *fname;
18236 FILE *fd;
18237 listitem_T *li;
18238 char_u *s;
18239 int ret = 0;
18240 int c;
18241
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018242 if (check_restricted() || check_secure())
18243 return;
18244
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018245 if (argvars[0].v_type != VAR_LIST)
18246 {
18247 EMSG2(_(e_listarg), "writefile()");
18248 return;
18249 }
18250 if (argvars[0].vval.v_list == NULL)
18251 return;
18252
18253 if (argvars[2].v_type != VAR_UNKNOWN
18254 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18255 binary = TRUE;
18256
18257 /* Always open the file in binary mode, library functions have a mind of
18258 * their own about CR-LF conversion. */
18259 fname = get_tv_string(&argvars[1]);
18260 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18261 {
18262 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18263 ret = -1;
18264 }
18265 else
18266 {
18267 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18268 li = li->li_next)
18269 {
18270 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18271 {
18272 if (*s == '\n')
18273 c = putc(NUL, fd);
18274 else
18275 c = putc(*s, fd);
18276 if (c == EOF)
18277 {
18278 ret = -1;
18279 break;
18280 }
18281 }
18282 if (!binary || li->li_next != NULL)
18283 if (putc('\n', fd) == EOF)
18284 {
18285 ret = -1;
18286 break;
18287 }
18288 if (ret < 0)
18289 {
18290 EMSG(_(e_write));
18291 break;
18292 }
18293 }
18294 fclose(fd);
18295 }
18296
18297 rettv->vval.v_number = ret;
18298}
18299
18300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018302 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303 */
18304 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018305var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000018306 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018307 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018308 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018310 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018312 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313
Bram Moolenaara5525202006-03-02 22:52:09 +000018314 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018315 if (varp->v_type == VAR_LIST)
18316 {
18317 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018318 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000018319 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000018320 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018321
18322 l = varp->vval.v_list;
18323 if (l == NULL)
18324 return NULL;
18325
18326 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018327 pos.lnum = list_find_nr(l, 0L, &error);
18328 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018329 return NULL; /* invalid line number */
18330
18331 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018332 pos.col = list_find_nr(l, 1L, &error);
18333 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018334 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018335 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000018336
18337 /* We accept "$" for the column number: last column. */
18338 li = list_find(l, 1L);
18339 if (li != NULL && li->li_tv.v_type == VAR_STRING
18340 && li->li_tv.vval.v_string != NULL
18341 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
18342 pos.col = len + 1;
18343
Bram Moolenaara5525202006-03-02 22:52:09 +000018344 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000018345 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018346 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000018347 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018348
Bram Moolenaara5525202006-03-02 22:52:09 +000018349#ifdef FEAT_VIRTUALEDIT
18350 /* Get the virtual offset. Defaults to zero. */
18351 pos.coladd = list_find_nr(l, 2L, &error);
18352 if (error)
18353 pos.coladd = 0;
18354#endif
18355
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018356 return &pos;
18357 }
18358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018359 name = get_tv_string_chk(varp);
18360 if (name == NULL)
18361 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018362 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018363 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000018364#ifdef FEAT_VISUAL
18365 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
18366 {
18367 if (VIsual_active)
18368 return &VIsual;
18369 return &curwin->w_cursor;
18370 }
18371#endif
18372 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018374 pp = getmark_fnum(name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
18376 return NULL;
18377 return pp;
18378 }
Bram Moolenaara5525202006-03-02 22:52:09 +000018379
18380#ifdef FEAT_VIRTUALEDIT
18381 pos.coladd = 0;
18382#endif
18383
Bram Moolenaar477933c2007-07-17 14:32:23 +000018384 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018385 {
18386 pos.col = 0;
18387 if (name[1] == '0') /* "w0": first visible line */
18388 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018389 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018390 pos.lnum = curwin->w_topline;
18391 return &pos;
18392 }
18393 else if (name[1] == '$') /* "w$": last visible line */
18394 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000018395 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000018396 pos.lnum = curwin->w_botline - 1;
18397 return &pos;
18398 }
18399 }
18400 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000018402 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018403 {
18404 pos.lnum = curbuf->b_ml.ml_line_count;
18405 pos.col = 0;
18406 }
18407 else
18408 {
18409 pos.lnum = curwin->w_cursor.lnum;
18410 pos.col = (colnr_T)STRLEN(ml_get_curline());
18411 }
18412 return &pos;
18413 }
18414 return NULL;
18415}
18416
18417/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018418 * Convert list in "arg" into a position and optional file number.
18419 * When "fnump" is NULL there is no file number, only 3 items.
18420 * Note that the column is passed on as-is, the caller may want to decrement
18421 * it to use 1 for the first column.
18422 * Return FAIL when conversion is not possible, doesn't check the position for
18423 * validity.
18424 */
18425 static int
18426list2fpos(arg, posp, fnump)
18427 typval_T *arg;
18428 pos_T *posp;
18429 int *fnump;
18430{
18431 list_T *l = arg->vval.v_list;
18432 long i = 0;
18433 long n;
18434
Bram Moolenaarbde35262006-07-23 20:12:24 +000018435 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18436 * when "fnump" isn't NULL and "coladd" is optional. */
18437 if (arg->v_type != VAR_LIST
18438 || l == NULL
18439 || l->lv_len < (fnump == NULL ? 2 : 3)
18440 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018441 return FAIL;
18442
18443 if (fnump != NULL)
18444 {
18445 n = list_find_nr(l, i++, NULL); /* fnum */
18446 if (n < 0)
18447 return FAIL;
18448 if (n == 0)
18449 n = curbuf->b_fnum; /* current buffer */
18450 *fnump = n;
18451 }
18452
18453 n = list_find_nr(l, i++, NULL); /* lnum */
18454 if (n < 0)
18455 return FAIL;
18456 posp->lnum = n;
18457
18458 n = list_find_nr(l, i++, NULL); /* col */
18459 if (n < 0)
18460 return FAIL;
18461 posp->col = n;
18462
18463#ifdef FEAT_VIRTUALEDIT
18464 n = list_find_nr(l, i, NULL);
18465 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000018466 posp->coladd = 0;
18467 else
18468 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018469#endif
18470
18471 return OK;
18472}
18473
18474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018475 * Get the length of an environment variable name.
18476 * Advance "arg" to the first character after the name.
18477 * Return 0 for error.
18478 */
18479 static int
18480get_env_len(arg)
18481 char_u **arg;
18482{
18483 char_u *p;
18484 int len;
18485
18486 for (p = *arg; vim_isIDc(*p); ++p)
18487 ;
18488 if (p == *arg) /* no name found */
18489 return 0;
18490
18491 len = (int)(p - *arg);
18492 *arg = p;
18493 return len;
18494}
18495
18496/*
18497 * Get the length of the name of a function or internal variable.
18498 * "arg" is advanced to the first non-white character after the name.
18499 * Return 0 if something is wrong.
18500 */
18501 static int
18502get_id_len(arg)
18503 char_u **arg;
18504{
18505 char_u *p;
18506 int len;
18507
18508 /* Find the end of the name. */
18509 for (p = *arg; eval_isnamec(*p); ++p)
18510 ;
18511 if (p == *arg) /* no name found */
18512 return 0;
18513
18514 len = (int)(p - *arg);
18515 *arg = skipwhite(p);
18516
18517 return len;
18518}
18519
18520/*
Bram Moolenaara7043832005-01-21 11:56:39 +000018521 * Get the length of the name of a variable or function.
18522 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000018523 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018524 * Return -1 if curly braces expansion failed.
18525 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526 * If the name contains 'magic' {}'s, expand them and return the
18527 * expanded name in an allocated string via 'alias' - caller must free.
18528 */
18529 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018530get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531 char_u **arg;
18532 char_u **alias;
18533 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018534 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535{
18536 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537 char_u *p;
18538 char_u *expr_start;
18539 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540
18541 *alias = NULL; /* default to no alias */
18542
18543 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
18544 && (*arg)[2] == (int)KE_SNR)
18545 {
18546 /* hard coded <SNR>, already translated */
18547 *arg += 3;
18548 return get_id_len(arg) + 3;
18549 }
18550 len = eval_fname_script(*arg);
18551 if (len > 0)
18552 {
18553 /* literal "<SID>", "s:" or "<SNR>" */
18554 *arg += len;
18555 }
18556
Bram Moolenaar071d4272004-06-13 20:20:40 +000018557 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018560 p = find_name_end(*arg, &expr_start, &expr_end,
18561 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562 if (expr_start != NULL)
18563 {
18564 char_u *temp_string;
18565
18566 if (!evaluate)
18567 {
18568 len += (int)(p - *arg);
18569 *arg = skipwhite(p);
18570 return len;
18571 }
18572
18573 /*
18574 * Include any <SID> etc in the expanded string:
18575 * Thus the -len here.
18576 */
18577 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
18578 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018579 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 *alias = temp_string;
18581 *arg = skipwhite(p);
18582 return (int)STRLEN(temp_string);
18583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584
18585 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018586 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587 EMSG2(_(e_invexpr2), *arg);
18588
18589 return len;
18590}
18591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018592/*
18593 * Find the end of a variable or function name, taking care of magic braces.
18594 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18595 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018596 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018597 * Return a pointer to just after the name. Equal to "arg" if there is no
18598 * valid name.
18599 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018601find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 char_u *arg;
18603 char_u **expr_start;
18604 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018605 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018606{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018607 int mb_nest = 0;
18608 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 char_u *p;
18610
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018611 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613 *expr_start = NULL;
18614 *expr_end = NULL;
18615 }
18616
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018617 /* Quick check for valid starting character. */
18618 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
18619 return arg;
18620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621 for (p = arg; *p != NUL
18622 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018623 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018624 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018625 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000018626 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018627 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000018628 if (*p == '\'')
18629 {
18630 /* skip over 'string' to avoid counting [ and ] inside it. */
18631 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
18632 ;
18633 if (*p == NUL)
18634 break;
18635 }
18636 else if (*p == '"')
18637 {
18638 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18639 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
18640 if (*p == '\\' && p[1] != NUL)
18641 ++p;
18642 if (*p == NUL)
18643 break;
18644 }
18645
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018648 if (*p == '[')
18649 ++br_nest;
18650 else if (*p == ']')
18651 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018652 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000018653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018654 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018656 if (*p == '{')
18657 {
18658 mb_nest++;
18659 if (expr_start != NULL && *expr_start == NULL)
18660 *expr_start = p;
18661 }
18662 else if (*p == '}')
18663 {
18664 mb_nest--;
18665 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
18666 *expr_end = p;
18667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669 }
18670
18671 return p;
18672}
18673
18674/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018675 * Expands out the 'magic' {}'s in a variable/function name.
18676 * Note that this can call itself recursively, to deal with
18677 * constructs like foo{bar}{baz}{bam}
18678 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18679 * "in_start" ^
18680 * "expr_start" ^
18681 * "expr_end" ^
18682 * "in_end" ^
18683 *
18684 * Returns a new allocated string, which the caller must free.
18685 * Returns NULL for failure.
18686 */
18687 static char_u *
18688make_expanded_name(in_start, expr_start, expr_end, in_end)
18689 char_u *in_start;
18690 char_u *expr_start;
18691 char_u *expr_end;
18692 char_u *in_end;
18693{
18694 char_u c1;
18695 char_u *retval = NULL;
18696 char_u *temp_result;
18697 char_u *nextcmd = NULL;
18698
18699 if (expr_end == NULL || in_end == NULL)
18700 return NULL;
18701 *expr_start = NUL;
18702 *expr_end = NUL;
18703 c1 = *in_end;
18704 *in_end = NUL;
18705
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018706 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018707 if (temp_result != NULL && nextcmd == NULL)
18708 {
18709 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
18710 + (in_end - expr_end) + 1));
18711 if (retval != NULL)
18712 {
18713 STRCPY(retval, in_start);
18714 STRCAT(retval, temp_result);
18715 STRCAT(retval, expr_end + 1);
18716 }
18717 }
18718 vim_free(temp_result);
18719
18720 *in_end = c1; /* put char back for error messages */
18721 *expr_start = '{';
18722 *expr_end = '}';
18723
18724 if (retval != NULL)
18725 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018726 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018727 if (expr_start != NULL)
18728 {
18729 /* Further expansion! */
18730 temp_result = make_expanded_name(retval, expr_start,
18731 expr_end, temp_result);
18732 vim_free(retval);
18733 retval = temp_result;
18734 }
18735 }
18736
18737 return retval;
18738}
18739
18740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000018742 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 */
18744 static int
18745eval_isnamec(c)
18746 int c;
18747{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018748 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
18749}
18750
18751/*
18752 * Return TRUE if character "c" can be used as the first character in a
18753 * variable or function name (excluding '{' and '}').
18754 */
18755 static int
18756eval_isnamec1(c)
18757 int c;
18758{
18759 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760}
18761
18762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763 * Set number v: variable to "val".
18764 */
18765 void
18766set_vim_var_nr(idx, val)
18767 int idx;
18768 long val;
18769{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018770 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771}
18772
18773/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018774 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775 */
18776 long
18777get_vim_var_nr(idx)
18778 int idx;
18779{
Bram Moolenaare9a41262005-01-15 22:18:47 +000018780 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781}
18782
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018783/*
18784 * Get string v: variable value. Uses a static buffer, can only be used once.
18785 */
18786 char_u *
18787get_vim_var_str(idx)
18788 int idx;
18789{
18790 return get_tv_string(&vimvars[idx].vv_tv);
18791}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018792
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018794 * Get List v: variable value. Caller must take care of reference count when
18795 * needed.
18796 */
18797 list_T *
18798get_vim_var_list(idx)
18799 int idx;
18800{
18801 return vimvars[idx].vv_list;
18802}
18803
18804/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000018805 * Set v:char to character "c".
18806 */
18807 void
18808set_vim_var_char(c)
18809 int c;
18810{
18811#ifdef FEAT_MBYTE
18812 char_u buf[MB_MAXBYTES];
18813#else
18814 char_u buf[2];
18815#endif
18816
18817#ifdef FEAT_MBYTE
18818 if (has_mbyte)
18819 buf[(*mb_char2bytes)(c, buf)] = NUL;
18820 else
18821#endif
18822 {
18823 buf[0] = c;
18824 buf[1] = NUL;
18825 }
18826 set_vim_var_string(VV_CHAR, buf, -1);
18827}
18828
18829/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018830 * Set v:count to "count" and v:count1 to "count1".
18831 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832 */
18833 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018834set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 long count;
18836 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018837 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000018839 if (set_prevcount)
18840 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018841 vimvars[VV_COUNT].vv_nr = count;
18842 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843}
18844
18845/*
18846 * Set string v: variable to a copy of "val".
18847 */
18848 void
18849set_vim_var_string(idx, val, len)
18850 int idx;
18851 char_u *val;
18852 int len; /* length of "val" to use or -1 (whole string) */
18853{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018854 /* Need to do this (at least) once, since we can't initialize a union.
18855 * Will always be invoked when "v:progname" is set. */
18856 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
18857
Bram Moolenaare9a41262005-01-15 22:18:47 +000018858 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018860 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018862 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018863 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018864 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865}
18866
18867/*
Bram Moolenaard812df62008-11-09 12:46:09 +000018868 * Set List v: variable to "val".
18869 */
18870 void
18871set_vim_var_list(idx, val)
18872 int idx;
18873 list_T *val;
18874{
18875 list_unref(vimvars[idx].vv_list);
18876 vimvars[idx].vv_list = val;
18877 if (val != NULL)
18878 ++val->lv_refcount;
18879}
18880
18881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882 * Set v:register if needed.
18883 */
18884 void
18885set_reg_var(c)
18886 int c;
18887{
18888 char_u regname;
18889
18890 if (c == 0 || c == ' ')
18891 regname = '"';
18892 else
18893 regname = c;
18894 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000018895 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896 set_vim_var_string(VV_REG, &regname, 1);
18897}
18898
18899/*
18900 * Get or set v:exception. If "oldval" == NULL, return the current value.
18901 * Otherwise, restore the value to "oldval" and return NULL.
18902 * Must always be called in pairs to save and restore v:exception! Does not
18903 * take care of memory allocations.
18904 */
18905 char_u *
18906v_exception(oldval)
18907 char_u *oldval;
18908{
18909 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018910 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911
Bram Moolenaare9a41262005-01-15 22:18:47 +000018912 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913 return NULL;
18914}
18915
18916/*
18917 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18918 * Otherwise, restore the value to "oldval" and return NULL.
18919 * Must always be called in pairs to save and restore v:throwpoint! Does not
18920 * take care of memory allocations.
18921 */
18922 char_u *
18923v_throwpoint(oldval)
18924 char_u *oldval;
18925{
18926 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018927 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928
Bram Moolenaare9a41262005-01-15 22:18:47 +000018929 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018930 return NULL;
18931}
18932
18933#if defined(FEAT_AUTOCMD) || defined(PROTO)
18934/*
18935 * Set v:cmdarg.
18936 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18937 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18938 * Must always be called in pairs!
18939 */
18940 char_u *
18941set_cmdarg(eap, oldarg)
18942 exarg_T *eap;
18943 char_u *oldarg;
18944{
18945 char_u *oldval;
18946 char_u *newval;
18947 unsigned len;
18948
Bram Moolenaare9a41262005-01-15 22:18:47 +000018949 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018950 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018951 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018952 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000018953 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018954 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955 }
18956
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018957 if (eap->force_bin == FORCE_BIN)
18958 len = 6;
18959 else if (eap->force_bin == FORCE_NOBIN)
18960 len = 8;
18961 else
18962 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018963
18964 if (eap->read_edit)
18965 len += 7;
18966
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018967 if (eap->force_ff != 0)
18968 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
18969# ifdef FEAT_MBYTE
18970 if (eap->force_enc != 0)
18971 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018972 if (eap->bad_char != 0)
18973 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018974# endif
18975
18976 newval = alloc(len + 1);
18977 if (newval == NULL)
18978 return NULL;
18979
18980 if (eap->force_bin == FORCE_BIN)
18981 sprintf((char *)newval, " ++bin");
18982 else if (eap->force_bin == FORCE_NOBIN)
18983 sprintf((char *)newval, " ++nobin");
18984 else
18985 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000018986
18987 if (eap->read_edit)
18988 STRCAT(newval, " ++edit");
18989
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000018990 if (eap->force_ff != 0)
18991 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
18992 eap->cmd + eap->force_ff);
18993# ifdef FEAT_MBYTE
18994 if (eap->force_enc != 0)
18995 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
18996 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020018997 if (eap->bad_char == BAD_KEEP)
18998 STRCPY(newval + STRLEN(newval), " ++bad=keep");
18999 else if (eap->bad_char == BAD_DROP)
19000 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19001 else if (eap->bad_char != 0)
19002 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019003# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019004 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019005 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019006}
19007#endif
19008
19009/*
19010 * Get the value of internal variable "name".
19011 * Return OK or FAIL.
19012 */
19013 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019014get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 char_u *name;
19016 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019017 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019018 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019019{
19020 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019021 typval_T *tv = NULL;
19022 typval_T atv;
19023 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025
19026 /* truncate the name, so that we can use strcmp() */
19027 cc = name[len];
19028 name[len] = NUL;
19029
19030 /*
19031 * Check for "b:changedtick".
19032 */
19033 if (STRCMP(name, "b:changedtick") == 0)
19034 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019035 atv.v_type = VAR_NUMBER;
19036 atv.vval.v_number = curbuf->b_changedtick;
19037 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038 }
19039
19040 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 * Check for user-defined variables.
19042 */
19043 else
19044 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019045 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019047 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 }
19049
Bram Moolenaare9a41262005-01-15 22:18:47 +000019050 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019051 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019052 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019053 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054 ret = FAIL;
19055 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019056 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019057 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019058
19059 name[len] = cc;
19060
19061 return ret;
19062}
19063
19064/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019065 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19066 * Also handle function call with Funcref variable: func(expr)
19067 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19068 */
19069 static int
19070handle_subscript(arg, rettv, evaluate, verbose)
19071 char_u **arg;
19072 typval_T *rettv;
19073 int evaluate; /* do more than finding the end */
19074 int verbose; /* give error messages */
19075{
19076 int ret = OK;
19077 dict_T *selfdict = NULL;
19078 char_u *s;
19079 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019080 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019081
19082 while (ret == OK
19083 && (**arg == '['
19084 || (**arg == '.' && rettv->v_type == VAR_DICT)
19085 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19086 && !vim_iswhite(*(*arg - 1)))
19087 {
19088 if (**arg == '(')
19089 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019090 /* need to copy the funcref so that we can clear rettv */
19091 functv = *rettv;
19092 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019093
19094 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019095 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019096 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019097 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19098 &len, evaluate, selfdict);
19099
19100 /* Clear the funcref afterwards, so that deleting it while
19101 * evaluating the arguments is possible (see test55). */
19102 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019103
19104 /* Stop the expression evaluation when immediately aborting on
19105 * error, or when an interrupt occurred or an exception was thrown
19106 * but not caught. */
19107 if (aborting())
19108 {
19109 if (ret == OK)
19110 clear_tv(rettv);
19111 ret = FAIL;
19112 }
19113 dict_unref(selfdict);
19114 selfdict = NULL;
19115 }
19116 else /* **arg == '[' || **arg == '.' */
19117 {
19118 dict_unref(selfdict);
19119 if (rettv->v_type == VAR_DICT)
19120 {
19121 selfdict = rettv->vval.v_dict;
19122 if (selfdict != NULL)
19123 ++selfdict->dv_refcount;
19124 }
19125 else
19126 selfdict = NULL;
19127 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19128 {
19129 clear_tv(rettv);
19130 ret = FAIL;
19131 }
19132 }
19133 }
19134 dict_unref(selfdict);
19135 return ret;
19136}
19137
19138/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019139 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019140 * value).
19141 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019142 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019143alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019144{
Bram Moolenaar33570922005-01-25 22:26:29 +000019145 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019146}
19147
19148/*
19149 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019150 * The string "s" must have been allocated, it is consumed.
19151 * Return NULL for out of memory, the variable otherwise.
19152 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019153 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019154alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 char_u *s;
19156{
Bram Moolenaar33570922005-01-25 22:26:29 +000019157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019159 rettv = alloc_tv();
19160 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019162 rettv->v_type = VAR_STRING;
19163 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164 }
19165 else
19166 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019167 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168}
19169
19170/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019171 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019173 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019175 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019176{
19177 if (varp != NULL)
19178 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019179 switch (varp->v_type)
19180 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019181 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019182 func_unref(varp->vval.v_string);
19183 /*FALLTHROUGH*/
19184 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019185 vim_free(varp->vval.v_string);
19186 break;
19187 case VAR_LIST:
19188 list_unref(varp->vval.v_list);
19189 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019190 case VAR_DICT:
19191 dict_unref(varp->vval.v_dict);
19192 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019193 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019194#ifdef FEAT_FLOAT
19195 case VAR_FLOAT:
19196#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019197 case VAR_UNKNOWN:
19198 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019199 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019200 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019201 break;
19202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 vim_free(varp);
19204 }
19205}
19206
19207/*
19208 * Free the memory for a variable value and set the value to NULL or 0.
19209 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019210 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019211clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019212 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213{
19214 if (varp != NULL)
19215 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019216 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019218 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019219 func_unref(varp->vval.v_string);
19220 /*FALLTHROUGH*/
19221 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019222 vim_free(varp->vval.v_string);
19223 varp->vval.v_string = NULL;
19224 break;
19225 case VAR_LIST:
19226 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019227 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019228 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019229 case VAR_DICT:
19230 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019231 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019232 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019233 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019234 varp->vval.v_number = 0;
19235 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019236#ifdef FEAT_FLOAT
19237 case VAR_FLOAT:
19238 varp->vval.v_float = 0.0;
19239 break;
19240#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019241 case VAR_UNKNOWN:
19242 break;
19243 default:
19244 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019245 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019246 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 }
19248}
19249
19250/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019251 * Set the value of a variable to NULL without freeing items.
19252 */
19253 static void
19254init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019255 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019256{
19257 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019258 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019259}
19260
19261/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019262 * Get the number value of a variable.
19263 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019264 * For incompatible types, return 0.
19265 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19266 * caller of incompatible types: it sets *denote to TRUE if "denote"
19267 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268 */
19269 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019270get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019271 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019273 int error = FALSE;
19274
19275 return get_tv_number_chk(varp, &error); /* return 0L on error */
19276}
19277
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019278 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019279get_tv_number_chk(varp, denote)
19280 typval_T *varp;
19281 int *denote;
19282{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019283 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019284
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019285 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019287 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019288 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019289#ifdef FEAT_FLOAT
19290 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019291 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019292 break;
19293#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019294 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019295 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019296 break;
19297 case VAR_STRING:
19298 if (varp->vval.v_string != NULL)
19299 vim_str2nr(varp->vval.v_string, NULL, NULL,
19300 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019301 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019302 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019303 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019304 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019305 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019306 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019307 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019308 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019309 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019310 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019311 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019312 if (denote == NULL) /* useful for values that must be unsigned */
19313 n = -1;
19314 else
19315 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019316 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317}
19318
19319/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019320 * Get the lnum from the first argument.
19321 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019322 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 */
19324 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019325get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000019326 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327{
Bram Moolenaar33570922005-01-25 22:26:29 +000019328 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 linenr_T lnum;
19330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019331 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 if (lnum == 0) /* no valid number, try using line() */
19333 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019334 rettv.v_type = VAR_NUMBER;
19335 f_line(argvars, &rettv);
19336 lnum = rettv.vval.v_number;
19337 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 }
19339 return lnum;
19340}
19341
19342/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000019343 * Get the lnum from the first argument.
19344 * Also accepts "$", then "buf" is used.
19345 * Returns 0 on error.
19346 */
19347 static linenr_T
19348get_tv_lnum_buf(argvars, buf)
19349 typval_T *argvars;
19350 buf_T *buf;
19351{
19352 if (argvars[0].v_type == VAR_STRING
19353 && argvars[0].vval.v_string != NULL
19354 && argvars[0].vval.v_string[0] == '$'
19355 && buf != NULL)
19356 return buf->b_ml.ml_line_count;
19357 return get_tv_number_chk(&argvars[0], NULL);
19358}
19359
19360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019361 * Get the string value of a variable.
19362 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000019363 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
19364 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 * If the String variable has never been set, return an empty string.
19366 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019367 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
19368 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369 */
19370 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019371get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019372 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019373{
19374 static char_u mybuf[NUMBUFLEN];
19375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019376 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377}
19378
19379 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019380get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000019381 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019382 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019384 char_u *res = get_tv_string_buf_chk(varp, buf);
19385
19386 return res != NULL ? res : (char_u *)"";
19387}
19388
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019389 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019390get_tv_string_chk(varp)
19391 typval_T *varp;
19392{
19393 static char_u mybuf[NUMBUFLEN];
19394
19395 return get_tv_string_buf_chk(varp, mybuf);
19396}
19397
19398 static char_u *
19399get_tv_string_buf_chk(varp, buf)
19400 typval_T *varp;
19401 char_u *buf;
19402{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019403 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019404 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019405 case VAR_NUMBER:
19406 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
19407 return buf;
19408 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019409 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019410 break;
19411 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019412 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000019413 break;
19414 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019415 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019416 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019417#ifdef FEAT_FLOAT
19418 case VAR_FLOAT:
19419 EMSG(_("E806: using Float as a String"));
19420 break;
19421#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019422 case VAR_STRING:
19423 if (varp->vval.v_string != NULL)
19424 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019425 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019426 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019427 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019428 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019430 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431}
19432
19433/*
19434 * Find variable "name" in the list of variables.
19435 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019436 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019437 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000019438 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019440 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019441find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019442 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019443 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444{
Bram Moolenaar071d4272004-06-13 20:20:40 +000019445 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019446 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447
Bram Moolenaara7043832005-01-21 11:56:39 +000019448 ht = find_var_ht(name, &varname);
19449 if (htp != NULL)
19450 *htp = ht;
19451 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019453 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454}
19455
19456/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019457 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000019458 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019460 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019461find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000019462 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000019463 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019464 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000019465{
Bram Moolenaar33570922005-01-25 22:26:29 +000019466 hashitem_T *hi;
19467
19468 if (*varname == NUL)
19469 {
19470 /* Must be something like "s:", otherwise "ht" would be NULL. */
19471 switch (varname[-2])
19472 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019473 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019474 case 'g': return &globvars_var;
19475 case 'v': return &vimvars_var;
19476 case 'b': return &curbuf->b_bufvar;
19477 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019478#ifdef FEAT_WINDOWS
19479 case 't': return &curtab->tp_winvar;
19480#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019481 case 'l': return current_funccal == NULL
19482 ? NULL : &current_funccal->l_vars_var;
19483 case 'a': return current_funccal == NULL
19484 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000019485 }
19486 return NULL;
19487 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019488
19489 hi = hash_find(ht, varname);
19490 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019491 {
19492 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019493 * worked find the variable again. Don't auto-load a script if it was
19494 * loaded already, otherwise it would be loaded every time when
19495 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019496 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019497 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019498 hi = hash_find(ht, varname);
19499 if (HASHITEM_EMPTY(hi))
19500 return NULL;
19501 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019502 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019503}
19504
19505/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019506 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000019507 * Set "varname" to the start of name without ':'.
19508 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019509 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000019510find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511 char_u *name;
19512 char_u **varname;
19513{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019514 hashitem_T *hi;
19515
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 if (name[1] != ':')
19517 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019518 /* The name must not start with a colon or #. */
19519 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520 return NULL;
19521 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019522
19523 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019524 hi = hash_find(&compat_hashtab, name);
19525 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000019526 return &compat_hashtab;
19527
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019529 return &globvarht; /* global variable */
19530 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531 }
19532 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019533 if (*name == 'g') /* global variable */
19534 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019535 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19536 */
19537 if (vim_strchr(name + 2, ':') != NULL
19538 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000019539 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019541 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019542 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000019543 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019544#ifdef FEAT_WINDOWS
19545 if (*name == 't') /* tab page variable */
19546 return &curtab->tp_vars.dv_hashtab;
19547#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000019548 if (*name == 'v') /* v: variable */
19549 return &vimvarht;
19550 if (*name == 'a' && current_funccal != NULL) /* function argument */
19551 return &current_funccal->l_avars.dv_hashtab;
19552 if (*name == 'l' && current_funccal != NULL) /* local function variable */
19553 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 if (*name == 's' /* script variable */
19555 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
19556 return &SCRIPT_VARS(current_SID);
19557 return NULL;
19558}
19559
19560/*
19561 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020019562 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563 * Returns NULL when it doesn't exist.
19564 */
19565 char_u *
19566get_var_value(name)
19567 char_u *name;
19568{
Bram Moolenaar33570922005-01-25 22:26:29 +000019569 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019570
Bram Moolenaara7043832005-01-21 11:56:39 +000019571 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 if (v == NULL)
19573 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000019574 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575}
19576
19577/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019578 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 * sourcing this script and when executing functions defined in the script.
19580 */
19581 void
19582new_script_vars(id)
19583 scid_T id;
19584{
Bram Moolenaara7043832005-01-21 11:56:39 +000019585 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000019586 hashtab_T *ht;
19587 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000019588
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
19590 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019591 /* Re-allocating ga_data means that an ht_array pointing to
19592 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000019593 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000019594 for (i = 1; i <= ga_scripts.ga_len; ++i)
19595 {
19596 ht = &SCRIPT_VARS(i);
19597 if (ht->ht_mask == HT_INIT_SIZE - 1)
19598 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019599 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000019600 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000019601 }
19602
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603 while (ga_scripts.ga_len < id)
19604 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020019605 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020019606 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaar33570922005-01-25 22:26:29 +000019607 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609 }
19610 }
19611}
19612
19613/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019614 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19615 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616 */
19617 void
Bram Moolenaar33570922005-01-25 22:26:29 +000019618init_var_dict(dict, dict_var)
19619 dict_T *dict;
19620 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621{
Bram Moolenaar33570922005-01-25 22:26:29 +000019622 hash_init(&dict->dv_hashtab);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019623 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000019624 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 dict_var->di_tv.vval.v_dict = dict;
19626 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019627 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000019628 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
19629 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630}
19631
19632/*
19633 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000019634 * Frees all allocated variables and the value they contain.
19635 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636 */
19637 void
Bram Moolenaara7043832005-01-21 11:56:39 +000019638vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000019639 hashtab_T *ht;
19640{
19641 vars_clear_ext(ht, TRUE);
19642}
19643
19644/*
19645 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19646 */
19647 static void
19648vars_clear_ext(ht, free_val)
19649 hashtab_T *ht;
19650 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651{
Bram Moolenaara7043832005-01-21 11:56:39 +000019652 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000019653 hashitem_T *hi;
19654 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655
Bram Moolenaar33570922005-01-25 22:26:29 +000019656 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019657 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000019658 for (hi = ht->ht_array; todo > 0; ++hi)
19659 {
19660 if (!HASHITEM_EMPTY(hi))
19661 {
19662 --todo;
19663
Bram Moolenaar33570922005-01-25 22:26:29 +000019664 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000019665 * ht_array might change then. hash_clear() takes care of it
19666 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000019667 v = HI2DI(hi);
19668 if (free_val)
19669 clear_tv(&v->di_tv);
19670 if ((v->di_flags & DI_FLAGS_FIX) == 0)
19671 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000019672 }
19673 }
19674 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019675 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676}
19677
Bram Moolenaara7043832005-01-21 11:56:39 +000019678/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019679 * Delete a variable from hashtab "ht" at item "hi".
19680 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000019681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000019683delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000019684 hashtab_T *ht;
19685 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686{
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000019688
19689 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000019690 clear_tv(&di->di_tv);
19691 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019692}
19693
19694/*
19695 * List the value of one internal variable.
19696 */
19697 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019698list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000019699 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019701 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019702{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019703 char_u *tofree;
19704 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019705 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019706
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000019707 current_copyID += COPYID_INC;
19708 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000019709 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019710 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019711 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712}
19713
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019715list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 char_u *prefix;
19717 char_u *name;
19718 int type;
19719 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019720 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721{
Bram Moolenaar31859182007-08-14 20:41:13 +000019722 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19723 msg_start();
19724 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725 if (name != NULL) /* "a:" vars don't have a name stored */
19726 msg_puts(name);
19727 msg_putchar(' ');
19728 msg_advance(22);
19729 if (type == VAR_NUMBER)
19730 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019731 else if (type == VAR_FUNC)
19732 msg_putchar('*');
19733 else if (type == VAR_LIST)
19734 {
19735 msg_putchar('[');
19736 if (*string == '[')
19737 ++string;
19738 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000019739 else if (type == VAR_DICT)
19740 {
19741 msg_putchar('{');
19742 if (*string == '{')
19743 ++string;
19744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745 else
19746 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019747
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019749
19750 if (type == VAR_FUNC)
19751 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000019752 if (*first)
19753 {
19754 msg_clr_eos();
19755 *first = FALSE;
19756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019757}
19758
19759/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019760 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761 * If the variable already exists, the value is updated.
19762 * Otherwise the variable is created.
19763 */
19764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019765set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000019767 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019768 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769{
Bram Moolenaar33570922005-01-25 22:26:29 +000019770 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000019772 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019773 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019775 ht = find_var_ht(name, &varname);
19776 if (ht == NULL || *varname == NUL)
19777 {
19778 EMSG2(_(e_illvar), name);
19779 return;
19780 }
19781 v = find_var_in_ht(ht, varname, TRUE);
19782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019783 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019784 {
19785 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
19786 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
19787 ? name[2] : name[0]))
19788 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019789 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019790 return;
19791 }
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010019792 /* Don't allow hiding a function. When "v" is not NULL we migth be
19793 * assigning another function to the same var, the type is checked
19794 * below. */
19795 if (v == NULL && function_exists(name))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019796 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000019797 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000019798 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019799 return;
19800 }
19801 }
19802
Bram Moolenaar33570922005-01-25 22:26:29 +000019803 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019805 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019806 if (var_check_ro(v->di_flags, name)
19807 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000019808 return;
19809 if (v->di_tv.v_type != tv->v_type
19810 && !((v->di_tv.v_type == VAR_STRING
19811 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019813 || tv->v_type == VAR_NUMBER))
19814#ifdef FEAT_FLOAT
19815 && !((v->di_tv.v_type == VAR_NUMBER
19816 || v->di_tv.v_type == VAR_FLOAT)
19817 && (tv->v_type == VAR_NUMBER
19818 || tv->v_type == VAR_FLOAT))
19819#endif
19820 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019821 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000019822 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019823 return;
19824 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019825
19826 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000019827 * Handle setting internal v: variables separately: we don't change
19828 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000019829 */
19830 if (ht == &vimvarht)
19831 {
19832 if (v->di_tv.v_type == VAR_STRING)
19833 {
19834 vim_free(v->di_tv.vval.v_string);
19835 if (copy || tv->v_type != VAR_STRING)
19836 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
19837 else
19838 {
19839 /* Take over the string to avoid an extra alloc/free. */
19840 v->di_tv.vval.v_string = tv->vval.v_string;
19841 tv->vval.v_string = NULL;
19842 }
19843 }
19844 else if (v->di_tv.v_type != VAR_NUMBER)
19845 EMSG2(_(e_intern2), "set_var()");
19846 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019847 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019848 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019849 if (STRCMP(varname, "searchforward") == 0)
19850 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
19851 }
Bram Moolenaar33570922005-01-25 22:26:29 +000019852 return;
19853 }
19854
19855 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019856 }
19857 else /* add a new variable */
19858 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000019859 /* Can't add "v:" variable. */
19860 if (ht == &vimvarht)
19861 {
19862 EMSG2(_(e_illvar), name);
19863 return;
19864 }
19865
Bram Moolenaar92124a32005-06-17 22:03:40 +000019866 /* Make sure the variable name is valid. */
19867 for (p = varname; *p != NUL; ++p)
Bram Moolenaara5792f52005-11-23 21:25:05 +000019868 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
19869 && *p != AUTOLOAD_CHAR)
Bram Moolenaar92124a32005-06-17 22:03:40 +000019870 {
19871 EMSG2(_(e_illvar), varname);
19872 return;
19873 }
19874
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019875 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
19876 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000019877 if (v == NULL)
19878 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000019879 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000019880 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019882 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883 return;
19884 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019885 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 }
Bram Moolenaara7043832005-01-21 11:56:39 +000019887
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019888 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000019889 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019890 else
19891 {
Bram Moolenaar33570922005-01-25 22:26:29 +000019892 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019893 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019894 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000019895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896}
19897
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019898/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019899 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000019900 * Also give an error message.
19901 */
19902 static int
19903var_check_ro(flags, name)
19904 int flags;
19905 char_u *name;
19906{
19907 if (flags & DI_FLAGS_RO)
19908 {
19909 EMSG2(_(e_readonlyvar), name);
19910 return TRUE;
19911 }
19912 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
19913 {
19914 EMSG2(_(e_readonlysbx), name);
19915 return TRUE;
19916 }
19917 return FALSE;
19918}
19919
19920/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000019921 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19922 * Also give an error message.
19923 */
19924 static int
19925var_check_fixed(flags, name)
19926 int flags;
19927 char_u *name;
19928{
19929 if (flags & DI_FLAGS_FIX)
19930 {
19931 EMSG2(_("E795: Cannot delete variable %s"), name);
19932 return TRUE;
19933 }
19934 return FALSE;
19935}
19936
19937/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019938 * Return TRUE if typeval "tv" is set to be locked (immutable).
19939 * Also give an error message, using "name".
19940 */
19941 static int
19942tv_check_lock(lock, name)
19943 int lock;
19944 char_u *name;
19945{
19946 if (lock & VAR_LOCKED)
19947 {
19948 EMSG2(_("E741: Value is locked: %s"),
19949 name == NULL ? (char_u *)_("Unknown") : name);
19950 return TRUE;
19951 }
19952 if (lock & VAR_FIXED)
19953 {
19954 EMSG2(_("E742: Cannot change value of %s"),
19955 name == NULL ? (char_u *)_("Unknown") : name);
19956 return TRUE;
19957 }
19958 return FALSE;
19959}
19960
19961/*
Bram Moolenaar33570922005-01-25 22:26:29 +000019962 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019963 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019964 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000019965 * It is OK for "from" and "to" to point to the same item. This is used to
19966 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019967 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010019968 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019969copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000019970 typval_T *from;
19971 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019973 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019974 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019975 switch (from->v_type)
19976 {
19977 case VAR_NUMBER:
19978 to->vval.v_number = from->vval.v_number;
19979 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019980#ifdef FEAT_FLOAT
19981 case VAR_FLOAT:
19982 to->vval.v_float = from->vval.v_float;
19983 break;
19984#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019985 case VAR_STRING:
19986 case VAR_FUNC:
19987 if (from->vval.v_string == NULL)
19988 to->vval.v_string = NULL;
19989 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019990 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019991 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019992 if (from->v_type == VAR_FUNC)
19993 func_ref(to->vval.v_string);
19994 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019995 break;
19996 case VAR_LIST:
19997 if (from->vval.v_list == NULL)
19998 to->vval.v_list = NULL;
19999 else
20000 {
20001 to->vval.v_list = from->vval.v_list;
20002 ++to->vval.v_list->lv_refcount;
20003 }
20004 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020005 case VAR_DICT:
20006 if (from->vval.v_dict == NULL)
20007 to->vval.v_dict = NULL;
20008 else
20009 {
20010 to->vval.v_dict = from->vval.v_dict;
20011 ++to->vval.v_dict->dv_refcount;
20012 }
20013 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020014 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020016 break;
20017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018}
20019
20020/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020021 * Make a copy of an item.
20022 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020023 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20024 * reference to an already copied list/dict can be used.
20025 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020026 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020027 static int
20028item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020029 typval_T *from;
20030 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020031 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020032 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020033{
20034 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020035 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020036
Bram Moolenaar33570922005-01-25 22:26:29 +000020037 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020038 {
20039 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020040 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020041 }
20042 ++recurse;
20043
20044 switch (from->v_type)
20045 {
20046 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020047#ifdef FEAT_FLOAT
20048 case VAR_FLOAT:
20049#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020050 case VAR_STRING:
20051 case VAR_FUNC:
20052 copy_tv(from, to);
20053 break;
20054 case VAR_LIST:
20055 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020056 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020057 if (from->vval.v_list == NULL)
20058 to->vval.v_list = NULL;
20059 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20060 {
20061 /* use the copy made earlier */
20062 to->vval.v_list = from->vval.v_list->lv_copylist;
20063 ++to->vval.v_list->lv_refcount;
20064 }
20065 else
20066 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20067 if (to->vval.v_list == NULL)
20068 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020069 break;
20070 case VAR_DICT:
20071 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020072 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020073 if (from->vval.v_dict == NULL)
20074 to->vval.v_dict = NULL;
20075 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20076 {
20077 /* use the copy made earlier */
20078 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20079 ++to->vval.v_dict->dv_refcount;
20080 }
20081 else
20082 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20083 if (to->vval.v_dict == NULL)
20084 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020085 break;
20086 default:
20087 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020088 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020089 }
20090 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020091 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020092}
20093
20094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 * ":echo expr1 ..." print each argument separated with a space, add a
20096 * newline at the end.
20097 * ":echon expr1 ..." print each argument plain.
20098 */
20099 void
20100ex_echo(eap)
20101 exarg_T *eap;
20102{
20103 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020104 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020105 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020106 char_u *p;
20107 int needclr = TRUE;
20108 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020109 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110
20111 if (eap->skip)
20112 ++emsg_skip;
20113 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20114 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020115 /* If eval1() causes an error message the text from the command may
20116 * still need to be cleared. E.g., "echo 22,44". */
20117 need_clr_eos = needclr;
20118
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020120 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121 {
20122 /*
20123 * Report the invalid expression unless the expression evaluation
20124 * has been cancelled due to an aborting error, an interrupt, or an
20125 * exception.
20126 */
20127 if (!aborting())
20128 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020129 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 break;
20131 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020132 need_clr_eos = FALSE;
20133
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 if (!eap->skip)
20135 {
20136 if (atstart)
20137 {
20138 atstart = FALSE;
20139 /* Call msg_start() after eval1(), evaluating the expression
20140 * may cause a message to appear. */
20141 if (eap->cmdidx == CMD_echo)
20142 msg_start();
20143 }
20144 else if (eap->cmdidx == CMD_echo)
20145 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020146 current_copyID += COPYID_INC;
20147 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020148 if (p != NULL)
20149 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020151 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020153 if (*p != TAB && needclr)
20154 {
20155 /* remove any text still there from the command */
20156 msg_clr_eos();
20157 needclr = FALSE;
20158 }
20159 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 }
20161 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020162 {
20163#ifdef FEAT_MBYTE
20164 if (has_mbyte)
20165 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020166 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020167
20168 (void)msg_outtrans_len_attr(p, i, echo_attr);
20169 p += i - 1;
20170 }
20171 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020173 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020175 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020176 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020178 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 arg = skipwhite(arg);
20180 }
20181 eap->nextcmd = check_nextcmd(arg);
20182
20183 if (eap->skip)
20184 --emsg_skip;
20185 else
20186 {
20187 /* remove text that may still be there from the command */
20188 if (needclr)
20189 msg_clr_eos();
20190 if (eap->cmdidx == CMD_echo)
20191 msg_end();
20192 }
20193}
20194
20195/*
20196 * ":echohl {name}".
20197 */
20198 void
20199ex_echohl(eap)
20200 exarg_T *eap;
20201{
20202 int id;
20203
20204 id = syn_name2id(eap->arg);
20205 if (id == 0)
20206 echo_attr = 0;
20207 else
20208 echo_attr = syn_id2attr(id);
20209}
20210
20211/*
20212 * ":execute expr1 ..." execute the result of an expression.
20213 * ":echomsg expr1 ..." Print a message
20214 * ":echoerr expr1 ..." Print an error
20215 * Each gets spaces around each argument and a newline at the end for
20216 * echo commands
20217 */
20218 void
20219ex_execute(eap)
20220 exarg_T *eap;
20221{
20222 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 int ret = OK;
20225 char_u *p;
20226 garray_T ga;
20227 int len;
20228 int save_did_emsg;
20229
20230 ga_init2(&ga, 1, 80);
20231
20232 if (eap->skip)
20233 ++emsg_skip;
20234 while (*arg != NUL && *arg != '|' && *arg != '\n')
20235 {
20236 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020237 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 {
20239 /*
20240 * Report the invalid expression unless the expression evaluation
20241 * has been cancelled due to an aborting error, an interrupt, or an
20242 * exception.
20243 */
20244 if (!aborting())
20245 EMSG2(_(e_invexpr2), p);
20246 ret = FAIL;
20247 break;
20248 }
20249
20250 if (!eap->skip)
20251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020252 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020253 len = (int)STRLEN(p);
20254 if (ga_grow(&ga, len + 2) == FAIL)
20255 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020256 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 ret = FAIL;
20258 break;
20259 }
20260 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 ga.ga_len += len;
20264 }
20265
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020266 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020267 arg = skipwhite(arg);
20268 }
20269
20270 if (ret != FAIL && ga.ga_data != NULL)
20271 {
20272 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000020273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000020275 out_flush();
20276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 else if (eap->cmdidx == CMD_echoerr)
20278 {
20279 /* We don't want to abort following commands, restore did_emsg. */
20280 save_did_emsg = did_emsg;
20281 EMSG((char_u *)ga.ga_data);
20282 if (!force_abort)
20283 did_emsg = save_did_emsg;
20284 }
20285 else if (eap->cmdidx == CMD_execute)
20286 do_cmdline((char_u *)ga.ga_data,
20287 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
20288 }
20289
20290 ga_clear(&ga);
20291
20292 if (eap->skip)
20293 --emsg_skip;
20294
20295 eap->nextcmd = check_nextcmd(arg);
20296}
20297
20298/*
20299 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
20300 * "arg" points to the "&" or '+' when called, to "option" when returning.
20301 * Returns NULL when no option name found. Otherwise pointer to the char
20302 * after the option name.
20303 */
20304 static char_u *
20305find_option_end(arg, opt_flags)
20306 char_u **arg;
20307 int *opt_flags;
20308{
20309 char_u *p = *arg;
20310
20311 ++p;
20312 if (*p == 'g' && p[1] == ':')
20313 {
20314 *opt_flags = OPT_GLOBAL;
20315 p += 2;
20316 }
20317 else if (*p == 'l' && p[1] == ':')
20318 {
20319 *opt_flags = OPT_LOCAL;
20320 p += 2;
20321 }
20322 else
20323 *opt_flags = 0;
20324
20325 if (!ASCII_ISALPHA(*p))
20326 return NULL;
20327 *arg = p;
20328
20329 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
20330 p += 4; /* termcap option */
20331 else
20332 while (ASCII_ISALPHA(*p))
20333 ++p;
20334 return p;
20335}
20336
20337/*
20338 * ":function"
20339 */
20340 void
20341ex_function(eap)
20342 exarg_T *eap;
20343{
20344 char_u *theline;
20345 int j;
20346 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 char_u *name = NULL;
20349 char_u *p;
20350 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020351 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020352 garray_T newargs;
20353 garray_T newlines;
20354 int varargs = FALSE;
20355 int mustend = FALSE;
20356 int flags = 0;
20357 ufunc_T *fp;
20358 int indent;
20359 int nesting;
20360 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020361 dictitem_T *v;
20362 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020363 static int func_nr = 0; /* number for nameless function */
20364 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020365 hashtab_T *ht;
20366 int todo;
20367 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020368 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369
20370 /*
20371 * ":function" without argument: list functions.
20372 */
20373 if (ends_excmd(*eap->arg))
20374 {
20375 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020376 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020377 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000020378 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020379 {
20380 if (!HASHITEM_EMPTY(hi))
20381 {
20382 --todo;
20383 fp = HI2UF(hi);
20384 if (!isdigit(*fp->uf_name))
20385 list_func_head(fp, FALSE);
20386 }
20387 }
20388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 eap->nextcmd = check_nextcmd(eap->arg);
20390 return;
20391 }
20392
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020393 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020394 * ":function /pat": list functions matching pattern.
20395 */
20396 if (*eap->arg == '/')
20397 {
20398 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
20399 if (!eap->skip)
20400 {
20401 regmatch_T regmatch;
20402
20403 c = *p;
20404 *p = NUL;
20405 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
20406 *p = c;
20407 if (regmatch.regprog != NULL)
20408 {
20409 regmatch.rm_ic = p_ic;
20410
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020411 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020412 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
20413 {
20414 if (!HASHITEM_EMPTY(hi))
20415 {
20416 --todo;
20417 fp = HI2UF(hi);
20418 if (!isdigit(*fp->uf_name)
20419 && vim_regexec(&regmatch, fp->uf_name, 0))
20420 list_func_head(fp, FALSE);
20421 }
20422 }
Bram Moolenaar69f2d5a2009-04-22 14:10:39 +000020423 vim_free(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000020424 }
20425 }
20426 if (*p == '/')
20427 ++p;
20428 eap->nextcmd = check_nextcmd(p);
20429 return;
20430 }
20431
20432 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020433 * Get the function name. There are these situations:
20434 * func normal function name
20435 * "name" == func, "fudi.fd_dict" == NULL
20436 * dict.func new dictionary entry
20437 * "name" == NULL, "fudi.fd_dict" set,
20438 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20439 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020440 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020441 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20442 * dict.func existing dict entry that's not a Funcref
20443 * "name" == NULL, "fudi.fd_dict" set,
20444 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020447 name = trans_function_name(&p, eap->skip, 0, &fudi);
20448 paren = (vim_strchr(p, '(') != NULL);
20449 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450 {
20451 /*
20452 * Return on an invalid expression in braces, unless the expression
20453 * evaluation has been cancelled due to an aborting error, an
20454 * interrupt, or an exception.
20455 */
20456 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020458 if (!eap->skip && fudi.fd_newkey != NULL)
20459 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020460 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 else
20464 eap->skip = TRUE;
20465 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000020466
Bram Moolenaar071d4272004-06-13 20:20:40 +000020467 /* An error in a function call during evaluation of an expression in magic
20468 * braces should not cause the function not to be defined. */
20469 saved_did_emsg = did_emsg;
20470 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471
20472 /*
20473 * ":function func" with only function name: list function.
20474 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020475 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020476 {
20477 if (!ends_excmd(*skipwhite(p)))
20478 {
20479 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020480 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481 }
20482 eap->nextcmd = check_nextcmd(p);
20483 if (eap->nextcmd != NULL)
20484 *p = NUL;
20485 if (!eap->skip && !got_int)
20486 {
20487 fp = find_func(name);
20488 if (fp != NULL)
20489 {
20490 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020491 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020493 if (FUNCLINE(fp, j) == NULL)
20494 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 msg_putchar('\n');
20496 msg_outnum((long)(j + 1));
20497 if (j < 9)
20498 msg_putchar(' ');
20499 if (j < 99)
20500 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020501 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 out_flush(); /* show a line at a time */
20503 ui_breakcheck();
20504 }
20505 if (!got_int)
20506 {
20507 msg_putchar('\n');
20508 msg_puts((char_u *)" endfunction");
20509 }
20510 }
20511 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020512 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020514 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020515 }
20516
20517 /*
20518 * ":function name(arg1, arg2)" Define function.
20519 */
20520 p = skipwhite(p);
20521 if (*p != '(')
20522 {
20523 if (!eap->skip)
20524 {
20525 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020526 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527 }
20528 /* attempt to continue by skipping some text */
20529 if (vim_strchr(p, '(') != NULL)
20530 p = vim_strchr(p, '(');
20531 }
20532 p = skipwhite(p + 1);
20533
20534 ga_init2(&newargs, (int)sizeof(char_u *), 3);
20535 ga_init2(&newlines, (int)sizeof(char_u *), 3);
20536
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020537 if (!eap->skip)
20538 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020539 /* Check the name of the function. Unless it's a dictionary function
20540 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020541 if (name != NULL)
20542 arg = name;
20543 else
20544 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000020545 if (arg != NULL && (fudi.fd_di == NULL
20546 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020547 {
20548 if (*arg == K_SPECIAL)
20549 j = 3;
20550 else
20551 j = 0;
20552 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
20553 : eval_isnamec(arg[j])))
20554 ++j;
20555 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000020556 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020557 }
20558 }
20559
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560 /*
20561 * Isolate the arguments: "arg1, arg2, ...)"
20562 */
20563 while (*p != ')')
20564 {
20565 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
20566 {
20567 varargs = TRUE;
20568 p += 3;
20569 mustend = TRUE;
20570 }
20571 else
20572 {
20573 arg = p;
20574 while (ASCII_ISALNUM(*p) || *p == '_')
20575 ++p;
20576 if (arg == p || isdigit(*arg)
20577 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
20578 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
20579 {
20580 if (!eap->skip)
20581 EMSG2(_("E125: Illegal argument: %s"), arg);
20582 break;
20583 }
20584 if (ga_grow(&newargs, 1) == FAIL)
20585 goto erret;
20586 c = *p;
20587 *p = NUL;
20588 arg = vim_strsave(arg);
20589 if (arg == NULL)
20590 goto erret;
20591 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
20592 *p = c;
20593 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020594 if (*p == ',')
20595 ++p;
20596 else
20597 mustend = TRUE;
20598 }
20599 p = skipwhite(p);
20600 if (mustend && *p != ')')
20601 {
20602 if (!eap->skip)
20603 EMSG2(_(e_invarg2), eap->arg);
20604 break;
20605 }
20606 }
20607 ++p; /* skip the ')' */
20608
Bram Moolenaare9a41262005-01-15 22:18:47 +000020609 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 for (;;)
20611 {
20612 p = skipwhite(p);
20613 if (STRNCMP(p, "range", 5) == 0)
20614 {
20615 flags |= FC_RANGE;
20616 p += 5;
20617 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000020618 else if (STRNCMP(p, "dict", 4) == 0)
20619 {
20620 flags |= FC_DICT;
20621 p += 4;
20622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020623 else if (STRNCMP(p, "abort", 5) == 0)
20624 {
20625 flags |= FC_ABORT;
20626 p += 5;
20627 }
20628 else
20629 break;
20630 }
20631
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020632 /* When there is a line break use what follows for the function body.
20633 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20634 if (*p == '\n')
20635 line_arg = p + 1;
20636 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637 EMSG(_(e_trailing));
20638
20639 /*
20640 * Read the body of the function, until ":endfunction" is found.
20641 */
20642 if (KeyTyped)
20643 {
20644 /* Check if the function already exists, don't let the user type the
20645 * whole function before telling him it doesn't work! For a script we
20646 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020647 if (!eap->skip && !eap->forceit)
20648 {
20649 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
20650 EMSG(_(e_funcdict));
20651 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020652 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654
Bram Moolenaard857f0e2005-06-21 22:37:39 +000020655 if (!eap->skip && did_emsg)
20656 goto erret;
20657
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658 msg_putchar('\n'); /* don't overwrite the function name */
20659 cmdline_row = msg_row;
20660 }
20661
20662 indent = 2;
20663 nesting = 0;
20664 for (;;)
20665 {
20666 msg_scroll = TRUE;
20667 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020668 sourcing_lnum_off = sourcing_lnum;
20669
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020670 if (line_arg != NULL)
20671 {
20672 /* Use eap->arg, split up in parts by line breaks. */
20673 theline = line_arg;
20674 p = vim_strchr(theline, '\n');
20675 if (p == NULL)
20676 line_arg += STRLEN(line_arg);
20677 else
20678 {
20679 *p = NUL;
20680 line_arg = p + 1;
20681 }
20682 }
20683 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020684 theline = getcmdline(':', 0L, indent);
20685 else
20686 theline = eap->getline(':', eap->cookie, indent);
20687 if (KeyTyped)
20688 lines_left = Rows - 1;
20689 if (theline == NULL)
20690 {
20691 EMSG(_("E126: Missing :endfunction"));
20692 goto erret;
20693 }
20694
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020695 /* Detect line continuation: sourcing_lnum increased more than one. */
20696 if (sourcing_lnum > sourcing_lnum_off + 1)
20697 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
20698 else
20699 sourcing_lnum_off = 0;
20700
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701 if (skip_until != NULL)
20702 {
20703 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20704 * don't check for ":endfunc". */
20705 if (STRCMP(theline, skip_until) == 0)
20706 {
20707 vim_free(skip_until);
20708 skip_until = NULL;
20709 }
20710 }
20711 else
20712 {
20713 /* skip ':' and blanks*/
20714 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
20715 ;
20716
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020717 /* Check for "endfunction". */
20718 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020719 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020720 if (line_arg == NULL)
20721 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722 break;
20723 }
20724
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020725 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 * at "end". */
20727 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
20728 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020729 else if (STRNCMP(p, "if", 2) == 0
20730 || STRNCMP(p, "wh", 2) == 0
20731 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 || STRNCMP(p, "try", 3) == 0)
20733 indent += 2;
20734
20735 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020736 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020738 if (*p == '!')
20739 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740 p += eval_fname_script(p);
20741 if (ASCII_ISALPHA(*p))
20742 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020743 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 if (*skipwhite(p) == '(')
20745 {
20746 ++nesting;
20747 indent += 2;
20748 }
20749 }
20750 }
20751
20752 /* Check for ":append" or ":insert". */
20753 p = skip_range(p, NULL);
20754 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
20755 || (p[0] == 'i'
20756 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
20757 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
20758 skip_until = vim_strsave((char_u *)".");
20759
20760 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20761 arg = skipwhite(skiptowhite(p));
20762 if (arg[0] == '<' && arg[1] =='<'
20763 && ((p[0] == 'p' && p[1] == 'y'
20764 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
20765 || (p[0] == 'p' && p[1] == 'e'
20766 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
20767 || (p[0] == 't' && p[1] == 'c'
20768 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
20769 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
20770 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000020771 || (p[0] == 'm' && p[1] == 'z'
20772 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020773 ))
20774 {
20775 /* ":python <<" continues until a dot, like ":append" */
20776 p = skipwhite(arg + 2);
20777 if (*p == NUL)
20778 skip_until = vim_strsave((char_u *)".");
20779 else
20780 skip_until = vim_strsave(p);
20781 }
20782 }
20783
20784 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020785 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020786 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020787 if (line_arg == NULL)
20788 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020790 }
20791
20792 /* Copy the line to newly allocated memory. get_one_sourceline()
20793 * allocates 250 bytes per line, this saves 80% on average. The cost
20794 * is an extra alloc/free. */
20795 p = vim_strsave(theline);
20796 if (p != NULL)
20797 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020798 if (line_arg == NULL)
20799 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000020800 theline = p;
20801 }
20802
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000020803 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
20804
20805 /* Add NULL lines for continuation lines, so that the line count is
20806 * equal to the index in the growarray. */
20807 while (sourcing_lnum_off-- > 0)
20808 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000020809
20810 /* Check for end of eap->arg. */
20811 if (line_arg != NULL && *line_arg == NUL)
20812 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020813 }
20814
20815 /* Don't define the function when skipping commands or when an error was
20816 * detected. */
20817 if (eap->skip || did_emsg)
20818 goto erret;
20819
20820 /*
20821 * If there are no errors, add the function
20822 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020823 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020824 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020825 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000020826 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020827 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020828 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020829 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020830 goto erret;
20831 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020832
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020833 fp = find_func(name);
20834 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020836 if (!eap->forceit)
20837 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020838 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020839 goto erret;
20840 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020841 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020842 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000020843 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020844 name);
20845 goto erret;
20846 }
20847 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020848 ga_clear_strings(&(fp->uf_args));
20849 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020850 vim_free(name);
20851 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 }
20854 else
20855 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020856 char numbuf[20];
20857
20858 fp = NULL;
20859 if (fudi.fd_newkey == NULL && !eap->forceit)
20860 {
20861 EMSG(_(e_funcdict));
20862 goto erret;
20863 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000020864 if (fudi.fd_di == NULL)
20865 {
20866 /* Can't add a function to a locked dictionary */
20867 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
20868 goto erret;
20869 }
20870 /* Can't change an existing function if it is locked */
20871 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
20872 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020873
20874 /* Give the function a sequential number. Can only be used with a
20875 * Funcref! */
20876 vim_free(name);
20877 sprintf(numbuf, "%d", ++func_nr);
20878 name = vim_strsave((char_u *)numbuf);
20879 if (name == NULL)
20880 goto erret;
20881 }
20882
20883 if (fp == NULL)
20884 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020885 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020886 {
20887 int slen, plen;
20888 char_u *scriptname;
20889
20890 /* Check that the autoload name matches the script name. */
20891 j = FAIL;
20892 if (sourcing_name != NULL)
20893 {
20894 scriptname = autoload_name(name);
20895 if (scriptname != NULL)
20896 {
20897 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020898 plen = (int)STRLEN(p);
20899 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020900 if (slen > plen && fnamecmp(p,
20901 sourcing_name + slen - plen) == 0)
20902 j = OK;
20903 vim_free(scriptname);
20904 }
20905 }
20906 if (j == FAIL)
20907 {
20908 EMSG2(_("E746: Function name does not match script file name: %s"), name);
20909 goto erret;
20910 }
20911 }
20912
20913 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 if (fp == NULL)
20915 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020916
20917 if (fudi.fd_dict != NULL)
20918 {
20919 if (fudi.fd_di == NULL)
20920 {
20921 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020922 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020923 if (fudi.fd_di == NULL)
20924 {
20925 vim_free(fp);
20926 goto erret;
20927 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020928 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
20929 {
20930 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000020931 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000020932 goto erret;
20933 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020934 }
20935 else
20936 /* overwrite existing dict entry */
20937 clear_tv(&fudi.fd_di->di_tv);
20938 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020939 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020940 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020941 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020942
20943 /* behave like "dict" was used */
20944 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020945 }
20946
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020948 STRCPY(fp->uf_name, name);
20949 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020950 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020951 fp->uf_args = newargs;
20952 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000020953#ifdef FEAT_PROFILE
20954 fp->uf_tml_count = NULL;
20955 fp->uf_tml_total = NULL;
20956 fp->uf_tml_self = NULL;
20957 fp->uf_profiling = FALSE;
20958 if (prof_def_func())
20959 func_do_profile(fp);
20960#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020961 fp->uf_varargs = varargs;
20962 fp->uf_flags = flags;
20963 fp->uf_calls = 0;
20964 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020965 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020966
20967erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 ga_clear_strings(&newargs);
20969 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020970ret_free:
20971 vim_free(skip_until);
20972 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975}
20976
20977/*
20978 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000020979 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020980 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020981 * flags:
20982 * TFN_INT: internal function name OK
20983 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000020984 * Advances "pp" to just after the function name (if no error).
20985 */
20986 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020987trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988 char_u **pp;
20989 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020990 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000020991 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000020993 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 char_u *start;
20995 char_u *end;
20996 int lead;
20997 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000020999 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021000
21001 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021004
21005 /* Check for hard coded <SNR>: already translated function ID (from a user
21006 * command). */
21007 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21008 && (*pp)[2] == (int)KE_SNR)
21009 {
21010 *pp += 3;
21011 len = get_id_len(pp) + 3;
21012 return vim_strnsave(start, len);
21013 }
21014
21015 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21016 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021018 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021020
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021021 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21022 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021023 if (end == start)
21024 {
21025 if (!skip)
21026 EMSG(_("E129: Function name required"));
21027 goto theend;
21028 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021029 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021030 {
21031 /*
21032 * Report an invalid expression in braces, unless the expression
21033 * evaluation has been cancelled due to an aborting error, an
21034 * interrupt, or an exception.
21035 */
21036 if (!aborting())
21037 {
21038 if (end != NULL)
21039 EMSG2(_(e_invarg2), start);
21040 }
21041 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021042 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021043 goto theend;
21044 }
21045
21046 if (lv.ll_tv != NULL)
21047 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021048 if (fdp != NULL)
21049 {
21050 fdp->fd_dict = lv.ll_dict;
21051 fdp->fd_newkey = lv.ll_newkey;
21052 lv.ll_newkey = NULL;
21053 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021054 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021055 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21056 {
21057 name = vim_strsave(lv.ll_tv->vval.v_string);
21058 *pp = end;
21059 }
21060 else
21061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021062 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21063 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021064 EMSG(_(e_funcref));
21065 else
21066 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021067 name = NULL;
21068 }
21069 goto theend;
21070 }
21071
21072 if (lv.ll_name == NULL)
21073 {
21074 /* Error found, but continue after the function name. */
21075 *pp = end;
21076 goto theend;
21077 }
21078
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021079 /* Check if the name is a Funcref. If so, use the value. */
21080 if (lv.ll_exp_name != NULL)
21081 {
21082 len = (int)STRLEN(lv.ll_exp_name);
21083 name = deref_func_name(lv.ll_exp_name, &len);
21084 if (name == lv.ll_exp_name)
21085 name = NULL;
21086 }
21087 else
21088 {
21089 len = (int)(end - *pp);
21090 name = deref_func_name(*pp, &len);
21091 if (name == *pp)
21092 name = NULL;
21093 }
21094 if (name != NULL)
21095 {
21096 name = vim_strsave(name);
21097 *pp = end;
21098 goto theend;
21099 }
21100
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021101 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021102 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021103 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021104 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21105 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21106 {
21107 /* When there was "s:" already or the name expanded to get a
21108 * leading "s:" then remove it. */
21109 lv.ll_name += 2;
21110 len -= 2;
21111 lead = 2;
21112 }
21113 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021114 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021115 {
21116 if (lead == 2) /* skip over "s:" */
21117 lv.ll_name += 2;
21118 len = (int)(end - lv.ll_name);
21119 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021120
21121 /*
21122 * Copy the function name to allocated memory.
21123 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21124 * Accept <SNR>123_name() outside a script.
21125 */
21126 if (skip)
21127 lead = 0; /* do nothing */
21128 else if (lead > 0)
21129 {
21130 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021131 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21132 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021133 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021134 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021135 if (current_SID <= 0)
21136 {
21137 EMSG(_(e_usingsid));
21138 goto theend;
21139 }
21140 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21141 lead += (int)STRLEN(sid_buf);
21142 }
21143 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021144 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021145 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021146 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021147 goto theend;
21148 }
21149 name = alloc((unsigned)(len + lead + 1));
21150 if (name != NULL)
21151 {
21152 if (lead > 0)
21153 {
21154 name[0] = K_SPECIAL;
21155 name[1] = KS_EXTRA;
21156 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021157 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021158 STRCPY(name + 3, sid_buf);
21159 }
21160 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21161 name[len + lead] = NUL;
21162 }
21163 *pp = end;
21164
21165theend:
21166 clear_lval(&lv);
21167 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168}
21169
21170/*
21171 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21172 * Return 2 if "p" starts with "s:".
21173 * Return 0 otherwise.
21174 */
21175 static int
21176eval_fname_script(p)
21177 char_u *p;
21178{
21179 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21180 || STRNICMP(p + 1, "SNR>", 4) == 0))
21181 return 5;
21182 if (p[0] == 's' && p[1] == ':')
21183 return 2;
21184 return 0;
21185}
21186
21187/*
21188 * Return TRUE if "p" starts with "<SID>" or "s:".
21189 * Only works if eval_fname_script() returned non-zero for "p"!
21190 */
21191 static int
21192eval_fname_sid(p)
21193 char_u *p;
21194{
21195 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21196}
21197
21198/*
21199 * List the head of the function: "name(arg1, arg2)".
21200 */
21201 static void
21202list_func_head(fp, indent)
21203 ufunc_T *fp;
21204 int indent;
21205{
21206 int j;
21207
21208 msg_start();
21209 if (indent)
21210 MSG_PUTS(" ");
21211 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021212 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 {
21214 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021215 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 }
21217 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021218 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021220 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 {
21222 if (j)
21223 MSG_PUTS(", ");
21224 msg_puts(FUNCARG(fp, j));
21225 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021226 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 {
21228 if (j)
21229 MSG_PUTS(", ");
21230 MSG_PUTS("...");
21231 }
21232 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021233 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000021234 if (p_verbose > 0)
21235 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236}
21237
21238/*
21239 * Find a function by name, return pointer to it in ufuncs.
21240 * Return NULL for unknown function.
21241 */
21242 static ufunc_T *
21243find_func(name)
21244 char_u *name;
21245{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021246 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021248 hi = hash_find(&func_hashtab, name);
21249 if (!HASHITEM_EMPTY(hi))
21250 return HI2UF(hi);
21251 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252}
21253
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021254#if defined(EXITFREE) || defined(PROTO)
21255 void
21256free_all_functions()
21257{
21258 hashitem_T *hi;
21259
21260 /* Need to start all over every time, because func_free() may change the
21261 * hash table. */
21262 while (func_hashtab.ht_used > 0)
21263 for (hi = func_hashtab.ht_array; ; ++hi)
21264 if (!HASHITEM_EMPTY(hi))
21265 {
21266 func_free(HI2UF(hi));
21267 break;
21268 }
21269}
21270#endif
21271
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021272/*
21273 * Return TRUE if a function "name" exists.
21274 */
21275 static int
21276function_exists(name)
21277 char_u *name;
21278{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021279 char_u *nm = name;
21280 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021281 int n = FALSE;
21282
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000021283 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000021284 nm = skipwhite(nm);
21285
21286 /* Only accept "funcname", "funcname ", "funcname (..." and
21287 * "funcname(...", not "funcname!...". */
21288 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021289 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021290 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021291 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021292 else
21293 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021294 }
Bram Moolenaar79783442006-05-05 21:18:03 +000021295 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021296 return n;
21297}
21298
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021299/*
21300 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021301 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021302 */
21303 static int
21304builtin_function(name)
21305 char_u *name;
21306{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021307 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
21308 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021309}
21310
Bram Moolenaar05159a02005-02-26 23:04:13 +000021311#if defined(FEAT_PROFILE) || defined(PROTO)
21312/*
21313 * Start profiling function "fp".
21314 */
21315 static void
21316func_do_profile(fp)
21317 ufunc_T *fp;
21318{
Bram Moolenaar904c6222010-07-24 16:57:39 +020021319 int len = fp->uf_lines.ga_len;
21320
21321 if (len == 0)
21322 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000021323 fp->uf_tm_count = 0;
21324 profile_zero(&fp->uf_tm_self);
21325 profile_zero(&fp->uf_tm_total);
21326 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021327 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021328 if (fp->uf_tml_total == NULL)
21329 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020021330 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021331 if (fp->uf_tml_self == NULL)
21332 fp->uf_tml_self = (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 fp->uf_tml_idx = -1;
21335 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
21336 || fp->uf_tml_self == NULL)
21337 return; /* out of memory */
21338
21339 fp->uf_profiling = TRUE;
21340}
21341
21342/*
21343 * Dump the profiling results for all functions in file "fd".
21344 */
21345 void
21346func_dump_profile(fd)
21347 FILE *fd;
21348{
21349 hashitem_T *hi;
21350 int todo;
21351 ufunc_T *fp;
21352 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000021353 ufunc_T **sorttab;
21354 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021355
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021356 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021357 if (todo == 0)
21358 return; /* nothing to dump */
21359
Bram Moolenaar73830342005-02-28 22:48:19 +000021360 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
21361
Bram Moolenaar05159a02005-02-26 23:04:13 +000021362 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
21363 {
21364 if (!HASHITEM_EMPTY(hi))
21365 {
21366 --todo;
21367 fp = HI2UF(hi);
21368 if (fp->uf_profiling)
21369 {
Bram Moolenaar73830342005-02-28 22:48:19 +000021370 if (sorttab != NULL)
21371 sorttab[st_len++] = fp;
21372
Bram Moolenaar05159a02005-02-26 23:04:13 +000021373 if (fp->uf_name[0] == K_SPECIAL)
21374 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
21375 else
21376 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
21377 if (fp->uf_tm_count == 1)
21378 fprintf(fd, "Called 1 time\n");
21379 else
21380 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
21381 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
21382 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
21383 fprintf(fd, "\n");
21384 fprintf(fd, "count total (s) self (s)\n");
21385
21386 for (i = 0; i < fp->uf_lines.ga_len; ++i)
21387 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021388 if (FUNCLINE(fp, i) == NULL)
21389 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000021390 prof_func_line(fd, fp->uf_tml_count[i],
21391 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021392 fprintf(fd, "%s\n", FUNCLINE(fp, i));
21393 }
21394 fprintf(fd, "\n");
21395 }
21396 }
21397 }
Bram Moolenaar73830342005-02-28 22:48:19 +000021398
21399 if (sorttab != NULL && st_len > 0)
21400 {
21401 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21402 prof_total_cmp);
21403 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
21404 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
21405 prof_self_cmp);
21406 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
21407 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000021408
21409 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021410}
Bram Moolenaar73830342005-02-28 22:48:19 +000021411
21412 static void
21413prof_sort_list(fd, sorttab, st_len, title, prefer_self)
21414 FILE *fd;
21415 ufunc_T **sorttab;
21416 int st_len;
21417 char *title;
21418 int prefer_self; /* when equal print only self time */
21419{
21420 int i;
21421 ufunc_T *fp;
21422
21423 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
21424 fprintf(fd, "count total (s) self (s) function\n");
21425 for (i = 0; i < 20 && i < st_len; ++i)
21426 {
21427 fp = sorttab[i];
21428 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
21429 prefer_self);
21430 if (fp->uf_name[0] == K_SPECIAL)
21431 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
21432 else
21433 fprintf(fd, " %s()\n", fp->uf_name);
21434 }
21435 fprintf(fd, "\n");
21436}
21437
21438/*
21439 * Print the count and times for one function or function line.
21440 */
21441 static void
21442prof_func_line(fd, count, total, self, prefer_self)
21443 FILE *fd;
21444 int count;
21445 proftime_T *total;
21446 proftime_T *self;
21447 int prefer_self; /* when equal print only self time */
21448{
21449 if (count > 0)
21450 {
21451 fprintf(fd, "%5d ", count);
21452 if (prefer_self && profile_equal(total, self))
21453 fprintf(fd, " ");
21454 else
21455 fprintf(fd, "%s ", profile_msg(total));
21456 if (!prefer_self && profile_equal(total, self))
21457 fprintf(fd, " ");
21458 else
21459 fprintf(fd, "%s ", profile_msg(self));
21460 }
21461 else
21462 fprintf(fd, " ");
21463}
21464
21465/*
21466 * Compare function for total time sorting.
21467 */
21468 static int
21469#ifdef __BORLANDC__
21470_RTLENTRYF
21471#endif
21472prof_total_cmp(s1, s2)
21473 const void *s1;
21474 const void *s2;
21475{
21476 ufunc_T *p1, *p2;
21477
21478 p1 = *(ufunc_T **)s1;
21479 p2 = *(ufunc_T **)s2;
21480 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
21481}
21482
21483/*
21484 * Compare function for self time sorting.
21485 */
21486 static int
21487#ifdef __BORLANDC__
21488_RTLENTRYF
21489#endif
21490prof_self_cmp(s1, s2)
21491 const void *s1;
21492 const void *s2;
21493{
21494 ufunc_T *p1, *p2;
21495
21496 p1 = *(ufunc_T **)s1;
21497 p2 = *(ufunc_T **)s2;
21498 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
21499}
21500
Bram Moolenaar05159a02005-02-26 23:04:13 +000021501#endif
21502
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021503/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021504 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021505 * Return TRUE if a package was loaded.
21506 */
21507 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021508script_autoload(name, reload)
21509 char_u *name;
21510 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021511{
21512 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021513 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021514 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021515 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021516
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020021517 /* Return quickly when autoload disabled. */
21518 if (no_autoload)
21519 return FALSE;
21520
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021521 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021522 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021523 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021524 return FALSE;
21525
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021526 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021527
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021528 /* Find the name in the list of previously loaded package names. Skip
21529 * "autoload/", it's always the same. */
21530 for (i = 0; i < ga_loaded.ga_len; ++i)
21531 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
21532 break;
21533 if (!reload && i < ga_loaded.ga_len)
21534 ret = FALSE; /* was loaded already */
21535 else
21536 {
21537 /* Remember the name if it wasn't loaded already. */
21538 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
21539 {
21540 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
21541 tofree = NULL;
21542 }
21543
21544 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000021545 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021546 ret = TRUE;
21547 }
21548
21549 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021550 return ret;
21551}
21552
21553/*
21554 * Return the autoload script name for a function or variable name.
21555 * Returns NULL when out of memory.
21556 */
21557 static char_u *
21558autoload_name(name)
21559 char_u *name;
21560{
21561 char_u *p;
21562 char_u *scriptname;
21563
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021564 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021565 scriptname = alloc((unsigned)(STRLEN(name) + 14));
21566 if (scriptname == NULL)
21567 return FALSE;
21568 STRCPY(scriptname, "autoload/");
21569 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021570 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021571 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021572 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021573 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021574 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021575}
21576
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21578
21579/*
21580 * Function given to ExpandGeneric() to obtain the list of user defined
21581 * function names.
21582 */
21583 char_u *
21584get_user_func_name(xp, idx)
21585 expand_T *xp;
21586 int idx;
21587{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021588 static long_u done;
21589 static hashitem_T *hi;
21590 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591
21592 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021593 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021594 done = 0;
21595 hi = func_hashtab.ht_array;
21596 }
21597 if (done < func_hashtab.ht_used)
21598 {
21599 if (done++ > 0)
21600 ++hi;
21601 while (HASHITEM_EMPTY(hi))
21602 ++hi;
21603 fp = HI2UF(hi);
21604
21605 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
21606 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021607
21608 cat_func_name(IObuff, fp);
21609 if (xp->xp_context != EXPAND_USER_FUNC)
21610 {
21611 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021612 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 STRCAT(IObuff, ")");
21614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615 return IObuff;
21616 }
21617 return NULL;
21618}
21619
21620#endif /* FEAT_CMDL_COMPL */
21621
21622/*
21623 * Copy the function name of "fp" to buffer "buf".
21624 * "buf" must be able to hold the function name plus three bytes.
21625 * Takes care of script-local function names.
21626 */
21627 static void
21628cat_func_name(buf, fp)
21629 char_u *buf;
21630 ufunc_T *fp;
21631{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021632 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 {
21634 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021635 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 }
21637 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021638 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639}
21640
21641/*
21642 * ":delfunction {name}"
21643 */
21644 void
21645ex_delfunction(eap)
21646 exarg_T *eap;
21647{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021648 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021649 char_u *p;
21650 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000021651 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652
21653 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021654 name = trans_function_name(&p, eap->skip, 0, &fudi);
21655 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021657 {
21658 if (fudi.fd_dict != NULL && !eap->skip)
21659 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 if (!ends_excmd(*skipwhite(p)))
21663 {
21664 vim_free(name);
21665 EMSG(_(e_trailing));
21666 return;
21667 }
21668 eap->nextcmd = check_nextcmd(p);
21669 if (eap->nextcmd != NULL)
21670 *p = NUL;
21671
21672 if (!eap->skip)
21673 fp = find_func(name);
21674 vim_free(name);
21675
21676 if (!eap->skip)
21677 {
21678 if (fp == NULL)
21679 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000021680 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681 return;
21682 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021683 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 {
21685 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
21686 return;
21687 }
21688
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021689 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021691 /* Delete the dict item that refers to the function, it will
21692 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021693 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021695 else
21696 func_free(fp);
21697 }
21698}
21699
21700/*
21701 * Free a function and remove it from the list of functions.
21702 */
21703 static void
21704func_free(fp)
21705 ufunc_T *fp;
21706{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021707 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708
21709 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021710 ga_clear_strings(&(fp->uf_args));
21711 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000021712#ifdef FEAT_PROFILE
21713 vim_free(fp->uf_tml_count);
21714 vim_free(fp->uf_tml_total);
21715 vim_free(fp->uf_tml_self);
21716#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021717
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021718 /* remove the function from the function hashtable */
21719 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
21720 if (HASHITEM_EMPTY(hi))
21721 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021722 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021723 hash_remove(&func_hashtab, hi);
21724
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021725 vim_free(fp);
21726}
21727
21728/*
21729 * Unreference a Function: decrement the reference count and free it when it
21730 * becomes zero. Only for numbered functions.
21731 */
21732 static void
21733func_unref(name)
21734 char_u *name;
21735{
21736 ufunc_T *fp;
21737
21738 if (name != NULL && isdigit(*name))
21739 {
21740 fp = find_func(name);
21741 if (fp == NULL)
21742 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021743 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021744 {
21745 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021746 * when "uf_calls" becomes zero. */
21747 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021748 func_free(fp);
21749 }
21750 }
21751}
21752
21753/*
21754 * Count a reference to a Function.
21755 */
21756 static void
21757func_ref(name)
21758 char_u *name;
21759{
21760 ufunc_T *fp;
21761
21762 if (name != NULL && isdigit(*name))
21763 {
21764 fp = find_func(name);
21765 if (fp == NULL)
21766 EMSG2(_(e_intern2), "func_ref()");
21767 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021768 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769 }
21770}
21771
21772/*
21773 * Call a user function.
21774 */
21775 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000021776call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 ufunc_T *fp; /* pointer to function */
21778 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000021779 typval_T *argvars; /* arguments */
21780 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 linenr_T firstline; /* first line of range */
21782 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000021783 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784{
Bram Moolenaar33570922005-01-25 22:26:29 +000021785 char_u *save_sourcing_name;
21786 linenr_T save_sourcing_lnum;
21787 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021788 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000021789 int save_did_emsg;
21790 static int depth = 0;
21791 dictitem_T *v;
21792 int fixvar_idx = 0; /* index in fixvar[] */
21793 int i;
21794 int ai;
21795 char_u numbuf[NUMBUFLEN];
21796 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021797#ifdef FEAT_PROFILE
21798 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021799 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801
21802 /* If depth of calling is getting too high, don't execute the function */
21803 if (depth >= p_mfd)
21804 {
21805 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021806 rettv->v_type = VAR_NUMBER;
21807 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 return;
21809 }
21810 ++depth;
21811
21812 line_breakcheck(); /* check for CTRL-C hit */
21813
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021814 fc = (funccall_T *)alloc(sizeof(funccall_T));
21815 fc->caller = current_funccal;
21816 current_funccal = fc;
21817 fc->func = fp;
21818 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021819 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021820 fc->linenr = 0;
21821 fc->returned = FALSE;
21822 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021824 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
21825 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021826
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021828 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000021829 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21830 * each argument variable and saves a lot of time.
21831 */
21832 /*
21833 * Init l: variables.
21834 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021835 init_var_dict(&fc->l_vars, &fc->l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000021836 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021837 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021838 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21839 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021840 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000021841 name = v->di_key;
21842 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000021843 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021844 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021845 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021846 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000021847 v->di_tv.vval.v_dict = selfdict;
21848 ++selfdict->dv_refcount;
21849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021850
Bram Moolenaar33570922005-01-25 22:26:29 +000021851 /*
21852 * Init a: variables.
21853 * Set a:0 to "argcount".
21854 * Set a:000 to a list with room for the "..." arguments.
21855 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021856 init_var_dict(&fc->l_avars, &fc->l_avars_var);
21857 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021858 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021859 /* Use "name" to avoid a warning from some compiler that checks the
21860 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021861 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000021862 name = v->di_key;
21863 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000021864 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021865 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021866 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021867 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021868 v->di_tv.vval.v_list = &fc->l_varlist;
21869 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
21870 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
21871 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021872
21873 /*
21874 * Set a:firstline to "firstline" and a:lastline to "lastline".
21875 * Set a:name to named arguments.
21876 * Set a:N to the "..." arguments.
21877 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021878 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021879 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021880 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000021881 (varnumber_T)lastline);
21882 for (i = 0; i < argcount; ++i)
21883 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021884 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021885 if (ai < 0)
21886 /* named argument a:name */
21887 name = FUNCARG(fp, i);
21888 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021889 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021890 /* "..." argument a:1, a:2, etc. */
21891 sprintf((char *)numbuf, "%d", ai + 1);
21892 name = numbuf;
21893 }
21894 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
21895 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021896 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000021897 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21898 }
21899 else
21900 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021901 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
21902 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000021903 if (v == NULL)
21904 break;
21905 v->di_flags = DI_FLAGS_RO;
21906 }
21907 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021908 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000021909
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021910 /* Note: the values are copied directly to avoid alloc/free.
21911 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021913 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000021914
21915 if (ai >= 0 && ai < MAX_FUNC_ARGS)
21916 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021917 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
21918 fc->l_listitems[ai].li_tv = argvars[i];
21919 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021920 }
21921 }
21922
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 /* Don't redraw while executing the function. */
21924 ++RedrawingDisabled;
21925 save_sourcing_name = sourcing_name;
21926 save_sourcing_lnum = sourcing_lnum;
21927 sourcing_lnum = 1;
21928 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021929 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930 if (sourcing_name != NULL)
21931 {
21932 if (save_sourcing_name != NULL
21933 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
21934 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
21935 else
21936 STRCPY(sourcing_name, "function ");
21937 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
21938
21939 if (p_verbose >= 12)
21940 {
21941 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021942 verbose_enter_scroll();
21943
Bram Moolenaar555b2802005-05-19 21:08:39 +000021944 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945 if (p_verbose >= 14)
21946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000021948 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000021949 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021950 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951
21952 msg_puts((char_u *)"(");
21953 for (i = 0; i < argcount; ++i)
21954 {
21955 if (i > 0)
21956 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021957 if (argvars[i].v_type == VAR_NUMBER)
21958 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 else
21960 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000021961 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
21962 if (s != NULL)
21963 {
21964 trunc_string(s, buf, MSG_BUF_CLEN);
21965 msg_puts(buf);
21966 vim_free(tofree);
21967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968 }
21969 }
21970 msg_puts((char_u *)")");
21971 }
21972 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000021973
21974 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000021975 --no_wait_return;
21976 }
21977 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000021978#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000021979 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000021980 {
21981 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
21982 func_do_profile(fp);
21983 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000021984 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000021985 {
21986 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000021987 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000021988 profile_zero(&fp->uf_tm_children);
21989 }
21990 script_prof_save(&wait_start);
21991 }
21992#endif
21993
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021995 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 save_did_emsg = did_emsg;
21997 did_emsg = FALSE;
21998
21999 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022000 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22002
22003 --RedrawingDisabled;
22004
22005 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022006 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022008 clear_tv(rettv);
22009 rettv->v_type = VAR_NUMBER;
22010 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011 }
22012
Bram Moolenaar05159a02005-02-26 23:04:13 +000022013#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022014 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022015 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022016 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022017 profile_end(&call_start);
22018 profile_sub_wait(&wait_start, &call_start);
22019 profile_add(&fp->uf_tm_total, &call_start);
22020 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022021 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022022 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022023 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22024 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022025 }
22026 }
22027#endif
22028
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029 /* when being verbose, mention the return value */
22030 if (p_verbose >= 12)
22031 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022033 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022036 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022037 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022038 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022039 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022040 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022042 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022043 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022044 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022045 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022046
Bram Moolenaar555b2802005-05-19 21:08:39 +000022047 /* The value may be very long. Skip the middle part, so that we
22048 * have some idea how it starts and ends. smsg() would always
22049 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022050 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022051 if (s != NULL)
22052 {
22053 trunc_string(s, buf, MSG_BUF_CLEN);
22054 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
22055 vim_free(tofree);
22056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 }
22058 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022059
22060 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061 --no_wait_return;
22062 }
22063
22064 vim_free(sourcing_name);
22065 sourcing_name = save_sourcing_name;
22066 sourcing_lnum = save_sourcing_lnum;
22067 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022068#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022069 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022070 script_prof_restore(&wait_start);
22071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022072
22073 if (p_verbose >= 12 && sourcing_name != NULL)
22074 {
22075 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022076 verbose_enter_scroll();
22077
Bram Moolenaar555b2802005-05-19 21:08:39 +000022078 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022080
22081 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 --no_wait_return;
22083 }
22084
22085 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022086 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022088
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022089 /* If the a:000 list and the l: and a: dicts are not referenced we can
22090 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022091 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22092 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22093 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22094 {
22095 free_funccal(fc, FALSE);
22096 }
22097 else
22098 {
22099 hashitem_T *hi;
22100 listitem_T *li;
22101 int todo;
22102
22103 /* "fc" is still in use. This can happen when returning "a:000" or
22104 * assigning "l:" to a global variable.
22105 * Link "fc" in the list for garbage collection later. */
22106 fc->caller = previous_funccal;
22107 previous_funccal = fc;
22108
22109 /* Make a copy of the a: variables, since we didn't do that above. */
22110 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22111 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22112 {
22113 if (!HASHITEM_EMPTY(hi))
22114 {
22115 --todo;
22116 v = HI2DI(hi);
22117 copy_tv(&v->di_tv, &v->di_tv);
22118 }
22119 }
22120
22121 /* Make a copy of the a:000 items, since we didn't do that above. */
22122 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22123 copy_tv(&li->li_tv, &li->li_tv);
22124 }
22125}
22126
22127/*
22128 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022129 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022130 */
22131 static int
22132can_free_funccal(fc, copyID)
22133 funccall_T *fc;
22134 int copyID;
22135{
22136 return (fc->l_varlist.lv_copyID != copyID
22137 && fc->l_vars.dv_copyID != copyID
22138 && fc->l_avars.dv_copyID != copyID);
22139}
22140
22141/*
22142 * Free "fc" and what it contains.
22143 */
22144 static void
22145free_funccal(fc, free_val)
22146 funccall_T *fc;
22147 int free_val; /* a: vars were allocated */
22148{
22149 listitem_T *li;
22150
22151 /* The a: variables typevals may not have been allocated, only free the
22152 * allocated variables. */
22153 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22154
22155 /* free all l: variables */
22156 vars_clear(&fc->l_vars.dv_hashtab);
22157
22158 /* Free the a:000 variables if they were allocated. */
22159 if (free_val)
22160 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22161 clear_tv(&li->li_tv);
22162
22163 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164}
22165
22166/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022167 * Add a number variable "name" to dict "dp" with value "nr".
22168 */
22169 static void
22170add_nr_var(dp, v, name, nr)
22171 dict_T *dp;
22172 dictitem_T *v;
22173 char *name;
22174 varnumber_T nr;
22175{
22176 STRCPY(v->di_key, name);
22177 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22178 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22179 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022180 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022181 v->di_tv.vval.v_number = nr;
22182}
22183
22184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022185 * ":return [expr]"
22186 */
22187 void
22188ex_return(eap)
22189 exarg_T *eap;
22190{
22191 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022192 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022193 int returning = FALSE;
22194
22195 if (current_funccal == NULL)
22196 {
22197 EMSG(_("E133: :return not inside a function"));
22198 return;
22199 }
22200
22201 if (eap->skip)
22202 ++emsg_skip;
22203
22204 eap->nextcmd = NULL;
22205 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022206 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 {
22208 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022209 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022211 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 }
22213 /* It's safer to return also on error. */
22214 else if (!eap->skip)
22215 {
22216 /*
22217 * Return unless the expression evaluation has been cancelled due to an
22218 * aborting error, an interrupt, or an exception.
22219 */
22220 if (!aborting())
22221 returning = do_return(eap, FALSE, TRUE, NULL);
22222 }
22223
22224 /* When skipping or the return gets pending, advance to the next command
22225 * in this line (!returning). Otherwise, ignore the rest of the line.
22226 * Following lines will be ignored by get_func_line(). */
22227 if (returning)
22228 eap->nextcmd = NULL;
22229 else if (eap->nextcmd == NULL) /* no argument */
22230 eap->nextcmd = check_nextcmd(arg);
22231
22232 if (eap->skip)
22233 --emsg_skip;
22234}
22235
22236/*
22237 * Return from a function. Possibly makes the return pending. Also called
22238 * for a pending return at the ":endtry" or after returning from an extra
22239 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000022240 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022241 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242 * FALSE when the return gets pending.
22243 */
22244 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022245do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022246 exarg_T *eap;
22247 int reanimate;
22248 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022249 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250{
22251 int idx;
22252 struct condstack *cstack = eap->cstack;
22253
22254 if (reanimate)
22255 /* Undo the return. */
22256 current_funccal->returned = FALSE;
22257
22258 /*
22259 * Cleanup (and inactivate) conditionals, but stop when a try conditional
22260 * not in its finally clause (which then is to be executed next) is found.
22261 * In this case, make the ":return" pending for execution at the ":endtry".
22262 * Otherwise, return normally.
22263 */
22264 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
22265 if (idx >= 0)
22266 {
22267 cstack->cs_pending[idx] = CSTP_RETURN;
22268
22269 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022270 /* A pending return again gets pending. "rettv" points to an
22271 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022273 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 else
22275 {
22276 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022277 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022279 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022281 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 {
22283 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022284 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022285 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 else
22287 EMSG(_(e_outofmem));
22288 }
22289 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022290 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291
22292 if (reanimate)
22293 {
22294 /* The pending return value could be overwritten by a ":return"
22295 * without argument in a finally clause; reset the default
22296 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022297 current_funccal->rettv->v_type = VAR_NUMBER;
22298 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 }
22300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022301 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022302 }
22303 else
22304 {
22305 current_funccal->returned = TRUE;
22306
22307 /* If the return is carried out now, store the return value. For
22308 * a return immediately after reanimation, the value is already
22309 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022310 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022311 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022312 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000022313 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022315 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 }
22317 }
22318
22319 return idx < 0;
22320}
22321
22322/*
22323 * Free the variable with a pending return value.
22324 */
22325 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022326discard_pending_return(rettv)
22327 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328{
Bram Moolenaar33570922005-01-25 22:26:29 +000022329 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330}
22331
22332/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022333 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000022334 * is an allocated string. Used by report_pending() for verbose messages.
22335 */
22336 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022337get_return_cmd(rettv)
22338 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022340 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022341 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022342 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022344 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022345 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022346 if (s == NULL)
22347 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022348
22349 STRCPY(IObuff, ":return ");
22350 STRNCPY(IObuff + 8, s, IOSIZE - 8);
22351 if (STRLEN(s) + 8 >= IOSIZE)
22352 STRCPY(IObuff + IOSIZE - 4, "...");
22353 vim_free(tofree);
22354 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355}
22356
22357/*
22358 * Get next function line.
22359 * Called by do_cmdline() to get the next line.
22360 * Returns allocated string, or NULL for end of function.
22361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362 char_u *
22363get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022364 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022366 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367{
Bram Moolenaar33570922005-01-25 22:26:29 +000022368 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022369 ufunc_T *fp = fcp->func;
22370 char_u *retval;
22371 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372
22373 /* If breakpoints have been added/deleted need to check for it. */
22374 if (fcp->dbg_tick != debug_tick)
22375 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022376 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 sourcing_lnum);
22378 fcp->dbg_tick = debug_tick;
22379 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022380#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022381 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022382 func_line_end(cookie);
22383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384
Bram Moolenaar05159a02005-02-26 23:04:13 +000022385 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022386 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
22387 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022388 retval = NULL;
22389 else
22390 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022391 /* Skip NULL lines (continuation lines). */
22392 while (fcp->linenr < gap->ga_len
22393 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
22394 ++fcp->linenr;
22395 if (fcp->linenr >= gap->ga_len)
22396 retval = NULL;
22397 else
22398 {
22399 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
22400 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022401#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022402 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022403 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022404#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 }
22407
22408 /* Did we encounter a breakpoint? */
22409 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
22410 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022411 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022413 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 sourcing_lnum);
22415 fcp->dbg_tick = debug_tick;
22416 }
22417
22418 return retval;
22419}
22420
Bram Moolenaar05159a02005-02-26 23:04:13 +000022421#if defined(FEAT_PROFILE) || defined(PROTO)
22422/*
22423 * Called when starting to read a function line.
22424 * "sourcing_lnum" must be correct!
22425 * When skipping lines it may not actually be executed, but we won't find out
22426 * until later and we need to store the time now.
22427 */
22428 void
22429func_line_start(cookie)
22430 void *cookie;
22431{
22432 funccall_T *fcp = (funccall_T *)cookie;
22433 ufunc_T *fp = fcp->func;
22434
22435 if (fp->uf_profiling && sourcing_lnum >= 1
22436 && sourcing_lnum <= fp->uf_lines.ga_len)
22437 {
22438 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022439 /* Skip continuation lines. */
22440 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
22441 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022442 fp->uf_tml_execed = FALSE;
22443 profile_start(&fp->uf_tml_start);
22444 profile_zero(&fp->uf_tml_children);
22445 profile_get_wait(&fp->uf_tml_wait);
22446 }
22447}
22448
22449/*
22450 * Called when actually executing a function line.
22451 */
22452 void
22453func_line_exec(cookie)
22454 void *cookie;
22455{
22456 funccall_T *fcp = (funccall_T *)cookie;
22457 ufunc_T *fp = fcp->func;
22458
22459 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22460 fp->uf_tml_execed = TRUE;
22461}
22462
22463/*
22464 * Called when done with a function line.
22465 */
22466 void
22467func_line_end(cookie)
22468 void *cookie;
22469{
22470 funccall_T *fcp = (funccall_T *)cookie;
22471 ufunc_T *fp = fcp->func;
22472
22473 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
22474 {
22475 if (fp->uf_tml_execed)
22476 {
22477 ++fp->uf_tml_count[fp->uf_tml_idx];
22478 profile_end(&fp->uf_tml_start);
22479 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022480 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000022481 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
22482 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022483 }
22484 fp->uf_tml_idx = -1;
22485 }
22486}
22487#endif
22488
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489/*
22490 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022491 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 */
22493 int
22494func_has_ended(cookie)
22495 void *cookie;
22496{
Bram Moolenaar33570922005-01-25 22:26:29 +000022497 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498
22499 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22500 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022501 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 || fcp->returned);
22503}
22504
22505/*
22506 * return TRUE if cookie indicates a function which "abort"s on errors.
22507 */
22508 int
22509func_has_abort(cookie)
22510 void *cookie;
22511{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022512 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513}
22514
22515#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22516typedef enum
22517{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022518 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
22519 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
22520 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521} var_flavour_T;
22522
22523static var_flavour_T var_flavour __ARGS((char_u *varname));
22524
22525 static var_flavour_T
22526var_flavour(varname)
22527 char_u *varname;
22528{
22529 char_u *p = varname;
22530
22531 if (ASCII_ISUPPER(*p))
22532 {
22533 while (*(++p))
22534 if (ASCII_ISLOWER(*p))
22535 return VAR_FLAVOUR_SESSION;
22536 return VAR_FLAVOUR_VIMINFO;
22537 }
22538 else
22539 return VAR_FLAVOUR_DEFAULT;
22540}
22541#endif
22542
22543#if defined(FEAT_VIMINFO) || defined(PROTO)
22544/*
22545 * Restore global vars that start with a capital from the viminfo file
22546 */
22547 int
22548read_viminfo_varlist(virp, writing)
22549 vir_T *virp;
22550 int writing;
22551{
22552 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022553 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000022554 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555
22556 if (!writing && (find_viminfo_parameter('!') != NULL))
22557 {
22558 tab = vim_strchr(virp->vir_line + 1, '\t');
22559 if (tab != NULL)
22560 {
22561 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022562 switch (*tab)
22563 {
22564 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022565#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022566 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022567#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022568 case 'D': type = VAR_DICT; break;
22569 case 'L': type = VAR_LIST; break;
22570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571
22572 tab = vim_strchr(tab, '\t');
22573 if (tab != NULL)
22574 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022575 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022576 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022577 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022579#ifdef FEAT_FLOAT
22580 else if (type == VAR_FLOAT)
22581 (void)string2float(tab + 1, &tv.vval.v_float);
22582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022584 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022585 if (type == VAR_DICT || type == VAR_LIST)
22586 {
22587 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
22588
22589 if (etv == NULL)
22590 /* Failed to parse back the dict or list, use it as a
22591 * string. */
22592 tv.v_type = VAR_STRING;
22593 else
22594 {
22595 vim_free(tv.vval.v_string);
22596 tv = *etv;
22597 }
22598 }
22599
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022600 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022601
22602 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022603 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022604 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
22605 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022606 }
22607 }
22608 }
22609
22610 return viminfo_readline(virp);
22611}
22612
22613/*
22614 * Write global vars that start with a capital to the viminfo file
22615 */
22616 void
22617write_viminfo_varlist(fp)
22618 FILE *fp;
22619{
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 hashitem_T *hi;
22621 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022622 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022623 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022624 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022625 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022626 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627
22628 if (find_viminfo_parameter('!') == NULL)
22629 return;
22630
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022631 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000022632
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022633 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022636 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022638 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022639 this_var = HI2DI(hi);
22640 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022641 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022642 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000022643 {
22644 case VAR_STRING: s = "STR"; break;
22645 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022646#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022647 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022648#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020022649 case VAR_DICT: s = "DIC"; break;
22650 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000022651 default: continue;
22652 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022653 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000022654 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022655 if (p != NULL)
22656 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000022657 vim_free(tofree);
22658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 }
22660 }
22661}
22662#endif
22663
22664#if defined(FEAT_SESSION) || defined(PROTO)
22665 int
22666store_session_globals(fd)
22667 FILE *fd;
22668{
Bram Moolenaar33570922005-01-25 22:26:29 +000022669 hashitem_T *hi;
22670 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000022671 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022672 char_u *p, *t;
22673
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022674 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000022675 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022677 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022679 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022680 this_var = HI2DI(hi);
22681 if ((this_var->di_tv.v_type == VAR_NUMBER
22682 || this_var->di_tv.v_type == VAR_STRING)
22683 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000022684 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022685 /* Escape special characters with a backslash. Turn a LF and
22686 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022687 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000022688 (char_u *)"\\\"\n\r");
22689 if (p == NULL) /* out of memory */
22690 break;
22691 for (t = p; *t != NUL; ++t)
22692 if (*t == '\n')
22693 *t = 'n';
22694 else if (*t == '\r')
22695 *t = 'r';
22696 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000022697 this_var->di_key,
22698 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22699 : ' ',
22700 p,
22701 (this_var->di_tv.v_type == VAR_STRING) ? '"'
22702 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000022703 || put_eol(fd) == FAIL)
22704 {
22705 vim_free(p);
22706 return FAIL;
22707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 vim_free(p);
22709 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022710#ifdef FEAT_FLOAT
22711 else if (this_var->di_tv.v_type == VAR_FLOAT
22712 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
22713 {
22714 float_T f = this_var->di_tv.vval.v_float;
22715 int sign = ' ';
22716
22717 if (f < 0)
22718 {
22719 f = -f;
22720 sign = '-';
22721 }
22722 if ((fprintf(fd, "let %s = %c&%f",
22723 this_var->di_key, sign, f) < 0)
22724 || put_eol(fd) == FAIL)
22725 return FAIL;
22726 }
22727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 }
22729 }
22730 return OK;
22731}
22732#endif
22733
Bram Moolenaar661b1822005-07-28 22:36:45 +000022734/*
22735 * Display script name where an item was last set.
22736 * Should only be invoked when 'verbose' is non-zero.
22737 */
22738 void
22739last_set_msg(scriptID)
22740 scid_T scriptID;
22741{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022742 char_u *p;
22743
Bram Moolenaar661b1822005-07-28 22:36:45 +000022744 if (scriptID != 0)
22745 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022746 p = home_replace_save(NULL, get_scriptname(scriptID));
22747 if (p != NULL)
22748 {
22749 verbose_enter();
22750 MSG_PUTS(_("\n\tLast set from "));
22751 MSG_PUTS(p);
22752 vim_free(p);
22753 verbose_leave();
22754 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000022755 }
22756}
22757
Bram Moolenaard812df62008-11-09 12:46:09 +000022758/*
22759 * List v:oldfiles in a nice way.
22760 */
Bram Moolenaard812df62008-11-09 12:46:09 +000022761 void
22762ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000022763 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000022764{
22765 list_T *l = vimvars[VV_OLDFILES].vv_list;
22766 listitem_T *li;
22767 int nr = 0;
22768
22769 if (l == NULL)
22770 msg((char_u *)_("No old files"));
22771 else
22772 {
22773 msg_start();
22774 msg_scroll = TRUE;
22775 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
22776 {
22777 msg_outnum((long)++nr);
22778 MSG_PUTS(": ");
22779 msg_outtrans(get_tv_string(&li->li_tv));
22780 msg_putchar('\n');
22781 out_flush(); /* output one line at a time */
22782 ui_breakcheck();
22783 }
22784 /* Assume "got_int" was set to truncate the listing. */
22785 got_int = FALSE;
22786
22787#ifdef FEAT_BROWSE_CMD
22788 if (cmdmod.browse)
22789 {
22790 quit_more = FALSE;
22791 nr = prompt_for_number(FALSE);
22792 msg_starthere();
22793 if (nr > 0)
22794 {
22795 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
22796 (long)nr);
22797
22798 if (p != NULL)
22799 {
22800 p = expand_env_save(p);
22801 eap->arg = p;
22802 eap->cmdidx = CMD_edit;
22803 cmdmod.browse = FALSE;
22804 do_exedit(eap, NULL);
22805 vim_free(p);
22806 }
22807 }
22808 }
22809#endif
22810 }
22811}
22812
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813#endif /* FEAT_EVAL */
22814
Bram Moolenaar071d4272004-06-13 20:20:40 +000022815
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022816#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817
22818#ifdef WIN3264
22819/*
22820 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22821 */
22822static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22823static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
22824static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
22825
22826/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022827 * Get the short path (8.3) for the filename in "fnamep".
22828 * Only works for a valid file name.
22829 * When the path gets longer "fnamep" is changed and the allocated buffer
22830 * is put in "bufp".
22831 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22832 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833 */
22834 static int
22835get_short_pathname(fnamep, bufp, fnamelen)
22836 char_u **fnamep;
22837 char_u **bufp;
22838 int *fnamelen;
22839{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022840 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841 char_u *newbuf;
22842
22843 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 l = GetShortPathName(*fnamep, *fnamep, len);
22845 if (l > len - 1)
22846 {
22847 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022848 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849 newbuf = vim_strnsave(*fnamep, l);
22850 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022851 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852
22853 vim_free(*bufp);
22854 *fnamep = *bufp = newbuf;
22855
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022856 /* Really should always succeed, as the buffer is big enough. */
22857 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858 }
22859
22860 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022861 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862}
22863
22864/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022865 * Get the short path (8.3) for the filename in "fname". The converted
22866 * path is returned in "bufp".
22867 *
22868 * Some of the directories specified in "fname" may not exist. This function
22869 * will shorten the existing directories at the beginning of the path and then
22870 * append the remaining non-existing path.
22871 *
22872 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022873 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022874 * bufp - Pointer to an allocated buffer for the filename.
22875 * fnamelen - Length of the filename pointed to by fname
22876 *
22877 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 */
22879 static int
22880shortpath_for_invalid_fname(fname, bufp, fnamelen)
22881 char_u **fname;
22882 char_u **bufp;
22883 int *fnamelen;
22884{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022885 char_u *short_fname, *save_fname, *pbuf_unused;
22886 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022888 int old_len, len;
22889 int new_len, sfx_len;
22890 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891
22892 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022893 old_len = *fnamelen;
22894 save_fname = vim_strnsave(*fname, old_len);
22895 pbuf_unused = NULL;
22896 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022898 endp = save_fname + old_len - 1; /* Find the end of the copy */
22899 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022901 /*
22902 * Try shortening the supplied path till it succeeds by removing one
22903 * directory at a time from the tail of the path.
22904 */
22905 len = 0;
22906 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022907 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022908 /* go back one path-separator */
22909 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
22910 --endp;
22911 if (endp <= save_fname)
22912 break; /* processed the complete path */
22913
22914 /*
22915 * Replace the path separator with a NUL and try to shorten the
22916 * resulting path.
22917 */
22918 ch = *endp;
22919 *endp = 0;
22920 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000022921 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022922 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
22923 {
22924 retval = FAIL;
22925 goto theend;
22926 }
22927 *endp = ch; /* preserve the string */
22928
22929 if (len > 0)
22930 break; /* successfully shortened the path */
22931
22932 /* failed to shorten the path. Skip the path separator */
22933 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022934 }
22935
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022936 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022938 /*
22939 * Succeeded in shortening the path. Now concatenate the shortened
22940 * path with the remaining path at the tail.
22941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022943 /* Compute the length of the new path. */
22944 sfx_len = (int)(save_endp - endp) + 1;
22945 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022947 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022948 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022949 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022951 /* There is not enough space in the currently allocated string,
22952 * copy it to a buffer big enough. */
22953 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022955 {
22956 retval = FAIL;
22957 goto theend;
22958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022959 }
22960 else
22961 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022962 /* Transfer short_fname to the main buffer (it's big enough),
22963 * unless get_short_pathname() did its work in-place. */
22964 *fname = *bufp = save_fname;
22965 if (short_fname != save_fname)
22966 vim_strncpy(save_fname, short_fname, len);
22967 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022969
22970 /* concat the not-shortened part of the path */
22971 vim_strncpy(*fname + len, endp, sfx_len);
22972 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022974
22975theend:
22976 vim_free(pbuf_unused);
22977 vim_free(save_fname);
22978
22979 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980}
22981
22982/*
22983 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000022984 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022985 */
22986 static int
22987shortpath_for_partial(fnamep, bufp, fnamelen)
22988 char_u **fnamep;
22989 char_u **bufp;
22990 int *fnamelen;
22991{
22992 int sepcount, len, tflen;
22993 char_u *p;
22994 char_u *pbuf, *tfname;
22995 int hasTilde;
22996
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022997 /* Count up the path separators from the RHS.. so we know which part
22998 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022999 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023000 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023001 if (vim_ispathsep(*p))
23002 ++sepcount;
23003
23004 /* Need full path first (use expand_env() to remove a "~/") */
23005 hasTilde = (**fnamep == '~');
23006 if (hasTilde)
23007 pbuf = tfname = expand_env_save(*fnamep);
23008 else
23009 pbuf = tfname = FullName_save(*fnamep, FALSE);
23010
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023011 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023013 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23014 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015
23016 if (len == 0)
23017 {
23018 /* Don't have a valid filename, so shorten the rest of the
23019 * path if we can. This CAN give us invalid 8.3 filenames, but
23020 * there's not a lot of point in guessing what it might be.
23021 */
23022 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023023 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23024 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 }
23026
23027 /* Count the paths backward to find the beginning of the desired string. */
23028 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023029 {
23030#ifdef FEAT_MBYTE
23031 if (has_mbyte)
23032 p -= mb_head_off(tfname, p);
23033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 if (vim_ispathsep(*p))
23035 {
23036 if (sepcount == 0 || (hasTilde && sepcount == 1))
23037 break;
23038 else
23039 sepcount --;
23040 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042 if (hasTilde)
23043 {
23044 --p;
23045 if (p >= tfname)
23046 *p = '~';
23047 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023048 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 }
23050 else
23051 ++p;
23052
23053 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23054 vim_free(*bufp);
23055 *fnamelen = (int)STRLEN(p);
23056 *bufp = pbuf;
23057 *fnamep = p;
23058
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023059 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060}
23061#endif /* WIN3264 */
23062
23063/*
23064 * Adjust a filename, according to a string of modifiers.
23065 * *fnamep must be NUL terminated when called. When returning, the length is
23066 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023067 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 * When there is an error, *fnamep is set to NULL.
23069 */
23070 int
23071modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23072 char_u *src; /* string with modifiers */
23073 int *usedlen; /* characters after src that are used */
23074 char_u **fnamep; /* file name so far */
23075 char_u **bufp; /* buffer for allocated file name or NULL */
23076 int *fnamelen; /* length of fnamep */
23077{
23078 int valid = 0;
23079 char_u *tail;
23080 char_u *s, *p, *pbuf;
23081 char_u dirname[MAXPATHL];
23082 int c;
23083 int has_fullname = 0;
23084#ifdef WIN3264
23085 int has_shortname = 0;
23086#endif
23087
23088repeat:
23089 /* ":p" - full path/file_name */
23090 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23091 {
23092 has_fullname = 1;
23093
23094 valid |= VALID_PATH;
23095 *usedlen += 2;
23096
23097 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23098 if ((*fnamep)[0] == '~'
23099#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23100 && ((*fnamep)[1] == '/'
23101# ifdef BACKSLASH_IN_FILENAME
23102 || (*fnamep)[1] == '\\'
23103# endif
23104 || (*fnamep)[1] == NUL)
23105
23106#endif
23107 )
23108 {
23109 *fnamep = expand_env_save(*fnamep);
23110 vim_free(*bufp); /* free any allocated file name */
23111 *bufp = *fnamep;
23112 if (*fnamep == NULL)
23113 return -1;
23114 }
23115
23116 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023117 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 {
23119 if (vim_ispathsep(*p)
23120 && p[1] == '.'
23121 && (p[2] == NUL
23122 || vim_ispathsep(p[2])
23123 || (p[2] == '.'
23124 && (p[3] == NUL || vim_ispathsep(p[3])))))
23125 break;
23126 }
23127
23128 /* FullName_save() is slow, don't use it when not needed. */
23129 if (*p != NUL || !vim_isAbsName(*fnamep))
23130 {
23131 *fnamep = FullName_save(*fnamep, *p != NUL);
23132 vim_free(*bufp); /* free any allocated file name */
23133 *bufp = *fnamep;
23134 if (*fnamep == NULL)
23135 return -1;
23136 }
23137
23138 /* Append a path separator to a directory. */
23139 if (mch_isdir(*fnamep))
23140 {
23141 /* Make room for one or two extra characters. */
23142 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23143 vim_free(*bufp); /* free any allocated file name */
23144 *bufp = *fnamep;
23145 if (*fnamep == NULL)
23146 return -1;
23147 add_pathsep(*fnamep);
23148 }
23149 }
23150
23151 /* ":." - path relative to the current directory */
23152 /* ":~" - path relative to the home directory */
23153 /* ":8" - shortname path - postponed till after */
23154 while (src[*usedlen] == ':'
23155 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23156 {
23157 *usedlen += 2;
23158 if (c == '8')
23159 {
23160#ifdef WIN3264
23161 has_shortname = 1; /* Postpone this. */
23162#endif
23163 continue;
23164 }
23165 pbuf = NULL;
23166 /* Need full path first (use expand_env() to remove a "~/") */
23167 if (!has_fullname)
23168 {
23169 if (c == '.' && **fnamep == '~')
23170 p = pbuf = expand_env_save(*fnamep);
23171 else
23172 p = pbuf = FullName_save(*fnamep, FALSE);
23173 }
23174 else
23175 p = *fnamep;
23176
23177 has_fullname = 0;
23178
23179 if (p != NULL)
23180 {
23181 if (c == '.')
23182 {
23183 mch_dirname(dirname, MAXPATHL);
23184 s = shorten_fname(p, dirname);
23185 if (s != NULL)
23186 {
23187 *fnamep = s;
23188 if (pbuf != NULL)
23189 {
23190 vim_free(*bufp); /* free any allocated file name */
23191 *bufp = pbuf;
23192 pbuf = NULL;
23193 }
23194 }
23195 }
23196 else
23197 {
23198 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
23199 /* Only replace it when it starts with '~' */
23200 if (*dirname == '~')
23201 {
23202 s = vim_strsave(dirname);
23203 if (s != NULL)
23204 {
23205 *fnamep = s;
23206 vim_free(*bufp);
23207 *bufp = s;
23208 }
23209 }
23210 }
23211 vim_free(pbuf);
23212 }
23213 }
23214
23215 tail = gettail(*fnamep);
23216 *fnamelen = (int)STRLEN(*fnamep);
23217
23218 /* ":h" - head, remove "/file_name", can be repeated */
23219 /* Don't remove the first "/" or "c:\" */
23220 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
23221 {
23222 valid |= VALID_HEAD;
23223 *usedlen += 2;
23224 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023225 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023226 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 *fnamelen = (int)(tail - *fnamep);
23228#ifdef VMS
23229 if (*fnamelen > 0)
23230 *fnamelen += 1; /* the path separator is part of the path */
23231#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000023232 if (*fnamelen == 0)
23233 {
23234 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
23235 p = vim_strsave((char_u *)".");
23236 if (p == NULL)
23237 return -1;
23238 vim_free(*bufp);
23239 *bufp = *fnamep = tail = p;
23240 *fnamelen = 1;
23241 }
23242 else
23243 {
23244 while (tail > s && !after_pathsep(s, tail))
23245 mb_ptr_back(*fnamep, tail);
23246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247 }
23248
23249 /* ":8" - shortname */
23250 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
23251 {
23252 *usedlen += 2;
23253#ifdef WIN3264
23254 has_shortname = 1;
23255#endif
23256 }
23257
23258#ifdef WIN3264
23259 /* Check shortname after we have done 'heads' and before we do 'tails'
23260 */
23261 if (has_shortname)
23262 {
23263 pbuf = NULL;
23264 /* Copy the string if it is shortened by :h */
23265 if (*fnamelen < (int)STRLEN(*fnamep))
23266 {
23267 p = vim_strnsave(*fnamep, *fnamelen);
23268 if (p == 0)
23269 return -1;
23270 vim_free(*bufp);
23271 *bufp = *fnamep = p;
23272 }
23273
23274 /* Split into two implementations - makes it easier. First is where
23275 * there isn't a full name already, second is where there is.
23276 */
23277 if (!has_fullname && !vim_isAbsName(*fnamep))
23278 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023279 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 return -1;
23281 }
23282 else
23283 {
23284 int l;
23285
23286 /* Simple case, already have the full-name
23287 * Nearly always shorter, so try first time. */
23288 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023289 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 return -1;
23291
23292 if (l == 0)
23293 {
23294 /* Couldn't find the filename.. search the paths.
23295 */
23296 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023297 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298 return -1;
23299 }
23300 *fnamelen = l;
23301 }
23302 }
23303#endif /* WIN3264 */
23304
23305 /* ":t" - tail, just the basename */
23306 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
23307 {
23308 *usedlen += 2;
23309 *fnamelen -= (int)(tail - *fnamep);
23310 *fnamep = tail;
23311 }
23312
23313 /* ":e" - extension, can be repeated */
23314 /* ":r" - root, without extension, can be repeated */
23315 while (src[*usedlen] == ':'
23316 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
23317 {
23318 /* find a '.' in the tail:
23319 * - for second :e: before the current fname
23320 * - otherwise: The last '.'
23321 */
23322 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
23323 s = *fnamep - 2;
23324 else
23325 s = *fnamep + *fnamelen - 1;
23326 for ( ; s > tail; --s)
23327 if (s[0] == '.')
23328 break;
23329 if (src[*usedlen + 1] == 'e') /* :e */
23330 {
23331 if (s > tail)
23332 {
23333 *fnamelen += (int)(*fnamep - (s + 1));
23334 *fnamep = s + 1;
23335#ifdef VMS
23336 /* cut version from the extension */
23337 s = *fnamep + *fnamelen - 1;
23338 for ( ; s > *fnamep; --s)
23339 if (s[0] == ';')
23340 break;
23341 if (s > *fnamep)
23342 *fnamelen = s - *fnamep;
23343#endif
23344 }
23345 else if (*fnamep <= tail)
23346 *fnamelen = 0;
23347 }
23348 else /* :r */
23349 {
23350 if (s > tail) /* remove one extension */
23351 *fnamelen = (int)(s - *fnamep);
23352 }
23353 *usedlen += 2;
23354 }
23355
23356 /* ":s?pat?foo?" - substitute */
23357 /* ":gs?pat?foo?" - global substitute */
23358 if (src[*usedlen] == ':'
23359 && (src[*usedlen + 1] == 's'
23360 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
23361 {
23362 char_u *str;
23363 char_u *pat;
23364 char_u *sub;
23365 int sep;
23366 char_u *flags;
23367 int didit = FALSE;
23368
23369 flags = (char_u *)"";
23370 s = src + *usedlen + 2;
23371 if (src[*usedlen + 1] == 'g')
23372 {
23373 flags = (char_u *)"g";
23374 ++s;
23375 }
23376
23377 sep = *s++;
23378 if (sep)
23379 {
23380 /* find end of pattern */
23381 p = vim_strchr(s, sep);
23382 if (p != NULL)
23383 {
23384 pat = vim_strnsave(s, (int)(p - s));
23385 if (pat != NULL)
23386 {
23387 s = p + 1;
23388 /* find end of substitution */
23389 p = vim_strchr(s, sep);
23390 if (p != NULL)
23391 {
23392 sub = vim_strnsave(s, (int)(p - s));
23393 str = vim_strnsave(*fnamep, *fnamelen);
23394 if (sub != NULL && str != NULL)
23395 {
23396 *usedlen = (int)(p + 1 - src);
23397 s = do_string_sub(str, pat, sub, flags);
23398 if (s != NULL)
23399 {
23400 *fnamep = s;
23401 *fnamelen = (int)STRLEN(s);
23402 vim_free(*bufp);
23403 *bufp = s;
23404 didit = TRUE;
23405 }
23406 }
23407 vim_free(sub);
23408 vim_free(str);
23409 }
23410 vim_free(pat);
23411 }
23412 }
23413 /* after using ":s", repeat all the modifiers */
23414 if (didit)
23415 goto repeat;
23416 }
23417 }
23418
23419 return valid;
23420}
23421
23422/*
23423 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
23424 * "flags" can be "g" to do a global substitute.
23425 * Returns an allocated string, NULL for error.
23426 */
23427 char_u *
23428do_string_sub(str, pat, sub, flags)
23429 char_u *str;
23430 char_u *pat;
23431 char_u *sub;
23432 char_u *flags;
23433{
23434 int sublen;
23435 regmatch_T regmatch;
23436 int i;
23437 int do_all;
23438 char_u *tail;
23439 garray_T ga;
23440 char_u *ret;
23441 char_u *save_cpo;
23442
23443 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
23444 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023445 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446
23447 ga_init2(&ga, 1, 200);
23448
23449 do_all = (flags[0] == 'g');
23450
23451 regmatch.rm_ic = p_ic;
23452 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
23453 if (regmatch.regprog != NULL)
23454 {
23455 tail = str;
23456 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
23457 {
23458 /*
23459 * Get some space for a temporary buffer to do the substitution
23460 * into. It will contain:
23461 * - The text up to where the match is.
23462 * - The substituted text.
23463 * - The text after the match.
23464 */
23465 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
23466 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
23467 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
23468 {
23469 ga_clear(&ga);
23470 break;
23471 }
23472
23473 /* copy the text up to where the match is */
23474 i = (int)(regmatch.startp[0] - tail);
23475 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
23476 /* add the substituted text */
23477 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
23478 + ga.ga_len + i, TRUE, TRUE, FALSE);
23479 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 /* avoid getting stuck on a match with an empty string */
23481 if (tail == regmatch.endp[0])
23482 {
23483 if (*tail == NUL)
23484 break;
23485 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
23486 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023487 }
23488 else
23489 {
23490 tail = regmatch.endp[0];
23491 if (*tail == NUL)
23492 break;
23493 }
23494 if (!do_all)
23495 break;
23496 }
23497
23498 if (ga.ga_data != NULL)
23499 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
23500
23501 vim_free(regmatch.regprog);
23502 }
23503
23504 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
23505 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000023506 if (p_cpo == empty_option)
23507 p_cpo = save_cpo;
23508 else
23509 /* Darn, evaluating {sub} expression changed the value. */
23510 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511
23512 return ret;
23513}
23514
23515#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */